]> git.ipfire.org Git - thirdparty/glibc.git/blame - sunrpc/pmap_rmt.c
Merge glibc-ports into ports/ directory.
[thirdparty/glibc.git] / sunrpc / pmap_rmt.c
CommitLineData
28f540f4
RM
1/*
2 * pmap_rmt.c
3 * Client interface to pmap rpc service.
4 * remote call and broadcast service
5 *
a7ab6ec8
UD
6 * Copyright (c) 2010, Oracle America, Inc.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials
17 * provided with the distribution.
18 * * Neither the name of the "Oracle America, Inc." nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28f540f4
RM
34 */
35
e7fd8a39
UD
36#include <unistd.h>
37#include <string.h>
4360eafd 38#include <libintl.h>
28f540f4
RM
39#include <rpc/rpc.h>
40#include <rpc/pmap_prot.h>
41#include <rpc/pmap_clnt.h>
42#include <rpc/pmap_rmt.h>
099a6fbd 43#include <sys/poll.h>
28f540f4
RM
44#include <sys/socket.h>
45#include <stdio.h>
46#include <errno.h>
47#undef _POSIX_SOURCE /* Ultrix <sys/param.h> needs --roland@gnu */
48#include <sys/param.h> /* Ultrix needs before net/if --roland@gnu */
49#include <net/if.h>
412c954a 50#include <ifaddrs.h>
28f540f4
RM
51#include <sys/ioctl.h>
52#include <arpa/inet.h>
53#define MAX_BROADCAST_SIZE 1400
54
090ca000
UD
55extern u_long _create_xid (void);
56
c4563d2d 57static const struct timeval timeout = {3, 0};
28f540f4
RM
58
59/*
60 * pmapper remote-call-service interface.
61 * This routine is used to call the pmapper remote call service
62 * which will look up a service program in the port maps, and then
63 * remotely call that routine with the given parameters. This allows
64 * programs to do a lookup and call in one step.
e7fd8a39 65 */
28f540f4 66enum clnt_stat
e7fd8a39
UD
67pmap_rmtcall (addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
68 struct sockaddr_in *addr;
69 u_long prog, vers, proc;
70 xdrproc_t xdrargs, xdrres;
71 caddr_t argsp, resp;
72 struct timeval tout;
73 u_long *port_ptr;
28f540f4 74{
e7fd8a39
UD
75 int socket = -1;
76 CLIENT *client;
77 struct rmtcallargs a;
78 struct rmtcallres r;
79 enum clnt_stat stat;
28f540f4 80
e7fd8a39 81 addr->sin_port = htons (PMAPPORT);
7b57bfe5 82 client = clntudp_create (addr, PMAPPROG, PMAPVERS, timeout, &socket);
e7fd8a39
UD
83 if (client != (CLIENT *) NULL)
84 {
85 a.prog = prog;
86 a.vers = vers;
87 a.proc = proc;
88 a.args_ptr = argsp;
89 a.xdr_args = xdrargs;
90 r.port_ptr = port_ptr;
91 r.results_ptr = resp;
92 r.xdr_results = xdrres;
77fe0b9c 93 stat = CLNT_CALL (client, PMAPPROC_CALLIT,
7b57bfe5
UD
94 (xdrproc_t)xdr_rmtcall_args,
95 (caddr_t)&a, (xdrproc_t)xdr_rmtcallres,
e7fd8a39
UD
96 (caddr_t)&r, tout);
97 CLNT_DESTROY (client);
98 }
99 else
100 {
101 stat = RPC_FAILED;
102 }
50304ef0 103 /* (void)__close(socket); CLNT_DESTROY already closed it */
e7fd8a39
UD
104 addr->sin_port = 0;
105 return stat;
28f540f4 106}
021db4be 107libc_hidden_nolink_sunrpc (pmap_rmtcall, GLIBC_2_0)
28f540f4
RM
108
109
110/*
111 * XDR remote call arguments
112 * written for XDR_ENCODE direction only
113 */
114bool_t
090ca000 115xdr_rmtcall_args (XDR *xdrs, struct rmtcallargs *cap)
28f540f4 116{
e7fd8a39 117 u_int lenposition, argposition, position;
28f540f4 118
7b57bfe5
UD
119 if (xdr_u_long (xdrs, &(cap->prog)) &&
120 xdr_u_long (xdrs, &(cap->vers)) &&
121 xdr_u_long (xdrs, &(cap->proc)))
e7fd8a39 122 {
7a50b1f6 123 u_long dummy_arglen = 0;
e7fd8a39 124 lenposition = XDR_GETPOS (xdrs);
7b57bfe5 125 if (!xdr_u_long (xdrs, &dummy_arglen))
e7fd8a39
UD
126 return FALSE;
127 argposition = XDR_GETPOS (xdrs);
128 if (!(*(cap->xdr_args)) (xdrs, cap->args_ptr))
129 return FALSE;
130 position = XDR_GETPOS (xdrs);
131 cap->arglen = (u_long) position - (u_long) argposition;
132 XDR_SETPOS (xdrs, lenposition);
7b57bfe5 133 if (!xdr_u_long (xdrs, &(cap->arglen)))
e7fd8a39
UD
134 return FALSE;
135 XDR_SETPOS (xdrs, position);
136 return TRUE;
137 }
138 return FALSE;
28f540f4 139}
021db4be 140libc_hidden_nolink_sunrpc (xdr_rmtcall_args, GLIBC_2_0)
28f540f4
RM
141
142/*
143 * XDR remote call results
144 * written for XDR_DECODE direction only
145 */
146bool_t
e7fd8a39
UD
147xdr_rmtcallres (xdrs, crp)
148 XDR *xdrs;
149 struct rmtcallres *crp;
28f540f4 150{
e7fd8a39 151 caddr_t port_ptr;
28f540f4 152
e7fd8a39 153 port_ptr = (caddr_t) crp->port_ptr;
7b57bfe5
UD
154 if (xdr_reference (xdrs, &port_ptr, sizeof (u_long),
155 (xdrproc_t) xdr_u_long)
156 && xdr_u_long (xdrs, &crp->resultslen))
e7fd8a39
UD
157 {
158 crp->port_ptr = (u_long *) port_ptr;
159 return (*(crp->xdr_results)) (xdrs, crp->results_ptr);
160 }
161 return FALSE;
28f540f4 162}
021db4be 163libc_hidden_nolink_sunrpc (xdr_rmtcallres, GLIBC_2_0)
28f540f4
RM
164
165
166/*
167 * The following is kludged-up support for simple rpc broadcasts.
cbd3dceb 168 * Someday a large, complicated system will replace these trivial
28f540f4
RM
169 * routines which only support udp/ip .
170 */
171
172static int
dfd2257a 173internal_function
412c954a 174getbroadcastnets (struct in_addr *addrs, int naddrs)
28f540f4 175{
412c954a 176 struct ifaddrs *ifa;
28f540f4 177
6c81dc42 178 if (getifaddrs (&ifa) != 0)
e7fd8a39 179 {
412c954a
UD
180 perror ("broadcast: getifaddrs");
181 return 0;
e7fd8a39 182 }
412c954a
UD
183
184 int i = 0;
185 struct ifaddrs *run = ifa;
186 while (run != NULL && i < naddrs)
e7fd8a39 187 {
412c954a
UD
188 if ((run->ifa_flags & IFF_BROADCAST) != 0
189 && (run->ifa_flags & IFF_UP) != 0
f7e7a396 190 && run->ifa_addr != NULL
412c954a
UD
191 && run->ifa_addr->sa_family == AF_INET)
192 /* Copy the broadcast address. */
193 addrs[i++] = ((struct sockaddr_in *) run->ifa_broadaddr)->sin_addr;
194
195 run = run->ifa_next;
e7fd8a39 196 }
412c954a
UD
197
198 freeifaddrs (ifa);
199
e7fd8a39 200 return i;
28f540f4
RM
201}
202
28f540f4 203
cbd3dceb 204enum clnt_stat
e7fd8a39
UD
205clnt_broadcast (prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
206 u_long prog; /* program number */
207 u_long vers; /* version number */
208 u_long proc; /* procedure number */
209 xdrproc_t xargs; /* xdr routine for args */
210 caddr_t argsp; /* pointer to args */
211 xdrproc_t xresults; /* xdr routine for results */
212 caddr_t resultsp; /* pointer to results */
213 resultproc_t eachresult; /* call with each result obtained */
28f540f4 214{
a1129917 215 enum clnt_stat stat = RPC_FAILED;
7b57bfe5 216 AUTH *unix_auth = authunix_create_default ();
e7fd8a39
UD
217 XDR xdr_stream;
218 XDR *xdrs = &xdr_stream;
090ca000 219 struct timeval t;
f671aeab 220 int outlen, inlen, nets;
41df5ed4 221 socklen_t fromlen;
e7fd8a39
UD
222 int sock;
223 int on = 1;
099a6fbd
UD
224 struct pollfd fd;
225 int milliseconds;
e7fd8a39
UD
226 int i;
227 bool_t done = FALSE;
228 u_long xid;
229 u_long port;
230 struct in_addr addrs[20];
231 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
232 struct rmtcallargs a;
233 struct rmtcallres r;
234 struct rpc_msg msg;
e7fd8a39 235 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
28f540f4 236
e7fd8a39
UD
237 /*
238 * initialization: create a socket, a broadcast address, and
239 * preserialize the arguments into a send buffer.
240 */
50304ef0 241 if ((sock = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
e7fd8a39
UD
242 {
243 perror (_("Cannot create socket for broadcast rpc"));
244 stat = RPC_CANTSEND;
245 goto done_broad;
246 }
28f540f4 247#ifdef SO_BROADCAST
b2bffca2 248 if (__setsockopt (sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0)
e7fd8a39
UD
249 {
250 perror (_("Cannot set socket option SO_BROADCAST"));
251 stat = RPC_CANTSEND;
252 goto done_broad;
253 }
28f540f4 254#endif /* def SO_BROADCAST */
099a6fbd
UD
255 fd.fd = sock;
256 fd.events = POLLIN;
412c954a 257 nets = getbroadcastnets (addrs, sizeof (addrs) / sizeof (addrs[0]));
50304ef0 258 __bzero ((char *) &baddr, sizeof (baddr));
e7fd8a39
UD
259 baddr.sin_family = AF_INET;
260 baddr.sin_port = htons (PMAPPORT);
261 baddr.sin_addr.s_addr = htonl (INADDR_ANY);
262/* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
090ca000 263 msg.rm_xid = xid = _create_xid ();
e7fd8a39
UD
264 t.tv_usec = 0;
265 msg.rm_direction = CALL;
266 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
267 msg.rm_call.cb_prog = PMAPPROG;
268 msg.rm_call.cb_vers = PMAPVERS;
269 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
270 msg.rm_call.cb_cred = unix_auth->ah_cred;
271 msg.rm_call.cb_verf = unix_auth->ah_verf;
272 a.prog = prog;
273 a.vers = vers;
274 a.proc = proc;
275 a.xdr_args = xargs;
276 a.args_ptr = argsp;
277 r.port_ptr = &port;
278 r.xdr_results = xresults;
279 r.results_ptr = resultsp;
7b57bfe5
UD
280 xdrmem_create (xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
281 if ((!xdr_callmsg (xdrs, &msg))
282 || (!xdr_rmtcall_args (xdrs, &a)))
e7fd8a39
UD
283 {
284 stat = RPC_CANTENCODEARGS;
285 goto done_broad;
286 }
287 outlen = (int) xdr_getpos (xdrs);
288 xdr_destroy (xdrs);
289 /*
290 * Basic loop: broadcast a packet and wait a while for response(s).
291 * The response timeout grows larger per iteration.
292 */
293 for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2)
294 {
295 for (i = 0; i < nets; i++)
296 {
297 baddr.sin_addr = addrs[i];
b2bffca2
UD
298 if (__sendto (sock, outbuf, outlen, 0,
299 (struct sockaddr *) &baddr,
300 sizeof (struct sockaddr)) != outlen)
e7fd8a39
UD
301 {
302 perror (_("Cannot send broadcast packet"));
303 stat = RPC_CANTSEND;
304 goto done_broad;
305 }
28f540f4 306 }
e7fd8a39
UD
307 if (eachresult == NULL)
308 {
309 stat = RPC_SUCCESS;
310 goto done_broad;
311 }
312 recv_again:
313 msg.acpted_rply.ar_verf = _null_auth;
314 msg.acpted_rply.ar_results.where = (caddr_t) & r;
7b57bfe5 315 msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_rmtcallres;
099a6fbd
UD
316 milliseconds = t.tv_sec * 1000 + t.tv_usec / 1000;
317 switch (__poll(&fd, 1, milliseconds))
e7fd8a39 318 {
28f540f4 319
e7fd8a39
UD
320 case 0: /* timed out */
321 stat = RPC_TIMEDOUT;
322 continue;
28f540f4 323
e7fd8a39
UD
324 case -1: /* some kind of error */
325 if (errno == EINTR)
326 goto recv_again;
099a6fbd 327 perror (_("Broadcast poll problem"));
e7fd8a39
UD
328 stat = RPC_CANTRECV;
329 goto done_broad;
28f540f4 330
099a6fbd 331 } /* end of poll results switch */
e7fd8a39
UD
332 try_again:
333 fromlen = sizeof (struct sockaddr);
b2bffca2
UD
334 inlen = __recvfrom (sock, inbuf, UDPMSGSIZE, 0,
335 (struct sockaddr *) &raddr, &fromlen);
e7fd8a39
UD
336 if (inlen < 0)
337 {
338 if (errno == EINTR)
339 goto try_again;
340 perror (_("Cannot receive reply to broadcast"));
341 stat = RPC_CANTRECV;
342 goto done_broad;
343 }
1f205a47 344 if ((size_t) inlen < sizeof (u_long))
e7fd8a39
UD
345 goto recv_again;
346 /*
347 * see if reply transaction id matches sent id.
348 * If so, decode the results.
349 */
7b57bfe5
UD
350 xdrmem_create (xdrs, inbuf, (u_int) inlen, XDR_DECODE);
351 if (xdr_replymsg (xdrs, &msg))
e7fd8a39 352 {
5512461f 353 if (((u_int32_t) msg.rm_xid == (u_int32_t) xid) &&
e7fd8a39
UD
354 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
355 (msg.acpted_rply.ar_stat == SUCCESS))
356 {
357 raddr.sin_port = htons ((u_short) port);
358 done = (*eachresult) (resultsp, &raddr);
359 }
360 /* otherwise, we just ignore the errors ... */
361 }
362 else
363 {
28f540f4 364#ifdef notdef
e7fd8a39 365 /* some kind of deserialization problem ... */
5512461f 366 if ((u_int32_t) msg.rm_xid == (u_int32_t) xid)
e7fd8a39
UD
367 fprintf (stderr, "Broadcast deserialization problem");
368 /* otherwise, just random garbage */
28f540f4 369#endif
28f540f4 370 }
e7fd8a39 371 xdrs->x_op = XDR_FREE;
7b57bfe5
UD
372 msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
373 (void) xdr_replymsg (xdrs, &msg);
e7fd8a39
UD
374 (void) (*xresults) (xdrs, resultsp);
375 xdr_destroy (xdrs);
376 if (done)
377 {
378 stat = RPC_SUCCESS;
379 goto done_broad;
380 }
381 else
382 {
383 goto recv_again;
384 }
385 }
28f540f4 386done_broad:
50304ef0 387 (void) __close (sock);
e7fd8a39
UD
388 AUTH_DESTROY (unix_auth);
389 return stat;
28f540f4 390}
021db4be 391libc_hidden_nolink_sunrpc (clnt_broadcast, GLIBC_2_0)