xref: /freebsd-13-stable/lib/libc/rpc/rpc_generic.c (revision ad8b59ffe7b84cae35dd19b4285863b4b506efa9)
1 /*	$NetBSD: rpc_generic.c,v 1.4 2000/09/28 09:07:04 kleink Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2009, Sun Microsystems, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * - Redistributions of source code must retain the above copyright notice,
12  *   this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright notice,
14  *   this list of conditions and the following disclaimer in the documentation
15  *   and/or other materials provided with the distribution.
16  * - Neither the name of Sun Microsystems, Inc. nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*
33  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34  */
35 
36 /* #pragma ident	"@(#)rpc_generic.c	1.17	94/04/24 SMI" */
37 #include <sys/cdefs.h>
38 /*
39  * rpc_generic.c, Miscl routines for RPC.
40  *
41  */
42 
43 #include "namespace.h"
44 #include "reentrant.h"
45 #include <sys/param.h>
46 #include <sys/socket.h>
47 #include <sys/time.h>
48 #include <sys/un.h>
49 #include <sys/resource.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52 #include <rpc/rpc.h>
53 #include <ctype.h>
54 #include <stddef.h>
55 #include <stdio.h>
56 #include <netdb.h>
57 #include <netconfig.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <syslog.h>
61 #include <rpc/nettype.h>
62 #include "un-namespace.h"
63 #include "rpc_com.h"
64 #include "mt_misc.h"
65 
66 struct handle {
67 	NCONF_HANDLE *nhandle;
68 	int nflag;		/* Whether NETPATH or NETCONFIG */
69 	int nettype;
70 };
71 
72 static const struct _rpcnettype {
73 	const char *name;
74 	const int type;
75 } _rpctypelist[] = {
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 },
82 	{ "tcp", _RPC_TCP },
83 	{ "udp", _RPC_UDP },
84 	{ 0, _RPC_NONE }
85 };
86 
87 struct netid_af {
88 	const char	*netid;
89 	int		af;
90 	int		protocol;
91 };
92 
93 static const struct netid_af na_cvt[] = {
94 	{ "udp",  AF_INET,  IPPROTO_UDP },
95 	{ "tcp",  AF_INET,  IPPROTO_TCP },
96 #ifdef INET6
97 	{ "udp6", AF_INET6, IPPROTO_UDP },
98 	{ "tcp6", AF_INET6, IPPROTO_TCP },
99 #endif
100 	{ "local", AF_LOCAL, 0 }
101 };
102 
103 #if 0
104 static char *strlocase(char *);
105 #endif
106 static int getnettype(const char *);
107 
108 
109 /*
110  * Find the appropriate buffer size
111  *
112  * size - Size requested
113  */
114 u_int
115 /*ARGSUSED*/
__rpc_get_t_size(int af,int proto,int size)116 __rpc_get_t_size(int af, int proto, int size)
117 {
118 	int maxsize, defsize;
119 
120 	maxsize = 256 * 1024;	/* XXX */
121 	switch (proto) {
122 	case IPPROTO_TCP:
123 		defsize = 64 * 1024;	/* XXX */
124 		break;
125 	case IPPROTO_UDP:
126 		defsize = UDPMSGSIZE;
127 		break;
128 	default:
129 		defsize = RPC_MAXDATASIZE;
130 		break;
131 	}
132 	if (size == 0)
133 		return defsize;
134 
135 	/* Check whether the value is within the upper max limit */
136 	return (size > maxsize ? (u_int)maxsize : (u_int)size);
137 }
138 
139 /*
140  * Find the appropriate address buffer size
141  */
142 u_int
__rpc_get_a_size(int af)143 __rpc_get_a_size(int af)
144 {
145 	switch (af) {
146 	case AF_INET:
147 		return sizeof (struct sockaddr_in);
148 #ifdef INET6
149 	case AF_INET6:
150 		return sizeof (struct sockaddr_in6);
151 #endif
152 	case AF_LOCAL:
153 		return sizeof (struct sockaddr_un);
154 	default:
155 		break;
156 	}
157 	return ((u_int)RPC_MAXADDRSIZE);
158 }
159 
160 #if 0
161 static char *
162 strlocase(char *p)
163 {
164 	char *t = p;
165 
166 	for (; *p; p++)
167 		if (isupper(*p))
168 			*p = tolower(*p);
169 	return (t);
170 }
171 #endif
172 
173 /*
174  * Returns the type of the network as defined in <rpc/nettype.h>
175  * If nettype is NULL, it defaults to NETPATH.
176  */
177 static int
getnettype(const char * nettype)178 getnettype(const char *nettype)
179 {
180 	int i;
181 
182 	if ((nettype == NULL) || (nettype[0] == 0)) {
183 		return (_RPC_NETPATH);	/* Default */
184 	}
185 
186 #if 0
187 	nettype = strlocase(nettype);
188 #endif
189 	for (i = 0; _rpctypelist[i].name; i++)
190 		if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
191 			return (_rpctypelist[i].type);
192 		}
193 	return (_rpctypelist[i].type);
194 }
195 
196 static thread_key_t tcp_key, udp_key;
197 static once_t keys_once = ONCE_INITIALIZER;
198 static int tcp_key_error, udp_key_error;
199 
200 static void
keys_init(void)201 keys_init(void)
202 {
203 
204 	tcp_key_error = thr_keycreate(&tcp_key, free);
205 	udp_key_error = thr_keycreate(&udp_key, free);
206 }
207 
208 /*
209  * For the given nettype (tcp or udp only), return the first structure found.
210  * This should be freed by calling freenetconfigent()
211  */
212 struct netconfig *
__rpc_getconfip(const char * nettype)213 __rpc_getconfip(const char *nettype)
214 {
215 	char *netid;
216 	char *netid_tcp = (char *) NULL;
217 	char *netid_udp = (char *) NULL;
218 	static char *netid_tcp_main;
219 	static char *netid_udp_main;
220 	struct netconfig *dummy;
221 	int main_thread;
222 
223 	if ((main_thread = thr_main())) {
224 		netid_udp = netid_udp_main;
225 		netid_tcp = netid_tcp_main;
226 	} else {
227 		if (thr_once(&keys_once, keys_init) != 0 ||
228 		    tcp_key_error != 0 || udp_key_error != 0)
229 			return (NULL);
230 		netid_tcp = (char *)thr_getspecific(tcp_key);
231 		netid_udp = (char *)thr_getspecific(udp_key);
232 	}
233 	if (!netid_udp && !netid_tcp) {
234 		struct netconfig *nconf;
235 		void *confighandle;
236 
237 		if (!(confighandle = setnetconfig())) {
238 			syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
239 			return (NULL);
240 		}
241 		while ((nconf = getnetconfig(confighandle)) != NULL) {
242 			if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
243 				if (strcmp(nconf->nc_proto, NC_TCP) == 0 &&
244 				    netid_tcp == NULL) {
245 					netid_tcp = strdup(nconf->nc_netid);
246 					if (main_thread)
247 						netid_tcp_main = netid_tcp;
248 					else
249 						thr_setspecific(tcp_key,
250 							(void *) netid_tcp);
251 				} else
252 				if (strcmp(nconf->nc_proto, NC_UDP) == 0 &&
253 				    netid_udp == NULL) {
254 					netid_udp = strdup(nconf->nc_netid);
255 					if (main_thread)
256 						netid_udp_main = netid_udp;
257 					else
258 						thr_setspecific(udp_key,
259 						(void *) netid_udp);
260 				}
261 			}
262 		}
263 		endnetconfig(confighandle);
264 	}
265 	if (strcmp(nettype, "udp") == 0)
266 		netid = netid_udp;
267 	else if (strcmp(nettype, "tcp") == 0)
268 		netid = netid_tcp;
269 	else {
270 		return (NULL);
271 	}
272 	if ((netid == NULL) || (netid[0] == 0)) {
273 		return (NULL);
274 	}
275 	dummy = getnetconfigent(netid);
276 	return (dummy);
277 }
278 
279 /*
280  * Returns the type of the nettype, which should then be used with
281  * __rpc_getconf().
282  */
283 void *
__rpc_setconf(const char * nettype)284 __rpc_setconf(const char *nettype)
285 {
286 	struct handle *handle;
287 
288 	handle = (struct handle *) malloc(sizeof (struct handle));
289 	if (handle == NULL) {
290 		return (NULL);
291 	}
292 	switch (handle->nettype = getnettype(nettype)) {
293 	case _RPC_NETPATH:
294 	case _RPC_CIRCUIT_N:
295 	case _RPC_DATAGRAM_N:
296 		if (!(handle->nhandle = setnetpath()))
297 			goto failed;
298 		handle->nflag = TRUE;
299 		break;
300 	case _RPC_VISIBLE:
301 	case _RPC_CIRCUIT_V:
302 	case _RPC_DATAGRAM_V:
303 	case _RPC_TCP:
304 	case _RPC_UDP:
305 		if (!(handle->nhandle = setnetconfig())) {
306 		        syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
307 			goto failed;
308 		}
309 		handle->nflag = FALSE;
310 		break;
311 	default:
312 		goto failed;
313 	}
314 
315 	return (handle);
316 
317 failed:
318 	free(handle);
319 	return (NULL);
320 }
321 
322 /*
323  * Returns the next netconfig struct for the given "net" type.
324  * __rpc_setconf() should have been called previously.
325  */
326 struct netconfig *
__rpc_getconf(void * vhandle)327 __rpc_getconf(void *vhandle)
328 {
329 	struct handle *handle;
330 	struct netconfig *nconf;
331 
332 	handle = (struct handle *)vhandle;
333 	if (handle == NULL) {
334 		return (NULL);
335 	}
336 	for (;;) {
337 		if (handle->nflag)
338 			nconf = getnetpath(handle->nhandle);
339 		else
340 			nconf = getnetconfig(handle->nhandle);
341 		if (nconf == NULL)
342 			break;
343 		if ((nconf->nc_semantics != NC_TPI_CLTS) &&
344 			(nconf->nc_semantics != NC_TPI_COTS) &&
345 			(nconf->nc_semantics != NC_TPI_COTS_ORD))
346 			continue;
347 		switch (handle->nettype) {
348 		case _RPC_VISIBLE:
349 			if (!(nconf->nc_flag & NC_VISIBLE))
350 				continue;
351 			/* FALLTHROUGH */
352 		case _RPC_NETPATH:	/* Be happy */
353 			break;
354 		case _RPC_CIRCUIT_V:
355 			if (!(nconf->nc_flag & NC_VISIBLE))
356 				continue;
357 			/* FALLTHROUGH */
358 		case _RPC_CIRCUIT_N:
359 			if ((nconf->nc_semantics != NC_TPI_COTS) &&
360 				(nconf->nc_semantics != NC_TPI_COTS_ORD))
361 				continue;
362 			break;
363 		case _RPC_DATAGRAM_V:
364 			if (!(nconf->nc_flag & NC_VISIBLE))
365 				continue;
366 			/* FALLTHROUGH */
367 		case _RPC_DATAGRAM_N:
368 			if (nconf->nc_semantics != NC_TPI_CLTS)
369 				continue;
370 			break;
371 		case _RPC_TCP:
372 			if (((nconf->nc_semantics != NC_TPI_COTS) &&
373 				(nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
374 				(strcmp(nconf->nc_protofmly, NC_INET)
375 #ifdef INET6
376 				 && strcmp(nconf->nc_protofmly, NC_INET6))
377 #else
378 				)
379 #endif
380 				||
381 				strcmp(nconf->nc_proto, NC_TCP))
382 				continue;
383 			break;
384 		case _RPC_UDP:
385 			if ((nconf->nc_semantics != NC_TPI_CLTS) ||
386 				(strcmp(nconf->nc_protofmly, NC_INET)
387 #ifdef INET6
388 				&& strcmp(nconf->nc_protofmly, NC_INET6))
389 #else
390 				)
391 #endif
392 				||
393 				strcmp(nconf->nc_proto, NC_UDP))
394 				continue;
395 			break;
396 		}
397 		break;
398 	}
399 	return (nconf);
400 }
401 
402 void
__rpc_endconf(void * vhandle)403 __rpc_endconf(void *vhandle)
404 {
405 	struct handle *handle;
406 
407 	handle = (struct handle *) vhandle;
408 	if (handle == NULL) {
409 		return;
410 	}
411 	if (handle->nflag) {
412 		endnetpath(handle->nhandle);
413 	} else {
414 		endnetconfig(handle->nhandle);
415 	}
416 	free(handle);
417 }
418 
419 /*
420  * Used to ping the NULL procedure for clnt handle.
421  * Returns NULL if fails, else a non-NULL pointer.
422  */
423 void *
rpc_nullproc(CLIENT * clnt)424 rpc_nullproc(CLIENT *clnt)
425 {
426 	struct timeval TIMEOUT = {25, 0};
427 
428 	if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL,
429 		(xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
430 		return (NULL);
431 	}
432 	return ((void *) clnt);
433 }
434 
435 /*
436  * Try all possible transports until
437  * one succeeds in finding the netconf for the given fd.
438  */
439 struct netconfig *
__rpcgettp(int fd)440 __rpcgettp(int fd)
441 {
442 	const char *netid;
443 	struct __rpc_sockinfo si;
444 
445 	if (!__rpc_fd2sockinfo(fd, &si))
446 		return NULL;
447 
448 	if (!__rpc_sockinfo2netid(&si, &netid))
449 		return NULL;
450 
451 	/*LINTED const castaway*/
452 	return getnetconfigent((char *)netid);
453 }
454 
455 int
__rpc_fd2sockinfo(int fd,struct __rpc_sockinfo * sip)456 __rpc_fd2sockinfo(int fd, struct __rpc_sockinfo *sip)
457 {
458 	socklen_t len;
459 	int type, proto;
460 	struct sockaddr_storage ss;
461 
462 	len = sizeof ss;
463 	if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &len) < 0)
464 		return 0;
465 	sip->si_alen = len;
466 
467 	len = sizeof type;
468 	if (_getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &len) < 0)
469 		return 0;
470 
471 	/* XXX */
472 	if (ss.ss_family != AF_LOCAL) {
473 		if (type == SOCK_STREAM)
474 			proto = IPPROTO_TCP;
475 		else if (type == SOCK_DGRAM)
476 			proto = IPPROTO_UDP;
477 		else
478 			return 0;
479 	} else
480 		proto = 0;
481 
482 	sip->si_af = ss.ss_family;
483 	sip->si_proto = proto;
484 	sip->si_socktype = type;
485 
486 	return 1;
487 }
488 
489 /*
490  * Linear search, but the number of entries is small.
491  */
492 int
__rpc_nconf2sockinfo(const struct netconfig * nconf,struct __rpc_sockinfo * sip)493 __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip)
494 {
495 	int i;
496 
497 	for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
498 		if (strcmp(na_cvt[i].netid, nconf->nc_netid) == 0 || (
499 		    strcmp(nconf->nc_netid, "unix") == 0 &&
500 		    strcmp(na_cvt[i].netid, "local") == 0)) {
501 			sip->si_af = na_cvt[i].af;
502 			sip->si_proto = na_cvt[i].protocol;
503 			sip->si_socktype =
504 			    __rpc_seman2socktype((int)nconf->nc_semantics);
505 			if (sip->si_socktype == -1)
506 				return 0;
507 			sip->si_alen = __rpc_get_a_size(sip->si_af);
508 			return 1;
509 		}
510 
511 	return 0;
512 }
513 
514 int
__rpc_nconf2fd(const struct netconfig * nconf)515 __rpc_nconf2fd(const struct netconfig *nconf)
516 {
517 	struct __rpc_sockinfo si;
518 
519 	if (!__rpc_nconf2sockinfo(nconf, &si))
520 		return 0;
521 
522 	return _socket(si.si_af, si.si_socktype, si.si_proto);
523 }
524 
525 int
__rpc_sockinfo2netid(struct __rpc_sockinfo * sip,const char ** netid)526 __rpc_sockinfo2netid(struct __rpc_sockinfo *sip, const char **netid)
527 {
528 	int i;
529 	struct netconfig *nconf;
530 
531 	nconf = getnetconfigent("local");
532 
533 	for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++) {
534 		if (na_cvt[i].af == sip->si_af &&
535 		    na_cvt[i].protocol == sip->si_proto) {
536 			if (strcmp(na_cvt[i].netid, "local") == 0 && nconf == NULL) {
537 				if (netid)
538 					*netid = "unix";
539 			} else {
540 				if (netid)
541 					*netid = na_cvt[i].netid;
542 			}
543 			if (nconf != NULL)
544 				freenetconfigent(nconf);
545 			return 1;
546 		}
547 	}
548 	if (nconf != NULL)
549 		freenetconfigent(nconf);
550 
551 	return 0;
552 }
553 
554 char *
taddr2uaddr(const struct netconfig * nconf,const struct netbuf * nbuf)555 taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
556 {
557 	struct __rpc_sockinfo si;
558 
559 	if (!__rpc_nconf2sockinfo(nconf, &si))
560 		return NULL;
561 	return __rpc_taddr2uaddr_af(si.si_af, nbuf);
562 }
563 
564 struct netbuf *
uaddr2taddr(const struct netconfig * nconf,const char * uaddr)565 uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
566 {
567 	struct __rpc_sockinfo si;
568 
569 	if (!__rpc_nconf2sockinfo(nconf, &si))
570 		return NULL;
571 	return __rpc_uaddr2taddr_af(si.si_af, uaddr);
572 }
573 
574 char *
__rpc_taddr2uaddr_af(int af,const struct netbuf * nbuf)575 __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
576 {
577 	char *ret;
578 	struct sockaddr_in *sin;
579 	struct sockaddr_un *sun;
580 	char namebuf[INET_ADDRSTRLEN];
581 #ifdef INET6
582 	struct sockaddr_in6 *sin6;
583 	char namebuf6[INET6_ADDRSTRLEN];
584 #endif
585 	u_int16_t port;
586 
587 	switch (af) {
588 	case AF_INET:
589 		if (nbuf->len < sizeof(*sin))
590 			return NULL;
591 		sin = nbuf->buf;
592 		if (inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
593 		    == NULL)
594 			return NULL;
595 		port = ntohs(sin->sin_port);
596 		if (asprintf(&ret, "%s.%u.%u", namebuf, ((u_int32_t)port) >> 8,
597 		    port & 0xff) < 0)
598 			return NULL;
599 		break;
600 #ifdef INET6
601 	case AF_INET6:
602 		if (nbuf->len < sizeof(*sin6))
603 			return NULL;
604 		sin6 = nbuf->buf;
605 		if (inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
606 		    == NULL)
607 			return NULL;
608 		port = ntohs(sin6->sin6_port);
609 		if (asprintf(&ret, "%s.%u.%u", namebuf6, ((u_int32_t)port) >> 8,
610 		    port & 0xff) < 0)
611 			return NULL;
612 		break;
613 #endif
614 	case AF_LOCAL:
615 		sun = nbuf->buf;
616 		if (asprintf(&ret, "%.*s", (int)(sun->sun_len -
617 		    offsetof(struct sockaddr_un, sun_path)),
618 		    sun->sun_path) < 0)
619 			return (NULL);
620 		break;
621 	default:
622 		return NULL;
623 	}
624 
625 	return ret;
626 }
627 
628 struct netbuf *
__rpc_uaddr2taddr_af(int af,const char * uaddr)629 __rpc_uaddr2taddr_af(int af, const char *uaddr)
630 {
631 	struct netbuf *ret = NULL;
632 	char *addrstr, *p;
633 	unsigned port, portlo, porthi;
634 	struct sockaddr_in *sin;
635 #ifdef INET6
636 	struct sockaddr_in6 *sin6;
637 #endif
638 	struct sockaddr_un *sun;
639 
640 	port = 0;
641 	sin = NULL;
642 
643 	if (uaddr == NULL)
644 		return NULL;
645 
646 	addrstr = strdup(uaddr);
647 	if (addrstr == NULL)
648 		return NULL;
649 
650 	/*
651 	 * AF_LOCAL addresses are expected to be absolute
652 	 * pathnames, anything else will be AF_INET or AF_INET6.
653 	 */
654 	if (*addrstr != '/') {
655 		p = strrchr(addrstr, '.');
656 		if (p == NULL)
657 			goto out;
658 		portlo = (unsigned)atoi(p + 1);
659 		*p = '\0';
660 
661 		p = strrchr(addrstr, '.');
662 		if (p == NULL)
663 			goto out;
664 		porthi = (unsigned)atoi(p + 1);
665 		*p = '\0';
666 		port = (porthi << 8) | portlo;
667 	}
668 
669 	ret = (struct netbuf *)malloc(sizeof *ret);
670 	if (ret == NULL)
671 		goto out;
672 
673 	switch (af) {
674 	case AF_INET:
675 		sin = (struct sockaddr_in *)malloc(sizeof *sin);
676 		if (sin == NULL)
677 			goto out;
678 		memset(sin, 0, sizeof *sin);
679 		sin->sin_family = AF_INET;
680 		sin->sin_port = htons(port);
681 		if (inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) {
682 			free(sin);
683 			free(ret);
684 			ret = NULL;
685 			goto out;
686 		}
687 		sin->sin_len = ret->maxlen = ret->len = sizeof *sin;
688 		ret->buf = sin;
689 		break;
690 #ifdef INET6
691 	case AF_INET6:
692 		sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6);
693 		if (sin6 == NULL)
694 			goto out;
695 		memset(sin6, 0, sizeof *sin6);
696 		sin6->sin6_family = AF_INET6;
697 		sin6->sin6_port = htons(port);
698 		if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) {
699 			free(sin6);
700 			free(ret);
701 			ret = NULL;
702 			goto out;
703 		}
704 		sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6;
705 		ret->buf = sin6;
706 		break;
707 #endif
708 	case AF_LOCAL:
709 		sun = (struct sockaddr_un *)malloc(sizeof *sun);
710 		if (sun == NULL)
711 			goto out;
712 		memset(sun, 0, sizeof *sun);
713 		sun->sun_family = AF_LOCAL;
714 		strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1);
715 		ret->len = ret->maxlen = sun->sun_len = SUN_LEN(sun);
716 		ret->buf = sun;
717 		break;
718 	default:
719 		break;
720 	}
721 out:
722 	free(addrstr);
723 	return ret;
724 }
725 
726 int
__rpc_seman2socktype(int semantics)727 __rpc_seman2socktype(int semantics)
728 {
729 	switch (semantics) {
730 	case NC_TPI_CLTS:
731 		return SOCK_DGRAM;
732 	case NC_TPI_COTS_ORD:
733 		return SOCK_STREAM;
734 	case NC_TPI_RAW:
735 		return SOCK_RAW;
736 	default:
737 		break;
738 	}
739 
740 	return -1;
741 }
742 
743 int
__rpc_socktype2seman(int socktype)744 __rpc_socktype2seman(int socktype)
745 {
746 	switch (socktype) {
747 	case SOCK_DGRAM:
748 		return NC_TPI_CLTS;
749 	case SOCK_STREAM:
750 		return NC_TPI_COTS_ORD;
751 	case SOCK_RAW:
752 		return NC_TPI_RAW;
753 	default:
754 		break;
755 	}
756 
757 	return -1;
758 }
759 
760 /*
761  * XXXX - IPv6 scope IDs can't be handled in universal addresses.
762  * Here, we compare the original server address to that of the RPC
763  * service we just received back from a call to rpcbind on the remote
764  * machine. If they are both "link local" or "site local", copy
765  * the scope id of the server address over to the service address.
766  */
767 int
__rpc_fixup_addr(struct netbuf * new,const struct netbuf * svc)768 __rpc_fixup_addr(struct netbuf *new, const struct netbuf *svc)
769 {
770 #ifdef INET6
771 	struct sockaddr *sa_new, *sa_svc;
772 	struct sockaddr_in6 *sin6_new, *sin6_svc;
773 
774 	sa_svc = (struct sockaddr *)svc->buf;
775 	sa_new = (struct sockaddr *)new->buf;
776 
777 	if (sa_new->sa_family == sa_svc->sa_family &&
778 	    sa_new->sa_family == AF_INET6) {
779 		sin6_new = (struct sockaddr_in6 *)new->buf;
780 		sin6_svc = (struct sockaddr_in6 *)svc->buf;
781 
782 		if ((IN6_IS_ADDR_LINKLOCAL(&sin6_new->sin6_addr) &&
783 		     IN6_IS_ADDR_LINKLOCAL(&sin6_svc->sin6_addr)) ||
784 		    (IN6_IS_ADDR_SITELOCAL(&sin6_new->sin6_addr) &&
785 		     IN6_IS_ADDR_SITELOCAL(&sin6_svc->sin6_addr))) {
786 			sin6_new->sin6_scope_id = sin6_svc->sin6_scope_id;
787 		}
788 	}
789 #endif
790 	return 1;
791 }
792 
793 int
__rpc_sockisbound(int fd)794 __rpc_sockisbound(int fd)
795 {
796 	struct sockaddr_storage ss;
797 	socklen_t slen;
798 
799 	slen = sizeof (struct sockaddr_storage);
800 	if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0)
801 		return 0;
802 
803 	switch (ss.ss_family) {
804 		case AF_INET:
805 			return (((struct sockaddr_in *)
806 			    (void *)&ss)->sin_port != 0);
807 #ifdef INET6
808 		case AF_INET6:
809 			return (((struct sockaddr_in6 *)
810 			    (void *)&ss)->sin6_port != 0);
811 #endif
812 		case AF_LOCAL:
813 			/* XXX check this */
814 			return (((struct sockaddr_un *)
815 			    (void *)&ss)->sun_path[0] != '\0');
816 		default:
817 			break;
818 	}
819 
820 	return 0;
821 }
822