1 /* $NetBSD: rpc_generic.c,v 1.4 2000/09/28 09:07:04 kleink Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2009, Sun Microsystems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
36 /* #pragma ident "@(#)rpc_generic.c 1.17 94/04/24 SMI" */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * rpc_generic.c, Miscl routines for RPC.
45 #include "namespace.h"
46 #include "reentrant.h"
47 #include <sys/param.h>
48 #include <sys/socket.h>
51 #include <sys/resource.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
59 #include <netconfig.h>
63 #include <rpc/nettype.h>
64 #include "un-namespace.h"
69 NCONF_HANDLE *nhandle;
70 int nflag; /* Whether NETPATH or NETCONFIG */
74 static const struct _rpcnettype {
78 { "netpath", _RPC_NETPATH },
79 { "visible", _RPC_VISIBLE },
80 { "circuit_v", _RPC_CIRCUIT_V },
81 { "datagram_v", _RPC_DATAGRAM_V },
82 { "circuit_n", _RPC_CIRCUIT_N },
83 { "datagram_n", _RPC_DATAGRAM_N },
95 static const struct netid_af na_cvt[] = {
96 { "udp", AF_INET, IPPROTO_UDP },
97 { "tcp", AF_INET, IPPROTO_TCP },
99 { "udp6", AF_INET6, IPPROTO_UDP },
100 { "tcp6", AF_INET6, IPPROTO_TCP },
102 { "local", AF_LOCAL, 0 }
106 static char *strlocase(char *);
108 static int getnettype(const char *);
111 * Cache the result of getrlimit(), so we don't have to do an
112 * expensive call every time.
123 if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
124 return (tbsize = (int)rl.rlim_max);
127 * Something wrong. I'll try to save face by returning a
128 * pessimistic number.
135 * Find the appropriate buffer size
137 * size - Size requested
141 __rpc_get_t_size(int af, int proto, int size)
143 int maxsize, defsize;
145 maxsize = 256 * 1024; /* XXX */
148 defsize = 64 * 1024; /* XXX */
151 defsize = UDPMSGSIZE;
154 defsize = RPC_MAXDATASIZE;
160 /* Check whether the value is within the upper max limit */
161 return (size > maxsize ? (u_int)maxsize : (u_int)size);
165 * Find the appropriate address buffer size
168 __rpc_get_a_size(int af)
172 return sizeof (struct sockaddr_in);
175 return sizeof (struct sockaddr_in6);
178 return sizeof (struct sockaddr_un);
182 return ((u_int)RPC_MAXADDRSIZE);
199 * Returns the type of the network as defined in <rpc/nettype.h>
200 * If nettype is NULL, it defaults to NETPATH.
203 getnettype(const char *nettype)
207 if ((nettype == NULL) || (nettype[0] == 0)) {
208 return (_RPC_NETPATH); /* Default */
212 nettype = strlocase(nettype);
214 for (i = 0; _rpctypelist[i].name; i++)
215 if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
216 return (_rpctypelist[i].type);
218 return (_rpctypelist[i].type);
221 static thread_key_t tcp_key, udp_key;
222 static once_t keys_once = ONCE_INITIALIZER;
223 static int tcp_key_error, udp_key_error;
229 tcp_key_error = thr_keycreate(&tcp_key, free);
230 udp_key_error = thr_keycreate(&udp_key, free);
234 * For the given nettype (tcp or udp only), return the first structure found.
235 * This should be freed by calling freenetconfigent()
238 __rpc_getconfip(const char *nettype)
241 char *netid_tcp = (char *) NULL;
242 char *netid_udp = (char *) NULL;
243 static char *netid_tcp_main;
244 static char *netid_udp_main;
245 struct netconfig *dummy;
248 if ((main_thread = thr_main())) {
249 netid_udp = netid_udp_main;
250 netid_tcp = netid_tcp_main;
252 if (thr_once(&keys_once, keys_init) != 0 ||
253 tcp_key_error != 0 || udp_key_error != 0)
255 netid_tcp = (char *)thr_getspecific(tcp_key);
256 netid_udp = (char *)thr_getspecific(udp_key);
258 if (!netid_udp && !netid_tcp) {
259 struct netconfig *nconf;
262 if (!(confighandle = setnetconfig())) {
263 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
266 while ((nconf = getnetconfig(confighandle)) != NULL) {
267 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
268 if (strcmp(nconf->nc_proto, NC_TCP) == 0 &&
270 netid_tcp = strdup(nconf->nc_netid);
272 netid_tcp_main = netid_tcp;
274 thr_setspecific(tcp_key,
277 if (strcmp(nconf->nc_proto, NC_UDP) == 0 &&
279 netid_udp = strdup(nconf->nc_netid);
281 netid_udp_main = netid_udp;
283 thr_setspecific(udp_key,
288 endnetconfig(confighandle);
290 if (strcmp(nettype, "udp") == 0)
292 else if (strcmp(nettype, "tcp") == 0)
297 if ((netid == NULL) || (netid[0] == 0)) {
300 dummy = getnetconfigent(netid);
305 * Returns the type of the nettype, which should then be used with
309 __rpc_setconf(const char *nettype)
311 struct handle *handle;
313 handle = (struct handle *) malloc(sizeof (struct handle));
314 if (handle == NULL) {
317 switch (handle->nettype = getnettype(nettype)) {
320 case _RPC_DATAGRAM_N:
321 if (!(handle->nhandle = setnetpath()))
323 handle->nflag = TRUE;
327 case _RPC_DATAGRAM_V:
330 if (!(handle->nhandle = setnetconfig())) {
331 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
334 handle->nflag = FALSE;
348 * Returns the next netconfig struct for the given "net" type.
349 * __rpc_setconf() should have been called previously.
352 __rpc_getconf(void *vhandle)
354 struct handle *handle;
355 struct netconfig *nconf;
357 handle = (struct handle *)vhandle;
358 if (handle == NULL) {
363 nconf = getnetpath(handle->nhandle);
365 nconf = getnetconfig(handle->nhandle);
368 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
369 (nconf->nc_semantics != NC_TPI_COTS) &&
370 (nconf->nc_semantics != NC_TPI_COTS_ORD))
372 switch (handle->nettype) {
374 if (!(nconf->nc_flag & NC_VISIBLE))
377 case _RPC_NETPATH: /* Be happy */
380 if (!(nconf->nc_flag & NC_VISIBLE))
384 if ((nconf->nc_semantics != NC_TPI_COTS) &&
385 (nconf->nc_semantics != NC_TPI_COTS_ORD))
388 case _RPC_DATAGRAM_V:
389 if (!(nconf->nc_flag & NC_VISIBLE))
392 case _RPC_DATAGRAM_N:
393 if (nconf->nc_semantics != NC_TPI_CLTS)
397 if (((nconf->nc_semantics != NC_TPI_COTS) &&
398 (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
399 (strcmp(nconf->nc_protofmly, NC_INET)
401 && strcmp(nconf->nc_protofmly, NC_INET6))
406 strcmp(nconf->nc_proto, NC_TCP))
410 if ((nconf->nc_semantics != NC_TPI_CLTS) ||
411 (strcmp(nconf->nc_protofmly, NC_INET)
413 && strcmp(nconf->nc_protofmly, NC_INET6))
418 strcmp(nconf->nc_proto, NC_UDP))
428 __rpc_endconf(void *vhandle)
430 struct handle *handle;
432 handle = (struct handle *) vhandle;
433 if (handle == NULL) {
437 endnetpath(handle->nhandle);
439 endnetconfig(handle->nhandle);
445 * Used to ping the NULL procedure for clnt handle.
446 * Returns NULL if fails, else a non-NULL pointer.
449 rpc_nullproc(CLIENT *clnt)
451 struct timeval TIMEOUT = {25, 0};
453 if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL,
454 (xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
457 return ((void *) clnt);
461 * Try all possible transports until
462 * one succeeds in finding the netconf for the given fd.
468 struct __rpc_sockinfo si;
470 if (!__rpc_fd2sockinfo(fd, &si))
473 if (!__rpc_sockinfo2netid(&si, &netid))
476 /*LINTED const castaway*/
477 return getnetconfigent((char *)netid);
481 __rpc_fd2sockinfo(int fd, struct __rpc_sockinfo *sip)
485 struct sockaddr_storage ss;
488 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &len) < 0)
493 if (_getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &len) < 0)
497 if (ss.ss_family != AF_LOCAL) {
498 if (type == SOCK_STREAM)
500 else if (type == SOCK_DGRAM)
507 sip->si_af = ss.ss_family;
508 sip->si_proto = proto;
509 sip->si_socktype = type;
515 * Linear search, but the number of entries is small.
518 __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip)
522 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
523 if (strcmp(na_cvt[i].netid, nconf->nc_netid) == 0 || (
524 strcmp(nconf->nc_netid, "unix") == 0 &&
525 strcmp(na_cvt[i].netid, "local") == 0)) {
526 sip->si_af = na_cvt[i].af;
527 sip->si_proto = na_cvt[i].protocol;
529 __rpc_seman2socktype((int)nconf->nc_semantics);
530 if (sip->si_socktype == -1)
532 sip->si_alen = __rpc_get_a_size(sip->si_af);
540 __rpc_nconf2fd(const struct netconfig *nconf)
542 struct __rpc_sockinfo si;
544 if (!__rpc_nconf2sockinfo(nconf, &si))
547 return _socket(si.si_af, si.si_socktype, si.si_proto);
551 __rpc_sockinfo2netid(struct __rpc_sockinfo *sip, const char **netid)
554 struct netconfig *nconf;
556 nconf = getnetconfigent("local");
558 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++) {
559 if (na_cvt[i].af == sip->si_af &&
560 na_cvt[i].protocol == sip->si_proto) {
561 if (strcmp(na_cvt[i].netid, "local") == 0 && nconf == NULL) {
566 *netid = na_cvt[i].netid;
569 freenetconfigent(nconf);
574 freenetconfigent(nconf);
580 taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
582 struct __rpc_sockinfo si;
584 if (!__rpc_nconf2sockinfo(nconf, &si))
586 return __rpc_taddr2uaddr_af(si.si_af, nbuf);
590 uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
592 struct __rpc_sockinfo si;
594 if (!__rpc_nconf2sockinfo(nconf, &si))
596 return __rpc_uaddr2taddr_af(si.si_af, uaddr);
600 __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
603 struct sockaddr_in *sin;
604 struct sockaddr_un *sun;
605 char namebuf[INET_ADDRSTRLEN];
607 struct sockaddr_in6 *sin6;
608 char namebuf6[INET6_ADDRSTRLEN];
614 if (nbuf->len < sizeof(*sin))
617 if (inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
620 port = ntohs(sin->sin_port);
621 if (asprintf(&ret, "%s.%u.%u", namebuf, ((u_int32_t)port) >> 8,
627 if (nbuf->len < sizeof(*sin6))
630 if (inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
633 port = ntohs(sin6->sin6_port);
634 if (asprintf(&ret, "%s.%u.%u", namebuf6, ((u_int32_t)port) >> 8,
641 if (asprintf(&ret, "%.*s", (int)(sun->sun_len -
642 offsetof(struct sockaddr_un, sun_path)),
654 __rpc_uaddr2taddr_af(int af, const char *uaddr)
656 struct netbuf *ret = NULL;
658 unsigned port, portlo, porthi;
659 struct sockaddr_in *sin;
661 struct sockaddr_in6 *sin6;
663 struct sockaddr_un *sun;
671 addrstr = strdup(uaddr);
676 * AF_LOCAL addresses are expected to be absolute
677 * pathnames, anything else will be AF_INET or AF_INET6.
679 if (*addrstr != '/') {
680 p = strrchr(addrstr, '.');
683 portlo = (unsigned)atoi(p + 1);
686 p = strrchr(addrstr, '.');
689 porthi = (unsigned)atoi(p + 1);
691 port = (porthi << 8) | portlo;
694 ret = (struct netbuf *)malloc(sizeof *ret);
700 sin = (struct sockaddr_in *)malloc(sizeof *sin);
703 memset(sin, 0, sizeof *sin);
704 sin->sin_family = AF_INET;
705 sin->sin_port = htons(port);
706 if (inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) {
712 sin->sin_len = ret->maxlen = ret->len = sizeof *sin;
717 sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6);
720 memset(sin6, 0, sizeof *sin6);
721 sin6->sin6_family = AF_INET6;
722 sin6->sin6_port = htons(port);
723 if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) {
729 sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6;
734 sun = (struct sockaddr_un *)malloc(sizeof *sun);
737 memset(sun, 0, sizeof *sun);
738 sun->sun_family = AF_LOCAL;
739 strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1);
740 ret->len = ret->maxlen = sun->sun_len = SUN_LEN(sun);
752 __rpc_seman2socktype(int semantics)
757 case NC_TPI_COTS_ORD:
769 __rpc_socktype2seman(int socktype)
775 return NC_TPI_COTS_ORD;
786 * XXXX - IPv6 scope IDs can't be handled in universal addresses.
787 * Here, we compare the original server address to that of the RPC
788 * service we just received back from a call to rpcbind on the remote
789 * machine. If they are both "link local" or "site local", copy
790 * the scope id of the server address over to the service address.
793 __rpc_fixup_addr(struct netbuf *new, const struct netbuf *svc)
796 struct sockaddr *sa_new, *sa_svc;
797 struct sockaddr_in6 *sin6_new, *sin6_svc;
799 sa_svc = (struct sockaddr *)svc->buf;
800 sa_new = (struct sockaddr *)new->buf;
802 if (sa_new->sa_family == sa_svc->sa_family &&
803 sa_new->sa_family == AF_INET6) {
804 sin6_new = (struct sockaddr_in6 *)new->buf;
805 sin6_svc = (struct sockaddr_in6 *)svc->buf;
807 if ((IN6_IS_ADDR_LINKLOCAL(&sin6_new->sin6_addr) &&
808 IN6_IS_ADDR_LINKLOCAL(&sin6_svc->sin6_addr)) ||
809 (IN6_IS_ADDR_SITELOCAL(&sin6_new->sin6_addr) &&
810 IN6_IS_ADDR_SITELOCAL(&sin6_svc->sin6_addr))) {
811 sin6_new->sin6_scope_id = sin6_svc->sin6_scope_id;
819 __rpc_sockisbound(int fd)
821 struct sockaddr_storage ss;
824 slen = sizeof (struct sockaddr_storage);
825 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0)
828 switch (ss.ss_family) {
830 return (((struct sockaddr_in *)
831 (void *)&ss)->sin_port != 0);
834 return (((struct sockaddr_in6 *)
835 (void *)&ss)->sin6_port != 0);
839 return (((struct sockaddr_un *)
840 (void *)&ss)->sun_path[0] != '\0');