OSDN Git Service

Hide *clnt|pmap|svc* and some rpc. inet/rpc is full of relocs ...
[uclinux-h8/uClibc.git] / libc / inet / rpc / clnt_udp.c
1 /* @(#)clnt_udp.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[] = "@(#)clnt_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro";
32 #endif
33
34 /*
35  * clnt_udp.c, Implements a UDP/IP based, client side RPC.
36  *
37  * Copyright (C) 1984, Sun Microsystems, Inc.
38  */
39
40 /* CMSG_NXTHDR is using it */
41 #define __cmsg_nxthdr __cmsg_nxthdr_internal
42
43 #define authnone_create __authnone_create
44 #define xdrmem_create __xdrmem_create
45
46 #define pmap_getport __pmap_getport
47
48 #define __FORCE_GLIBC
49 #include <features.h>
50
51 #include <stdio.h>
52 #include <unistd.h>
53 #include <rpc/rpc.h>
54 #include <rpc/xdr.h>
55 #include <rpc/clnt.h>
56 #include <sys/poll.h>
57 #include <sys/socket.h>
58 #include <sys/ioctl.h>
59 #include <netdb.h>
60 #include <errno.h>
61 #include <rpc/pmap_clnt.h>
62 #include <net/if.h>
63 #ifdef USE_IN_LIBIO
64 # include <wchar.h>
65 #endif
66
67 #ifdef IP_RECVERR
68 #include "errqueue.h"
69 #include <sys/uio.h>
70 #endif
71
72 extern bool_t xdr_opaque_auth (XDR *, struct opaque_auth *);
73 extern u_long _create_xid (void) attribute_hidden;
74
75 /*
76  * UDP bases client side rpc operations
77  */
78 static enum clnt_stat clntudp_call (CLIENT *, u_long, xdrproc_t, caddr_t,
79                                     xdrproc_t, caddr_t, struct timeval);
80 static void clntudp_abort (void);
81 static void clntudp_geterr (CLIENT *, struct rpc_err *);
82 static bool_t clntudp_freeres (CLIENT *, xdrproc_t, caddr_t);
83 static bool_t clntudp_control (CLIENT *, int, char *);
84 static void clntudp_destroy (CLIENT *);
85
86 static struct clnt_ops udp_ops =
87 {
88   clntudp_call,
89   clntudp_abort,
90   clntudp_geterr,
91   clntudp_freeres,
92   clntudp_destroy,
93   clntudp_control
94 };
95
96 /*
97  * Private data kept per client handle
98  */
99 struct cu_data
100   {
101     int cu_sock;
102     bool_t cu_closeit;
103     struct sockaddr_in cu_raddr;
104     int cu_rlen;
105     struct timeval cu_wait;
106     struct timeval cu_total;
107     struct rpc_err cu_error;
108     XDR cu_outxdrs;
109     u_int cu_xdrpos;
110     u_int cu_sendsz;
111     char *cu_outbuf;
112     u_int cu_recvsz;
113     char cu_inbuf[1];
114   };
115
116 /*
117  * Create a UDP based client handle.
118  * If *sockp<0, *sockp is set to a newly created UPD socket.
119  * If raddr->sin_port is 0 a binder on the remote machine
120  * is consulted for the correct port number.
121  * NB: It is the clients responsibility to close *sockp.
122  * NB: The rpch->cl_auth is initialized to null authentication.
123  *     Caller may wish to set this something more useful.
124  *
125  * wait is the amount of time used between retransmitting a call if
126  * no response has been heard; retransmission occurs until the actual
127  * rpc call times out.
128  *
129  * sendsz and recvsz are the maximum allowable packet sizes that can be
130  * sent and received.
131  */
132 CLIENT attribute_hidden *
133 __clntudp_bufcreate (struct sockaddr_in *raddr, u_long program, u_long version,
134                    struct timeval wait, int *sockp, u_int sendsz,
135                    u_int recvsz)
136 {
137   CLIENT *cl;
138   struct cu_data *cu = NULL;
139   struct rpc_msg call_msg;
140
141   cl = (CLIENT *) mem_alloc (sizeof (CLIENT));
142   sendsz = ((sendsz + 3) / 4) * 4;
143   recvsz = ((recvsz + 3) / 4) * 4;
144   cu = (struct cu_data *) mem_alloc (sizeof (*cu) + sendsz + recvsz);
145   if (cl == NULL || cu == NULL)
146     {
147       struct rpc_createerr *ce = &get_rpc_createerr ();
148 #ifdef USE_IN_LIBIO
149       if (_IO_fwide (stderr, 0) > 0)
150         (void) __fwprintf (stderr, L"%s",
151                            _("clntudp_create: out of memory\n"));
152       else
153 #endif
154         (void) fputs (_("clntudp_create: out of memory\n"), stderr);
155       ce->cf_stat = RPC_SYSTEMERROR;
156       ce->cf_error.re_errno = ENOMEM;
157       goto fooy;
158     }
159   cu->cu_outbuf = &cu->cu_inbuf[recvsz];
160
161   if (raddr->sin_port == 0)
162     {
163       u_short port;
164       if ((port =
165            pmap_getport (raddr, program, version, IPPROTO_UDP)) == 0)
166         {
167           goto fooy;
168         }
169       raddr->sin_port = htons (port);
170     }
171   cl->cl_ops = &udp_ops;
172   cl->cl_private = (caddr_t) cu;
173   cu->cu_raddr = *raddr;
174   cu->cu_rlen = sizeof (cu->cu_raddr);
175   cu->cu_wait = wait;
176   cu->cu_total.tv_sec = -1;
177   cu->cu_total.tv_usec = -1;
178   cu->cu_sendsz = sendsz;
179   cu->cu_recvsz = recvsz;
180   call_msg.rm_xid = _create_xid ();
181   call_msg.rm_direction = CALL;
182   call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
183   call_msg.rm_call.cb_prog = program;
184   call_msg.rm_call.cb_vers = version;
185   xdrmem_create (&(cu->cu_outxdrs), cu->cu_outbuf,
186                  sendsz, XDR_ENCODE);
187   if (!xdr_callhdr (&(cu->cu_outxdrs), &call_msg))
188     {
189       goto fooy;
190     }
191   cu->cu_xdrpos = XDR_GETPOS (&(cu->cu_outxdrs));
192   if (*sockp < 0)
193     {
194       int dontblock = 1;
195
196       *sockp = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
197       if (*sockp < 0)
198         {
199           struct rpc_createerr *ce = &get_rpc_createerr ();
200           ce->cf_stat = RPC_SYSTEMERROR;
201           ce->cf_error.re_errno = errno;
202           goto fooy;
203         }
204       /* attempt to bind to prov port */
205       (void) bindresvport (*sockp, (struct sockaddr_in *) 0);
206       /* the sockets rpc controls are non-blocking */
207       (void) ioctl (*sockp, FIONBIO, (char *) &dontblock);
208 #ifdef IP_RECVERR
209       {
210         int on = 1;
211         setsockopt(*sockp, SOL_IP, IP_RECVERR, &on, sizeof(on));
212       }
213 #endif
214       cu->cu_closeit = TRUE;
215     }
216   else
217     {
218       cu->cu_closeit = FALSE;
219     }
220   cu->cu_sock = *sockp;
221   cl->cl_auth = authnone_create ();
222   return cl;
223 fooy:
224   if (cu)
225     mem_free ((caddr_t) cu, sizeof (*cu) + sendsz + recvsz);
226   if (cl)
227     mem_free ((caddr_t) cl, sizeof (CLIENT));
228   return (CLIENT *) NULL;
229 }
230 strong_alias(__clntudp_bufcreate,clntudp_bufcreate)
231
232 CLIENT attribute_hidden *
233 __clntudp_create (struct sockaddr_in *raddr, u_long program, u_long version, struct timeval wait, int *sockp)
234 {
235
236   return __clntudp_bufcreate (raddr, program, version, wait, sockp,
237                             UDPMSGSIZE, UDPMSGSIZE);
238 }
239 strong_alias(__clntudp_create,clntudp_create)
240
241 static int
242 is_network_up (int sock)
243 {
244   struct ifconf ifc;
245   char buf[UDPMSGSIZE];
246   struct ifreq ifreq, *ifr;
247   int n;
248
249   ifc.ifc_len = sizeof (buf);
250   ifc.ifc_buf = buf;
251   if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) == 0)
252     {
253       ifr = ifc.ifc_req;
254       for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++)
255         {
256           ifreq = *ifr;
257           if (ioctl (sock, SIOCGIFFLAGS, (char *) &ifreq) < 0)
258             break;
259
260           if ((ifreq.ifr_flags & IFF_UP)
261               && ifr->ifr_addr.sa_family == AF_INET)
262             return 1;
263         }
264     }
265   return 0;
266 }
267
268 static enum clnt_stat
269 clntudp_call (cl, proc, xargs, argsp, xresults, resultsp, utimeout)
270      CLIENT *cl;        /* client handle */
271      u_long proc;               /* procedure number */
272      xdrproc_t xargs;           /* xdr routine for args */
273      caddr_t argsp;             /* pointer to args */
274      xdrproc_t xresults;        /* xdr routine for results */
275      caddr_t resultsp;          /* pointer to results */
276      struct timeval utimeout;   /* seconds to wait before giving up */
277 {
278   struct cu_data *cu = (struct cu_data *) cl->cl_private;
279   XDR *xdrs;
280   int outlen = 0;
281   int inlen;
282   socklen_t fromlen;
283   struct pollfd fd;
284   int milliseconds = (cu->cu_wait.tv_sec * 1000) +
285     (cu->cu_wait.tv_usec / 1000);
286   struct sockaddr_in from;
287   struct rpc_msg reply_msg;
288   XDR reply_xdrs;
289   struct timeval time_waited;
290   bool_t ok;
291   int nrefreshes = 2;           /* number of times to refresh cred */
292   struct timeval timeout;
293   int anyup;                    /* any network interface up */
294
295   if (cu->cu_total.tv_usec == -1)
296     {
297       timeout = utimeout;       /* use supplied timeout */
298     }
299   else
300     {
301       timeout = cu->cu_total;   /* use default timeout */
302     }
303
304   time_waited.tv_sec = 0;
305   time_waited.tv_usec = 0;
306 call_again:
307   xdrs = &(cu->cu_outxdrs);
308   if (xargs == NULL)
309     goto get_reply;
310   xdrs->x_op = XDR_ENCODE;
311   XDR_SETPOS (xdrs, cu->cu_xdrpos);
312   /*
313    * the transaction is the first thing in the out buffer
314    */
315   (*(uint32_t *) (cu->cu_outbuf))++;
316   if ((!XDR_PUTLONG (xdrs, (long *) &proc)) ||
317       (!AUTH_MARSHALL (cl->cl_auth, xdrs)) ||
318       (!(*xargs) (xdrs, argsp)))
319     return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
320   outlen = (int) XDR_GETPOS (xdrs);
321
322 send_again:
323   if (sendto (cu->cu_sock, cu->cu_outbuf, outlen, 0,
324               (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen)
325       != outlen)
326     {
327       cu->cu_error.re_errno = errno;
328       return (cu->cu_error.re_status = RPC_CANTSEND);
329     }
330
331   /*
332    * Hack to provide rpc-based message passing
333    */
334   if (timeout.tv_sec == 0 && timeout.tv_usec == 0)
335     {
336       return (cu->cu_error.re_status = RPC_TIMEDOUT);
337     }
338  get_reply:
339   /*
340    * sub-optimal code appears here because we have
341    * some clock time to spare while the packets are in flight.
342    * (We assume that this is actually only executed once.)
343    */
344   reply_msg.acpted_rply.ar_verf = _null_auth;
345   reply_msg.acpted_rply.ar_results.where = resultsp;
346   reply_msg.acpted_rply.ar_results.proc = xresults;
347   fd.fd = cu->cu_sock;
348   fd.events = POLLIN;
349   anyup = 0;
350   for (;;)
351     {
352       switch (poll (&fd, 1, milliseconds))
353         {
354
355         case 0:
356           if (anyup == 0)
357             {
358               anyup = is_network_up (cu->cu_sock);
359               if (!anyup)
360                 return (cu->cu_error.re_status = RPC_CANTRECV);
361             }
362
363           time_waited.tv_sec += cu->cu_wait.tv_sec;
364           time_waited.tv_usec += cu->cu_wait.tv_usec;
365           while (time_waited.tv_usec >= 1000000)
366             {
367               time_waited.tv_sec++;
368               time_waited.tv_usec -= 1000000;
369             }
370           if ((time_waited.tv_sec < timeout.tv_sec) ||
371               ((time_waited.tv_sec == timeout.tv_sec) &&
372                (time_waited.tv_usec < timeout.tv_usec)))
373             goto send_again;
374           return (cu->cu_error.re_status = RPC_TIMEDOUT);
375
376           /*
377            * buggy in other cases because time_waited is not being
378            * updated.
379            */
380         case -1:
381           if (errno == EINTR)
382             continue;
383           cu->cu_error.re_errno = errno;
384           return (cu->cu_error.re_status = RPC_CANTRECV);
385         }
386 #ifdef IP_RECVERR
387       if (fd.revents & POLLERR)
388         {
389           struct msghdr msg;
390           struct cmsghdr *cmsg;
391           struct sock_extended_err *e;
392           struct sockaddr_in err_addr;
393           struct iovec iov;
394           char *cbuf = (char *) alloca (outlen + 256);
395           int ret;
396
397           iov.iov_base = cbuf + 256;
398           iov.iov_len = outlen;
399           msg.msg_name = (void *) &err_addr;
400           msg.msg_namelen = sizeof (err_addr);
401           msg.msg_iov = &iov;
402           msg.msg_iovlen = 1;
403           msg.msg_flags = 0;
404           msg.msg_control = cbuf;
405           msg.msg_controllen = 256;
406           ret = recvmsg (cu->cu_sock, &msg, MSG_ERRQUEUE);
407           if (ret >= 0
408               && __memcmp (cbuf + 256, cu->cu_outbuf, ret) == 0
409               && (msg.msg_flags & MSG_ERRQUEUE)
410               && ((msg.msg_namelen == 0
411                    && ret >= 12)
412                   || (msg.msg_namelen == sizeof (err_addr)
413                       && err_addr.sin_family == AF_INET
414                       && __memcmp (&err_addr.sin_addr, &cu->cu_raddr.sin_addr,
415                                  sizeof (err_addr.sin_addr)) == 0
416                       && err_addr.sin_port == cu->cu_raddr.sin_port)))
417             for (cmsg = CMSG_FIRSTHDR (&msg); cmsg;
418                  cmsg = CMSG_NXTHDR (&msg, cmsg))
419               if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR)
420                 {
421                   e = (struct sock_extended_err *) CMSG_DATA(cmsg);
422                   cu->cu_error.re_errno = e->ee_errno;
423                   return (cu->cu_error.re_status = RPC_CANTRECV);
424                 }
425         }
426 #endif
427       do
428         {
429           fromlen = sizeof (struct sockaddr);
430           inlen = recvfrom (cu->cu_sock, cu->cu_inbuf,
431                             (int) cu->cu_recvsz, 0,
432                             (struct sockaddr *) &from, &fromlen);
433         }
434       while (inlen < 0 && errno == EINTR);
435       if (inlen < 0)
436         {
437           if (errno == EWOULDBLOCK)
438             continue;
439           cu->cu_error.re_errno = errno;
440           return (cu->cu_error.re_status = RPC_CANTRECV);
441         }
442       if (inlen < 4)
443         continue;
444
445       /* see if reply transaction id matches sent id.
446         Don't do this if we only wait for a replay */
447       if (xargs != NULL
448           && (*((u_int32_t *) (cu->cu_inbuf))
449               != *((u_int32_t *) (cu->cu_outbuf))))
450         continue;
451       /* we now assume we have the proper reply */
452       break;
453     }
454
455   /*
456    * now decode and validate the response
457    */
458   xdrmem_create (&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE);
459   ok = xdr_replymsg (&reply_xdrs, &reply_msg);
460   /* XDR_DESTROY(&reply_xdrs);  save a few cycles on noop destroy */
461   if (ok)
462     {
463       _seterr_reply (&reply_msg, &(cu->cu_error));
464       if (cu->cu_error.re_status == RPC_SUCCESS)
465         {
466           if (!AUTH_VALIDATE (cl->cl_auth,
467                               &reply_msg.acpted_rply.ar_verf))
468             {
469               cu->cu_error.re_status = RPC_AUTHERROR;
470               cu->cu_error.re_why = AUTH_INVALIDRESP;
471             }
472           if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
473             {
474               xdrs->x_op = XDR_FREE;
475               (void) xdr_opaque_auth (xdrs,
476                                       &(reply_msg.acpted_rply.ar_verf));
477             }
478         }                       /* end successful completion */
479       else
480         {
481           /* maybe our credentials need to be refreshed ... */
482           if (nrefreshes > 0 && AUTH_REFRESH (cl->cl_auth))
483             {
484               nrefreshes--;
485               goto call_again;
486             }
487         }                       /* end of unsuccessful completion */
488     }                           /* end of valid reply message */
489   else
490     {
491       cu->cu_error.re_status = RPC_CANTDECODERES;
492     }
493   return cu->cu_error.re_status;
494 }
495
496 static void
497 clntudp_geterr (CLIENT *cl, struct rpc_err *errp)
498 {
499   struct cu_data *cu = (struct cu_data *) cl->cl_private;
500
501   *errp = cu->cu_error;
502 }
503
504
505 static bool_t
506 clntudp_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
507 {
508   struct cu_data *cu = (struct cu_data *) cl->cl_private;
509   XDR *xdrs = &(cu->cu_outxdrs);
510
511   xdrs->x_op = XDR_FREE;
512   return (*xdr_res) (xdrs, res_ptr);
513 }
514
515 static void
516 clntudp_abort (void)
517 {
518 }
519
520 static bool_t
521 clntudp_control (CLIENT *cl, int request, char *info)
522 {
523   struct cu_data *cu = (struct cu_data *) cl->cl_private;
524
525   switch (request)
526     {
527     case CLSET_FD_CLOSE:
528       cu->cu_closeit = TRUE;
529       break;
530     case CLSET_FD_NCLOSE:
531       cu->cu_closeit = FALSE;
532       break;
533     case CLSET_TIMEOUT:
534       cu->cu_total = *(struct timeval *) info;
535       break;
536     case CLGET_TIMEOUT:
537       *(struct timeval *) info = cu->cu_total;
538       break;
539     case CLSET_RETRY_TIMEOUT:
540       cu->cu_wait = *(struct timeval *) info;
541       break;
542     case CLGET_RETRY_TIMEOUT:
543       *(struct timeval *) info = cu->cu_wait;
544       break;
545     case CLGET_SERVER_ADDR:
546       *(struct sockaddr_in *) info = cu->cu_raddr;
547       break;
548     case CLGET_FD:
549       *(int *)info = cu->cu_sock;
550       break;
551     case CLGET_XID:
552       /*
553        * use the knowledge that xid is the
554        * first element in the call structure *.
555        * This will get the xid of the PREVIOUS call
556        */
557       *(u_long *)info = ntohl(*(u_long *)cu->cu_outbuf);
558       break;
559     case CLSET_XID:
560       /* This will set the xid of the NEXT call */
561       *(u_long *)cu->cu_outbuf =  htonl(*(u_long *)info - 1);
562       /* decrement by 1 as clntudp_call() increments once */
563     case CLGET_VERS:
564       /*
565        * This RELIES on the information that, in the call body,
566        * the version number field is the fifth field from the
567        * begining of the RPC header. MUST be changed if the
568        * call_struct is changed
569        */
570       *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
571                                           4 * BYTES_PER_XDR_UNIT));
572       break;
573     case CLSET_VERS:
574       *(u_long *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
575         = htonl(*(u_long *)info);
576       break;
577     case CLGET_PROG:
578       /*
579        * This RELIES on the information that, in the call body,
580        * the program number field is the  field from the
581        * begining of the RPC header. MUST be changed if the
582        * call_struct is changed
583        */
584       *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
585                                           3 * BYTES_PER_XDR_UNIT));
586       break;
587     case CLSET_PROG:
588       *(u_long *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
589         = htonl(*(u_long *)info);
590       break;
591     /* The following are only possible with TI-RPC */
592     case CLGET_SVC_ADDR:
593     case CLSET_SVC_ADDR:
594     case CLSET_PUSH_TIMOD:
595     case CLSET_POP_TIMOD:
596     default:
597       return FALSE;
598     }
599   return TRUE;
600 }
601
602 static void
603 clntudp_destroy (CLIENT *cl)
604 {
605   struct cu_data *cu = (struct cu_data *) cl->cl_private;
606
607   if (cu->cu_closeit)
608     {
609       (void) __close (cu->cu_sock);
610     }
611   XDR_DESTROY (&(cu->cu_outxdrs));
612   mem_free ((caddr_t) cu, (sizeof (*cu) + cu->cu_sendsz + cu->cu_recvsz));
613   mem_free ((caddr_t) cl, sizeof (CLIENT));
614 }