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)
147 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
148 if (ifap->ifa_addr->sa_family != af ||
149 !(ifap->ifa_flags & IFF_UP))
151 bip = (struct broadif *)malloc(sizeof *bip);
154 bip->index = if_nametoindex(ifap->ifa_name);
159 (ifap->ifa_flags & IFF_BROADCAST) &&
160 ifap->ifa_broadaddr) {
161 memcpy(&bip->broadaddr, ifap->ifa_broadaddr,
162 (size_t)ifap->ifa_broadaddr->sa_len);
163 sin = (struct sockaddr_in *)(void *)&bip->broadaddr;
165 ((struct sockaddr_in *)
166 (void *)res->ai_addr)->sin_port;
169 if (af == AF_INET6 && (ifap->ifa_flags & IFF_MULTICAST)) {
170 sin6 = (struct sockaddr_in6 *)(void *)&bip->broadaddr;
171 inet_pton(af, RPCB_MULTICAST_ADDR, &sin6->sin6_addr);
172 sin6->sin6_family = af;
173 sin6->sin6_len = sizeof *sin6;
175 ((struct sockaddr_in6 *)
176 (void *)res->ai_addr)->sin6_port;
177 sin6->sin6_scope_id = bip->index;
184 TAILQ_INSERT_TAIL(list, bip, link);
194 __rpc_freebroadifs(broadlist_t *list)
196 struct broadif *bip, *next;
198 bip = TAILQ_FIRST(list);
200 while (bip != NULL) {
201 next = TAILQ_NEXT(bip, link);
209 __rpc_broadenable(int af, int s, struct broadif *bip)
214 if (af == AF_INET6) {
215 fprintf(stderr, "set v6 multicast if to %d\n", bip->index);
216 if (_setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, &bip->index,
217 sizeof bip->index) < 0)
221 if (_setsockopt(s, SOL_SOCKET, SO_BROADCAST, &o, sizeof o) < 0)
229 rpc_broadcast_exp(prog, vers, proc, xargs, argsp, xresults, resultsp,
230 eachresult, inittime, waittime, nettype)
231 rpcprog_t prog; /* program number */
232 rpcvers_t vers; /* version number */
233 rpcproc_t proc; /* procedure number */
234 xdrproc_t xargs; /* xdr routine for args */
235 caddr_t argsp; /* pointer to args */
236 xdrproc_t xresults; /* xdr routine for results */
237 caddr_t resultsp; /* pointer to results */
238 resultproc_t eachresult; /* call with each result obtained */
239 int inittime; /* how long to wait initially */
240 int waittime; /* maximum time to wait */
241 const char *nettype; /* transport type */
243 enum clnt_stat stat = RPC_SUCCESS; /* Return status */
244 XDR xdr_stream; /* XDR stream */
245 XDR *xdrs = &xdr_stream;
246 struct rpc_msg msg; /* RPC message */
248 char *outbuf = NULL; /* Broadcast msg buffer */
249 char *inbuf = NULL; /* Reply buf */
251 u_int maxbufsize = 0;
252 AUTH *sys_auth = authunix_create_default();
255 char uaddress[1024]; /* A self imposed limit */
256 char *uaddrp = uaddress;
257 int pmap_reply_flag; /* reply recvd from PORTMAP */
258 /* An array of all the suitable broadcast transports */
260 int fd; /* File descriptor */
263 struct netconfig *nconf; /* Netconfig structure */
264 u_int asize; /* Size of the addr buf */
265 u_int dsize; /* Size of the data buf */
266 struct sockaddr_storage raddr; /* Remote address */
269 struct pollfd pfd[MAXBCAST];
271 struct r_rpcb_rmtcallargs barg; /* Remote arguments */
272 struct r_rpcb_rmtcallres bres; /* Remote results */
274 struct netconfig *nconf;
280 size_t outlen_pmap = 0;
281 u_long port; /* Remote port number */
282 int pmap_flag = 0; /* UDP exists ? */
283 char *outbuf_pmap = NULL;
284 struct rmtcallargs barg_pmap; /* Remote arguments */
285 struct rmtcallres bres_pmap; /* Remote results */
289 if (sys_auth == NULL) {
290 return (RPC_SYSTEMERROR);
293 * initialization: create a fd, a broadcast address, and send the
294 * request on the broadcast transport.
295 * Listen on all of them and on replies, call the user supplied
300 nettype = "datagram_n";
301 if ((handle = __rpc_setconf(nettype)) == NULL) {
302 return (RPC_UNKNOWNPROTO);
304 while ((nconf = __rpc_getconf(handle)) != NULL) {
306 struct __rpc_sockinfo si;
308 if (nconf->nc_semantics != NC_TPI_CLTS)
310 if (fdlistno >= MAXBCAST)
311 break; /* No more slots available */
312 if (!__rpc_nconf2sockinfo(nconf, &si))
315 TAILQ_INIT(&fdlist[fdlistno].nal);
316 if (__rpc_getbroadifs(si.si_af, si.si_proto, si.si_socktype,
317 &fdlist[fdlistno].nal) == 0)
320 fd = _socket(si.si_af, si.si_socktype, si.si_proto);
325 fdlist[fdlistno].af = si.si_af;
326 fdlist[fdlistno].proto = si.si_proto;
327 fdlist[fdlistno].fd = fd;
328 fdlist[fdlistno].nconf = nconf;
329 fdlist[fdlistno].asize = __rpc_get_a_size(si.si_af);
330 pfd[fdlistno].events = POLLIN | POLLPRI |
331 POLLRDNORM | POLLRDBAND;
332 pfd[fdlistno].fd = fdlist[fdlistno].fd = fd;
333 fdlist[fdlistno].dsize = __rpc_get_t_size(si.si_af, si.si_proto,
336 if (maxbufsize <= fdlist[fdlistno].dsize)
337 maxbufsize = fdlist[fdlistno].dsize;
340 if (si.si_af == AF_INET && si.si_proto == IPPROTO_UDP) {
341 udpbufsz = fdlist[fdlistno].dsize;
342 if ((outbuf_pmap = malloc(udpbufsz)) == NULL) {
344 stat = RPC_SYSTEMERROR;
354 if (stat == RPC_SUCCESS)
355 stat = RPC_UNKNOWNPROTO;
358 if (maxbufsize == 0) {
359 if (stat == RPC_SUCCESS)
363 inbuf = malloc(maxbufsize);
364 outbuf = malloc(maxbufsize);
365 if ((inbuf == NULL) || (outbuf == NULL)) {
366 stat = RPC_SYSTEMERROR;
370 /* Serialize all the arguments which have to be sent */
371 (void) gettimeofday(&t, NULL);
372 msg.rm_xid = __RPC_GETXID(&t);
373 msg.rm_direction = CALL;
374 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
375 msg.rm_call.cb_prog = RPCBPROG;
376 msg.rm_call.cb_vers = RPCBVERS;
377 msg.rm_call.cb_proc = RPCBPROC_CALLIT;
381 barg.args.args_val = argsp;
382 barg.xdr_args = xargs;
384 bres.results.results_val = resultsp;
385 bres.xdr_res = xresults;
386 msg.rm_call.cb_cred = sys_auth->ah_cred;
387 msg.rm_call.cb_verf = sys_auth->ah_verf;
388 xdrmem_create(xdrs, outbuf, maxbufsize, XDR_ENCODE);
389 if ((!xdr_callmsg(xdrs, &msg)) ||
390 (!xdr_rpcb_rmtcallargs(xdrs,
391 (struct rpcb_rmtcallargs *)(void *)&barg))) {
392 stat = RPC_CANTENCODEARGS;
395 outlen = xdr_getpos(xdrs);
399 /* Prepare the packet for version 2 PORTMAP */
401 msg.rm_xid++; /* One way to distinguish */
402 msg.rm_call.cb_prog = PMAPPROG;
403 msg.rm_call.cb_vers = PMAPVERS;
404 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
405 barg_pmap.prog = prog;
406 barg_pmap.vers = vers;
407 barg_pmap.proc = proc;
408 barg_pmap.args_ptr = argsp;
409 barg_pmap.xdr_args = xargs;
410 bres_pmap.port_ptr = &port;
411 bres_pmap.xdr_results = xresults;
412 bres_pmap.results_ptr = resultsp;
413 xdrmem_create(xdrs, outbuf_pmap, udpbufsz, XDR_ENCODE);
414 if ((! xdr_callmsg(xdrs, &msg)) ||
415 (! xdr_rmtcall_args(xdrs, &barg_pmap))) {
416 stat = RPC_CANTENCODEARGS;
419 outlen_pmap = xdr_getpos(xdrs);
425 * Basic loop: broadcast the packets to transports which
426 * support data packets of size such that one can encode
428 * Wait a while for response(s).
429 * The response timeout grows larger per iteration.
431 for (msec = inittime; msec <= waittime; msec += msec) {
434 /* Broadcast all the packets now */
435 for (i = 0; i < fdlistno; i++) {
436 if (fdlist[i].dsize < outlen) {
440 for (bip = TAILQ_FIRST(&fdlist[i].nal); bip != NULL;
441 bip = TAILQ_NEXT(bip, link)) {
444 addr = &bip->broadaddr;
446 __rpc_broadenable(fdlist[i].af, fdlist[i].fd,
450 * Only use version 3 if lowvers is not set
454 if (_sendto(fdlist[i].fd, outbuf,
455 outlen, 0, (struct sockaddr*)addr,
456 (size_t)fdlist[i].asize) !=
461 warnx("clnt_bcast: cannot send"
468 fprintf(stderr, "Broadcast packet sent "
470 fdlist[i].nconf->nc_netid);
474 * Send the version 2 packet also
478 fdlist[i].proto == IPPROTO_UDP) {
479 if (_sendto(fdlist[i].fd, outbuf_pmap,
480 outlen_pmap, 0, addr,
481 (size_t)fdlist[i].asize) !=
484 "Cannot send broadcast packet");
490 fprintf(stderr, "PMAP Broadcast packet "
492 fdlist[i].nconf->nc_netid);
496 /* End for sending all packets on this transport */
497 } /* End for sending on all transports */
499 if (eachresult == NULL) {
505 * Get all the replies from these broadcast requests
509 switch (pollretval = _poll(pfd, fdlistno, msec)) {
510 case 0: /* timed out */
513 case -1: /* some kind of error - we ignore it */
515 } /* end of poll results switch */
517 for (i = fds_found = 0;
518 i < fdlistno && fds_found < pollretval; i++) {
521 if (pfd[i].revents == 0)
523 else if (pfd[i].revents & POLLNVAL) {
525 * Something bad has happened to this descri-
526 * ptor. We can cause _poll() to ignore
527 * it simply by using a negative fd. We do that
528 * rather than compacting the pfd[] and fdlist[]
537 fprintf(stderr, "response for %s\n",
538 fdlist[i].nconf->nc_netid);
541 inlen = _recvfrom(fdlist[i].fd, inbuf, fdlist[i].dsize,
542 0, (struct sockaddr *)(void *)&fdlist[i].raddr,
547 warnx("clnt_bcast: Cannot receive reply to "
552 if (inlen < sizeof (u_int32_t))
553 continue; /* Drop that and go ahead */
555 * see if reply transaction id matches sent id.
556 * If so, decode the results. If return id is xid + 1
557 * it was a PORTMAP reply
559 if (*((u_int32_t *)(void *)(inbuf)) ==
560 *((u_int32_t *)(void *)(outbuf))) {
562 msg.acpted_rply.ar_verf = _null_auth;
563 msg.acpted_rply.ar_results.where =
564 (caddr_t)(void *)&bres;
565 msg.acpted_rply.ar_results.proc =
566 (xdrproc_t)xdr_rpcb_rmtcallres;
568 } else if (pmap_flag &&
569 *((u_int32_t *)(void *)(inbuf)) ==
570 *((u_int32_t *)(void *)(outbuf_pmap))) {
572 msg.acpted_rply.ar_verf = _null_auth;
573 msg.acpted_rply.ar_results.where =
574 (caddr_t)(void *)&bres_pmap;
575 msg.acpted_rply.ar_results.proc =
576 (xdrproc_t)xdr_rmtcallres;
580 xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
581 if (xdr_replymsg(xdrs, &msg)) {
582 if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
583 (msg.acpted_rply.ar_stat == SUCCESS)) {
584 struct netbuf taddr, *np;
585 struct sockaddr_in *sin;
588 if (pmap_flag && pmap_reply_flag) {
589 sin = (struct sockaddr_in *)
590 (void *)&fdlist[i].raddr;
592 htons((u_short)port);
593 taddr.len = taddr.maxlen =
594 fdlist[i].raddr.ss_len;
595 taddr.buf = &fdlist[i].raddr;
596 done = (*eachresult)(resultsp,
597 &taddr, fdlist[i].nconf);
601 fprintf(stderr, "uaddr %s\n",
605 fdlist[i].nconf, uaddrp);
606 done = (*eachresult)(resultsp,
607 np, fdlist[i].nconf);
613 /* otherwise, we just ignore the errors ... */
615 /* else some kind of deserialization problem ... */
617 xdrs->x_op = XDR_FREE;
618 msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
619 (void) xdr_replymsg(xdrs, &msg);
620 (void) (*xresults)(xdrs, resultsp);
628 } /* The recv for loop */
629 } /* The giant for loop */
638 (void) free(outbuf_pmap);
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);