1 /* $NetBSD: clnt_bcast.c,v 1.22 2010/03/07 23:49:14 dholland 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 "@(#)clnt_bcast.c 1.18 94/05/03 SMI" */
37 #include <sys/cdefs.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
40 static char sccsid
[] = "@(#)clnt_bcast.c 1.15 89/04/21 Copyr 1988 Sun Micro";
42 __RCSID("$NetBSD: clnt_bcast.c,v 1.22 2010/03/07 23:49:14 dholland Exp $");
48 * Client interface to broadcast service.
50 * Copyright (C) 1988, Sun Microsystems, Inc.
52 * The following is kludged-up support for simple rpc broadcasts.
53 * Someday a large, complicated system will replace these routines.
56 #include "namespace.h"
57 #include <sys/types.h>
58 #include <sys/socket.h>
59 #include <sys/queue.h>
61 #include <netinet/in.h>
66 #include <rpc/pmap_prot.h>
67 #include <rpc/pmap_clnt.h>
68 #include <rpc/pmap_rmt.h>
70 #include <rpc/nettype.h>
71 #include <arpa/inet.h>
83 #include "rpc_internal.h"
85 #define MAXBCAST 20 /* Max no of broadcasting transports */
86 #define INITTIME 4000 /* Time to wait initially */
87 #define WAITTIME 8000 /* Maximum time to wait */
90 * If nettype is NULL, it broadcasts on all the available
91 * datagram_n transports. May potentially lead to broadacst storms
92 * and hence should be used with caution, care and courage.
94 * The current parameter xdr packet size is limited by the max tsdu
95 * size of the transport. If the max tsdu size of any transport is
96 * smaller than the parameter xdr packet, then broadcast is not
97 * sent on that transport.
99 * Also, the packet size should be less the packet size of
100 * the data link layer (for ethernet it is 1400 bytes). There is
101 * no easy way to find out the max size of the data link layer and
102 * we are assuming that the args would be smaller than that.
104 * The result size has to be smaller than the transport tsdu size.
106 * If PORTMAP has been defined, we send two packets for UDP, one for
107 * rpcbind and one for portmap. For those machines which support
108 * both rpcbind and portmap, it will cause them to reply twice, and
109 * also here it will get two responses ... inefficient and clumsy.
113 __weak_alias(rpc_broadcast_exp
,_rpc_broadcast_exp
)
114 __weak_alias(rpc_broadcast
,_rpc_broadcast
)
119 struct sockaddr_storage broadaddr
;
120 TAILQ_ENTRY(broadif
) link
;
123 typedef TAILQ_HEAD(, broadif
) broadlist_t
;
125 int __rpc_getbroadifs
__P((int, int, int, broadlist_t
*));
126 void __rpc_freebroadifs
__P((broadlist_t
*));
127 int __rpc_broadenable
__P((int, int, struct broadif
*));
129 int __rpc_lowvers
= 0;
132 __rpc_getbroadifs(int af
, int proto
, int socktype
, broadlist_t
*list
)
136 struct ifaddrs
*ifap
, *ifp
;
138 struct sockaddr_in6
*sin6
;
140 struct sockaddr_in
*gbsin
;
141 struct addrinfo hints
, *res
;
143 _DIAGASSERT(list
!= NULL
);
145 if (getifaddrs(&ifp
) < 0)
148 memset(&hints
, 0, sizeof hints
);
150 hints
.ai_family
= af
;
151 hints
.ai_protocol
= proto
;
152 hints
.ai_socktype
= socktype
;
154 if (getaddrinfo(NULL
, "sunrpc", &hints
, &res
) != 0) {
159 for (ifap
= ifp
; ifap
!= NULL
; ifap
= ifap
->ifa_next
) {
160 if (ifap
->ifa_addr
->sa_family
!= af
||
161 !(ifap
->ifa_flags
& IFF_UP
))
163 bip
= malloc(sizeof(*bip
));
166 bip
->index
= if_nametoindex(ifap
->ifa_name
);
171 (ifap
->ifa_flags
& IFF_BROADCAST
) &&
172 ifap
->ifa_broadaddr
) {
173 memcpy(&bip
->broadaddr
, ifap
->ifa_broadaddr
,
174 (size_t)ifap
->ifa_broadaddr
->sa_len
);
175 gbsin
= (struct sockaddr_in
*)(void *)&bip
->broadaddr
;
177 ((struct sockaddr_in
*)
178 (void *)res
->ai_addr
)->sin_port
;
181 if (af
== AF_INET6
&& (ifap
->ifa_flags
& IFF_MULTICAST
)) {
182 sin6
= (struct sockaddr_in6
*)(void *)&bip
->broadaddr
;
183 inet_pton(af
, RPCB_MULTICAST_ADDR
, &sin6
->sin6_addr
);
184 sin6
->sin6_family
= af
;
185 sin6
->sin6_len
= sizeof *sin6
;
187 ((struct sockaddr_in6
*)
188 (void *)res
->ai_addr
)->sin6_port
;
189 sin6
->sin6_scope_id
= bip
->index
;
196 TAILQ_INSERT_TAIL(list
, bip
, link
);
206 __rpc_freebroadifs(broadlist_t
*list
)
208 struct broadif
*bip
, *next
;
210 _DIAGASSERT(list
!= NULL
);
212 bip
= TAILQ_FIRST(list
);
214 while (bip
!= NULL
) {
215 next
= TAILQ_NEXT(bip
, link
);
223 __rpc_broadenable(int af
, int s
, struct broadif
*bip
)
228 _DIAGASSERT(bip
!= NULL
);
230 if (af
== AF_INET6
) {
231 fprintf(stderr
, "set v6 multicast if to %d\n", bip
->index
);
232 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_IF
, &bip
->index
,
233 sizeof bip
->index
) < 0)
237 if (setsockopt(s
, SOL_SOCKET
, SO_BROADCAST
, &o
, sizeof o
) < 0)
245 rpc_broadcast_exp(prog
, vers
, proc
, xargs
, argsp
, xresults
, resultsp
,
246 eachresult
, inittime
, waittime
, nettype
)
247 rpcprog_t prog
; /* program number */
248 rpcvers_t vers
; /* version number */
249 rpcproc_t proc
; /* procedure number */
250 xdrproc_t xargs
; /* xdr routine for args */
251 const char * argsp
; /* pointer to args */
252 xdrproc_t xresults
; /* xdr routine for results */
253 caddr_t resultsp
; /* pointer to results */
254 resultproc_t eachresult
; /* call with each result obtained */
255 int inittime
; /* how long to wait initially */
256 int waittime
; /* maximum time to wait */
257 const char *nettype
; /* transport type */
259 enum clnt_stat stat
= RPC_SUCCESS
; /* Return status */
260 XDR xdr_stream
; /* XDR stream */
261 XDR
*xdrs
= &xdr_stream
;
262 struct rpc_msg msg
; /* RPC message */
263 char *outbuf
= NULL
; /* Broadcast msg buffer */
264 char *inbuf
= NULL
; /* Reply buf */
266 u_int maxbufsize
= 0;
267 AUTH
*sys_auth
= authunix_create_default();
270 char uaddress
[1024]; /* A self imposed limit */
271 char *uaddrp
= uaddress
;
272 int pmap_reply_flag
; /* reply recvd from PORTMAP */
273 /* An array of all the suitable broadcast transports */
275 int fd
; /* File descriptor */
278 struct netconfig
*nconf
; /* Netconfig structure */
279 u_int asize
; /* Size of the addr buf */
280 u_int dsize
; /* Size of the data buf */
281 struct sockaddr_storage raddr
; /* Remote address */
284 struct pollfd pfd
[MAXBCAST
];
286 struct r_rpcb_rmtcallargs barg
; /* Remote arguments */
287 struct r_rpcb_rmtcallres bres
; /* Remote results */
289 struct netconfig
*nconf
;
296 size_t outlen_pmap
= 0;
297 u_long port
; /* Remote port number */
298 int pmap_flag
= 0; /* UDP exists ? */
299 char *outbuf_pmap
= NULL
;
300 struct rmtcallargs barg_pmap
; /* Remote arguments */
301 struct rmtcallres bres_pmap
; /* Remote results */
305 if (sys_auth
== NULL
) {
306 return (RPC_SYSTEMERROR
);
309 * initialization: create a fd, a broadcast address, and send the
310 * request on the broadcast transport.
311 * Listen on all of them and on replies, call the user supplied
316 nettype
= "datagram_n";
317 if ((handle
= __rpc_setconf(nettype
)) == NULL
) {
318 AUTH_DESTROY(sys_auth
);
319 return (RPC_UNKNOWNPROTO
);
321 while ((nconf
= __rpc_getconf(handle
)) != NULL
) {
323 struct __rpc_sockinfo si
;
325 if (nconf
->nc_semantics
!= NC_TPI_CLTS
)
327 if (fdlistno
>= MAXBCAST
)
328 break; /* No more slots available */
329 if (!__rpc_nconf2sockinfo(nconf
, &si
))
332 TAILQ_INIT(&fdlist
[fdlistno
].nal
);
333 if (__rpc_getbroadifs(si
.si_af
, si
.si_proto
, si
.si_socktype
,
334 &fdlist
[fdlistno
].nal
) == 0)
337 fd
= socket(si
.si_af
, si
.si_socktype
, si
.si_proto
);
342 fdlist
[fdlistno
].af
= si
.si_af
;
343 fdlist
[fdlistno
].proto
= si
.si_proto
;
344 fdlist
[fdlistno
].fd
= fd
;
345 fdlist
[fdlistno
].nconf
= nconf
;
346 fdlist
[fdlistno
].asize
= __rpc_get_a_size(si
.si_af
);
347 pfd
[fdlistno
].events
= POLLIN
| POLLPRI
|
348 POLLRDNORM
| POLLRDBAND
;
349 pfd
[fdlistno
].fd
= fdlist
[fdlistno
].fd
= fd
;
350 fdlist
[fdlistno
].dsize
= __rpc_get_t_size(si
.si_af
, si
.si_proto
,
353 if (maxbufsize
<= fdlist
[fdlistno
].dsize
)
354 maxbufsize
= fdlist
[fdlistno
].dsize
;
357 if (si
.si_af
== AF_INET
&& si
.si_proto
== IPPROTO_UDP
) {
358 udpbufsz
= fdlist
[fdlistno
].dsize
;
359 if ((outbuf_pmap
= malloc(udpbufsz
)) == NULL
) {
361 stat
= RPC_SYSTEMERROR
;
371 if (stat
== RPC_SUCCESS
)
372 stat
= RPC_UNKNOWNPROTO
;
375 if (maxbufsize
== 0) {
376 if (stat
== RPC_SUCCESS
)
380 inbuf
= malloc(maxbufsize
);
381 outbuf
= malloc(maxbufsize
);
382 if ((inbuf
== NULL
) || (outbuf
== NULL
)) {
383 stat
= RPC_SYSTEMERROR
;
387 /* Serialize all the arguments which have to be sent */
388 msg
.rm_xid
= __RPC_GETXID();
389 msg
.rm_direction
= CALL
;
390 msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
391 msg
.rm_call
.cb_prog
= RPCBPROG
;
392 msg
.rm_call
.cb_vers
= RPCBVERS
;
393 msg
.rm_call
.cb_proc
= RPCBPROC_CALLIT
;
397 barg
.args
.args_val
= argsp
;
398 barg
.xdr_args
= xargs
;
400 bres
.results
.results_val
= resultsp
;
401 bres
.xdr_res
= xresults
;
402 msg
.rm_call
.cb_cred
= sys_auth
->ah_cred
;
403 msg
.rm_call
.cb_verf
= sys_auth
->ah_verf
;
404 xdrmem_create(xdrs
, outbuf
, maxbufsize
, XDR_ENCODE
);
405 if ((!xdr_callmsg(xdrs
, &msg
)) ||
406 (!xdr_rpcb_rmtcallargs(xdrs
,
407 (struct rpcb_rmtcallargs
*)(void *)&barg
))) {
408 stat
= RPC_CANTENCODEARGS
;
411 outlen
= xdr_getpos(xdrs
);
415 /* Prepare the packet for version 2 PORTMAP */
417 msg
.rm_xid
++; /* One way to distinguish */
418 msg
.rm_call
.cb_prog
= PMAPPROG
;
419 msg
.rm_call
.cb_vers
= PMAPVERS
;
420 msg
.rm_call
.cb_proc
= PMAPPROC_CALLIT
;
421 barg_pmap
.prog
= prog
;
422 barg_pmap
.vers
= vers
;
423 barg_pmap
.proc
= proc
;
424 barg_pmap
.args_ptr
= argsp
;
425 barg_pmap
.xdr_args
= xargs
;
426 bres_pmap
.port_ptr
= &port
;
427 bres_pmap
.xdr_results
= xresults
;
428 bres_pmap
.results_ptr
= resultsp
;
429 xdrmem_create(xdrs
, outbuf_pmap
, udpbufsz
, XDR_ENCODE
);
430 if ((! xdr_callmsg(xdrs
, &msg
)) ||
431 (! xdr_rmtcall_args(xdrs
, &barg_pmap
))) {
432 stat
= RPC_CANTENCODEARGS
;
435 outlen_pmap
= xdr_getpos(xdrs
);
441 * Basic loop: broadcast the packets to transports which
442 * support data packets of size such that one can encode
444 * Wait a while for response(s).
445 * The response timeout grows larger per iteration.
447 for (msec
= inittime
; msec
<= waittime
; msec
+= msec
) {
450 /* Broadcast all the packets now */
451 for (i
= 0; i
< fdlistno
; i
++) {
452 if (fdlist
[i
].dsize
< outlen
) {
456 for (bip
= TAILQ_FIRST(&fdlist
[i
].nal
); bip
!= NULL
;
457 bip
= TAILQ_NEXT(bip
, link
)) {
460 addr
= &bip
->broadaddr
;
462 __rpc_broadenable(fdlist
[i
].af
, fdlist
[i
].fd
,
466 * Only use version 3 if lowvers is not set
470 if ((size_t)sendto(fdlist
[i
].fd
, outbuf
,
471 outlen
, 0, (struct sockaddr
*)addr
,
472 (size_t)fdlist
[i
].asize
) !=
474 warn("clnt_bcast: cannot send"
475 " broadcast packet");
481 fprintf(stderr
, "Broadcast packet sent "
483 fdlist
[i
].nconf
->nc_netid
);
487 * Send the version 2 packet also
491 fdlist
[i
].proto
== IPPROTO_UDP
) {
492 if ((size_t)sendto(fdlist
[i
].fd
,
493 outbuf_pmap
, outlen_pmap
, 0, addr
,
494 (size_t)fdlist
[i
].asize
) !=
504 fprintf(stderr
, "PMAP Broadcast packet "
506 fdlist
[i
].nconf
->nc_netid
);
510 /* End for sending all packets on this transport */
511 } /* End for sending on all transports */
513 if (eachresult
== NULL
) {
519 * Get all the replies from these broadcast requests
522 ts
.tv_sec
= msec
/ 1000;
523 ts
.tv_nsec
= (msec
% 1000) * 1000000;
525 switch (pollretval
= pollts(pfd
, fdlistno
, &ts
, NULL
)) {
526 case 0: /* timed out */
529 case -1: /* some kind of error - we ignore it */
531 } /* end of poll results switch */
533 for (i
= fds_found
= 0;
534 i
< fdlistno
&& fds_found
< pollretval
; i
++) {
537 if (pfd
[i
].revents
== 0)
539 else if (pfd
[i
].revents
& POLLNVAL
) {
541 * Something bad has happened to this descri-
542 * ptor. We can cause pollts() to ignore
543 * it simply by using a negative fd. We do that
544 * rather than compacting the pfd[] and fdlist[]
553 fprintf(stderr
, "response for %s\n",
554 fdlist
[i
].nconf
->nc_netid
);
557 inlen
= recvfrom(fdlist
[i
].fd
, inbuf
, fdlist
[i
].dsize
,
558 0, (struct sockaddr
*)(void *)&fdlist
[i
].raddr
,
563 warnx("clnt_bcast: Cannot receive reply to "
568 if (inlen
< (ssize_t
)sizeof(u_int32_t
))
569 continue; /* Drop that and go ahead */
571 * see if reply transaction id matches sent id.
572 * If so, decode the results. If return id is xid + 1
573 * it was a PORTMAP reply
575 if (*((u_int32_t
*)(void *)(inbuf
)) ==
576 *((u_int32_t
*)(void *)(outbuf
))) {
578 msg
.acpted_rply
.ar_verf
= _null_auth
;
579 msg
.acpted_rply
.ar_results
.where
=
580 (caddr_t
)(void *)&bres
;
581 msg
.acpted_rply
.ar_results
.proc
=
582 (xdrproc_t
)xdr_rpcb_rmtcallres
;
584 } else if (pmap_flag
&&
585 *((u_int32_t
*)(void *)(inbuf
)) ==
586 *((u_int32_t
*)(void *)(outbuf_pmap
))) {
588 msg
.acpted_rply
.ar_verf
= _null_auth
;
589 msg
.acpted_rply
.ar_results
.where
=
590 (caddr_t
)(void *)&bres_pmap
;
591 msg
.acpted_rply
.ar_results
.proc
=
592 (xdrproc_t
)xdr_rmtcallres
;
596 xdrmem_create(xdrs
, inbuf
, (u_int
)inlen
, XDR_DECODE
);
597 if (xdr_replymsg(xdrs
, &msg
)) {
598 if ((msg
.rm_reply
.rp_stat
== MSG_ACCEPTED
) &&
599 (msg
.acpted_rply
.ar_stat
== SUCCESS
)) {
600 struct netbuf taddr
, *np
;
601 struct sockaddr_in
*bsin
;
604 if (pmap_flag
&& pmap_reply_flag
) {
605 bsin
= (struct sockaddr_in
*)
606 (void *)&fdlist
[i
].raddr
;
608 htons((u_short
)port
);
609 taddr
.len
= taddr
.maxlen
=
610 fdlist
[i
].raddr
.ss_len
;
611 taddr
.buf
= &fdlist
[i
].raddr
;
612 done
= (*eachresult
)(resultsp
,
613 &taddr
, fdlist
[i
].nconf
);
617 fprintf(stderr
, "uaddr %s\n",
621 fdlist
[i
].nconf
, uaddrp
);
622 done
= (*eachresult
)(resultsp
,
623 np
, fdlist
[i
].nconf
);
629 /* otherwise, we just ignore the errors ... */
631 /* else some kind of deserialization problem ... */
633 xdrs
->x_op
= XDR_FREE
;
634 msg
.acpted_rply
.ar_results
.proc
= (xdrproc_t
) xdr_void
;
635 (void) xdr_replymsg(xdrs
, &msg
);
636 (void) (*xresults
)(xdrs
, resultsp
);
644 } /* The recv for loop */
645 } /* The giant for loop */
654 (void) free(outbuf_pmap
);
656 for (i
= 0; i
< fdlistno
; i
++) {
657 (void) close(fdlist
[i
].fd
);
658 __rpc_freebroadifs(&fdlist
[i
].nal
);
660 AUTH_DESTROY(sys_auth
);
661 (void) __rpc_endconf(handle
);
668 rpc_broadcast(prog
, vers
, proc
, xargs
, argsp
, xresults
, resultsp
,
670 rpcprog_t prog
; /* program number */
671 rpcvers_t vers
; /* version number */
672 rpcproc_t proc
; /* procedure number */
673 xdrproc_t xargs
; /* xdr routine for args */
674 const char * argsp
; /* pointer to args */
675 xdrproc_t xresults
; /* xdr routine for results */
676 caddr_t resultsp
; /* pointer to results */
677 resultproc_t eachresult
; /* call with each result obtained */
678 const char *nettype
; /* transport type */
680 enum clnt_stat dummy
;
682 dummy
= rpc_broadcast_exp(prog
, vers
, proc
, xargs
, argsp
,
683 xresults
, resultsp
, eachresult
,
684 INITTIME
, WAITTIME
, nettype
);