1*84d9c625SLionel Sambuc /* $NetBSD: clnt_bcast.c,v 1.26 2013/03/11 20:19:28 tron Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
4*84d9c625SLionel Sambuc * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras *
6*84d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
7*84d9c625SLionel Sambuc * modification, are permitted provided that the following conditions are
8*84d9c625SLionel Sambuc * met:
92fe8fb19SBen Gras *
10*84d9c625SLionel Sambuc * * Redistributions of source code must retain the above copyright
11*84d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*84d9c625SLionel Sambuc * * Redistributions in binary form must reproduce the above
13*84d9c625SLionel Sambuc * copyright notice, this list of conditions and the following
14*84d9c625SLionel Sambuc * disclaimer in the documentation and/or other materials
15*84d9c625SLionel Sambuc * provided with the distribution.
16*84d9c625SLionel Sambuc * * Neither the name of the "Oracle America, Inc." nor the names of its
17*84d9c625SLionel Sambuc * contributors may be used to endorse or promote products derived
18*84d9c625SLionel Sambuc * from this software without specific prior written permission.
192fe8fb19SBen Gras *
20*84d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*84d9c625SLionel Sambuc * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*84d9c625SLionel Sambuc * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*84d9c625SLionel Sambuc * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*84d9c625SLionel Sambuc * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*84d9c625SLionel Sambuc * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*84d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*84d9c625SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*84d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*84d9c625SLionel Sambuc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*84d9c625SLionel Sambuc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*84d9c625SLionel Sambuc * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras */
332fe8fb19SBen Gras /*
342fe8fb19SBen Gras * Copyright (c) 1986-1991 by Sun Microsystems Inc.
352fe8fb19SBen Gras */
362fe8fb19SBen Gras
372fe8fb19SBen Gras /* #ident "@(#)clnt_bcast.c 1.18 94/05/03 SMI" */
382fe8fb19SBen Gras
392fe8fb19SBen Gras #include <sys/cdefs.h>
402fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
412fe8fb19SBen Gras #if 0
422fe8fb19SBen Gras static char sccsid[] = "@(#)clnt_bcast.c 1.15 89/04/21 Copyr 1988 Sun Micro";
432fe8fb19SBen Gras #else
44*84d9c625SLionel Sambuc __RCSID("$NetBSD: clnt_bcast.c,v 1.26 2013/03/11 20:19:28 tron Exp $");
452fe8fb19SBen Gras #endif
462fe8fb19SBen Gras #endif
472fe8fb19SBen Gras
482fe8fb19SBen Gras /*
492fe8fb19SBen Gras * clnt_bcast.c
502fe8fb19SBen Gras * Client interface to broadcast service.
512fe8fb19SBen Gras *
522fe8fb19SBen Gras * Copyright (C) 1988, Sun Microsystems, Inc.
532fe8fb19SBen Gras *
542fe8fb19SBen Gras * The following is kludged-up support for simple rpc broadcasts.
552fe8fb19SBen Gras * Someday a large, complicated system will replace these routines.
562fe8fb19SBen Gras */
572fe8fb19SBen Gras
582fe8fb19SBen Gras #include "namespace.h"
592fe8fb19SBen Gras #include <sys/types.h>
602fe8fb19SBen Gras #include <sys/socket.h>
612fe8fb19SBen Gras #include <sys/queue.h>
622fe8fb19SBen Gras #include <net/if.h>
632fe8fb19SBen Gras #include <netinet/in.h>
642fe8fb19SBen Gras #include <ifaddrs.h>
652fe8fb19SBen Gras #include <sys/poll.h>
662fe8fb19SBen Gras #include <rpc/rpc.h>
672fe8fb19SBen Gras #ifdef PORTMAP
682fe8fb19SBen Gras #include <rpc/pmap_prot.h>
692fe8fb19SBen Gras #include <rpc/pmap_clnt.h>
702fe8fb19SBen Gras #include <rpc/pmap_rmt.h>
712fe8fb19SBen Gras #endif
722fe8fb19SBen Gras #include <rpc/nettype.h>
732fe8fb19SBen Gras #include <arpa/inet.h>
742fe8fb19SBen Gras #ifdef RPC_DEBUG
752fe8fb19SBen Gras #include <stdio.h>
762fe8fb19SBen Gras #endif
772fe8fb19SBen Gras #include <assert.h>
782fe8fb19SBen Gras #include <errno.h>
792fe8fb19SBen Gras #include <stdlib.h>
802fe8fb19SBen Gras #include <unistd.h>
812fe8fb19SBen Gras #include <netdb.h>
822fe8fb19SBen Gras #include <err.h>
832fe8fb19SBen Gras #include <string.h>
842fe8fb19SBen Gras
852fe8fb19SBen Gras #include "rpc_internal.h"
86*84d9c625SLionel Sambuc #include "svc_fdset.h"
872fe8fb19SBen Gras
882fe8fb19SBen Gras #define MAXBCAST 20 /* Max no of broadcasting transports */
892fe8fb19SBen Gras #define INITTIME 4000 /* Time to wait initially */
902fe8fb19SBen Gras #define WAITTIME 8000 /* Maximum time to wait */
912fe8fb19SBen Gras
922fe8fb19SBen Gras /*
932fe8fb19SBen Gras * If nettype is NULL, it broadcasts on all the available
942fe8fb19SBen Gras * datagram_n transports. May potentially lead to broadacst storms
952fe8fb19SBen Gras * and hence should be used with caution, care and courage.
962fe8fb19SBen Gras *
972fe8fb19SBen Gras * The current parameter xdr packet size is limited by the max tsdu
982fe8fb19SBen Gras * size of the transport. If the max tsdu size of any transport is
992fe8fb19SBen Gras * smaller than the parameter xdr packet, then broadcast is not
1002fe8fb19SBen Gras * sent on that transport.
1012fe8fb19SBen Gras *
1022fe8fb19SBen Gras * Also, the packet size should be less the packet size of
1032fe8fb19SBen Gras * the data link layer (for ethernet it is 1400 bytes). There is
1042fe8fb19SBen Gras * no easy way to find out the max size of the data link layer and
1052fe8fb19SBen Gras * we are assuming that the args would be smaller than that.
1062fe8fb19SBen Gras *
1072fe8fb19SBen Gras * The result size has to be smaller than the transport tsdu size.
1082fe8fb19SBen Gras *
1092fe8fb19SBen Gras * If PORTMAP has been defined, we send two packets for UDP, one for
1102fe8fb19SBen Gras * rpcbind and one for portmap. For those machines which support
1112fe8fb19SBen Gras * both rpcbind and portmap, it will cause them to reply twice, and
1122fe8fb19SBen Gras * also here it will get two responses ... inefficient and clumsy.
1132fe8fb19SBen Gras */
1142fe8fb19SBen Gras
1152fe8fb19SBen Gras #ifdef __weak_alias
1162fe8fb19SBen Gras __weak_alias(rpc_broadcast_exp,_rpc_broadcast_exp)
1172fe8fb19SBen Gras __weak_alias(rpc_broadcast,_rpc_broadcast)
1182fe8fb19SBen Gras #endif
1192fe8fb19SBen Gras
1202fe8fb19SBen Gras struct broadif {
1212fe8fb19SBen Gras int index;
1222fe8fb19SBen Gras struct sockaddr_storage broadaddr;
1232fe8fb19SBen Gras TAILQ_ENTRY(broadif) link;
1242fe8fb19SBen Gras };
1252fe8fb19SBen Gras
1262fe8fb19SBen Gras typedef TAILQ_HEAD(, broadif) broadlist_t;
1272fe8fb19SBen Gras
128f14fb602SLionel Sambuc int __rpc_getbroadifs(int, int, int, broadlist_t *);
129f14fb602SLionel Sambuc void __rpc_freebroadifs(broadlist_t *);
130f14fb602SLionel Sambuc int __rpc_broadenable(int, int, struct broadif *);
1312fe8fb19SBen Gras
1322fe8fb19SBen Gras int __rpc_lowvers = 0;
1332fe8fb19SBen Gras
1342fe8fb19SBen Gras int
__rpc_getbroadifs(int af,int proto,int socktype,broadlist_t * list)1352fe8fb19SBen Gras __rpc_getbroadifs(int af, int proto, int socktype, broadlist_t *list)
1362fe8fb19SBen Gras {
1372fe8fb19SBen Gras int count = 0;
1382fe8fb19SBen Gras struct broadif *bip;
1392fe8fb19SBen Gras struct ifaddrs *ifap, *ifp;
1402fe8fb19SBen Gras #ifdef INET6
1412fe8fb19SBen Gras struct sockaddr_in6 *sin6;
1422fe8fb19SBen Gras #endif
1432fe8fb19SBen Gras struct sockaddr_in *gbsin;
1442fe8fb19SBen Gras struct addrinfo hints, *res;
1452fe8fb19SBen Gras
1462fe8fb19SBen Gras _DIAGASSERT(list != NULL);
1472fe8fb19SBen Gras
1482fe8fb19SBen Gras if (getifaddrs(&ifp) < 0)
1492fe8fb19SBen Gras return 0;
1502fe8fb19SBen Gras
1512fe8fb19SBen Gras memset(&hints, 0, sizeof hints);
1522fe8fb19SBen Gras
1532fe8fb19SBen Gras hints.ai_family = af;
1542fe8fb19SBen Gras hints.ai_protocol = proto;
1552fe8fb19SBen Gras hints.ai_socktype = socktype;
1562fe8fb19SBen Gras
1572fe8fb19SBen Gras if (getaddrinfo(NULL, "sunrpc", &hints, &res) != 0) {
1582fe8fb19SBen Gras freeifaddrs(ifp);
1592fe8fb19SBen Gras return 0;
1602fe8fb19SBen Gras }
1612fe8fb19SBen Gras
1622fe8fb19SBen Gras for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
1632fe8fb19SBen Gras if (ifap->ifa_addr->sa_family != af ||
1642fe8fb19SBen Gras !(ifap->ifa_flags & IFF_UP))
1652fe8fb19SBen Gras continue;
1662fe8fb19SBen Gras bip = malloc(sizeof(*bip));
1672fe8fb19SBen Gras if (bip == NULL)
1682fe8fb19SBen Gras break;
1692fe8fb19SBen Gras bip->index = if_nametoindex(ifap->ifa_name);
1702fe8fb19SBen Gras if (
1712fe8fb19SBen Gras #ifdef INET6
1722fe8fb19SBen Gras af != AF_INET6 &&
1732fe8fb19SBen Gras #endif
1742fe8fb19SBen Gras (ifap->ifa_flags & IFF_BROADCAST) &&
1752fe8fb19SBen Gras ifap->ifa_broadaddr) {
1762fe8fb19SBen Gras memcpy(&bip->broadaddr, ifap->ifa_broadaddr,
1772fe8fb19SBen Gras (size_t)ifap->ifa_broadaddr->sa_len);
1782fe8fb19SBen Gras gbsin = (struct sockaddr_in *)(void *)&bip->broadaddr;
1792fe8fb19SBen Gras gbsin->sin_port =
1802fe8fb19SBen Gras ((struct sockaddr_in *)
1812fe8fb19SBen Gras (void *)res->ai_addr)->sin_port;
1822fe8fb19SBen Gras } else
1832fe8fb19SBen Gras #ifdef INET6
1842fe8fb19SBen Gras if (af == AF_INET6 && (ifap->ifa_flags & IFF_MULTICAST)) {
1852fe8fb19SBen Gras sin6 = (struct sockaddr_in6 *)(void *)&bip->broadaddr;
1862fe8fb19SBen Gras inet_pton(af, RPCB_MULTICAST_ADDR, &sin6->sin6_addr);
1872fe8fb19SBen Gras sin6->sin6_family = af;
1882fe8fb19SBen Gras sin6->sin6_len = sizeof *sin6;
1892fe8fb19SBen Gras sin6->sin6_port =
1902fe8fb19SBen Gras ((struct sockaddr_in6 *)
1912fe8fb19SBen Gras (void *)res->ai_addr)->sin6_port;
1922fe8fb19SBen Gras sin6->sin6_scope_id = bip->index;
1932fe8fb19SBen Gras } else
1942fe8fb19SBen Gras #endif
1952fe8fb19SBen Gras {
1962fe8fb19SBen Gras free(bip);
1972fe8fb19SBen Gras continue;
1982fe8fb19SBen Gras }
1992fe8fb19SBen Gras TAILQ_INSERT_TAIL(list, bip, link);
2002fe8fb19SBen Gras count++;
2012fe8fb19SBen Gras }
2022fe8fb19SBen Gras freeifaddrs(ifp);
2032fe8fb19SBen Gras freeaddrinfo(res);
2042fe8fb19SBen Gras
2052fe8fb19SBen Gras return count;
2062fe8fb19SBen Gras }
2072fe8fb19SBen Gras
2082fe8fb19SBen Gras void
__rpc_freebroadifs(broadlist_t * list)2092fe8fb19SBen Gras __rpc_freebroadifs(broadlist_t *list)
2102fe8fb19SBen Gras {
2112fe8fb19SBen Gras struct broadif *bip, *next;
2122fe8fb19SBen Gras
2132fe8fb19SBen Gras _DIAGASSERT(list != NULL);
2142fe8fb19SBen Gras
2152fe8fb19SBen Gras bip = TAILQ_FIRST(list);
2162fe8fb19SBen Gras
2172fe8fb19SBen Gras while (bip != NULL) {
2182fe8fb19SBen Gras next = TAILQ_NEXT(bip, link);
2192fe8fb19SBen Gras free(bip);
2202fe8fb19SBen Gras bip = next;
2212fe8fb19SBen Gras }
2222fe8fb19SBen Gras }
2232fe8fb19SBen Gras
2242fe8fb19SBen Gras int
2252fe8fb19SBen Gras /*ARGSUSED*/
__rpc_broadenable(int af,int s,struct broadif * bip)2262fe8fb19SBen Gras __rpc_broadenable(int af, int s, struct broadif *bip)
2272fe8fb19SBen Gras {
2282fe8fb19SBen Gras int o = 1;
2292fe8fb19SBen Gras
2302fe8fb19SBen Gras #if 0
2312fe8fb19SBen Gras _DIAGASSERT(bip != NULL);
2322fe8fb19SBen Gras
2332fe8fb19SBen Gras if (af == AF_INET6) {
2342fe8fb19SBen Gras fprintf(stderr, "set v6 multicast if to %d\n", bip->index);
2352fe8fb19SBen Gras if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, &bip->index,
2362fe8fb19SBen Gras sizeof bip->index) < 0)
2372fe8fb19SBen Gras return -1;
2382fe8fb19SBen Gras } else
2392fe8fb19SBen Gras #endif
240f14fb602SLionel Sambuc if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &o,
241f14fb602SLionel Sambuc (socklen_t)sizeof(o)) == -1)
2422fe8fb19SBen Gras return -1;
2432fe8fb19SBen Gras
2442fe8fb19SBen Gras return 0;
2452fe8fb19SBen Gras }
2462fe8fb19SBen Gras
2472fe8fb19SBen Gras
2482fe8fb19SBen Gras enum clnt_stat
rpc_broadcast_exp(rpcprog_t prog,rpcvers_t vers,rpcproc_t proc,xdrproc_t xargs,const char * argsp,xdrproc_t xresults,caddr_t resultsp,resultproc_t eachresult,int inittime,int waittime,const char * nettype)249f14fb602SLionel Sambuc rpc_broadcast_exp(
250f14fb602SLionel Sambuc rpcprog_t prog, /* program number */
251f14fb602SLionel Sambuc rpcvers_t vers, /* version number */
252f14fb602SLionel Sambuc rpcproc_t proc, /* procedure number */
253f14fb602SLionel Sambuc xdrproc_t xargs, /* xdr routine for args */
254f14fb602SLionel Sambuc const char * argsp, /* pointer to args */
255f14fb602SLionel Sambuc xdrproc_t xresults, /* xdr routine for results */
256f14fb602SLionel Sambuc caddr_t resultsp, /* pointer to results */
257f14fb602SLionel Sambuc resultproc_t eachresult, /* call with each result obtained */
258f14fb602SLionel Sambuc int inittime, /* how long to wait initially */
259f14fb602SLionel Sambuc int waittime, /* maximum time to wait */
260f14fb602SLionel Sambuc const char * nettype) /* transport type */
2612fe8fb19SBen Gras {
2622fe8fb19SBen Gras enum clnt_stat stat = RPC_SUCCESS; /* Return status */
2632fe8fb19SBen Gras XDR xdr_stream; /* XDR stream */
2642fe8fb19SBen Gras XDR *xdrs = &xdr_stream;
2652fe8fb19SBen Gras struct rpc_msg msg; /* RPC message */
2662fe8fb19SBen Gras char *outbuf = NULL; /* Broadcast msg buffer */
2672fe8fb19SBen Gras char *inbuf = NULL; /* Reply buf */
2682fe8fb19SBen Gras ssize_t inlen;
2692fe8fb19SBen Gras u_int maxbufsize = 0;
2702fe8fb19SBen Gras AUTH *sys_auth = authunix_create_default();
2712fe8fb19SBen Gras size_t i;
2722fe8fb19SBen Gras void *handle;
2732fe8fb19SBen Gras char uaddress[1024]; /* A self imposed limit */
2742fe8fb19SBen Gras char *uaddrp = uaddress;
2752fe8fb19SBen Gras int pmap_reply_flag; /* reply recvd from PORTMAP */
2762fe8fb19SBen Gras /* An array of all the suitable broadcast transports */
2772fe8fb19SBen Gras struct {
2782fe8fb19SBen Gras int fd; /* File descriptor */
2792fe8fb19SBen Gras int af;
2802fe8fb19SBen Gras int proto;
2812fe8fb19SBen Gras struct netconfig *nconf; /* Netconfig structure */
2822fe8fb19SBen Gras u_int asize; /* Size of the addr buf */
2832fe8fb19SBen Gras u_int dsize; /* Size of the data buf */
2842fe8fb19SBen Gras struct sockaddr_storage raddr; /* Remote address */
2852fe8fb19SBen Gras broadlist_t nal;
2862fe8fb19SBen Gras } fdlist[MAXBCAST];
2872fe8fb19SBen Gras struct pollfd pfd[MAXBCAST];
288f14fb602SLionel Sambuc nfds_t fdlistno = 0;
2892fe8fb19SBen Gras struct r_rpcb_rmtcallargs barg; /* Remote arguments */
2902fe8fb19SBen Gras struct r_rpcb_rmtcallres bres; /* Remote results */
2912fe8fb19SBen Gras size_t outlen;
2922fe8fb19SBen Gras struct netconfig *nconf;
2932fe8fb19SBen Gras int msec;
2942fe8fb19SBen Gras int pollretval;
2952fe8fb19SBen Gras int fds_found;
2962fe8fb19SBen Gras struct timespec ts;
2972fe8fb19SBen Gras
2982fe8fb19SBen Gras #ifdef PORTMAP
2992fe8fb19SBen Gras size_t outlen_pmap = 0;
3002fe8fb19SBen Gras u_long port; /* Remote port number */
3012fe8fb19SBen Gras int pmap_flag = 0; /* UDP exists ? */
3022fe8fb19SBen Gras char *outbuf_pmap = NULL;
3032fe8fb19SBen Gras struct rmtcallargs barg_pmap; /* Remote arguments */
3042fe8fb19SBen Gras struct rmtcallres bres_pmap; /* Remote results */
3052fe8fb19SBen Gras u_int udpbufsz = 0;
3062fe8fb19SBen Gras #endif /* PORTMAP */
3072fe8fb19SBen Gras
3082fe8fb19SBen Gras if (sys_auth == NULL) {
3092fe8fb19SBen Gras return (RPC_SYSTEMERROR);
3102fe8fb19SBen Gras }
3112fe8fb19SBen Gras /*
3122fe8fb19SBen Gras * initialization: create a fd, a broadcast address, and send the
3132fe8fb19SBen Gras * request on the broadcast transport.
3142fe8fb19SBen Gras * Listen on all of them and on replies, call the user supplied
3152fe8fb19SBen Gras * function.
3162fe8fb19SBen Gras */
3172fe8fb19SBen Gras
3182fe8fb19SBen Gras if (nettype == NULL)
3192fe8fb19SBen Gras nettype = "datagram_n";
3202fe8fb19SBen Gras if ((handle = __rpc_setconf(nettype)) == NULL) {
3212fe8fb19SBen Gras AUTH_DESTROY(sys_auth);
3222fe8fb19SBen Gras return (RPC_UNKNOWNPROTO);
3232fe8fb19SBen Gras }
3242fe8fb19SBen Gras while ((nconf = __rpc_getconf(handle)) != NULL) {
3252fe8fb19SBen Gras int fd;
3262fe8fb19SBen Gras struct __rpc_sockinfo si;
3272fe8fb19SBen Gras
3282fe8fb19SBen Gras if (nconf->nc_semantics != NC_TPI_CLTS)
3292fe8fb19SBen Gras continue;
3302fe8fb19SBen Gras if (fdlistno >= MAXBCAST)
3312fe8fb19SBen Gras break; /* No more slots available */
3322fe8fb19SBen Gras if (!__rpc_nconf2sockinfo(nconf, &si))
3332fe8fb19SBen Gras continue;
3342fe8fb19SBen Gras
3352fe8fb19SBen Gras TAILQ_INIT(&fdlist[fdlistno].nal);
3362fe8fb19SBen Gras if (__rpc_getbroadifs(si.si_af, si.si_proto, si.si_socktype,
3372fe8fb19SBen Gras &fdlist[fdlistno].nal) == 0)
3382fe8fb19SBen Gras continue;
3392fe8fb19SBen Gras
3402fe8fb19SBen Gras fd = socket(si.si_af, si.si_socktype, si.si_proto);
3412fe8fb19SBen Gras if (fd < 0) {
3422fe8fb19SBen Gras stat = RPC_CANTSEND;
3432fe8fb19SBen Gras continue;
3442fe8fb19SBen Gras }
3452fe8fb19SBen Gras fdlist[fdlistno].af = si.si_af;
3462fe8fb19SBen Gras fdlist[fdlistno].proto = si.si_proto;
3472fe8fb19SBen Gras fdlist[fdlistno].fd = fd;
3482fe8fb19SBen Gras fdlist[fdlistno].nconf = nconf;
3492fe8fb19SBen Gras fdlist[fdlistno].asize = __rpc_get_a_size(si.si_af);
3502fe8fb19SBen Gras pfd[fdlistno].events = POLLIN | POLLPRI |
3512fe8fb19SBen Gras POLLRDNORM | POLLRDBAND;
3522fe8fb19SBen Gras pfd[fdlistno].fd = fdlist[fdlistno].fd = fd;
3532fe8fb19SBen Gras fdlist[fdlistno].dsize = __rpc_get_t_size(si.si_af, si.si_proto,
3542fe8fb19SBen Gras 0);
3552fe8fb19SBen Gras
3562fe8fb19SBen Gras if (maxbufsize <= fdlist[fdlistno].dsize)
3572fe8fb19SBen Gras maxbufsize = fdlist[fdlistno].dsize;
3582fe8fb19SBen Gras
3592fe8fb19SBen Gras #ifdef PORTMAP
3602fe8fb19SBen Gras if (si.si_af == AF_INET && si.si_proto == IPPROTO_UDP) {
3612fe8fb19SBen Gras udpbufsz = fdlist[fdlistno].dsize;
3622fe8fb19SBen Gras if ((outbuf_pmap = malloc(udpbufsz)) == NULL) {
3632fe8fb19SBen Gras close(fd);
3642fe8fb19SBen Gras stat = RPC_SYSTEMERROR;
3652fe8fb19SBen Gras goto done_broad;
3662fe8fb19SBen Gras }
3672fe8fb19SBen Gras pmap_flag = 1;
3682fe8fb19SBen Gras }
3692fe8fb19SBen Gras #endif
3702fe8fb19SBen Gras fdlistno++;
3712fe8fb19SBen Gras }
3722fe8fb19SBen Gras
3732fe8fb19SBen Gras if (fdlistno == 0) {
3742fe8fb19SBen Gras if (stat == RPC_SUCCESS)
3752fe8fb19SBen Gras stat = RPC_UNKNOWNPROTO;
3762fe8fb19SBen Gras goto done_broad;
3772fe8fb19SBen Gras }
3782fe8fb19SBen Gras if (maxbufsize == 0) {
3792fe8fb19SBen Gras if (stat == RPC_SUCCESS)
3802fe8fb19SBen Gras stat = RPC_CANTSEND;
3812fe8fb19SBen Gras goto done_broad;
3822fe8fb19SBen Gras }
3832fe8fb19SBen Gras inbuf = malloc(maxbufsize);
3842fe8fb19SBen Gras outbuf = malloc(maxbufsize);
3852fe8fb19SBen Gras if ((inbuf == NULL) || (outbuf == NULL)) {
3862fe8fb19SBen Gras stat = RPC_SYSTEMERROR;
3872fe8fb19SBen Gras goto done_broad;
3882fe8fb19SBen Gras }
3892fe8fb19SBen Gras
3902fe8fb19SBen Gras /* Serialize all the arguments which have to be sent */
3912fe8fb19SBen Gras msg.rm_xid = __RPC_GETXID();
3922fe8fb19SBen Gras msg.rm_direction = CALL;
3932fe8fb19SBen Gras msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
3942fe8fb19SBen Gras msg.rm_call.cb_prog = RPCBPROG;
3952fe8fb19SBen Gras msg.rm_call.cb_vers = RPCBVERS;
3962fe8fb19SBen Gras msg.rm_call.cb_proc = RPCBPROC_CALLIT;
3972fe8fb19SBen Gras barg.prog = prog;
3982fe8fb19SBen Gras barg.vers = vers;
3992fe8fb19SBen Gras barg.proc = proc;
4002fe8fb19SBen Gras barg.args.args_val = argsp;
4012fe8fb19SBen Gras barg.xdr_args = xargs;
4022fe8fb19SBen Gras bres.addr = uaddrp;
4032fe8fb19SBen Gras bres.results.results_val = resultsp;
4042fe8fb19SBen Gras bres.xdr_res = xresults;
4052fe8fb19SBen Gras msg.rm_call.cb_cred = sys_auth->ah_cred;
4062fe8fb19SBen Gras msg.rm_call.cb_verf = sys_auth->ah_verf;
4072fe8fb19SBen Gras xdrmem_create(xdrs, outbuf, maxbufsize, XDR_ENCODE);
4082fe8fb19SBen Gras if ((!xdr_callmsg(xdrs, &msg)) ||
4092fe8fb19SBen Gras (!xdr_rpcb_rmtcallargs(xdrs,
4102fe8fb19SBen Gras (struct rpcb_rmtcallargs *)(void *)&barg))) {
4112fe8fb19SBen Gras stat = RPC_CANTENCODEARGS;
4122fe8fb19SBen Gras goto done_broad;
4132fe8fb19SBen Gras }
4142fe8fb19SBen Gras outlen = xdr_getpos(xdrs);
4152fe8fb19SBen Gras xdr_destroy(xdrs);
4162fe8fb19SBen Gras
4172fe8fb19SBen Gras #ifdef PORTMAP
4182fe8fb19SBen Gras /* Prepare the packet for version 2 PORTMAP */
4192fe8fb19SBen Gras if (pmap_flag) {
4202fe8fb19SBen Gras msg.rm_xid++; /* One way to distinguish */
4212fe8fb19SBen Gras msg.rm_call.cb_prog = PMAPPROG;
4222fe8fb19SBen Gras msg.rm_call.cb_vers = PMAPVERS;
4232fe8fb19SBen Gras msg.rm_call.cb_proc = PMAPPROC_CALLIT;
4242fe8fb19SBen Gras barg_pmap.prog = prog;
4252fe8fb19SBen Gras barg_pmap.vers = vers;
4262fe8fb19SBen Gras barg_pmap.proc = proc;
4272fe8fb19SBen Gras barg_pmap.args_ptr = argsp;
4282fe8fb19SBen Gras barg_pmap.xdr_args = xargs;
4292fe8fb19SBen Gras bres_pmap.port_ptr = &port;
4302fe8fb19SBen Gras bres_pmap.xdr_results = xresults;
4312fe8fb19SBen Gras bres_pmap.results_ptr = resultsp;
4322fe8fb19SBen Gras xdrmem_create(xdrs, outbuf_pmap, udpbufsz, XDR_ENCODE);
4332fe8fb19SBen Gras if ((! xdr_callmsg(xdrs, &msg)) ||
4342fe8fb19SBen Gras (! xdr_rmtcall_args(xdrs, &barg_pmap))) {
4352fe8fb19SBen Gras stat = RPC_CANTENCODEARGS;
4362fe8fb19SBen Gras goto done_broad;
4372fe8fb19SBen Gras }
4382fe8fb19SBen Gras outlen_pmap = xdr_getpos(xdrs);
4392fe8fb19SBen Gras xdr_destroy(xdrs);
4402fe8fb19SBen Gras }
4412fe8fb19SBen Gras #endif /* PORTMAP */
4422fe8fb19SBen Gras
4432fe8fb19SBen Gras /*
4442fe8fb19SBen Gras * Basic loop: broadcast the packets to transports which
4452fe8fb19SBen Gras * support data packets of size such that one can encode
4462fe8fb19SBen Gras * all the arguments.
4472fe8fb19SBen Gras * Wait a while for response(s).
4482fe8fb19SBen Gras * The response timeout grows larger per iteration.
4492fe8fb19SBen Gras */
4502fe8fb19SBen Gras for (msec = inittime; msec <= waittime; msec += msec) {
4512fe8fb19SBen Gras struct broadif *bip;
4522fe8fb19SBen Gras
4532fe8fb19SBen Gras /* Broadcast all the packets now */
4542fe8fb19SBen Gras for (i = 0; i < fdlistno; i++) {
4552fe8fb19SBen Gras if (fdlist[i].dsize < outlen) {
4562fe8fb19SBen Gras stat = RPC_CANTSEND;
4572fe8fb19SBen Gras continue;
4582fe8fb19SBen Gras }
4592fe8fb19SBen Gras for (bip = TAILQ_FIRST(&fdlist[i].nal); bip != NULL;
4602fe8fb19SBen Gras bip = TAILQ_NEXT(bip, link)) {
4612fe8fb19SBen Gras void *addr;
4622fe8fb19SBen Gras
4632fe8fb19SBen Gras addr = &bip->broadaddr;
4642fe8fb19SBen Gras
4652fe8fb19SBen Gras __rpc_broadenable(fdlist[i].af, fdlist[i].fd,
4662fe8fb19SBen Gras bip);
4672fe8fb19SBen Gras
4682fe8fb19SBen Gras /*
4692fe8fb19SBen Gras * Only use version 3 if lowvers is not set
4702fe8fb19SBen Gras */
4712fe8fb19SBen Gras
4722fe8fb19SBen Gras if (!__rpc_lowvers)
4732fe8fb19SBen Gras if ((size_t)sendto(fdlist[i].fd, outbuf,
4742fe8fb19SBen Gras outlen, 0, (struct sockaddr*)addr,
475f14fb602SLionel Sambuc (socklen_t)fdlist[i].asize) !=
4762fe8fb19SBen Gras outlen) {
4772fe8fb19SBen Gras warn("clnt_bcast: cannot send"
4782fe8fb19SBen Gras " broadcast packet");
4792fe8fb19SBen Gras stat = RPC_CANTSEND;
4802fe8fb19SBen Gras continue;
4812fe8fb19SBen Gras }
4822fe8fb19SBen Gras #ifdef RPC_DEBUG
4832fe8fb19SBen Gras if (!__rpc_lowvers)
4842fe8fb19SBen Gras fprintf(stderr, "Broadcast packet sent "
4852fe8fb19SBen Gras "for %s\n",
4862fe8fb19SBen Gras fdlist[i].nconf->nc_netid);
4872fe8fb19SBen Gras #endif
4882fe8fb19SBen Gras #ifdef PORTMAP
4892fe8fb19SBen Gras /*
4902fe8fb19SBen Gras * Send the version 2 packet also
4912fe8fb19SBen Gras * for UDP/IP
4922fe8fb19SBen Gras */
4932fe8fb19SBen Gras if (pmap_flag &&
4942fe8fb19SBen Gras fdlist[i].proto == IPPROTO_UDP) {
4952fe8fb19SBen Gras if ((size_t)sendto(fdlist[i].fd,
4962fe8fb19SBen Gras outbuf_pmap, outlen_pmap, 0, addr,
497f14fb602SLionel Sambuc (socklen_t)fdlist[i].asize) !=
4982fe8fb19SBen Gras outlen_pmap) {
4992fe8fb19SBen Gras warnx("clnt_bcast: "
5002fe8fb19SBen Gras "Cannot send "
5012fe8fb19SBen Gras "broadcast packet");
5022fe8fb19SBen Gras stat = RPC_CANTSEND;
5032fe8fb19SBen Gras continue;
5042fe8fb19SBen Gras }
5052fe8fb19SBen Gras }
5062fe8fb19SBen Gras #ifdef RPC_DEBUG
5072fe8fb19SBen Gras fprintf(stderr, "PMAP Broadcast packet "
5082fe8fb19SBen Gras "sent for %s\n",
5092fe8fb19SBen Gras fdlist[i].nconf->nc_netid);
5102fe8fb19SBen Gras #endif
5112fe8fb19SBen Gras #endif /* PORTMAP */
5122fe8fb19SBen Gras }
5132fe8fb19SBen Gras /* End for sending all packets on this transport */
5142fe8fb19SBen Gras } /* End for sending on all transports */
5152fe8fb19SBen Gras
5162fe8fb19SBen Gras if (eachresult == NULL) {
5172fe8fb19SBen Gras stat = RPC_SUCCESS;
5182fe8fb19SBen Gras goto done_broad;
5192fe8fb19SBen Gras }
5202fe8fb19SBen Gras
5212fe8fb19SBen Gras /*
5222fe8fb19SBen Gras * Get all the replies from these broadcast requests
5232fe8fb19SBen Gras */
5242fe8fb19SBen Gras recv_again:
5252fe8fb19SBen Gras ts.tv_sec = msec / 1000;
5262fe8fb19SBen Gras ts.tv_nsec = (msec % 1000) * 1000000;
5272fe8fb19SBen Gras
5282fe8fb19SBen Gras switch (pollretval = pollts(pfd, fdlistno, &ts, NULL)) {
5292fe8fb19SBen Gras case 0: /* timed out */
5302fe8fb19SBen Gras stat = RPC_TIMEDOUT;
5312fe8fb19SBen Gras continue;
5322fe8fb19SBen Gras case -1: /* some kind of error - we ignore it */
5332fe8fb19SBen Gras goto recv_again;
5342fe8fb19SBen Gras } /* end of poll results switch */
5352fe8fb19SBen Gras
5362fe8fb19SBen Gras for (i = fds_found = 0;
5372fe8fb19SBen Gras i < fdlistno && fds_found < pollretval; i++) {
5382fe8fb19SBen Gras bool_t done = FALSE;
5392fe8fb19SBen Gras
5402fe8fb19SBen Gras if (pfd[i].revents == 0)
5412fe8fb19SBen Gras continue;
5422fe8fb19SBen Gras else if (pfd[i].revents & POLLNVAL) {
5432fe8fb19SBen Gras /*
5442fe8fb19SBen Gras * Something bad has happened to this descri-
5452fe8fb19SBen Gras * ptor. We can cause pollts() to ignore
5462fe8fb19SBen Gras * it simply by using a negative fd. We do that
5472fe8fb19SBen Gras * rather than compacting the pfd[] and fdlist[]
5482fe8fb19SBen Gras * arrays.
5492fe8fb19SBen Gras */
5502fe8fb19SBen Gras pfd[i].fd = -1;
5512fe8fb19SBen Gras fds_found++;
5522fe8fb19SBen Gras continue;
5532fe8fb19SBen Gras } else
5542fe8fb19SBen Gras fds_found++;
5552fe8fb19SBen Gras #ifdef RPC_DEBUG
5562fe8fb19SBen Gras fprintf(stderr, "response for %s\n",
5572fe8fb19SBen Gras fdlist[i].nconf->nc_netid);
5582fe8fb19SBen Gras #endif
5592fe8fb19SBen Gras try_again:
5602fe8fb19SBen Gras inlen = recvfrom(fdlist[i].fd, inbuf, fdlist[i].dsize,
5612fe8fb19SBen Gras 0, (struct sockaddr *)(void *)&fdlist[i].raddr,
5622fe8fb19SBen Gras &fdlist[i].asize);
5632fe8fb19SBen Gras if (inlen < 0) {
5642fe8fb19SBen Gras if (errno == EINTR)
5652fe8fb19SBen Gras goto try_again;
5662fe8fb19SBen Gras warnx("clnt_bcast: Cannot receive reply to "
5672fe8fb19SBen Gras "broadcast");
5682fe8fb19SBen Gras stat = RPC_CANTRECV;
5692fe8fb19SBen Gras continue;
5702fe8fb19SBen Gras }
5712fe8fb19SBen Gras if (inlen < (ssize_t)sizeof(u_int32_t))
5722fe8fb19SBen Gras continue; /* Drop that and go ahead */
5732fe8fb19SBen Gras /*
5742fe8fb19SBen Gras * see if reply transaction id matches sent id.
5752fe8fb19SBen Gras * If so, decode the results. If return id is xid + 1
5762fe8fb19SBen Gras * it was a PORTMAP reply
5772fe8fb19SBen Gras */
5782fe8fb19SBen Gras if (*((u_int32_t *)(void *)(inbuf)) ==
5792fe8fb19SBen Gras *((u_int32_t *)(void *)(outbuf))) {
5802fe8fb19SBen Gras pmap_reply_flag = 0;
5812fe8fb19SBen Gras msg.acpted_rply.ar_verf = _null_auth;
5822fe8fb19SBen Gras msg.acpted_rply.ar_results.where =
5832fe8fb19SBen Gras (caddr_t)(void *)&bres;
5842fe8fb19SBen Gras msg.acpted_rply.ar_results.proc =
5852fe8fb19SBen Gras (xdrproc_t)xdr_rpcb_rmtcallres;
5862fe8fb19SBen Gras #ifdef PORTMAP
5872fe8fb19SBen Gras } else if (pmap_flag &&
5882fe8fb19SBen Gras *((u_int32_t *)(void *)(inbuf)) ==
5892fe8fb19SBen Gras *((u_int32_t *)(void *)(outbuf_pmap))) {
5902fe8fb19SBen Gras pmap_reply_flag = 1;
5912fe8fb19SBen Gras msg.acpted_rply.ar_verf = _null_auth;
5922fe8fb19SBen Gras msg.acpted_rply.ar_results.where =
5932fe8fb19SBen Gras (caddr_t)(void *)&bres_pmap;
5942fe8fb19SBen Gras msg.acpted_rply.ar_results.proc =
5952fe8fb19SBen Gras (xdrproc_t)xdr_rmtcallres;
5962fe8fb19SBen Gras #endif /* PORTMAP */
5972fe8fb19SBen Gras } else
5982fe8fb19SBen Gras continue;
5992fe8fb19SBen Gras xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
6002fe8fb19SBen Gras if (xdr_replymsg(xdrs, &msg)) {
6012fe8fb19SBen Gras if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
6022fe8fb19SBen Gras (msg.acpted_rply.ar_stat == SUCCESS)) {
6032fe8fb19SBen Gras struct netbuf taddr, *np;
6042fe8fb19SBen Gras struct sockaddr_in *bsin;
6052fe8fb19SBen Gras
6062fe8fb19SBen Gras #ifdef PORTMAP
6072fe8fb19SBen Gras if (pmap_flag && pmap_reply_flag) {
6082fe8fb19SBen Gras bsin = (struct sockaddr_in *)
6092fe8fb19SBen Gras (void *)&fdlist[i].raddr;
6102fe8fb19SBen Gras bsin->sin_port =
6112fe8fb19SBen Gras htons((u_short)port);
6122fe8fb19SBen Gras taddr.len = taddr.maxlen =
6132fe8fb19SBen Gras fdlist[i].raddr.ss_len;
6142fe8fb19SBen Gras taddr.buf = &fdlist[i].raddr;
6152fe8fb19SBen Gras done = (*eachresult)(resultsp,
6162fe8fb19SBen Gras &taddr, fdlist[i].nconf);
6172fe8fb19SBen Gras } else {
6182fe8fb19SBen Gras #endif
6192fe8fb19SBen Gras #ifdef RPC_DEBUG
6202fe8fb19SBen Gras fprintf(stderr, "uaddr %s\n",
6212fe8fb19SBen Gras uaddrp);
6222fe8fb19SBen Gras #endif
6232fe8fb19SBen Gras np = uaddr2taddr(
6242fe8fb19SBen Gras fdlist[i].nconf, uaddrp);
6252fe8fb19SBen Gras done = (*eachresult)(resultsp,
6262fe8fb19SBen Gras np, fdlist[i].nconf);
6272fe8fb19SBen Gras free(np);
6282fe8fb19SBen Gras #ifdef PORTMAP
6292fe8fb19SBen Gras }
6302fe8fb19SBen Gras #endif
6312fe8fb19SBen Gras }
6322fe8fb19SBen Gras /* otherwise, we just ignore the errors ... */
6332fe8fb19SBen Gras }
6342fe8fb19SBen Gras /* else some kind of deserialization problem ... */
6352fe8fb19SBen Gras
6362fe8fb19SBen Gras xdrs->x_op = XDR_FREE;
6372fe8fb19SBen Gras msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
6382fe8fb19SBen Gras (void) xdr_replymsg(xdrs, &msg);
6392fe8fb19SBen Gras (void) (*xresults)(xdrs, resultsp);
6402fe8fb19SBen Gras XDR_DESTROY(xdrs);
6412fe8fb19SBen Gras if (done) {
6422fe8fb19SBen Gras stat = RPC_SUCCESS;
6432fe8fb19SBen Gras goto done_broad;
6442fe8fb19SBen Gras } else {
6452fe8fb19SBen Gras goto recv_again;
6462fe8fb19SBen Gras }
6472fe8fb19SBen Gras } /* The recv for loop */
6482fe8fb19SBen Gras } /* The giant for loop */
6492fe8fb19SBen Gras
6502fe8fb19SBen Gras done_broad:
6512fe8fb19SBen Gras if (inbuf)
6522fe8fb19SBen Gras (void) free(inbuf);
6532fe8fb19SBen Gras if (outbuf)
6542fe8fb19SBen Gras (void) free(outbuf);
6552fe8fb19SBen Gras #ifdef PORTMAP
6562fe8fb19SBen Gras if (outbuf_pmap)
6572fe8fb19SBen Gras (void) free(outbuf_pmap);
6582fe8fb19SBen Gras #endif
6592fe8fb19SBen Gras for (i = 0; i < fdlistno; i++) {
6602fe8fb19SBen Gras (void) close(fdlist[i].fd);
6612fe8fb19SBen Gras __rpc_freebroadifs(&fdlist[i].nal);
6622fe8fb19SBen Gras }
6632fe8fb19SBen Gras AUTH_DESTROY(sys_auth);
6642fe8fb19SBen Gras (void) __rpc_endconf(handle);
6652fe8fb19SBen Gras
6662fe8fb19SBen Gras return (stat);
6672fe8fb19SBen Gras }
6682fe8fb19SBen Gras
6692fe8fb19SBen Gras
6702fe8fb19SBen Gras enum clnt_stat
rpc_broadcast(rpcprog_t prog,rpcvers_t vers,rpcproc_t proc,xdrproc_t xargs,const char * argsp,xdrproc_t xresults,caddr_t resultsp,resultproc_t eachresult,const char * nettype)671f14fb602SLionel Sambuc rpc_broadcast(
672f14fb602SLionel Sambuc rpcprog_t prog, /* program number */
673f14fb602SLionel Sambuc rpcvers_t vers, /* version number */
674f14fb602SLionel Sambuc rpcproc_t proc, /* procedure number */
675f14fb602SLionel Sambuc xdrproc_t xargs, /* xdr routine for args */
676f14fb602SLionel Sambuc const char * argsp, /* pointer to args */
677f14fb602SLionel Sambuc xdrproc_t xresults, /* xdr routine for results */
678f14fb602SLionel Sambuc caddr_t resultsp, /* pointer to results */
679f14fb602SLionel Sambuc resultproc_t eachresult, /* call with each result obtained */
680f14fb602SLionel Sambuc const char * nettype) /* transport type */
6812fe8fb19SBen Gras {
6822fe8fb19SBen Gras enum clnt_stat dummy;
6832fe8fb19SBen Gras
6842fe8fb19SBen Gras dummy = rpc_broadcast_exp(prog, vers, proc, xargs, argsp,
6852fe8fb19SBen Gras xresults, resultsp, eachresult,
6862fe8fb19SBen Gras INITTIME, WAITTIME, nettype);
6872fe8fb19SBen Gras return (dummy);
6882fe8fb19SBen Gras }
689