OSDN Git Service

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