rpc_generic.c revision 92990
1130803Smarcel/*	$NetBSD: rpc_generic.c,v 1.4 2000/09/28 09:07:04 kleink Exp $	*/
2130803Smarcel
3130803Smarcel/*
4130803Smarcel * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5130803Smarcel * unrestricted use provided that this legend is included on all tape
6130803Smarcel * media and as a part of the software program in whole or part.  Users
7130803Smarcel * may copy or modify Sun RPC without charge, but are not authorized
8130803Smarcel * to license or distribute it to anyone else except as part of a product or
9130803Smarcel * program developed by the user.
10130803Smarcel *
11130803Smarcel * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12130803Smarcel * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13130803Smarcel * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14130803Smarcel *
15130803Smarcel * Sun RPC is provided with no support and without any obligation on the
16130803Smarcel * part of Sun Microsystems, Inc. to assist in its use, correction,
17130803Smarcel * modification or enhancement.
18130803Smarcel *
19130803Smarcel * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20130803Smarcel * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21130803Smarcel * OR ANY PART THEREOF.
22130803Smarcel *
23130803Smarcel * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24130803Smarcel * or profits or other special, indirect and consequential damages, even if
25130803Smarcel * Sun has been advised of the possibility of such damages.
26130803Smarcel *
27130803Smarcel * Sun Microsystems, Inc.
28130803Smarcel * 2550 Garcia Avenue
29130803Smarcel * Mountain View, California  94043
30130803Smarcel */
31130803Smarcel/*
32130803Smarcel * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33130803Smarcel */
34130803Smarcel
35130803Smarcel/* #pragma ident	"@(#)rpc_generic.c	1.17	94/04/24 SMI" */
36130803Smarcel#include <sys/cdefs.h>
37130803Smarcel__FBSDID("$FreeBSD: head/lib/libc/rpc/rpc_generic.c 92990 2002-03-22 23:18:37Z obrien $");
38130803Smarcel
39130803Smarcel/*
40130803Smarcel * rpc_generic.c, Miscl routines for RPC.
41130803Smarcel *
42130803Smarcel */
43130803Smarcel
44130803Smarcel#include "namespace.h"
45130803Smarcel#include "reentrant.h"
46130803Smarcel#include <sys/types.h>
47130803Smarcel#include <sys/param.h>
48130803Smarcel#include <sys/socket.h>
49130803Smarcel#include <sys/time.h>
50130803Smarcel#include <sys/un.h>
51130803Smarcel#include <sys/resource.h>
52130803Smarcel#include <netinet/in.h>
53130803Smarcel#include <arpa/inet.h>
54130803Smarcel#include <rpc/rpc.h>
55130803Smarcel#include <ctype.h>
56130803Smarcel#include <stddef.h>
57130803Smarcel#include <stdio.h>
58130803Smarcel#include <netdb.h>
59130803Smarcel#include <netconfig.h>
60130803Smarcel#include <stdlib.h>
61130803Smarcel#include <string.h>
62130803Smarcel#include <syslog.h>
63130803Smarcel#include <rpc/nettype.h>
64130803Smarcel#include "un-namespace.h"
65130803Smarcel#include "rpc_com.h"
66130803Smarcel
67130803Smarcelstruct handle {
68130803Smarcel	NCONF_HANDLE *nhandle;
69130803Smarcel	int nflag;		/* Whether NETPATH or NETCONFIG */
70130803Smarcel	int nettype;
71130803Smarcel};
72130803Smarcel
73130803Smarcelstatic const struct _rpcnettype {
74130803Smarcel	const char *name;
75130803Smarcel	const int type;
76130803Smarcel} _rpctypelist[] = {
77130803Smarcel	{ "netpath", _RPC_NETPATH },
78130803Smarcel	{ "visible", _RPC_VISIBLE },
79130803Smarcel	{ "circuit_v", _RPC_CIRCUIT_V },
80130803Smarcel	{ "datagram_v", _RPC_DATAGRAM_V },
81130803Smarcel	{ "circuit_n", _RPC_CIRCUIT_N },
82130803Smarcel	{ "datagram_n", _RPC_DATAGRAM_N },
83130803Smarcel	{ "tcp", _RPC_TCP },
84130803Smarcel	{ "udp", _RPC_UDP },
85130803Smarcel	{ 0, _RPC_NONE }
86130803Smarcel};
87130803Smarcel
88130803Smarcelstruct netid_af {
89130803Smarcel	const char	*netid;
90130803Smarcel	int		af;
91130803Smarcel	int		protocol;
92130803Smarcel};
93130803Smarcel
94130803Smarcelstatic const struct netid_af na_cvt[] = {
95130803Smarcel	{ "udp",  AF_INET,  IPPROTO_UDP },
96130803Smarcel	{ "tcp",  AF_INET,  IPPROTO_TCP },
97130803Smarcel#ifdef INET6
98130803Smarcel	{ "udp6", AF_INET6, IPPROTO_UDP },
99130803Smarcel	{ "tcp6", AF_INET6, IPPROTO_TCP },
100130803Smarcel#endif
101130803Smarcel	{ "unix", AF_LOCAL, 0 }
102130803Smarcel};
103130803Smarcel
104130803Smarcel#if 0
105130803Smarcelstatic char *strlocase(char *);
106130803Smarcel#endif
107130803Smarcelstatic int getnettype(const char *);
108130803Smarcel
109130803Smarcel/*
110130803Smarcel * Cache the result of getrlimit(), so we don't have to do an
111130803Smarcel * expensive call every time.
112130803Smarcel */
113130803Smarcelint
114130803Smarcel__rpc_dtbsize()
115130803Smarcel{
116130803Smarcel	static int tbsize;
117130803Smarcel	struct rlimit rl;
118130803Smarcel
119130803Smarcel	if (tbsize) {
120130803Smarcel		return (tbsize);
121130803Smarcel	}
122130803Smarcel	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
123130803Smarcel		return (tbsize = (int)rl.rlim_max);
124130803Smarcel	}
125130803Smarcel	/*
126130803Smarcel	 * Something wrong.  I'll try to save face by returning a
127130803Smarcel	 * pessimistic number.
128130803Smarcel	 */
129130803Smarcel	return (32);
130130803Smarcel}
131130803Smarcel
132130803Smarcel
133130803Smarcel/*
134130803Smarcel * Find the appropriate buffer size
135130803Smarcel */
136130803Smarcelu_int
137130803Smarcel/*ARGSUSED*/
138130803Smarcel__rpc_get_t_size(af, proto, size)
139130803Smarcel	int af, proto;
140130803Smarcel	int size;	/* Size requested */
141130803Smarcel{
142130803Smarcel	int maxsize, defsize;
143130803Smarcel
144130803Smarcel	maxsize = 256 * 1024;	/* XXX */
145130803Smarcel	switch (proto) {
146130803Smarcel	case IPPROTO_TCP:
147130803Smarcel		defsize = 64 * 1024;	/* XXX */
148130803Smarcel		break;
149130803Smarcel	case IPPROTO_UDP:
150130803Smarcel		defsize = UDPMSGSIZE;
151130803Smarcel		break;
152130803Smarcel	default:
153130803Smarcel		defsize = RPC_MAXDATASIZE;
154130803Smarcel		break;
155130803Smarcel	}
156130803Smarcel	if (size == 0)
157130803Smarcel		return defsize;
158130803Smarcel
159130803Smarcel	/* Check whether the value is within the upper max limit */
160130803Smarcel	return (size > maxsize ? (u_int)maxsize : (u_int)size);
161130803Smarcel}
162130803Smarcel
163130803Smarcel/*
164130803Smarcel * Find the appropriate address buffer size
165130803Smarcel */
166130803Smarcelu_int
167130803Smarcel__rpc_get_a_size(af)
168130803Smarcel	int af;
169130803Smarcel{
170130803Smarcel	switch (af) {
171130803Smarcel	case AF_INET:
172130803Smarcel		return sizeof (struct sockaddr_in);
173130803Smarcel#ifdef INET6
174130803Smarcel	case AF_INET6:
175130803Smarcel		return sizeof (struct sockaddr_in6);
176130803Smarcel#endif
177130803Smarcel	case AF_LOCAL:
178130803Smarcel		return sizeof (struct sockaddr_un);
179130803Smarcel	default:
180130803Smarcel		break;
181130803Smarcel	}
182130803Smarcel	return ((u_int)RPC_MAXADDRSIZE);
183130803Smarcel}
184130803Smarcel
185130803Smarcel#if 0
186130803Smarcelstatic char *
187130803Smarcelstrlocase(p)
188130803Smarcel	char *p;
189130803Smarcel{
190130803Smarcel	char *t = p;
191130803Smarcel
192130803Smarcel	for (; *p; p++)
193130803Smarcel		if (isupper(*p))
194130803Smarcel			*p = tolower(*p);
195130803Smarcel	return (t);
196130803Smarcel}
197130803Smarcel#endif
198130803Smarcel
199130803Smarcel/*
200130803Smarcel * Returns the type of the network as defined in <rpc/nettype.h>
201130803Smarcel * If nettype is NULL, it defaults to NETPATH.
202130803Smarcel */
203130803Smarcelstatic int
204130803Smarcelgetnettype(nettype)
205130803Smarcel	const char *nettype;
206130803Smarcel{
207130803Smarcel	int i;
208130803Smarcel
209130803Smarcel	if ((nettype == NULL) || (nettype[0] == NULL)) {
210130803Smarcel		return (_RPC_NETPATH);	/* Default */
211130803Smarcel	}
212130803Smarcel
213130803Smarcel#if 0
214130803Smarcel	nettype = strlocase(nettype);
215130803Smarcel#endif
216130803Smarcel	for (i = 0; _rpctypelist[i].name; i++)
217130803Smarcel		if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
218130803Smarcel			return (_rpctypelist[i].type);
219130803Smarcel		}
220130803Smarcel	return (_rpctypelist[i].type);
221130803Smarcel}
222130803Smarcel
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)) {
528			sip->si_af = na_cvt[i].af;
529			sip->si_proto = na_cvt[i].protocol;
530			sip->si_socktype =
531			    __rpc_seman2socktype((int)nconf->nc_semantics);
532			if (sip->si_socktype == -1)
533				return 0;
534			sip->si_alen = __rpc_get_a_size(sip->si_af);
535			return 1;
536		}
537
538	return 0;
539}
540
541int
542__rpc_nconf2fd(const struct netconfig *nconf)
543{
544	struct __rpc_sockinfo si;
545
546	if (!__rpc_nconf2sockinfo(nconf, &si))
547		return 0;
548
549	return _socket(si.si_af, si.si_socktype, si.si_proto);
550}
551
552int
553__rpc_sockinfo2netid(struct __rpc_sockinfo *sip, const char **netid)
554{
555	int i;
556
557	for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
558		if (na_cvt[i].af == sip->si_af &&
559		    na_cvt[i].protocol == sip->si_proto) {
560			if (netid)
561				*netid = na_cvt[i].netid;
562			return 1;
563		}
564
565	return 0;
566}
567
568char *
569taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
570{
571	struct __rpc_sockinfo si;
572
573	if (!__rpc_nconf2sockinfo(nconf, &si))
574		return NULL;
575	return __rpc_taddr2uaddr_af(si.si_af, nbuf);
576}
577
578struct netbuf *
579uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
580{
581	struct __rpc_sockinfo si;
582
583	if (!__rpc_nconf2sockinfo(nconf, &si))
584		return NULL;
585	return __rpc_uaddr2taddr_af(si.si_af, uaddr);
586}
587
588char *
589__rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
590{
591	char *ret;
592	struct sockaddr_in *sin;
593	struct sockaddr_un *sun;
594	char namebuf[INET_ADDRSTRLEN];
595#ifdef INET6
596	struct sockaddr_in6 *sin6;
597	char namebuf6[INET6_ADDRSTRLEN];
598#endif
599	u_int16_t port;
600
601	switch (af) {
602	case AF_INET:
603		sin = nbuf->buf;
604		if (inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
605		    == NULL)
606			return NULL;
607		port = ntohs(sin->sin_port);
608		if (asprintf(&ret, "%s.%u.%u", namebuf, ((u_int32_t)port) >> 8,
609		    port & 0xff) < 0)
610			return NULL;
611		break;
612#ifdef INET6
613	case AF_INET6:
614		sin6 = nbuf->buf;
615		if (inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
616		    == NULL)
617			return NULL;
618		port = ntohs(sin6->sin6_port);
619		if (asprintf(&ret, "%s.%u.%u", namebuf6, ((u_int32_t)port) >> 8,
620		    port & 0xff) < 0)
621			return NULL;
622		break;
623#endif
624	case AF_LOCAL:
625		sun = nbuf->buf;
626		if (asprintf(&ret, "%.*s", (int)(sun->sun_len -
627		    offsetof(struct sockaddr_un, sun_path)),
628		    sun->sun_path) < 0)
629			return (NULL);
630		break;
631	default:
632		return NULL;
633	}
634
635	return ret;
636}
637
638struct netbuf *
639__rpc_uaddr2taddr_af(int af, const char *uaddr)
640{
641	struct netbuf *ret = NULL;
642	char *addrstr, *p;
643	unsigned port, portlo, porthi;
644	struct sockaddr_in *sin;
645#ifdef INET6
646	struct sockaddr_in6 *sin6;
647#endif
648	struct sockaddr_un *sun;
649
650	port = 0;
651	sin = NULL;
652	addrstr = strdup(uaddr);
653	if (addrstr == NULL)
654		return NULL;
655
656	/*
657	 * AF_LOCAL addresses are expected to be absolute
658	 * pathnames, anything else will be AF_INET or AF_INET6.
659	 */
660	if (*addrstr != '/') {
661		p = strrchr(addrstr, '.');
662		if (p == NULL)
663			goto out;
664		portlo = (unsigned)atoi(p + 1);
665		*p = '\0';
666
667		p = strrchr(addrstr, '.');
668		if (p == NULL)
669			goto out;
670		porthi = (unsigned)atoi(p + 1);
671		*p = '\0';
672		port = (porthi << 8) | portlo;
673	}
674
675	ret = (struct netbuf *)malloc(sizeof *ret);
676
677	switch (af) {
678	case AF_INET:
679		sin = (struct sockaddr_in *)malloc(sizeof *sin);
680		if (sin == NULL)
681			goto out;
682		memset(sin, 0, sizeof *sin);
683		sin->sin_family = AF_INET;
684		sin->sin_port = htons(port);
685		if (inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) {
686			free(sin);
687			free(ret);
688			ret = NULL;
689			goto out;
690		}
691		sin->sin_len = ret->maxlen = ret->len = sizeof *sin;
692		ret->buf = sin;
693		break;
694#ifdef INET6
695	case AF_INET6:
696		sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6);
697		if (sin6 == NULL)
698			goto out;
699		memset(sin6, 0, sizeof *sin6);
700		sin6->sin6_family = AF_INET6;
701		sin6->sin6_port = htons(port);
702		if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) {
703			free(sin);
704			free(ret);
705			ret = NULL;
706			goto out;
707		}
708		sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6;
709		ret->buf = sin6;
710		break;
711#endif
712	case AF_LOCAL:
713		sun = (struct sockaddr_un *)malloc(sizeof *sun);
714		if (sun == NULL)
715			goto out;
716		memset(sun, 0, sizeof *sun);
717		sun->sun_family = AF_LOCAL;
718		strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1);
719		ret->len = ret->maxlen = sun->sun_len = SUN_LEN(sun);
720		ret->buf = sun;
721		break;
722	default:
723		break;
724	}
725out:
726	free(addrstr);
727	return ret;
728}
729
730int
731__rpc_seman2socktype(int semantics)
732{
733	switch (semantics) {
734	case NC_TPI_CLTS:
735		return SOCK_DGRAM;
736	case NC_TPI_COTS_ORD:
737		return SOCK_STREAM;
738	case NC_TPI_RAW:
739		return SOCK_RAW;
740	default:
741		break;
742	}
743
744	return -1;
745}
746
747int
748__rpc_socktype2seman(int socktype)
749{
750	switch (socktype) {
751	case SOCK_DGRAM:
752		return NC_TPI_CLTS;
753	case SOCK_STREAM:
754		return NC_TPI_COTS_ORD;
755	case SOCK_RAW:
756		return NC_TPI_RAW;
757	default:
758		break;
759	}
760
761	return -1;
762}
763
764/*
765 * XXXX - IPv6 scope IDs can't be handled in universal addresses.
766 * Here, we compare the original server address to that of the RPC
767 * service we just received back from a call to rpcbind on the remote
768 * machine. If they are both "link local" or "site local", copy
769 * the scope id of the server address over to the service address.
770 */
771int
772__rpc_fixup_addr(struct netbuf *new, const struct netbuf *svc)
773{
774#ifdef INET6
775	struct sockaddr *sa_new, *sa_svc;
776	struct sockaddr_in6 *sin6_new, *sin6_svc;
777
778	sa_svc = (struct sockaddr *)svc->buf;
779	sa_new = (struct sockaddr *)new->buf;
780
781	if (sa_new->sa_family == sa_svc->sa_family &&
782	    sa_new->sa_family == AF_INET6) {
783		sin6_new = (struct sockaddr_in6 *)new->buf;
784		sin6_svc = (struct sockaddr_in6 *)svc->buf;
785
786		if ((IN6_IS_ADDR_LINKLOCAL(&sin6_new->sin6_addr) &&
787		     IN6_IS_ADDR_LINKLOCAL(&sin6_svc->sin6_addr)) ||
788		    (IN6_IS_ADDR_SITELOCAL(&sin6_new->sin6_addr) &&
789		     IN6_IS_ADDR_SITELOCAL(&sin6_svc->sin6_addr))) {
790			sin6_new->sin6_scope_id = sin6_svc->sin6_scope_id;
791		}
792	}
793#endif
794	return 1;
795}
796
797int
798__rpc_sockisbound(int fd)
799{
800	struct sockaddr_storage ss;
801	socklen_t slen;
802
803	slen = sizeof (struct sockaddr_storage);
804	if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0)
805		return 0;
806
807	switch (ss.ss_family) {
808		case AF_INET:
809			return (((struct sockaddr_in *)
810			    (void *)&ss)->sin_port != 0);
811#ifdef INET6
812		case AF_INET6:
813			return (((struct sockaddr_in6 *)
814			    (void *)&ss)->sin6_port != 0);
815#endif
816		case AF_LOCAL:
817			/* XXX check this */
818			return (((struct sockaddr_un *)
819			    (void *)&ss)->sun_path[0] != '\0');
820		default:
821			break;
822	}
823
824	return 0;
825}
826