1 /* $NetBSD: rpc_generic.c,v 1.4 2000/09/28 09:07:04 kleink Exp $ */
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 /* #pragma ident "@(#)rpc_generic.c 1.17 94/04/24 SMI" */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * rpc_generic.c, Miscl routines for RPC.
44 #include "namespace.h"
45 #include "reentrant.h"
46 #include <sys/types.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
139 __rpc_get_t_size(af, proto, size)
141 int size; /* Size requested */
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
173 return sizeof (struct sockaddr_in);
176 return sizeof (struct sockaddr_in6);
179 return sizeof (struct sockaddr_un);
183 return ((u_int)RPC_MAXADDRSIZE);
201 * Returns the type of the network as defined in <rpc/nettype.h>
202 * If nettype is NULL, it defaults to NETPATH.
210 if ((nettype == NULL) || (nettype[0] == 0)) {
211 return (_RPC_NETPATH); /* Default */
215 nettype = strlocase(nettype);
217 for (i = 0; _rpctypelist[i].name; i++)
218 if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
219 return (_rpctypelist[i].type);
221 return (_rpctypelist[i].type);
225 * For the given nettype (tcp or udp only), return the first structure found.
226 * This should be freed by calling freenetconfigent()
229 __rpc_getconfip(nettype)
233 char *netid_tcp = (char *) NULL;
234 char *netid_udp = (char *) NULL;
235 static char *netid_tcp_main;
236 static char *netid_udp_main;
237 struct netconfig *dummy;
239 static thread_key_t tcp_key, udp_key;
241 if ((main_thread = thr_main())) {
242 netid_udp = netid_udp_main;
243 netid_tcp = netid_tcp_main;
246 mutex_lock(&tsd_lock);
248 thr_keycreate(&tcp_key, free);
249 mutex_unlock(&tsd_lock);
251 netid_tcp = (char *)thr_getspecific(tcp_key);
253 mutex_lock(&tsd_lock);
255 thr_keycreate(&udp_key, free);
256 mutex_unlock(&tsd_lock);
258 netid_udp = (char *)thr_getspecific(udp_key);
260 if (!netid_udp && !netid_tcp) {
261 struct netconfig *nconf;
264 if (!(confighandle = setnetconfig())) {
265 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
268 while ((nconf = getnetconfig(confighandle)) != NULL) {
269 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
270 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
271 netid_tcp = strdup(nconf->nc_netid);
273 netid_tcp_main = netid_tcp;
275 thr_setspecific(tcp_key,
278 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(nettype)
312 struct handle *handle;
314 handle = (struct handle *) malloc(sizeof (struct handle));
315 if (handle == NULL) {
318 switch (handle->nettype = getnettype(nettype)) {
321 case _RPC_DATAGRAM_N:
322 if (!(handle->nhandle = setnetpath()))
324 handle->nflag = TRUE;
328 case _RPC_DATAGRAM_V:
331 if (!(handle->nhandle = setnetconfig())) {
332 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
335 handle->nflag = FALSE;
349 * Returns the next netconfig struct for the given "net" type.
350 * __rpc_setconf() should have been called previously.
353 __rpc_getconf(vhandle)
356 struct handle *handle;
357 struct netconfig *nconf;
359 handle = (struct handle *)vhandle;
360 if (handle == NULL) {
365 nconf = getnetpath(handle->nhandle);
367 nconf = getnetconfig(handle->nhandle);
370 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
371 (nconf->nc_semantics != NC_TPI_COTS) &&
372 (nconf->nc_semantics != NC_TPI_COTS_ORD))
374 switch (handle->nettype) {
376 if (!(nconf->nc_flag & NC_VISIBLE))
379 case _RPC_NETPATH: /* Be happy */
382 if (!(nconf->nc_flag & NC_VISIBLE))
386 if ((nconf->nc_semantics != NC_TPI_COTS) &&
387 (nconf->nc_semantics != NC_TPI_COTS_ORD))
390 case _RPC_DATAGRAM_V:
391 if (!(nconf->nc_flag & NC_VISIBLE))
394 case _RPC_DATAGRAM_N:
395 if (nconf->nc_semantics != NC_TPI_CLTS)
399 if (((nconf->nc_semantics != NC_TPI_COTS) &&
400 (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
401 (strcmp(nconf->nc_protofmly, NC_INET)
403 && strcmp(nconf->nc_protofmly, NC_INET6))
408 strcmp(nconf->nc_proto, NC_TCP))
412 if ((nconf->nc_semantics != NC_TPI_CLTS) ||
413 (strcmp(nconf->nc_protofmly, NC_INET)
415 && strcmp(nconf->nc_protofmly, NC_INET6))
420 strcmp(nconf->nc_proto, NC_UDP))
430 __rpc_endconf(vhandle)
433 struct handle *handle;
435 handle = (struct handle *) vhandle;
436 if (handle == NULL) {
440 endnetpath(handle->nhandle);
442 endnetconfig(handle->nhandle);
448 * Used to ping the NULL procedure for clnt handle.
449 * Returns NULL if fails, else a non-NULL pointer.
455 struct timeval TIMEOUT = {25, 0};
457 if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL,
458 (xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
461 return ((void *) clnt);
465 * Try all possible transports until
466 * one succeeds in finding the netconf for the given fd.
473 struct __rpc_sockinfo si;
475 if (!__rpc_fd2sockinfo(fd, &si))
478 if (!__rpc_sockinfo2netid(&si, &netid))
481 /*LINTED const castaway*/
482 return getnetconfigent((char *)netid);
486 __rpc_fd2sockinfo(int fd, struct __rpc_sockinfo *sip)
490 struct sockaddr_storage ss;
493 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &len) < 0)
498 if (_getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &len) < 0)
502 if (ss.ss_family != AF_LOCAL) {
503 if (type == SOCK_STREAM)
505 else if (type == SOCK_DGRAM)
512 sip->si_af = ss.ss_family;
513 sip->si_proto = proto;
514 sip->si_socktype = type;
520 * Linear search, but the number of entries is small.
523 __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip)
527 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
528 if (strcmp(na_cvt[i].netid, nconf->nc_netid) == 0 || (
529 strcmp(nconf->nc_netid, "unix") == 0 &&
530 strcmp(na_cvt[i].netid, "local") == 0)) {
531 sip->si_af = na_cvt[i].af;
532 sip->si_proto = na_cvt[i].protocol;
534 __rpc_seman2socktype((int)nconf->nc_semantics);
535 if (sip->si_socktype == -1)
537 sip->si_alen = __rpc_get_a_size(sip->si_af);
545 __rpc_nconf2fd(const struct netconfig *nconf)
547 struct __rpc_sockinfo si;
549 if (!__rpc_nconf2sockinfo(nconf, &si))
552 return _socket(si.si_af, si.si_socktype, si.si_proto);
556 __rpc_sockinfo2netid(struct __rpc_sockinfo *sip, const char **netid)
559 struct netconfig *nconf;
561 nconf = getnetconfigent("local");
563 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++) {
564 if (na_cvt[i].af == sip->si_af &&
565 na_cvt[i].protocol == sip->si_proto) {
566 if (strcmp(na_cvt[i].netid, "local") == 0 && nconf == NULL) {
571 *netid = na_cvt[i].netid;
574 freenetconfigent(nconf);
579 freenetconfigent(nconf);
585 taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
587 struct __rpc_sockinfo si;
589 if (!__rpc_nconf2sockinfo(nconf, &si))
591 return __rpc_taddr2uaddr_af(si.si_af, nbuf);
595 uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
597 struct __rpc_sockinfo si;
599 if (!__rpc_nconf2sockinfo(nconf, &si))
601 return __rpc_uaddr2taddr_af(si.si_af, uaddr);
605 __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
608 struct sockaddr_in *sin;
609 struct sockaddr_un *sun;
610 char namebuf[INET_ADDRSTRLEN];
612 struct sockaddr_in6 *sin6;
613 char namebuf6[INET6_ADDRSTRLEN];
620 if (inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
623 port = ntohs(sin->sin_port);
624 if (asprintf(&ret, "%s.%u.%u", namebuf, ((u_int32_t)port) >> 8,
631 if (inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
634 port = ntohs(sin6->sin6_port);
635 if (asprintf(&ret, "%s.%u.%u", namebuf6, ((u_int32_t)port) >> 8,
642 if (asprintf(&ret, "%.*s", (int)(sun->sun_len -
643 offsetof(struct sockaddr_un, sun_path)),
655 __rpc_uaddr2taddr_af(int af, const char *uaddr)
657 struct netbuf *ret = NULL;
659 unsigned port, portlo, porthi;
660 struct sockaddr_in *sin;
662 struct sockaddr_in6 *sin6;
664 struct sockaddr_un *sun;
668 addrstr = strdup(uaddr);
673 * AF_LOCAL addresses are expected to be absolute
674 * pathnames, anything else will be AF_INET or AF_INET6.
676 if (*addrstr != '/') {
677 p = strrchr(addrstr, '.');
680 portlo = (unsigned)atoi(p + 1);
683 p = strrchr(addrstr, '.');
686 porthi = (unsigned)atoi(p + 1);
688 port = (porthi << 8) | portlo;
691 ret = (struct netbuf *)malloc(sizeof *ret);
697 sin = (struct sockaddr_in *)malloc(sizeof *sin);
700 memset(sin, 0, sizeof *sin);
701 sin->sin_family = AF_INET;
702 sin->sin_port = htons(port);
703 if (inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) {
709 sin->sin_len = ret->maxlen = ret->len = sizeof *sin;
714 sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6);
717 memset(sin6, 0, sizeof *sin6);
718 sin6->sin6_family = AF_INET6;
719 sin6->sin6_port = htons(port);
720 if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) {
726 sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6;
731 sun = (struct sockaddr_un *)malloc(sizeof *sun);
734 memset(sun, 0, sizeof *sun);
735 sun->sun_family = AF_LOCAL;
736 strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1);
737 ret->len = ret->maxlen = sun->sun_len = SUN_LEN(sun);
749 __rpc_seman2socktype(int semantics)
754 case NC_TPI_COTS_ORD:
766 __rpc_socktype2seman(int socktype)
772 return NC_TPI_COTS_ORD;
783 * XXXX - IPv6 scope IDs can't be handled in universal addresses.
784 * Here, we compare the original server address to that of the RPC
785 * service we just received back from a call to rpcbind on the remote
786 * machine. If they are both "link local" or "site local", copy
787 * the scope id of the server address over to the service address.
790 __rpc_fixup_addr(struct netbuf *new, const struct netbuf *svc)
793 struct sockaddr *sa_new, *sa_svc;
794 struct sockaddr_in6 *sin6_new, *sin6_svc;
796 sa_svc = (struct sockaddr *)svc->buf;
797 sa_new = (struct sockaddr *)new->buf;
799 if (sa_new->sa_family == sa_svc->sa_family &&
800 sa_new->sa_family == AF_INET6) {
801 sin6_new = (struct sockaddr_in6 *)new->buf;
802 sin6_svc = (struct sockaddr_in6 *)svc->buf;
804 if ((IN6_IS_ADDR_LINKLOCAL(&sin6_new->sin6_addr) &&
805 IN6_IS_ADDR_LINKLOCAL(&sin6_svc->sin6_addr)) ||
806 (IN6_IS_ADDR_SITELOCAL(&sin6_new->sin6_addr) &&
807 IN6_IS_ADDR_SITELOCAL(&sin6_svc->sin6_addr))) {
808 sin6_new->sin6_scope_id = sin6_svc->sin6_scope_id;
816 __rpc_sockisbound(int fd)
818 struct sockaddr_storage ss;
821 slen = sizeof (struct sockaddr_storage);
822 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0)
825 switch (ss.ss_family) {
827 return (((struct sockaddr_in *)
828 (void *)&ss)->sin_port != 0);
831 return (((struct sockaddr_in6 *)
832 (void *)&ss)->sin6_port != 0);
836 return (((struct sockaddr_un *)
837 (void *)&ss)->sun_path[0] != '\0');