1 /* $NetBSD: clnt_bcast.c,v 1.3 2000/07/06 03:05:20 christos Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2009, Sun Microsystems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #ident "@(#)clnt_bcast.c 1.18 94/05/03 SMI"
38 static char sccsid[] = "@(#)clnt_bcast.c 1.15 89/04/21 Copyr 1988 Sun Micro";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
46 * Client interface to broadcast service.
48 * Copyright (C) 1988, Sun Microsystems, Inc.
50 * The following is kludged-up support for simple rpc broadcasts.
51 * Someday a large, complicated system will replace these routines.
54 #include "namespace.h"
55 #include <sys/types.h>
56 #include <sys/socket.h>
57 #include <sys/queue.h>
59 #include <netinet/in.h>
64 #include <rpc/pmap_prot.h>
65 #include <rpc/pmap_clnt.h>
66 #include <rpc/pmap_rmt.h>
68 #include <rpc/nettype.h>
69 #include <arpa/inet.h>
79 #include "un-namespace.h"
83 #define MAXBCAST 20 /* Max no of broadcasting transports */
84 #define INITTIME 4000 /* Time to wait initially */
85 #define WAITTIME 8000 /* Maximum time to wait */
88 * If nettype is NULL, it broadcasts on all the available
89 * datagram_n transports. May potentially lead to broadacst storms
90 * and hence should be used with caution, care and courage.
92 * The current parameter xdr packet size is limited by the max tsdu
93 * size of the transport. If the max tsdu size of any transport is
94 * smaller than the parameter xdr packet, then broadcast is not
95 * sent on that transport.
97 * Also, the packet size should be less the packet size of
98 * the data link layer (for ethernet it is 1400 bytes). There is
99 * no easy way to find out the max size of the data link layer and
100 * we are assuming that the args would be smaller than that.
102 * The result size has to be smaller than the transport tsdu size.
104 * If PORTMAP has been defined, we send two packets for UDP, one for
105 * rpcbind and one for portmap. For those machines which support
106 * both rpcbind and portmap, it will cause them to reply twice, and
107 * also here it will get two responses ... inefficient and clumsy.
112 struct sockaddr_storage broadaddr;
113 TAILQ_ENTRY(broadif) link;
116 typedef TAILQ_HEAD(, broadif) broadlist_t;
118 int __rpc_getbroadifs(int, int, int, broadlist_t *);
119 void __rpc_freebroadifs(broadlist_t *);
120 int __rpc_broadenable(int, int, struct broadif *);
122 int __rpc_lowvers = 0;
125 __rpc_getbroadifs(int af, int proto, int socktype, broadlist_t *list)
129 struct ifaddrs *ifap, *ifp;
131 struct sockaddr_in6 *sin6;
133 struct sockaddr_in *sin;
134 struct addrinfo hints, *res;
136 if (getifaddrs(&ifp) < 0)
139 memset(&hints, 0, sizeof hints);
141 hints.ai_family = af;
142 hints.ai_protocol = proto;
143 hints.ai_socktype = socktype;
145 if (getaddrinfo(NULL, "sunrpc", &hints, &res) != 0) {
150 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
151 if (ifap->ifa_addr->sa_family != af ||
152 !(ifap->ifa_flags & IFF_UP))
154 bip = (struct broadif *)malloc(sizeof *bip);
157 bip->index = if_nametoindex(ifap->ifa_name);
162 (ifap->ifa_flags & IFF_BROADCAST) &&
163 ifap->ifa_broadaddr) {
164 memcpy(&bip->broadaddr, ifap->ifa_broadaddr,
165 (size_t)ifap->ifa_broadaddr->sa_len);
166 sin = (struct sockaddr_in *)(void *)&bip->broadaddr;
168 ((struct sockaddr_in *)
169 (void *)res->ai_addr)->sin_port;
172 if (af == AF_INET6 && (ifap->ifa_flags & IFF_MULTICAST)) {
173 sin6 = (struct sockaddr_in6 *)(void *)&bip->broadaddr;
174 inet_pton(af, RPCB_MULTICAST_ADDR, &sin6->sin6_addr);
175 sin6->sin6_family = af;
176 sin6->sin6_len = sizeof *sin6;
178 ((struct sockaddr_in6 *)
179 (void *)res->ai_addr)->sin6_port;
180 sin6->sin6_scope_id = bip->index;
187 TAILQ_INSERT_TAIL(list, bip, link);
197 __rpc_freebroadifs(broadlist_t *list)
199 struct broadif *bip, *next;
201 bip = TAILQ_FIRST(list);
203 while (bip != NULL) {
204 next = TAILQ_NEXT(bip, link);
212 __rpc_broadenable(int af, int s, struct broadif *bip)
217 if (af == AF_INET6) {
218 fprintf(stderr, "set v6 multicast if to %d\n", bip->index);
219 if (_setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, &bip->index,
220 sizeof bip->index) < 0)
224 if (_setsockopt(s, SOL_SOCKET, SO_BROADCAST, &o, sizeof o) < 0)
231 * rpc_broadcast_exp()
233 * prog - program number
234 * vers - version number
235 * proc - procedure number
236 * xargs - xdr routine for args
237 * argsp - pointer to args
238 * xresults - xdr routine for results
239 * resultsp - pointer to results
240 * eachresult - call with each result obtained
241 * inittime - how long to wait initially
242 * waittime - maximum time to wait
243 * nettype - transport type
246 rpc_broadcast_exp(rpcprog_t prog, rpcvers_t vers, rpcproc_t proc,
247 xdrproc_t xargs, caddr_t argsp, xdrproc_t xresults, caddr_t resultsp,
248 resultproc_t eachresult, int inittime, int waittime,
251 enum clnt_stat stat = RPC_SUCCESS; /* Return status */
252 XDR xdr_stream; /* XDR stream */
253 XDR *xdrs = &xdr_stream;
254 struct rpc_msg msg; /* RPC message */
256 char *outbuf = NULL; /* Broadcast msg buffer */
257 char *inbuf = NULL; /* Reply buf */
259 u_int maxbufsize = 0;
260 AUTH *sys_auth = authunix_create_default();
263 char uaddress[1024]; /* A self imposed limit */
264 char *uaddrp = uaddress;
265 int pmap_reply_flag; /* reply recvd from PORTMAP */
266 /* An array of all the suitable broadcast transports */
268 int fd; /* File descriptor */
271 struct netconfig *nconf; /* Netconfig structure */
272 u_int asize; /* Size of the addr buf */
273 u_int dsize; /* Size of the data buf */
274 struct sockaddr_storage raddr; /* Remote address */
277 struct pollfd pfd[MAXBCAST];
279 struct r_rpcb_rmtcallargs barg; /* Remote arguments */
280 struct r_rpcb_rmtcallres bres; /* Remote results */
282 struct netconfig *nconf;
288 size_t outlen_pmap = 0;
289 u_long port; /* Remote port number */
290 int pmap_flag = 0; /* UDP exists ? */
291 char *outbuf_pmap = NULL;
292 struct rmtcallargs barg_pmap; /* Remote arguments */
293 struct rmtcallres bres_pmap; /* Remote results */
297 if (sys_auth == NULL) {
298 return (RPC_SYSTEMERROR);
301 * initialization: create a fd, a broadcast address, and send the
302 * request on the broadcast transport.
303 * Listen on all of them and on replies, call the user supplied
308 nettype = "datagram_n";
309 if ((handle = __rpc_setconf(nettype)) == NULL) {
310 AUTH_DESTROY(sys_auth);
311 return (RPC_UNKNOWNPROTO);
313 while ((nconf = __rpc_getconf(handle)) != NULL) {
315 struct __rpc_sockinfo si;
317 if (nconf->nc_semantics != NC_TPI_CLTS)
319 if (fdlistno >= MAXBCAST)
320 break; /* No more slots available */
321 if (!__rpc_nconf2sockinfo(nconf, &si))
324 TAILQ_INIT(&fdlist[fdlistno].nal);
325 if (__rpc_getbroadifs(si.si_af, si.si_proto, si.si_socktype,
326 &fdlist[fdlistno].nal) == 0)
329 fd = _socket(si.si_af, si.si_socktype, si.si_proto);
334 fdlist[fdlistno].af = si.si_af;
335 fdlist[fdlistno].proto = si.si_proto;
336 fdlist[fdlistno].fd = fd;
337 fdlist[fdlistno].nconf = nconf;
338 fdlist[fdlistno].asize = __rpc_get_a_size(si.si_af);
339 pfd[fdlistno].events = POLLIN | POLLPRI |
340 POLLRDNORM | POLLRDBAND;
341 pfd[fdlistno].fd = fdlist[fdlistno].fd = fd;
342 fdlist[fdlistno].dsize = __rpc_get_t_size(si.si_af, si.si_proto,
345 if (maxbufsize <= fdlist[fdlistno].dsize)
346 maxbufsize = fdlist[fdlistno].dsize;
349 if (si.si_af == AF_INET && si.si_proto == IPPROTO_UDP) {
350 udpbufsz = fdlist[fdlistno].dsize;
351 outbuf_pmap = reallocf(outbuf_pmap, udpbufsz);
352 if (outbuf_pmap == NULL) {
354 stat = RPC_SYSTEMERROR;
364 if (stat == RPC_SUCCESS)
365 stat = RPC_UNKNOWNPROTO;
368 if (maxbufsize == 0) {
369 if (stat == RPC_SUCCESS)
373 inbuf = malloc(maxbufsize);
374 outbuf = malloc(maxbufsize);
375 if ((inbuf == NULL) || (outbuf == NULL)) {
376 stat = RPC_SYSTEMERROR;
380 /* Serialize all the arguments which have to be sent */
381 (void) gettimeofday(&t, NULL);
382 msg.rm_xid = __RPC_GETXID(&t);
383 msg.rm_direction = CALL;
384 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
385 msg.rm_call.cb_prog = RPCBPROG;
386 msg.rm_call.cb_vers = RPCBVERS;
387 msg.rm_call.cb_proc = RPCBPROC_CALLIT;
391 barg.args.args_val = argsp;
392 barg.xdr_args = xargs;
394 bres.results.results_val = resultsp;
395 bres.xdr_res = xresults;
396 msg.rm_call.cb_cred = sys_auth->ah_cred;
397 msg.rm_call.cb_verf = sys_auth->ah_verf;
398 xdrmem_create(xdrs, outbuf, maxbufsize, XDR_ENCODE);
399 if ((!xdr_callmsg(xdrs, &msg)) ||
400 (!xdr_rpcb_rmtcallargs(xdrs,
401 (struct rpcb_rmtcallargs *)(void *)&barg))) {
402 stat = RPC_CANTENCODEARGS;
405 outlen = xdr_getpos(xdrs);
409 /* Prepare the packet for version 2 PORTMAP */
411 msg.rm_xid++; /* One way to distinguish */
412 msg.rm_call.cb_prog = PMAPPROG;
413 msg.rm_call.cb_vers = PMAPVERS;
414 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
415 barg_pmap.prog = prog;
416 barg_pmap.vers = vers;
417 barg_pmap.proc = proc;
418 barg_pmap.args_ptr = argsp;
419 barg_pmap.xdr_args = xargs;
420 bres_pmap.port_ptr = &port;
421 bres_pmap.xdr_results = xresults;
422 bres_pmap.results_ptr = resultsp;
423 xdrmem_create(xdrs, outbuf_pmap, udpbufsz, XDR_ENCODE);
424 if ((! xdr_callmsg(xdrs, &msg)) ||
425 (! xdr_rmtcall_args(xdrs, &barg_pmap))) {
426 stat = RPC_CANTENCODEARGS;
429 outlen_pmap = xdr_getpos(xdrs);
435 * Basic loop: broadcast the packets to transports which
436 * support data packets of size such that one can encode
438 * Wait a while for response(s).
439 * The response timeout grows larger per iteration.
441 for (msec = inittime; msec <= waittime; msec += msec) {
444 /* Broadcast all the packets now */
445 for (i = 0; i < fdlistno; i++) {
446 if (fdlist[i].dsize < outlen) {
450 for (bip = TAILQ_FIRST(&fdlist[i].nal); bip != NULL;
451 bip = TAILQ_NEXT(bip, link)) {
454 addr = &bip->broadaddr;
456 __rpc_broadenable(fdlist[i].af, fdlist[i].fd,
460 * Only use version 3 if lowvers is not set
464 if (_sendto(fdlist[i].fd, outbuf,
465 outlen, 0, (struct sockaddr*)addr,
466 (size_t)fdlist[i].asize) !=
471 warnx("clnt_bcast: cannot send "
478 fprintf(stderr, "Broadcast packet sent "
480 fdlist[i].nconf->nc_netid);
484 * Send the version 2 packet also
488 fdlist[i].proto == IPPROTO_UDP) {
489 if (_sendto(fdlist[i].fd, outbuf_pmap,
490 outlen_pmap, 0, addr,
491 (size_t)fdlist[i].asize) !=
494 "Cannot send broadcast packet");
500 fprintf(stderr, "PMAP Broadcast packet "
502 fdlist[i].nconf->nc_netid);
506 /* End for sending all packets on this transport */
507 } /* End for sending on all transports */
509 if (eachresult == NULL) {
515 * Get all the replies from these broadcast requests
519 switch (pollretval = _poll(pfd, fdlistno, msec)) {
520 case 0: /* timed out */
523 case -1: /* some kind of error - we ignore it */
525 } /* end of poll results switch */
527 for (i = fds_found = 0;
528 i < fdlistno && fds_found < pollretval; i++) {
531 if (pfd[i].revents == 0)
533 else if (pfd[i].revents & POLLNVAL) {
535 * Something bad has happened to this descri-
536 * ptor. We can cause _poll() to ignore
537 * it simply by using a negative fd. We do that
538 * rather than compacting the pfd[] and fdlist[]
547 fprintf(stderr, "response for %s\n",
548 fdlist[i].nconf->nc_netid);
551 inlen = _recvfrom(fdlist[i].fd, inbuf, fdlist[i].dsize,
552 0, (struct sockaddr *)(void *)&fdlist[i].raddr,
557 warnx("clnt_bcast: Cannot receive reply to "
562 if (inlen < sizeof (u_int32_t))
563 continue; /* Drop that and go ahead */
565 * see if reply transaction id matches sent id.
566 * If so, decode the results. If return id is xid + 1
567 * it was a PORTMAP reply
569 if (*((u_int32_t *)(void *)(inbuf)) ==
570 *((u_int32_t *)(void *)(outbuf))) {
572 msg.acpted_rply.ar_verf = _null_auth;
573 msg.acpted_rply.ar_results.where =
574 (caddr_t)(void *)&bres;
575 msg.acpted_rply.ar_results.proc =
576 (xdrproc_t)xdr_rpcb_rmtcallres;
578 } else if (pmap_flag &&
579 *((u_int32_t *)(void *)(inbuf)) ==
580 *((u_int32_t *)(void *)(outbuf_pmap))) {
582 msg.acpted_rply.ar_verf = _null_auth;
583 msg.acpted_rply.ar_results.where =
584 (caddr_t)(void *)&bres_pmap;
585 msg.acpted_rply.ar_results.proc =
586 (xdrproc_t)xdr_rmtcallres;
590 xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
591 if (xdr_replymsg(xdrs, &msg)) {
592 if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
593 (msg.acpted_rply.ar_stat == SUCCESS)) {
594 struct netbuf taddr, *np;
595 struct sockaddr_in *sin;
598 if (pmap_flag && pmap_reply_flag) {
599 sin = (struct sockaddr_in *)
600 (void *)&fdlist[i].raddr;
602 htons((u_short)port);
603 taddr.len = taddr.maxlen =
604 fdlist[i].raddr.ss_len;
605 taddr.buf = &fdlist[i].raddr;
606 done = (*eachresult)(resultsp,
607 &taddr, fdlist[i].nconf);
611 fprintf(stderr, "uaddr %s\n",
615 fdlist[i].nconf, uaddrp);
616 done = (*eachresult)(resultsp,
617 np, fdlist[i].nconf);
623 /* otherwise, we just ignore the errors ... */
625 /* else some kind of deserialization problem ... */
627 xdrs->x_op = XDR_FREE;
628 msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
629 (void) xdr_replymsg(xdrs, &msg);
630 (void) (*xresults)(xdrs, resultsp);
638 } /* The recv for loop */
639 } /* The giant for loop */
647 for (i = 0; i < fdlistno; i++) {
648 (void)_close(fdlist[i].fd);
649 __rpc_freebroadifs(&fdlist[i].nal);
651 AUTH_DESTROY(sys_auth);
652 (void) __rpc_endconf(handle);
660 * prog - program number
661 * vers - version number
662 * proc - procedure number
663 * xargs - xdr routine for args
664 * argsp - pointer to args
665 * xresults - xdr routine for results
666 * resultsp - pointer to results
667 * eachresult - call with each result obtained
668 * nettype - transport type
671 rpc_broadcast(rpcprog_t prog, rpcvers_t vers, rpcproc_t proc, xdrproc_t xargs,
672 caddr_t argsp, xdrproc_t xresults, caddr_t resultsp,
673 resultproc_t eachresult, const char *nettype)
675 enum clnt_stat dummy;
677 dummy = rpc_broadcast_exp(prog, vers, proc, xargs, argsp,
678 xresults, resultsp, eachresult,
679 INITTIME, WAITTIME, nettype);