clnt_bcast.c revision 1.2.4.2 1 1.2.4.2 minoura /* $NetBSD: clnt_bcast.c,v 1.2.4.2 2000/06/23 16:17:40 minoura Exp $ */
2 1.2.4.2 minoura
3 1.2.4.2 minoura /*
4 1.2.4.2 minoura * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 1.2.4.2 minoura * unrestricted use provided that this legend is included on all tape
6 1.2.4.2 minoura * media and as a part of the software program in whole or part. Users
7 1.2.4.2 minoura * may copy or modify Sun RPC without charge, but are not authorized
8 1.2.4.2 minoura * to license or distribute it to anyone else except as part of a product or
9 1.2.4.2 minoura * program developed by the user.
10 1.2.4.2 minoura *
11 1.2.4.2 minoura * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 1.2.4.2 minoura * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 1.2.4.2 minoura * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 1.2.4.2 minoura *
15 1.2.4.2 minoura * Sun RPC is provided with no support and without any obligation on the
16 1.2.4.2 minoura * part of Sun Microsystems, Inc. to assist in its use, correction,
17 1.2.4.2 minoura * modification or enhancement.
18 1.2.4.2 minoura *
19 1.2.4.2 minoura * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 1.2.4.2 minoura * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 1.2.4.2 minoura * OR ANY PART THEREOF.
22 1.2.4.2 minoura *
23 1.2.4.2 minoura * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 1.2.4.2 minoura * or profits or other special, indirect and consequential damages, even if
25 1.2.4.2 minoura * Sun has been advised of the possibility of such damages.
26 1.2.4.2 minoura *
27 1.2.4.2 minoura * Sun Microsystems, Inc.
28 1.2.4.2 minoura * 2550 Garcia Avenue
29 1.2.4.2 minoura * Mountain View, California 94043
30 1.2.4.2 minoura */
31 1.2.4.2 minoura /*
32 1.2.4.2 minoura * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33 1.2.4.2 minoura */
34 1.2.4.2 minoura
35 1.2.4.2 minoura /* #ident "@(#)clnt_bcast.c 1.18 94/05/03 SMI" */
36 1.2.4.2 minoura
37 1.2.4.2 minoura #if 0
38 1.2.4.2 minoura #if !defined(lint) && defined(SCCSIDS)
39 1.2.4.2 minoura static char sccsid[] = "@(#)clnt_bcast.c 1.15 89/04/21 Copyr 1988 Sun Micro";
40 1.2.4.2 minoura #endif
41 1.2.4.2 minoura #endif
42 1.2.4.2 minoura
43 1.2.4.2 minoura
44 1.2.4.2 minoura /*
45 1.2.4.2 minoura * clnt_bcast.c
46 1.2.4.2 minoura * Client interface to broadcast service.
47 1.2.4.2 minoura *
48 1.2.4.2 minoura * Copyright (C) 1988, Sun Microsystems, Inc.
49 1.2.4.2 minoura *
50 1.2.4.2 minoura * The following is kludged-up support for simple rpc broadcasts.
51 1.2.4.2 minoura * Someday a large, complicated system will replace these routines.
52 1.2.4.2 minoura */
53 1.2.4.2 minoura
54 1.2.4.2 minoura #include "namespace.h"
55 1.2.4.2 minoura #include <sys/types.h>
56 1.2.4.2 minoura #include <sys/socket.h>
57 1.2.4.2 minoura #include <sys/queue.h>
58 1.2.4.2 minoura #include <net/if.h>
59 1.2.4.2 minoura #include <netinet/in.h>
60 1.2.4.2 minoura #include <ifaddrs.h>
61 1.2.4.2 minoura #include <sys/poll.h>
62 1.2.4.2 minoura #include <rpc/rpc.h>
63 1.2.4.2 minoura #ifdef PORTMAP
64 1.2.4.2 minoura #include <rpc/pmap_prot.h>
65 1.2.4.2 minoura #include <rpc/pmap_clnt.h>
66 1.2.4.2 minoura #include <rpc/pmap_rmt.h>
67 1.2.4.2 minoura #endif
68 1.2.4.2 minoura #include <arpa/inet.h>
69 1.2.4.2 minoura #ifdef RPC_DEBUG
70 1.2.4.2 minoura #include <stdio.h>
71 1.2.4.2 minoura #endif
72 1.2.4.2 minoura #include <errno.h>
73 1.2.4.2 minoura #include <stdlib.h>
74 1.2.4.2 minoura #include <unistd.h>
75 1.2.4.2 minoura #include <netdb.h>
76 1.2.4.2 minoura #include <err.h>
77 1.2.4.2 minoura #include <string.h>
78 1.2.4.2 minoura
79 1.2.4.2 minoura #include "rpc_com.h"
80 1.2.4.2 minoura
81 1.2.4.2 minoura #define MAXBCAST 20 /* Max no of broadcasting transports */
82 1.2.4.2 minoura #define INITTIME 4000 /* Time to wait initially */
83 1.2.4.2 minoura #define WAITTIME 8000 /* Maximum time to wait */
84 1.2.4.2 minoura
85 1.2.4.2 minoura /*
86 1.2.4.2 minoura * If nettype is NULL, it broadcasts on all the available
87 1.2.4.2 minoura * datagram_n transports. May potentially lead to broadacst storms
88 1.2.4.2 minoura * and hence should be used with caution, care and courage.
89 1.2.4.2 minoura *
90 1.2.4.2 minoura * The current parameter xdr packet size is limited by the max tsdu
91 1.2.4.2 minoura * size of the transport. If the max tsdu size of any transport is
92 1.2.4.2 minoura * smaller than the parameter xdr packet, then broadcast is not
93 1.2.4.2 minoura * sent on that transport.
94 1.2.4.2 minoura *
95 1.2.4.2 minoura * Also, the packet size should be less the packet size of
96 1.2.4.2 minoura * the data link layer (for ethernet it is 1400 bytes). There is
97 1.2.4.2 minoura * no easy way to find out the max size of the data link layer and
98 1.2.4.2 minoura * we are assuming that the args would be smaller than that.
99 1.2.4.2 minoura *
100 1.2.4.2 minoura * The result size has to be smaller than the transport tsdu size.
101 1.2.4.2 minoura *
102 1.2.4.2 minoura * If PORTMAP has been defined, we send two packets for UDP, one for
103 1.2.4.2 minoura * rpcbind and one for portmap. For those machines which support
104 1.2.4.2 minoura * both rpcbind and portmap, it will cause them to reply twice, and
105 1.2.4.2 minoura * also here it will get two responses ... inefficient and clumsy.
106 1.2.4.2 minoura */
107 1.2.4.2 minoura
108 1.2.4.2 minoura #ifdef __weak_alias
109 1.2.4.2 minoura __weak_alias(rpc_broadcast_exp,_rpc_broadcast_exp)
110 1.2.4.2 minoura __weak_alias(rpc_broadcast,_rpc_broadcast)
111 1.2.4.2 minoura #endif
112 1.2.4.2 minoura
113 1.2.4.2 minoura struct broadif {
114 1.2.4.2 minoura int index;
115 1.2.4.2 minoura struct sockaddr_storage broadaddr;
116 1.2.4.2 minoura TAILQ_ENTRY(broadif) link;
117 1.2.4.2 minoura };
118 1.2.4.2 minoura
119 1.2.4.2 minoura typedef TAILQ_HEAD(, broadif) broadlist_t;
120 1.2.4.2 minoura
121 1.2.4.2 minoura int __rpc_getbroadifs __P((int, int, int, broadlist_t *));
122 1.2.4.2 minoura void __rpc_freebroadifs __P((broadlist_t *));
123 1.2.4.2 minoura int __rpc_broadenable __P((int, int, struct broadif *));
124 1.2.4.2 minoura
125 1.2.4.2 minoura int __rpc_lowvers = 0;
126 1.2.4.2 minoura
127 1.2.4.2 minoura int
128 1.2.4.2 minoura __rpc_getbroadifs(int af, int proto, int socktype, broadlist_t *list)
129 1.2.4.2 minoura {
130 1.2.4.2 minoura int count = 0;
131 1.2.4.2 minoura struct broadif *bip;
132 1.2.4.2 minoura struct ifaddrs *ifap, *ifp;
133 1.2.4.2 minoura #ifdef INET6
134 1.2.4.2 minoura struct sockaddr_in6 *sin6;
135 1.2.4.2 minoura #endif
136 1.2.4.2 minoura struct sockaddr_in *sin;
137 1.2.4.2 minoura struct addrinfo hints, *res;
138 1.2.4.2 minoura
139 1.2.4.2 minoura if (getifaddrs(&ifp) < 0)
140 1.2.4.2 minoura return 0;
141 1.2.4.2 minoura
142 1.2.4.2 minoura memset(&hints, 0, sizeof hints);
143 1.2.4.2 minoura
144 1.2.4.2 minoura hints.ai_family = af;
145 1.2.4.2 minoura hints.ai_protocol = proto;
146 1.2.4.2 minoura hints.ai_socktype = socktype;
147 1.2.4.2 minoura
148 1.2.4.2 minoura if (getaddrinfo(NULL, "sunrpc", &hints, &res) != 0)
149 1.2.4.2 minoura return 0;
150 1.2.4.2 minoura
151 1.2.4.2 minoura for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
152 1.2.4.2 minoura if (ifap->ifa_addr->sa_family != af ||
153 1.2.4.2 minoura !(ifap->ifa_flags & IFF_UP))
154 1.2.4.2 minoura continue;
155 1.2.4.2 minoura #ifdef INET6
156 1.2.4.2 minoura if ((af == AF_INET6 && !(ifap->ifa_flags & IFF_MULTICAST)) ||
157 1.2.4.2 minoura !(ifap->ifa_flags & IFF_BROADCAST))
158 1.2.4.2 minoura continue;
159 1.2.4.2 minoura #endif
160 1.2.4.2 minoura bip = (struct broadif *)malloc(sizeof *bip);
161 1.2.4.2 minoura if (bip == NULL)
162 1.2.4.2 minoura break;
163 1.2.4.2 minoura bip->index = if_nametoindex(ifap->ifa_name);
164 1.2.4.2 minoura #ifdef INET6
165 1.2.4.2 minoura if (af != AF_INET6 && (ifap->ifa_flags & IFF_BROADCAST)) {
166 1.2.4.2 minoura #else
167 1.2.4.2 minoura if (ifap->ifa_flags & IFF_BROADCAST) {
168 1.2.4.2 minoura #endif
169 1.2.4.2 minoura memcpy(&bip->broadaddr, ifap->ifa_broadaddr,
170 1.2.4.2 minoura ifap->ifa_broadaddr->sa_len);
171 1.2.4.2 minoura sin = (struct sockaddr_in *)&bip->broadaddr;
172 1.2.4.2 minoura sin->sin_port =
173 1.2.4.2 minoura ((struct sockaddr_in *)res->ai_addr)->sin_port;
174 1.2.4.2 minoura #ifdef INET6
175 1.2.4.2 minoura } else if (af == AF_INET6) {
176 1.2.4.2 minoura sin6 = (struct sockaddr_in6 *)&bip->broadaddr;
177 1.2.4.2 minoura inet_pton(af, RPCB_MULTICAST_ADDR, &sin6->sin6_addr);
178 1.2.4.2 minoura sin6->sin6_family = af;
179 1.2.4.2 minoura sin6->sin6_len = sizeof *sin6;
180 1.2.4.2 minoura sin6->sin6_port =
181 1.2.4.2 minoura ((struct sockaddr_in6 *)res->ai_addr)->sin6_port;
182 1.2.4.2 minoura sin6->sin6_scope_id = bip->index;
183 1.2.4.2 minoura #endif
184 1.2.4.2 minoura }
185 1.2.4.2 minoura TAILQ_INSERT_TAIL(list, bip, link);
186 1.2.4.2 minoura count++;
187 1.2.4.2 minoura }
188 1.2.4.2 minoura freeifaddrs(ifp);
189 1.2.4.2 minoura freeaddrinfo(res);
190 1.2.4.2 minoura
191 1.2.4.2 minoura return count;
192 1.2.4.2 minoura }
193 1.2.4.2 minoura
194 1.2.4.2 minoura void
195 1.2.4.2 minoura __rpc_freebroadifs(broadlist_t *list)
196 1.2.4.2 minoura {
197 1.2.4.2 minoura struct broadif *bip, *next;
198 1.2.4.2 minoura
199 1.2.4.2 minoura bip = TAILQ_FIRST(list);
200 1.2.4.2 minoura
201 1.2.4.2 minoura while (bip != NULL) {
202 1.2.4.2 minoura next = TAILQ_NEXT(bip, link);
203 1.2.4.2 minoura free(bip);
204 1.2.4.2 minoura bip = next;
205 1.2.4.2 minoura }
206 1.2.4.2 minoura }
207 1.2.4.2 minoura
208 1.2.4.2 minoura int
209 1.2.4.2 minoura __rpc_broadenable(int af, int s, struct broadif *bip)
210 1.2.4.2 minoura {
211 1.2.4.2 minoura int o = 1;
212 1.2.4.2 minoura
213 1.2.4.2 minoura #if 0
214 1.2.4.2 minoura if (af == AF_INET6) {
215 1.2.4.2 minoura fprintf(stderr, "set v6 multicast if to %d\n", bip->index);
216 1.2.4.2 minoura if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_IF, &bip->index,
217 1.2.4.2 minoura sizeof bip->index) < 0)
218 1.2.4.2 minoura return -1;
219 1.2.4.2 minoura } else
220 1.2.4.2 minoura #endif
221 1.2.4.2 minoura if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &o, sizeof o) < 0)
222 1.2.4.2 minoura return -1;
223 1.2.4.2 minoura
224 1.2.4.2 minoura return 0;
225 1.2.4.2 minoura }
226 1.2.4.2 minoura
227 1.2.4.2 minoura
228 1.2.4.2 minoura enum clnt_stat
229 1.2.4.2 minoura rpc_broadcast_exp(prog, vers, proc, xargs, argsp, xresults, resultsp,
230 1.2.4.2 minoura eachresult, inittime, waittime, nettype)
231 1.2.4.2 minoura rpcprog_t prog; /* program number */
232 1.2.4.2 minoura rpcvers_t vers; /* version number */
233 1.2.4.2 minoura rpcproc_t proc; /* procedure number */
234 1.2.4.2 minoura xdrproc_t xargs; /* xdr routine for args */
235 1.2.4.2 minoura caddr_t argsp; /* pointer to args */
236 1.2.4.2 minoura xdrproc_t xresults; /* xdr routine for results */
237 1.2.4.2 minoura caddr_t resultsp; /* pointer to results */
238 1.2.4.2 minoura resultproc_t eachresult; /* call with each result obtained */
239 1.2.4.2 minoura int inittime; /* how long to wait initially */
240 1.2.4.2 minoura int waittime; /* maximum time to wait */
241 1.2.4.2 minoura const char *nettype; /* transport type */
242 1.2.4.2 minoura {
243 1.2.4.2 minoura enum clnt_stat stat = RPC_SUCCESS; /* Return status */
244 1.2.4.2 minoura XDR xdr_stream; /* XDR stream */
245 1.2.4.2 minoura register XDR *xdrs = &xdr_stream;
246 1.2.4.2 minoura struct rpc_msg msg; /* RPC message */
247 1.2.4.2 minoura struct timeval t;
248 1.2.4.2 minoura char *outbuf = NULL; /* Broadcast msg buffer */
249 1.2.4.2 minoura char *inbuf = NULL; /* Reply buf */
250 1.2.4.2 minoura int inlen;
251 1.2.4.2 minoura u_int maxbufsize = 0;
252 1.2.4.2 minoura AUTH *sys_auth = authunix_create_default();
253 1.2.4.2 minoura int i;
254 1.2.4.2 minoura void *handle;
255 1.2.4.2 minoura char uaddress[1024]; /* A self imposed limit */
256 1.2.4.2 minoura char *uaddrp = uaddress;
257 1.2.4.2 minoura int pmap_reply_flag; /* reply recvd from PORTMAP */
258 1.2.4.2 minoura /* An array of all the suitable broadcast transports */
259 1.2.4.2 minoura struct {
260 1.2.4.2 minoura int fd; /* File descriptor */
261 1.2.4.2 minoura int af;
262 1.2.4.2 minoura int proto;
263 1.2.4.2 minoura struct netconfig *nconf; /* Netconfig structure */
264 1.2.4.2 minoura u_int asize; /* Size of the addr buf */
265 1.2.4.2 minoura u_int dsize; /* Size of the data buf */
266 1.2.4.2 minoura struct sockaddr_storage raddr; /* Remote address */
267 1.2.4.2 minoura broadlist_t nal;
268 1.2.4.2 minoura } fdlist[MAXBCAST];
269 1.2.4.2 minoura struct pollfd pfd[MAXBCAST];
270 1.2.4.2 minoura register int fdlistno = 0;
271 1.2.4.2 minoura struct r_rpcb_rmtcallargs barg; /* Remote arguments */
272 1.2.4.2 minoura struct r_rpcb_rmtcallres bres; /* Remote results */
273 1.2.4.2 minoura int outlen, outlen_pmap;
274 1.2.4.2 minoura struct netconfig *nconf;
275 1.2.4.2 minoura int msec;
276 1.2.4.2 minoura int pollretval;
277 1.2.4.2 minoura int fds_found;
278 1.2.4.2 minoura
279 1.2.4.2 minoura #ifdef PORTMAP
280 1.2.4.2 minoura u_long port; /* Remote port number */
281 1.2.4.2 minoura int pmap_flag = 0; /* UDP exists ? */
282 1.2.4.2 minoura char *outbuf_pmap = NULL;
283 1.2.4.2 minoura struct rmtcallargs barg_pmap; /* Remote arguments */
284 1.2.4.2 minoura struct rmtcallres bres_pmap; /* Remote results */
285 1.2.4.2 minoura u_int udpbufsz = 0;
286 1.2.4.2 minoura #endif /* PORTMAP */
287 1.2.4.2 minoura
288 1.2.4.2 minoura if (sys_auth == (AUTH *)NULL) {
289 1.2.4.2 minoura return (RPC_SYSTEMERROR);
290 1.2.4.2 minoura }
291 1.2.4.2 minoura /*
292 1.2.4.2 minoura * initialization: create a fd, a broadcast address, and send the
293 1.2.4.2 minoura * request on the broadcast transport.
294 1.2.4.2 minoura * Listen on all of them and on replies, call the user supplied
295 1.2.4.2 minoura * function.
296 1.2.4.2 minoura */
297 1.2.4.2 minoura
298 1.2.4.2 minoura if (nettype == NULL)
299 1.2.4.2 minoura nettype = "datagram_n";
300 1.2.4.2 minoura if ((handle = __rpc_setconf((char *)nettype)) == NULL) {
301 1.2.4.2 minoura return (RPC_UNKNOWNPROTO);
302 1.2.4.2 minoura }
303 1.2.4.2 minoura while ((nconf = __rpc_getconf(handle))) {
304 1.2.4.2 minoura int fd;
305 1.2.4.2 minoura struct __rpc_sockinfo si;
306 1.2.4.2 minoura
307 1.2.4.2 minoura if (nconf->nc_semantics != NC_TPI_CLTS)
308 1.2.4.2 minoura continue;
309 1.2.4.2 minoura if (fdlistno >= MAXBCAST)
310 1.2.4.2 minoura break; /* No more slots available */
311 1.2.4.2 minoura if (!__rpc_nconf2sockinfo(nconf, &si))
312 1.2.4.2 minoura continue;
313 1.2.4.2 minoura
314 1.2.4.2 minoura TAILQ_INIT(&fdlist[fdlistno].nal);
315 1.2.4.2 minoura if (__rpc_getbroadifs(si.si_af, si.si_proto, si.si_socktype,
316 1.2.4.2 minoura &fdlist[fdlistno].nal) == 0)
317 1.2.4.2 minoura continue;
318 1.2.4.2 minoura
319 1.2.4.2 minoura fd = socket(si.si_af, si.si_socktype, si.si_proto);
320 1.2.4.2 minoura if (fd < 0) {
321 1.2.4.2 minoura stat = RPC_CANTSEND;
322 1.2.4.2 minoura continue;
323 1.2.4.2 minoura }
324 1.2.4.2 minoura fdlist[fdlistno].af = si.si_af;
325 1.2.4.2 minoura fdlist[fdlistno].proto = si.si_proto;
326 1.2.4.2 minoura fdlist[fdlistno].fd = fd;
327 1.2.4.2 minoura fdlist[fdlistno].nconf = nconf;
328 1.2.4.2 minoura fdlist[fdlistno].asize = __rpc_get_a_size(si.si_af);
329 1.2.4.2 minoura pfd[fdlistno].events = POLLIN | POLLPRI |
330 1.2.4.2 minoura POLLRDNORM | POLLRDBAND;
331 1.2.4.2 minoura pfd[fdlistno].fd = fdlist[fdlistno].fd = fd;
332 1.2.4.2 minoura fdlist[fdlistno].dsize = __rpc_get_t_size(si.si_af, si.si_proto,
333 1.2.4.2 minoura 0);
334 1.2.4.2 minoura
335 1.2.4.2 minoura if (maxbufsize <= fdlist[fdlistno].dsize)
336 1.2.4.2 minoura maxbufsize = fdlist[fdlistno].dsize;
337 1.2.4.2 minoura
338 1.2.4.2 minoura #ifdef PORTMAP
339 1.2.4.2 minoura if (si.si_af == AF_INET && si.si_proto == IPPROTO_UDP) {
340 1.2.4.2 minoura udpbufsz = fdlist[fdlistno].dsize;
341 1.2.4.2 minoura if ((outbuf_pmap = malloc(udpbufsz)) == NULL) {
342 1.2.4.2 minoura close(fd);
343 1.2.4.2 minoura stat = RPC_SYSTEMERROR;
344 1.2.4.2 minoura goto done_broad;
345 1.2.4.2 minoura }
346 1.2.4.2 minoura pmap_flag = 1;
347 1.2.4.2 minoura }
348 1.2.4.2 minoura #endif
349 1.2.4.2 minoura fdlistno++;
350 1.2.4.2 minoura }
351 1.2.4.2 minoura
352 1.2.4.2 minoura if (fdlistno == 0) {
353 1.2.4.2 minoura if (stat == RPC_SUCCESS)
354 1.2.4.2 minoura stat = RPC_UNKNOWNPROTO;
355 1.2.4.2 minoura goto done_broad;
356 1.2.4.2 minoura }
357 1.2.4.2 minoura if (maxbufsize == 0) {
358 1.2.4.2 minoura if (stat == RPC_SUCCESS)
359 1.2.4.2 minoura stat = RPC_CANTSEND;
360 1.2.4.2 minoura goto done_broad;
361 1.2.4.2 minoura }
362 1.2.4.2 minoura inbuf = malloc(maxbufsize);
363 1.2.4.2 minoura outbuf = malloc(maxbufsize);
364 1.2.4.2 minoura if ((inbuf == NULL) || (outbuf == NULL)) {
365 1.2.4.2 minoura stat = RPC_SYSTEMERROR;
366 1.2.4.2 minoura goto done_broad;
367 1.2.4.2 minoura }
368 1.2.4.2 minoura
369 1.2.4.2 minoura /* Serialize all the arguments which have to be sent */
370 1.2.4.2 minoura (void) gettimeofday(&t, (struct timezone *)0);
371 1.2.4.2 minoura msg.rm_xid = getpid() ^ t.tv_sec ^ t.tv_usec;
372 1.2.4.2 minoura msg.rm_direction = CALL;
373 1.2.4.2 minoura msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
374 1.2.4.2 minoura msg.rm_call.cb_prog = RPCBPROG;
375 1.2.4.2 minoura msg.rm_call.cb_vers = RPCBVERS;
376 1.2.4.2 minoura msg.rm_call.cb_proc = RPCBPROC_CALLIT;
377 1.2.4.2 minoura barg.prog = prog;
378 1.2.4.2 minoura barg.vers = vers;
379 1.2.4.2 minoura barg.proc = proc;
380 1.2.4.2 minoura barg.args.args_val = argsp;
381 1.2.4.2 minoura barg.xdr_args = xargs;
382 1.2.4.2 minoura bres.addr = uaddrp;
383 1.2.4.2 minoura bres.results.results_val = resultsp;
384 1.2.4.2 minoura bres.xdr_res = xresults;
385 1.2.4.2 minoura msg.rm_call.cb_cred = sys_auth->ah_cred;
386 1.2.4.2 minoura msg.rm_call.cb_verf = sys_auth->ah_verf;
387 1.2.4.2 minoura xdrmem_create(xdrs, outbuf, maxbufsize, XDR_ENCODE);
388 1.2.4.2 minoura if ((!xdr_callmsg(xdrs, &msg)) ||
389 1.2.4.2 minoura (!xdr_rpcb_rmtcallargs(xdrs, (struct rpcb_rmtcallargs *)&barg))) {
390 1.2.4.2 minoura stat = RPC_CANTENCODEARGS;
391 1.2.4.2 minoura goto done_broad;
392 1.2.4.2 minoura }
393 1.2.4.2 minoura outlen = xdr_getpos(xdrs);
394 1.2.4.2 minoura xdr_destroy(xdrs);
395 1.2.4.2 minoura
396 1.2.4.2 minoura #ifdef PORTMAP
397 1.2.4.2 minoura /* Prepare the packet for version 2 PORTMAP */
398 1.2.4.2 minoura if (pmap_flag) {
399 1.2.4.2 minoura msg.rm_xid++; /* One way to distinguish */
400 1.2.4.2 minoura msg.rm_call.cb_prog = PMAPPROG;
401 1.2.4.2 minoura msg.rm_call.cb_vers = PMAPVERS;
402 1.2.4.2 minoura msg.rm_call.cb_proc = PMAPPROC_CALLIT;
403 1.2.4.2 minoura barg_pmap.prog = prog;
404 1.2.4.2 minoura barg_pmap.vers = vers;
405 1.2.4.2 minoura barg_pmap.proc = proc;
406 1.2.4.2 minoura barg_pmap.args_ptr = argsp;
407 1.2.4.2 minoura barg_pmap.xdr_args = xargs;
408 1.2.4.2 minoura bres_pmap.port_ptr = &port;
409 1.2.4.2 minoura bres_pmap.xdr_results = xresults;
410 1.2.4.2 minoura bres_pmap.results_ptr = resultsp;
411 1.2.4.2 minoura xdrmem_create(xdrs, outbuf_pmap, udpbufsz, XDR_ENCODE);
412 1.2.4.2 minoura if ((! xdr_callmsg(xdrs, &msg)) ||
413 1.2.4.2 minoura (! xdr_rmtcall_args(xdrs, &barg_pmap))) {
414 1.2.4.2 minoura stat = RPC_CANTENCODEARGS;
415 1.2.4.2 minoura goto done_broad;
416 1.2.4.2 minoura }
417 1.2.4.2 minoura outlen_pmap = xdr_getpos(xdrs);
418 1.2.4.2 minoura xdr_destroy(xdrs);
419 1.2.4.2 minoura }
420 1.2.4.2 minoura #endif /* PORTMAP */
421 1.2.4.2 minoura
422 1.2.4.2 minoura /*
423 1.2.4.2 minoura * Basic loop: broadcast the packets to transports which
424 1.2.4.2 minoura * support data packets of size such that one can encode
425 1.2.4.2 minoura * all the arguments.
426 1.2.4.2 minoura * Wait a while for response(s).
427 1.2.4.2 minoura * The response timeout grows larger per iteration.
428 1.2.4.2 minoura */
429 1.2.4.2 minoura for (msec = inittime; msec <= waittime; msec += msec) {
430 1.2.4.2 minoura struct broadif *bip;
431 1.2.4.2 minoura
432 1.2.4.2 minoura /* Broadcast all the packets now */
433 1.2.4.2 minoura for (i = 0; i < fdlistno; i++) {
434 1.2.4.2 minoura if (fdlist[i].dsize < outlen) {
435 1.2.4.2 minoura stat = RPC_CANTSEND;
436 1.2.4.2 minoura continue;
437 1.2.4.2 minoura }
438 1.2.4.2 minoura for (bip = TAILQ_FIRST(&fdlist[i].nal); bip != NULL;
439 1.2.4.2 minoura bip = TAILQ_NEXT(bip, link)) {
440 1.2.4.2 minoura void *addr;
441 1.2.4.2 minoura
442 1.2.4.2 minoura addr = &bip->broadaddr;
443 1.2.4.2 minoura
444 1.2.4.2 minoura __rpc_broadenable(fdlist[i].af, fdlist[i].fd,
445 1.2.4.2 minoura bip);
446 1.2.4.2 minoura
447 1.2.4.2 minoura /*
448 1.2.4.2 minoura * Only use version 3 if lowvers is not set
449 1.2.4.2 minoura */
450 1.2.4.2 minoura
451 1.2.4.2 minoura if (!__rpc_lowvers)
452 1.2.4.2 minoura if (sendto(fdlist[i].fd, outbuf,
453 1.2.4.2 minoura outlen, 0, (struct sockaddr*)addr,
454 1.2.4.2 minoura fdlist[i].asize) != outlen) {
455 1.2.4.2 minoura #ifdef RPC_DEBUG
456 1.2.4.2 minoura perror("sendto");
457 1.2.4.2 minoura #endif
458 1.2.4.2 minoura warnx("clnt_bcast: cannot send"
459 1.2.4.2 minoura "broadcast packet");
460 1.2.4.2 minoura stat = RPC_CANTSEND;
461 1.2.4.2 minoura continue;
462 1.2.4.2 minoura };
463 1.2.4.2 minoura #ifdef RPC_DEBUG
464 1.2.4.2 minoura if (!__rpc_lowvers)
465 1.2.4.2 minoura fprintf(stderr, "Broadcast packet sent "
466 1.2.4.2 minoura "for %s\n",
467 1.2.4.2 minoura fdlist[i].nconf->nc_netid);
468 1.2.4.2 minoura #endif
469 1.2.4.2 minoura #ifdef PORTMAP
470 1.2.4.2 minoura /*
471 1.2.4.2 minoura * Send the version 2 packet also
472 1.2.4.2 minoura * for UDP/IP
473 1.2.4.2 minoura */
474 1.2.4.2 minoura if (fdlist[i].proto == IPPROTO_UDP) {
475 1.2.4.2 minoura if (sendto(fdlist[i].fd, outbuf_pmap,
476 1.2.4.2 minoura outlen_pmap, 0, addr,
477 1.2.4.2 minoura fdlist[i].asize) !=
478 1.2.4.2 minoura outlen_pmap) {
479 1.2.4.2 minoura warnx("clnt_bcast: "
480 1.2.4.2 minoura "Cannot send broadcast packet");
481 1.2.4.2 minoura stat = RPC_CANTSEND;
482 1.2.4.2 minoura continue;
483 1.2.4.2 minoura }
484 1.2.4.2 minoura }
485 1.2.4.2 minoura #ifdef RPC_DEBUG
486 1.2.4.2 minoura fprintf(stderr, "PMAP Broadcast packet "
487 1.2.4.2 minoura "sent for %s\n",
488 1.2.4.2 minoura fdlist[i].nconf->nc_netid);
489 1.2.4.2 minoura #endif
490 1.2.4.2 minoura #endif /* PORTMAP */
491 1.2.4.2 minoura }
492 1.2.4.2 minoura /* End for sending all packets on this transport */
493 1.2.4.2 minoura } /* End for sending on all transports */
494 1.2.4.2 minoura
495 1.2.4.2 minoura if (eachresult == NULL) {
496 1.2.4.2 minoura stat = RPC_SUCCESS;
497 1.2.4.2 minoura goto done_broad;
498 1.2.4.2 minoura }
499 1.2.4.2 minoura
500 1.2.4.2 minoura /*
501 1.2.4.2 minoura * Get all the replies from these broadcast requests
502 1.2.4.2 minoura */
503 1.2.4.2 minoura recv_again:
504 1.2.4.2 minoura
505 1.2.4.2 minoura switch (pollretval = poll(pfd, fdlistno, msec)) {
506 1.2.4.2 minoura case 0: /* timed out */
507 1.2.4.2 minoura stat = RPC_TIMEDOUT;
508 1.2.4.2 minoura continue;
509 1.2.4.2 minoura case -1: /* some kind of error - we ignore it */
510 1.2.4.2 minoura goto recv_again;
511 1.2.4.2 minoura } /* end of poll results switch */
512 1.2.4.2 minoura
513 1.2.4.2 minoura for (i = fds_found = 0;
514 1.2.4.2 minoura i < fdlistno && fds_found < pollretval; i++) {
515 1.2.4.2 minoura bool_t done = FALSE;
516 1.2.4.2 minoura
517 1.2.4.2 minoura if (pfd[i].revents == 0)
518 1.2.4.2 minoura continue;
519 1.2.4.2 minoura else if (pfd[i].revents & POLLNVAL) {
520 1.2.4.2 minoura /*
521 1.2.4.2 minoura * Something bad has happened to this descri-
522 1.2.4.2 minoura * ptor. We can cause poll() to ignore
523 1.2.4.2 minoura * it simply by using a negative fd. We do that
524 1.2.4.2 minoura * rather than compacting the pfd[] and fdlist[]
525 1.2.4.2 minoura * arrays.
526 1.2.4.2 minoura */
527 1.2.4.2 minoura pfd[i].fd = -1;
528 1.2.4.2 minoura fds_found++;
529 1.2.4.2 minoura continue;
530 1.2.4.2 minoura } else
531 1.2.4.2 minoura fds_found++;
532 1.2.4.2 minoura #ifdef RPC_DEBUG
533 1.2.4.2 minoura fprintf(stderr, "response for %s\n",
534 1.2.4.2 minoura fdlist[i].nconf->nc_netid);
535 1.2.4.2 minoura #endif
536 1.2.4.2 minoura try_again:
537 1.2.4.2 minoura inlen = recvfrom(fdlist[i].fd, inbuf, fdlist[i].dsize,
538 1.2.4.2 minoura 0, (struct sockaddr *)&fdlist[i].raddr,
539 1.2.4.2 minoura &fdlist[i].asize);
540 1.2.4.2 minoura if (inlen < 0) {
541 1.2.4.2 minoura if (errno == EINTR)
542 1.2.4.2 minoura goto try_again;
543 1.2.4.2 minoura warnx("clnt_bcast: Cannot receive reply to "
544 1.2.4.2 minoura "broadcast");
545 1.2.4.2 minoura stat = RPC_CANTRECV;
546 1.2.4.2 minoura continue;
547 1.2.4.2 minoura }
548 1.2.4.2 minoura if (inlen < sizeof (u_int32_t))
549 1.2.4.2 minoura continue; /* Drop that and go ahead */
550 1.2.4.2 minoura /*
551 1.2.4.2 minoura * see if reply transaction id matches sent id.
552 1.2.4.2 minoura * If so, decode the results. If return id is xid + 1
553 1.2.4.2 minoura * it was a PORTMAP reply
554 1.2.4.2 minoura */
555 1.2.4.2 minoura if (*((u_int32_t *)(inbuf)) == *((u_int32_t *)(outbuf))) {
556 1.2.4.2 minoura pmap_reply_flag = 0;
557 1.2.4.2 minoura msg.acpted_rply.ar_verf = _null_auth;
558 1.2.4.2 minoura msg.acpted_rply.ar_results.where =
559 1.2.4.2 minoura (caddr_t)&bres;
560 1.2.4.2 minoura msg.acpted_rply.ar_results.proc =
561 1.2.4.2 minoura (xdrproc_t)xdr_rpcb_rmtcallres;
562 1.2.4.2 minoura #ifdef PORTMAP
563 1.2.4.2 minoura } else if (pmap_flag &&
564 1.2.4.2 minoura *((u_int32_t *)(inbuf)) ==
565 1.2.4.2 minoura *((u_int32_t *)(outbuf_pmap))) {
566 1.2.4.2 minoura pmap_reply_flag = 1;
567 1.2.4.2 minoura msg.acpted_rply.ar_verf = _null_auth;
568 1.2.4.2 minoura msg.acpted_rply.ar_results.where =
569 1.2.4.2 minoura (caddr_t)&bres_pmap;
570 1.2.4.2 minoura msg.acpted_rply.ar_results.proc =
571 1.2.4.2 minoura (xdrproc_t)xdr_rmtcallres;
572 1.2.4.2 minoura #endif /* PORTMAP */
573 1.2.4.2 minoura } else
574 1.2.4.2 minoura continue;
575 1.2.4.2 minoura xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
576 1.2.4.2 minoura if (xdr_replymsg(xdrs, &msg)) {
577 1.2.4.2 minoura if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
578 1.2.4.2 minoura (msg.acpted_rply.ar_stat == SUCCESS)) {
579 1.2.4.2 minoura struct netbuf taddr, *np;
580 1.2.4.2 minoura struct sockaddr_in *sin;
581 1.2.4.2 minoura
582 1.2.4.2 minoura #ifdef PORTMAP
583 1.2.4.2 minoura if (pmap_flag && pmap_reply_flag) {
584 1.2.4.2 minoura sin = (struct sockaddr_in *)
585 1.2.4.2 minoura &fdlist[i].raddr;
586 1.2.4.2 minoura sin->sin_port =
587 1.2.4.2 minoura htons((u_short)port);
588 1.2.4.2 minoura taddr.len = taddr.maxlen =
589 1.2.4.2 minoura fdlist[i].raddr.ss_len;
590 1.2.4.2 minoura taddr.buf = &fdlist[i].raddr;
591 1.2.4.2 minoura done = (*eachresult)(resultsp,
592 1.2.4.2 minoura &taddr, fdlist[i].nconf);
593 1.2.4.2 minoura } else {
594 1.2.4.2 minoura #endif
595 1.2.4.2 minoura #ifdef RPC_DEBUG
596 1.2.4.2 minoura fprintf(stderr, "uaddr %s\n",
597 1.2.4.2 minoura uaddrp);
598 1.2.4.2 minoura #endif
599 1.2.4.2 minoura np = uaddr2taddr(
600 1.2.4.2 minoura fdlist[i].nconf, uaddrp);
601 1.2.4.2 minoura done = (*eachresult)(resultsp,
602 1.2.4.2 minoura np, fdlist[i].nconf);
603 1.2.4.2 minoura free(np);
604 1.2.4.2 minoura #ifdef PORTMAP
605 1.2.4.2 minoura }
606 1.2.4.2 minoura #endif
607 1.2.4.2 minoura }
608 1.2.4.2 minoura /* otherwise, we just ignore the errors ... */
609 1.2.4.2 minoura }
610 1.2.4.2 minoura /* else some kind of deserialization problem ... */
611 1.2.4.2 minoura
612 1.2.4.2 minoura xdrs->x_op = XDR_FREE;
613 1.2.4.2 minoura msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
614 1.2.4.2 minoura (void) xdr_replymsg(xdrs, &msg);
615 1.2.4.2 minoura (void) (*xresults)(xdrs, resultsp);
616 1.2.4.2 minoura XDR_DESTROY(xdrs);
617 1.2.4.2 minoura if (done) {
618 1.2.4.2 minoura stat = RPC_SUCCESS;
619 1.2.4.2 minoura goto done_broad;
620 1.2.4.2 minoura } else {
621 1.2.4.2 minoura goto recv_again;
622 1.2.4.2 minoura }
623 1.2.4.2 minoura } /* The recv for loop */
624 1.2.4.2 minoura } /* The giant for loop */
625 1.2.4.2 minoura
626 1.2.4.2 minoura done_broad:
627 1.2.4.2 minoura if (inbuf)
628 1.2.4.2 minoura (void) free(inbuf);
629 1.2.4.2 minoura if (outbuf)
630 1.2.4.2 minoura (void) free(outbuf);
631 1.2.4.2 minoura #ifdef PORTMAP
632 1.2.4.2 minoura if (outbuf_pmap)
633 1.2.4.2 minoura (void) free(outbuf_pmap);
634 1.2.4.2 minoura #endif
635 1.2.4.2 minoura for (i = 0; i < fdlistno; i++) {
636 1.2.4.2 minoura (void) close(fdlist[i].fd);
637 1.2.4.2 minoura __rpc_freebroadifs(&fdlist[i].nal);
638 1.2.4.2 minoura }
639 1.2.4.2 minoura AUTH_DESTROY(sys_auth);
640 1.2.4.2 minoura (void) __rpc_endconf(handle);
641 1.2.4.2 minoura
642 1.2.4.2 minoura return (stat);
643 1.2.4.2 minoura }
644 1.2.4.2 minoura
645 1.2.4.2 minoura
646 1.2.4.2 minoura enum clnt_stat
647 1.2.4.2 minoura rpc_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp,
648 1.2.4.2 minoura eachresult, nettype)
649 1.2.4.2 minoura rpcprog_t prog; /* program number */
650 1.2.4.2 minoura rpcvers_t vers; /* version number */
651 1.2.4.2 minoura rpcproc_t proc; /* procedure number */
652 1.2.4.2 minoura xdrproc_t xargs; /* xdr routine for args */
653 1.2.4.2 minoura caddr_t argsp; /* pointer to args */
654 1.2.4.2 minoura xdrproc_t xresults; /* xdr routine for results */
655 1.2.4.2 minoura caddr_t resultsp; /* pointer to results */
656 1.2.4.2 minoura resultproc_t eachresult; /* call with each result obtained */
657 1.2.4.2 minoura const char *nettype; /* transport type */
658 1.2.4.2 minoura {
659 1.2.4.2 minoura enum clnt_stat dummy;
660 1.2.4.2 minoura
661 1.2.4.2 minoura dummy = rpc_broadcast_exp(prog, vers, proc, xargs, argsp,
662 1.2.4.2 minoura xresults, resultsp, eachresult,
663 1.2.4.2 minoura INITTIME, WAITTIME, nettype);
664 1.2.4.2 minoura return (dummy);
665 1.2.4.2 minoura }
666