1 /* $NetBSD: clnt_bcast.c,v 1.3 2000/07/06 03:05:20 christos 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 #if defined(LIBC_SCCS) && !defined(lint)
36 #ident "@(#)clnt_bcast.c 1.18 94/05/03 SMI"
37 static char sccsid[] = "@(#)clnt_bcast.c 1.15 89/04/21 Copyr 1988 Sun Micro";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
45 * Client interface to broadcast service.
47 * Copyright (C) 1988, Sun Microsystems, Inc.
49 * The following is kludged-up support for simple rpc broadcasts.
50 * Someday a large, complicated system will replace these routines.
53 #include "namespace.h"
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <sys/queue.h>
58 #include <netinet/in.h>
63 #include <rpc/pmap_prot.h>
64 #include <rpc/pmap_clnt.h>
65 #include <rpc/pmap_rmt.h>
67 #include <rpc/nettype.h>
68 #include <arpa/inet.h>
78 #include "un-namespace.h"
82 #define MAXBCAST 20 /* Max no of broadcasting transports */
83 #define INITTIME 4000 /* Time to wait initially */
84 #define WAITTIME 8000 /* Maximum time to wait */
87 * If nettype is NULL, it broadcasts on all the available
88 * datagram_n transports. May potentially lead to broadacst storms
89 * and hence should be used with caution, care and courage.
91 * The current parameter xdr packet size is limited by the max tsdu
92 * size of the transport. If the max tsdu size of any transport is
93 * smaller than the parameter xdr packet, then broadcast is not
94 * sent on that transport.
96 * Also, the packet size should be less the packet size of
97 * the data link layer (for ethernet it is 1400 bytes). There is
98 * no easy way to find out the max size of the data link layer and
99 * we are assuming that the args would be smaller than that.
101 * The result size has to be smaller than the transport tsdu size.
103 * If PORTMAP has been defined, we send two packets for UDP, one for
104 * rpcbind and one for portmap. For those machines which support
105 * both rpcbind and portmap, it will cause them to reply twice, and
106 * also here it will get two responses ... inefficient and clumsy.
111 struct sockaddr_storage broadaddr;
112 TAILQ_ENTRY(broadif) link;
115 typedef TAILQ_HEAD(, broadif) broadlist_t;
117 int __rpc_getbroadifs(int, int, int, broadlist_t *);
118 void __rpc_freebroadifs(broadlist_t *);
119 int __rpc_broadenable(int, int, struct broadif *);
121 int __rpc_lowvers = 0;
124 __rpc_getbroadifs(int af, int proto, int socktype, broadlist_t *list)
128 struct ifaddrs *ifap, *ifp;
130 struct sockaddr_in6 *sin6;
132 struct sockaddr_in *sin;
133 struct addrinfo hints, *res;
135 if (getifaddrs(&ifp) < 0)
138 memset(&hints, 0, sizeof hints);
140 hints.ai_family = af;
141 hints.ai_protocol = proto;
142 hints.ai_socktype = socktype;
144 if (getaddrinfo(NULL, "sunrpc", &hints, &res) != 0) {
149 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
150 if (ifap->ifa_addr->sa_family != af ||
151 !(ifap->ifa_flags & IFF_UP))
153 bip = (struct broadif *)malloc(sizeof *bip);
156 bip->index = if_nametoindex(ifap->ifa_name);
161 (ifap->ifa_flags & IFF_BROADCAST) &&
162 ifap->ifa_broadaddr) {
163 memcpy(&bip->broadaddr, ifap->ifa_broadaddr,
164 (size_t)ifap->ifa_broadaddr->sa_len);
165 sin = (struct sockaddr_in *)(void *)&bip->broadaddr;
167 ((struct sockaddr_in *)
168 (void *)res->ai_addr)->sin_port;
171 if (af == AF_INET6 && (ifap->ifa_flags & IFF_MULTICAST)) {
172 sin6 = (struct sockaddr_in6 *)(void *)&bip->broadaddr;
173 inet_pton(af, RPCB_MULTICAST_ADDR, &sin6->sin6_addr);
174 sin6->sin6_family = af;
175 sin6->sin6_len = sizeof *sin6;
177 ((struct sockaddr_in6 *)
178 (void *)res->ai_addr)->sin6_port;
179 sin6->sin6_scope_id = bip->index;
186 TAILQ_INSERT_TAIL(list, bip, link);
196 __rpc_freebroadifs(broadlist_t *list)
198 struct broadif *bip, *next;
200 bip = TAILQ_FIRST(list);
202 while (bip != NULL) {
203 next = TAILQ_NEXT(bip, link);
211 __rpc_broadenable(int af, int s, struct broadif *bip)
216 if (af == AF_INET6) {
217 fprintf(stderr, "set v6 multicast if to %d\n", bip->index);
218 if (_setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, &bip->index,
219 sizeof bip->index) < 0)
223 if (_setsockopt(s, SOL_SOCKET, SO_BROADCAST, &o, sizeof o) < 0)
231 rpc_broadcast_exp(prog, vers, proc, xargs, argsp, xresults, resultsp,
232 eachresult, inittime, waittime, nettype)
233 rpcprog_t prog; /* program number */
234 rpcvers_t vers; /* version number */
235 rpcproc_t proc; /* procedure number */
236 xdrproc_t xargs; /* xdr routine for args */
237 caddr_t argsp; /* pointer to args */
238 xdrproc_t xresults; /* xdr routine for results */
239 caddr_t resultsp; /* pointer to results */
240 resultproc_t eachresult; /* call with each result obtained */
241 int inittime; /* how long to wait initially */
242 int waittime; /* maximum time to wait */
243 const char *nettype; /* transport type */
245 enum clnt_stat stat = RPC_SUCCESS; /* Return status */
246 XDR xdr_stream; /* XDR stream */
247 XDR *xdrs = &xdr_stream;
248 struct rpc_msg msg; /* RPC message */
250 char *outbuf = NULL; /* Broadcast msg buffer */
251 char *inbuf = NULL; /* Reply buf */
253 u_int maxbufsize = 0;
254 AUTH *sys_auth = authunix_create_default();
257 char uaddress[1024]; /* A self imposed limit */
258 char *uaddrp = uaddress;
259 int pmap_reply_flag; /* reply recvd from PORTMAP */
260 /* An array of all the suitable broadcast transports */
262 int fd; /* File descriptor */
265 struct netconfig *nconf; /* Netconfig structure */
266 u_int asize; /* Size of the addr buf */
267 u_int dsize; /* Size of the data buf */
268 struct sockaddr_storage raddr; /* Remote address */
271 struct pollfd pfd[MAXBCAST];
273 struct r_rpcb_rmtcallargs barg; /* Remote arguments */
274 struct r_rpcb_rmtcallres bres; /* Remote results */
276 struct netconfig *nconf;
282 size_t outlen_pmap = 0;
283 u_long port; /* Remote port number */
284 int pmap_flag = 0; /* UDP exists ? */
285 char *outbuf_pmap = NULL;
286 struct rmtcallargs barg_pmap; /* Remote arguments */
287 struct rmtcallres bres_pmap; /* Remote results */
291 if (sys_auth == NULL) {
292 return (RPC_SYSTEMERROR);
295 * initialization: create a fd, a broadcast address, and send the
296 * request on the broadcast transport.
297 * Listen on all of them and on replies, call the user supplied
302 nettype = "datagram_n";
303 if ((handle = __rpc_setconf(nettype)) == NULL) {
304 AUTH_DESTROY(sys_auth);
305 return (RPC_UNKNOWNPROTO);
307 while ((nconf = __rpc_getconf(handle)) != NULL) {
309 struct __rpc_sockinfo si;
311 if (nconf->nc_semantics != NC_TPI_CLTS)
313 if (fdlistno >= MAXBCAST)
314 break; /* No more slots available */
315 if (!__rpc_nconf2sockinfo(nconf, &si))
318 TAILQ_INIT(&fdlist[fdlistno].nal);
319 if (__rpc_getbroadifs(si.si_af, si.si_proto, si.si_socktype,
320 &fdlist[fdlistno].nal) == 0)
323 fd = _socket(si.si_af, si.si_socktype, si.si_proto);
328 fdlist[fdlistno].af = si.si_af;
329 fdlist[fdlistno].proto = si.si_proto;
330 fdlist[fdlistno].fd = fd;
331 fdlist[fdlistno].nconf = nconf;
332 fdlist[fdlistno].asize = __rpc_get_a_size(si.si_af);
333 pfd[fdlistno].events = POLLIN | POLLPRI |
334 POLLRDNORM | POLLRDBAND;
335 pfd[fdlistno].fd = fdlist[fdlistno].fd = fd;
336 fdlist[fdlistno].dsize = __rpc_get_t_size(si.si_af, si.si_proto,
339 if (maxbufsize <= fdlist[fdlistno].dsize)
340 maxbufsize = fdlist[fdlistno].dsize;
343 if (si.si_af == AF_INET && si.si_proto == IPPROTO_UDP) {
344 udpbufsz = fdlist[fdlistno].dsize;
345 if ((outbuf_pmap = malloc(udpbufsz)) == 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 */
641 (void) free(outbuf_pmap);
643 for (i = 0; i < fdlistno; i++) {
644 (void)_close(fdlist[i].fd);
645 __rpc_freebroadifs(&fdlist[i].nal);
647 AUTH_DESTROY(sys_auth);
648 (void) __rpc_endconf(handle);
655 rpc_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp,
657 rpcprog_t prog; /* program number */
658 rpcvers_t vers; /* version number */
659 rpcproc_t proc; /* procedure number */
660 xdrproc_t xargs; /* xdr routine for args */
661 caddr_t argsp; /* pointer to args */
662 xdrproc_t xresults; /* xdr routine for results */
663 caddr_t resultsp; /* pointer to results */
664 resultproc_t eachresult; /* call with each result obtained */
665 const char *nettype; /* transport type */
667 enum clnt_stat dummy;
669 dummy = rpc_broadcast_exp(prog, vers, proc, xargs, argsp,
670 xresults, resultsp, eachresult,
671 INITTIME, WAITTIME, nettype);