2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
28 * @(#)rpc_generic.c 1.17 94/04/24 SMI
29 * $NetBSD: rpc_generic.c,v 1.4 2000/09/28 09:07:04 kleink Exp $
30 * $FreeBSD: src/lib/libc/rpc/rpc_generic.c,v 1.14 2007/09/20 22:35:24 matteo Exp $
34 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
38 * rpc_generic.c, Misc routines for RPC.
42 #include "namespace.h"
43 #include "reentrant.h"
44 #include <sys/types.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
137 __rpc_get_t_size(int af, int proto,
138 int size) /* Size requested */
140 int maxsize, defsize;
142 maxsize = 256 * 1024; /* XXX */
145 defsize = 64 * 1024; /* XXX */
148 defsize = UDPMSGSIZE;
151 defsize = RPC_MAXDATASIZE;
157 /* Check whether the value is within the upper max limit */
158 return (size > maxsize ? (u_int)maxsize : (u_int)size);
162 * Find the appropriate address buffer size
165 __rpc_get_a_size(int af)
169 return sizeof (struct sockaddr_in);
172 return sizeof (struct sockaddr_in6);
175 return sizeof (struct sockaddr_un);
179 return ((u_int)RPC_MAXADDRSIZE);
196 * Returns the type of the network as defined in <rpc/nettype.h>
197 * If nettype is NULL, it defaults to NETPATH.
200 getnettype(const char *nettype)
204 if ((nettype == NULL) || (nettype[0] == 0)) {
205 return (_RPC_NETPATH); /* Default */
209 nettype = strlocase(nettype);
211 for (i = 0; _rpctypelist[i].name; i++)
212 if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
213 return (_rpctypelist[i].type);
215 return (_rpctypelist[i].type);
219 * For the given nettype (tcp or udp only), return the first structure found.
220 * This should be freed by calling freenetconfigent()
223 __rpc_getconfip(const char *nettype)
226 char *netid_tcp = (char *) NULL;
227 char *netid_udp = (char *) NULL;
228 static char *netid_tcp_main;
229 static char *netid_udp_main;
230 struct netconfig *dummy;
232 static thread_key_t tcp_key, udp_key;
234 if ((main_thread = thr_main())) {
235 netid_udp = netid_udp_main;
236 netid_tcp = netid_tcp_main;
239 mutex_lock(&tsd_lock);
241 thr_keycreate(&tcp_key, free);
242 mutex_unlock(&tsd_lock);
244 netid_tcp = (char *)thr_getspecific(tcp_key);
246 mutex_lock(&tsd_lock);
248 thr_keycreate(&udp_key, free);
249 mutex_unlock(&tsd_lock);
251 netid_udp = (char *)thr_getspecific(udp_key);
253 if (!netid_udp && !netid_tcp) {
254 struct netconfig *nconf;
257 if (!(confighandle = setnetconfig())) {
258 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
261 while ((nconf = getnetconfig(confighandle)) != NULL) {
262 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
263 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
264 netid_tcp = strdup(nconf->nc_netid);
266 netid_tcp_main = netid_tcp;
268 thr_setspecific(tcp_key,
271 if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
272 netid_udp = strdup(nconf->nc_netid);
274 netid_udp_main = netid_udp;
276 thr_setspecific(udp_key,
281 endnetconfig(confighandle);
283 if (strcmp(nettype, "udp") == 0)
285 else if (strcmp(nettype, "tcp") == 0)
290 if ((netid == NULL) || (netid[0] == 0)) {
293 dummy = getnetconfigent(netid);
298 * Returns the type of the nettype, which should then be used with
302 __rpc_setconf(const char *nettype)
304 struct handle *handle;
306 handle = (struct handle *) malloc(sizeof (struct handle));
307 if (handle == NULL) {
310 switch (handle->nettype = getnettype(nettype)) {
313 case _RPC_DATAGRAM_N:
314 if (!(handle->nhandle = setnetpath()))
316 handle->nflag = TRUE;
320 case _RPC_DATAGRAM_V:
323 if (!(handle->nhandle = setnetconfig())) {
324 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
327 handle->nflag = FALSE;
341 * Returns the next netconfig struct for the given "net" type.
342 * __rpc_setconf() should have been called previously.
345 __rpc_getconf(void *vhandle)
347 struct handle *handle;
348 struct netconfig *nconf;
350 handle = (struct handle *)vhandle;
351 if (handle == NULL) {
356 nconf = getnetpath(handle->nhandle);
358 nconf = getnetconfig(handle->nhandle);
361 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
362 (nconf->nc_semantics != NC_TPI_COTS) &&
363 (nconf->nc_semantics != NC_TPI_COTS_ORD))
365 switch (handle->nettype) {
367 if (!(nconf->nc_flag & NC_VISIBLE))
370 case _RPC_NETPATH: /* Be happy */
373 if (!(nconf->nc_flag & NC_VISIBLE))
377 if ((nconf->nc_semantics != NC_TPI_COTS) &&
378 (nconf->nc_semantics != NC_TPI_COTS_ORD))
381 case _RPC_DATAGRAM_V:
382 if (!(nconf->nc_flag & NC_VISIBLE))
385 case _RPC_DATAGRAM_N:
386 if (nconf->nc_semantics != NC_TPI_CLTS)
390 if (((nconf->nc_semantics != NC_TPI_COTS) &&
391 (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
392 (strcmp(nconf->nc_protofmly, NC_INET)
394 && strcmp(nconf->nc_protofmly, NC_INET6))
399 strcmp(nconf->nc_proto, NC_TCP))
403 if ((nconf->nc_semantics != NC_TPI_CLTS) ||
404 (strcmp(nconf->nc_protofmly, NC_INET)
406 && strcmp(nconf->nc_protofmly, NC_INET6))
411 strcmp(nconf->nc_proto, NC_UDP))
421 __rpc_endconf(void *vhandle)
423 struct handle *handle;
425 handle = (struct handle *) vhandle;
426 if (handle == NULL) {
430 endnetpath(handle->nhandle);
432 endnetconfig(handle->nhandle);
438 * Used to ping the NULL procedure for clnt handle.
439 * Returns NULL if fails, else a non-NULL pointer.
442 rpc_nullproc(CLIENT *clnt)
444 struct timeval TIMEOUT = {25, 0};
446 if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL,
447 (xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
450 return ((void *) clnt);
454 * Try all possible transports until
455 * one succeeds in finding the netconf for the given fd.
461 struct __rpc_sockinfo si;
463 if (!__rpc_fd2sockinfo(fd, &si))
466 if (!__rpc_sockinfo2netid(&si, &netid))
469 /*LINTED const castaway*/
470 return getnetconfigent((char *)netid);
474 __rpc_fd2sockinfo(int fd, struct __rpc_sockinfo *sip)
478 struct sockaddr_storage ss;
481 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &len) < 0)
486 if (_getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &len) < 0)
490 if (ss.ss_family != AF_LOCAL) {
491 if (type == SOCK_STREAM)
493 else if (type == SOCK_DGRAM)
500 sip->si_af = ss.ss_family;
501 sip->si_proto = proto;
502 sip->si_socktype = type;
508 * Linear search, but the number of entries is small.
511 __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip)
515 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
516 if (strcmp(na_cvt[i].netid, nconf->nc_netid) == 0 || (
517 strcmp(nconf->nc_netid, "unix") == 0 &&
518 strcmp(na_cvt[i].netid, "local") == 0)) {
519 sip->si_af = na_cvt[i].af;
520 sip->si_proto = na_cvt[i].protocol;
522 __rpc_seman2socktype((int)nconf->nc_semantics);
523 if (sip->si_socktype == -1)
525 sip->si_alen = __rpc_get_a_size(sip->si_af);
533 __rpc_nconf2fd(const struct netconfig *nconf)
535 struct __rpc_sockinfo si;
537 if (!__rpc_nconf2sockinfo(nconf, &si))
540 return _socket(si.si_af, si.si_socktype, si.si_proto);
544 __rpc_sockinfo2netid(struct __rpc_sockinfo *sip, const char **netid)
547 struct netconfig *nconf;
549 nconf = getnetconfigent("local");
551 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++) {
552 if (na_cvt[i].af == sip->si_af &&
553 na_cvt[i].protocol == sip->si_proto) {
554 if (strcmp(na_cvt[i].netid, "local") == 0 && nconf == NULL) {
559 *netid = na_cvt[i].netid;
562 freenetconfigent(nconf);
567 freenetconfigent(nconf);
573 taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
575 struct __rpc_sockinfo si;
577 if (!__rpc_nconf2sockinfo(nconf, &si))
579 return __rpc_taddr2uaddr_af(si.si_af, nbuf);
583 uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
585 struct __rpc_sockinfo si;
587 if (!__rpc_nconf2sockinfo(nconf, &si))
589 return __rpc_uaddr2taddr_af(si.si_af, uaddr);
593 __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
596 struct sockaddr_in *sin;
597 struct sockaddr_un *sun;
598 char namebuf[INET_ADDRSTRLEN];
600 struct sockaddr_in6 *sin6;
601 char namebuf6[INET6_ADDRSTRLEN];
608 if (inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
611 port = ntohs(sin->sin_port);
612 if (asprintf(&ret, "%s.%u.%u", namebuf, ((u_int32_t)port) >> 8,
619 if (inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
622 port = ntohs(sin6->sin6_port);
623 if (asprintf(&ret, "%s.%u.%u", namebuf6, ((u_int32_t)port) >> 8,
630 if (asprintf(&ret, "%.*s", (int)(sun->sun_len -
631 offsetof(struct sockaddr_un, sun_path)),
643 __rpc_uaddr2taddr_af(int af, const char *uaddr)
645 struct netbuf *ret = NULL;
647 unsigned port, portlo, porthi;
648 struct sockaddr_in *sin;
650 struct sockaddr_in6 *sin6;
652 struct sockaddr_un *sun;
656 addrstr = strdup(uaddr);
661 * AF_LOCAL addresses are expected to be absolute
662 * pathnames, anything else will be AF_INET or AF_INET6.
664 if (*addrstr != '/') {
665 p = strrchr(addrstr, '.');
668 portlo = (unsigned)atoi(p + 1);
671 p = strrchr(addrstr, '.');
674 porthi = (unsigned)atoi(p + 1);
676 port = (porthi << 8) | portlo;
679 ret = (struct netbuf *)malloc(sizeof *ret);
685 sin = (struct sockaddr_in *)malloc(sizeof *sin);
688 memset(sin, 0, sizeof *sin);
689 sin->sin_family = AF_INET;
690 sin->sin_port = htons(port);
691 if (inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) {
697 sin->sin_len = ret->maxlen = ret->len = sizeof *sin;
702 sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6);
705 memset(sin6, 0, sizeof *sin6);
706 sin6->sin6_family = AF_INET6;
707 sin6->sin6_port = htons(port);
708 if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) {
714 sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6;
719 sun = (struct sockaddr_un *)malloc(sizeof *sun);
722 memset(sun, 0, sizeof *sun);
723 sun->sun_family = AF_LOCAL;
724 strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1);
725 ret->len = ret->maxlen = sun->sun_len = SUN_LEN(sun);
737 __rpc_seman2socktype(int semantics)
742 case NC_TPI_COTS_ORD:
754 __rpc_socktype2seman(int socktype)
760 return NC_TPI_COTS_ORD;
771 * XXXX - IPv6 scope IDs can't be handled in universal addresses.
772 * Here, we compare the original server address to that of the RPC
773 * service we just received back from a call to rpcbind on the remote
774 * machine. If they are both "link local" or "site local", copy
775 * the scope id of the server address over to the service address.
778 __rpc_fixup_addr(struct netbuf *new, const struct netbuf *svc)
781 struct sockaddr *sa_new, *sa_svc;
782 struct sockaddr_in6 *sin6_new, *sin6_svc;
784 sa_svc = (struct sockaddr *)svc->buf;
785 sa_new = (struct sockaddr *)new->buf;
787 if (sa_new->sa_family == sa_svc->sa_family &&
788 sa_new->sa_family == AF_INET6) {
789 sin6_new = (struct sockaddr_in6 *)new->buf;
790 sin6_svc = (struct sockaddr_in6 *)svc->buf;
792 if ((IN6_IS_ADDR_LINKLOCAL(&sin6_new->sin6_addr) &&
793 IN6_IS_ADDR_LINKLOCAL(&sin6_svc->sin6_addr)) ||
794 (IN6_IS_ADDR_SITELOCAL(&sin6_new->sin6_addr) &&
795 IN6_IS_ADDR_SITELOCAL(&sin6_svc->sin6_addr))) {
796 sin6_new->sin6_scope_id = sin6_svc->sin6_scope_id;
804 __rpc_sockisbound(int fd)
806 struct sockaddr_storage ss;
809 slen = sizeof (struct sockaddr_storage);
810 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0)
813 switch (ss.ss_family) {
815 return (((struct sockaddr_in *)
816 (void *)&ss)->sin_port != 0);
819 return (((struct sockaddr_in6 *)
820 (void *)&ss)->sin6_port != 0);
824 return (((struct sockaddr_un *)
825 (void *)&ss)->sun_path[0] != '\0');