rpc_generic.c revision 109951
1249259Sdim/*	$NetBSD: rpc_generic.c,v 1.4 2000/09/28 09:07:04 kleink Exp $	*/
2249259Sdim
3249259Sdim/*
4249259Sdim * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5249259Sdim * unrestricted use provided that this legend is included on all tape
6249259Sdim * media and as a part of the software program in whole or part.  Users
7249259Sdim * may copy or modify Sun RPC without charge, but are not authorized
8249259Sdim * to license or distribute it to anyone else except as part of a product or
9249259Sdim * program developed by the user.
10249259Sdim *
11249259Sdim * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12249259Sdim * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13249259Sdim * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14249259Sdim *
15249259Sdim * Sun RPC is provided with no support and without any obligation on the
16249259Sdim * part of Sun Microsystems, Inc. to assist in its use, correction,
17249259Sdim * modification or enhancement.
18249259Sdim *
19249259Sdim * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20249259Sdim * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21249259Sdim * OR ANY PART THEREOF.
22249259Sdim *
23249259Sdim * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24249259Sdim * or profits or other special, indirect and consequential damages, even if
25249259Sdim * Sun has been advised of the possibility of such damages.
26249259Sdim *
27249259Sdim * Sun Microsystems, Inc.
28249259Sdim * 2550 Garcia Avenue
29249259Sdim * Mountain View, California  94043
30249259Sdim */
31249259Sdim/*
32249259Sdim * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33249259Sdim */
34249259Sdim
35249259Sdim/* #pragma ident	"@(#)rpc_generic.c	1.17	94/04/24 SMI" */
36249259Sdim#include <sys/cdefs.h>
37249259Sdim__FBSDID("$FreeBSD: head/lib/libc/rpc/rpc_generic.c 109951 2003-01-27 22:22:59Z mbr $");
38249259Sdim
39249259Sdim/*
40249259Sdim * rpc_generic.c, Miscl routines for RPC.
41249259Sdim *
42249259Sdim */
43249259Sdim
44249259Sdim#include "namespace.h"
45249259Sdim#include "reentrant.h"
46249259Sdim#include <sys/types.h>
47249259Sdim#include <sys/param.h>
48249259Sdim#include <sys/socket.h>
49249259Sdim#include <sys/time.h>
50249259Sdim#include <sys/un.h>
51249259Sdim#include <sys/resource.h>
52249259Sdim#include <netinet/in.h>
53249259Sdim#include <arpa/inet.h>
54249259Sdim#include <rpc/rpc.h>
55249259Sdim#include <ctype.h>
56249259Sdim#include <stddef.h>
57249259Sdim#include <stdio.h>
58249259Sdim#include <netdb.h>
59249259Sdim#include <netconfig.h>
60249259Sdim#include <stdlib.h>
61249259Sdim#include <string.h>
62249259Sdim#include <syslog.h>
63249259Sdim#include <rpc/nettype.h>
64249259Sdim#include "un-namespace.h"
65249259Sdim#include "rpc_com.h"
66249259Sdim
67249259Sdimstruct handle {
68249259Sdim	NCONF_HANDLE *nhandle;
69249259Sdim	int nflag;		/* Whether NETPATH or NETCONFIG */
70249259Sdim	int nettype;
71249259Sdim};
72249259Sdim
73249259Sdimstatic const struct _rpcnettype {
74249259Sdim	const char *name;
75249259Sdim	const int type;
76249259Sdim} _rpctypelist[] = {
77249259Sdim	{ "netpath", _RPC_NETPATH },
78249259Sdim	{ "visible", _RPC_VISIBLE },
79249259Sdim	{ "circuit_v", _RPC_CIRCUIT_V },
80249259Sdim	{ "datagram_v", _RPC_DATAGRAM_V },
81249259Sdim	{ "circuit_n", _RPC_CIRCUIT_N },
82249259Sdim	{ "datagram_n", _RPC_DATAGRAM_N },
83249259Sdim	{ "tcp", _RPC_TCP },
84249259Sdim	{ "udp", _RPC_UDP },
85249259Sdim	{ 0, _RPC_NONE }
86249259Sdim};
87249259Sdim
88249259Sdimstruct netid_af {
89249259Sdim	const char	*netid;
90249259Sdim	int		af;
91249259Sdim	int		protocol;
92249259Sdim};
93249259Sdim
94249259Sdimstatic const struct netid_af na_cvt[] = {
95249259Sdim	{ "udp",  AF_INET,  IPPROTO_UDP },
96249259Sdim	{ "tcp",  AF_INET,  IPPROTO_TCP },
97249259Sdim#ifdef INET6
98249259Sdim	{ "udp6", AF_INET6, IPPROTO_UDP },
99249259Sdim	{ "tcp6", AF_INET6, IPPROTO_TCP },
100249259Sdim#endif
101249259Sdim	{ "local", AF_LOCAL, 0 }
102249259Sdim};
103249259Sdim
104249259Sdim#if 0
105249259Sdimstatic char *strlocase(char *);
106249259Sdim#endif
107249259Sdimstatic int getnettype(const char *);
108249259Sdim
109249259Sdim/*
110249259Sdim * Cache the result of getrlimit(), so we don't have to do an
111249259Sdim * expensive call every time.
112249259Sdim */
113249259Sdimint
114249259Sdim__rpc_dtbsize()
115249259Sdim{
116249259Sdim	static int tbsize;
117249259Sdim	struct rlimit rl;
118249259Sdim
119249259Sdim	if (tbsize) {
120249259Sdim		return (tbsize);
121249259Sdim	}
122249259Sdim	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
123249259Sdim		return (tbsize = (int)rl.rlim_max);
124249259Sdim	}
125249259Sdim	/*
126249259Sdim	 * Something wrong.  I'll try to save face by returning a
127249259Sdim	 * pessimistic number.
128249259Sdim	 */
129249259Sdim	return (32);
130249259Sdim}
131249259Sdim
132249259Sdim
133249259Sdim/*
134249259Sdim * Find the appropriate buffer size
135249259Sdim */
136249259Sdimu_int
137249259Sdim/*ARGSUSED*/
138249259Sdim__rpc_get_t_size(af, proto, size)
139249259Sdim	int af, proto;
140249259Sdim	int size;	/* Size requested */
141249259Sdim{
142249259Sdim	int maxsize, defsize;
143249259Sdim
144249259Sdim	maxsize = 256 * 1024;	/* XXX */
145249259Sdim	switch (proto) {
146249259Sdim	case IPPROTO_TCP:
147249259Sdim		defsize = 64 * 1024;	/* XXX */
148249259Sdim		break;
149249259Sdim	case IPPROTO_UDP:
150		defsize = UDPMSGSIZE;
151		break;
152	default:
153		defsize = RPC_MAXDATASIZE;
154		break;
155	}
156	if (size == 0)
157		return defsize;
158
159	/* Check whether the value is within the upper max limit */
160	return (size > maxsize ? (u_int)maxsize : (u_int)size);
161}
162
163/*
164 * Find the appropriate address buffer size
165 */
166u_int
167__rpc_get_a_size(af)
168	int af;
169{
170	switch (af) {
171	case AF_INET:
172		return sizeof (struct sockaddr_in);
173#ifdef INET6
174	case AF_INET6:
175		return sizeof (struct sockaddr_in6);
176#endif
177	case AF_LOCAL:
178		return sizeof (struct sockaddr_un);
179	default:
180		break;
181	}
182	return ((u_int)RPC_MAXADDRSIZE);
183}
184
185#if 0
186static char *
187strlocase(p)
188	char *p;
189{
190	char *t = p;
191
192	for (; *p; p++)
193		if (isupper(*p))
194			*p = tolower(*p);
195	return (t);
196}
197#endif
198
199/*
200 * Returns the type of the network as defined in <rpc/nettype.h>
201 * If nettype is NULL, it defaults to NETPATH.
202 */
203static int
204getnettype(nettype)
205	const char *nettype;
206{
207	int i;
208
209	if ((nettype == NULL) || (nettype[0] == NULL)) {
210		return (_RPC_NETPATH);	/* Default */
211	}
212
213#if 0
214	nettype = strlocase(nettype);
215#endif
216	for (i = 0; _rpctypelist[i].name; i++)
217		if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
218			return (_rpctypelist[i].type);
219		}
220	return (_rpctypelist[i].type);
221}
222
223/*
224 * For the given nettype (tcp or udp only), return the first structure found.
225 * This should be freed by calling freenetconfigent()
226 */
227struct netconfig *
228__rpc_getconfip(nettype)
229	const char *nettype;
230{
231	char *netid;
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;
237	int main_thread;
238	static thread_key_t tcp_key, udp_key;
239	extern mutex_t tsd_lock;
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] == NULL)) {
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 */
308void *
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 */
351struct 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
428void
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 */
450void *
451rpc_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 */
467struct 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
484int
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 */
521int
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
543int
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
554int
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
583char *
584taddr2uaddr(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
593struct netbuf *
594uaddr2taddr(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
603char *
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
653struct 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	}
742out:
743	free(addrstr);
744	return ret;
745}
746
747int
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
764int
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 */
788int
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
814int
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