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