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/types.h>
46 #include <sys/param.h>
47 #include <sys/socket.h>
50 #include <sys/resource.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
58 #include <netconfig.h>
62 #include <rpc/nettype.h>
63 #include "un-namespace.h"
68 NCONF_HANDLE *nhandle;
69 int nflag; /* Whether NETPATH or NETCONFIG */
73 static const struct _rpcnettype {
77 { "netpath", _RPC_NETPATH },
78 { "visible", _RPC_VISIBLE },
79 { "circuit_v", _RPC_CIRCUIT_V },
80 { "datagram_v", _RPC_DATAGRAM_V },
81 { "circuit_n", _RPC_CIRCUIT_N },
82 { "datagram_n", _RPC_DATAGRAM_N },
94 static const struct netid_af na_cvt[] = {
95 { "udp", AF_INET, IPPROTO_UDP },
96 { "tcp", AF_INET, IPPROTO_TCP },
98 { "udp6", AF_INET6, IPPROTO_UDP },
99 { "tcp6", AF_INET6, IPPROTO_TCP },
101 { "local", AF_LOCAL, 0 }
105 static char *strlocase(char *);
107 static int getnettype(const char *);
110 * Cache the result of getrlimit(), so we don't have to do an
111 * expensive call every time.
122 if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
123 return (tbsize = (int)rl.rlim_max);
126 * Something wrong. I'll try to save face by returning a
127 * pessimistic number.
134 * Find the appropriate buffer size
136 * size - Size requested
140 __rpc_get_t_size(int af, int proto, int size)
142 int maxsize, defsize;
144 maxsize = 256 * 1024; /* XXX */
147 defsize = 64 * 1024; /* XXX */
150 defsize = UDPMSGSIZE;
153 defsize = RPC_MAXDATASIZE;
159 /* Check whether the value is within the upper max limit */
160 return (size > maxsize ? (u_int)maxsize : (u_int)size);
164 * Find the appropriate address buffer size
167 __rpc_get_a_size(int af)
171 return sizeof (struct sockaddr_in);
174 return sizeof (struct sockaddr_in6);
177 return sizeof (struct sockaddr_un);
181 return ((u_int)RPC_MAXADDRSIZE);
198 * Returns the type of the network as defined in <rpc/nettype.h>
199 * If nettype is NULL, it defaults to NETPATH.
202 getnettype(const char *nettype)
206 if ((nettype == NULL) || (nettype[0] == 0)) {
207 return (_RPC_NETPATH); /* Default */
211 nettype = strlocase(nettype);
213 for (i = 0; _rpctypelist[i].name; i++)
214 if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
215 return (_rpctypelist[i].type);
217 return (_rpctypelist[i].type);
220 static thread_key_t tcp_key, udp_key;
221 static once_t keys_once = ONCE_INITIALIZER;
222 static int tcp_key_error, udp_key_error;
228 tcp_key_error = thr_keycreate(&tcp_key, free);
229 udp_key_error = thr_keycreate(&udp_key, free);
233 * For the given nettype (tcp or udp only), return the first structure found.
234 * This should be freed by calling freenetconfigent()
237 __rpc_getconfip(const char *nettype)
240 char *netid_tcp = (char *) NULL;
241 char *netid_udp = (char *) NULL;
242 static char *netid_tcp_main;
243 static char *netid_udp_main;
244 struct netconfig *dummy;
247 if ((main_thread = thr_main())) {
248 netid_udp = netid_udp_main;
249 netid_tcp = netid_tcp_main;
251 if (thr_once(&keys_once, keys_init) != 0 ||
252 tcp_key_error != 0 || udp_key_error != 0)
254 netid_tcp = (char *)thr_getspecific(tcp_key);
255 netid_udp = (char *)thr_getspecific(udp_key);
257 if (!netid_udp && !netid_tcp) {
258 struct netconfig *nconf;
261 if (!(confighandle = setnetconfig())) {
262 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
265 while ((nconf = getnetconfig(confighandle)) != NULL) {
266 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
267 if (strcmp(nconf->nc_proto, NC_TCP) == 0 &&
269 netid_tcp = strdup(nconf->nc_netid);
271 netid_tcp_main = netid_tcp;
273 thr_setspecific(tcp_key,
276 if (strcmp(nconf->nc_proto, NC_UDP) == 0 &&
278 netid_udp = strdup(nconf->nc_netid);
280 netid_udp_main = netid_udp;
282 thr_setspecific(udp_key,
287 endnetconfig(confighandle);
289 if (strcmp(nettype, "udp") == 0)
291 else if (strcmp(nettype, "tcp") == 0)
296 if ((netid == NULL) || (netid[0] == 0)) {
299 dummy = getnetconfigent(netid);
304 * Returns the type of the nettype, which should then be used with
308 __rpc_setconf(const char *nettype)
310 struct handle *handle;
312 handle = (struct handle *) malloc(sizeof (struct handle));
313 if (handle == NULL) {
316 switch (handle->nettype = getnettype(nettype)) {
319 case _RPC_DATAGRAM_N:
320 if (!(handle->nhandle = setnetpath()))
322 handle->nflag = TRUE;
326 case _RPC_DATAGRAM_V:
329 if (!(handle->nhandle = setnetconfig())) {
330 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
333 handle->nflag = FALSE;
347 * Returns the next netconfig struct for the given "net" type.
348 * __rpc_setconf() should have been called previously.
351 __rpc_getconf(void *vhandle)
353 struct handle *handle;
354 struct netconfig *nconf;
356 handle = (struct handle *)vhandle;
357 if (handle == NULL) {
362 nconf = getnetpath(handle->nhandle);
364 nconf = getnetconfig(handle->nhandle);
367 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
368 (nconf->nc_semantics != NC_TPI_COTS) &&
369 (nconf->nc_semantics != NC_TPI_COTS_ORD))
371 switch (handle->nettype) {
373 if (!(nconf->nc_flag & NC_VISIBLE))
376 case _RPC_NETPATH: /* Be happy */
379 if (!(nconf->nc_flag & NC_VISIBLE))
383 if ((nconf->nc_semantics != NC_TPI_COTS) &&
384 (nconf->nc_semantics != NC_TPI_COTS_ORD))
387 case _RPC_DATAGRAM_V:
388 if (!(nconf->nc_flag & NC_VISIBLE))
391 case _RPC_DATAGRAM_N:
392 if (nconf->nc_semantics != NC_TPI_CLTS)
396 if (((nconf->nc_semantics != NC_TPI_COTS) &&
397 (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
398 (strcmp(nconf->nc_protofmly, NC_INET)
400 && strcmp(nconf->nc_protofmly, NC_INET6))
405 strcmp(nconf->nc_proto, NC_TCP))
409 if ((nconf->nc_semantics != NC_TPI_CLTS) ||
410 (strcmp(nconf->nc_protofmly, NC_INET)
412 && strcmp(nconf->nc_protofmly, NC_INET6))
417 strcmp(nconf->nc_proto, NC_UDP))
427 __rpc_endconf(void *vhandle)
429 struct handle *handle;
431 handle = (struct handle *) vhandle;
432 if (handle == NULL) {
436 endnetpath(handle->nhandle);
438 endnetconfig(handle->nhandle);
444 * Used to ping the NULL procedure for clnt handle.
445 * Returns NULL if fails, else a non-NULL pointer.
448 rpc_nullproc(CLIENT *clnt)
450 struct timeval TIMEOUT = {25, 0};
452 if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL,
453 (xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
456 return ((void *) clnt);
460 * Try all possible transports until
461 * one succeeds in finding the netconf for the given fd.
467 struct __rpc_sockinfo si;
469 if (!__rpc_fd2sockinfo(fd, &si))
472 if (!__rpc_sockinfo2netid(&si, &netid))
475 /*LINTED const castaway*/
476 return getnetconfigent((char *)netid);
480 __rpc_fd2sockinfo(int fd, struct __rpc_sockinfo *sip)
484 struct sockaddr_storage ss;
487 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &len) < 0)
492 if (_getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &len) < 0)
496 if (ss.ss_family != AF_LOCAL) {
497 if (type == SOCK_STREAM)
499 else if (type == SOCK_DGRAM)
506 sip->si_af = ss.ss_family;
507 sip->si_proto = proto;
508 sip->si_socktype = type;
514 * Linear search, but the number of entries is small.
517 __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip)
521 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
522 if (strcmp(na_cvt[i].netid, nconf->nc_netid) == 0 || (
523 strcmp(nconf->nc_netid, "unix") == 0 &&
524 strcmp(na_cvt[i].netid, "local") == 0)) {
525 sip->si_af = na_cvt[i].af;
526 sip->si_proto = na_cvt[i].protocol;
528 __rpc_seman2socktype((int)nconf->nc_semantics);
529 if (sip->si_socktype == -1)
531 sip->si_alen = __rpc_get_a_size(sip->si_af);
539 __rpc_nconf2fd(const struct netconfig *nconf)
541 struct __rpc_sockinfo si;
543 if (!__rpc_nconf2sockinfo(nconf, &si))
546 return _socket(si.si_af, si.si_socktype, si.si_proto);
550 __rpc_sockinfo2netid(struct __rpc_sockinfo *sip, const char **netid)
553 struct netconfig *nconf;
555 nconf = getnetconfigent("local");
557 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++) {
558 if (na_cvt[i].af == sip->si_af &&
559 na_cvt[i].protocol == sip->si_proto) {
560 if (strcmp(na_cvt[i].netid, "local") == 0 && nconf == NULL) {
565 *netid = na_cvt[i].netid;
568 freenetconfigent(nconf);
573 freenetconfigent(nconf);
579 taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
581 struct __rpc_sockinfo si;
583 if (!__rpc_nconf2sockinfo(nconf, &si))
585 return __rpc_taddr2uaddr_af(si.si_af, nbuf);
589 uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
591 struct __rpc_sockinfo si;
593 if (!__rpc_nconf2sockinfo(nconf, &si))
595 return __rpc_uaddr2taddr_af(si.si_af, uaddr);
599 __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
602 struct sockaddr_in *sin;
603 struct sockaddr_un *sun;
604 char namebuf[INET_ADDRSTRLEN];
606 struct sockaddr_in6 *sin6;
607 char namebuf6[INET6_ADDRSTRLEN];
613 if (nbuf->len < sizeof(*sin))
616 if (inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
619 port = ntohs(sin->sin_port);
620 if (asprintf(&ret, "%s.%u.%u", namebuf, ((u_int32_t)port) >> 8,
626 if (nbuf->len < sizeof(*sin6))
629 if (inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
632 port = ntohs(sin6->sin6_port);
633 if (asprintf(&ret, "%s.%u.%u", namebuf6, ((u_int32_t)port) >> 8,
640 if (asprintf(&ret, "%.*s", (int)(sun->sun_len -
641 offsetof(struct sockaddr_un, sun_path)),
653 __rpc_uaddr2taddr_af(int af, const char *uaddr)
655 struct netbuf *ret = NULL;
657 unsigned port, portlo, porthi;
658 struct sockaddr_in *sin;
660 struct sockaddr_in6 *sin6;
662 struct sockaddr_un *sun;
670 addrstr = strdup(uaddr);
675 * AF_LOCAL addresses are expected to be absolute
676 * pathnames, anything else will be AF_INET or AF_INET6.
678 if (*addrstr != '/') {
679 p = strrchr(addrstr, '.');
682 portlo = (unsigned)atoi(p + 1);
685 p = strrchr(addrstr, '.');
688 porthi = (unsigned)atoi(p + 1);
690 port = (porthi << 8) | portlo;
693 ret = (struct netbuf *)malloc(sizeof *ret);
699 sin = (struct sockaddr_in *)malloc(sizeof *sin);
702 memset(sin, 0, sizeof *sin);
703 sin->sin_family = AF_INET;
704 sin->sin_port = htons(port);
705 if (inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) {
711 sin->sin_len = ret->maxlen = ret->len = sizeof *sin;
716 sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6);
719 memset(sin6, 0, sizeof *sin6);
720 sin6->sin6_family = AF_INET6;
721 sin6->sin6_port = htons(port);
722 if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) {
728 sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6;
733 sun = (struct sockaddr_un *)malloc(sizeof *sun);
736 memset(sun, 0, sizeof *sun);
737 sun->sun_family = AF_LOCAL;
738 strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1);
739 ret->len = ret->maxlen = sun->sun_len = SUN_LEN(sun);
751 __rpc_seman2socktype(int semantics)
756 case NC_TPI_COTS_ORD:
768 __rpc_socktype2seman(int socktype)
774 return NC_TPI_COTS_ORD;
785 * XXXX - IPv6 scope IDs can't be handled in universal addresses.
786 * Here, we compare the original server address to that of the RPC
787 * service we just received back from a call to rpcbind on the remote
788 * machine. If they are both "link local" or "site local", copy
789 * the scope id of the server address over to the service address.
792 __rpc_fixup_addr(struct netbuf *new, const struct netbuf *svc)
795 struct sockaddr *sa_new, *sa_svc;
796 struct sockaddr_in6 *sin6_new, *sin6_svc;
798 sa_svc = (struct sockaddr *)svc->buf;
799 sa_new = (struct sockaddr *)new->buf;
801 if (sa_new->sa_family == sa_svc->sa_family &&
802 sa_new->sa_family == AF_INET6) {
803 sin6_new = (struct sockaddr_in6 *)new->buf;
804 sin6_svc = (struct sockaddr_in6 *)svc->buf;
806 if ((IN6_IS_ADDR_LINKLOCAL(&sin6_new->sin6_addr) &&
807 IN6_IS_ADDR_LINKLOCAL(&sin6_svc->sin6_addr)) ||
808 (IN6_IS_ADDR_SITELOCAL(&sin6_new->sin6_addr) &&
809 IN6_IS_ADDR_SITELOCAL(&sin6_svc->sin6_addr))) {
810 sin6_new->sin6_scope_id = sin6_svc->sin6_scope_id;
818 __rpc_sockisbound(int fd)
820 struct sockaddr_storage ss;
823 slen = sizeof (struct sockaddr_storage);
824 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0)
827 switch (ss.ss_family) {
829 return (((struct sockaddr_in *)
830 (void *)&ss)->sin_port != 0);
833 return (((struct sockaddr_in6 *)
834 (void *)&ss)->sin6_port != 0);
838 return (((struct sockaddr_un *)
839 (void *)&ss)->sun_path[0] != '\0');