Fix another x86 sys/ucontext.h namespace issue (bug 21457).
[glibc.git] / sunrpc / pmap_rmt.c
blob6d4ed7206cbd002c756613e688b8eb635c9129c4
1 /*
2 * pmap_rmt.c
3 * Client interface to pmap rpc service.
4 * remote call and broadcast service
6 * Copyright (c) 2010, Oracle America, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
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.
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.
36 #include <unistd.h>
37 #include <string.h>
38 #include <libintl.h>
39 #include <rpc/rpc.h>
40 #include <rpc/pmap_prot.h>
41 #include <rpc/pmap_clnt.h>
42 #include <rpc/pmap_rmt.h>
43 #include <sys/poll.h>
44 #include <sys/socket.h>
45 #include <stdio.h>
46 #include <errno.h>
47 #include <sys/param.h>
48 #include <net/if.h>
49 #include <ifaddrs.h>
50 #include <sys/ioctl.h>
51 #include <arpa/inet.h>
52 #include <shlib-compat.h>
54 #define MAX_BROADCAST_SIZE 1400
56 extern u_long _create_xid (void);
58 static const struct timeval timeout = {3, 0};
61 * pmapper remote-call-service interface.
62 * This routine is used to call the pmapper remote call service
63 * which will look up a service program in the port maps, and then
64 * remotely call that routine with the given parameters. This allows
65 * programs to do a lookup and call in one step.
67 enum clnt_stat
68 pmap_rmtcall (struct sockaddr_in *addr, u_long prog, u_long vers, u_long proc,
69 xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres, caddr_t resp,
70 struct timeval tout, u_long *port_ptr)
72 int socket = -1;
73 CLIENT *client;
74 struct rmtcallargs a;
75 struct rmtcallres r;
76 enum clnt_stat stat;
78 addr->sin_port = htons (PMAPPORT);
79 client = clntudp_create (addr, PMAPPROG, PMAPVERS, timeout, &socket);
80 if (client != (CLIENT *) NULL)
82 a.prog = prog;
83 a.vers = vers;
84 a.proc = proc;
85 a.args_ptr = argsp;
86 a.xdr_args = xdrargs;
87 r.port_ptr = port_ptr;
88 r.results_ptr = resp;
89 r.xdr_results = xdrres;
90 stat = CLNT_CALL (client, PMAPPROC_CALLIT,
91 (xdrproc_t)xdr_rmtcall_args,
92 (caddr_t)&a, (xdrproc_t)xdr_rmtcallres,
93 (caddr_t)&r, tout);
94 CLNT_DESTROY (client);
96 else
98 stat = RPC_FAILED;
100 /* (void)__close(socket); CLNT_DESTROY already closed it */
101 addr->sin_port = 0;
102 return stat;
104 libc_hidden_nolink_sunrpc (pmap_rmtcall, GLIBC_2_0)
108 * XDR remote call arguments
109 * written for XDR_ENCODE direction only
111 bool_t
112 xdr_rmtcall_args (XDR *xdrs, struct rmtcallargs *cap)
114 u_int lenposition, argposition, position;
116 if (xdr_u_long (xdrs, &(cap->prog)) &&
117 xdr_u_long (xdrs, &(cap->vers)) &&
118 xdr_u_long (xdrs, &(cap->proc)))
120 u_long dummy_arglen = 0;
121 lenposition = XDR_GETPOS (xdrs);
122 if (!xdr_u_long (xdrs, &dummy_arglen))
123 return FALSE;
124 argposition = XDR_GETPOS (xdrs);
125 if (!(*(cap->xdr_args)) (xdrs, cap->args_ptr))
126 return FALSE;
127 position = XDR_GETPOS (xdrs);
128 cap->arglen = (u_long) position - (u_long) argposition;
129 XDR_SETPOS (xdrs, lenposition);
130 if (!xdr_u_long (xdrs, &(cap->arglen)))
131 return FALSE;
132 XDR_SETPOS (xdrs, position);
133 return TRUE;
135 return FALSE;
137 libc_hidden_nolink_sunrpc (xdr_rmtcall_args, GLIBC_2_0)
140 * XDR remote call results
141 * written for XDR_DECODE direction only
143 bool_t
144 xdr_rmtcallres (XDR *xdrs, struct rmtcallres *crp)
146 caddr_t port_ptr;
148 port_ptr = (caddr_t) crp->port_ptr;
149 if (xdr_reference (xdrs, &port_ptr, sizeof (u_long),
150 (xdrproc_t) xdr_u_long)
151 && xdr_u_long (xdrs, &crp->resultslen))
153 crp->port_ptr = (u_long *) port_ptr;
154 return (*(crp->xdr_results)) (xdrs, crp->results_ptr);
156 return FALSE;
158 libc_hidden_nolink_sunrpc (xdr_rmtcallres, GLIBC_2_0)
162 * The following is kludged-up support for simple rpc broadcasts.
163 * Someday a large, complicated system will replace these trivial
164 * routines which only support udp/ip .
167 static int
168 internal_function
169 getbroadcastnets (struct in_addr *addrs, int naddrs)
171 struct ifaddrs *ifa;
173 if (getifaddrs (&ifa) != 0)
175 perror ("broadcast: getifaddrs");
176 return 0;
179 int i = 0;
180 struct ifaddrs *run = ifa;
181 while (run != NULL && i < naddrs)
183 if ((run->ifa_flags & IFF_BROADCAST) != 0
184 && (run->ifa_flags & IFF_UP) != 0
185 && run->ifa_addr != NULL
186 && run->ifa_addr->sa_family == AF_INET)
187 /* Copy the broadcast address. */
188 addrs[i++] = ((struct sockaddr_in *) run->ifa_broadaddr)->sin_addr;
190 run = run->ifa_next;
193 freeifaddrs (ifa);
195 return i;
199 enum clnt_stat
200 clnt_broadcast (/* program number */
201 u_long prog,
202 /* version number */
203 u_long vers,
204 /* procedure number */
205 u_long proc,
206 /* xdr routine for args */
207 xdrproc_t xargs,
208 /* pointer to args */
209 caddr_t argsp,
210 /* xdr routine for results */
211 xdrproc_t xresults,
212 /* pointer to results */
213 caddr_t resultsp,
214 /* call with each result obtained */
215 resultproc_t eachresult)
217 enum clnt_stat stat = RPC_FAILED;
218 AUTH *unix_auth = authunix_create_default ();
219 XDR xdr_stream;
220 XDR *xdrs = &xdr_stream;
221 struct timeval t;
222 int outlen, inlen, nets;
223 socklen_t fromlen;
224 int sock;
225 int on = 1;
226 struct pollfd fd;
227 int milliseconds;
228 int i;
229 bool_t done = FALSE;
230 u_long xid;
231 u_long port;
232 struct in_addr addrs[20];
233 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
234 struct rmtcallargs a;
235 struct rmtcallres r;
236 struct rpc_msg msg;
237 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
240 * initialization: create a socket, a broadcast address, and
241 * preserialize the arguments into a send buffer.
243 if ((sock = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
245 perror (_("Cannot create socket for broadcast rpc"));
246 stat = RPC_CANTSEND;
247 goto done_broad;
249 #ifdef SO_BROADCAST
250 if (__setsockopt (sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0)
252 perror (_("Cannot set socket option SO_BROADCAST"));
253 stat = RPC_CANTSEND;
254 goto done_broad;
256 #endif /* def SO_BROADCAST */
257 fd.fd = sock;
258 fd.events = POLLIN;
259 nets = getbroadcastnets (addrs, sizeof (addrs) / sizeof (addrs[0]));
260 memset ((char *) &baddr, 0, sizeof (baddr));
261 baddr.sin_family = AF_INET;
262 baddr.sin_port = htons (PMAPPORT);
263 baddr.sin_addr.s_addr = htonl (INADDR_ANY);
264 /* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
265 msg.rm_xid = xid = _create_xid ();
266 t.tv_usec = 0;
267 msg.rm_direction = CALL;
268 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
269 msg.rm_call.cb_prog = PMAPPROG;
270 msg.rm_call.cb_vers = PMAPVERS;
271 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
272 msg.rm_call.cb_cred = unix_auth->ah_cred;
273 msg.rm_call.cb_verf = unix_auth->ah_verf;
274 a.prog = prog;
275 a.vers = vers;
276 a.proc = proc;
277 a.xdr_args = xargs;
278 a.args_ptr = argsp;
279 r.port_ptr = &port;
280 r.xdr_results = xresults;
281 r.results_ptr = resultsp;
282 xdrmem_create (xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
283 if ((!xdr_callmsg (xdrs, &msg))
284 || (!xdr_rmtcall_args (xdrs, &a)))
286 stat = RPC_CANTENCODEARGS;
287 goto done_broad;
289 outlen = (int) xdr_getpos (xdrs);
290 xdr_destroy (xdrs);
292 * Basic loop: broadcast a packet and wait a while for response(s).
293 * The response timeout grows larger per iteration.
295 for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2)
297 for (i = 0; i < nets; i++)
299 baddr.sin_addr = addrs[i];
300 if (__sendto (sock, outbuf, outlen, 0,
301 (struct sockaddr *) &baddr,
302 sizeof (struct sockaddr)) != outlen)
304 perror (_("Cannot send broadcast packet"));
305 stat = RPC_CANTSEND;
306 goto done_broad;
309 if (eachresult == NULL)
311 stat = RPC_SUCCESS;
312 goto done_broad;
314 recv_again:
315 msg.acpted_rply.ar_verf = _null_auth;
316 msg.acpted_rply.ar_results.where = (caddr_t) & r;
317 msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_rmtcallres;
318 milliseconds = t.tv_sec * 1000 + t.tv_usec / 1000;
319 switch (__poll(&fd, 1, milliseconds))
322 case 0: /* timed out */
323 stat = RPC_TIMEDOUT;
324 continue;
326 case -1: /* some kind of error */
327 if (errno == EINTR)
328 goto recv_again;
329 perror (_("Broadcast poll problem"));
330 stat = RPC_CANTRECV;
331 goto done_broad;
333 } /* end of poll results switch */
334 try_again:
335 fromlen = sizeof (struct sockaddr);
336 inlen = __recvfrom (sock, inbuf, UDPMSGSIZE, 0,
337 (struct sockaddr *) &raddr, &fromlen);
338 if (inlen < 0)
340 if (errno == EINTR)
341 goto try_again;
342 perror (_("Cannot receive reply to broadcast"));
343 stat = RPC_CANTRECV;
344 goto done_broad;
346 if ((size_t) inlen < sizeof (u_long))
347 goto recv_again;
349 * see if reply transaction id matches sent id.
350 * If so, decode the results.
352 xdrmem_create (xdrs, inbuf, (u_int) inlen, XDR_DECODE);
353 if (xdr_replymsg (xdrs, &msg))
355 if (((u_int32_t) msg.rm_xid == (u_int32_t) xid) &&
356 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
357 (msg.acpted_rply.ar_stat == SUCCESS))
359 raddr.sin_port = htons ((u_short) port);
360 done = (*eachresult) (resultsp, &raddr);
362 /* otherwise, we just ignore the errors ... */
364 else
366 #ifdef notdef
367 /* some kind of deserialization problem ... */
368 if ((u_int32_t) msg.rm_xid == (u_int32_t) xid)
369 fprintf (stderr, "Broadcast deserialization problem");
370 /* otherwise, just random garbage */
371 #endif
373 xdrs->x_op = XDR_FREE;
374 msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
375 (void) xdr_replymsg (xdrs, &msg);
376 (void) (*xresults) (xdrs, resultsp);
377 xdr_destroy (xdrs);
378 if (done)
380 stat = RPC_SUCCESS;
381 goto done_broad;
383 else
385 goto recv_again;
388 done_broad:
389 (void) __close (sock);
390 AUTH_DESTROY (unix_auth);
391 return stat;
393 libc_hidden_nolink_sunrpc (clnt_broadcast, GLIBC_2_0)