2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)clnt_bcast.c 1.18 94/05/03 SMI; 1.15 89/04/21 Copyr 1988 Sun Micro
30 * $NetBSD: clnt_bcast.c,v 1.3 2000/07/06 03:05:20 christos Exp $
31 * $FreeBSD: src/lib/libc/rpc/clnt_bcast.c,v 1.9 2006/09/09 22:14:42 mbr Exp $
34 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
39 * Client interface to broadcast service.
41 * Copyright (C) 1988, Sun Microsystems, Inc.
43 * The following is kludged-up support for simple rpc broadcasts.
44 * Someday a large, complicated system will replace these routines.
47 #include "namespace.h"
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <sys/queue.h>
52 #include <netinet/in.h>
57 #include <rpc/pmap_prot.h>
58 #include <rpc/pmap_clnt.h>
59 #include <rpc/pmap_rmt.h>
61 #include <rpc/nettype.h>
62 #include <arpa/inet.h>
72 #include "un-namespace.h"
76 #define MAXBCAST 20 /* Max no of broadcasting transports */
77 #define INITTIME 4000 /* Time to wait initially */
78 #define WAITTIME 8000 /* Maximum time to wait */
81 * If nettype is NULL, it broadcasts on all the available
82 * datagram_n transports. May potentially lead to broadacst storms
83 * and hence should be used with caution, care and courage.
85 * The current parameter xdr packet size is limited by the max tsdu
86 * size of the transport. If the max tsdu size of any transport is
87 * smaller than the parameter xdr packet, then broadcast is not
88 * sent on that transport.
90 * Also, the packet size should be less the packet size of
91 * the data link layer (for ethernet it is 1400 bytes). There is
92 * no easy way to find out the max size of the data link layer and
93 * we are assuming that the args would be smaller than that.
95 * The result size has to be smaller than the transport tsdu size.
97 * If PORTMAP has been defined, we send two packets for UDP, one for
98 * rpcbind and one for portmap. For those machines which support
99 * both rpcbind and portmap, it will cause them to reply twice, and
100 * also here it will get two responses ... inefficient and clumsy.
105 struct sockaddr_storage broadaddr
;
106 TAILQ_ENTRY(broadif
) link
;
109 typedef TAILQ_HEAD(, broadif
) broadlist_t
;
111 int __rpc_broadenable(int, int, struct broadif
*);
112 void __rpc_freebroadifs(broadlist_t
*);
113 int __rpc_getbroadifs(int, int, int, broadlist_t
*);
115 int __rpc_lowvers
= 0;
118 __rpc_getbroadifs(int af
, int proto
, int socktype
, broadlist_t
*list
)
122 struct ifaddrs
*ifap
, *ifp
;
124 struct sockaddr_in6
*sin6
;
126 struct sockaddr_in
*sin
;
127 struct addrinfo hints
, *res
;
129 if (getifaddrs(&ifp
) < 0)
132 memset(&hints
, 0, sizeof hints
);
134 hints
.ai_family
= af
;
135 hints
.ai_protocol
= proto
;
136 hints
.ai_socktype
= socktype
;
138 if (getaddrinfo(NULL
, "sunrpc", &hints
, &res
) != 0) {
143 for (ifap
= ifp
; ifap
!= NULL
; ifap
= ifap
->ifa_next
) {
144 if (ifap
->ifa_addr
->sa_family
!= af
||
145 !(ifap
->ifa_flags
& IFF_UP
))
147 bip
= (struct broadif
*)malloc(sizeof *bip
);
150 bip
->index
= if_nametoindex(ifap
->ifa_name
);
155 (ifap
->ifa_flags
& IFF_BROADCAST
) &&
156 ifap
->ifa_broadaddr
) {
157 memcpy(&bip
->broadaddr
, ifap
->ifa_broadaddr
,
158 (size_t)ifap
->ifa_broadaddr
->sa_len
);
159 sin
= (struct sockaddr_in
*)(void *)&bip
->broadaddr
;
161 ((struct sockaddr_in
*)
162 (void *)res
->ai_addr
)->sin_port
;
165 if (af
== AF_INET6
&& (ifap
->ifa_flags
& IFF_MULTICAST
)) {
166 sin6
= (struct sockaddr_in6
*)(void *)&bip
->broadaddr
;
167 inet_pton(af
, RPCB_MULTICAST_ADDR
, &sin6
->sin6_addr
);
168 sin6
->sin6_family
= af
;
169 sin6
->sin6_len
= sizeof *sin6
;
171 ((struct sockaddr_in6
*)
172 (void *)res
->ai_addr
)->sin6_port
;
173 sin6
->sin6_scope_id
= bip
->index
;
180 TAILQ_INSERT_TAIL(list
, bip
, link
);
190 __rpc_freebroadifs(broadlist_t
*list
)
192 struct broadif
*bip
, *next
;
194 bip
= TAILQ_FIRST(list
);
196 while (bip
!= NULL
) {
197 next
= TAILQ_NEXT(bip
, link
);
205 __rpc_broadenable(int af
, int s
, struct broadif
*bip
)
210 if (af
== AF_INET6
) {
211 fprintf(stderr
, "set v6 multicast if to %d\n", bip
->index
);
212 if (_setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_IF
, &bip
->index
,
213 sizeof bip
->index
) < 0)
217 if (_setsockopt(s
, SOL_SOCKET
, SO_BROADCAST
, &o
, sizeof o
) < 0)
225 rpc_broadcast_exp(prog
, vers
, proc
, xargs
, argsp
, xresults
, resultsp
,
226 eachresult
, inittime
, waittime
, nettype
)
227 rpcprog_t prog
; /* program number */
228 rpcvers_t vers
; /* version number */
229 rpcproc_t proc
; /* procedure number */
230 xdrproc_t xargs
; /* xdr routine for args */
231 caddr_t argsp
; /* pointer to args */
232 xdrproc_t xresults
; /* xdr routine for results */
233 caddr_t resultsp
; /* pointer to results */
234 resultproc_t eachresult
; /* call with each result obtained */
235 int inittime
; /* how long to wait initially */
236 int waittime
; /* maximum time to wait */
237 const char *nettype
; /* transport type */
239 enum clnt_stat stat
= RPC_SUCCESS
; /* Return status */
240 XDR xdr_stream
; /* XDR stream */
241 XDR
*xdrs
= &xdr_stream
;
242 struct rpc_msg msg
; /* RPC message */
244 char *outbuf
= NULL
; /* Broadcast msg buffer */
245 char *inbuf
= NULL
; /* Reply buf */
247 u_int maxbufsize
= 0;
248 AUTH
*sys_auth
= authunix_create_default();
251 char uaddress
[1024]; /* A self imposed limit */
252 char *uaddrp
= uaddress
;
253 int pmap_reply_flag
; /* reply recvd from PORTMAP */
254 /* An array of all the suitable broadcast transports */
256 int fd
; /* File descriptor */
259 struct netconfig
*nconf
; /* Netconfig structure */
260 u_int asize
; /* Size of the addr buf */
261 u_int dsize
; /* Size of the data buf */
262 struct sockaddr_storage raddr
; /* Remote address */
265 struct pollfd pfd
[MAXBCAST
];
267 struct r_rpcb_rmtcallargs barg
; /* Remote arguments */
268 struct r_rpcb_rmtcallres bres
; /* Remote results */
270 struct netconfig
*nconf
;
276 size_t outlen_pmap
= 0;
277 u_long port
; /* Remote port number */
278 int pmap_flag
= 0; /* UDP exists ? */
279 char *outbuf_pmap
= NULL
;
280 struct rmtcallargs barg_pmap
; /* Remote arguments */
281 struct rmtcallres bres_pmap
; /* Remote results */
285 if (sys_auth
== NULL
) {
286 return (RPC_SYSTEMERROR
);
289 * initialization: create a fd, a broadcast address, and send the
290 * request on the broadcast transport.
291 * Listen on all of them and on replies, call the user supplied
296 nettype
= "datagram_n";
297 if ((handle
= __rpc_setconf(nettype
)) == NULL
) {
298 AUTH_DESTROY(sys_auth
);
299 return (RPC_UNKNOWNPROTO
);
301 while ((nconf
= __rpc_getconf(handle
)) != NULL
) {
303 struct __rpc_sockinfo si
;
305 if (nconf
->nc_semantics
!= NC_TPI_CLTS
)
307 if (fdlistno
>= MAXBCAST
)
308 break; /* No more slots available */
309 if (!__rpc_nconf2sockinfo(nconf
, &si
))
312 TAILQ_INIT(&fdlist
[fdlistno
].nal
);
313 if (__rpc_getbroadifs(si
.si_af
, si
.si_proto
, si
.si_socktype
,
314 &fdlist
[fdlistno
].nal
) == 0)
317 fd
= _socket(si
.si_af
, si
.si_socktype
, si
.si_proto
);
322 fdlist
[fdlistno
].af
= si
.si_af
;
323 fdlist
[fdlistno
].proto
= si
.si_proto
;
324 fdlist
[fdlistno
].fd
= fd
;
325 fdlist
[fdlistno
].nconf
= nconf
;
326 fdlist
[fdlistno
].asize
= __rpc_get_a_size(si
.si_af
);
327 pfd
[fdlistno
].events
= POLLIN
| POLLPRI
|
328 POLLRDNORM
| POLLRDBAND
;
329 pfd
[fdlistno
].fd
= fdlist
[fdlistno
].fd
= fd
;
330 fdlist
[fdlistno
].dsize
= __rpc_get_t_size(si
.si_af
, si
.si_proto
,
333 if (maxbufsize
<= fdlist
[fdlistno
].dsize
)
334 maxbufsize
= fdlist
[fdlistno
].dsize
;
337 if (si
.si_af
== AF_INET
&& si
.si_proto
== IPPROTO_UDP
) {
338 udpbufsz
= fdlist
[fdlistno
].dsize
;
339 if ((outbuf_pmap
= malloc(udpbufsz
)) == NULL
) {
341 stat
= RPC_SYSTEMERROR
;
351 if (stat
== RPC_SUCCESS
)
352 stat
= RPC_UNKNOWNPROTO
;
355 if (maxbufsize
== 0) {
356 if (stat
== RPC_SUCCESS
)
360 inbuf
= malloc(maxbufsize
);
361 outbuf
= malloc(maxbufsize
);
362 if ((inbuf
== NULL
) || (outbuf
== NULL
)) {
363 stat
= RPC_SYSTEMERROR
;
367 /* Serialize all the arguments which have to be sent */
368 gettimeofday(&t
, NULL
);
369 msg
.rm_xid
= __RPC_GETXID(&t
);
370 msg
.rm_direction
= CALL
;
371 msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
372 msg
.rm_call
.cb_prog
= RPCBPROG
;
373 msg
.rm_call
.cb_vers
= RPCBVERS
;
374 msg
.rm_call
.cb_proc
= RPCBPROC_CALLIT
;
378 barg
.args
.args_val
= argsp
;
379 barg
.xdr_args
= xargs
;
381 bres
.results
.results_val
= resultsp
;
382 bres
.xdr_res
= xresults
;
383 msg
.rm_call
.cb_cred
= sys_auth
->ah_cred
;
384 msg
.rm_call
.cb_verf
= sys_auth
->ah_verf
;
385 xdrmem_create(xdrs
, outbuf
, maxbufsize
, XDR_ENCODE
);
386 if ((!xdr_callmsg(xdrs
, &msg
)) ||
387 (!xdr_rpcb_rmtcallargs(xdrs
,
388 (struct rpcb_rmtcallargs
*)(void *)&barg
))) {
389 stat
= RPC_CANTENCODEARGS
;
392 outlen
= xdr_getpos(xdrs
);
396 /* Prepare the packet for version 2 PORTMAP */
398 msg
.rm_xid
++; /* One way to distinguish */
399 msg
.rm_call
.cb_prog
= PMAPPROG
;
400 msg
.rm_call
.cb_vers
= PMAPVERS
;
401 msg
.rm_call
.cb_proc
= PMAPPROC_CALLIT
;
402 barg_pmap
.prog
= prog
;
403 barg_pmap
.vers
= vers
;
404 barg_pmap
.proc
= proc
;
405 barg_pmap
.args_ptr
= argsp
;
406 barg_pmap
.xdr_args
= xargs
;
407 bres_pmap
.port_ptr
= &port
;
408 bres_pmap
.xdr_results
= xresults
;
409 bres_pmap
.results_ptr
= resultsp
;
410 xdrmem_create(xdrs
, outbuf_pmap
, udpbufsz
, XDR_ENCODE
);
411 if ((! xdr_callmsg(xdrs
, &msg
)) ||
412 (! xdr_rmtcall_args(xdrs
, &barg_pmap
))) {
413 stat
= RPC_CANTENCODEARGS
;
416 outlen_pmap
= xdr_getpos(xdrs
);
422 * Basic loop: broadcast the packets to transports which
423 * support data packets of size such that one can encode
425 * Wait a while for response(s).
426 * The response timeout grows larger per iteration.
428 for (msec
= inittime
; msec
<= waittime
; msec
+= msec
) {
431 /* Broadcast all the packets now */
432 for (i
= 0; i
< fdlistno
; i
++) {
433 if (fdlist
[i
].dsize
< outlen
) {
437 for (bip
= TAILQ_FIRST(&fdlist
[i
].nal
); bip
!= NULL
;
438 bip
= TAILQ_NEXT(bip
, link
)) {
441 addr
= &bip
->broadaddr
;
443 __rpc_broadenable(fdlist
[i
].af
, fdlist
[i
].fd
,
447 * Only use version 3 if lowvers is not set
451 if (_sendto(fdlist
[i
].fd
, outbuf
,
452 outlen
, 0, (struct sockaddr
*)addr
,
453 (size_t)fdlist
[i
].asize
) !=
458 warnx("clnt_bcast: cannot send"
465 fprintf(stderr
, "Broadcast packet sent "
467 fdlist
[i
].nconf
->nc_netid
);
471 * Send the version 2 packet also
475 fdlist
[i
].proto
== IPPROTO_UDP
) {
476 if (_sendto(fdlist
[i
].fd
, outbuf_pmap
,
477 outlen_pmap
, 0, addr
,
478 (size_t)fdlist
[i
].asize
) !=
481 "Cannot send broadcast packet");
487 fprintf(stderr
, "PMAP Broadcast packet "
489 fdlist
[i
].nconf
->nc_netid
);
493 /* End for sending all packets on this transport */
494 } /* End for sending on all transports */
496 if (eachresult
== NULL
) {
502 * Get all the replies from these broadcast requests
506 switch (pollretval
= _poll(pfd
, fdlistno
, msec
)) {
507 case 0: /* timed out */
510 case -1: /* some kind of error - we ignore it */
512 } /* end of poll results switch */
514 for (i
= fds_found
= 0;
515 i
< fdlistno
&& fds_found
< pollretval
; i
++) {
518 if (pfd
[i
].revents
== 0)
520 else if (pfd
[i
].revents
& POLLNVAL
) {
522 * Something bad has happened to this descri-
523 * ptor. We can cause _poll() to ignore
524 * it simply by using a negative fd. We do that
525 * rather than compacting the pfd[] and fdlist[]
534 fprintf(stderr
, "response for %s\n",
535 fdlist
[i
].nconf
->nc_netid
);
538 inlen
= _recvfrom(fdlist
[i
].fd
, inbuf
, fdlist
[i
].dsize
,
539 0, (struct sockaddr
*)(void *)&fdlist
[i
].raddr
,
544 warnx("clnt_bcast: Cannot receive reply to "
549 if (inlen
< sizeof (u_int32_t
))
550 continue; /* Drop that and go ahead */
552 * see if reply transaction id matches sent id.
553 * If so, decode the results. If return id is xid + 1
554 * it was a PORTMAP reply
556 if (*((u_int32_t
*)(void *)(inbuf
)) ==
557 *((u_int32_t
*)(void *)(outbuf
))) {
559 msg
.acpted_rply
.ar_verf
= _null_auth
;
560 msg
.acpted_rply
.ar_results
.where
=
561 (caddr_t
)(void *)&bres
;
562 msg
.acpted_rply
.ar_results
.proc
=
563 (xdrproc_t
)xdr_rpcb_rmtcallres
;
565 } else if (pmap_flag
&&
566 *((u_int32_t
*)(void *)(inbuf
)) ==
567 *((u_int32_t
*)(void *)(outbuf_pmap
))) {
569 msg
.acpted_rply
.ar_verf
= _null_auth
;
570 msg
.acpted_rply
.ar_results
.where
=
571 (caddr_t
)(void *)&bres_pmap
;
572 msg
.acpted_rply
.ar_results
.proc
=
573 (xdrproc_t
)xdr_rmtcallres
;
577 xdrmem_create(xdrs
, inbuf
, (u_int
)inlen
, XDR_DECODE
);
578 if (xdr_replymsg(xdrs
, &msg
)) {
579 if ((msg
.rm_reply
.rp_stat
== MSG_ACCEPTED
) &&
580 (msg
.acpted_rply
.ar_stat
== SUCCESS
)) {
581 struct netbuf taddr
, *np
;
582 struct sockaddr_in
*sin
;
585 if (pmap_flag
&& pmap_reply_flag
) {
586 sin
= (struct sockaddr_in
*)
587 (void *)&fdlist
[i
].raddr
;
589 htons((u_short
)port
);
590 taddr
.len
= taddr
.maxlen
=
591 fdlist
[i
].raddr
.ss_len
;
592 taddr
.buf
= &fdlist
[i
].raddr
;
593 done
= (*eachresult
)(resultsp
,
594 &taddr
, fdlist
[i
].nconf
);
598 fprintf(stderr
, "uaddr %s\n",
602 fdlist
[i
].nconf
, uaddrp
);
603 done
= (*eachresult
)(resultsp
,
604 np
, fdlist
[i
].nconf
);
610 /* otherwise, we just ignore the errors ... */
612 /* else some kind of deserialization problem ... */
614 xdrs
->x_op
= XDR_FREE
;
615 msg
.acpted_rply
.ar_results
.proc
= (xdrproc_t
) xdr_void
;
616 xdr_replymsg(xdrs
, &msg
);
617 (*xresults
)(xdrs
, resultsp
);
625 } /* The recv for loop */
626 } /* The giant for loop */
637 for (i
= 0; i
< fdlistno
; i
++) {
638 _close(fdlist
[i
].fd
);
639 __rpc_freebroadifs(&fdlist
[i
].nal
);
641 AUTH_DESTROY(sys_auth
);
642 __rpc_endconf(handle
);
649 rpc_broadcast(prog
, vers
, proc
, xargs
, argsp
, xresults
, resultsp
,
651 rpcprog_t prog
; /* program number */
652 rpcvers_t vers
; /* version number */
653 rpcproc_t proc
; /* procedure number */
654 xdrproc_t xargs
; /* xdr routine for args */
655 caddr_t argsp
; /* pointer to args */
656 xdrproc_t xresults
; /* xdr routine for results */
657 caddr_t resultsp
; /* pointer to results */
658 resultproc_t eachresult
; /* call with each result obtained */
659 const char *nettype
; /* transport type */
661 enum clnt_stat dummy
;
663 dummy
= rpc_broadcast_exp(prog
, vers
, proc
, xargs
, argsp
,
664 xresults
, resultsp
, eachresult
,
665 INITTIME
, WAITTIME
, nettype
);