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