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