1 /* $NetBSD: rpcb_clnt.c,v 1.25 2010/03/23 20:28:58 drochner Exp $ */
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.
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.
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.
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.
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.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 /* #ident "@(#)rpcb_clnt.c 1.27 94/04/24 SMI" */
37 #include <sys/cdefs.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
40 static char sccsid
[] = "@(#)rpcb_clnt.c 1.30 89/06/21 Copyr 1988 Sun Micro";
42 __RCSID("$NetBSD: rpcb_clnt.c,v 1.25 2010/03/23 20:28:58 drochner Exp $");
48 * interface to rpcbind rpc service.
50 * Copyright (C) 1988, Sun Microsystems, Inc.
53 #include "namespace.h"
54 #include "reentrant.h"
55 #include <sys/types.h>
56 #include <sys/socket.h>
58 #include <sys/utsname.h>
60 #include <rpc/rpcb_prot.h>
61 #include <rpc/nettype.h>
62 #include <netconfig.h>
64 #include <netinet/in.h> /* FOR IPPROTO_TCP/UDP definitions */
65 #include <rpc/pmap_prot.h>
76 #include "rpc_internal.h"
79 __weak_alias(rpcb_set
,_rpcb_set
)
80 __weak_alias(rpcb_unset
,_rpcb_unset
)
81 __weak_alias(rpcb_getmaps
,_rpcb_getmaps
)
82 __weak_alias(rpcb_taddr2uaddr
,_rpcb_taddr2uaddr
)
83 __weak_alias(rpcb_uaddr2taddr
,_rpcb_uaddr2taddr
)
86 static struct timeval tottimeout
= { 60, 0 };
87 static const struct timeval rmttimeout
= { 3, 0 };
89 static const char nullstring
[] = "\000";
93 struct address_cache
{
97 struct netbuf
*ac_taddr
;
98 struct address_cache
*ac_next
;
101 static struct address_cache
*front
;
102 static int cachesize
;
104 #define CLCR_GET_RPCB_TIMEOUT 1
105 #define CLCR_SET_RPCB_TIMEOUT 2
108 extern int __rpc_lowvers
;
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
*,
114 static CLIENT
*getclnthandle
__P((const char *, const struct netconfig
*,
116 static CLIENT
*local_rpcb
__P((void));
117 static struct netbuf
*got_entry
__P((rpcb_entry_list_ptr
,
118 const struct netconfig
*));
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.
127 __rpc_control(request
, info
)
132 _DIAGASSERT(info
!= NULL
);
135 case CLCR_GET_RPCB_TIMEOUT
:
136 *(struct timeval
*)info
= tottimeout
;
138 case CLCR_SET_RPCB_TIMEOUT
:
139 tottimeout
= *(struct timeval
*)info
;
141 case CLCR_SET_LOWVERS
:
142 __rpc_lowvers
= *(int *)info
;
144 case CLCR_GET_LOWVERS
:
145 *(int *)info
= __rpc_lowvers
;
154 * It might seem that a reader/writer lock would be more reasonable here.
155 * However because getclnthandle(), the only user of the cache functions,
156 * may do a delete_cache() operation if a check_cache() fails to return an
157 * address useful to clnt_tli_create(), we may as well use a mutex.
160 * As it turns out, if the cache lock is *not* a reader/writer lock, we will
161 * block all clnt_create's if we are trying to connect to a host that's down,
162 * since the lock will be held all during that time.
165 extern rwlock_t rpcbaddr_cache_lock
;
169 * The routines check_cache(), add_cache(), delete_cache() manage the
170 * cache of rpcbind addresses for (host, netid).
173 static struct address_cache
*
174 check_cache(host
, netid
)
175 const char *host
, *netid
;
177 struct address_cache
*cptr
;
179 _DIAGASSERT(host
!= NULL
);
180 _DIAGASSERT(netid
!= NULL
);
182 /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
184 for (cptr
= front
; cptr
!= NULL
; cptr
= cptr
->ac_next
) {
185 if (!strcmp(cptr
->ac_host
, host
) &&
186 !strcmp(cptr
->ac_netid
, netid
)) {
188 fprintf(stderr
, "Found cache entry for %s: %s\n",
201 struct address_cache
*cptr
, *prevptr
= NULL
;
203 _DIAGASSERT(addr
!= NULL
);
205 /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
206 for (cptr
= front
; cptr
!= NULL
; cptr
= cptr
->ac_next
) {
207 if (!memcmp(cptr
->ac_taddr
->buf
, addr
->buf
, addr
->len
)) {
209 free(cptr
->ac_netid
);
210 free(cptr
->ac_taddr
->buf
);
211 free(cptr
->ac_taddr
);
213 free(cptr
->ac_uaddr
);
215 prevptr
->ac_next
= cptr
->ac_next
;
217 front
= cptr
->ac_next
;
227 add_cache(host
, netid
, taddr
, uaddr
)
228 const char *host
, *netid
;
230 struct netbuf
*taddr
;
232 struct address_cache
*ad_cache
, *cptr
, *prevptr
;
234 _DIAGASSERT(host
!= NULL
);
235 _DIAGASSERT(netid
!= NULL
);
236 /* uaddr may be NULL */
237 /* taddr may be NULL ??? */
239 ad_cache
= malloc(sizeof(*ad_cache
));
243 ad_cache
->ac_host
= strdup(host
);
244 ad_cache
->ac_netid
= strdup(netid
);
245 ad_cache
->ac_uaddr
= uaddr
? strdup(uaddr
) : NULL
;
246 ad_cache
->ac_taddr
= malloc(sizeof(*ad_cache
->ac_taddr
));
247 if (!ad_cache
->ac_host
|| !ad_cache
->ac_netid
|| !ad_cache
->ac_taddr
||
248 (uaddr
&& !ad_cache
->ac_uaddr
)) {
251 ad_cache
->ac_taddr
->len
= ad_cache
->ac_taddr
->maxlen
= taddr
->len
;
252 ad_cache
->ac_taddr
->buf
= malloc(taddr
->len
);
253 if (ad_cache
->ac_taddr
->buf
== NULL
) {
255 if (ad_cache
->ac_host
)
256 free(ad_cache
->ac_host
);
257 if (ad_cache
->ac_netid
)
258 free(ad_cache
->ac_netid
);
259 if (ad_cache
->ac_uaddr
)
260 free(ad_cache
->ac_uaddr
);
261 if (ad_cache
->ac_taddr
)
262 free(ad_cache
->ac_taddr
);
266 memcpy(ad_cache
->ac_taddr
->buf
, taddr
->buf
, taddr
->len
);
268 fprintf(stderr
, "Added to cache: %s : %s\n", host
, netid
);
271 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
273 rwlock_wrlock(&rpcbaddr_cache_lock
);
274 if (cachesize
< CACHESIZE
) {
275 ad_cache
->ac_next
= front
;
279 /* Free the last entry */
282 while (cptr
->ac_next
) {
284 cptr
= cptr
->ac_next
;
288 fprintf(stderr
, "Deleted from cache: %s : %s\n",
289 cptr
->ac_host
, cptr
->ac_netid
);
292 free(cptr
->ac_netid
);
293 free(cptr
->ac_taddr
->buf
);
294 free(cptr
->ac_taddr
);
296 free(cptr
->ac_uaddr
);
299 prevptr
->ac_next
= NULL
;
300 ad_cache
->ac_next
= front
;
304 ad_cache
->ac_next
= NULL
;
308 rwlock_unlock(&rpcbaddr_cache_lock
);
312 * This routine will return a client handle that is connected to the
313 * rpcbind. Returns NULL on error and free's everything.
316 getclnthandle(host
, nconf
, targaddr
)
318 const struct netconfig
*nconf
;
322 struct netbuf
*addr
, taddr
;
323 struct netbuf addr_to_delete
;
324 struct __rpc_sockinfo si
;
325 struct addrinfo hints
, *res
, *tres
;
326 struct address_cache
*ad_cache
;
329 _DIAGASSERT(host
!= NULL
);
330 _DIAGASSERT(nconf
!= NULL
);
331 /* targaddr may be NULL */
333 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */
335 /* Get the address of the rpcbind. Check cache first */
337 addr_to_delete
.len
= 0;
338 addr_to_delete
.buf
= NULL
;
339 rwlock_rdlock(&rpcbaddr_cache_lock
);
340 ad_cache
= check_cache(host
, nconf
->nc_netid
);
341 if (ad_cache
!= NULL
) {
342 addr
= ad_cache
->ac_taddr
;
343 client
= clnt_tli_create(RPC_ANYFD
, nconf
, addr
,
344 (rpcprog_t
)RPCBPROG
, (rpcvers_t
)RPCBVERS4
, 0, 0);
345 if (client
!= NULL
) {
347 *targaddr
= ad_cache
->ac_uaddr
;
348 rwlock_unlock(&rpcbaddr_cache_lock
);
351 addr_to_delete
.len
= addr
->len
;
352 addr_to_delete
.buf
= malloc(addr
->len
);
353 if (addr_to_delete
.buf
== NULL
) {
354 addr_to_delete
.len
= 0;
356 memcpy(addr_to_delete
.buf
, addr
->buf
, addr
->len
);
359 rwlock_unlock(&rpcbaddr_cache_lock
);
360 if (addr_to_delete
.len
!= 0) {
362 * Assume this may be due to cache data being
365 rwlock_wrlock(&rpcbaddr_cache_lock
);
366 delete_cache(&addr_to_delete
);
367 rwlock_unlock(&rpcbaddr_cache_lock
);
368 free(addr_to_delete
.buf
);
370 if (!__rpc_nconf2sockinfo(nconf
, &si
)) {
371 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
375 memset(&hints
, 0, sizeof hints
);
376 hints
.ai_family
= si
.si_af
;
377 hints
.ai_socktype
= si
.si_socktype
;
378 hints
.ai_protocol
= si
.si_proto
;
381 printf("trying netid %s family %d proto %d socktype %d\n",
382 nconf
->nc_netid
, si
.si_af
, si
.si_proto
, si
.si_socktype
);
385 if (getaddrinfo(host
, "sunrpc", &hints
, &res
) != 0) {
386 rpc_createerr
.cf_stat
= RPC_UNKNOWNHOST
;
390 for (tres
= res
; tres
!= NULL
; tres
= tres
->ai_next
) {
391 taddr
.buf
= tres
->ai_addr
;
392 taddr
.len
= taddr
.maxlen
= tres
->ai_addrlen
;
398 ua
= taddr2uaddr(nconf
, &taddr
);
399 fprintf(stderr
, "Got it [%s]\n", ua
);
408 fprintf(stderr
, "\tnetbuf len = %d, maxlen = %d\n",
409 taddr
.len
, taddr
.maxlen
);
410 fprintf(stderr
, "\tAddress is ");
411 for (i
= 0; i
< taddr
.len
; i
++)
412 fprintf(stderr
, "%u.", ((char *)(taddr
.buf
))[i
]);
413 fprintf(stderr
, "\n");
416 client
= clnt_tli_create(RPC_ANYFD
, nconf
, &taddr
,
417 (rpcprog_t
)RPCBPROG
, (rpcvers_t
)RPCBVERS4
, 0, 0);
420 clnt_pcreateerror("rpcbind clnt interface");
425 tmpaddr
= targaddr
? taddr2uaddr(nconf
, &taddr
) : NULL
;
426 add_cache(host
, nconf
->nc_netid
, &taddr
, tmpaddr
);
437 #define IN4_LOCALHOST_STRING "127.0.0.1"
438 #define IN6_LOCALHOST_STRING "::1"
441 * This routine will return a client handle that is connected to the local
442 * rpcbind. Returns NULL on error and free's everything.
448 static struct netconfig
*loopnconf
;
449 static const char *hostname
;
451 extern mutex_t loopnconf_lock
;
456 struct sockaddr_un sun
;
459 * Try connecting to the local rpcbind through a local socket
460 * first. If this doesn't work, try all transports defined in
461 * the netconfig file.
463 memset(&sun
, 0, sizeof sun
);
464 sock
= socket(AF_LOCAL
, SOCK_STREAM
, 0);
467 sun
.sun_family
= AF_LOCAL
;
468 strcpy(sun
.sun_path
, _PATH_RPCBINDSOCK
);
469 nbuf
.len
= sun
.sun_len
= SUN_LEN(&sun
);
470 nbuf
.maxlen
= sizeof (struct sockaddr_un
);
473 tsize
= __rpc_get_t_size(AF_LOCAL
, 0, 0);
474 client
= clnt_vc_create(sock
, &nbuf
, (rpcprog_t
)RPCBPROG
,
475 (rpcvers_t
)RPCBVERS
, tsize
, tsize
);
477 if (client
!= NULL
) {
478 /* XXX - mark the socket to be closed in destructor */
479 (void) CLNT_CONTROL(client
, CLSET_FD_CLOSE
, NULL
);
483 /* XXX - nobody needs this socket anymore, free the descriptor */
488 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
489 mutex_lock(&loopnconf_lock
);
490 if (loopnconf
== NULL
) {
491 struct netconfig
*nconf
, *tmpnconf
= NULL
;
495 nc_handle
= setnetconfig();
496 if (nc_handle
== NULL
) {
497 /* fails to open netconfig file */
498 syslog (LOG_ERR
, "rpc: failed to open " NETCONFIG
);
499 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
500 mutex_unlock(&loopnconf_lock
);
503 while ((nconf
= getnetconfig(nc_handle
)) != NULL
) {
505 if ((strcmp(nconf
->nc_protofmly
, NC_INET6
) == 0 ||
509 strcmp(nconf
->nc_protofmly
, NC_INET
) == 0) &&
510 (nconf
->nc_semantics
== NC_TPI_COTS
||
511 nconf
->nc_semantics
== NC_TPI_COTS_ORD
)) {
512 fd
= __rpc_nconf2fd(nconf
);
514 * Can't create a socket, assume that
515 * this family isn't configured in the kernel.
521 if (!strcmp(nconf
->nc_protofmly
, NC_INET
))
522 hostname
= IN4_LOCALHOST_STRING
;
524 hostname
= IN6_LOCALHOST_STRING
;
527 if (tmpnconf
== NULL
) {
528 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
529 mutex_unlock(&loopnconf_lock
);
532 loopnconf
= getnetconfigent(tmpnconf
->nc_netid
);
533 /* loopnconf is never freed */
534 endnetconfig(nc_handle
);
536 mutex_unlock(&loopnconf_lock
);
537 client
= getclnthandle(hostname
, loopnconf
, NULL
);
542 * Set a mapping between program, version and address.
543 * Calls the rpcbind service to do the mapping.
546 rpcb_set(program
, version
, nconf
, address
)
549 const struct netconfig
*nconf
; /* Network structure of transport */
550 const struct netbuf
*address
; /* Services netconfig address */
557 /* parameter checking */
559 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
562 if (address
== NULL
) {
563 rpc_createerr
.cf_stat
= RPC_UNKNOWNADDR
;
566 client
= local_rpcb();
571 /* convert to universal */
572 parms
.r_addr
= taddr2uaddr(__UNCONST(nconf
), __UNCONST(address
));
574 CLNT_DESTROY(client
);
575 rpc_createerr
.cf_stat
= RPC_N2AXLATEFAILURE
;
576 return (FALSE
); /* no universal address */
578 parms
.r_prog
= program
;
579 parms
.r_vers
= version
;
580 parms
.r_netid
= nconf
->nc_netid
;
582 * Though uid is not being used directly, we still send it for
583 * completeness. For non-unix platforms, perhaps some other
584 * string or an empty string can be sent.
586 (void) snprintf(uidbuf
, sizeof uidbuf
, "%d", geteuid());
587 parms
.r_owner
= uidbuf
;
589 CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_SET
, (xdrproc_t
) xdr_rpcb
,
590 (char *)(void *)&parms
, (xdrproc_t
) xdr_bool
,
591 (char *)(void *)&rslt
, tottimeout
);
593 CLNT_DESTROY(client
);
599 * Remove the mapping between program, version and netbuf address.
600 * Calls the rpcbind service to do the un-mapping.
601 * If netbuf is NULL, unset for all the transports, otherwise unset
602 * only for the given transport.
605 rpcb_unset(program
, version
, nconf
)
608 const struct netconfig
*nconf
;
615 client
= local_rpcb();
620 parms
.r_prog
= program
;
621 parms
.r_vers
= version
;
623 parms
.r_netid
= nconf
->nc_netid
;
625 parms
.r_netid
= __UNCONST(&nullstring
[0]); /* unsets all */
627 parms
.r_addr
= __UNCONST(&nullstring
[0]);
628 (void) snprintf(uidbuf
, sizeof uidbuf
, "%d", geteuid());
629 parms
.r_owner
= uidbuf
;
631 CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_UNSET
, (xdrproc_t
) xdr_rpcb
,
632 (char *)(void *)&parms
, (xdrproc_t
) xdr_bool
,
633 (char *)(void *)&rslt
, tottimeout
);
635 CLNT_DESTROY(client
);
640 * From the merged list, find the appropriate entry
642 static struct netbuf
*
643 got_entry(relp
, nconf
)
644 rpcb_entry_list_ptr relp
;
645 const struct netconfig
*nconf
;
647 struct netbuf
*na
= NULL
;
648 rpcb_entry_list_ptr sp
;
651 _DIAGASSERT(nconf
!= NULL
);
653 for (sp
= relp
; sp
!= NULL
; sp
= sp
->rpcb_entry_next
) {
654 rmap
= &sp
->rpcb_entry_map
;
655 if ((strcmp(nconf
->nc_proto
, rmap
->r_nc_proto
) == 0) &&
656 (strcmp(nconf
->nc_protofmly
, rmap
->r_nc_protofmly
) == 0) &&
657 (nconf
->nc_semantics
== rmap
->r_nc_semantics
) &&
658 (rmap
->r_maddr
!= NULL
) && (rmap
->r_maddr
[0] != 0)) {
659 na
= uaddr2taddr(nconf
, rmap
->r_maddr
);
661 fprintf(stderr
, "\tRemote address is [%s].\n",
665 "\tCouldn't resolve remote address!\n");
674 * An internal function which optimizes rpcb_getaddr function. It also
675 * returns the client handle that it uses to contact the remote rpcbind.
677 * The algorithm used: If the transports is TCP or UDP, it first tries
678 * version 2 (portmap), 4 and then 3 (svr4). This order should be
679 * changed in the next OS release to 4, 2 and 3. We are assuming that by
680 * that time, version 4 would be available on many machines on the network.
681 * With this algorithm, we get performance as well as a plan for
682 * obsoleting version 2.
684 * For all other transports, the algorithm remains as 4 and then 3.
686 * XXX: Due to some problems with t_connect(), we do not reuse the same client
687 * handle for COTS cases and hence in these cases we do not return the
688 * client handle. This code will change if t_connect() ever
689 * starts working properly. Also look under clnt_vc.c.
692 __rpcb_findaddr(program
, version
, nconf
, host
, clpp
)
695 const struct netconfig
*nconf
;
699 CLIENT
*client
= NULL
;
701 enum clnt_stat clnt_st
;
704 struct netbuf
*address
= NULL
;
705 rpcvers_t start_vers
= RPCBVERS4
;
706 struct netbuf servaddr
;
708 /* nconf is handled below */
709 _DIAGASSERT(host
!= NULL
);
710 /* clpp may be NULL */
712 /* parameter checking */
714 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
721 /* Try version 2 for TCP or UDP */
722 if (strcmp(nconf
->nc_protofmly
, NC_INET
) == 0) {
724 struct netbuf remote
;
725 rpcvers_t pmapvers
= 2;
726 struct pmap pmapparms
;
729 * Try UDP only - there are some portmappers out
730 * there that use UDP only.
732 if (strcmp(nconf
->nc_proto
, NC_TCP
) == 0) {
733 struct netconfig
*newnconf
;
735 if ((newnconf
= getnetconfigent("udp")) == NULL
) {
736 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
739 client
= getclnthandle(host
, newnconf
, &parms
.r_addr
);
740 freenetconfigent(newnconf
);
742 client
= getclnthandle(host
, nconf
, &parms
.r_addr
);
744 if (client
== NULL
) {
748 /* Set the version */
749 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&pmapvers
);
750 pmapparms
.pm_prog
= program
;
751 pmapparms
.pm_vers
= version
;
752 pmapparms
.pm_prot
= strcmp(nconf
->nc_proto
, NC_TCP
) ?
753 IPPROTO_UDP
: IPPROTO_TCP
;
754 pmapparms
.pm_port
= 0; /* not needed */
755 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)PMAPPROC_GETPORT
,
756 (xdrproc_t
) xdr_pmap
, (caddr_t
)(void *)&pmapparms
,
757 (xdrproc_t
) xdr_u_short
, (caddr_t
)(void *)&port
,
759 if (clnt_st
!= RPC_SUCCESS
) {
760 if ((clnt_st
== RPC_PROGVERSMISMATCH
) ||
761 (clnt_st
== RPC_PROGUNAVAIL
))
762 goto try_rpcbind
; /* Try different versions */
763 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
764 clnt_geterr(client
, &rpc_createerr
.cf_error
);
766 } else if (port
== 0) {
768 rpc_createerr
.cf_stat
= RPC_PROGNOTREGISTERED
;
772 CLNT_CONTROL(client
, CLGET_SVC_ADDR
, (char *)(void *)&remote
);
773 if (((address
= malloc(sizeof(struct netbuf
))) == NULL
) ||
774 ((address
->buf
= malloc(remote
.len
)) == NULL
)) {
775 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
776 clnt_geterr(client
, &rpc_createerr
.cf_error
);
783 memcpy(address
->buf
, remote
.buf
, remote
.len
);
784 memcpy(&((char *)address
->buf
)[sizeof (short)],
785 (char *)(void *)&port
, sizeof (short));
786 address
->len
= address
->maxlen
= remote
.len
;
793 * Now we try version 4 and then 3.
794 * We also send the remote system the address we used to
795 * contact it in case it can help to connect back with us
797 parms
.r_prog
= program
;
798 parms
.r_vers
= version
;
799 parms
.r_owner
= __UNCONST(&nullstring
[0]); /* not needed; */
800 /* just for xdring */
801 parms
.r_netid
= nconf
->nc_netid
; /* not really needed */
804 * If a COTS transport is being used, try getting address via CLTS
805 * transport. This works only with version 4.
806 * NOTE: This is being done for all transports EXCEPT LOOPBACK
807 * because with loopback the cost to go to a COTS is same as
808 * the cost to go through CLTS, plus you get the advantage of
809 * finding out immediately if the local rpcbind process is dead.
812 if ((nconf
->nc_semantics
== NC_TPI_COTS_ORD
||
813 nconf
->nc_semantics
== NC_TPI_COTS
) &&
814 (strcmp(nconf
->nc_protofmly
, NC_LOOPBACK
) != 0))
816 if (client
!= NULL
) {
817 CLNT_DESTROY(client
);
820 if (nconf
->nc_semantics
== NC_TPI_CLTS
)
824 struct netconfig
*nconf_clts
;
825 rpcb_entry_list_ptr relp
= NULL
;
827 if (client
== NULL
) {
828 /* This did not go through the above PORTMAP/TCP code */
830 if ((handle
= __rpc_setconf("datagram_v")) != NULL
)
832 if ((handle
= __rpc_setconf("circuit_v")) != NULL
)
835 while ((nconf_clts
= __rpc_getconf(handle
))
837 if (strcmp(nconf_clts
->nc_protofmly
,
838 nconf
->nc_protofmly
) != 0) {
841 client
= getclnthandle(host
, nconf_clts
,
845 __rpc_endconf(handle
);
848 goto regular_rpcbind
; /* Go the regular way */
850 /* This is a UDP PORTMAP handle. Change to version 4 */
852 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
855 * We also send the remote system the address we used to
856 * contact it in case it can help it connect back with us
858 if (parms
.r_addr
== NULL
) {
860 parms
.r_addr
= __UNCONST(&nullstring
[0]);
862 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETADDRLIST
,
863 (xdrproc_t
) xdr_rpcb
, (char *)(void *)&parms
,
864 (xdrproc_t
) xdr_rpcb_entry_list_ptr
,
865 (char *)(void *)&relp
, tottimeout
);
866 if (clnt_st
== RPC_SUCCESS
) {
867 if ((address
= got_entry(relp
, nconf
)) != NULL
) {
868 xdr_free((xdrproc_t
) xdr_rpcb_entry_list_ptr
,
869 (char *)(void *)&relp
);
870 CLNT_CONTROL(client
, CLGET_SVC_ADDR
,
871 (char *)(void *)&servaddr
);
872 __rpc_fixup_addr(address
, &servaddr
);
875 /* Entry not found for this transport */
876 xdr_free((xdrproc_t
) xdr_rpcb_entry_list_ptr
,
877 (char *)(void *)&relp
);
879 * XXX: should have perhaps returned with error but
880 * since the remote machine might not always be able
881 * to send the address on all transports, we try the
882 * regular way with regular_rpcbind
884 goto regular_rpcbind
;
885 } else if ((clnt_st
== RPC_PROGVERSMISMATCH
) ||
886 (clnt_st
== RPC_PROGUNAVAIL
)) {
887 start_vers
= RPCBVERS
; /* Try version 3 now */
888 goto regular_rpcbind
; /* Try different versions */
890 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
891 clnt_geterr(client
, &rpc_createerr
.cf_error
);
898 /* Now the same transport is to be used to get the address */
900 if (client
&& ((nconf
->nc_semantics
== NC_TPI_COTS_ORD
) ||
901 (nconf
->nc_semantics
== NC_TPI_COTS
)))
903 if (client
&& nconf
->nc_semantics
== NC_TPI_CLTS
)
906 /* A CLTS type of client - destroy it */
907 CLNT_DESTROY(client
);
911 if (client
== NULL
) {
912 client
= getclnthandle(host
, nconf
, &parms
.r_addr
);
913 if (client
== NULL
) {
917 if (parms
.r_addr
== NULL
)
918 parms
.r_addr
= __UNCONST(&nullstring
[0]);
920 /* First try from start_vers and then version 3 (RPCBVERS) */
921 for (vers
= start_vers
; vers
>= RPCBVERS
; vers
--) {
922 /* Set the version */
923 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
924 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETADDR
,
925 (xdrproc_t
) xdr_rpcb
, (char *)(void *)&parms
,
926 (xdrproc_t
) xdr_wrapstring
, (char *)(void *) &ua
,
928 if (clnt_st
== RPC_SUCCESS
) {
929 if ((ua
== NULL
) || (ua
[0] == 0)) {
930 /* address unknown */
931 rpc_createerr
.cf_stat
= RPC_PROGNOTREGISTERED
;
934 address
= uaddr2taddr(nconf
, ua
);
936 fprintf(stderr
, "\tRemote address is [%s]\n", ua
);
939 "\tCouldn't resolve remote address!\n");
941 xdr_free((xdrproc_t
)xdr_wrapstring
,
942 (char *)(void *)&ua
);
945 /* We don't know about your universal address */
946 rpc_createerr
.cf_stat
= RPC_N2AXLATEFAILURE
;
949 CLNT_CONTROL(client
, CLGET_SVC_ADDR
,
950 (char *)(void *)&servaddr
);
951 __rpc_fixup_addr(address
, &servaddr
);
953 } else if (clnt_st
== RPC_PROGVERSMISMATCH
) {
954 struct rpc_err rpcerr
;
956 clnt_geterr(client
, &rpcerr
);
957 if (rpcerr
.re_vers
.low
> RPCBVERS4
)
958 goto error
; /* a new version, can't handle */
959 } else if (clnt_st
!= RPC_PROGUNAVAIL
) {
960 /* Cant handle this error */
961 rpc_createerr
.cf_stat
= clnt_st
;
962 clnt_geterr(client
, &rpc_createerr
.cf_error
);
969 CLNT_DESTROY(client
);
973 if (nconf
->nc_semantics
!= NC_TPI_CLTS
) {
974 /* This client is the connectionless one */
976 CLNT_DESTROY(client
);
983 CLNT_DESTROY(client
);
990 * Find the mapped address for program, version.
991 * Calls the rpcbind service remotely to do the lookup.
992 * Uses the transport specified in nconf.
993 * Returns FALSE (0) if no map exists, else returns 1.
995 * Assuming that the address is all properly allocated
998 rpcb_getaddr(program
, version
, nconf
, address
, host
)
1001 const struct netconfig
*nconf
;
1002 struct netbuf
*address
;
1007 _DIAGASSERT(address
!= NULL
);
1009 if ((na
= __rpcb_findaddr(program
, version
, nconf
,
1010 host
, NULL
)) == NULL
)
1013 if (na
->len
> address
->maxlen
) {
1014 /* Too long address */
1017 rpc_createerr
.cf_stat
= RPC_FAILED
;
1020 memcpy(address
->buf
, na
->buf
, (size_t)na
->len
);
1021 address
->len
= na
->len
;
1028 * Get a copy of the current maps.
1029 * Calls the rpcbind service remotely to get the maps.
1031 * It returns only a list of the services
1032 * It returns NULL on failure.
1035 rpcb_getmaps(nconf
, host
)
1036 const struct netconfig
*nconf
;
1039 rpcblist_ptr head
= NULL
;
1041 enum clnt_stat clnt_st
;
1044 client
= getclnthandle(host
, nconf
, NULL
);
1045 if (client
== NULL
) {
1048 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_DUMP
,
1049 (xdrproc_t
) xdr_void
, NULL
, (xdrproc_t
) xdr_rpcblist_ptr
,
1050 (char *)(void *)&head
, tottimeout
);
1051 if (clnt_st
== RPC_SUCCESS
)
1054 if ((clnt_st
!= RPC_PROGVERSMISMATCH
) &&
1055 (clnt_st
!= RPC_PROGUNAVAIL
)) {
1056 rpc_createerr
.cf_stat
= RPC_RPCBFAILURE
;
1057 clnt_geterr(client
, &rpc_createerr
.cf_error
);
1061 /* fall back to earlier version */
1062 CLNT_CONTROL(client
, CLGET_VERS
, (char *)(void *)&vers
);
1063 if (vers
== RPCBVERS4
) {
1065 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
1066 if (CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_DUMP
,
1067 (xdrproc_t
) xdr_void
, NULL
, (xdrproc_t
) xdr_rpcblist_ptr
,
1068 (char *)(void *)&head
, tottimeout
) == RPC_SUCCESS
)
1071 rpc_createerr
.cf_stat
= RPC_RPCBFAILURE
;
1072 clnt_geterr(client
, &rpc_createerr
.cf_error
);
1075 CLNT_DESTROY(client
);
1080 * rpcbinder remote-call-service interface.
1081 * This routine is used to call the rpcbind remote call service
1082 * which will look up a service program in the address maps, and then
1083 * remotely call that routine with the given parameters. This allows
1084 * programs to do a lookup and call in one step.
1087 rpcb_rmtcall(nconf
, host
, prog
, vers
, proc
, xdrargs
, argsp
,
1088 xdrres
, resp
, tout
, addr_ptr
)
1089 const struct netconfig
*nconf
; /* Netconfig structure */
1090 const char *host
; /* Remote host name */
1093 rpcproc_t proc
; /* Remote proc identifiers */
1094 xdrproc_t xdrargs
, xdrres
; /* XDR routines */
1095 const char *argsp
; /* Argument */
1096 caddr_t resp
; /* Result */
1097 struct timeval tout
; /* Timeout value for this call */
1098 const struct netbuf
*addr_ptr
; /* Preallocated netbuf address */
1101 enum clnt_stat stat
;
1102 struct r_rpcb_rmtcallargs a
;
1103 struct r_rpcb_rmtcallres r
;
1104 rpcvers_t rpcb_vers
;
1106 stat
= RPC_FAILED
; /* XXXGCC -Wuninitialized [dreamcast] */
1108 client
= getclnthandle(host
, nconf
, NULL
);
1109 if (client
== NULL
) {
1110 return (RPC_FAILED
);
1112 CLNT_CONTROL(client
, CLSET_RETRY_TIMEOUT
, __UNCONST(&rmttimeout
));
1116 a
.args
.args_val
= argsp
;
1117 a
.xdr_args
= xdrargs
;
1119 r
.results
.results_val
= resp
;
1122 for (rpcb_vers
= RPCBVERS4
; rpcb_vers
>= RPCBVERS
; rpcb_vers
--) {
1123 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&rpcb_vers
);
1124 stat
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_CALLIT
,
1125 (xdrproc_t
) xdr_rpcb_rmtcallargs
, (char *)(void *)&a
,
1126 (xdrproc_t
) xdr_rpcb_rmtcallres
, (char *)(void *)&r
, tout
);
1127 if ((stat
== RPC_SUCCESS
) && (addr_ptr
!= NULL
)) {
1129 na
= uaddr2taddr(__UNCONST(nconf
), r
.addr
);
1131 stat
= RPC_N2AXLATEFAILURE
;
1132 ((struct netbuf
*)__UNCONST(addr_ptr
))->len
= 0;
1135 if (na
->len
> addr_ptr
->maxlen
) {
1136 /* Too long address */
1137 stat
= RPC_FAILED
; /* XXX A better error no */
1140 ((struct netbuf
*)__UNCONST(addr_ptr
))->len
= 0;
1143 memcpy(addr_ptr
->buf
, na
->buf
, (size_t)na
->len
);
1144 ((struct netbuf
*)__UNCONST(addr_ptr
))->len
= na
->len
;
1148 } else if ((stat
!= RPC_PROGVERSMISMATCH
) &&
1149 (stat
!= RPC_PROGUNAVAIL
)) {
1154 CLNT_DESTROY(client
);
1156 xdr_free((xdrproc_t
) xdr_wrapstring
, (char *)(void *)&r
.addr
);
1161 * Gets the time on the remote host.
1162 * Returns 1 if succeeds else 0.
1165 rpcb_gettime(host
, timep
)
1169 CLIENT
*client
= NULL
;
1171 struct netconfig
*nconf
;
1176 if ((host
== NULL
) || (host
[0] == 0)) {
1181 if ((handle
= __rpc_setconf("netpath")) == NULL
) {
1182 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1185 rpc_createerr
.cf_stat
= RPC_SUCCESS
;
1186 while (client
== NULL
) {
1187 if ((nconf
= __rpc_getconf(handle
)) == NULL
) {
1188 if (rpc_createerr
.cf_stat
== RPC_SUCCESS
)
1189 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1192 client
= getclnthandle(host
, nconf
, NULL
);
1196 __rpc_endconf(handle
);
1197 if (client
== NULL
) {
1201 st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETTIME
,
1202 (xdrproc_t
) xdr_void
, NULL
,
1203 (xdrproc_t
) xdr_int
, (char *)(void *)timep
, tottimeout
);
1205 if ((st
== RPC_PROGVERSMISMATCH
) || (st
== RPC_PROGUNAVAIL
)) {
1206 CLNT_CONTROL(client
, CLGET_VERS
, (char *)(void *)&vers
);
1207 if (vers
== RPCBVERS4
) {
1208 /* fall back to earlier version */
1210 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
1211 st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETTIME
,
1212 (xdrproc_t
) xdr_void
, NULL
,
1213 (xdrproc_t
) xdr_int
, (char *)(void *)timep
,
1217 CLNT_DESTROY(client
);
1218 return (st
== RPC_SUCCESS
? TRUE
: FALSE
);
1222 * Converts taddr to universal address. This routine should never
1223 * really be called because local n2a libraries are always provided.
1226 rpcb_taddr2uaddr(nconf
, taddr
)
1227 struct netconfig
*nconf
;
1228 struct netbuf
*taddr
;
1234 /* parameter checking */
1235 if (nconf
== NULL
) {
1236 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1239 if (taddr
== NULL
) {
1240 rpc_createerr
.cf_stat
= RPC_UNKNOWNADDR
;
1243 client
= local_rpcb();
1248 CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_TADDR2UADDR
,
1249 (xdrproc_t
) xdr_netbuf
, (char *)(void *)taddr
,
1250 (xdrproc_t
) xdr_wrapstring
, (char *)(void *)&uaddr
, tottimeout
);
1251 CLNT_DESTROY(client
);
1256 * Converts universal address to netbuf. This routine should never
1257 * really be called because local n2a libraries are always provided.
1260 rpcb_uaddr2taddr(nconf
, uaddr
)
1261 struct netconfig
*nconf
;
1265 struct netbuf
*taddr
;
1268 /* parameter checking */
1269 if (nconf
== NULL
) {
1270 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1273 if (uaddr
== NULL
) {
1274 rpc_createerr
.cf_stat
= RPC_UNKNOWNADDR
;
1277 client
= local_rpcb();
1282 taddr
= (struct netbuf
*)calloc(1, sizeof (struct netbuf
));
1283 if (taddr
== NULL
) {
1284 CLNT_DESTROY(client
);
1287 if (CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_UADDR2TADDR
,
1288 (xdrproc_t
) xdr_wrapstring
, (char *)(void *)&uaddr
,
1289 (xdrproc_t
) xdr_netbuf
, (char *)(void *)taddr
,
1290 tottimeout
) != RPC_SUCCESS
) {
1294 CLNT_DESTROY(client
);