Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.git] / lib / libc / rpc / clnt_bcast.c
blobf424bd6dfba14dedec110e45ec8fa762ac66023b
1 /*
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.
26 * 2550 Garcia Avenue
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 $
34 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
38 * clnt_bcast.c
39 * Client interface to broadcast service.
41 * Copyright (C) 1988, Sun Microsystems, Inc.
43 * The following is kludged-up support for simple rpc broadcasts.
44 * Someday a large, complicated system will replace these routines.
47 #include "namespace.h"
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <sys/queue.h>
51 #include <net/if.h>
52 #include <netinet/in.h>
53 #include <ifaddrs.h>
54 #include <sys/poll.h>
55 #include <rpc/rpc.h>
56 #ifdef PORTMAP
57 #include <rpc/pmap_prot.h>
58 #include <rpc/pmap_clnt.h>
59 #include <rpc/pmap_rmt.h>
60 #endif /* PORTMAP */
61 #include <rpc/nettype.h>
62 #include <arpa/inet.h>
63 #ifdef RPC_DEBUG
64 #include <stdio.h>
65 #endif
66 #include <errno.h>
67 #include <stdlib.h>
68 #include <unistd.h>
69 #include <netdb.h>
70 #include <err.h>
71 #include <string.h>
72 #include "un-namespace.h"
74 #include "rpc_com.h"
76 #define MAXBCAST 20 /* Max no of broadcasting transports */
77 #define INITTIME 4000 /* Time to wait initially */
78 #define WAITTIME 8000 /* Maximum time to wait */
81 * If nettype is NULL, it broadcasts on all the available
82 * datagram_n transports. May potentially lead to broadacst storms
83 * and hence should be used with caution, care and courage.
85 * The current parameter xdr packet size is limited by the max tsdu
86 * size of the transport. If the max tsdu size of any transport is
87 * smaller than the parameter xdr packet, then broadcast is not
88 * sent on that transport.
90 * Also, the packet size should be less the packet size of
91 * the data link layer (for ethernet it is 1400 bytes). There is
92 * no easy way to find out the max size of the data link layer and
93 * we are assuming that the args would be smaller than that.
95 * The result size has to be smaller than the transport tsdu size.
97 * If PORTMAP has been defined, we send two packets for UDP, one for
98 * rpcbind and one for portmap. For those machines which support
99 * both rpcbind and portmap, it will cause them to reply twice, and
100 * also here it will get two responses ... inefficient and clumsy.
103 struct broadif {
104 int index;
105 struct sockaddr_storage broadaddr;
106 TAILQ_ENTRY(broadif) link;
109 typedef TAILQ_HEAD(, broadif) broadlist_t;
111 int __rpc_broadenable(int, int, struct broadif *);
112 void __rpc_freebroadifs(broadlist_t *);
113 int __rpc_getbroadifs(int, int, int, broadlist_t *);
115 int __rpc_lowvers = 0;
118 __rpc_getbroadifs(int af, int proto, int socktype, broadlist_t *list)
120 int count = 0;
121 struct broadif *bip;
122 struct ifaddrs *ifap, *ifp;
123 #ifdef INET6
124 struct sockaddr_in6 *sin6;
125 #endif
126 struct sockaddr_in *sin;
127 struct addrinfo hints, *res;
129 if (getifaddrs(&ifp) < 0)
130 return 0;
132 memset(&hints, 0, sizeof hints);
134 hints.ai_family = af;
135 hints.ai_protocol = proto;
136 hints.ai_socktype = socktype;
138 if (getaddrinfo(NULL, "sunrpc", &hints, &res) != 0) {
139 freeifaddrs(ifp);
140 return 0;
143 for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
144 if (ifap->ifa_addr->sa_family != af ||
145 !(ifap->ifa_flags & IFF_UP))
146 continue;
147 bip = (struct broadif *)malloc(sizeof *bip);
148 if (bip == NULL)
149 break;
150 bip->index = if_nametoindex(ifap->ifa_name);
151 if (
152 #ifdef INET6
153 af != AF_INET6 &&
154 #endif
155 (ifap->ifa_flags & IFF_BROADCAST) &&
156 ifap->ifa_broadaddr) {
157 memcpy(&bip->broadaddr, ifap->ifa_broadaddr,
158 (size_t)ifap->ifa_broadaddr->sa_len);
159 sin = (struct sockaddr_in *)(void *)&bip->broadaddr;
160 sin->sin_port =
161 ((struct sockaddr_in *)
162 (void *)res->ai_addr)->sin_port;
163 } else
164 #ifdef INET6
165 if (af == AF_INET6 && (ifap->ifa_flags & IFF_MULTICAST)) {
166 sin6 = (struct sockaddr_in6 *)(void *)&bip->broadaddr;
167 inet_pton(af, RPCB_MULTICAST_ADDR, &sin6->sin6_addr);
168 sin6->sin6_family = af;
169 sin6->sin6_len = sizeof *sin6;
170 sin6->sin6_port =
171 ((struct sockaddr_in6 *)
172 (void *)res->ai_addr)->sin6_port;
173 sin6->sin6_scope_id = bip->index;
174 } else
175 #endif
177 free(bip);
178 continue;
180 TAILQ_INSERT_TAIL(list, bip, link);
181 count++;
183 freeifaddrs(ifp);
184 freeaddrinfo(res);
186 return count;
189 void
190 __rpc_freebroadifs(broadlist_t *list)
192 struct broadif *bip, *next;
194 bip = TAILQ_FIRST(list);
196 while (bip != NULL) {
197 next = TAILQ_NEXT(bip, link);
198 free(bip);
199 bip = next;
204 /*ARGSUSED*/
205 __rpc_broadenable(int af, int s, struct broadif *bip)
207 int o = 1;
209 #if 0
210 if (af == AF_INET6) {
211 fprintf(stderr, "set v6 multicast if to %d\n", bip->index);
212 if (_setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, &bip->index,
213 sizeof bip->index) < 0)
214 return -1;
215 } else
216 #endif
217 if (_setsockopt(s, SOL_SOCKET, SO_BROADCAST, &o, sizeof o) < 0)
218 return -1;
220 return 0;
224 enum clnt_stat
225 rpc_broadcast_exp(prog, vers, proc, xargs, argsp, xresults, resultsp,
226 eachresult, inittime, waittime, nettype)
227 rpcprog_t prog; /* program number */
228 rpcvers_t vers; /* version number */
229 rpcproc_t proc; /* procedure number */
230 xdrproc_t xargs; /* xdr routine for args */
231 caddr_t argsp; /* pointer to args */
232 xdrproc_t xresults; /* xdr routine for results */
233 caddr_t resultsp; /* pointer to results */
234 resultproc_t eachresult; /* call with each result obtained */
235 int inittime; /* how long to wait initially */
236 int waittime; /* maximum time to wait */
237 const char *nettype; /* transport type */
239 enum clnt_stat stat = RPC_SUCCESS; /* Return status */
240 XDR xdr_stream; /* XDR stream */
241 XDR *xdrs = &xdr_stream;
242 struct rpc_msg msg; /* RPC message */
243 struct timeval t;
244 char *outbuf = NULL; /* Broadcast msg buffer */
245 char *inbuf = NULL; /* Reply buf */
246 int inlen;
247 u_int maxbufsize = 0;
248 AUTH *sys_auth = authunix_create_default();
249 int i;
250 void *handle;
251 char uaddress[1024]; /* A self imposed limit */
252 char *uaddrp = uaddress;
253 int pmap_reply_flag; /* reply recvd from PORTMAP */
254 /* An array of all the suitable broadcast transports */
255 struct {
256 int fd; /* File descriptor */
257 int af;
258 int proto;
259 struct netconfig *nconf; /* Netconfig structure */
260 u_int asize; /* Size of the addr buf */
261 u_int dsize; /* Size of the data buf */
262 struct sockaddr_storage raddr; /* Remote address */
263 broadlist_t nal;
264 } fdlist[MAXBCAST];
265 struct pollfd pfd[MAXBCAST];
266 size_t fdlistno = 0;
267 struct r_rpcb_rmtcallargs barg; /* Remote arguments */
268 struct r_rpcb_rmtcallres bres; /* Remote results */
269 size_t outlen;
270 struct netconfig *nconf;
271 int msec;
272 int pollretval;
273 int fds_found;
275 #ifdef PORTMAP
276 size_t outlen_pmap = 0;
277 u_long port; /* Remote port number */
278 int pmap_flag = 0; /* UDP exists ? */
279 char *outbuf_pmap = NULL;
280 struct rmtcallargs barg_pmap; /* Remote arguments */
281 struct rmtcallres bres_pmap; /* Remote results */
282 u_int udpbufsz = 0;
283 #endif /* PORTMAP */
285 if (sys_auth == NULL) {
286 return (RPC_SYSTEMERROR);
289 * initialization: create a fd, a broadcast address, and send the
290 * request on the broadcast transport.
291 * Listen on all of them and on replies, call the user supplied
292 * function.
295 if (nettype == NULL)
296 nettype = "datagram_n";
297 if ((handle = __rpc_setconf(nettype)) == NULL) {
298 AUTH_DESTROY(sys_auth);
299 return (RPC_UNKNOWNPROTO);
301 while ((nconf = __rpc_getconf(handle)) != NULL) {
302 int fd;
303 struct __rpc_sockinfo si;
305 if (nconf->nc_semantics != NC_TPI_CLTS)
306 continue;
307 if (fdlistno >= MAXBCAST)
308 break; /* No more slots available */
309 if (!__rpc_nconf2sockinfo(nconf, &si))
310 continue;
312 TAILQ_INIT(&fdlist[fdlistno].nal);
313 if (__rpc_getbroadifs(si.si_af, si.si_proto, si.si_socktype,
314 &fdlist[fdlistno].nal) == 0)
315 continue;
317 fd = _socket(si.si_af, si.si_socktype, si.si_proto);
318 if (fd < 0) {
319 stat = RPC_CANTSEND;
320 continue;
322 fdlist[fdlistno].af = si.si_af;
323 fdlist[fdlistno].proto = si.si_proto;
324 fdlist[fdlistno].fd = fd;
325 fdlist[fdlistno].nconf = nconf;
326 fdlist[fdlistno].asize = __rpc_get_a_size(si.si_af);
327 pfd[fdlistno].events = POLLIN | POLLPRI |
328 POLLRDNORM | POLLRDBAND;
329 pfd[fdlistno].fd = fdlist[fdlistno].fd = fd;
330 fdlist[fdlistno].dsize = __rpc_get_t_size(si.si_af, si.si_proto,
333 if (maxbufsize <= fdlist[fdlistno].dsize)
334 maxbufsize = fdlist[fdlistno].dsize;
336 #ifdef PORTMAP
337 if (si.si_af == AF_INET && si.si_proto == IPPROTO_UDP) {
338 udpbufsz = fdlist[fdlistno].dsize;
339 if ((outbuf_pmap = malloc(udpbufsz)) == NULL) {
340 _close(fd);
341 stat = RPC_SYSTEMERROR;
342 goto done_broad;
344 pmap_flag = 1;
346 #endif /* PORTMAP */
347 fdlistno++;
350 if (fdlistno == 0) {
351 if (stat == RPC_SUCCESS)
352 stat = RPC_UNKNOWNPROTO;
353 goto done_broad;
355 if (maxbufsize == 0) {
356 if (stat == RPC_SUCCESS)
357 stat = RPC_CANTSEND;
358 goto done_broad;
360 inbuf = malloc(maxbufsize);
361 outbuf = malloc(maxbufsize);
362 if ((inbuf == NULL) || (outbuf == NULL)) {
363 stat = RPC_SYSTEMERROR;
364 goto done_broad;
367 /* Serialize all the arguments which have to be sent */
368 gettimeofday(&t, NULL);
369 msg.rm_xid = __RPC_GETXID(&t);
370 msg.rm_direction = CALL;
371 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
372 msg.rm_call.cb_prog = RPCBPROG;
373 msg.rm_call.cb_vers = RPCBVERS;
374 msg.rm_call.cb_proc = RPCBPROC_CALLIT;
375 barg.prog = prog;
376 barg.vers = vers;
377 barg.proc = proc;
378 barg.args.args_val = argsp;
379 barg.xdr_args = xargs;
380 bres.addr = uaddrp;
381 bres.results.results_val = resultsp;
382 bres.xdr_res = xresults;
383 msg.rm_call.cb_cred = sys_auth->ah_cred;
384 msg.rm_call.cb_verf = sys_auth->ah_verf;
385 xdrmem_create(xdrs, outbuf, maxbufsize, XDR_ENCODE);
386 if ((!xdr_callmsg(xdrs, &msg)) ||
387 (!xdr_rpcb_rmtcallargs(xdrs,
388 (struct rpcb_rmtcallargs *)(void *)&barg))) {
389 stat = RPC_CANTENCODEARGS;
390 goto done_broad;
392 outlen = xdr_getpos(xdrs);
393 xdr_destroy(xdrs);
395 #ifdef PORTMAP
396 /* Prepare the packet for version 2 PORTMAP */
397 if (pmap_flag) {
398 msg.rm_xid++; /* One way to distinguish */
399 msg.rm_call.cb_prog = PMAPPROG;
400 msg.rm_call.cb_vers = PMAPVERS;
401 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
402 barg_pmap.prog = prog;
403 barg_pmap.vers = vers;
404 barg_pmap.proc = proc;
405 barg_pmap.args_ptr = argsp;
406 barg_pmap.xdr_args = xargs;
407 bres_pmap.port_ptr = &port;
408 bres_pmap.xdr_results = xresults;
409 bres_pmap.results_ptr = resultsp;
410 xdrmem_create(xdrs, outbuf_pmap, udpbufsz, XDR_ENCODE);
411 if ((! xdr_callmsg(xdrs, &msg)) ||
412 (! xdr_rmtcall_args(xdrs, &barg_pmap))) {
413 stat = RPC_CANTENCODEARGS;
414 goto done_broad;
416 outlen_pmap = xdr_getpos(xdrs);
417 xdr_destroy(xdrs);
419 #endif /* PORTMAP */
422 * Basic loop: broadcast the packets to transports which
423 * support data packets of size such that one can encode
424 * all the arguments.
425 * Wait a while for response(s).
426 * The response timeout grows larger per iteration.
428 for (msec = inittime; msec <= waittime; msec += msec) {
429 struct broadif *bip;
431 /* Broadcast all the packets now */
432 for (i = 0; i < fdlistno; i++) {
433 if (fdlist[i].dsize < outlen) {
434 stat = RPC_CANTSEND;
435 continue;
437 for (bip = TAILQ_FIRST(&fdlist[i].nal); bip != NULL;
438 bip = TAILQ_NEXT(bip, link)) {
439 void *addr;
441 addr = &bip->broadaddr;
443 __rpc_broadenable(fdlist[i].af, fdlist[i].fd,
444 bip);
447 * Only use version 3 if lowvers is not set
450 if (!__rpc_lowvers)
451 if (_sendto(fdlist[i].fd, outbuf,
452 outlen, 0, (struct sockaddr*)addr,
453 (size_t)fdlist[i].asize) !=
454 outlen) {
455 #ifdef RPC_DEBUG
456 perror("sendto");
457 #endif
458 warnx("clnt_bcast: cannot send"
459 "broadcast packet");
460 stat = RPC_CANTSEND;
461 continue;
463 #ifdef RPC_DEBUG
464 if (!__rpc_lowvers)
465 fprintf(stderr, "Broadcast packet sent "
466 "for %s\n",
467 fdlist[i].nconf->nc_netid);
468 #endif
469 #ifdef PORTMAP
471 * Send the version 2 packet also
472 * for UDP/IP
474 if (pmap_flag &&
475 fdlist[i].proto == IPPROTO_UDP) {
476 if (_sendto(fdlist[i].fd, outbuf_pmap,
477 outlen_pmap, 0, addr,
478 (size_t)fdlist[i].asize) !=
479 outlen_pmap) {
480 warnx("clnt_bcast: "
481 "Cannot send broadcast packet");
482 stat = RPC_CANTSEND;
483 continue;
486 #ifdef RPC_DEBUG
487 fprintf(stderr, "PMAP Broadcast packet "
488 "sent for %s\n",
489 fdlist[i].nconf->nc_netid);
490 #endif
491 #endif /* PORTMAP */
493 /* End for sending all packets on this transport */
494 } /* End for sending on all transports */
496 if (eachresult == NULL) {
497 stat = RPC_SUCCESS;
498 goto done_broad;
502 * Get all the replies from these broadcast requests
504 recv_again:
506 switch (pollretval = _poll(pfd, fdlistno, msec)) {
507 case 0: /* timed out */
508 stat = RPC_TIMEDOUT;
509 continue;
510 case -1: /* some kind of error - we ignore it */
511 goto recv_again;
512 } /* end of poll results switch */
514 for (i = fds_found = 0;
515 i < fdlistno && fds_found < pollretval; i++) {
516 bool_t done = FALSE;
518 if (pfd[i].revents == 0)
519 continue;
520 else if (pfd[i].revents & POLLNVAL) {
522 * Something bad has happened to this descri-
523 * ptor. We can cause _poll() to ignore
524 * it simply by using a negative fd. We do that
525 * rather than compacting the pfd[] and fdlist[]
526 * arrays.
528 pfd[i].fd = -1;
529 fds_found++;
530 continue;
531 } else
532 fds_found++;
533 #ifdef RPC_DEBUG
534 fprintf(stderr, "response for %s\n",
535 fdlist[i].nconf->nc_netid);
536 #endif
537 try_again:
538 inlen = _recvfrom(fdlist[i].fd, inbuf, fdlist[i].dsize,
539 0, (struct sockaddr *)(void *)&fdlist[i].raddr,
540 &fdlist[i].asize);
541 if (inlen < 0) {
542 if (errno == EINTR)
543 goto try_again;
544 warnx("clnt_bcast: Cannot receive reply to "
545 "broadcast");
546 stat = RPC_CANTRECV;
547 continue;
549 if (inlen < sizeof (u_int32_t))
550 continue; /* Drop that and go ahead */
552 * see if reply transaction id matches sent id.
553 * If so, decode the results. If return id is xid + 1
554 * it was a PORTMAP reply
556 if (*((u_int32_t *)(void *)(inbuf)) ==
557 *((u_int32_t *)(void *)(outbuf))) {
558 pmap_reply_flag = 0;
559 msg.acpted_rply.ar_verf = _null_auth;
560 msg.acpted_rply.ar_results.where =
561 (caddr_t)(void *)&bres;
562 msg.acpted_rply.ar_results.proc =
563 (xdrproc_t)xdr_rpcb_rmtcallres;
564 #ifdef PORTMAP
565 } else if (pmap_flag &&
566 *((u_int32_t *)(void *)(inbuf)) ==
567 *((u_int32_t *)(void *)(outbuf_pmap))) {
568 pmap_reply_flag = 1;
569 msg.acpted_rply.ar_verf = _null_auth;
570 msg.acpted_rply.ar_results.where =
571 (caddr_t)(void *)&bres_pmap;
572 msg.acpted_rply.ar_results.proc =
573 (xdrproc_t)xdr_rmtcallres;
574 #endif /* PORTMAP */
575 } else
576 continue;
577 xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
578 if (xdr_replymsg(xdrs, &msg)) {
579 if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
580 (msg.acpted_rply.ar_stat == SUCCESS)) {
581 struct netbuf taddr, *np;
582 struct sockaddr_in *sin;
584 #ifdef PORTMAP
585 if (pmap_flag && pmap_reply_flag) {
586 sin = (struct sockaddr_in *)
587 (void *)&fdlist[i].raddr;
588 sin->sin_port =
589 htons((u_short)port);
590 taddr.len = taddr.maxlen =
591 fdlist[i].raddr.ss_len;
592 taddr.buf = &fdlist[i].raddr;
593 done = (*eachresult)(resultsp,
594 &taddr, fdlist[i].nconf);
595 } else {
596 #endif /* PORTMAP */
597 #ifdef RPC_DEBUG
598 fprintf(stderr, "uaddr %s\n",
599 uaddrp);
600 #endif
601 np = uaddr2taddr(
602 fdlist[i].nconf, uaddrp);
603 done = (*eachresult)(resultsp,
604 np, fdlist[i].nconf);
605 free(np);
606 #ifdef PORTMAP
608 #endif /* PORTMAP */
610 /* otherwise, we just ignore the errors ... */
612 /* else some kind of deserialization problem ... */
614 xdrs->x_op = XDR_FREE;
615 msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
616 xdr_replymsg(xdrs, &msg);
617 (*xresults)(xdrs, resultsp);
618 XDR_DESTROY(xdrs);
619 if (done) {
620 stat = RPC_SUCCESS;
621 goto done_broad;
622 } else {
623 goto recv_again;
625 } /* The recv for loop */
626 } /* The giant for loop */
628 done_broad:
629 if (inbuf)
630 free(inbuf);
631 if (outbuf)
632 free(outbuf);
633 #ifdef PORTMAP
634 if (outbuf_pmap)
635 free(outbuf_pmap);
636 #endif /* PORTMAP */
637 for (i = 0; i < fdlistno; i++) {
638 _close(fdlist[i].fd);
639 __rpc_freebroadifs(&fdlist[i].nal);
641 AUTH_DESTROY(sys_auth);
642 __rpc_endconf(handle);
644 return (stat);
648 enum clnt_stat
649 rpc_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp,
650 eachresult, nettype)
651 rpcprog_t prog; /* program number */
652 rpcvers_t vers; /* version number */
653 rpcproc_t proc; /* procedure number */
654 xdrproc_t xargs; /* xdr routine for args */
655 caddr_t argsp; /* pointer to args */
656 xdrproc_t xresults; /* xdr routine for results */
657 caddr_t resultsp; /* pointer to results */
658 resultproc_t eachresult; /* call with each result obtained */
659 const char *nettype; /* transport type */
661 enum clnt_stat dummy;
663 dummy = rpc_broadcast_exp(prog, vers, proc, xargs, argsp,
664 xresults, resultsp, eachresult,
665 INITTIME, WAITTIME, nettype);
666 return (dummy);