blob: e4dbd44df44fefaf91bf3facf3b6c6e9b9f2c128 [file] [log] [blame]
Patrick Schaaf2dd59ef2012-02-27 22:27:31 +01001/* @(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC */
2/*
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
9 *
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 *
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
17 *
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
21 *
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
25 *
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
29 */
30#if 0
31static char sccsid[] = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
32#endif
33
34/*
35 * pmap_rmt.c
36 * Client interface to pmap rpc service.
37 * remote call and broadcast service
38 *
39 * Copyright (C) 1984, Sun Microsystems, Inc.
40 */
41
42#define __FORCE_GLIBC
43#include <features.h>
44
45#include <unistd.h>
46#include <string.h>
47#include <rpc/rpc.h>
48#include <rpc/pmap_prot.h>
49#include <rpc/pmap_clnt.h>
50#include <rpc/pmap_rmt.h>
51#include <sys/poll.h>
52#include <sys/socket.h>
53#include <stdio.h>
54#include <errno.h>
55#undef _POSIX_SOURCE /* Ultrix <sys/param.h> needs --roland@gnu */
56#include <sys/param.h> /* Ultrix needs before net/if --roland@gnu */
57#include <net/if.h>
58#include <sys/ioctl.h>
59#include <arpa/inet.h>
60#define MAX_BROADCAST_SIZE 1400
61
maxwen27116ba2015-08-14 21:41:28 +020062#if defined(ANDROID) && !defined(BIONIC_L)
63static struct in_addr inet_makeaddr(in_addr_t net, in_addr_t host)
Patrick Schaaf2dd59ef2012-02-27 22:27:31 +010064{
65 struct in_addr a;
66
67 if (net <= 0x000000ff)
68 a.s_addr = (net << 24) | (host & 0x000000ff);
69 else if (net <= 0x0000ffff)
70 a.s_addr = (net << 16) | (host & 0x0000ffff);
71 else if (net <= 0x00ffffff)
72 a.s_addr = (net << 8) | (host & 0x00ffffff);
73 else
74 a.s_addr = net | host;
75 a.s_addr = htonl(a.s_addr);
76 return a;
77}
maxwen27116ba2015-08-14 21:41:28 +020078static in_addr_t inet_netof(struct in_addr in)
Patrick Schaaf2dd59ef2012-02-27 22:27:31 +010079{
80 in_addr_t i = ntohl(in.s_addr);
81
82 if (i < 0x80000000)
83 return i >> 24;
84 if (i < 0xc0000000)
85 return i >> 16;
86 return i >> 8;
87}
88
89#endif
90
91
92extern u_long _create_xid (void) attribute_hidden;
93
94static const struct timeval timeout = {3, 0};
95
96/*
97 * pmapper remote-call-service interface.
98 * This routine is used to call the pmapper remote call service
99 * which will look up a service program in the port maps, and then
100 * remotely call that routine with the given parameters. This allows
101 * programs to do a lookup and call in one step.
102 */
103enum clnt_stat
104pmap_rmtcall (struct sockaddr_in *addr, u_long prog, u_long vers, u_long proc,
105 xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres, caddr_t resp,
106 struct timeval tout, u_long *port_ptr)
107{
108 int _socket = -1;
109 CLIENT *client;
110 struct rmtcallargs a;
111 struct rmtcallres r;
112 enum clnt_stat stat;
113
114 addr->sin_port = htons (PMAPPORT);
115 client = clntudp_create (addr, PMAPPROG, PMAPVERS, timeout, &_socket);
116 if (client != (CLIENT *) NULL)
117 {
118 a.prog = prog;
119 a.vers = vers;
120 a.proc = proc;
121 a.args_ptr = argsp;
122 a.xdr_args = xdrargs;
123 r.port_ptr = port_ptr;
124 r.results_ptr = resp;
125 r.xdr_results = xdrres;
126 stat = CLNT_CALL (client, PMAPPROC_CALLIT, (xdrproc_t)xdr_rmtcall_args,
127 (caddr_t)&a, (xdrproc_t)xdr_rmtcallres,
128 (caddr_t)&r, tout);
129 CLNT_DESTROY (client);
130 }
131 else
132 {
133 stat = RPC_FAILED;
134 }
135 /* (void)close(_socket); CLNT_DESTROY already closed it */
136 addr->sin_port = 0;
137 return stat;
138}
139
140
141/*
142 * XDR remote call arguments
143 * written for XDR_ENCODE direction only
144 */
145bool_t
146xdr_rmtcall_args (XDR *xdrs, struct rmtcallargs *cap)
147{
148 u_int lenposition, argposition, position;
149
150 if (xdr_u_long (xdrs, &(cap->prog)) &&
151 xdr_u_long (xdrs, &(cap->vers)) &&
152 xdr_u_long (xdrs, &(cap->proc)))
153 {
154 u_long dummy_arglen = 0;
155 lenposition = XDR_GETPOS (xdrs);
156 if (!xdr_u_long (xdrs, &dummy_arglen))
157 return FALSE;
158 argposition = XDR_GETPOS (xdrs);
159 if (!(*(cap->xdr_args)) (xdrs, cap->args_ptr))
160 return FALSE;
161 position = XDR_GETPOS (xdrs);
162 cap->arglen = (u_long) position - (u_long) argposition;
163 XDR_SETPOS (xdrs, lenposition);
164 if (!xdr_u_long (xdrs, &(cap->arglen)))
165 return FALSE;
166 XDR_SETPOS (xdrs, position);
167 return TRUE;
168 }
169 return FALSE;
170}
171libc_hidden_def(xdr_rmtcall_args)
172
173/*
174 * XDR remote call results
175 * written for XDR_DECODE direction only
176 */
177bool_t
178xdr_rmtcallres (XDR *xdrs, struct rmtcallres *crp)
179{
180 caddr_t port_ptr;
181
182 port_ptr = (caddr_t) crp->port_ptr;
183 if (xdr_reference (xdrs, &port_ptr, sizeof (u_long), (xdrproc_t) xdr_u_long)
184 && xdr_u_long (xdrs, &crp->resultslen))
185 {
186 crp->port_ptr = (u_long *) port_ptr;
187 return (*(crp->xdr_results)) (xdrs, crp->results_ptr);
188 }
189 return FALSE;
190}
191libc_hidden_def(xdr_rmtcallres)
192
193
194/*
195 * The following is kludged-up support for simple rpc broadcasts.
196 * Someday a large, complicated system will replace these trivial
197 * routines which only support udp/ip .
198 */
199
200static int
201internal_function
202getbroadcastnets (struct in_addr *addrs, int sock, char *buf)
203 /* int sock: any valid socket will do */
204 /* char *buf: why allocate more when we can use existing... */
205{
206 struct ifconf ifc;
207 struct ifreq ifreq, *ifr;
208 struct sockaddr_in *sin;
209 int n, i;
210
211 ifc.ifc_len = UDPMSGSIZE;
212 ifc.ifc_buf = buf;
213 if (ioctl (sock, SIOCGIFCONF, (char *) &ifc) < 0)
214 {
215 perror (_("broadcast: ioctl (get interface configuration)"));
216 return (0);
217 }
218 ifr = ifc.ifc_req;
219 for (i = 0, n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++)
220 {
221 ifreq = *ifr;
222 if (ioctl (sock, SIOCGIFFLAGS, (char *) &ifreq) < 0)
223 {
224 perror (_("broadcast: ioctl (get interface flags)"));
225 continue;
226 }
227 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
228 (ifreq.ifr_flags & IFF_UP) &&
229 ifr->ifr_addr.sa_family == AF_INET)
230 {
231 sin = (struct sockaddr_in *) &ifr->ifr_addr;
232#ifdef SIOCGIFBRDADDR /* 4.3BSD */
233 if (ioctl (sock, SIOCGIFBRDADDR, (char *) &ifreq) < 0)
234 {
235 addrs[i++] = inet_makeaddr (inet_netof
236 /* Changed to pass struct instead of s_addr member
237 by roland@gnu. */
238 (sin->sin_addr), INADDR_ANY);
239 }
240 else
241 {
242 addrs[i++] = ((struct sockaddr_in *)
243 &ifreq.ifr_addr)->sin_addr;
244 }
245#else /* 4.2 BSD */
246 addrs[i++] = inet_makeaddr (inet_netof
247 (sin->sin_addr.s_addr), INADDR_ANY);
248#endif
249 }
250 }
251 return i;
252}
253
254
255enum clnt_stat
256clnt_broadcast (
257 u_long prog, /* program number */
258 u_long vers, /* version number */
259 u_long proc, /* procedure number */
260 xdrproc_t xargs, /* xdr routine for args */
261 caddr_t argsp, /* pointer to args */
262 xdrproc_t xresults, /* xdr routine for results */
263 caddr_t resultsp, /* pointer to results */
264 resultproc_t eachresult /* call with each result obtained */)
265{
266 enum clnt_stat stat = RPC_FAILED;
267 AUTH *unix_auth = authunix_create_default ();
268 XDR xdr_stream;
269 XDR *xdrs = &xdr_stream;
270 struct timeval t;
271 int outlen, inlen, nets;
272 socklen_t fromlen;
273 int sock;
274 int on = 1;
275 struct pollfd fd;
276 int milliseconds;
277 int i;
278 bool_t done = FALSE;
279 u_long xid;
280 u_long port;
281 struct in_addr addrs[20];
282 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
283 struct rmtcallargs a;
284 struct rmtcallres r;
285 struct rpc_msg msg;
286 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
287
288 /*
289 * initialization: create a socket, a broadcast address, and
290 * preserialize the arguments into a send buffer.
291 */
292 if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
293 {
294 perror (_("Cannot create socket for broadcast rpc"));
295 stat = RPC_CANTSEND;
296 goto done_broad;
297 }
298#ifdef SO_BROADCAST
299 if (setsockopt (sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0)
300 {
301 perror (_("Cannot set socket option SO_BROADCAST"));
302 stat = RPC_CANTSEND;
303 goto done_broad;
304 }
305#endif /* def SO_BROADCAST */
306 fd.fd = sock;
307 fd.events = POLLIN;
308 nets = getbroadcastnets (addrs, sock, inbuf);
309 memset ((char *) &baddr, 0, sizeof (baddr));
310 baddr.sin_family = AF_INET;
311 baddr.sin_port = htons (PMAPPORT);
312 baddr.sin_addr.s_addr = htonl (INADDR_ANY);
313/* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
314 msg.rm_xid = xid = _create_xid ();
315 t.tv_usec = 0;
316 msg.rm_direction = CALL;
317 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
318 msg.rm_call.cb_prog = PMAPPROG;
319 msg.rm_call.cb_vers = PMAPVERS;
320 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
321 msg.rm_call.cb_cred = unix_auth->ah_cred;
322 msg.rm_call.cb_verf = unix_auth->ah_verf;
323 a.prog = prog;
324 a.vers = vers;
325 a.proc = proc;
326 a.xdr_args = xargs;
327 a.args_ptr = argsp;
328 r.port_ptr = &port;
329 r.xdr_results = xresults;
330 r.results_ptr = resultsp;
331 xdrmem_create (xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
332 if ((!xdr_callmsg (xdrs, &msg)) || (!xdr_rmtcall_args (xdrs, &a)))
333 {
334 stat = RPC_CANTENCODEARGS;
335 goto done_broad;
336 }
337 outlen = (int) xdr_getpos (xdrs);
338 xdr_destroy (xdrs);
339 /*
340 * Basic loop: broadcast a packet and wait a while for response(s).
341 * The response timeout grows larger per iteration.
342 */
343 for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2)
344 {
345 for (i = 0; i < nets; i++)
346 {
347 baddr.sin_addr = addrs[i];
348 if (sendto (sock, outbuf, outlen, 0,
349 (struct sockaddr *) &baddr,
350 sizeof (struct sockaddr)) != outlen)
351 {
352 perror (_("Cannot send broadcast packet"));
353 stat = RPC_CANTSEND;
354 goto done_broad;
355 }
356 }
357 if (eachresult == NULL)
358 {
359 stat = RPC_SUCCESS;
360 goto done_broad;
361 }
362 recv_again:
363 msg.acpted_rply.ar_verf = _null_auth;
364 msg.acpted_rply.ar_results.where = (caddr_t) & r;
365 msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_rmtcallres;
366 milliseconds = t.tv_sec * 1000 + t.tv_usec / 1000;
367 switch (poll(&fd, 1, milliseconds))
368 {
369
370 case 0: /* timed out */
371 stat = RPC_TIMEDOUT;
372 continue;
373
374 case -1: /* some kind of error */
375 if (errno == EINTR)
376 goto recv_again;
377 perror (_("Broadcast poll problem"));
378 stat = RPC_CANTRECV;
379 goto done_broad;
380
381 } /* end of poll results switch */
382 try_again:
383 fromlen = sizeof (struct sockaddr);
384 inlen = recvfrom (sock, inbuf, UDPMSGSIZE, 0,
385 (struct sockaddr *) &raddr, &fromlen);
386 if (inlen < 0)
387 {
388 if (errno == EINTR)
389 goto try_again;
390 perror (_("Cannot receive reply to broadcast"));
391 stat = RPC_CANTRECV;
392 goto done_broad;
393 }
394 if ((size_t) inlen < sizeof (u_long))
395 goto recv_again;
396 /*
397 * see if reply transaction id matches sent id.
398 * If so, decode the results.
399 */
400 xdrmem_create (xdrs, inbuf, (u_int) inlen, XDR_DECODE);
401 if (xdr_replymsg (xdrs, &msg))
402 {
403 if (((u_int32_t) msg.rm_xid == (u_int32_t) xid) &&
404 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
405 (msg.acpted_rply.ar_stat == SUCCESS))
406 {
407 raddr.sin_port = htons ((u_short) port);
408 done = (*eachresult) (resultsp, &raddr);
409 }
410 /* otherwise, we just ignore the errors ... */
411 }
412 else
413 {
414#ifdef notdef
415 /* some kind of deserialization problem ... */
416 if ((u_int32_t) msg.rm_xid == (u_int32_t) xid)
417 fprintf (stderr, "Broadcast deserialization problem");
418 /* otherwise, just random garbage */
419#endif
420 }
421 xdrs->x_op = XDR_FREE;
422 msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
423 (void) xdr_replymsg (xdrs, &msg);
424 (void) (*xresults) (xdrs, resultsp);
425 xdr_destroy (xdrs);
426 if (done)
427 {
428 stat = RPC_SUCCESS;
429 goto done_broad;
430 }
431 else
432 {
433 goto recv_again;
434 }
435 }
436done_broad:
437 (void) close (sock);
438 AUTH_DESTROY (unix_auth);
439 return stat;
440}