chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / sunrpc / pmap_rmt.c
1 /*
2  * pmap_rmt.c
3  * Client interface to pmap rpc service.
4  * remote call and broadcast service
5  *
6  * Copyright (C) 1984, Sun Microsystems, 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 Sun Microsystems, 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.
34  */
35
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 #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>
50 #include <ifaddrs.h>
51 #include <sys/ioctl.h>
52 #include <arpa/inet.h>
53 #define MAX_BROADCAST_SIZE 1400
54
55 extern u_long _create_xid (void);
56
57 static const struct timeval timeout = {3, 0};
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.
65  */
66 enum clnt_stat
67 pmap_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;
74 {
75   int socket = -1;
76   CLIENT *client;
77   struct rmtcallargs a;
78   struct rmtcallres r;
79   enum clnt_stat stat;
80
81   addr->sin_port = htons (PMAPPORT);
82   client = INTUSE(clntudp_create) (addr, PMAPPROG, PMAPVERS, timeout, &socket);
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;
93       stat = CLNT_CALL (client, PMAPPROC_CALLIT,
94                         (xdrproc_t)INTUSE(xdr_rmtcall_args),
95                         (caddr_t)&a, (xdrproc_t)INTUSE(xdr_rmtcallres),
96                         (caddr_t)&r, tout);
97       CLNT_DESTROY (client);
98     }
99   else
100     {
101       stat = RPC_FAILED;
102     }
103   /* (void)__close(socket); CLNT_DESTROY already closed it */
104   addr->sin_port = 0;
105   return stat;
106 }
107
108
109 /*
110  * XDR remote call arguments
111  * written for XDR_ENCODE direction only
112  */
113 bool_t
114 xdr_rmtcall_args (XDR *xdrs, struct rmtcallargs *cap)
115 {
116   u_int lenposition, argposition, position;
117
118   if (INTUSE(xdr_u_long) (xdrs, &(cap->prog)) &&
119       INTUSE(xdr_u_long) (xdrs, &(cap->vers)) &&
120       INTUSE(xdr_u_long) (xdrs, &(cap->proc)))
121     {
122       u_long dummy_arglen = 0;
123       lenposition = XDR_GETPOS (xdrs);
124       if (!INTUSE(xdr_u_long) (xdrs, &dummy_arglen))
125         return FALSE;
126       argposition = XDR_GETPOS (xdrs);
127       if (!(*(cap->xdr_args)) (xdrs, cap->args_ptr))
128         return FALSE;
129       position = XDR_GETPOS (xdrs);
130       cap->arglen = (u_long) position - (u_long) argposition;
131       XDR_SETPOS (xdrs, lenposition);
132       if (!INTUSE(xdr_u_long) (xdrs, &(cap->arglen)))
133         return FALSE;
134       XDR_SETPOS (xdrs, position);
135       return TRUE;
136     }
137   return FALSE;
138 }
139 INTDEF(xdr_rmtcall_args)
140
141 /*
142  * XDR remote call results
143  * written for XDR_DECODE direction only
144  */
145 bool_t
146 xdr_rmtcallres (xdrs, crp)
147      XDR *xdrs;
148      struct rmtcallres *crp;
149 {
150   caddr_t port_ptr;
151
152   port_ptr = (caddr_t) crp->port_ptr;
153   if (INTUSE(xdr_reference) (xdrs, &port_ptr, sizeof (u_long),
154                              (xdrproc_t) INTUSE(xdr_u_long))
155       && INTUSE(xdr_u_long) (xdrs, &crp->resultslen))
156     {
157       crp->port_ptr = (u_long *) port_ptr;
158       return (*(crp->xdr_results)) (xdrs, crp->results_ptr);
159     }
160   return FALSE;
161 }
162 INTDEF(xdr_rmtcallres)
163
164
165 /*
166  * The following is kludged-up support for simple rpc broadcasts.
167  * Someday a large, complicated system will replace these trivial
168  * routines which only support udp/ip .
169  */
170
171 static int
172 internal_function
173 getbroadcastnets (struct in_addr *addrs, int naddrs)
174 {
175   struct ifaddrs *ifa;
176
177   if (getifaddrs (&ifa) != 0)
178     {
179       perror ("broadcast: getifaddrs");
180       return 0;
181     }
182
183   int i = 0;
184   struct ifaddrs *run = ifa;
185   while (run != NULL && i < naddrs)
186     {
187       if ((run->ifa_flags & IFF_BROADCAST) != 0
188           && (run->ifa_flags & IFF_UP) != 0
189           && run->ifa_addr != NULL
190           && run->ifa_addr->sa_family == AF_INET)
191         /* Copy the broadcast address.  */
192         addrs[i++] = ((struct sockaddr_in *) run->ifa_broadaddr)->sin_addr;
193
194       run = run->ifa_next;
195     }
196
197   freeifaddrs (ifa);
198
199   return i;
200 }
201
202
203 enum clnt_stat
204 clnt_broadcast (prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
205      u_long prog;               /* program number */
206      u_long vers;               /* version number */
207      u_long proc;               /* procedure number */
208      xdrproc_t xargs;           /* xdr routine for args */
209      caddr_t argsp;             /* pointer to args */
210      xdrproc_t xresults;        /* xdr routine for results */
211      caddr_t resultsp;          /* pointer to results */
212      resultproc_t eachresult;   /* call with each result obtained */
213 {
214   enum clnt_stat stat = RPC_FAILED;
215   AUTH *unix_auth = INTUSE(authunix_create_default) ();
216   XDR xdr_stream;
217   XDR *xdrs = &xdr_stream;
218   struct timeval t;
219   int outlen, inlen, nets;
220   socklen_t fromlen;
221   int sock;
222   int on = 1;
223   struct pollfd fd;
224   int milliseconds;
225   int i;
226   bool_t done = FALSE;
227   u_long xid;
228   u_long port;
229   struct in_addr addrs[20];
230   struct sockaddr_in baddr, raddr;      /* broadcast and response addresses */
231   struct rmtcallargs a;
232   struct rmtcallres r;
233   struct rpc_msg msg;
234   char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
235
236   /*
237    * initialization: create a socket, a broadcast address, and
238    * preserialize the arguments into a send buffer.
239    */
240   if ((sock = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
241     {
242       perror (_("Cannot create socket for broadcast rpc"));
243       stat = RPC_CANTSEND;
244       goto done_broad;
245     }
246 #ifdef SO_BROADCAST
247   if (__setsockopt (sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0)
248     {
249       perror (_("Cannot set socket option SO_BROADCAST"));
250       stat = RPC_CANTSEND;
251       goto done_broad;
252     }
253 #endif /* def SO_BROADCAST */
254   fd.fd = sock;
255   fd.events = POLLIN;
256   nets = getbroadcastnets (addrs, sizeof (addrs) / sizeof (addrs[0]));
257   __bzero ((char *) &baddr, sizeof (baddr));
258   baddr.sin_family = AF_INET;
259   baddr.sin_port = htons (PMAPPORT);
260   baddr.sin_addr.s_addr = htonl (INADDR_ANY);
261 /*      baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
262   msg.rm_xid = xid = _create_xid ();
263   t.tv_usec = 0;
264   msg.rm_direction = CALL;
265   msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
266   msg.rm_call.cb_prog = PMAPPROG;
267   msg.rm_call.cb_vers = PMAPVERS;
268   msg.rm_call.cb_proc = PMAPPROC_CALLIT;
269   msg.rm_call.cb_cred = unix_auth->ah_cred;
270   msg.rm_call.cb_verf = unix_auth->ah_verf;
271   a.prog = prog;
272   a.vers = vers;
273   a.proc = proc;
274   a.xdr_args = xargs;
275   a.args_ptr = argsp;
276   r.port_ptr = &port;
277   r.xdr_results = xresults;
278   r.results_ptr = resultsp;
279   INTUSE(xdrmem_create) (xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
280   if ((!INTUSE(xdr_callmsg) (xdrs, &msg))
281       || (!INTUSE(xdr_rmtcall_args) (xdrs, &a)))
282     {
283       stat = RPC_CANTENCODEARGS;
284       goto done_broad;
285     }
286   outlen = (int) xdr_getpos (xdrs);
287   xdr_destroy (xdrs);
288   /*
289    * Basic loop: broadcast a packet and wait a while for response(s).
290    * The response timeout grows larger per iteration.
291    */
292   for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2)
293     {
294       for (i = 0; i < nets; i++)
295         {
296           baddr.sin_addr = addrs[i];
297           if (__sendto (sock, outbuf, outlen, 0,
298                         (struct sockaddr *) &baddr,
299                         sizeof (struct sockaddr)) != outlen)
300             {
301               perror (_("Cannot send broadcast packet"));
302               stat = RPC_CANTSEND;
303               goto done_broad;
304             }
305         }
306       if (eachresult == NULL)
307         {
308           stat = RPC_SUCCESS;
309           goto done_broad;
310         }
311     recv_again:
312       msg.acpted_rply.ar_verf = _null_auth;
313       msg.acpted_rply.ar_results.where = (caddr_t) & r;
314       msg.acpted_rply.ar_results.proc = (xdrproc_t) INTUSE(xdr_rmtcallres);
315       milliseconds = t.tv_sec * 1000 + t.tv_usec / 1000;
316       switch (__poll(&fd, 1, milliseconds))
317         {
318
319         case 0:         /* timed out */
320           stat = RPC_TIMEDOUT;
321           continue;
322
323         case -1:                /* some kind of error */
324           if (errno == EINTR)
325             goto recv_again;
326           perror (_("Broadcast poll problem"));
327           stat = RPC_CANTRECV;
328           goto done_broad;
329
330         }                       /* end of poll results switch */
331     try_again:
332       fromlen = sizeof (struct sockaddr);
333       inlen = __recvfrom (sock, inbuf, UDPMSGSIZE, 0,
334                           (struct sockaddr *) &raddr, &fromlen);
335       if (inlen < 0)
336         {
337           if (errno == EINTR)
338             goto try_again;
339           perror (_("Cannot receive reply to broadcast"));
340           stat = RPC_CANTRECV;
341           goto done_broad;
342         }
343       if ((size_t) inlen < sizeof (u_long))
344         goto recv_again;
345       /*
346        * see if reply transaction id matches sent id.
347        * If so, decode the results.
348        */
349       INTUSE(xdrmem_create) (xdrs, inbuf, (u_int) inlen, XDR_DECODE);
350       if (INTUSE(xdr_replymsg) (xdrs, &msg))
351         {
352           if (((u_int32_t) msg.rm_xid == (u_int32_t) xid) &&
353               (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
354               (msg.acpted_rply.ar_stat == SUCCESS))
355             {
356               raddr.sin_port = htons ((u_short) port);
357               done = (*eachresult) (resultsp, &raddr);
358             }
359           /* otherwise, we just ignore the errors ... */
360         }
361       else
362         {
363 #ifdef notdef
364           /* some kind of deserialization problem ... */
365           if ((u_int32_t) msg.rm_xid == (u_int32_t) xid)
366             fprintf (stderr, "Broadcast deserialization problem");
367           /* otherwise, just random garbage */
368 #endif
369         }
370       xdrs->x_op = XDR_FREE;
371       msg.acpted_rply.ar_results.proc = (xdrproc_t)INTUSE(xdr_void);
372       (void) INTUSE(xdr_replymsg) (xdrs, &msg);
373       (void) (*xresults) (xdrs, resultsp);
374       xdr_destroy (xdrs);
375       if (done)
376         {
377           stat = RPC_SUCCESS;
378           goto done_broad;
379         }
380       else
381         {
382           goto recv_again;
383         }
384     }
385 done_broad:
386   (void) __close (sock);
387   AUTH_DESTROY (unix_auth);
388   return stat;
389 }