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