1 /* $NetBSD: rpcb_clnt.c,v 1.23 2008/04/25 23:51:41 dogcow 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.23 2008/04/25 23:51:41 dogcow 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_rmtcall
,_rpcb_rmtcall
)
83 __weak_alias(rpcb_gettime
,_rpcb_gettime
)
84 __weak_alias(rpcb_taddr2uaddr
,_rpcb_taddr2uaddr
)
85 __weak_alias(rpcb_uaddr2taddr
,_rpcb_uaddr2taddr
)
88 static struct timeval tottimeout
= { 60, 0 };
89 static const struct timeval rmttimeout
= { 3, 0 };
91 static const char nullstring
[] = "\000";
95 struct address_cache
{
99 struct netbuf
*ac_taddr
;
100 struct address_cache
*ac_next
;
103 static struct address_cache
*front
;
104 static int cachesize
;
106 #define CLCR_GET_RPCB_TIMEOUT 1
107 #define CLCR_SET_RPCB_TIMEOUT 2
110 extern int __rpc_lowvers
;
112 static struct address_cache
*check_cache
__P((const char *, const char *));
113 static void delete_cache
__P((struct netbuf
*));
114 static void add_cache
__P((const char *, const char *, struct netbuf
*,
116 static CLIENT
*getclnthandle
__P((const char *, const struct netconfig
*,
118 static CLIENT
*local_rpcb
__P((void));
119 static struct netbuf
*got_entry
__P((rpcb_entry_list_ptr
,
120 const struct netconfig
*));
123 * This routine adjusts the timeout used for calls to the remote rpcbind.
124 * Also, this routine can be used to set the use of portmapper version 2
125 * only when doing rpc_broadcasts
126 * These are private routines that may not be provided in future releases.
129 __rpc_control(request
, info
)
134 _DIAGASSERT(info
!= NULL
);
137 case CLCR_GET_RPCB_TIMEOUT
:
138 *(struct timeval
*)info
= tottimeout
;
140 case CLCR_SET_RPCB_TIMEOUT
:
141 tottimeout
= *(struct timeval
*)info
;
143 case CLCR_SET_LOWVERS
:
144 __rpc_lowvers
= *(int *)info
;
146 case CLCR_GET_LOWVERS
:
147 *(int *)info
= __rpc_lowvers
;
156 * It might seem that a reader/writer lock would be more reasonable here.
157 * However because getclnthandle(), the only user of the cache functions,
158 * may do a delete_cache() operation if a check_cache() fails to return an
159 * address useful to clnt_tli_create(), we may as well use a mutex.
162 * As it turns out, if the cache lock is *not* a reader/writer lock, we will
163 * block all clnt_create's if we are trying to connect to a host that's down,
164 * since the lock will be held all during that time.
167 extern rwlock_t rpcbaddr_cache_lock
;
171 * The routines check_cache(), add_cache(), delete_cache() manage the
172 * cache of rpcbind addresses for (host, netid).
175 static struct address_cache
*
176 check_cache(host
, netid
)
177 const char *host
, *netid
;
179 struct address_cache
*cptr
;
181 _DIAGASSERT(host
!= NULL
);
182 _DIAGASSERT(netid
!= NULL
);
184 /* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
186 for (cptr
= front
; cptr
!= NULL
; cptr
= cptr
->ac_next
) {
187 if (!strcmp(cptr
->ac_host
, host
) &&
188 !strcmp(cptr
->ac_netid
, netid
)) {
190 fprintf(stderr
, "Found cache entry for %s: %s\n",
203 struct address_cache
*cptr
, *prevptr
= NULL
;
205 _DIAGASSERT(addr
!= NULL
);
207 /* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
208 for (cptr
= front
; cptr
!= NULL
; cptr
= cptr
->ac_next
) {
209 if (!memcmp(cptr
->ac_taddr
->buf
, addr
->buf
, addr
->len
)) {
211 free(cptr
->ac_netid
);
212 free(cptr
->ac_taddr
->buf
);
213 free(cptr
->ac_taddr
);
215 free(cptr
->ac_uaddr
);
217 prevptr
->ac_next
= cptr
->ac_next
;
219 front
= cptr
->ac_next
;
229 add_cache(host
, netid
, taddr
, uaddr
)
230 const char *host
, *netid
;
232 struct netbuf
*taddr
;
234 struct address_cache
*ad_cache
, *cptr
, *prevptr
;
236 _DIAGASSERT(host
!= NULL
);
237 _DIAGASSERT(netid
!= NULL
);
238 /* uaddr may be NULL */
239 /* taddr may be NULL ??? */
241 ad_cache
= malloc(sizeof(*ad_cache
));
245 ad_cache
->ac_host
= strdup(host
);
246 ad_cache
->ac_netid
= strdup(netid
);
247 ad_cache
->ac_uaddr
= uaddr
? strdup(uaddr
) : NULL
;
248 ad_cache
->ac_taddr
= malloc(sizeof(*ad_cache
->ac_taddr
));
249 if (!ad_cache
->ac_host
|| !ad_cache
->ac_netid
|| !ad_cache
->ac_taddr
||
250 (uaddr
&& !ad_cache
->ac_uaddr
)) {
253 ad_cache
->ac_taddr
->len
= ad_cache
->ac_taddr
->maxlen
= taddr
->len
;
254 ad_cache
->ac_taddr
->buf
= malloc(taddr
->len
);
255 if (ad_cache
->ac_taddr
->buf
== NULL
) {
257 if (ad_cache
->ac_host
)
258 free(ad_cache
->ac_host
);
259 if (ad_cache
->ac_netid
)
260 free(ad_cache
->ac_netid
);
261 if (ad_cache
->ac_uaddr
)
262 free(ad_cache
->ac_uaddr
);
263 if (ad_cache
->ac_taddr
)
264 free(ad_cache
->ac_taddr
);
268 memcpy(ad_cache
->ac_taddr
->buf
, taddr
->buf
, taddr
->len
);
270 fprintf(stderr
, "Added to cache: %s : %s\n", host
, netid
);
273 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: cptr */
275 rwlock_wrlock(&rpcbaddr_cache_lock
);
276 if (cachesize
< CACHESIZE
) {
277 ad_cache
->ac_next
= front
;
281 /* Free the last entry */
284 while (cptr
->ac_next
) {
286 cptr
= cptr
->ac_next
;
290 fprintf(stderr
, "Deleted from cache: %s : %s\n",
291 cptr
->ac_host
, cptr
->ac_netid
);
294 free(cptr
->ac_netid
);
295 free(cptr
->ac_taddr
->buf
);
296 free(cptr
->ac_taddr
);
298 free(cptr
->ac_uaddr
);
301 prevptr
->ac_next
= NULL
;
302 ad_cache
->ac_next
= front
;
306 ad_cache
->ac_next
= NULL
;
310 rwlock_unlock(&rpcbaddr_cache_lock
);
314 * This routine will return a client handle that is connected to the
315 * rpcbind. Returns NULL on error and free's everything.
318 getclnthandle(host
, nconf
, targaddr
)
320 const struct netconfig
*nconf
;
324 struct netbuf
*addr
, taddr
;
325 struct netbuf addr_to_delete
;
326 struct __rpc_sockinfo si
;
327 struct addrinfo hints
, *res
, *tres
;
328 struct address_cache
*ad_cache
;
331 _DIAGASSERT(host
!= NULL
);
332 _DIAGASSERT(nconf
!= NULL
);
333 /* targaddr may be NULL */
335 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock: ad_cache */
337 /* Get the address of the rpcbind. Check cache first */
339 addr_to_delete
.len
= 0;
340 addr_to_delete
.buf
= NULL
;
341 rwlock_rdlock(&rpcbaddr_cache_lock
);
342 ad_cache
= check_cache(host
, nconf
->nc_netid
);
343 if (ad_cache
!= NULL
) {
344 addr
= ad_cache
->ac_taddr
;
345 client
= clnt_tli_create(RPC_ANYFD
, nconf
, addr
,
346 (rpcprog_t
)RPCBPROG
, (rpcvers_t
)RPCBVERS4
, 0, 0);
347 if (client
!= NULL
) {
349 *targaddr
= ad_cache
->ac_uaddr
;
350 rwlock_unlock(&rpcbaddr_cache_lock
);
353 addr_to_delete
.len
= addr
->len
;
354 addr_to_delete
.buf
= malloc(addr
->len
);
355 if (addr_to_delete
.buf
== NULL
) {
356 addr_to_delete
.len
= 0;
358 memcpy(addr_to_delete
.buf
, addr
->buf
, addr
->len
);
361 rwlock_unlock(&rpcbaddr_cache_lock
);
362 if (addr_to_delete
.len
!= 0) {
364 * Assume this may be due to cache data being
367 rwlock_wrlock(&rpcbaddr_cache_lock
);
368 delete_cache(&addr_to_delete
);
369 rwlock_unlock(&rpcbaddr_cache_lock
);
370 free(addr_to_delete
.buf
);
372 if (!__rpc_nconf2sockinfo(nconf
, &si
)) {
373 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
377 memset(&hints
, 0, sizeof hints
);
378 hints
.ai_family
= si
.si_af
;
379 hints
.ai_socktype
= si
.si_socktype
;
380 hints
.ai_protocol
= si
.si_proto
;
383 printf("trying netid %s family %d proto %d socktype %d\n",
384 nconf
->nc_netid
, si
.si_af
, si
.si_proto
, si
.si_socktype
);
387 if (getaddrinfo(host
, "sunrpc", &hints
, &res
) != 0) {
388 rpc_createerr
.cf_stat
= RPC_UNKNOWNHOST
;
392 for (tres
= res
; tres
!= NULL
; tres
= tres
->ai_next
) {
393 taddr
.buf
= tres
->ai_addr
;
394 taddr
.len
= taddr
.maxlen
= tres
->ai_addrlen
;
400 ua
= taddr2uaddr(nconf
, &taddr
);
401 fprintf(stderr
, "Got it [%s]\n", ua
);
410 fprintf(stderr
, "\tnetbuf len = %d, maxlen = %d\n",
411 taddr
.len
, taddr
.maxlen
);
412 fprintf(stderr
, "\tAddress is ");
413 for (i
= 0; i
< taddr
.len
; i
++)
414 fprintf(stderr
, "%u.", ((char *)(taddr
.buf
))[i
]);
415 fprintf(stderr
, "\n");
418 client
= clnt_tli_create(RPC_ANYFD
, nconf
, &taddr
,
419 (rpcprog_t
)RPCBPROG
, (rpcvers_t
)RPCBVERS4
, 0, 0);
422 clnt_pcreateerror("rpcbind clnt interface");
427 tmpaddr
= targaddr
? taddr2uaddr(nconf
, &taddr
) : NULL
;
428 add_cache(host
, nconf
->nc_netid
, &taddr
, tmpaddr
);
439 #define IN4_LOCALHOST_STRING "127.0.0.1"
440 #define IN6_LOCALHOST_STRING "::1"
443 * This routine will return a client handle that is connected to the local
444 * rpcbind. Returns NULL on error and free's everything.
450 static struct netconfig
*loopnconf
;
451 static const char *hostname
;
453 extern mutex_t loopnconf_lock
;
458 struct sockaddr_un sun
;
461 * Try connecting to the local rpcbind through a local socket
462 * first. If this doesn't work, try all transports defined in
463 * the netconfig file.
465 memset(&sun
, 0, sizeof sun
);
466 sock
= socket(AF_LOCAL
, SOCK_STREAM
, 0);
469 sun
.sun_family
= AF_LOCAL
;
470 strcpy(sun
.sun_path
, _PATH_RPCBINDSOCK
);
471 nbuf
.len
= sun
.sun_len
= SUN_LEN(&sun
);
472 nbuf
.maxlen
= sizeof (struct sockaddr_un
);
475 tsize
= __rpc_get_t_size(AF_LOCAL
, 0, 0);
476 client
= clnt_vc_create(sock
, &nbuf
, (rpcprog_t
)RPCBPROG
,
477 (rpcvers_t
)RPCBVERS
, tsize
, tsize
);
479 if (client
!= NULL
) {
480 /* XXX - mark the socket to be closed in destructor */
481 (void) CLNT_CONTROL(client
, CLSET_FD_CLOSE
, NULL
);
485 /* XXX - nobody needs this socket anymore, free the descriptor */
490 /* VARIABLES PROTECTED BY loopnconf_lock: loopnconf */
491 mutex_lock(&loopnconf_lock
);
492 if (loopnconf
== NULL
) {
493 struct netconfig
*nconf
, *tmpnconf
= NULL
;
497 nc_handle
= setnetconfig();
498 if (nc_handle
== NULL
) {
499 /* fails to open netconfig file */
500 syslog (LOG_ERR
, "rpc: failed to open " NETCONFIG
);
501 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
502 mutex_unlock(&loopnconf_lock
);
505 while ((nconf
= getnetconfig(nc_handle
)) != NULL
) {
507 if ((strcmp(nconf
->nc_protofmly
, NC_INET6
) == 0 ||
511 strcmp(nconf
->nc_protofmly
, NC_INET
) == 0) &&
512 (nconf
->nc_semantics
== NC_TPI_COTS
||
513 nconf
->nc_semantics
== NC_TPI_COTS_ORD
)) {
514 fd
= __rpc_nconf2fd(nconf
);
516 * Can't create a socket, assume that
517 * this family isn't configured in the kernel.
523 if (!strcmp(nconf
->nc_protofmly
, NC_INET
))
524 hostname
= IN4_LOCALHOST_STRING
;
526 hostname
= IN6_LOCALHOST_STRING
;
529 if (tmpnconf
== NULL
) {
530 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
531 mutex_unlock(&loopnconf_lock
);
534 loopnconf
= getnetconfigent(tmpnconf
->nc_netid
);
535 /* loopnconf is never freed */
536 endnetconfig(nc_handle
);
538 mutex_unlock(&loopnconf_lock
);
539 client
= getclnthandle(hostname
, loopnconf
, NULL
);
544 * Set a mapping between program, version and address.
545 * Calls the rpcbind service to do the mapping.
548 rpcb_set(program
, version
, nconf
, address
)
551 const struct netconfig
*nconf
; /* Network structure of transport */
552 const struct netbuf
*address
; /* Services netconfig address */
559 /* parameter checking */
561 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
564 if (address
== NULL
) {
565 rpc_createerr
.cf_stat
= RPC_UNKNOWNADDR
;
568 client
= local_rpcb();
573 /* convert to universal */
574 parms
.r_addr
= taddr2uaddr(__UNCONST(nconf
), __UNCONST(address
));
576 CLNT_DESTROY(client
);
577 rpc_createerr
.cf_stat
= RPC_N2AXLATEFAILURE
;
578 return (FALSE
); /* no universal address */
580 parms
.r_prog
= program
;
581 parms
.r_vers
= version
;
582 parms
.r_netid
= nconf
->nc_netid
;
584 * Though uid is not being used directly, we still send it for
585 * completeness. For non-unix platforms, perhaps some other
586 * string or an empty string can be sent.
588 (void) snprintf(uidbuf
, sizeof uidbuf
, "%d", geteuid());
589 parms
.r_owner
= uidbuf
;
591 CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_SET
, (xdrproc_t
) xdr_rpcb
,
592 (char *)(void *)&parms
, (xdrproc_t
) xdr_bool
,
593 (char *)(void *)&rslt
, tottimeout
);
595 CLNT_DESTROY(client
);
601 * Remove the mapping between program, version and netbuf address.
602 * Calls the rpcbind service to do the un-mapping.
603 * If netbuf is NULL, unset for all the transports, otherwise unset
604 * only for the given transport.
607 rpcb_unset(program
, version
, nconf
)
610 const struct netconfig
*nconf
;
617 client
= local_rpcb();
622 parms
.r_prog
= program
;
623 parms
.r_vers
= version
;
625 parms
.r_netid
= nconf
->nc_netid
;
627 parms
.r_netid
= __UNCONST(&nullstring
[0]); /* unsets all */
629 parms
.r_addr
= __UNCONST(&nullstring
[0]);
630 (void) snprintf(uidbuf
, sizeof uidbuf
, "%d", geteuid());
631 parms
.r_owner
= uidbuf
;
633 CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_UNSET
, (xdrproc_t
) xdr_rpcb
,
634 (char *)(void *)&parms
, (xdrproc_t
) xdr_bool
,
635 (char *)(void *)&rslt
, tottimeout
);
637 CLNT_DESTROY(client
);
642 * From the merged list, find the appropriate entry
644 static struct netbuf
*
645 got_entry(relp
, nconf
)
646 rpcb_entry_list_ptr relp
;
647 const struct netconfig
*nconf
;
649 struct netbuf
*na
= NULL
;
650 rpcb_entry_list_ptr sp
;
653 _DIAGASSERT(nconf
!= NULL
);
655 for (sp
= relp
; sp
!= NULL
; sp
= sp
->rpcb_entry_next
) {
656 rmap
= &sp
->rpcb_entry_map
;
657 if ((strcmp(nconf
->nc_proto
, rmap
->r_nc_proto
) == 0) &&
658 (strcmp(nconf
->nc_protofmly
, rmap
->r_nc_protofmly
) == 0) &&
659 (nconf
->nc_semantics
== rmap
->r_nc_semantics
) &&
660 (rmap
->r_maddr
!= NULL
) && (rmap
->r_maddr
[0] != 0)) {
661 na
= uaddr2taddr(nconf
, rmap
->r_maddr
);
663 fprintf(stderr
, "\tRemote address is [%s].\n",
667 "\tCouldn't resolve remote address!\n");
676 * An internal function which optimizes rpcb_getaddr function. It also
677 * returns the client handle that it uses to contact the remote rpcbind.
679 * The algorithm used: If the transports is TCP or UDP, it first tries
680 * version 2 (portmap), 4 and then 3 (svr4). This order should be
681 * changed in the next OS release to 4, 2 and 3. We are assuming that by
682 * that time, version 4 would be available on many machines on the network.
683 * With this algorithm, we get performance as well as a plan for
684 * obsoleting version 2.
686 * For all other transports, the algorithm remains as 4 and then 3.
688 * XXX: Due to some problems with t_connect(), we do not reuse the same client
689 * handle for COTS cases and hence in these cases we do not return the
690 * client handle. This code will change if t_connect() ever
691 * starts working properly. Also look under clnt_vc.c.
694 __rpcb_findaddr(program
, version
, nconf
, host
, clpp
)
697 const struct netconfig
*nconf
;
701 CLIENT
*client
= NULL
;
703 enum clnt_stat clnt_st
;
706 struct netbuf
*address
= NULL
;
707 rpcvers_t start_vers
= RPCBVERS4
;
708 struct netbuf servaddr
;
710 /* nconf is handled below */
711 _DIAGASSERT(host
!= NULL
);
712 /* clpp may be NULL */
714 /* parameter checking */
716 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
723 /* Try version 2 for TCP or UDP */
724 if (strcmp(nconf
->nc_protofmly
, NC_INET
) == 0) {
726 struct netbuf remote
;
727 rpcvers_t pmapvers
= 2;
728 struct pmap pmapparms
;
731 * Try UDP only - there are some portmappers out
732 * there that use UDP only.
734 if (strcmp(nconf
->nc_proto
, NC_TCP
) == 0) {
735 struct netconfig
*newnconf
;
737 if ((newnconf
= getnetconfigent("udp")) == NULL
) {
738 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
741 client
= getclnthandle(host
, newnconf
, &parms
.r_addr
);
742 freenetconfigent(newnconf
);
744 client
= getclnthandle(host
, nconf
, &parms
.r_addr
);
746 if (client
== NULL
) {
750 /* Set the version */
751 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&pmapvers
);
752 pmapparms
.pm_prog
= program
;
753 pmapparms
.pm_vers
= version
;
754 pmapparms
.pm_prot
= strcmp(nconf
->nc_proto
, NC_TCP
) ?
755 IPPROTO_UDP
: IPPROTO_TCP
;
756 pmapparms
.pm_port
= 0; /* not needed */
757 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)PMAPPROC_GETPORT
,
758 (xdrproc_t
) xdr_pmap
, (caddr_t
)(void *)&pmapparms
,
759 (xdrproc_t
) xdr_u_short
, (caddr_t
)(void *)&port
,
761 if (clnt_st
!= RPC_SUCCESS
) {
762 if ((clnt_st
== RPC_PROGVERSMISMATCH
) ||
763 (clnt_st
== RPC_PROGUNAVAIL
))
764 goto try_rpcbind
; /* Try different versions */
765 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
766 clnt_geterr(client
, &rpc_createerr
.cf_error
);
768 } else if (port
== 0) {
770 rpc_createerr
.cf_stat
= RPC_PROGNOTREGISTERED
;
774 CLNT_CONTROL(client
, CLGET_SVC_ADDR
, (char *)(void *)&remote
);
775 if (((address
= malloc(sizeof(struct netbuf
))) == NULL
) ||
776 ((address
->buf
= malloc(remote
.len
)) == NULL
)) {
777 rpc_createerr
.cf_stat
= RPC_SYSTEMERROR
;
778 clnt_geterr(client
, &rpc_createerr
.cf_error
);
785 memcpy(address
->buf
, remote
.buf
, remote
.len
);
786 memcpy(&((char *)address
->buf
)[sizeof (short)],
787 (char *)(void *)&port
, sizeof (short));
788 address
->len
= address
->maxlen
= remote
.len
;
795 * Now we try version 4 and then 3.
796 * We also send the remote system the address we used to
797 * contact it in case it can help to connect back with us
799 parms
.r_prog
= program
;
800 parms
.r_vers
= version
;
801 parms
.r_owner
= __UNCONST(&nullstring
[0]); /* not needed; */
802 /* just for xdring */
803 parms
.r_netid
= nconf
->nc_netid
; /* not really needed */
806 * If a COTS transport is being used, try getting address via CLTS
807 * transport. This works only with version 4.
808 * NOTE: This is being done for all transports EXCEPT LOOPBACK
809 * because with loopback the cost to go to a COTS is same as
810 * the cost to go through CLTS, plus you get the advantage of
811 * finding out immediately if the local rpcbind process is dead.
814 if ((nconf
->nc_semantics
== NC_TPI_COTS_ORD
||
815 nconf
->nc_semantics
== NC_TPI_COTS
) &&
816 (strcmp(nconf
->nc_protofmly
, NC_LOOPBACK
) != 0))
818 if (client
!= NULL
) {
819 CLNT_DESTROY(client
);
822 if (nconf
->nc_semantics
== NC_TPI_CLTS
)
826 struct netconfig
*nconf_clts
;
827 rpcb_entry_list_ptr relp
= NULL
;
829 if (client
== NULL
) {
830 /* This did not go through the above PORTMAP/TCP code */
832 if ((handle
= __rpc_setconf("datagram_v")) != NULL
)
834 if ((handle
= __rpc_setconf("circuit_v")) != NULL
)
837 while ((nconf_clts
= __rpc_getconf(handle
))
839 if (strcmp(nconf_clts
->nc_protofmly
,
840 nconf
->nc_protofmly
) != 0) {
843 client
= getclnthandle(host
, nconf_clts
,
847 __rpc_endconf(handle
);
850 goto regular_rpcbind
; /* Go the regular way */
852 /* This is a UDP PORTMAP handle. Change to version 4 */
854 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
857 * We also send the remote system the address we used to
858 * contact it in case it can help it connect back with us
860 if (parms
.r_addr
== NULL
) {
862 parms
.r_addr
= __UNCONST(&nullstring
[0]);
864 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETADDRLIST
,
865 (xdrproc_t
) xdr_rpcb
, (char *)(void *)&parms
,
866 (xdrproc_t
) xdr_rpcb_entry_list_ptr
,
867 (char *)(void *)&relp
, tottimeout
);
868 if (clnt_st
== RPC_SUCCESS
) {
869 if ((address
= got_entry(relp
, nconf
)) != NULL
) {
870 xdr_free((xdrproc_t
) xdr_rpcb_entry_list_ptr
,
871 (char *)(void *)&relp
);
872 CLNT_CONTROL(client
, CLGET_SVC_ADDR
,
873 (char *)(void *)&servaddr
);
874 __rpc_fixup_addr(address
, &servaddr
);
877 /* Entry not found for this transport */
878 xdr_free((xdrproc_t
) xdr_rpcb_entry_list_ptr
,
879 (char *)(void *)&relp
);
881 * XXX: should have perhaps returned with error but
882 * since the remote machine might not always be able
883 * to send the address on all transports, we try the
884 * regular way with regular_rpcbind
886 goto regular_rpcbind
;
887 } else if ((clnt_st
== RPC_PROGVERSMISMATCH
) ||
888 (clnt_st
== RPC_PROGUNAVAIL
)) {
889 start_vers
= RPCBVERS
; /* Try version 3 now */
890 goto regular_rpcbind
; /* Try different versions */
892 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
893 clnt_geterr(client
, &rpc_createerr
.cf_error
);
900 /* Now the same transport is to be used to get the address */
902 if (client
&& ((nconf
->nc_semantics
== NC_TPI_COTS_ORD
) ||
903 (nconf
->nc_semantics
== NC_TPI_COTS
)))
905 if (client
&& nconf
->nc_semantics
== NC_TPI_CLTS
)
908 /* A CLTS type of client - destroy it */
909 CLNT_DESTROY(client
);
913 if (client
== NULL
) {
914 client
= getclnthandle(host
, nconf
, &parms
.r_addr
);
915 if (client
== NULL
) {
919 if (parms
.r_addr
== NULL
)
920 parms
.r_addr
= __UNCONST(&nullstring
[0]);
922 /* First try from start_vers and then version 3 (RPCBVERS) */
923 for (vers
= start_vers
; vers
>= RPCBVERS
; vers
--) {
924 /* Set the version */
925 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
926 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETADDR
,
927 (xdrproc_t
) xdr_rpcb
, (char *)(void *)&parms
,
928 (xdrproc_t
) xdr_wrapstring
, (char *)(void *) &ua
,
930 if (clnt_st
== RPC_SUCCESS
) {
931 if ((ua
== NULL
) || (ua
[0] == 0)) {
932 /* address unknown */
933 rpc_createerr
.cf_stat
= RPC_PROGNOTREGISTERED
;
936 address
= uaddr2taddr(nconf
, ua
);
938 fprintf(stderr
, "\tRemote address is [%s]\n", ua
);
941 "\tCouldn't resolve remote address!\n");
943 xdr_free((xdrproc_t
)xdr_wrapstring
,
944 (char *)(void *)&ua
);
947 /* We don't know about your universal address */
948 rpc_createerr
.cf_stat
= RPC_N2AXLATEFAILURE
;
951 CLNT_CONTROL(client
, CLGET_SVC_ADDR
,
952 (char *)(void *)&servaddr
);
953 __rpc_fixup_addr(address
, &servaddr
);
955 } else if (clnt_st
== RPC_PROGVERSMISMATCH
) {
956 struct rpc_err rpcerr
;
958 clnt_geterr(client
, &rpcerr
);
959 if (rpcerr
.re_vers
.low
> RPCBVERS4
)
960 goto error
; /* a new version, can't handle */
961 } else if (clnt_st
!= RPC_PROGUNAVAIL
) {
962 /* Cant handle this error */
963 rpc_createerr
.cf_stat
= clnt_st
;
964 clnt_geterr(client
, &rpc_createerr
.cf_error
);
971 CLNT_DESTROY(client
);
975 if (nconf
->nc_semantics
!= NC_TPI_CLTS
) {
976 /* This client is the connectionless one */
978 CLNT_DESTROY(client
);
985 CLNT_DESTROY(client
);
992 * Find the mapped address for program, version.
993 * Calls the rpcbind service remotely to do the lookup.
994 * Uses the transport specified in nconf.
995 * Returns FALSE (0) if no map exists, else returns 1.
997 * Assuming that the address is all properly allocated
1000 rpcb_getaddr(program
, version
, nconf
, address
, host
)
1003 const struct netconfig
*nconf
;
1004 struct netbuf
*address
;
1009 _DIAGASSERT(address
!= NULL
);
1011 if ((na
= __rpcb_findaddr(program
, version
, nconf
,
1012 host
, NULL
)) == NULL
)
1015 if (na
->len
> address
->maxlen
) {
1016 /* Too long address */
1019 rpc_createerr
.cf_stat
= RPC_FAILED
;
1022 memcpy(address
->buf
, na
->buf
, (size_t)na
->len
);
1023 address
->len
= na
->len
;
1030 * Get a copy of the current maps.
1031 * Calls the rpcbind service remotely to get the maps.
1033 * It returns only a list of the services
1034 * It returns NULL on failure.
1037 rpcb_getmaps(nconf
, host
)
1038 const struct netconfig
*nconf
;
1041 rpcblist_ptr head
= NULL
;
1043 enum clnt_stat clnt_st
;
1046 client
= getclnthandle(host
, nconf
, NULL
);
1047 if (client
== NULL
) {
1050 clnt_st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_DUMP
,
1051 (xdrproc_t
) xdr_void
, NULL
, (xdrproc_t
) xdr_rpcblist_ptr
,
1052 (char *)(void *)&head
, tottimeout
);
1053 if (clnt_st
== RPC_SUCCESS
)
1056 if ((clnt_st
!= RPC_PROGVERSMISMATCH
) &&
1057 (clnt_st
!= RPC_PROGUNAVAIL
)) {
1058 rpc_createerr
.cf_stat
= RPC_RPCBFAILURE
;
1059 clnt_geterr(client
, &rpc_createerr
.cf_error
);
1063 /* fall back to earlier version */
1064 CLNT_CONTROL(client
, CLGET_VERS
, (char *)(void *)&vers
);
1065 if (vers
== RPCBVERS4
) {
1067 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
1068 if (CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_DUMP
,
1069 (xdrproc_t
) xdr_void
, NULL
, (xdrproc_t
) xdr_rpcblist_ptr
,
1070 (char *)(void *)&head
, tottimeout
) == RPC_SUCCESS
)
1073 rpc_createerr
.cf_stat
= RPC_RPCBFAILURE
;
1074 clnt_geterr(client
, &rpc_createerr
.cf_error
);
1077 CLNT_DESTROY(client
);
1082 * rpcbinder remote-call-service interface.
1083 * This routine is used to call the rpcbind remote call service
1084 * which will look up a service program in the address maps, and then
1085 * remotely call that routine with the given parameters. This allows
1086 * programs to do a lookup and call in one step.
1089 rpcb_rmtcall(nconf
, host
, prog
, vers
, proc
, xdrargs
, argsp
,
1090 xdrres
, resp
, tout
, addr_ptr
)
1091 const struct netconfig
*nconf
; /* Netconfig structure */
1092 const char *host
; /* Remote host name */
1095 rpcproc_t proc
; /* Remote proc identifiers */
1096 xdrproc_t xdrargs
, xdrres
; /* XDR routines */
1097 const char *argsp
; /* Argument */
1098 caddr_t resp
; /* Result */
1099 struct timeval tout
; /* Timeout value for this call */
1100 const struct netbuf
*addr_ptr
; /* Preallocated netbuf address */
1103 enum clnt_stat stat
;
1104 struct r_rpcb_rmtcallargs a
;
1105 struct r_rpcb_rmtcallres r
;
1106 rpcvers_t rpcb_vers
;
1108 stat
= RPC_FAILED
; /* XXXGCC -Wuninitialized [dreamcast] */
1110 client
= getclnthandle(host
, nconf
, NULL
);
1111 if (client
== NULL
) {
1112 return (RPC_FAILED
);
1114 CLNT_CONTROL(client
, CLSET_RETRY_TIMEOUT
, __UNCONST(&rmttimeout
));
1118 a
.args
.args_val
= argsp
;
1119 a
.xdr_args
= xdrargs
;
1121 r
.results
.results_val
= resp
;
1124 for (rpcb_vers
= RPCBVERS4
; rpcb_vers
>= RPCBVERS
; rpcb_vers
--) {
1125 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&rpcb_vers
);
1126 stat
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_CALLIT
,
1127 (xdrproc_t
) xdr_rpcb_rmtcallargs
, (char *)(void *)&a
,
1128 (xdrproc_t
) xdr_rpcb_rmtcallres
, (char *)(void *)&r
, tout
);
1129 if ((stat
== RPC_SUCCESS
) && (addr_ptr
!= NULL
)) {
1131 na
= uaddr2taddr(__UNCONST(nconf
), r
.addr
);
1133 stat
= RPC_N2AXLATEFAILURE
;
1134 ((struct netbuf
*)__UNCONST(addr_ptr
))->len
= 0;
1137 if (na
->len
> addr_ptr
->maxlen
) {
1138 /* Too long address */
1139 stat
= RPC_FAILED
; /* XXX A better error no */
1142 ((struct netbuf
*)__UNCONST(addr_ptr
))->len
= 0;
1145 memcpy(addr_ptr
->buf
, na
->buf
, (size_t)na
->len
);
1146 ((struct netbuf
*)__UNCONST(addr_ptr
))->len
= na
->len
;
1150 } else if ((stat
!= RPC_PROGVERSMISMATCH
) &&
1151 (stat
!= RPC_PROGUNAVAIL
)) {
1156 CLNT_DESTROY(client
);
1158 xdr_free((xdrproc_t
) xdr_wrapstring
, (char *)(void *)&r
.addr
);
1163 * Gets the time on the remote host.
1164 * Returns 1 if succeeds else 0.
1167 rpcb_gettime(host
, timep
)
1171 CLIENT
*client
= NULL
;
1173 struct netconfig
*nconf
;
1178 if ((host
== NULL
) || (host
[0] == 0)) {
1183 if ((handle
= __rpc_setconf("netpath")) == NULL
) {
1184 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1187 rpc_createerr
.cf_stat
= RPC_SUCCESS
;
1188 while (client
== NULL
) {
1189 if ((nconf
= __rpc_getconf(handle
)) == NULL
) {
1190 if (rpc_createerr
.cf_stat
== RPC_SUCCESS
)
1191 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1194 client
= getclnthandle(host
, nconf
, NULL
);
1198 __rpc_endconf(handle
);
1199 if (client
== NULL
) {
1203 st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETTIME
,
1204 (xdrproc_t
) xdr_void
, NULL
,
1205 (xdrproc_t
) xdr_int
, (char *)(void *)timep
, tottimeout
);
1207 if ((st
== RPC_PROGVERSMISMATCH
) || (st
== RPC_PROGUNAVAIL
)) {
1208 CLNT_CONTROL(client
, CLGET_VERS
, (char *)(void *)&vers
);
1209 if (vers
== RPCBVERS4
) {
1210 /* fall back to earlier version */
1212 CLNT_CONTROL(client
, CLSET_VERS
, (char *)(void *)&vers
);
1213 st
= CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_GETTIME
,
1214 (xdrproc_t
) xdr_void
, NULL
,
1215 (xdrproc_t
) xdr_int
, (char *)(void *)timep
,
1219 CLNT_DESTROY(client
);
1220 return (st
== RPC_SUCCESS
? TRUE
: FALSE
);
1224 * Converts taddr to universal address. This routine should never
1225 * really be called because local n2a libraries are always provided.
1228 rpcb_taddr2uaddr(nconf
, taddr
)
1229 struct netconfig
*nconf
;
1230 struct netbuf
*taddr
;
1236 /* parameter checking */
1237 if (nconf
== NULL
) {
1238 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1241 if (taddr
== NULL
) {
1242 rpc_createerr
.cf_stat
= RPC_UNKNOWNADDR
;
1245 client
= local_rpcb();
1250 CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_TADDR2UADDR
,
1251 (xdrproc_t
) xdr_netbuf
, (char *)(void *)taddr
,
1252 (xdrproc_t
) xdr_wrapstring
, (char *)(void *)&uaddr
, tottimeout
);
1253 CLNT_DESTROY(client
);
1258 * Converts universal address to netbuf. This routine should never
1259 * really be called because local n2a libraries are always provided.
1262 rpcb_uaddr2taddr(nconf
, uaddr
)
1263 struct netconfig
*nconf
;
1267 struct netbuf
*taddr
;
1270 /* parameter checking */
1271 if (nconf
== NULL
) {
1272 rpc_createerr
.cf_stat
= RPC_UNKNOWNPROTO
;
1275 if (uaddr
== NULL
) {
1276 rpc_createerr
.cf_stat
= RPC_UNKNOWNADDR
;
1279 client
= local_rpcb();
1284 taddr
= (struct netbuf
*)calloc(1, sizeof (struct netbuf
));
1285 if (taddr
== NULL
) {
1286 CLNT_DESTROY(client
);
1289 if (CLNT_CALL(client
, (rpcproc_t
)RPCBPROC_UADDR2TADDR
,
1290 (xdrproc_t
) xdr_wrapstring
, (char *)(void *)&uaddr
,
1291 (xdrproc_t
) xdr_netbuf
, (char *)(void *)taddr
,
1292 tottimeout
) != RPC_SUCCESS
) {
1296 CLNT_DESTROY(client
);