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