Home | History | Annotate | Line # | Download | only in rpc
rpcb_clnt.c revision 1.3.2.2
      1 /*	$NetBSD: rpcb_clnt.c,v 1.3.2.2 2001/11/25 20:33:13 he 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 /* #ident	"@(#)rpcb_clnt.c	1.27	94/04/24 SMI" */
     36 
     37 
     38 #if 0
     39 #if !defined(lint) && defined(SCCSIDS)
     40 static char sccsid[] = "@(#)rpcb_clnt.c 1.30 89/06/21 Copyr 1988 Sun Micro";
     41 #endif
     42 #endif
     43 
     44 /*
     45  * rpcb_clnt.c
     46  * interface to rpcbind rpc service.
     47  *
     48  * Copyright (C) 1988, Sun Microsystems, Inc.
     49  */
     50 
     51 #include "namespace.h"
     52 #include "reentrant.h"
     53 #include <sys/types.h>
     54 #include <sys/socket.h>
     55 #include <sys/un.h>
     56 #include <sys/utsname.h>
     57 #include <rpc/rpc.h>
     58 #include <rpc/rpcb_prot.h>
     59 #include <netconfig.h>
     60 #ifdef PORTMAP
     61 #include <netinet/in.h>		/* FOR IPPROTO_TCP/UDP definitions */
     62 #include <rpc/pmap_prot.h>
     63 #endif
     64 #include <stdio.h>
     65 #include <errno.h>
     66 #include <stdlib.h>
     67 #include <string.h>
     68 #include <unistd.h>
     69 #include <netdb.h>
     70 #include <syslog.h>
     71 
     72 #include "rpc_com.h"
     73 
     74 #ifdef __weak_alias
     75 __weak_alias(rpcb_set,_rpcb_set)
     76 __weak_alias(rpcb_unset,_rpcb_unset)
     77 __weak_alias(rpcb_getmaps,_rpcb_getmaps)
     78 __weak_alias(rpcb_rmtcall,_rpcb_rmtcall)
     79 __weak_alias(rpcb_gettime,_rpcb_gettime)
     80 __weak_alias(rpcb_taddr2uaddr,_rpcb_taddr2uaddr)
     81 __weak_alias(rpcb_uaddr2taddr,_rpcb_uaddr2taddr)
     82 #endif
     83 
     84 static struct timeval tottimeout = { 60, 0 };
     85 static const struct timeval rmttimeout = { 3, 0 };
     86 
     87 extern bool_t xdr_wrapstring __P((XDR *, char **));
     88 
     89 static const char nullstring[] = "\000";
     90 
     91 #define	CACHESIZE 6
     92 
     93 struct address_cache {
     94 	char *ac_host;
     95 	char *ac_netid;
     96 	char *ac_uaddr;
     97 	struct netbuf *ac_taddr;
     98 	struct address_cache *ac_next;
     99 };
    100 
    101 static struct address_cache *front;
    102 static int cachesize;
    103 
    104 #define	CLCR_GET_RPCB_TIMEOUT	1
    105 #define	CLCR_SET_RPCB_TIMEOUT	2
    106 
    107 
    108 extern int __rpc_lowvers;
    109 
    110 static struct address_cache *check_cache __P((const char *, const char *));
    111 static void delete_cache __P((struct netbuf *));
    112 static void add_cache __P((const char *, const char *, struct netbuf *,
    113 			   char *));
    114 static CLIENT *getclnthandle __P((const char *, const struct netconfig *,
    115 				  char **));
    116 static CLIENT *local_rpcb __P((void));
    117 static struct netbuf *got_entry __P((rpcb_entry_list_ptr,
    118 				     const struct netconfig *));
    119 
    120 /*
    121  * This routine adjusts the timeout used for calls to the remote rpcbind.
    122  * Also, this routine can be used to set the use of portmapper version 2
    123  * only when doing rpc_broadcasts
    124  * These are private routines that may not be provided in future releases.
    125  */
    126 bool_t
    127 __rpc_control(request, info)
    128 	int	request;
    129 	void	*info;
    130 {
    131 	switch (request) {
    132 	case CLCR_GET_RPCB_TIMEOUT:
    133 		*(struct timeval *)info = tottimeout;
    134 		break;
    135 	case CLCR_SET_RPCB_TIMEOUT:
    136 		tottimeout = *(struct timeval *)info;
    137 		break;
    138 	case CLCR_SET_LOWVERS:
    139 		__rpc_lowvers = *(int *)info;
    140 		break;
    141 	case CLCR_GET_LOWVERS:
    142 		*(int *)info = __rpc_lowvers;
    143 		break;
    144 	default:
    145 		return (FALSE);
    146 	}
    147 	return (TRUE);
    148 }
    149 
    150 /*
    151  *	It might seem that a reader/writer lock would be more reasonable here.
    152  *	However because getclnthandle(), the only user of the cache functions,
    153  *	may do a delete_cache() operation if a check_cache() fails to return an
    154  *	address useful to clnt_tli_create(), we may as well use a mutex.
    155  */
    156 /*
    157  * As it turns out, if the cache lock is *not* a reader/writer lock, we will
    158  * block all clnt_create's if we are trying to connect to a host that's down,
    159  * since the lock will be held all during that time.
    160  */
    161 #ifdef __REENT
    162 extern rwlock_t	rpcbaddr_cache_lock;
    163 #endif
    164 
    165 /*
    166  * The routines check_cache(), add_cache(), delete_cache() manage the
    167  * cache of rpcbind addresses for (host, netid).
    168  */
    169 
    170 static struct address_cache *
    171 check_cache(host, netid)
    172 	const char *host, *netid;
    173 {
    174 	struct address_cache *cptr;
    175 
    176 	/* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
    177 
    178 	for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
    179 		if (!strcmp(cptr->ac_host, host) &&
    180 		    !strcmp(cptr->ac_netid, netid)) {
    181 #ifdef ND_DEBUG
    182 			fprintf(stderr, "Found cache entry for %s: %s\n",
    183 				host, netid);
    184 #endif
    185 			return (cptr);
    186 		}
    187 	}
    188 	return ((struct address_cache *) NULL);
    189 }
    190 
    191 static void
    192 delete_cache(addr)
    193 	struct netbuf *addr;
    194 {
    195 	struct address_cache *cptr, *prevptr = NULL;
    196 
    197 	/* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
    198 	for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
    199 		if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
    200 			free(cptr->ac_host);
    201 			free(cptr->ac_netid);
    202 			free(cptr->ac_taddr->buf);
    203 			free(cptr->ac_taddr);
    204 			if (cptr->ac_uaddr)
    205 				free(cptr->ac_uaddr);
    206 			if (prevptr)
    207 				prevptr->ac_next = cptr->ac_next;
    208 			else
    209 				front = cptr->ac_next;
    210 			free(cptr);
    211 			cachesize--;
    212 			break;
    213 		}
    214 		prevptr = cptr;
    215 	}
    216 }
    217 
    218 static void
    219 add_cache(host, netid, taddr, uaddr)
    220 	const char *host, *netid;
    221 	char *uaddr;
    222 	struct netbuf *taddr;
    223 {
    224 	struct address_cache  *ad_cache, *cptr, *prevptr;
    225 
    226 	ad_cache = (struct address_cache *)
    227 			malloc(sizeof (struct address_cache));
    228 	if (!ad_cache) {
    229 		return;
    230 	}
    231 	ad_cache->ac_host = strdup(host);
    232 	ad_cache->ac_netid = strdup(netid);
    233 	ad_cache->ac_uaddr = uaddr ? strdup(uaddr) : NULL;
    234 	ad_cache->ac_taddr = (struct netbuf *)malloc(sizeof (struct netbuf));
    235 	if (!ad_cache->ac_host || !ad_cache->ac_netid || !ad_cache->ac_taddr ||
    236 		(uaddr && !ad_cache->ac_uaddr)) {
    237 		return;
    238 	}
    239 	ad_cache->ac_taddr->len = ad_cache->ac_taddr->maxlen = taddr->len;
    240 	ad_cache->ac_taddr->buf = (char *) malloc(taddr->len);
    241 	if (ad_cache->ac_taddr->buf == NULL) {
    242 		return;
    243 	}
    244 	memcpy(ad_cache->ac_taddr->buf, taddr->buf, taddr->len);
    245 #ifdef ND_DEBUG
    246 	fprintf(stderr, "Added to cache: %s : %s\n", host, netid);
    247 #endif
    248 
    249 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock:  cptr */
    250 
    251 	rwlock_wrlock(&rpcbaddr_cache_lock);
    252 	if (cachesize < CACHESIZE) {
    253 		ad_cache->ac_next = front;
    254 		front = ad_cache;
    255 		cachesize++;
    256 	} else {
    257 		/* Free the last entry */
    258 		cptr = front;
    259 		prevptr = NULL;
    260 		while (cptr->ac_next) {
    261 			prevptr = cptr;
    262 			cptr = cptr->ac_next;
    263 		}
    264 
    265 #ifdef ND_DEBUG
    266 		fprintf(stderr, "Deleted from cache: %s : %s\n",
    267 			cptr->ac_host, cptr->ac_netid);
    268 #endif
    269 		free(cptr->ac_host);
    270 		free(cptr->ac_netid);
    271 		free(cptr->ac_taddr->buf);
    272 		free(cptr->ac_taddr);
    273 		if (cptr->ac_uaddr)
    274 			free(cptr->ac_uaddr);
    275 
    276 		if (prevptr) {
    277 			prevptr->ac_next = NULL;
    278 			ad_cache->ac_next = front;
    279 			front = ad_cache;
    280 		} else {
    281 			front = ad_cache;
    282 			ad_cache->ac_next = NULL;
    283 		}
    284 		free(cptr);
    285 	}
    286 	rwlock_unlock(&rpcbaddr_cache_lock);
    287 }
    288 
    289 /*
    290  * This routine will return a client handle that is connected to the
    291  * rpcbind. Returns NULL on error and free's everything.
    292  */
    293 static CLIENT *
    294 getclnthandle(host, nconf, targaddr)
    295 	const char *host;
    296 	const struct netconfig *nconf;
    297 	char **targaddr;
    298 {
    299 	register CLIENT *client;
    300 	struct netbuf *addr, taddr;
    301 	struct netbuf addr_to_delete;
    302 	struct __rpc_sockinfo si;
    303 	struct addrinfo hints, *res, *tres;
    304 	struct address_cache *ad_cache;
    305 	char *tmpaddr;
    306 
    307 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock:  ad_cache */
    308 
    309 	/* Get the address of the rpcbind.  Check cache first */
    310 	addr_to_delete.len = 0;
    311 	rwlock_rdlock(&rpcbaddr_cache_lock);
    312 	ad_cache = check_cache(host, nconf->nc_netid);
    313 	if (ad_cache != NULL) {
    314 		addr = ad_cache->ac_taddr;
    315 		client = clnt_tli_create(RPC_ANYFD, nconf, addr, RPCBPROG,
    316 					RPCBVERS4, 0, 0);
    317 		if (client != NULL) {
    318 			if (targaddr)
    319 				*targaddr = ad_cache->ac_uaddr;
    320 			rwlock_unlock(&rpcbaddr_cache_lock);
    321 			return (client);
    322 		}
    323 		addr_to_delete.len = addr->len;
    324 		addr_to_delete.buf = (char *)malloc(addr->len);
    325 		if (addr_to_delete.buf == NULL) {
    326 			addr_to_delete.len = 0;
    327 		} else {
    328 			memcpy(addr_to_delete.buf, addr->buf, addr->len);
    329 		}
    330 	}
    331 	rwlock_unlock(&rpcbaddr_cache_lock);
    332 	if (addr_to_delete.len != 0) {
    333 		/*
    334 		 * Assume this may be due to cache data being
    335 		 *  outdated
    336 		 */
    337 		rwlock_wrlock(&rpcbaddr_cache_lock);
    338 		delete_cache(&addr_to_delete);
    339 		rwlock_unlock(&rpcbaddr_cache_lock);
    340 		free(addr_to_delete.buf);
    341 	}
    342 	if (!__rpc_nconf2sockinfo(nconf, &si)) {
    343 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
    344 		return NULL;
    345 	}
    346 
    347 	memset(&hints, 0, sizeof hints);
    348 	hints.ai_family = si.si_af;
    349 	hints.ai_socktype = si.si_socktype;
    350 	hints.ai_protocol = si.si_proto;
    351 
    352 #ifdef CLNT_DEBUG
    353 	printf("trying netid %s family %d proto %d socktype %d\n",
    354 	    nconf->nc_netid, si.si_af, si.si_proto, si.si_socktype);
    355 #endif
    356 
    357 	if (getaddrinfo(host, "sunrpc", &hints, &res) != 0) {
    358 		rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
    359 		return NULL;
    360 	}
    361 
    362 	for (tres = res; tres != NULL; tres = tres->ai_next) {
    363 		taddr.buf = tres->ai_addr;
    364 		taddr.len = taddr.maxlen = tres->ai_addrlen;
    365 
    366 #ifdef ND_DEBUG
    367 		{
    368 			char *ua;
    369 
    370 			ua = taddr2uaddr(nconf, &taddr);
    371 			fprintf(stderr, "Got it [%s]\n", ua);
    372 			free(ua);
    373 		}
    374 #endif
    375 
    376 #ifdef ND_DEBUG
    377 		{
    378 			int i;
    379 
    380 			fprintf(stderr, "\tnetbuf len = %d, maxlen = %d\n",
    381 				taddr.len, taddr.maxlen);
    382 			fprintf(stderr, "\tAddress is ");
    383 			for (i = 0; i < taddr.len; i++)
    384 				fprintf(stderr, "%u.", ((char *)(taddr.buf))[i]);
    385 			fprintf(stderr, "\n");
    386 		}
    387 #endif
    388 		client = clnt_tli_create(RPC_ANYFD, nconf, &taddr, RPCBPROG,
    389 					RPCBVERS4, 0, 0);
    390 #ifdef ND_DEBUG
    391 		if (! client) {
    392 			clnt_pcreateerror("rpcbind clnt interface");
    393 		}
    394 #endif
    395 
    396 		if (client) {
    397 			tmpaddr = targaddr ? taddr2uaddr(nconf, &taddr) : NULL;
    398 			add_cache(host, nconf->nc_netid, &taddr, tmpaddr);
    399 			if (targaddr)
    400 				*targaddr = tmpaddr;
    401 			break;
    402 		}
    403 	}
    404 	freeaddrinfo(res);
    405 	return (client);
    406 }
    407 
    408 /* XXX */
    409 #define IN4_LOCALHOST_STRING	"127.0.0.1"
    410 #define IN6_LOCALHOST_STRING	"::1"
    411 
    412 /*
    413  * This routine will return a client handle that is connected to the local
    414  * rpcbind. Returns NULL on error and free's everything.
    415  */
    416 static CLIENT *
    417 local_rpcb()
    418 {
    419 	CLIENT *client;
    420 	static struct netconfig *loopnconf;
    421 	static char *hostname;
    422 #ifdef __REENT
    423 	extern mutex_t loopnconf_lock;
    424 #endif
    425 	int sock, tsize;
    426 	struct netbuf nbuf;
    427 	struct sockaddr_un sun;
    428 
    429 	/*
    430 	 * Try connecting to the local rpcbind through a local socket
    431 	 * first. If this doesn't work, try all transports defined in
    432 	 * the netconfig file.
    433 	 */
    434 	memset(&sun, 0, sizeof sun);
    435 	sock = socket(AF_LOCAL, SOCK_STREAM, 0);
    436 	if (sock < 0)
    437 		goto try_nconf;
    438 	sun.sun_family = AF_LOCAL;
    439 	strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
    440 	nbuf.len = sun.sun_len = SUN_LEN(&sun);
    441 	nbuf.maxlen = sizeof (struct sockaddr_un);
    442 	nbuf.buf = &sun;
    443 
    444 	tsize = __rpc_get_t_size(AF_LOCAL, 0, 0);
    445 	client = clnt_vc_create(sock, &nbuf, RPCBPROG, RPCBVERS, tsize, tsize);
    446 
    447 	if (client != NULL) {
    448 		/* XXX - mark the socket to be closed in destructor */
    449 		(void) CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL);
    450 		return client;
    451 	}
    452 
    453 	/* XXX - nobody needs this socket anymore, free the descriptor */
    454 	close(sock);
    455 
    456 try_nconf:
    457 
    458 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
    459 	mutex_lock(&loopnconf_lock);
    460 	if (loopnconf == NULL) {
    461 		struct netconfig *nconf, *tmpnconf = NULL;
    462 		void *nc_handle;
    463 		int fd;
    464 
    465 		nc_handle = setnetconfig();
    466 		if (nc_handle == NULL) {
    467 			/* fails to open netconfig file */
    468 			syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
    469 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
    470 			mutex_unlock(&loopnconf_lock);
    471 			return (NULL);
    472 		}
    473 		while ((nconf = getnetconfig(nc_handle))) {
    474 #ifdef INET6
    475 			if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0 ||
    476 #else
    477 			if (
    478 #endif
    479 			     strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
    480 			    (nconf->nc_semantics == NC_TPI_COTS ||
    481 			     nconf->nc_semantics == NC_TPI_COTS_ORD)) {
    482 				fd = __rpc_nconf2fd(nconf);
    483 				/*
    484 				 * Can't create a socket, assume that
    485 				 * this family isn't configured in the kernel.
    486 				 */
    487 				if (fd < 0)
    488 					continue;
    489 				close(fd);
    490 				tmpnconf = nconf;
    491 				if (!strcmp(nconf->nc_protofmly, NC_INET))
    492 					hostname = IN4_LOCALHOST_STRING;
    493 				else
    494 					hostname = IN6_LOCALHOST_STRING;
    495 			}
    496 		}
    497 		if (tmpnconf == NULL) {
    498 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
    499 			mutex_unlock(&loopnconf_lock);
    500 			return (NULL);
    501 		}
    502 		loopnconf = getnetconfigent(tmpnconf->nc_netid);
    503 		/* loopnconf is never freed */
    504 		endnetconfig(nc_handle);
    505 	}
    506 	mutex_unlock(&loopnconf_lock);
    507 	client = getclnthandle(hostname, loopnconf, (char **)NULL);
    508 	return (client);
    509 }
    510 
    511 /*
    512  * Set a mapping between program, version and address.
    513  * Calls the rpcbind service to do the mapping.
    514  */
    515 bool_t
    516 rpcb_set(program, version, nconf, address)
    517 	rpcprog_t program;
    518 	rpcvers_t version;
    519 	const struct netconfig *nconf;	/* Network structure of transport */
    520 	const struct netbuf *address;		/* Services netconfig address */
    521 {
    522 	register CLIENT *client;
    523 	bool_t rslt = FALSE;
    524 	RPCB parms;
    525 	char uidbuf[32];
    526 
    527 	/* parameter checking */
    528 	if (nconf == (struct netconfig *)NULL) {
    529 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
    530 		return (FALSE);
    531 	}
    532 	if (address == NULL) {
    533 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
    534 		return (FALSE);
    535 	}
    536 	client = local_rpcb();
    537 	if (! client) {
    538 		return (FALSE);
    539 	}
    540 
    541 	/* convert to universal */
    542 	parms.r_addr = taddr2uaddr((struct netconfig *) nconf,
    543 				   (struct netbuf *)address);
    544 	if (!parms.r_addr) {
    545 		CLNT_DESTROY(client);
    546 		rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
    547 		return (FALSE); /* no universal address */
    548 	}
    549 	parms.r_prog = program;
    550 	parms.r_vers = version;
    551 	parms.r_netid = nconf->nc_netid;
    552 	/*
    553 	 * Though uid is not being used directly, we still send it for
    554 	 * completeness.  For non-unix platforms, perhaps some other
    555 	 * string or an empty string can be sent.
    556 	 */
    557 	(void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
    558 	parms.r_owner = uidbuf;
    559 
    560 	CLNT_CALL(client, RPCBPROC_SET, (xdrproc_t) xdr_rpcb, (char *)&parms,
    561 			(xdrproc_t) xdr_bool, (char *)&rslt, tottimeout);
    562 
    563 	CLNT_DESTROY(client);
    564 	free(parms.r_addr);
    565 	return (rslt);
    566 }
    567 
    568 /*
    569  * Remove the mapping between program, version and netbuf address.
    570  * Calls the rpcbind service to do the un-mapping.
    571  * If netbuf is NULL, unset for all the transports, otherwise unset
    572  * only for the given transport.
    573  */
    574 bool_t
    575 rpcb_unset(program, version, nconf)
    576 	rpcprog_t program;
    577 	rpcvers_t version;
    578 	const struct netconfig *nconf;
    579 {
    580 	register CLIENT *client;
    581 	bool_t rslt = FALSE;
    582 	RPCB parms;
    583 	char uidbuf[32];
    584 
    585 	client = local_rpcb();
    586 	if (! client) {
    587 		return (FALSE);
    588 	}
    589 
    590 	parms.r_prog = program;
    591 	parms.r_vers = version;
    592 	if (nconf)
    593 		parms.r_netid = nconf->nc_netid;
    594 	else
    595 		parms.r_netid = (char *) &nullstring[0]; /* unsets  all */
    596 	parms.r_addr = (char *) &nullstring[0];
    597 	(void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
    598 	parms.r_owner = uidbuf;
    599 
    600 	CLNT_CALL(client, RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb, (char *)&parms,
    601 			(xdrproc_t) xdr_bool, (char *)&rslt, tottimeout);
    602 
    603 	CLNT_DESTROY(client);
    604 	return (rslt);
    605 }
    606 
    607 /*
    608  * From the merged list, find the appropriate entry
    609  */
    610 static struct netbuf *
    611 got_entry(relp, nconf)
    612 	rpcb_entry_list_ptr relp;
    613 	const struct netconfig *nconf;
    614 {
    615 	struct netbuf *na = NULL;
    616 	rpcb_entry_list_ptr sp;
    617 	rpcb_entry *rmap;
    618 
    619 	for (sp = relp; sp != NULL; sp = sp->rpcb_entry_next) {
    620 		rmap = &sp->rpcb_entry_map;
    621 		if ((strcmp(nconf->nc_proto, rmap->r_nc_proto) == 0) &&
    622 		    (strcmp(nconf->nc_protofmly, rmap->r_nc_protofmly) == 0) &&
    623 		    (nconf->nc_semantics == rmap->r_nc_semantics) &&
    624 		    (rmap->r_maddr != NULL) && (rmap->r_maddr[0] != NULL)) {
    625 			na = uaddr2taddr(nconf, rmap->r_maddr);
    626 #ifdef ND_DEBUG
    627 			fprintf(stderr, "\tRemote address is [%s].\n",
    628 				rmap->r_maddr);
    629 			if (!na)
    630 				fprintf(stderr,
    631 				    "\tCouldn't resolve remote address!\n");
    632 #endif
    633 			break;
    634 		}
    635 	}
    636 	return (na);
    637 }
    638 
    639 /*
    640  * An internal function which optimizes rpcb_getaddr function.  It also
    641  * returns the client handle that it uses to contact the remote rpcbind.
    642  *
    643  * The algorithm used: If the transports is TCP or UDP, it first tries
    644  * version 2 (portmap), 4 and then 3 (svr4).  This order should be
    645  * changed in the next OS release to 4, 2 and 3.  We are assuming that by
    646  * that time, version 4 would be available on many machines on the network.
    647  * With this algorithm, we get performance as well as a plan for
    648  * obsoleting version 2.
    649  *
    650  * For all other transports, the algorithm remains as 4 and then 3.
    651  *
    652  * XXX: Due to some problems with t_connect(), we do not reuse the same client
    653  * handle for COTS cases and hence in these cases we do not return the
    654  * client handle.  This code will change if t_connect() ever
    655  * starts working properly.  Also look under clnt_vc.c.
    656  */
    657 struct netbuf *
    658 __rpcb_findaddr(program, version, nconf, host, clpp)
    659 	rpcprog_t program;
    660 	rpcvers_t version;
    661 	const struct netconfig *nconf;
    662 	const char *host;
    663 	CLIENT **clpp;
    664 {
    665 	register CLIENT *client = NULL;
    666 	RPCB parms;
    667 	enum clnt_stat clnt_st;
    668 	char *ua = NULL;
    669 	rpcvers_t vers;
    670 	struct netbuf *address = NULL;
    671 	rpcvers_t start_vers = RPCBVERS4;
    672 	struct netbuf servaddr;
    673 
    674 	/* parameter checking */
    675 	if (nconf == (struct netconfig *)NULL) {
    676 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
    677 		return (NULL);
    678 	}
    679 
    680 	parms.r_addr = NULL;
    681 
    682 #ifdef PORTMAP
    683 	/* Try version 2 for TCP or UDP */
    684 	if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
    685 		u_short port = 0;
    686 		struct netbuf remote;
    687 		rpcvers_t pmapvers = 2;
    688 		struct pmap pmapparms;
    689 
    690 		/*
    691 		 * Try UDP only - there are some portmappers out
    692 		 * there that use UDP only.
    693 		 */
    694 		if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
    695 			struct netconfig *newnconf;
    696 
    697 			if ((newnconf = getnetconfigent("udp")) == NULL) {
    698 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
    699 				return (NULL);
    700 			}
    701 			client = getclnthandle(host, newnconf, &parms.r_addr);
    702 			freenetconfigent(newnconf);
    703 		} else {
    704 			client = getclnthandle(host, nconf, &parms.r_addr);
    705 		}
    706 		if (client == (CLIENT *)NULL) {
    707 			return (NULL);
    708 		}
    709 
    710 		/* Set the version */
    711 		CLNT_CONTROL(client, CLSET_VERS, (char *)&pmapvers);
    712 		pmapparms.pm_prog = program;
    713 		pmapparms.pm_vers = version;
    714 		pmapparms.pm_prot = strcmp(nconf->nc_proto, NC_TCP) ?
    715 					IPPROTO_UDP : IPPROTO_TCP;
    716 		pmapparms.pm_port = 0;	/* not needed */
    717 		clnt_st = CLNT_CALL(client, PMAPPROC_GETPORT,
    718 			(xdrproc_t) xdr_pmap, (caddr_t) &pmapparms,
    719 			(xdrproc_t) xdr_u_short, (caddr_t) &port,
    720 			tottimeout);
    721 		if (clnt_st != RPC_SUCCESS) {
    722 			if ((clnt_st == RPC_PROGVERSMISMATCH) ||
    723 				(clnt_st == RPC_PROGUNAVAIL))
    724 				goto try_rpcbind; /* Try different versions */
    725 			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
    726 			clnt_geterr(client, &rpc_createerr.cf_error);
    727 			goto error;
    728 		} else if (port == 0) {
    729 			address = NULL;
    730 			rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
    731 			goto error;
    732 		}
    733 		port = htons(port);
    734 		CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&remote);
    735 		if (((address = (struct netbuf *)
    736 			malloc(sizeof (struct netbuf))) == NULL) ||
    737 		    ((address->buf = (char *)
    738 			malloc(remote.len)) == NULL)) {
    739 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
    740 			clnt_geterr(client, &rpc_createerr.cf_error);
    741 			if (address) {
    742 				free(address);
    743 				address = NULL;
    744 			}
    745 			goto error;
    746 		}
    747 		memcpy(address->buf, remote.buf, remote.len);
    748 		memcpy(&((char *)address->buf)[sizeof (short)],
    749 				(char *)&port, sizeof (short));
    750 		address->len = address->maxlen = remote.len;
    751 		goto done;
    752 	}
    753 #endif
    754 
    755 try_rpcbind:
    756 	/*
    757 	 * Now we try version 4 and then 3.
    758 	 * We also send the remote system the address we used to
    759 	 * contact it in case it can help to connect back with us
    760 	 */
    761 	parms.r_prog = program;
    762 	parms.r_vers = version;
    763 	parms.r_owner = (char *) &nullstring[0];	/* not needed; */
    764 							/* just for xdring */
    765 	parms.r_netid = nconf->nc_netid; /* not really needed */
    766 
    767 	/*
    768 	 * If a COTS transport is being used, try getting address via CLTS
    769 	 * transport.  This works only with version 4.
    770 	 * NOTE: This is being done for all transports EXCEPT LOOPBACK
    771 	 * because with loopback the cost to go to a COTS is same as
    772 	 * the cost to go through CLTS, plus you get the advantage of
    773 	 * finding out immediately if the local rpcbind process is dead.
    774 	 */
    775 #if 1
    776 	if ((nconf->nc_semantics == NC_TPI_COTS_ORD ||
    777 			nconf->nc_semantics == NC_TPI_COTS) &&
    778 	    (strcmp(nconf->nc_protofmly, NC_LOOPBACK) != 0)) {
    779 #else
    780 	if (client != NULL) {
    781 		CLNT_DESTROY(client);
    782 		client = NULL;
    783 	}
    784 	if (nconf->nc_semantics == NC_TPI_CLTS) {
    785 #endif
    786 		void *handle;
    787 		struct netconfig *nconf_clts;
    788 		rpcb_entry_list_ptr relp = NULL;
    789 
    790 		if (client == NULL) {
    791 			/* This did not go through the above PORTMAP/TCP code */
    792 #if 1
    793 			if ((handle = __rpc_setconf("datagram_v")) != NULL) {
    794 #else
    795 			if ((handle = __rpc_setconf("circuit_v")) != NULL) {
    796 #endif
    797 				while ((nconf_clts = __rpc_getconf(handle))
    798 					!= NULL) {
    799 					if (strcmp(nconf_clts->nc_protofmly,
    800 						nconf->nc_protofmly) != 0) {
    801 						continue;
    802 					}
    803 					client = getclnthandle(host, nconf_clts,
    804 							&parms.r_addr);
    805 					break;
    806 				}
    807 				__rpc_endconf(handle);
    808 			}
    809 			if (client == (CLIENT *)NULL)
    810 				goto regular_rpcbind;	/* Go the regular way */
    811 		} else {
    812 			/* This is a UDP PORTMAP handle.  Change to version 4 */
    813 			vers = RPCBVERS4;
    814 			CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
    815 		}
    816 		/*
    817 		 * We also send the remote system the address we used to
    818 		 * contact it in case it can help it connect back with us
    819 		 */
    820 		if (parms.r_addr == NULL)
    821 			parms.r_addr = (char *) &nullstring[0]; /* for XDRing */
    822 		clnt_st = CLNT_CALL(client, RPCBPROC_GETADDRLIST,
    823 				    (xdrproc_t) xdr_rpcb, (char *) &parms,
    824 				    (xdrproc_t) xdr_rpcb_entry_list_ptr,
    825 				    (char *) &relp, tottimeout);
    826 		if (clnt_st == RPC_SUCCESS) {
    827 			if ((address = got_entry(relp, nconf))) {
    828 				xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
    829 					(char *)&relp);
    830 				goto done;
    831 			}
    832 			/* Entry not found for this transport */
    833 			xdr_free((xdrproc_t) xdr_rpcb_entry_list_ptr,
    834 				(char *)&relp);
    835 			/*
    836 			 * XXX: should have perhaps returned with error but
    837 			 * since the remote machine might not always be able
    838 			 * to send the address on all transports, we try the
    839 			 * regular way with regular_rpcbind
    840 			 */
    841 			goto regular_rpcbind;
    842 		} else if ((clnt_st == RPC_PROGVERSMISMATCH) ||
    843 			(clnt_st == RPC_PROGUNAVAIL)) {
    844 			start_vers = RPCBVERS;	/* Try version 3 now */
    845 			goto regular_rpcbind; /* Try different versions */
    846 		} else {
    847 			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
    848 			clnt_geterr(client, &rpc_createerr.cf_error);
    849 			goto error;
    850 		}
    851 	}
    852 
    853 regular_rpcbind:
    854 
    855 	/* Now the same transport is to be used to get the address */
    856 #if 1
    857 	if (client && ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
    858 			(nconf->nc_semantics == NC_TPI_COTS))) {
    859 #else
    860 	if (client && nconf->nc_semantics == NC_TPI_CLTS) {
    861 #endif
    862 		/* A CLTS type of client - destroy it */
    863 		CLNT_DESTROY(client);
    864 		client = NULL;
    865 	}
    866 
    867 	if (client == NULL) {
    868 		client = getclnthandle(host, nconf, &parms.r_addr);
    869 		if (client == NULL) {
    870 			goto error;
    871 		}
    872 	}
    873 	if (parms.r_addr == NULL)
    874 		parms.r_addr = (char *) &nullstring[0];
    875 
    876 	/* First try from start_vers and then version 3 (RPCBVERS) */
    877 	for (vers = start_vers;  vers >= RPCBVERS; vers--) {
    878 		/* Set the version */
    879 		CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
    880 		clnt_st = CLNT_CALL(client, RPCBPROC_GETADDR,
    881 				    (xdrproc_t) xdr_rpcb, (char *) &parms,
    882 				    (xdrproc_t) xdr_wrapstring,
    883 				    (char *) &ua, tottimeout);
    884 		if (clnt_st == RPC_SUCCESS) {
    885 			if ((ua == NULL) || (ua[0] == NULL)) {
    886 				/* address unknown */
    887 				rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
    888 				goto error;
    889 			}
    890 			address = uaddr2taddr(nconf, ua);
    891 #ifdef ND_DEBUG
    892 			fprintf(stderr, "\tRemote address is [%s]\n", ua);
    893 			if (!address)
    894 				fprintf(stderr,
    895 					"\tCouldn't resolve remote address!\n");
    896 #endif
    897 			xdr_free((xdrproc_t)xdr_wrapstring, (char *)&ua);
    898 
    899 			if (! address) {
    900 				/* We don't know about your universal address */
    901 				rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
    902 				goto error;
    903 			}
    904 			CLNT_CONTROL(client, CLGET_SVC_ADDR, (char *)&servaddr);
    905 			__rpc_fixup_addr(address, &servaddr);
    906 			goto done;
    907 		} else if (clnt_st == RPC_PROGVERSMISMATCH) {
    908 			struct rpc_err rpcerr;
    909 
    910 			clnt_geterr(client, &rpcerr);
    911 			if (rpcerr.re_vers.low > RPCBVERS4)
    912 				goto error;  /* a new version, can't handle */
    913 		} else if (clnt_st != RPC_PROGUNAVAIL) {
    914 			/* Cant handle this error */
    915 			rpc_createerr.cf_stat = clnt_st;
    916 			clnt_geterr(client, &rpc_createerr.cf_error);
    917 			goto error;
    918 		}
    919 	}
    920 
    921 	if ((address == NULL) || (address->len == 0)) {
    922 		rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
    923 		clnt_geterr(client, &rpc_createerr.cf_error);
    924 	}
    925 
    926 error:
    927 	if (client) {
    928 		CLNT_DESTROY(client);
    929 		client = NULL;
    930 	}
    931 done:
    932 	if (nconf->nc_semantics != NC_TPI_CLTS) {
    933 		/* This client is the connectionless one */
    934 		if (client) {
    935 			CLNT_DESTROY(client);
    936 			client = NULL;
    937 		}
    938 	}
    939 	if (clpp) {
    940 		*clpp = client;
    941 	} else if (client) {
    942 		CLNT_DESTROY(client);
    943 	}
    944 	return (address);
    945 }
    946 
    947 
    948 /*
    949  * Find the mapped address for program, version.
    950  * Calls the rpcbind service remotely to do the lookup.
    951  * Uses the transport specified in nconf.
    952  * Returns FALSE (0) if no map exists, else returns 1.
    953  *
    954  * Assuming that the address is all properly allocated
    955  */
    956 int
    957 rpcb_getaddr(program, version, nconf, address, host)
    958 	rpcprog_t program;
    959 	rpcvers_t version;
    960 	const struct netconfig *nconf;
    961 	struct netbuf *address;
    962 	const char *host;
    963 {
    964 	struct netbuf *na;
    965 
    966 	if ((na = __rpcb_findaddr(program, version, nconf,
    967 				host, (CLIENT **) NULL)) == NULL)
    968 		return (FALSE);
    969 
    970 	if (na->len > address->maxlen) {
    971 		/* Too long address */
    972 		free(na->buf);
    973 		free(na);
    974 		rpc_createerr.cf_stat = RPC_FAILED;
    975 		return (FALSE);
    976 	}
    977 	memcpy(address->buf, na->buf, (int)na->len);
    978 	address->len = na->len;
    979 	free(na->buf);
    980 	free(na);
    981 	return (TRUE);
    982 }
    983 
    984 /*
    985  * Get a copy of the current maps.
    986  * Calls the rpcbind service remotely to get the maps.
    987  *
    988  * It returns only a list of the services
    989  * It returns NULL on failure.
    990  */
    991 rpcblist *
    992 rpcb_getmaps(nconf, host)
    993 	const struct netconfig *nconf;
    994 	const char *host;
    995 {
    996 	rpcblist_ptr head = (rpcblist_ptr)NULL;
    997 	register CLIENT *client;
    998 	enum clnt_stat clnt_st;
    999 	rpcvers_t vers = 0;
   1000 
   1001 	client = getclnthandle(host, nconf, (char **)NULL);
   1002 	if (client == (CLIENT *)NULL) {
   1003 		return (head);
   1004 	}
   1005 	clnt_st = CLNT_CALL(client, RPCBPROC_DUMP,
   1006 			(xdrproc_t) xdr_void, NULL,
   1007 			(xdrproc_t) xdr_rpcblist_ptr,
   1008 			(char *)&head, tottimeout);
   1009 	if (clnt_st == RPC_SUCCESS)
   1010 		goto done;
   1011 
   1012 	if ((clnt_st != RPC_PROGVERSMISMATCH) &&
   1013 		    (clnt_st != RPC_PROGUNAVAIL)) {
   1014 		rpc_createerr.cf_stat = RPC_RPCBFAILURE;
   1015 		clnt_geterr(client, &rpc_createerr.cf_error);
   1016 		goto done;
   1017 	}
   1018 
   1019 	/* fall back to earlier version */
   1020 	CLNT_CONTROL(client, CLGET_VERS, (char *)&vers);
   1021 	if (vers == RPCBVERS4) {
   1022 		vers = RPCBVERS;
   1023 		CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
   1024 		if (CLNT_CALL(client, RPCBPROC_DUMP,
   1025 			(xdrproc_t) xdr_void,
   1026 			(char *) NULL, (xdrproc_t) xdr_rpcblist_ptr,
   1027 			(char *)&head, tottimeout) == RPC_SUCCESS)
   1028 				goto done;
   1029 	}
   1030 	rpc_createerr.cf_stat = RPC_RPCBFAILURE;
   1031 	clnt_geterr(client, &rpc_createerr.cf_error);
   1032 
   1033 done:
   1034 	CLNT_DESTROY(client);
   1035 	return (head);
   1036 }
   1037 
   1038 /*
   1039  * rpcbinder remote-call-service interface.
   1040  * This routine is used to call the rpcbind remote call service
   1041  * which will look up a service program in the address maps, and then
   1042  * remotely call that routine with the given parameters. This allows
   1043  * programs to do a lookup and call in one step.
   1044 */
   1045 enum clnt_stat
   1046 rpcb_rmtcall(nconf, host, prog, vers, proc, xdrargs, argsp,
   1047 		xdrres, resp, tout, addr_ptr)
   1048 	const struct netconfig *nconf;	/* Netconfig structure */
   1049 	const char *host;			/* Remote host name */
   1050 	rpcprog_t prog;
   1051 	rpcvers_t vers;
   1052 	rpcproc_t proc;			/* Remote proc identifiers */
   1053 	xdrproc_t xdrargs, xdrres;	/* XDR routines */
   1054 	caddr_t argsp, resp;		/* Argument and Result */
   1055 	struct timeval tout;		/* Timeout value for this call */
   1056 	const struct netbuf *addr_ptr;	/* Preallocated netbuf address */
   1057 {
   1058 	register CLIENT *client;
   1059 	enum clnt_stat stat;
   1060 	struct r_rpcb_rmtcallargs a;
   1061 	struct r_rpcb_rmtcallres r;
   1062 	rpcvers_t rpcb_vers;
   1063 
   1064 
   1065 	client = getclnthandle(host, nconf, (char **)NULL);
   1066 	if (client == (CLIENT *)NULL) {
   1067 		return (RPC_FAILED);
   1068 	}
   1069 	CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)&rmttimeout);
   1070 	a.prog = prog;
   1071 	a.vers = vers;
   1072 	a.proc = proc;
   1073 	a.args.args_val = argsp;
   1074 	a.xdr_args = xdrargs;
   1075 	r.addr = NULL;
   1076 	r.results.results_val = resp;
   1077 	r.xdr_res = xdrres;
   1078 
   1079 	for (rpcb_vers = RPCBVERS4; rpcb_vers >= RPCBVERS; rpcb_vers--) {
   1080 		CLNT_CONTROL(client, CLSET_VERS, (char *)&rpcb_vers);
   1081 		stat = CLNT_CALL(client, RPCBPROC_CALLIT,
   1082 			(xdrproc_t) xdr_rpcb_rmtcallargs, (char *)&a,
   1083 			(xdrproc_t) xdr_rpcb_rmtcallres, (char *)&r, tout);
   1084 		if ((stat == RPC_SUCCESS) && (addr_ptr != NULL)) {
   1085 			struct netbuf *na;
   1086 
   1087 			na = uaddr2taddr((struct netconfig *) nconf, r.addr);
   1088 			if (! na) {
   1089 				stat = RPC_N2AXLATEFAILURE;
   1090 				((struct netbuf *) addr_ptr)->len = 0;
   1091 				goto error;
   1092 			}
   1093 			if (na->len > addr_ptr->maxlen) {
   1094 				/* Too long address */
   1095 				stat = RPC_FAILED; /* XXX A better error no */
   1096 				free(na->buf);
   1097 				free(na);
   1098 				((struct netbuf *) addr_ptr)->len = 0;
   1099 				goto error;
   1100 			}
   1101 			memcpy(addr_ptr->buf, na->buf, (int)na->len);
   1102 			((struct netbuf *)addr_ptr)->len = na->len;
   1103 			free(na->buf);
   1104 			free(na);
   1105 			break;
   1106 		} else if ((stat != RPC_PROGVERSMISMATCH) &&
   1107 			    (stat != RPC_PROGUNAVAIL)) {
   1108 			goto error;
   1109 		}
   1110 	}
   1111 error:
   1112 	CLNT_DESTROY(client);
   1113 	if (r.addr)
   1114 		xdr_free((xdrproc_t) xdr_wrapstring, (char *)&r.addr);
   1115 	return (stat);
   1116 }
   1117 
   1118 /*
   1119  * Gets the time on the remote host.
   1120  * Returns 1 if succeeds else 0.
   1121  */
   1122 bool_t
   1123 rpcb_gettime(host, timep)
   1124 	const char *host;
   1125 	time_t *timep;
   1126 {
   1127 	CLIENT *client = NULL;
   1128 	void *handle;
   1129 	struct netconfig *nconf;
   1130 	rpcvers_t vers;
   1131 	enum clnt_stat st;
   1132 
   1133 
   1134 	if ((host == NULL) || (host[0] == NULL)) {
   1135 		time(timep);
   1136 		return (TRUE);
   1137 	}
   1138 
   1139 	if ((handle = __rpc_setconf("netpath")) == NULL) {
   1140 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
   1141 		return (FALSE);
   1142 	}
   1143 	rpc_createerr.cf_stat = RPC_SUCCESS;
   1144 	while (client == (CLIENT *)NULL) {
   1145 		if ((nconf = __rpc_getconf(handle)) == NULL) {
   1146 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
   1147 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
   1148 			break;
   1149 		}
   1150 		client = getclnthandle(host, nconf, (char **)NULL);
   1151 		if (client)
   1152 			break;
   1153 	}
   1154 	__rpc_endconf(handle);
   1155 	if (client == (CLIENT *) NULL) {
   1156 		return (FALSE);
   1157 	}
   1158 
   1159 	st = CLNT_CALL(client, RPCBPROC_GETTIME,
   1160 		(xdrproc_t) xdr_void, (char *)NULL,
   1161 		(xdrproc_t) xdr_int, (char *)timep, tottimeout);
   1162 
   1163 	if ((st == RPC_PROGVERSMISMATCH) || (st == RPC_PROGUNAVAIL)) {
   1164 		CLNT_CONTROL(client, CLGET_VERS, (char *)&vers);
   1165 		if (vers == RPCBVERS4) {
   1166 			/* fall back to earlier version */
   1167 			vers = RPCBVERS;
   1168 			CLNT_CONTROL(client, CLSET_VERS, (char *)&vers);
   1169 			st = CLNT_CALL(client, RPCBPROC_GETTIME,
   1170 				(xdrproc_t) xdr_void, (char *)NULL,
   1171 				(xdrproc_t) xdr_int, (char *) timep,
   1172 				tottimeout);
   1173 		}
   1174 	}
   1175 	CLNT_DESTROY(client);
   1176 	return (st == RPC_SUCCESS? TRUE: FALSE);
   1177 }
   1178 
   1179 /*
   1180  * Converts taddr to universal address.  This routine should never
   1181  * really be called because local n2a libraries are always provided.
   1182  */
   1183 char *
   1184 rpcb_taddr2uaddr(nconf, taddr)
   1185 	struct netconfig *nconf;
   1186 	struct netbuf *taddr;
   1187 {
   1188 	CLIENT *client;
   1189 	char *uaddr = NULL;
   1190 
   1191 
   1192 	/* parameter checking */
   1193 	if (nconf == (struct netconfig *)NULL) {
   1194 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
   1195 		return (NULL);
   1196 	}
   1197 	if (taddr == NULL) {
   1198 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
   1199 		return (NULL);
   1200 	}
   1201 	client = local_rpcb();
   1202 	if (! client) {
   1203 		return (NULL);
   1204 	}
   1205 
   1206 	CLNT_CALL(client, RPCBPROC_TADDR2UADDR, (xdrproc_t) xdr_netbuf,
   1207 		(char *)taddr, (xdrproc_t) xdr_wrapstring, (char *)&uaddr,
   1208 		tottimeout);
   1209 	CLNT_DESTROY(client);
   1210 	return (uaddr);
   1211 }
   1212 
   1213 /*
   1214  * Converts universal address to netbuf.  This routine should never
   1215  * really be called because local n2a libraries are always provided.
   1216  */
   1217 struct netbuf *
   1218 rpcb_uaddr2taddr(nconf, uaddr)
   1219 	struct netconfig *nconf;
   1220 	char *uaddr;
   1221 {
   1222 	CLIENT *client;
   1223 	struct netbuf *taddr;
   1224 
   1225 
   1226 	/* parameter checking */
   1227 	if (nconf == (struct netconfig *)NULL) {
   1228 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
   1229 		return (NULL);
   1230 	}
   1231 	if (uaddr == NULL) {
   1232 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
   1233 		return (NULL);
   1234 	}
   1235 	client = local_rpcb();
   1236 	if (! client) {
   1237 		return (NULL);
   1238 	}
   1239 
   1240 	taddr = (struct netbuf *)calloc(1, sizeof (struct netbuf));
   1241 	if (taddr == NULL) {
   1242 		CLNT_DESTROY(client);
   1243 		return (NULL);
   1244 	}
   1245 	if (CLNT_CALL(client, RPCBPROC_UADDR2TADDR, (xdrproc_t) xdr_wrapstring,
   1246 		(char *) &uaddr, (xdrproc_t) xdr_netbuf, (char *)taddr,
   1247 		tottimeout) != RPC_SUCCESS) {
   1248 		free(taddr);
   1249 		taddr = NULL;
   1250 	}
   1251 	CLNT_DESTROY(client);
   1252 	return (taddr);
   1253 }
   1254