libcr copy: Retarget build paths from ../libc to ../libcr and retarget
[dragonfly.git] / lib / libcr / rpc / clnt_udp.c
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  *
29  * @(#)clnt_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro
30  * @(#)clnt_udp.c       2.2 88/08/01 4.0 RPCSRC
31  * $FreeBSD: src/lib/libc/rpc/clnt_udp.c,v 1.15.2.1 2001/06/28 21:44:24 iedowse Exp $
32  * $DragonFly: src/lib/libcr/rpc/Attic/clnt_udp.c,v 1.2 2003/06/17 04:26:44 dillon Exp $
33  */
34
35 /*
36  * clnt_udp.c, Implements a UDP/IP based, client side RPC.
37  *
38  * Copyright (C) 1984, Sun Microsystems, Inc.
39  */
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <rpc/rpc.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <netdb.h>
49 #include <errno.h>
50 #include <rpc/pmap_clnt.h>
51
52 /*
53  * UDP bases client side rpc operations
54  */
55 static enum clnt_stat   clntudp_call();
56 static void             clntudp_abort();
57 static void             clntudp_geterr();
58 static bool_t           clntudp_freeres();
59 static bool_t           clntudp_control();
60 static void             clntudp_destroy();
61
62 static struct clnt_ops udp_ops = {
63         clntudp_call,
64         clntudp_abort,
65         clntudp_geterr,
66         clntudp_freeres,
67         clntudp_destroy,
68         clntudp_control
69 };
70
71 /*
72  * Private data kept per client handle
73  */
74 struct cu_data {
75         int                cu_sock;
76         bool_t             cu_closeit;
77         struct sockaddr_in cu_raddr;
78         int                cu_rlen;
79         struct timeval     cu_wait;
80         struct timeval     cu_total;
81         struct rpc_err     cu_error;
82         XDR                cu_outxdrs;
83         u_int              cu_xdrpos;
84         u_int              cu_sendsz;
85         char               *cu_outbuf;
86         u_int              cu_recvsz;
87         int                cu_connect;          /* Use connect(). */
88         int                cu_connected;        /* Have done connect(). */
89         char               cu_inbuf[1];
90 };
91
92 /*
93  * Create a UDP based client handle.
94  * If *sockp<0, *sockp is set to a newly created UPD socket.
95  * If raddr->sin_port is 0 a binder on the remote machine
96  * is consulted for the correct port number.
97  * NB: It is the clients responsibility to close *sockp.
98  * NB: The rpch->cl_auth is initialized to null authentication.
99  *     Caller may wish to set this something more useful.
100  *
101  * wait is the amount of time used between retransmitting a call if
102  * no response has been heard;  retransmition occurs until the actual
103  * rpc call times out.
104  *
105  * sendsz and recvsz are the maximum allowable packet sizes that can be
106  * sent and received.
107  */
108 CLIENT *
109 clntudp_bufcreate(raddr, program, version, wait, sockp, sendsz, recvsz)
110         struct sockaddr_in *raddr;
111         u_long program;
112         u_long version;
113         struct timeval wait;
114         register int *sockp;
115         u_int sendsz;
116         u_int recvsz;
117 {
118         CLIENT *cl;
119         register struct cu_data *cu = NULL;
120         struct timeval now;
121         struct rpc_msg call_msg;
122         static u_int32_t disrupt;
123
124         if (disrupt == 0)
125                 disrupt = (u_int32_t)(long)raddr;
126
127         cl = (CLIENT *)mem_alloc(sizeof(CLIENT));
128         if (cl == NULL) {
129                 (void) fprintf(stderr, "clntudp_create: out of memory\n");
130                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
131                 rpc_createerr.cf_error.re_errno = errno;
132                 goto fooy;
133         }
134         sendsz = ((sendsz + 3) / 4) * 4;
135         recvsz = ((recvsz + 3) / 4) * 4;
136         cu = (struct cu_data *)mem_alloc(sizeof(*cu) + sendsz + recvsz);
137         if (cu == NULL) {
138                 (void) fprintf(stderr, "clntudp_create: out of memory\n");
139                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
140                 rpc_createerr.cf_error.re_errno = errno;
141                 goto fooy;
142         }
143         cu->cu_outbuf = &cu->cu_inbuf[recvsz];
144
145         (void)gettimeofday(&now, (struct timezone *)0);
146         if (raddr->sin_port == 0) {
147                 u_short port;
148                 if ((port =
149                     pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
150                         goto fooy;
151                 }
152                 raddr->sin_port = htons(port);
153         }
154         cl->cl_ops = &udp_ops;
155         cl->cl_private = (caddr_t)cu;
156         cu->cu_raddr = *raddr;
157         cu->cu_rlen = sizeof (cu->cu_raddr);
158         cu->cu_wait = wait;
159         cu->cu_total.tv_sec = -1;
160         cu->cu_total.tv_usec = -1;
161         cu->cu_sendsz = sendsz;
162         cu->cu_recvsz = recvsz;
163         cu->cu_connect = FALSE;
164         cu->cu_connected = FALSE;
165         call_msg.rm_xid = (++disrupt) ^ getpid() ^ now.tv_sec ^ now.tv_usec;
166         call_msg.rm_direction = CALL;
167         call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
168         call_msg.rm_call.cb_prog = program;
169         call_msg.rm_call.cb_vers = version;
170         xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf,
171             sendsz, XDR_ENCODE);
172         if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
173                 goto fooy;
174         }
175         cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
176         if (*sockp < 0) {
177                 int dontblock = 1;
178
179                 *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
180                 if (*sockp < 0) {
181                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
182                         rpc_createerr.cf_error.re_errno = errno;
183                         goto fooy;
184                 }
185                 /* attempt to bind to priv port */
186                 (void)bindresvport(*sockp, (struct sockaddr_in *)0);
187                 /* the sockets rpc controls are non-blocking */
188                 (void)ioctl(*sockp, FIONBIO, (char *) &dontblock);
189                 cu->cu_closeit = TRUE;
190         } else {
191                 cu->cu_closeit = FALSE;
192         }
193         cu->cu_sock = *sockp;
194         cl->cl_auth = authnone_create();
195         return (cl);
196 fooy:
197         if (cu)
198                 mem_free((caddr_t)cu, sizeof(*cu) + sendsz + recvsz);
199         if (cl)
200                 mem_free((caddr_t)cl, sizeof(CLIENT));
201         return ((CLIENT *)NULL);
202 }
203
204 CLIENT *
205 clntudp_create(raddr, program, version, wait, sockp)
206         struct sockaddr_in *raddr;
207         u_long program;
208         u_long version;
209         struct timeval wait;
210         register int *sockp;
211 {
212
213         return(clntudp_bufcreate(raddr, program, version, wait, sockp,
214             UDPMSGSIZE, UDPMSGSIZE));
215 }
216
217 static enum clnt_stat
218 clntudp_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout)
219         register CLIENT *cl;            /* client handle */
220         u_long          proc;           /* procedure number */
221         xdrproc_t       xargs;          /* xdr routine for args */
222         caddr_t         argsp;          /* pointer to args */
223         xdrproc_t       xresults;       /* xdr routine for results */
224         caddr_t         resultsp;       /* pointer to results */
225         struct timeval  utimeout;       /* seconds to wait before giving up */
226 {
227         register struct cu_data *cu = (struct cu_data *)cl->cl_private;
228         register XDR *xdrs;
229         register int outlen;
230         register int inlen;
231         struct sockaddr *sa;
232         int fromlen;
233         fd_set *fds, readfds;
234         struct sockaddr_in from;
235         struct rpc_msg reply_msg;
236         XDR reply_xdrs;
237         struct timeval time_waited, start, after, tmp1, tmp2, tv;
238         bool_t ok;
239         int nrefreshes = 2;     /* number of times to refresh cred */
240         struct timeval timeout;
241         socklen_t salen;
242
243         if (cu->cu_total.tv_usec == -1)
244                 timeout = utimeout;     /* use supplied timeout */
245         else
246                 timeout = cu->cu_total; /* use default timeout */
247
248         if (cu->cu_sock + 1 > FD_SETSIZE) {
249                 int bytes = howmany(cu->cu_sock + 1, NFDBITS) * sizeof(fd_mask);
250                 fds = (fd_set *)malloc(bytes);
251                 if (fds == NULL)
252                         return (cu->cu_error.re_status = RPC_CANTSEND);
253                 memset(fds, 0, bytes);
254         } else {
255                 fds = &readfds;
256                 FD_ZERO(fds);
257         }
258
259         if (cu->cu_connect && !cu->cu_connected) {
260                 if (connect(cu->cu_sock, (struct sockaddr *)&cu->cu_raddr,
261                     cu->cu_rlen) < 0) {
262                         cu->cu_error.re_errno = errno;
263                         if (fds != &readfds)
264                                 free(fds);
265                         return (cu->cu_error.re_status = RPC_CANTSEND);
266                 }
267                 cu->cu_connected = 1;
268         }
269         if (cu->cu_connected) {
270                 sa = NULL;
271                 salen = 0;
272         } else {
273                 sa = (struct sockaddr *)&cu->cu_raddr;
274                 salen = cu->cu_rlen;
275         }
276         timerclear(&time_waited);
277
278 call_again:
279         xdrs = &(cu->cu_outxdrs);
280         xdrs->x_op = XDR_ENCODE;
281         XDR_SETPOS(xdrs, cu->cu_xdrpos);
282         /*
283          * the transaction is the first thing in the out buffer
284          */
285         (*(u_short *)(cu->cu_outbuf))++;
286         if ((! XDR_PUTLONG(xdrs, (long *)&proc)) ||
287             (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
288             (! (*xargs)(xdrs, argsp))) {
289                 if (fds != &readfds)
290                         free(fds);
291                 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
292         }
293         outlen = (int)XDR_GETPOS(xdrs);
294
295 send_again:
296         if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0, sa, salen)
297             != outlen) {
298                 cu->cu_error.re_errno = errno;
299                 if (fds != &readfds)
300                         free(fds);
301                 return (cu->cu_error.re_status = RPC_CANTSEND);
302         }
303
304         /*
305          * Hack to provide rpc-based message passing
306          */
307         if (!timerisset(&timeout)) {
308                 if (fds != &readfds)
309                         free(fds);
310                 return (cu->cu_error.re_status = RPC_TIMEDOUT);
311         }
312         /*
313          * sub-optimal code appears here because we have
314          * some clock time to spare while the packets are in flight.
315          * (We assume that this is actually only executed once.)
316          */
317         reply_msg.acpted_rply.ar_verf = _null_auth;
318         reply_msg.acpted_rply.ar_results.where = resultsp;
319         reply_msg.acpted_rply.ar_results.proc = xresults;
320
321         gettimeofday(&start, NULL);
322         for (;;) {
323                 /* XXX we know the other bits are still clear */
324                 FD_SET(cu->cu_sock, fds);
325                 tv = cu->cu_wait;
326                 switch (select(cu->cu_sock+1, fds, NULL, NULL, &tv)) {
327
328                 case 0:
329                         timeradd(&time_waited, &cu->cu_wait, &tmp1);
330                         time_waited = tmp1;
331                         if (timercmp(&time_waited, &timeout, <))
332                                 goto send_again;
333                         if (fds != &readfds)
334                                 free(fds);
335                         return (cu->cu_error.re_status = RPC_TIMEDOUT);
336
337                 case -1:
338                         if (errno == EINTR) {
339                                 gettimeofday(&after, NULL);
340                                 timersub(&after, &start, &tmp1);
341                                 timeradd(&time_waited, &tmp1, &tmp2);
342                                 time_waited = tmp2;
343                                 if (timercmp(&time_waited, &timeout, <))
344                                         continue;
345                                 if (fds != &readfds)
346                                         free(fds);
347                                 return (cu->cu_error.re_status = RPC_TIMEDOUT);
348                         }
349                         cu->cu_error.re_errno = errno;
350                         if (fds != &readfds)
351                                 free(fds);
352                         return (cu->cu_error.re_status = RPC_CANTRECV);
353                 }
354
355                 do {
356                         fromlen = sizeof(struct sockaddr);
357                         inlen = recvfrom(cu->cu_sock, cu->cu_inbuf,
358                                 (int) cu->cu_recvsz, 0,
359                                 (struct sockaddr *)&from, &fromlen);
360                 } while (inlen < 0 && errno == EINTR);
361                 if (inlen < 0) {
362                         if (errno == EWOULDBLOCK)
363                                 continue;
364                         cu->cu_error.re_errno = errno;
365                         if (fds != &readfds)
366                                 free(fds);
367                         return (cu->cu_error.re_status = RPC_CANTRECV);
368                 }
369                 if (inlen < sizeof(u_int32_t))
370                         continue;
371                 /* see if reply transaction id matches sent id */
372                 if (*((u_int32_t *)(cu->cu_inbuf)) != *((u_int32_t *)(cu->cu_outbuf)))
373                         continue;
374                 /* we now assume we have the proper reply */
375                 break;
376         }
377
378         /*
379          * now decode and validate the response
380          */
381         xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)inlen, XDR_DECODE);
382         ok = xdr_replymsg(&reply_xdrs, &reply_msg);
383         /* XDR_DESTROY(&reply_xdrs);  save a few cycles on noop destroy */
384         if (ok) {
385                 _seterr_reply(&reply_msg, &(cu->cu_error));
386                 if (cu->cu_error.re_status == RPC_SUCCESS) {
387                         if (! AUTH_VALIDATE(cl->cl_auth,
388                                 &reply_msg.acpted_rply.ar_verf)) {
389                                 cu->cu_error.re_status = RPC_AUTHERROR;
390                                 cu->cu_error.re_why = AUTH_INVALIDRESP;
391                         }
392                         if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
393                                 xdrs->x_op = XDR_FREE;
394                                 (void)xdr_opaque_auth(xdrs,
395                                     &(reply_msg.acpted_rply.ar_verf));
396                         }
397                 }  /* end successful completion */
398                 else {
399                         /* maybe our credentials need to be refreshed ... */
400                         if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth)) {
401                                 nrefreshes--;
402                                 goto call_again;
403                         }
404                 }  /* end of unsuccessful completion */
405         }  /* end of valid reply message */
406         else {
407                 /*
408                  * It's possible for xdr_replymsg() to fail partway
409                  * through its attempt to decode the result from the
410                  * server. If this happens, it will leave the reply
411                  * structure partially populated with dynamically
412                  * allocated memory. (This can happen if someone uses
413                  * clntudp_bufcreate() to create a CLIENT handle and
414                  * specifies a receive buffer size that is too small.)
415                  * This memory must be free()ed to avoid a leak.
416                  */
417                 int op = reply_xdrs.x_op;
418                 reply_xdrs.x_op = XDR_FREE;
419                 xdr_replymsg(&reply_xdrs, &reply_msg);
420                 reply_xdrs.x_op = op;
421                 cu->cu_error.re_status = RPC_CANTDECODERES;
422         }
423         if (fds != &readfds)
424                 free(fds);
425         return (cu->cu_error.re_status);
426 }
427
428 static void
429 clntudp_geterr(cl, errp)
430         CLIENT *cl;
431         struct rpc_err *errp;
432 {
433         register struct cu_data *cu = (struct cu_data *)cl->cl_private;
434
435         *errp = cu->cu_error;
436 }
437
438
439 static bool_t
440 clntudp_freeres(cl, xdr_res, res_ptr)
441         CLIENT *cl;
442         xdrproc_t xdr_res;
443         caddr_t res_ptr;
444 {
445         register struct cu_data *cu = (struct cu_data *)cl->cl_private;
446         register XDR *xdrs = &(cu->cu_outxdrs);
447
448         xdrs->x_op = XDR_FREE;
449         return ((*xdr_res)(xdrs, res_ptr));
450 }
451
452 static void
453 clntudp_abort(/*h*/)
454         /*CLIENT *h;*/
455 {
456 }
457
458
459 static bool_t
460 clntudp_control(cl, request, info)
461         CLIENT *cl;
462         int request;
463         char *info;
464 {
465         register struct cu_data *cu = (struct cu_data *)cl->cl_private;
466         register struct timeval *tv;
467         int len;
468
469         switch (request) {
470         case CLSET_FD_CLOSE:
471                 cu->cu_closeit = TRUE;
472                 break;
473         case CLSET_FD_NCLOSE:
474                 cu->cu_closeit = FALSE;
475                 break;
476         case CLSET_TIMEOUT:
477                 if (info == NULL)
478                         return(FALSE);
479                 tv = (struct timeval *)info;
480                 cu->cu_total.tv_sec = tv->tv_sec;
481                 cu->cu_total.tv_usec = tv->tv_usec;
482                 break;
483         case CLGET_TIMEOUT:
484                 if (info == NULL)
485                         return(FALSE);
486                 *(struct timeval *)info = cu->cu_total;
487                 break;
488         case CLSET_RETRY_TIMEOUT:
489                 if (info == NULL)
490                         return(FALSE);
491                 tv = (struct timeval *)info;
492                 cu->cu_wait.tv_sec = tv->tv_sec;
493                 cu->cu_wait.tv_usec = tv->tv_usec;
494                 break;
495         case CLGET_RETRY_TIMEOUT:
496                 if (info == NULL)
497                         return(FALSE);
498                 *(struct timeval *)info = cu->cu_wait;
499                 break;
500         case CLGET_SERVER_ADDR:
501                 if (info == NULL)
502                         return(FALSE);
503                 *(struct sockaddr_in *)info = cu->cu_raddr;
504                 break;
505         case CLGET_FD:
506                 if (info == NULL)
507                         return(FALSE);
508                 *(int *)info = cu->cu_sock;
509                 break;
510         case CLGET_XID:
511                 /*
512                  * use the knowledge that xid is the
513                  * first element in the call structure *.
514                  * This will get the xid of the PREVIOUS call
515                  */
516                 if (info == NULL)
517                         return(FALSE);
518                 *(u_long *)info = ntohl(*(u_long *)cu->cu_outbuf);
519                 break;
520         case CLSET_XID:
521                 /* This will set the xid of the NEXT call */
522                 if (info == NULL)
523                         return(FALSE);
524                 *(u_long *)cu->cu_outbuf =  htonl(*(u_long *)info - 1);
525                 /* decrement by 1 as clntudp_call() increments once */
526         case CLGET_VERS:
527                 /*
528                  * This RELIES on the information that, in the call body,
529                  * the version number field is the fifth field from the
530                  * begining of the RPC header. MUST be changed if the
531                  * call_struct is changed
532                  */
533                 if (info == NULL)
534                         return(FALSE);
535                 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
536                                                 4 * BYTES_PER_XDR_UNIT));
537                 break;
538         case CLSET_VERS:
539                 if (info == NULL)
540                         return(FALSE);
541                 *(u_long *)(cu->cu_outbuf + 4 * BYTES_PER_XDR_UNIT)
542                                 = htonl(*(u_long *)info);
543                 break;
544         case CLGET_PROG:
545                 /*
546                  * This RELIES on the information that, in the call body,
547                  * the program number field is the  field from the
548                  * begining of the RPC header. MUST be changed if the
549                  * call_struct is changed
550                  */
551                 if (info == NULL)
552                         return(FALSE);
553                 *(u_long *)info = ntohl(*(u_long *)(cu->cu_outbuf +
554                                                 3 * BYTES_PER_XDR_UNIT));
555                 break;
556         case CLSET_PROG:
557                 if (info == NULL)
558                         return(FALSE);
559                 *(u_long *)(cu->cu_outbuf + 3 * BYTES_PER_XDR_UNIT)
560                                 = htonl(*(u_long *)info);
561                 break;
562         case CLGET_LOCAL_ADDR:
563                 len = sizeof(struct sockaddr);
564                 if (getsockname(cu->cu_sock, (struct sockaddr *)info, &len) <0)
565                         return(FALSE);
566                 break;
567         case CLSET_CONNECT:
568                 cu->cu_connect = *(int *)(void *)info;
569                 break;
570         case CLGET_SVC_ADDR:
571         case CLSET_SVC_ADDR:
572         case CLSET_PUSH_TIMOD:
573         case CLSET_POP_TIMOD:
574         default:
575                 return (FALSE);
576         }
577         return (TRUE);
578 }
579
580 static void
581 clntudp_destroy(cl)
582         CLIENT *cl;
583 {
584         register struct cu_data *cu = (struct cu_data *)cl->cl_private;
585
586         if (cu->cu_closeit) {
587                 (void)_close(cu->cu_sock);
588         }
589         XDR_DESTROY(&(cu->cu_outxdrs));
590         mem_free((caddr_t)cu, (sizeof(*cu) + cu->cu_sendsz + cu->cu_recvsz));
591         mem_free((caddr_t)cl, sizeof(CLIENT));
592 }