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"
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
138 __rpc_get_t_size(af, proto, size)
140 int size; /* Size requested */
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
172 return sizeof (struct sockaddr_in);
175 return sizeof (struct sockaddr_in6);
178 return sizeof (struct sockaddr_un);
182 return ((u_int)RPC_MAXADDRSIZE);
200 * Returns the type of the network as defined in <rpc/nettype.h>
201 * If nettype is NULL, it defaults to NETPATH.
209 if ((nettype == NULL) || (nettype[0] == 0)) {
210 return (_RPC_NETPATH); /* Default */
214 nettype = strlocase(nettype);
216 for (i = 0; _rpctypelist[i].name; i++)
217 if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
218 return (_rpctypelist[i].type);
220 return (_rpctypelist[i].type);
224 * For the given nettype (tcp or udp only), return the first structure found.
225 * This should be freed by calling freenetconfigent()
228 __rpc_getconfip(nettype)
232 char *netid_tcp = (char *) NULL;
233 char *netid_udp = (char *) NULL;
234 static char *netid_tcp_main;
235 static char *netid_udp_main;
236 struct netconfig *dummy;
238 static thread_key_t tcp_key, udp_key;
239 extern mutex_t tsd_lock;
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())) {
326 handle->nflag = TRUE;
330 case _RPC_DATAGRAM_V:
333 if (!(handle->nhandle = setnetconfig())) {
334 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
338 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(vhandle)
355 struct handle *handle;
356 struct netconfig *nconf;
358 handle = (struct handle *)vhandle;
359 if (handle == NULL) {
364 nconf = getnetpath(handle->nhandle);
366 nconf = getnetconfig(handle->nhandle);
369 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
370 (nconf->nc_semantics != NC_TPI_COTS) &&
371 (nconf->nc_semantics != NC_TPI_COTS_ORD))
373 switch (handle->nettype) {
375 if (!(nconf->nc_flag & NC_VISIBLE))
378 case _RPC_NETPATH: /* Be happy */
381 if (!(nconf->nc_flag & NC_VISIBLE))
385 if ((nconf->nc_semantics != NC_TPI_COTS) &&
386 (nconf->nc_semantics != NC_TPI_COTS_ORD))
389 case _RPC_DATAGRAM_V:
390 if (!(nconf->nc_flag & NC_VISIBLE))
393 case _RPC_DATAGRAM_N:
394 if (nconf->nc_semantics != NC_TPI_CLTS)
398 if (((nconf->nc_semantics != NC_TPI_COTS) &&
399 (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
400 (strcmp(nconf->nc_protofmly, NC_INET)
402 && strcmp(nconf->nc_protofmly, NC_INET6))
407 strcmp(nconf->nc_proto, NC_TCP))
411 if ((nconf->nc_semantics != NC_TPI_CLTS) ||
412 (strcmp(nconf->nc_protofmly, NC_INET)
414 && strcmp(nconf->nc_protofmly, NC_INET6))
419 strcmp(nconf->nc_proto, NC_UDP))
429 __rpc_endconf(vhandle)
432 struct handle *handle;
434 handle = (struct handle *) vhandle;
435 if (handle == NULL) {
439 endnetpath(handle->nhandle);
441 endnetconfig(handle->nhandle);
447 * Used to ping the NULL procedure for clnt handle.
448 * Returns NULL if fails, else a non-NULL pointer.
454 struct timeval TIMEOUT = {25, 0};
456 if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL,
457 (xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
460 return ((void *) clnt);
464 * Try all possible transports until
465 * one succeeds in finding the netconf for the given fd.
472 struct __rpc_sockinfo si;
474 if (!__rpc_fd2sockinfo(fd, &si))
477 if (!__rpc_sockinfo2netid(&si, &netid))
480 /*LINTED const castaway*/
481 return getnetconfigent((char *)netid);
485 __rpc_fd2sockinfo(int fd, struct __rpc_sockinfo *sip)
489 struct sockaddr_storage ss;
492 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &len) < 0)
497 if (_getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &len) < 0)
501 if (ss.ss_family != AF_LOCAL) {
502 if (type == SOCK_STREAM)
504 else if (type == SOCK_DGRAM)
511 sip->si_af = ss.ss_family;
512 sip->si_proto = proto;
513 sip->si_socktype = type;
519 * Linear search, but the number of entries is small.
522 __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip)
526 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
527 if (strcmp(na_cvt[i].netid, nconf->nc_netid) == 0 || (
528 strcmp(nconf->nc_netid, "unix") == 0 &&
529 strcmp(na_cvt[i].netid, "local") == 0)) {
530 sip->si_af = na_cvt[i].af;
531 sip->si_proto = na_cvt[i].protocol;
533 __rpc_seman2socktype((int)nconf->nc_semantics);
534 if (sip->si_socktype == -1)
536 sip->si_alen = __rpc_get_a_size(sip->si_af);
544 __rpc_nconf2fd(const struct netconfig *nconf)
546 struct __rpc_sockinfo si;
548 if (!__rpc_nconf2sockinfo(nconf, &si))
551 return _socket(si.si_af, si.si_socktype, si.si_proto);
555 __rpc_sockinfo2netid(struct __rpc_sockinfo *sip, const char **netid)
558 struct netconfig *nconf;
560 nconf = getnetconfigent("local");
562 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++) {
563 if (na_cvt[i].af == sip->si_af &&
564 na_cvt[i].protocol == sip->si_proto) {
565 if (strcmp(na_cvt[i].netid, "local") == 0 && nconf == NULL) {
570 *netid = na_cvt[i].netid;
573 freenetconfigent(nconf);
578 freenetconfigent(nconf);
584 taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
586 struct __rpc_sockinfo si;
588 if (!__rpc_nconf2sockinfo(nconf, &si))
590 return __rpc_taddr2uaddr_af(si.si_af, nbuf);
594 uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
596 struct __rpc_sockinfo si;
598 if (!__rpc_nconf2sockinfo(nconf, &si))
600 return __rpc_uaddr2taddr_af(si.si_af, uaddr);
604 __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
607 struct sockaddr_in *sin;
608 struct sockaddr_un *sun;
609 char namebuf[INET_ADDRSTRLEN];
611 struct sockaddr_in6 *sin6;
612 char namebuf6[INET6_ADDRSTRLEN];
619 if (inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
622 port = ntohs(sin->sin_port);
623 if (asprintf(&ret, "%s.%u.%u", namebuf, ((u_int32_t)port) >> 8,
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;
667 addrstr = strdup(uaddr);
672 * AF_LOCAL addresses are expected to be absolute
673 * pathnames, anything else will be AF_INET or AF_INET6.
675 if (*addrstr != '/') {
676 p = strrchr(addrstr, '.');
679 portlo = (unsigned)atoi(p + 1);
682 p = strrchr(addrstr, '.');
685 porthi = (unsigned)atoi(p + 1);
687 port = (porthi << 8) | portlo;
690 ret = (struct netbuf *)malloc(sizeof *ret);
696 sin = (struct sockaddr_in *)malloc(sizeof *sin);
699 memset(sin, 0, sizeof *sin);
700 sin->sin_family = AF_INET;
701 sin->sin_port = htons(port);
702 if (inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) {
708 sin->sin_len = ret->maxlen = ret->len = sizeof *sin;
713 sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6);
716 memset(sin6, 0, sizeof *sin6);
717 sin6->sin6_family = AF_INET6;
718 sin6->sin6_port = htons(port);
719 if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) {
725 sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6;
730 sun = (struct sockaddr_un *)malloc(sizeof *sun);
733 memset(sun, 0, sizeof *sun);
734 sun->sun_family = AF_LOCAL;
735 strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1);
736 ret->len = ret->maxlen = sun->sun_len = SUN_LEN(sun);
748 __rpc_seman2socktype(int semantics)
753 case NC_TPI_COTS_ORD:
765 __rpc_socktype2seman(int socktype)
771 return NC_TPI_COTS_ORD;
782 * XXXX - IPv6 scope IDs can't be handled in universal addresses.
783 * Here, we compare the original server address to that of the RPC
784 * service we just received back from a call to rpcbind on the remote
785 * machine. If they are both "link local" or "site local", copy
786 * the scope id of the server address over to the service address.
789 __rpc_fixup_addr(struct netbuf *new, const struct netbuf *svc)
792 struct sockaddr *sa_new, *sa_svc;
793 struct sockaddr_in6 *sin6_new, *sin6_svc;
795 sa_svc = (struct sockaddr *)svc->buf;
796 sa_new = (struct sockaddr *)new->buf;
798 if (sa_new->sa_family == sa_svc->sa_family &&
799 sa_new->sa_family == AF_INET6) {
800 sin6_new = (struct sockaddr_in6 *)new->buf;
801 sin6_svc = (struct sockaddr_in6 *)svc->buf;
803 if ((IN6_IS_ADDR_LINKLOCAL(&sin6_new->sin6_addr) &&
804 IN6_IS_ADDR_LINKLOCAL(&sin6_svc->sin6_addr)) ||
805 (IN6_IS_ADDR_SITELOCAL(&sin6_new->sin6_addr) &&
806 IN6_IS_ADDR_SITELOCAL(&sin6_svc->sin6_addr))) {
807 sin6_new->sin6_scope_id = sin6_svc->sin6_scope_id;
815 __rpc_sockisbound(int fd)
817 struct sockaddr_storage ss;
820 slen = sizeof (struct sockaddr_storage);
821 if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0)
824 switch (ss.ss_family) {
826 return (((struct sockaddr_in *)
827 (void *)&ss)->sin_port != 0);
830 return (((struct sockaddr_in6 *)
831 (void *)&ss)->sin6_port != 0);
835 return (((struct sockaddr_un *)
836 (void *)&ss)->sun_path[0] != '\0');