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 $
35 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
40 * Client interface to broadcast service.
42 * Copyright (C) 1988, Sun Microsystems, Inc.
44 * The following is kludged-up support for simple rpc broadcasts.
45 * Someday a large, complicated system will replace these routines.
48 #include "namespace.h"
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <sys/queue.h>
53 #include <netinet/in.h>
58 #include <rpc/pmap_prot.h>
59 #include <rpc/pmap_clnt.h>
60 #include <rpc/pmap_rmt.h>
62 #include <rpc/nettype.h>
63 #include <arpa/inet.h>
73 #include "un-namespace.h"
77 #define MAXBCAST 20 /* Max no of broadcasting transports */
78 #define INITTIME 4000 /* Time to wait initially */
79 #define WAITTIME 8000 /* Maximum time to wait */
82 * If nettype is NULL, it broadcasts on all the available
83 * datagram_n transports. May potentially lead to broadacst storms
84 * and hence should be used with caution, care and courage.
86 * The current parameter xdr packet size is limited by the max tsdu
87 * size of the transport. If the max tsdu size of any transport is
88 * smaller than the parameter xdr packet, then broadcast is not
89 * sent on that transport.
91 * Also, the packet size should be less the packet size of
92 * the data link layer (for ethernet it is 1400 bytes). There is
93 * no easy way to find out the max size of the data link layer and
94 * we are assuming that the args would be smaller than that.
96 * The result size has to be smaller than the transport tsdu size.
98 * If PORTMAP has been defined, we send two packets for UDP, one for
99 * rpcbind and one for portmap. For those machines which support
100 * both rpcbind and portmap, it will cause them to reply twice, and
101 * also here it will get two responses ... inefficient and clumsy.
106 struct sockaddr_storage broadaddr
;
107 TAILQ_ENTRY(broadif
) link
;
110 typedef TAILQ_HEAD(, broadif
) broadlist_t
;
112 int __rpc_broadenable(int, int, struct broadif
*);
113 void __rpc_freebroadifs(broadlist_t
*);
114 int __rpc_getbroadifs(int, int, int, broadlist_t
*);
116 int __rpc_lowvers
= 0;
119 __rpc_getbroadifs(int af
, int proto
, int socktype
, broadlist_t
*list
)
123 struct ifaddrs
*ifap
, *ifp
;
125 struct sockaddr_in6
*sin6
;
127 struct sockaddr_in
*sin
;
128 struct addrinfo hints
, *res
;
130 if (getifaddrs(&ifp
) < 0)
133 memset(&hints
, 0, sizeof hints
);
135 hints
.ai_family
= af
;
136 hints
.ai_protocol
= proto
;
137 hints
.ai_socktype
= socktype
;
139 if (getaddrinfo(NULL
, "sunrpc", &hints
, &res
) != 0) {
144 for (ifap
= ifp
; ifap
!= NULL
; ifap
= ifap
->ifa_next
) {
145 if (ifap
->ifa_addr
->sa_family
!= af
||
146 !(ifap
->ifa_flags
& IFF_UP
))
148 bip
= (struct broadif
*)malloc(sizeof *bip
);
151 bip
->index
= if_nametoindex(ifap
->ifa_name
);
156 (ifap
->ifa_flags
& IFF_BROADCAST
) &&
157 ifap
->ifa_broadaddr
) {
158 memcpy(&bip
->broadaddr
, ifap
->ifa_broadaddr
,
159 (size_t)ifap
->ifa_broadaddr
->sa_len
);
160 sin
= (struct sockaddr_in
*)(void *)&bip
->broadaddr
;
162 ((struct sockaddr_in
*)
163 (void *)res
->ai_addr
)->sin_port
;
166 if (af
== AF_INET6
&& (ifap
->ifa_flags
& IFF_MULTICAST
)) {
167 sin6
= (struct sockaddr_in6
*)(void *)&bip
->broadaddr
;
168 inet_pton(af
, RPCB_MULTICAST_ADDR
, &sin6
->sin6_addr
);
169 sin6
->sin6_family
= af
;
170 sin6
->sin6_len
= sizeof *sin6
;
172 ((struct sockaddr_in6
*)
173 (void *)res
->ai_addr
)->sin6_port
;
174 sin6
->sin6_scope_id
= bip
->index
;
181 TAILQ_INSERT_TAIL(list
, bip
, link
);
191 __rpc_freebroadifs(broadlist_t
*list
)
193 struct broadif
*bip
, *next
;
195 bip
= TAILQ_FIRST(list
);
197 while (bip
!= NULL
) {
198 next
= TAILQ_NEXT(bip
, link
);
206 __rpc_broadenable(int af
, int s
, struct broadif
*bip
)
211 if (af
== AF_INET6
) {
212 fprintf(stderr
, "set v6 multicast if to %d\n", bip
->index
);
213 if (_setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_IF
, &bip
->index
,
214 sizeof bip
->index
) < 0)
218 if (_setsockopt(s
, SOL_SOCKET
, SO_BROADCAST
, &o
, sizeof o
) < 0)
226 rpc_broadcast_exp(prog
, vers
, proc
, xargs
, argsp
, xresults
, resultsp
,
227 eachresult
, inittime
, waittime
, nettype
)
228 rpcprog_t prog
; /* program number */
229 rpcvers_t vers
; /* version number */
230 rpcproc_t proc
; /* procedure number */
231 xdrproc_t xargs
; /* xdr routine for args */
232 caddr_t argsp
; /* pointer to args */
233 xdrproc_t xresults
; /* xdr routine for results */
234 caddr_t resultsp
; /* pointer to results */
235 resultproc_t eachresult
; /* call with each result obtained */
236 int inittime
; /* how long to wait initially */
237 int waittime
; /* maximum time to wait */
238 const char *nettype
; /* transport type */
240 enum clnt_stat stat
= RPC_SUCCESS
; /* Return status */
241 XDR xdr_stream
; /* XDR stream */
242 XDR
*xdrs
= &xdr_stream
;
243 struct rpc_msg msg
; /* RPC message */
245 char *outbuf
= NULL
; /* Broadcast msg buffer */
246 char *inbuf
= NULL
; /* Reply buf */
248 u_int maxbufsize
= 0;
249 AUTH
*sys_auth
= authunix_create_default();
252 char uaddress
[1024]; /* A self imposed limit */
253 char *uaddrp
= uaddress
;
254 int pmap_reply_flag
; /* reply recvd from PORTMAP */
255 /* An array of all the suitable broadcast transports */
257 int fd
; /* File descriptor */
260 struct netconfig
*nconf
; /* Netconfig structure */
261 u_int asize
; /* Size of the addr buf */
262 u_int dsize
; /* Size of the data buf */
263 struct sockaddr_storage raddr
; /* Remote address */
266 struct pollfd pfd
[MAXBCAST
];
268 struct r_rpcb_rmtcallargs barg
; /* Remote arguments */
269 struct r_rpcb_rmtcallres bres
; /* Remote results */
271 struct netconfig
*nconf
;
277 size_t outlen_pmap
= 0;
278 u_long port
; /* Remote port number */
279 int pmap_flag
= 0; /* UDP exists ? */
280 char *outbuf_pmap
= NULL
;
281 struct rmtcallargs barg_pmap
; /* Remote arguments */
282 struct rmtcallres bres_pmap
; /* Remote results */
286 if (sys_auth
== NULL
) {
287 return (RPC_SYSTEMERROR
);
290 * initialization: create a fd, a broadcast address, and send the
291 * request on the broadcast transport.
292 * Listen on all of them and on replies, call the user supplied
297 nettype
= "datagram_n";
298 if ((handle
= __rpc_setconf(nettype
)) == NULL
) {
299 AUTH_DESTROY(sys_auth
);
300 return (RPC_UNKNOWNPROTO
);
302 while ((nconf
= __rpc_getconf(handle
)) != NULL
) {
304 struct __rpc_sockinfo si
;
306 if (nconf
->nc_semantics
!= NC_TPI_CLTS
)
308 if (fdlistno
>= MAXBCAST
)
309 break; /* No more slots available */
310 if (!__rpc_nconf2sockinfo(nconf
, &si
))
313 TAILQ_INIT(&fdlist
[fdlistno
].nal
);
314 if (__rpc_getbroadifs(si
.si_af
, si
.si_proto
, si
.si_socktype
,
315 &fdlist
[fdlistno
].nal
) == 0)
318 fd
= _socket(si
.si_af
, si
.si_socktype
, si
.si_proto
);
323 fdlist
[fdlistno
].af
= si
.si_af
;
324 fdlist
[fdlistno
].proto
= si
.si_proto
;
325 fdlist
[fdlistno
].fd
= fd
;
326 fdlist
[fdlistno
].nconf
= nconf
;
327 fdlist
[fdlistno
].asize
= __rpc_get_a_size(si
.si_af
);
328 pfd
[fdlistno
].events
= POLLIN
| POLLPRI
|
329 POLLRDNORM
| POLLRDBAND
;
330 pfd
[fdlistno
].fd
= fdlist
[fdlistno
].fd
= fd
;
331 fdlist
[fdlistno
].dsize
= __rpc_get_t_size(si
.si_af
, si
.si_proto
,
334 if (maxbufsize
<= fdlist
[fdlistno
].dsize
)
335 maxbufsize
= fdlist
[fdlistno
].dsize
;
338 if (si
.si_af
== AF_INET
&& si
.si_proto
== IPPROTO_UDP
) {
339 udpbufsz
= fdlist
[fdlistno
].dsize
;
340 if ((outbuf_pmap
= malloc(udpbufsz
)) == NULL
) {
342 stat
= RPC_SYSTEMERROR
;
352 if (stat
== RPC_SUCCESS
)
353 stat
= RPC_UNKNOWNPROTO
;
356 if (maxbufsize
== 0) {
357 if (stat
== RPC_SUCCESS
)
361 inbuf
= malloc(maxbufsize
);
362 outbuf
= malloc(maxbufsize
);
363 if ((inbuf
== NULL
) || (outbuf
== NULL
)) {
364 stat
= RPC_SYSTEMERROR
;
368 /* Serialize all the arguments which have to be sent */
369 gettimeofday(&t
, NULL
);
370 msg
.rm_xid
= __RPC_GETXID(&t
);
371 msg
.rm_direction
= CALL
;
372 msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
373 msg
.rm_call
.cb_prog
= RPCBPROG
;
374 msg
.rm_call
.cb_vers
= RPCBVERS
;
375 msg
.rm_call
.cb_proc
= RPCBPROC_CALLIT
;
379 barg
.args
.args_val
= argsp
;
380 barg
.xdr_args
= xargs
;
382 bres
.results
.results_val
= resultsp
;
383 bres
.xdr_res
= xresults
;
384 msg
.rm_call
.cb_cred
= sys_auth
->ah_cred
;
385 msg
.rm_call
.cb_verf
= sys_auth
->ah_verf
;
386 xdrmem_create(xdrs
, outbuf
, maxbufsize
, XDR_ENCODE
);
387 if ((!xdr_callmsg(xdrs
, &msg
)) ||
388 (!xdr_rpcb_rmtcallargs(xdrs
,
389 (struct rpcb_rmtcallargs
*)(void *)&barg
))) {
390 stat
= RPC_CANTENCODEARGS
;
393 outlen
= xdr_getpos(xdrs
);
397 /* Prepare the packet for version 2 PORTMAP */
399 msg
.rm_xid
++; /* One way to distinguish */
400 msg
.rm_call
.cb_prog
= PMAPPROG
;
401 msg
.rm_call
.cb_vers
= PMAPVERS
;
402 msg
.rm_call
.cb_proc
= PMAPPROC_CALLIT
;
403 barg_pmap
.prog
= prog
;
404 barg_pmap
.vers
= vers
;
405 barg_pmap
.proc
= proc
;
406 barg_pmap
.args_ptr
= argsp
;
407 barg_pmap
.xdr_args
= xargs
;
408 bres_pmap
.port_ptr
= &port
;
409 bres_pmap
.xdr_results
= xresults
;
410 bres_pmap
.results_ptr
= resultsp
;
411 xdrmem_create(xdrs
, outbuf_pmap
, udpbufsz
, XDR_ENCODE
);
412 if ((! xdr_callmsg(xdrs
, &msg
)) ||
413 (! xdr_rmtcall_args(xdrs
, &barg_pmap
))) {
414 stat
= RPC_CANTENCODEARGS
;
417 outlen_pmap
= xdr_getpos(xdrs
);
423 * Basic loop: broadcast the packets to transports which
424 * support data packets of size such that one can encode
426 * Wait a while for response(s).
427 * The response timeout grows larger per iteration.
429 for (msec
= inittime
; msec
<= waittime
; msec
+= msec
) {
432 /* Broadcast all the packets now */
433 for (i
= 0; i
< fdlistno
; i
++) {
434 if (fdlist
[i
].dsize
< outlen
) {
438 for (bip
= TAILQ_FIRST(&fdlist
[i
].nal
); bip
!= NULL
;
439 bip
= TAILQ_NEXT(bip
, link
)) {
442 addr
= &bip
->broadaddr
;
444 __rpc_broadenable(fdlist
[i
].af
, fdlist
[i
].fd
,
448 * Only use version 3 if lowvers is not set
452 if (_sendto(fdlist
[i
].fd
, outbuf
,
453 outlen
, 0, (struct sockaddr
*)addr
,
454 (size_t)fdlist
[i
].asize
) !=
459 warnx("clnt_bcast: cannot send"
466 fprintf(stderr
, "Broadcast packet sent "
468 fdlist
[i
].nconf
->nc_netid
);
472 * Send the version 2 packet also
476 fdlist
[i
].proto
== IPPROTO_UDP
) {
477 if (_sendto(fdlist
[i
].fd
, outbuf_pmap
,
478 outlen_pmap
, 0, addr
,
479 (size_t)fdlist
[i
].asize
) !=
482 "Cannot send broadcast packet");
488 fprintf(stderr
, "PMAP Broadcast packet "
490 fdlist
[i
].nconf
->nc_netid
);
494 /* End for sending all packets on this transport */
495 } /* End for sending on all transports */
497 if (eachresult
== NULL
) {
503 * Get all the replies from these broadcast requests
507 switch (pollretval
= _poll(pfd
, fdlistno
, msec
)) {
508 case 0: /* timed out */
511 case -1: /* some kind of error - we ignore it */
513 } /* end of poll results switch */
515 for (i
= fds_found
= 0;
516 i
< fdlistno
&& fds_found
< pollretval
; i
++) {
519 if (pfd
[i
].revents
== 0)
521 else if (pfd
[i
].revents
& POLLNVAL
) {
523 * Something bad has happened to this descri-
524 * ptor. We can cause _poll() to ignore
525 * it simply by using a negative fd. We do that
526 * rather than compacting the pfd[] and fdlist[]
535 fprintf(stderr
, "response for %s\n",
536 fdlist
[i
].nconf
->nc_netid
);
539 inlen
= _recvfrom(fdlist
[i
].fd
, inbuf
, fdlist
[i
].dsize
,
540 0, (struct sockaddr
*)(void *)&fdlist
[i
].raddr
,
545 warnx("clnt_bcast: Cannot receive reply to "
550 if (inlen
< sizeof (u_int32_t
))
551 continue; /* Drop that and go ahead */
553 * see if reply transaction id matches sent id.
554 * If so, decode the results. If return id is xid + 1
555 * it was a PORTMAP reply
557 if (*((u_int32_t
*)(void *)(inbuf
)) ==
558 *((u_int32_t
*)(void *)(outbuf
))) {
560 msg
.acpted_rply
.ar_verf
= _null_auth
;
561 msg
.acpted_rply
.ar_results
.where
=
562 (caddr_t
)(void *)&bres
;
563 msg
.acpted_rply
.ar_results
.proc
=
564 (xdrproc_t
)xdr_rpcb_rmtcallres
;
566 } else if (pmap_flag
&&
567 *((u_int32_t
*)(void *)(inbuf
)) ==
568 *((u_int32_t
*)(void *)(outbuf_pmap
))) {
570 msg
.acpted_rply
.ar_verf
= _null_auth
;
571 msg
.acpted_rply
.ar_results
.where
=
572 (caddr_t
)(void *)&bres_pmap
;
573 msg
.acpted_rply
.ar_results
.proc
=
574 (xdrproc_t
)xdr_rmtcallres
;
578 xdrmem_create(xdrs
, inbuf
, (u_int
)inlen
, XDR_DECODE
);
579 if (xdr_replymsg(xdrs
, &msg
)) {
580 if ((msg
.rm_reply
.rp_stat
== MSG_ACCEPTED
) &&
581 (msg
.acpted_rply
.ar_stat
== SUCCESS
)) {
582 struct netbuf taddr
, *np
;
583 struct sockaddr_in
*sin
;
586 if (pmap_flag
&& pmap_reply_flag
) {
587 sin
= (struct sockaddr_in
*)
588 (void *)&fdlist
[i
].raddr
;
590 htons((u_short
)port
);
591 taddr
.len
= taddr
.maxlen
=
592 fdlist
[i
].raddr
.ss_len
;
593 taddr
.buf
= &fdlist
[i
].raddr
;
594 done
= (*eachresult
)(resultsp
,
595 &taddr
, fdlist
[i
].nconf
);
599 fprintf(stderr
, "uaddr %s\n",
603 fdlist
[i
].nconf
, uaddrp
);
604 done
= (*eachresult
)(resultsp
,
605 np
, fdlist
[i
].nconf
);
611 /* otherwise, we just ignore the errors ... */
613 /* else some kind of deserialization problem ... */
615 xdrs
->x_op
= XDR_FREE
;
616 msg
.acpted_rply
.ar_results
.proc
= (xdrproc_t
) xdr_void
;
617 xdr_replymsg(xdrs
, &msg
);
618 (*xresults
)(xdrs
, resultsp
);
626 } /* The recv for loop */
627 } /* The giant for loop */
638 for (i
= 0; i
< fdlistno
; i
++) {
639 _close(fdlist
[i
].fd
);
640 __rpc_freebroadifs(&fdlist
[i
].nal
);
642 AUTH_DESTROY(sys_auth
);
643 __rpc_endconf(handle
);
650 rpc_broadcast(prog
, vers
, proc
, xargs
, argsp
, xresults
, resultsp
,
652 rpcprog_t prog
; /* program number */
653 rpcvers_t vers
; /* version number */
654 rpcproc_t proc
; /* procedure number */
655 xdrproc_t xargs
; /* xdr routine for args */
656 caddr_t argsp
; /* pointer to args */
657 xdrproc_t xresults
; /* xdr routine for results */
658 caddr_t resultsp
; /* pointer to results */
659 resultproc_t eachresult
; /* call with each result obtained */
660 const char *nettype
; /* transport type */
662 enum clnt_stat dummy
;
664 dummy
= rpc_broadcast_exp(prog
, vers
, proc
, xargs
, argsp
,
665 xresults
, resultsp
, eachresult
,
666 INITTIME
, WAITTIME
, nettype
);