1 /* $NetBSD: clnt_bcast.c,v 1.3 2000/07/06 03:05:20 christos Exp $ */
4 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34 #if defined(LIBC_SCCS) && !defined(lint)
35 #ident "@(#)clnt_bcast.c 1.18 94/05/03 SMI"
36 static char sccsid[] = "@(#)clnt_bcast.c 1.15 89/04/21 Copyr 1988 Sun Micro";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
44 * Client interface to broadcast service.
46 * Copyright (C) 1988, Sun Microsystems, Inc.
48 * The following is kludged-up support for simple rpc broadcasts.
49 * Someday a large, complicated system will replace these routines.
52 #include "namespace.h"
53 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <sys/queue.h>
57 #include <netinet/in.h>
62 #include <rpc/pmap_prot.h>
63 #include <rpc/pmap_clnt.h>
64 #include <rpc/pmap_rmt.h>
66 #include <rpc/nettype.h>
67 #include <arpa/inet.h>
77 #include "un-namespace.h"
81 #define MAXBCAST 20 /* Max no of broadcasting transports */
82 #define INITTIME 4000 /* Time to wait initially */
83 #define WAITTIME 8000 /* Maximum time to wait */
86 * If nettype is NULL, it broadcasts on all the available
87 * datagram_n transports. May potentially lead to broadacst storms
88 * and hence should be used with caution, care and courage.
90 * The current parameter xdr packet size is limited by the max tsdu
91 * size of the transport. If the max tsdu size of any transport is
92 * smaller than the parameter xdr packet, then broadcast is not
93 * sent on that transport.
95 * Also, the packet size should be less the packet size of
96 * the data link layer (for ethernet it is 1400 bytes). There is
97 * no easy way to find out the max size of the data link layer and
98 * we are assuming that the args would be smaller than that.
100 * The result size has to be smaller than the transport tsdu size.
102 * If PORTMAP has been defined, we send two packets for UDP, one for
103 * rpcbind and one for portmap. For those machines which support
104 * both rpcbind and portmap, it will cause them to reply twice, and
105 * also here it will get two responses ... inefficient and clumsy.
110 struct sockaddr_storage broadaddr;
111 TAILQ_ENTRY(broadif) link;
114 typedef TAILQ_HEAD(, broadif) broadlist_t;
116 int __rpc_getbroadifs(int, int, int, broadlist_t *);
117 void __rpc_freebroadifs(broadlist_t *);
118 int __rpc_broadenable(int, int, struct broadif *);
120 int __rpc_lowvers = 0;
123 __rpc_getbroadifs(int af, int proto, int socktype, broadlist_t *list)
127 struct ifaddrs *ifap, *ifp;
129 struct sockaddr_in6 *sin6;
131 struct sockaddr_in *sin;
132 struct addrinfo hints, *res;
134 if (getifaddrs(&ifp) < 0)
137 memset(&hints, 0, sizeof hints);
139 hints.ai_family = af;
140 hints.ai_protocol = proto;
141 hints.ai_socktype = socktype;
143 if (getaddrinfo(NULL, "sunrpc", &hints, &res) != 0) {
148 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
149 if (ifap->ifa_addr->sa_family != af ||
150 !(ifap->ifa_flags & IFF_UP))
152 bip = (struct broadif *)malloc(sizeof *bip);
155 bip->index = if_nametoindex(ifap->ifa_name);
160 (ifap->ifa_flags & IFF_BROADCAST) &&
161 ifap->ifa_broadaddr) {
162 memcpy(&bip->broadaddr, ifap->ifa_broadaddr,
163 (size_t)ifap->ifa_broadaddr->sa_len);
164 sin = (struct sockaddr_in *)(void *)&bip->broadaddr;
166 ((struct sockaddr_in *)
167 (void *)res->ai_addr)->sin_port;
170 if (af == AF_INET6 && (ifap->ifa_flags & IFF_MULTICAST)) {
171 sin6 = (struct sockaddr_in6 *)(void *)&bip->broadaddr;
172 inet_pton(af, RPCB_MULTICAST_ADDR, &sin6->sin6_addr);
173 sin6->sin6_family = af;
174 sin6->sin6_len = sizeof *sin6;
176 ((struct sockaddr_in6 *)
177 (void *)res->ai_addr)->sin6_port;
178 sin6->sin6_scope_id = bip->index;
185 TAILQ_INSERT_TAIL(list, bip, link);
195 __rpc_freebroadifs(broadlist_t *list)
197 struct broadif *bip, *next;
199 bip = TAILQ_FIRST(list);
201 while (bip != NULL) {
202 next = TAILQ_NEXT(bip, link);
210 __rpc_broadenable(int af, int s, struct broadif *bip)
215 if (af == AF_INET6) {
216 fprintf(stderr, "set v6 multicast if to %d\n", bip->index);
217 if (_setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, &bip->index,
218 sizeof bip->index) < 0)
222 if (_setsockopt(s, SOL_SOCKET, SO_BROADCAST, &o, sizeof o) < 0)
230 rpc_broadcast_exp(prog, vers, proc, xargs, argsp, xresults, resultsp,
231 eachresult, inittime, waittime, nettype)
232 rpcprog_t prog; /* program number */
233 rpcvers_t vers; /* version number */
234 rpcproc_t proc; /* procedure number */
235 xdrproc_t xargs; /* xdr routine for args */
236 caddr_t argsp; /* pointer to args */
237 xdrproc_t xresults; /* xdr routine for results */
238 caddr_t resultsp; /* pointer to results */
239 resultproc_t eachresult; /* call with each result obtained */
240 int inittime; /* how long to wait initially */
241 int waittime; /* maximum time to wait */
242 const char *nettype; /* transport type */
244 enum clnt_stat stat = RPC_SUCCESS; /* Return status */
245 XDR xdr_stream; /* XDR stream */
246 XDR *xdrs = &xdr_stream;
247 struct rpc_msg msg; /* RPC message */
249 char *outbuf = NULL; /* Broadcast msg buffer */
250 char *inbuf = NULL; /* Reply buf */
252 u_int maxbufsize = 0;
253 AUTH *sys_auth = authunix_create_default();
256 char uaddress[1024]; /* A self imposed limit */
257 char *uaddrp = uaddress;
258 int pmap_reply_flag; /* reply recvd from PORTMAP */
259 /* An array of all the suitable broadcast transports */
261 int fd; /* File descriptor */
264 struct netconfig *nconf; /* Netconfig structure */
265 u_int asize; /* Size of the addr buf */
266 u_int dsize; /* Size of the data buf */
267 struct sockaddr_storage raddr; /* Remote address */
270 struct pollfd pfd[MAXBCAST];
272 struct r_rpcb_rmtcallargs barg; /* Remote arguments */
273 struct r_rpcb_rmtcallres bres; /* Remote results */
275 struct netconfig *nconf;
281 size_t outlen_pmap = 0;
282 u_long port; /* Remote port number */
283 int pmap_flag = 0; /* UDP exists ? */
284 char *outbuf_pmap = NULL;
285 struct rmtcallargs barg_pmap; /* Remote arguments */
286 struct rmtcallres bres_pmap; /* Remote results */
290 if (sys_auth == NULL) {
291 return (RPC_SYSTEMERROR);
294 * initialization: create a fd, a broadcast address, and send the
295 * request on the broadcast transport.
296 * Listen on all of them and on replies, call the user supplied
301 nettype = "datagram_n";
302 if ((handle = __rpc_setconf(nettype)) == NULL) {
303 AUTH_DESTROY(sys_auth);
304 return (RPC_UNKNOWNPROTO);
306 while ((nconf = __rpc_getconf(handle)) != NULL) {
308 struct __rpc_sockinfo si;
310 if (nconf->nc_semantics != NC_TPI_CLTS)
312 if (fdlistno >= MAXBCAST)
313 break; /* No more slots available */
314 if (!__rpc_nconf2sockinfo(nconf, &si))
317 TAILQ_INIT(&fdlist[fdlistno].nal);
318 if (__rpc_getbroadifs(si.si_af, si.si_proto, si.si_socktype,
319 &fdlist[fdlistno].nal) == 0)
322 fd = _socket(si.si_af, si.si_socktype, si.si_proto);
327 fdlist[fdlistno].af = si.si_af;
328 fdlist[fdlistno].proto = si.si_proto;
329 fdlist[fdlistno].fd = fd;
330 fdlist[fdlistno].nconf = nconf;
331 fdlist[fdlistno].asize = __rpc_get_a_size(si.si_af);
332 pfd[fdlistno].events = POLLIN | POLLPRI |
333 POLLRDNORM | POLLRDBAND;
334 pfd[fdlistno].fd = fdlist[fdlistno].fd = fd;
335 fdlist[fdlistno].dsize = __rpc_get_t_size(si.si_af, si.si_proto,
338 if (maxbufsize <= fdlist[fdlistno].dsize)
339 maxbufsize = fdlist[fdlistno].dsize;
342 if (si.si_af == AF_INET && si.si_proto == IPPROTO_UDP) {
343 udpbufsz = fdlist[fdlistno].dsize;
344 outbuf_pmap = reallocf(outbuf_pmap, udpbufsz);
345 if (outbuf_pmap == NULL) {
347 stat = RPC_SYSTEMERROR;
357 if (stat == RPC_SUCCESS)
358 stat = RPC_UNKNOWNPROTO;
361 if (maxbufsize == 0) {
362 if (stat == RPC_SUCCESS)
366 inbuf = malloc(maxbufsize);
367 outbuf = malloc(maxbufsize);
368 if ((inbuf == NULL) || (outbuf == NULL)) {
369 stat = RPC_SYSTEMERROR;
373 /* Serialize all the arguments which have to be sent */
374 (void) gettimeofday(&t, NULL);
375 msg.rm_xid = __RPC_GETXID(&t);
376 msg.rm_direction = CALL;
377 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
378 msg.rm_call.cb_prog = RPCBPROG;
379 msg.rm_call.cb_vers = RPCBVERS;
380 msg.rm_call.cb_proc = RPCBPROC_CALLIT;
384 barg.args.args_val = argsp;
385 barg.xdr_args = xargs;
387 bres.results.results_val = resultsp;
388 bres.xdr_res = xresults;
389 msg.rm_call.cb_cred = sys_auth->ah_cred;
390 msg.rm_call.cb_verf = sys_auth->ah_verf;
391 xdrmem_create(xdrs, outbuf, maxbufsize, XDR_ENCODE);
392 if ((!xdr_callmsg(xdrs, &msg)) ||
393 (!xdr_rpcb_rmtcallargs(xdrs,
394 (struct rpcb_rmtcallargs *)(void *)&barg))) {
395 stat = RPC_CANTENCODEARGS;
398 outlen = xdr_getpos(xdrs);
402 /* Prepare the packet for version 2 PORTMAP */
404 msg.rm_xid++; /* One way to distinguish */
405 msg.rm_call.cb_prog = PMAPPROG;
406 msg.rm_call.cb_vers = PMAPVERS;
407 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
408 barg_pmap.prog = prog;
409 barg_pmap.vers = vers;
410 barg_pmap.proc = proc;
411 barg_pmap.args_ptr = argsp;
412 barg_pmap.xdr_args = xargs;
413 bres_pmap.port_ptr = &port;
414 bres_pmap.xdr_results = xresults;
415 bres_pmap.results_ptr = resultsp;
416 xdrmem_create(xdrs, outbuf_pmap, udpbufsz, XDR_ENCODE);
417 if ((! xdr_callmsg(xdrs, &msg)) ||
418 (! xdr_rmtcall_args(xdrs, &barg_pmap))) {
419 stat = RPC_CANTENCODEARGS;
422 outlen_pmap = xdr_getpos(xdrs);
428 * Basic loop: broadcast the packets to transports which
429 * support data packets of size such that one can encode
431 * Wait a while for response(s).
432 * The response timeout grows larger per iteration.
434 for (msec = inittime; msec <= waittime; msec += msec) {
437 /* Broadcast all the packets now */
438 for (i = 0; i < fdlistno; i++) {
439 if (fdlist[i].dsize < outlen) {
443 for (bip = TAILQ_FIRST(&fdlist[i].nal); bip != NULL;
444 bip = TAILQ_NEXT(bip, link)) {
447 addr = &bip->broadaddr;
449 __rpc_broadenable(fdlist[i].af, fdlist[i].fd,
453 * Only use version 3 if lowvers is not set
457 if (_sendto(fdlist[i].fd, outbuf,
458 outlen, 0, (struct sockaddr*)addr,
459 (size_t)fdlist[i].asize) !=
464 warnx("clnt_bcast: cannot send "
471 fprintf(stderr, "Broadcast packet sent "
473 fdlist[i].nconf->nc_netid);
477 * Send the version 2 packet also
481 fdlist[i].proto == IPPROTO_UDP) {
482 if (_sendto(fdlist[i].fd, outbuf_pmap,
483 outlen_pmap, 0, addr,
484 (size_t)fdlist[i].asize) !=
487 "Cannot send broadcast packet");
493 fprintf(stderr, "PMAP Broadcast packet "
495 fdlist[i].nconf->nc_netid);
499 /* End for sending all packets on this transport */
500 } /* End for sending on all transports */
502 if (eachresult == NULL) {
508 * Get all the replies from these broadcast requests
512 switch (pollretval = _poll(pfd, fdlistno, msec)) {
513 case 0: /* timed out */
516 case -1: /* some kind of error - we ignore it */
518 } /* end of poll results switch */
520 for (i = fds_found = 0;
521 i < fdlistno && fds_found < pollretval; i++) {
524 if (pfd[i].revents == 0)
526 else if (pfd[i].revents & POLLNVAL) {
528 * Something bad has happened to this descri-
529 * ptor. We can cause _poll() to ignore
530 * it simply by using a negative fd. We do that
531 * rather than compacting the pfd[] and fdlist[]
540 fprintf(stderr, "response for %s\n",
541 fdlist[i].nconf->nc_netid);
544 inlen = _recvfrom(fdlist[i].fd, inbuf, fdlist[i].dsize,
545 0, (struct sockaddr *)(void *)&fdlist[i].raddr,
550 warnx("clnt_bcast: Cannot receive reply to "
555 if (inlen < sizeof (u_int32_t))
556 continue; /* Drop that and go ahead */
558 * see if reply transaction id matches sent id.
559 * If so, decode the results. If return id is xid + 1
560 * it was a PORTMAP reply
562 if (*((u_int32_t *)(void *)(inbuf)) ==
563 *((u_int32_t *)(void *)(outbuf))) {
565 msg.acpted_rply.ar_verf = _null_auth;
566 msg.acpted_rply.ar_results.where =
567 (caddr_t)(void *)&bres;
568 msg.acpted_rply.ar_results.proc =
569 (xdrproc_t)xdr_rpcb_rmtcallres;
571 } else if (pmap_flag &&
572 *((u_int32_t *)(void *)(inbuf)) ==
573 *((u_int32_t *)(void *)(outbuf_pmap))) {
575 msg.acpted_rply.ar_verf = _null_auth;
576 msg.acpted_rply.ar_results.where =
577 (caddr_t)(void *)&bres_pmap;
578 msg.acpted_rply.ar_results.proc =
579 (xdrproc_t)xdr_rmtcallres;
583 xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
584 if (xdr_replymsg(xdrs, &msg)) {
585 if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
586 (msg.acpted_rply.ar_stat == SUCCESS)) {
587 struct netbuf taddr, *np;
588 struct sockaddr_in *sin;
591 if (pmap_flag && pmap_reply_flag) {
592 sin = (struct sockaddr_in *)
593 (void *)&fdlist[i].raddr;
595 htons((u_short)port);
596 taddr.len = taddr.maxlen =
597 fdlist[i].raddr.ss_len;
598 taddr.buf = &fdlist[i].raddr;
599 done = (*eachresult)(resultsp,
600 &taddr, fdlist[i].nconf);
604 fprintf(stderr, "uaddr %s\n",
608 fdlist[i].nconf, uaddrp);
609 done = (*eachresult)(resultsp,
610 np, fdlist[i].nconf);
616 /* otherwise, we just ignore the errors ... */
618 /* else some kind of deserialization problem ... */
620 xdrs->x_op = XDR_FREE;
621 msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
622 (void) xdr_replymsg(xdrs, &msg);
623 (void) (*xresults)(xdrs, resultsp);
631 } /* The recv for loop */
632 } /* The giant for loop */
640 for (i = 0; i < fdlistno; i++) {
641 (void)_close(fdlist[i].fd);
642 __rpc_freebroadifs(&fdlist[i].nal);
644 AUTH_DESTROY(sys_auth);
645 (void) __rpc_endconf(handle);
652 rpc_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp,
654 rpcprog_t prog; /* program number */
655 rpcvers_t vers; /* version number */
656 rpcproc_t proc; /* procedure number */
657 xdrproc_t xargs; /* xdr routine for args */
658 caddr_t argsp; /* pointer to args */
659 xdrproc_t xresults; /* xdr routine for results */
660 caddr_t resultsp; /* pointer to results */
661 resultproc_t eachresult; /* call with each result obtained */
662 const char *nettype; /* transport type */
664 enum clnt_stat dummy;
666 dummy = rpc_broadcast_exp(prog, vers, proc, xargs, argsp,
667 xresults, resultsp, eachresult,
668 INITTIME, WAITTIME, nettype);