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