xref: /freebsd/lib/libc/rpc/rpc_generic.c (revision 7bd6fde3)
1 /*	$NetBSD: rpc_generic.c,v 1.4 2000/09/28 09:07:04 kleink Exp $	*/
2 
3 /*
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.
10  *
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.
14  *
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.
18  *
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.
22  *
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.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 /*
32  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33  */
34 
35 /* #pragma ident	"@(#)rpc_generic.c	1.17	94/04/24 SMI" */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 /*
40  * rpc_generic.c, Miscl routines for RPC.
41  *
42  */
43 
44 #include "namespace.h"
45 #include "reentrant.h"
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/socket.h>
49 #include <sys/time.h>
50 #include <sys/un.h>
51 #include <sys/resource.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
54 #include <rpc/rpc.h>
55 #include <ctype.h>
56 #include <stddef.h>
57 #include <stdio.h>
58 #include <netdb.h>
59 #include <netconfig.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <syslog.h>
63 #include <rpc/nettype.h>
64 #include "un-namespace.h"
65 #include "rpc_com.h"
66 #include "mt_misc.h"
67 
68 struct handle {
69 	NCONF_HANDLE *nhandle;
70 	int nflag;		/* Whether NETPATH or NETCONFIG */
71 	int nettype;
72 };
73 
74 static const struct _rpcnettype {
75 	const char *name;
76 	const int type;
77 } _rpctypelist[] = {
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 },
84 	{ "tcp", _RPC_TCP },
85 	{ "udp", _RPC_UDP },
86 	{ 0, _RPC_NONE }
87 };
88 
89 struct netid_af {
90 	const char	*netid;
91 	int		af;
92 	int		protocol;
93 };
94 
95 static const struct netid_af na_cvt[] = {
96 	{ "udp",  AF_INET,  IPPROTO_UDP },
97 	{ "tcp",  AF_INET,  IPPROTO_TCP },
98 #ifdef INET6
99 	{ "udp6", AF_INET6, IPPROTO_UDP },
100 	{ "tcp6", AF_INET6, IPPROTO_TCP },
101 #endif
102 	{ "local", AF_LOCAL, 0 }
103 };
104 
105 #if 0
106 static char *strlocase(char *);
107 #endif
108 static int getnettype(const char *);
109 
110 /*
111  * Cache the result of getrlimit(), so we don't have to do an
112  * expensive call every time.
113  */
114 int
115 __rpc_dtbsize()
116 {
117 	static int tbsize;
118 	struct rlimit rl;
119 
120 	if (tbsize) {
121 		return (tbsize);
122 	}
123 	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
124 		return (tbsize = (int)rl.rlim_max);
125 	}
126 	/*
127 	 * Something wrong.  I'll try to save face by returning a
128 	 * pessimistic number.
129 	 */
130 	return (32);
131 }
132 
133 
134 /*
135  * Find the appropriate buffer size
136  */
137 u_int
138 /*ARGSUSED*/
139 __rpc_get_t_size(af, proto, size)
140 	int af, proto;
141 	int size;	/* Size requested */
142 {
143 	int maxsize, defsize;
144 
145 	maxsize = 256 * 1024;	/* XXX */
146 	switch (proto) {
147 	case IPPROTO_TCP:
148 		defsize = 64 * 1024;	/* XXX */
149 		break;
150 	case IPPROTO_UDP:
151 		defsize = UDPMSGSIZE;
152 		break;
153 	default:
154 		defsize = RPC_MAXDATASIZE;
155 		break;
156 	}
157 	if (size == 0)
158 		return defsize;
159 
160 	/* Check whether the value is within the upper max limit */
161 	return (size > maxsize ? (u_int)maxsize : (u_int)size);
162 }
163 
164 /*
165  * Find the appropriate address buffer size
166  */
167 u_int
168 __rpc_get_a_size(af)
169 	int af;
170 {
171 	switch (af) {
172 	case AF_INET:
173 		return sizeof (struct sockaddr_in);
174 #ifdef INET6
175 	case AF_INET6:
176 		return sizeof (struct sockaddr_in6);
177 #endif
178 	case AF_LOCAL:
179 		return sizeof (struct sockaddr_un);
180 	default:
181 		break;
182 	}
183 	return ((u_int)RPC_MAXADDRSIZE);
184 }
185 
186 #if 0
187 static char *
188 strlocase(p)
189 	char *p;
190 {
191 	char *t = p;
192 
193 	for (; *p; p++)
194 		if (isupper(*p))
195 			*p = tolower(*p);
196 	return (t);
197 }
198 #endif
199 
200 /*
201  * Returns the type of the network as defined in <rpc/nettype.h>
202  * If nettype is NULL, it defaults to NETPATH.
203  */
204 static int
205 getnettype(nettype)
206 	const char *nettype;
207 {
208 	int i;
209 
210 	if ((nettype == NULL) || (nettype[0] == 0)) {
211 		return (_RPC_NETPATH);	/* Default */
212 	}
213 
214 #if 0
215 	nettype = strlocase(nettype);
216 #endif
217 	for (i = 0; _rpctypelist[i].name; i++)
218 		if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
219 			return (_rpctypelist[i].type);
220 		}
221 	return (_rpctypelist[i].type);
222 }
223 
224 /*
225  * For the given nettype (tcp or udp only), return the first structure found.
226  * This should be freed by calling freenetconfigent()
227  */
228 struct netconfig *
229 __rpc_getconfip(nettype)
230 	const char *nettype;
231 {
232 	char *netid;
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;
238 	int main_thread;
239 	static thread_key_t tcp_key, udp_key;
240 
241 	if ((main_thread = thr_main())) {
242 		netid_udp = netid_udp_main;
243 		netid_tcp = netid_tcp_main;
244 	} else {
245 		if (tcp_key == 0) {
246 			mutex_lock(&tsd_lock);
247 			if (tcp_key == 0)
248 				thr_keycreate(&tcp_key, free);
249 			mutex_unlock(&tsd_lock);
250 		}
251 		netid_tcp = (char *)thr_getspecific(tcp_key);
252 		if (udp_key == 0) {
253 			mutex_lock(&tsd_lock);
254 			if (udp_key == 0)
255 				thr_keycreate(&udp_key, free);
256 			mutex_unlock(&tsd_lock);
257 		}
258 		netid_udp = (char *)thr_getspecific(udp_key);
259 	}
260 	if (!netid_udp && !netid_tcp) {
261 		struct netconfig *nconf;
262 		void *confighandle;
263 
264 		if (!(confighandle = setnetconfig())) {
265 			syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
266 			return (NULL);
267 		}
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);
272 					if (main_thread)
273 						netid_tcp_main = netid_tcp;
274 					else
275 						thr_setspecific(tcp_key,
276 							(void *) netid_tcp);
277 				} else
278 				if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
279 					netid_udp = strdup(nconf->nc_netid);
280 					if (main_thread)
281 						netid_udp_main = netid_udp;
282 					else
283 						thr_setspecific(udp_key,
284 						(void *) netid_udp);
285 				}
286 			}
287 		}
288 		endnetconfig(confighandle);
289 	}
290 	if (strcmp(nettype, "udp") == 0)
291 		netid = netid_udp;
292 	else if (strcmp(nettype, "tcp") == 0)
293 		netid = netid_tcp;
294 	else {
295 		return (NULL);
296 	}
297 	if ((netid == NULL) || (netid[0] == 0)) {
298 		return (NULL);
299 	}
300 	dummy = getnetconfigent(netid);
301 	return (dummy);
302 }
303 
304 /*
305  * Returns the type of the nettype, which should then be used with
306  * __rpc_getconf().
307  */
308 void *
309 __rpc_setconf(nettype)
310 	const char *nettype;
311 {
312 	struct handle *handle;
313 
314 	handle = (struct handle *) malloc(sizeof (struct handle));
315 	if (handle == NULL) {
316 		return (NULL);
317 	}
318 	switch (handle->nettype = getnettype(nettype)) {
319 	case _RPC_NETPATH:
320 	case _RPC_CIRCUIT_N:
321 	case _RPC_DATAGRAM_N:
322 		if (!(handle->nhandle = setnetpath())) {
323 			free(handle);
324 			return (NULL);
325 		}
326 		handle->nflag = TRUE;
327 		break;
328 	case _RPC_VISIBLE:
329 	case _RPC_CIRCUIT_V:
330 	case _RPC_DATAGRAM_V:
331 	case _RPC_TCP:
332 	case _RPC_UDP:
333 		if (!(handle->nhandle = setnetconfig())) {
334 		        syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
335 			free(handle);
336 			return (NULL);
337 		}
338 		handle->nflag = FALSE;
339 		break;
340 	default:
341 		return (NULL);
342 	}
343 
344 	return (handle);
345 }
346 
347 /*
348  * Returns the next netconfig struct for the given "net" type.
349  * __rpc_setconf() should have been called previously.
350  */
351 struct netconfig *
352 __rpc_getconf(vhandle)
353 	void *vhandle;
354 {
355 	struct handle *handle;
356 	struct netconfig *nconf;
357 
358 	handle = (struct handle *)vhandle;
359 	if (handle == NULL) {
360 		return (NULL);
361 	}
362 	for (;;) {
363 		if (handle->nflag)
364 			nconf = getnetpath(handle->nhandle);
365 		else
366 			nconf = getnetconfig(handle->nhandle);
367 		if (nconf == NULL)
368 			break;
369 		if ((nconf->nc_semantics != NC_TPI_CLTS) &&
370 			(nconf->nc_semantics != NC_TPI_COTS) &&
371 			(nconf->nc_semantics != NC_TPI_COTS_ORD))
372 			continue;
373 		switch (handle->nettype) {
374 		case _RPC_VISIBLE:
375 			if (!(nconf->nc_flag & NC_VISIBLE))
376 				continue;
377 			/* FALLTHROUGH */
378 		case _RPC_NETPATH:	/* Be happy */
379 			break;
380 		case _RPC_CIRCUIT_V:
381 			if (!(nconf->nc_flag & NC_VISIBLE))
382 				continue;
383 			/* FALLTHROUGH */
384 		case _RPC_CIRCUIT_N:
385 			if ((nconf->nc_semantics != NC_TPI_COTS) &&
386 				(nconf->nc_semantics != NC_TPI_COTS_ORD))
387 				continue;
388 			break;
389 		case _RPC_DATAGRAM_V:
390 			if (!(nconf->nc_flag & NC_VISIBLE))
391 				continue;
392 			/* FALLTHROUGH */
393 		case _RPC_DATAGRAM_N:
394 			if (nconf->nc_semantics != NC_TPI_CLTS)
395 				continue;
396 			break;
397 		case _RPC_TCP:
398 			if (((nconf->nc_semantics != NC_TPI_COTS) &&
399 				(nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
400 				(strcmp(nconf->nc_protofmly, NC_INET)
401 #ifdef INET6
402 				 && strcmp(nconf->nc_protofmly, NC_INET6))
403 #else
404 				)
405 #endif
406 				||
407 				strcmp(nconf->nc_proto, NC_TCP))
408 				continue;
409 			break;
410 		case _RPC_UDP:
411 			if ((nconf->nc_semantics != NC_TPI_CLTS) ||
412 				(strcmp(nconf->nc_protofmly, NC_INET)
413 #ifdef INET6
414 				&& strcmp(nconf->nc_protofmly, NC_INET6))
415 #else
416 				)
417 #endif
418 				||
419 				strcmp(nconf->nc_proto, NC_UDP))
420 				continue;
421 			break;
422 		}
423 		break;
424 	}
425 	return (nconf);
426 }
427 
428 void
429 __rpc_endconf(vhandle)
430 	void * vhandle;
431 {
432 	struct handle *handle;
433 
434 	handle = (struct handle *) vhandle;
435 	if (handle == NULL) {
436 		return;
437 	}
438 	if (handle->nflag) {
439 		endnetpath(handle->nhandle);
440 	} else {
441 		endnetconfig(handle->nhandle);
442 	}
443 	free(handle);
444 }
445 
446 /*
447  * Used to ping the NULL procedure for clnt handle.
448  * Returns NULL if fails, else a non-NULL pointer.
449  */
450 void *
451 rpc_nullproc(clnt)
452 	CLIENT *clnt;
453 {
454 	struct timeval TIMEOUT = {25, 0};
455 
456 	if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL,
457 		(xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
458 		return (NULL);
459 	}
460 	return ((void *) clnt);
461 }
462 
463 /*
464  * Try all possible transports until
465  * one succeeds in finding the netconf for the given fd.
466  */
467 struct netconfig *
468 __rpcgettp(fd)
469 	int fd;
470 {
471 	const char *netid;
472 	struct __rpc_sockinfo si;
473 
474 	if (!__rpc_fd2sockinfo(fd, &si))
475 		return NULL;
476 
477 	if (!__rpc_sockinfo2netid(&si, &netid))
478 		return NULL;
479 
480 	/*LINTED const castaway*/
481 	return getnetconfigent((char *)netid);
482 }
483 
484 int
485 __rpc_fd2sockinfo(int fd, struct __rpc_sockinfo *sip)
486 {
487 	socklen_t len;
488 	int type, proto;
489 	struct sockaddr_storage ss;
490 
491 	len = sizeof ss;
492 	if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &len) < 0)
493 		return 0;
494 	sip->si_alen = len;
495 
496 	len = sizeof type;
497 	if (_getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &len) < 0)
498 		return 0;
499 
500 	/* XXX */
501 	if (ss.ss_family != AF_LOCAL) {
502 		if (type == SOCK_STREAM)
503 			proto = IPPROTO_TCP;
504 		else if (type == SOCK_DGRAM)
505 			proto = IPPROTO_UDP;
506 		else
507 			return 0;
508 	} else
509 		proto = 0;
510 
511 	sip->si_af = ss.ss_family;
512 	sip->si_proto = proto;
513 	sip->si_socktype = type;
514 
515 	return 1;
516 }
517 
518 /*
519  * Linear search, but the number of entries is small.
520  */
521 int
522 __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip)
523 {
524 	int i;
525 
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;
532 			sip->si_socktype =
533 			    __rpc_seman2socktype((int)nconf->nc_semantics);
534 			if (sip->si_socktype == -1)
535 				return 0;
536 			sip->si_alen = __rpc_get_a_size(sip->si_af);
537 			return 1;
538 		}
539 
540 	return 0;
541 }
542 
543 int
544 __rpc_nconf2fd(const struct netconfig *nconf)
545 {
546 	struct __rpc_sockinfo si;
547 
548 	if (!__rpc_nconf2sockinfo(nconf, &si))
549 		return 0;
550 
551 	return _socket(si.si_af, si.si_socktype, si.si_proto);
552 }
553 
554 int
555 __rpc_sockinfo2netid(struct __rpc_sockinfo *sip, const char **netid)
556 {
557 	int i;
558 	struct netconfig *nconf;
559 
560 	nconf = getnetconfigent("local");
561 
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) {
566 				if (netid)
567 					*netid = "unix";
568 			} else {
569 				if (netid)
570 					*netid = na_cvt[i].netid;
571 			}
572 			if (nconf != NULL)
573 				freenetconfigent(nconf);
574 			return 1;
575 		}
576 	}
577 	if (nconf != NULL)
578 		freenetconfigent(nconf);
579 
580 	return 0;
581 }
582 
583 char *
584 taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
585 {
586 	struct __rpc_sockinfo si;
587 
588 	if (!__rpc_nconf2sockinfo(nconf, &si))
589 		return NULL;
590 	return __rpc_taddr2uaddr_af(si.si_af, nbuf);
591 }
592 
593 struct netbuf *
594 uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
595 {
596 	struct __rpc_sockinfo si;
597 
598 	if (!__rpc_nconf2sockinfo(nconf, &si))
599 		return NULL;
600 	return __rpc_uaddr2taddr_af(si.si_af, uaddr);
601 }
602 
603 char *
604 __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
605 {
606 	char *ret;
607 	struct sockaddr_in *sin;
608 	struct sockaddr_un *sun;
609 	char namebuf[INET_ADDRSTRLEN];
610 #ifdef INET6
611 	struct sockaddr_in6 *sin6;
612 	char namebuf6[INET6_ADDRSTRLEN];
613 #endif
614 	u_int16_t port;
615 
616 	switch (af) {
617 	case AF_INET:
618 		sin = nbuf->buf;
619 		if (inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
620 		    == NULL)
621 			return NULL;
622 		port = ntohs(sin->sin_port);
623 		if (asprintf(&ret, "%s.%u.%u", namebuf, ((u_int32_t)port) >> 8,
624 		    port & 0xff) < 0)
625 			return NULL;
626 		break;
627 #ifdef INET6
628 	case AF_INET6:
629 		sin6 = nbuf->buf;
630 		if (inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
631 		    == NULL)
632 			return NULL;
633 		port = ntohs(sin6->sin6_port);
634 		if (asprintf(&ret, "%s.%u.%u", namebuf6, ((u_int32_t)port) >> 8,
635 		    port & 0xff) < 0)
636 			return NULL;
637 		break;
638 #endif
639 	case AF_LOCAL:
640 		sun = nbuf->buf;
641 		if (asprintf(&ret, "%.*s", (int)(sun->sun_len -
642 		    offsetof(struct sockaddr_un, sun_path)),
643 		    sun->sun_path) < 0)
644 			return (NULL);
645 		break;
646 	default:
647 		return NULL;
648 	}
649 
650 	return ret;
651 }
652 
653 struct netbuf *
654 __rpc_uaddr2taddr_af(int af, const char *uaddr)
655 {
656 	struct netbuf *ret = NULL;
657 	char *addrstr, *p;
658 	unsigned port, portlo, porthi;
659 	struct sockaddr_in *sin;
660 #ifdef INET6
661 	struct sockaddr_in6 *sin6;
662 #endif
663 	struct sockaddr_un *sun;
664 
665 	port = 0;
666 	sin = NULL;
667 	addrstr = strdup(uaddr);
668 	if (addrstr == NULL)
669 		return NULL;
670 
671 	/*
672 	 * AF_LOCAL addresses are expected to be absolute
673 	 * pathnames, anything else will be AF_INET or AF_INET6.
674 	 */
675 	if (*addrstr != '/') {
676 		p = strrchr(addrstr, '.');
677 		if (p == NULL)
678 			goto out;
679 		portlo = (unsigned)atoi(p + 1);
680 		*p = '\0';
681 
682 		p = strrchr(addrstr, '.');
683 		if (p == NULL)
684 			goto out;
685 		porthi = (unsigned)atoi(p + 1);
686 		*p = '\0';
687 		port = (porthi << 8) | portlo;
688 	}
689 
690 	ret = (struct netbuf *)malloc(sizeof *ret);
691 	if (ret == NULL)
692 		goto out;
693 
694 	switch (af) {
695 	case AF_INET:
696 		sin = (struct sockaddr_in *)malloc(sizeof *sin);
697 		if (sin == NULL)
698 			goto out;
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) {
703 			free(sin);
704 			free(ret);
705 			ret = NULL;
706 			goto out;
707 		}
708 		sin->sin_len = ret->maxlen = ret->len = sizeof *sin;
709 		ret->buf = sin;
710 		break;
711 #ifdef INET6
712 	case AF_INET6:
713 		sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6);
714 		if (sin6 == NULL)
715 			goto out;
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) {
720 			free(sin6);
721 			free(ret);
722 			ret = NULL;
723 			goto out;
724 		}
725 		sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6;
726 		ret->buf = sin6;
727 		break;
728 #endif
729 	case AF_LOCAL:
730 		sun = (struct sockaddr_un *)malloc(sizeof *sun);
731 		if (sun == NULL)
732 			goto out;
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);
737 		ret->buf = sun;
738 		break;
739 	default:
740 		break;
741 	}
742 out:
743 	free(addrstr);
744 	return ret;
745 }
746 
747 int
748 __rpc_seman2socktype(int semantics)
749 {
750 	switch (semantics) {
751 	case NC_TPI_CLTS:
752 		return SOCK_DGRAM;
753 	case NC_TPI_COTS_ORD:
754 		return SOCK_STREAM;
755 	case NC_TPI_RAW:
756 		return SOCK_RAW;
757 	default:
758 		break;
759 	}
760 
761 	return -1;
762 }
763 
764 int
765 __rpc_socktype2seman(int socktype)
766 {
767 	switch (socktype) {
768 	case SOCK_DGRAM:
769 		return NC_TPI_CLTS;
770 	case SOCK_STREAM:
771 		return NC_TPI_COTS_ORD;
772 	case SOCK_RAW:
773 		return NC_TPI_RAW;
774 	default:
775 		break;
776 	}
777 
778 	return -1;
779 }
780 
781 /*
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.
787  */
788 int
789 __rpc_fixup_addr(struct netbuf *new, const struct netbuf *svc)
790 {
791 #ifdef INET6
792 	struct sockaddr *sa_new, *sa_svc;
793 	struct sockaddr_in6 *sin6_new, *sin6_svc;
794 
795 	sa_svc = (struct sockaddr *)svc->buf;
796 	sa_new = (struct sockaddr *)new->buf;
797 
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;
802 
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;
808 		}
809 	}
810 #endif
811 	return 1;
812 }
813 
814 int
815 __rpc_sockisbound(int fd)
816 {
817 	struct sockaddr_storage ss;
818 	socklen_t slen;
819 
820 	slen = sizeof (struct sockaddr_storage);
821 	if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0)
822 		return 0;
823 
824 	switch (ss.ss_family) {
825 		case AF_INET:
826 			return (((struct sockaddr_in *)
827 			    (void *)&ss)->sin_port != 0);
828 #ifdef INET6
829 		case AF_INET6:
830 			return (((struct sockaddr_in6 *)
831 			    (void *)&ss)->sin6_port != 0);
832 #endif
833 		case AF_LOCAL:
834 			/* XXX check this */
835 			return (((struct sockaddr_un *)
836 			    (void *)&ss)->sun_path[0] != '\0');
837 		default:
838 			break;
839 	}
840 
841 	return 0;
842 }
843