1 /* $NetBSD: rpc_generic.c,v 1.4 2000/09/28 09:07:04 kleink Exp $ */
4 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34 /* #pragma ident "@(#)rpc_generic.c 1.17 94/04/24 SMI" */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * rpc_generic.c, Miscl routines for RPC.
43 #include "namespace.h"
44 #include "reentrant.h"
45 #include <sys/param.h>
46 #include <sys/socket.h>
49 #include <sys/resource.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
57 #include <netconfig.h>
61 #include <rpc/nettype.h>
62 #include "un-namespace.h"
67 NCONF_HANDLE *nhandle;
68 int nflag; /* Whether NETPATH or NETCONFIG */
72 static const struct _rpcnettype {
76 { "netpath", _RPC_NETPATH },
77 { "visible", _RPC_VISIBLE },
78 { "circuit_v", _RPC_CIRCUIT_V },
79 { "datagram_v", _RPC_DATAGRAM_V },
80 { "circuit_n", _RPC_CIRCUIT_N },
81 { "datagram_n", _RPC_DATAGRAM_N },
93 static const struct netid_af na_cvt[] = {
94 { "udp", AF_INET, IPPROTO_UDP },
95 { "tcp", AF_INET, IPPROTO_TCP },
97 { "udp6", AF_INET6, IPPROTO_UDP },
98 { "tcp6", AF_INET6, IPPROTO_TCP },
100 { "local", AF_LOCAL, 0 }
104 static char *strlocase(char *);
106 static int getnettype(const char *);
109 * Cache the result of getrlimit(), so we don't have to do an
110 * expensive call every time.
121 if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
122 return (tbsize = (int)rl.rlim_max);
125 * Something wrong. I'll try to save face by returning a
126 * pessimistic number.
133 * Find the appropriate buffer size
135 * size - Size requested
139 __rpc_get_t_size(int af, int proto, int size)
141 int maxsize, defsize;
143 maxsize = 256 * 1024; /* XXX */
146 defsize = 64 * 1024; /* XXX */
149 defsize = UDPMSGSIZE;
152 defsize = RPC_MAXDATASIZE;
158 /* Check whether the value is within the upper max limit */
159 return (size > maxsize ? (u_int)maxsize : (u_int)size);
163 * Find the appropriate address buffer size
166 __rpc_get_a_size(int af)
170 return sizeof (struct sockaddr_in);
173 return sizeof (struct sockaddr_in6);
176 return sizeof (struct sockaddr_un);
180 return ((u_int)RPC_MAXADDRSIZE);
197 * Returns the type of the network as defined in <rpc/nettype.h>
198 * If nettype is NULL, it defaults to NETPATH.
201 getnettype(const char *nettype)
205 if ((nettype == NULL) || (nettype[0] == 0)) {
206 return (_RPC_NETPATH); /* Default */
210 nettype = strlocase(nettype);
212 for (i = 0; _rpctypelist[i].name; i++)
213 if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
214 return (_rpctypelist[i].type);
216 return (_rpctypelist[i].type);
219 static thread_key_t tcp_key, udp_key;
220 static once_t keys_once = ONCE_INITIALIZER;
221 static int tcp_key_error, udp_key_error;
227 tcp_key_error = thr_keycreate(&tcp_key, free);
228 udp_key_error = thr_keycreate(&udp_key, free);
232 * For the given nettype (tcp or udp only), return the first structure found.
233 * This should be freed by calling freenetconfigent()
236 __rpc_getconfip(const char *nettype)
239 char *netid_tcp = (char *) NULL;
240 char *netid_udp = (char *) NULL;
241 static char *netid_tcp_main;
242 static char *netid_udp_main;
243 struct netconfig *dummy;
246 if ((main_thread = thr_main())) {
247 netid_udp = netid_udp_main;
248 netid_tcp = netid_tcp_main;
250 if (thr_once(&keys_once, keys_init) != 0 ||
251 tcp_key_error != 0 || udp_key_error != 0)
253 netid_tcp = (char *)thr_getspecific(tcp_key);
254 netid_udp = (char *)thr_getspecific(udp_key);
256 if (!netid_udp && !netid_tcp) {
257 struct netconfig *nconf;
260 if (!(confighandle = setnetconfig())) {
261 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
264 while ((nconf = getnetconfig(confighandle)) != NULL) {
265 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
266 if (strcmp(nconf->nc_proto, NC_TCP) == 0 &&
268 netid_tcp = strdup(nconf->nc_netid);
270 netid_tcp_main = netid_tcp;
272 thr_setspecific(tcp_key,
275 if (strcmp(nconf->nc_proto, NC_UDP) == 0 &&
277 netid_udp = strdup(nconf->nc_netid);
279 netid_udp_main = netid_udp;
281 thr_setspecific(udp_key,
286 endnetconfig(confighandle);
288 if (strcmp(nettype, "udp") == 0)
290 else if (strcmp(nettype, "tcp") == 0)
295 if ((netid == NULL) || (netid[0] == 0)) {
298 dummy = getnetconfigent(netid);
303 * Returns the type of the nettype, which should then be used with
307 __rpc_setconf(const char *nettype)
309 struct handle *handle;
311 handle = (struct handle *) malloc(sizeof (struct handle));
312 if (handle == NULL) {
315 switch (handle->nettype = getnettype(nettype)) {
318 case _RPC_DATAGRAM_N:
319 if (!(handle->nhandle = setnetpath()))
321 handle->nflag = TRUE;
325 case _RPC_DATAGRAM_V:
328 if (!(handle->nhandle = setnetconfig())) {
329 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
332 handle->nflag = FALSE;
346 * Returns the next netconfig struct for the given "net" type.
347 * __rpc_setconf() should have been called previously.
350 __rpc_getconf(void *vhandle)
352 struct handle *handle;
353 struct netconfig *nconf;
355 handle = (struct handle *)vhandle;
356 if (handle == NULL) {
361 nconf = getnetpath(handle->nhandle);
363 nconf = getnetconfig(handle->nhandle);
366 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
367 (nconf->nc_semantics != NC_TPI_COTS) &&
368 (nconf->nc_semantics != NC_TPI_COTS_ORD))
370 switch (handle->nettype) {
372 if (!(nconf->nc_flag & NC_VISIBLE))
375 case _RPC_NETPATH: /* Be happy */
378 if (!(nconf->nc_flag & NC_VISIBLE))
382 if ((nconf->nc_semantics != NC_TPI_COTS) &&
383 (nconf->nc_semantics != NC_TPI_COTS_ORD))
386 case _RPC_DATAGRAM_V:
387 if (!(nconf->nc_flag & NC_VISIBLE))
390 case _RPC_DATAGRAM_N:
391 if (nconf->nc_semantics != NC_TPI_CLTS)
395 if (((nconf->nc_semantics != NC_TPI_COTS) &&
396 (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
397 (strcmp(nconf->nc_protofmly, NC_INET)
399 && strcmp(nconf->nc_protofmly, NC_INET6))
404 strcmp(nconf->nc_proto, NC_TCP))
408 if ((nconf->nc_semantics != NC_TPI_CLTS) ||
409 (strcmp(nconf->nc_protofmly, NC_INET)
411 && strcmp(nconf->nc_protofmly, NC_INET6))
416 strcmp(nconf->nc_proto, NC_UDP))
426 __rpc_endconf(void *vhandle)
428 struct handle *handle;
430 handle = (struct handle *) vhandle;
431 if (handle == NULL) {
435 endnetpath(handle->nhandle);
437 endnetconfig(handle->nhandle);
443 * Used to ping the NULL procedure for clnt handle.
444 * Returns NULL if fails, else a non-NULL pointer.
447 rpc_nullproc(CLIENT *clnt)
449 struct timeval TIMEOUT = {25, 0};
451 if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL,
452 (xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
455 return ((void *) clnt);
459 * Try all possible transports until
460 * one succeeds in finding the netconf for the given fd.
466 struct __rpc_sockinfo si;
468 if (!__rpc_fd2sockinfo(fd, &si))
471 if (!__rpc_sockinfo2netid(&si, &netid))
474 /*LINTED const castaway*/
475 return getnetconfigent((char *)netid);
479 __rpc_fd2sockinfo(int fd, struct __rpc_sockinfo *sip)
483 struct sockaddr_storage ss;
486 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &len) < 0)
491 if (_getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &len) < 0)
495 if (ss.ss_family != AF_LOCAL) {
496 if (type == SOCK_STREAM)
498 else if (type == SOCK_DGRAM)
505 sip->si_af = ss.ss_family;
506 sip->si_proto = proto;
507 sip->si_socktype = type;
513 * Linear search, but the number of entries is small.
516 __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip)
520 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
521 if (strcmp(na_cvt[i].netid, nconf->nc_netid) == 0 || (
522 strcmp(nconf->nc_netid, "unix") == 0 &&
523 strcmp(na_cvt[i].netid, "local") == 0)) {
524 sip->si_af = na_cvt[i].af;
525 sip->si_proto = na_cvt[i].protocol;
527 __rpc_seman2socktype((int)nconf->nc_semantics);
528 if (sip->si_socktype == -1)
530 sip->si_alen = __rpc_get_a_size(sip->si_af);
538 __rpc_nconf2fd(const struct netconfig *nconf)
540 struct __rpc_sockinfo si;
542 if (!__rpc_nconf2sockinfo(nconf, &si))
545 return _socket(si.si_af, si.si_socktype, si.si_proto);
549 __rpc_sockinfo2netid(struct __rpc_sockinfo *sip, const char **netid)
552 struct netconfig *nconf;
554 nconf = getnetconfigent("local");
556 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++) {
557 if (na_cvt[i].af == sip->si_af &&
558 na_cvt[i].protocol == sip->si_proto) {
559 if (strcmp(na_cvt[i].netid, "local") == 0 && nconf == NULL) {
564 *netid = na_cvt[i].netid;
567 freenetconfigent(nconf);
572 freenetconfigent(nconf);
578 taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
580 struct __rpc_sockinfo si;
582 if (!__rpc_nconf2sockinfo(nconf, &si))
584 return __rpc_taddr2uaddr_af(si.si_af, nbuf);
588 uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
590 struct __rpc_sockinfo si;
592 if (!__rpc_nconf2sockinfo(nconf, &si))
594 return __rpc_uaddr2taddr_af(si.si_af, uaddr);
598 __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
601 struct sockaddr_in *sin;
602 struct sockaddr_un *sun;
603 char namebuf[INET_ADDRSTRLEN];
605 struct sockaddr_in6 *sin6;
606 char namebuf6[INET6_ADDRSTRLEN];
612 if (nbuf->len < sizeof(*sin))
615 if (inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
618 port = ntohs(sin->sin_port);
619 if (asprintf(&ret, "%s.%u.%u", namebuf, ((u_int32_t)port) >> 8,
625 if (nbuf->len < sizeof(*sin6))
628 if (inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
631 port = ntohs(sin6->sin6_port);
632 if (asprintf(&ret, "%s.%u.%u", namebuf6, ((u_int32_t)port) >> 8,
639 if (asprintf(&ret, "%.*s", (int)(sun->sun_len -
640 offsetof(struct sockaddr_un, sun_path)),
652 __rpc_uaddr2taddr_af(int af, const char *uaddr)
654 struct netbuf *ret = NULL;
656 unsigned port, portlo, porthi;
657 struct sockaddr_in *sin;
659 struct sockaddr_in6 *sin6;
661 struct sockaddr_un *sun;
669 addrstr = strdup(uaddr);
674 * AF_LOCAL addresses are expected to be absolute
675 * pathnames, anything else will be AF_INET or AF_INET6.
677 if (*addrstr != '/') {
678 p = strrchr(addrstr, '.');
681 portlo = (unsigned)atoi(p + 1);
684 p = strrchr(addrstr, '.');
687 porthi = (unsigned)atoi(p + 1);
689 port = (porthi << 8) | portlo;
692 ret = (struct netbuf *)malloc(sizeof *ret);
698 sin = (struct sockaddr_in *)malloc(sizeof *sin);
701 memset(sin, 0, sizeof *sin);
702 sin->sin_family = AF_INET;
703 sin->sin_port = htons(port);
704 if (inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) {
710 sin->sin_len = ret->maxlen = ret->len = sizeof *sin;
715 sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6);
718 memset(sin6, 0, sizeof *sin6);
719 sin6->sin6_family = AF_INET6;
720 sin6->sin6_port = htons(port);
721 if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) {
727 sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6;
732 sun = (struct sockaddr_un *)malloc(sizeof *sun);
735 memset(sun, 0, sizeof *sun);
736 sun->sun_family = AF_LOCAL;
737 strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1);
738 ret->len = ret->maxlen = sun->sun_len = SUN_LEN(sun);
750 __rpc_seman2socktype(int semantics)
755 case NC_TPI_COTS_ORD:
767 __rpc_socktype2seman(int socktype)
773 return NC_TPI_COTS_ORD;
784 * XXXX - IPv6 scope IDs can't be handled in universal addresses.
785 * Here, we compare the original server address to that of the RPC
786 * service we just received back from a call to rpcbind on the remote
787 * machine. If they are both "link local" or "site local", copy
788 * the scope id of the server address over to the service address.
791 __rpc_fixup_addr(struct netbuf *new, const struct netbuf *svc)
794 struct sockaddr *sa_new, *sa_svc;
795 struct sockaddr_in6 *sin6_new, *sin6_svc;
797 sa_svc = (struct sockaddr *)svc->buf;
798 sa_new = (struct sockaddr *)new->buf;
800 if (sa_new->sa_family == sa_svc->sa_family &&
801 sa_new->sa_family == AF_INET6) {
802 sin6_new = (struct sockaddr_in6 *)new->buf;
803 sin6_svc = (struct sockaddr_in6 *)svc->buf;
805 if ((IN6_IS_ADDR_LINKLOCAL(&sin6_new->sin6_addr) &&
806 IN6_IS_ADDR_LINKLOCAL(&sin6_svc->sin6_addr)) ||
807 (IN6_IS_ADDR_SITELOCAL(&sin6_new->sin6_addr) &&
808 IN6_IS_ADDR_SITELOCAL(&sin6_svc->sin6_addr))) {
809 sin6_new->sin6_scope_id = sin6_svc->sin6_scope_id;
817 __rpc_sockisbound(int fd)
819 struct sockaddr_storage ss;
822 slen = sizeof (struct sockaddr_storage);
823 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0)
826 switch (ss.ss_family) {
828 return (((struct sockaddr_in *)
829 (void *)&ss)->sin_port != 0);
832 return (((struct sockaddr_in6 *)
833 (void *)&ss)->sin6_port != 0);
837 return (((struct sockaddr_un *)
838 (void *)&ss)->sun_path[0] != '\0');