Add proper per-file copyright notices/licenses and top-level license.
[bluesky.git] / TBBT / trace_play / rpc / clnt_udp.c
1 #ifndef lint
2 static char sfs_clnt_id[] = "@(#)clnt_udp.c     2.1     97/10/23";
3 #endif
4 /* @(#)clnt_udp.c       2.2 88/08/01 4.0 RPCSRC */
5 /*
6  *   Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
7  *      All rights reserved.
8  *              Standard Performance Evaluation Corporation (SPEC)
9  *              6585 Merchant Place, Suite 100
10  *              Warrenton, VA 20187
11  *
12  *      This product contains benchmarks acquired from several sources who
13  *      understand and agree with SPEC's goal of creating fair and objective
14  *      benchmarks to measure computer performance.
15  *
16  *      This copyright notice is placed here only to protect SPEC in the
17  *      event the source is misused in any manner that is contrary to the
18  *      spirit, the goals and the intent of SPEC.
19  *
20  *      The source code is provided to the user or company under the license
21  *      agreement for the SPEC Benchmark Suite for this product.
22  */
23 /*
24  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
25  * unrestricted use provided that this legend is included on all tape
26  * media and as a part of the software program in whole or part.  Users
27  * may copy or modify Sun RPC without charge, but are not authorized
28  * to license or distribute it to anyone else except as part of a product or
29  * program developed by the user.
30  * 
31  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
32  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
33  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
34  * 
35  * Sun RPC is provided with no support and without any obligation on the
36  * part of Sun Microsystems, Inc. to assist in its use, correction,
37  * modification or enhancement.
38  * 
39  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
40  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
41  * OR ANY PART THEREOF.
42  * 
43  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
44  * or profits or other special, indirect and consequential damages, even if
45  * Sun has been advised of the possibility of such damages.
46  * 
47  * Sun Microsystems, Inc.
48  * 2550 Garcia Avenue
49  * Mountain View, California  94043
50  */
51 #if !defined(lint) && defined(SCCSIDS)
52 static char sccsid[] = "@(#)clnt_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro";
53 #endif
54
55 /*
56  * clnt_udp.c, Implements a UDP/IP based, client side RPC.
57  *
58  * Copyright (C) 1984, Sun Microsystems, Inc.
59  */
60
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <unistd.h>
64 #ifndef FreeBSD
65 #include <stropts.h>
66 #endif /* ndef Free BSD */
67 #include <string.h>
68 #include "rpc/rpc.h"
69 #include "rpc/osdep.h"
70 #include <netdb.h>
71 #include <errno.h>
72 #include "rpc/pmap_clnt.h"
73
74 /* 
75  * Private data kept per client handle
76  */
77 struct cu_data {
78         int                cu_sock;
79         bool_t             cu_closeit;
80         struct sockaddr_in cu_raddr;
81         int                cu_rlen;
82         struct timeval     cu_wait;
83         struct timeval     cu_total;
84         struct rpc_err     cu_error;
85         XDR                cu_outxdrs;
86         uint_t             cu_xdrpos;
87         uint_t             cu_sendsz;
88         char               *cu_outbuf;
89         uint_t             cu_recvsz;
90         char               cu_inbuf[1];
91 };
92
93 /*
94  * UDP bases client side rpc operations
95  */
96 static enum clnt_stat   clntudp_call(CLIENT *, uint32_t, xdrproc_t, void *,
97                                         xdrproc_t, void *, struct timeval);
98 static void             clntudp_abort(CLIENT *);
99 static void             clntudp_geterr(CLIENT *, struct rpc_err *);
100 static bool_t           clntudp_freeres(CLIENT *, xdrproc_t, void *);
101 static bool_t           clntudp_control(CLIENT *, uint_t, void *);
102 static void             clntudp_destroy(CLIENT *);
103 static bool_t           clntudp_getreply(CLIENT *, xdrproc_t, void *,
104                                 int, uint32_t *, uint32_t *, struct timeval *);
105 static int              clntudp_poll(CLIENT *, uint32_t);
106
107 static struct clnt_ops udp_ops = {
108         clntudp_call,
109         clntudp_abort,
110         clntudp_geterr,
111         clntudp_freeres,
112         clntudp_destroy,
113         clntudp_control,
114         clntudp_getreply,
115         clntudp_poll
116 };
117
118 /*
119  * Create a UDP based client handle.
120  * If *sockp<0, *sockp is set to a newly created UPD socket.
121  * If raddr->sin_port is 0 a binder on the remote machine
122  * is consulted for the correct port number.
123  * NB: It is the clients responsibility to close *sockp.
124  * NB: The rpch->cl_auth is initialized to null authentication.
125  *     Caller may wish to set this something more useful.
126  *
127  * wait is the amount of time used between retransmitting a call if
128  * no response has been heard;  retransmition occurs until the actual
129  * rpc call times out.
130  *
131  * sendsz and recvsz are the maximum allowable packet sizes that can be
132  * sent and received.
133  */
134 CLIENT *
135 clntudp_bufcreate(
136         struct sockaddr_in *raddr,
137         uint32_t program,
138         uint32_t version,
139         struct timeval wait,
140         int *sockp,
141         uint_t sendsz,
142         uint_t recvsz)
143 {
144         CLIENT *cl;
145         struct cu_data *cu;
146         struct timeval now;
147         struct rpc_msg call_msg;
148
149         cl = (CLIENT *)mem_alloc(sizeof(CLIENT));
150         if (cl == NULL) {
151                 (void) fprintf(stderr, "clntudp_create: out of memory\n");
152                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
153                 rpc_createerr.cf_error.re_errno = errno;
154                 goto fooy;
155         }
156         sendsz = ((sendsz + 3) / 4) * 4;
157         recvsz = ((recvsz + 3) / 4) * 4;
158         cu = (struct cu_data *)mem_alloc(sizeof(struct cu_data) +
159                                                 sendsz + recvsz);
160         if (cu == NULL) {
161                 (void) fprintf(stderr, "clntudp_create: out of memory\n");
162                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
163                 rpc_createerr.cf_error.re_errno = errno;
164                 goto fooy;
165         }
166         cu->cu_outbuf = &cu->cu_inbuf[recvsz];
167
168         (void)gettimeofday(&now, (struct timezone *)0);
169         if (raddr->sin_port == 0) {
170                 uint16_t port;
171                 if ((port =
172                     pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
173                         goto fooy;
174                 }
175                 raddr->sin_port = htons(port);
176         }
177         cl->cl_ops = &udp_ops;
178         cl->cl_private = (char *)cu;
179         cu->cu_raddr = *raddr;
180         cu->cu_rlen = sizeof (cu->cu_raddr);
181         cu->cu_wait = wait;
182         cu->cu_total.tv_sec = -1;
183         cu->cu_total.tv_usec = -1;
184         cu->cu_sendsz = sendsz;
185         cu->cu_recvsz = recvsz;
186         call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec;
187         call_msg.rm_direction = CALL;
188         call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
189         call_msg.rm_call.cb_prog = program;
190         call_msg.rm_call.cb_vers = version;
191         xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf,
192             sendsz, XDR_ENCODE);
193         if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
194                 goto fooy;
195         }
196         cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
197         if (*sockp < 0) {
198 #if defined(O_NONBLOCK)
199                 int flags;
200 #elif defined(FIONBIO)
201                 int dontblock = 1;
202 #endif
203
204                 *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
205                 if (*sockp < 0) {
206                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
207                         rpc_createerr.cf_error.re_errno = errno;
208                         goto fooy;
209                 }
210                 /* attempt to bind to prov port */
211                 (void)bindresvport(*sockp, (struct sockaddr_in *)0);
212                 /* the sockets rpc controls are non-blocking */
213 #if defined(O_NONBLOCK)
214                 flags = fcntl(*sockp, F_GETFL, 0) | O_NONBLOCK;
215                 (void)fcntl(*sockp, F_SETFL, flags);
216 #elif defined(FIONBIO)
217                 (void)ioctl(*sockp, FIONBIO, (char *) &dontblock);
218 #endif
219                 cu->cu_closeit = TRUE;
220         } else {
221                 cu->cu_closeit = FALSE;
222         }
223         cu->cu_sock = *sockp;
224         cl->cl_auth = authnone_create();
225         return (cl);
226 fooy:
227         if (cu)
228                 mem_free((void *)cu, sizeof(struct cu_data) + sendsz + recvsz);
229         if (cl)
230                 mem_free((void *)cl, sizeof(CLIENT));
231         return ((CLIENT *)NULL);
232 }
233
234 CLIENT *
235 clntudp_create(
236         struct sockaddr_in *raddr,
237         uint32_t program,
238         uint32_t version,
239         struct timeval wait,
240         int *sockp)
241 {
242
243         return(clntudp_bufcreate(raddr, program, version, wait, sockp,
244             UDPMSGSIZE, UDPMSGSIZE));
245 }
246
247 static enum clnt_stat 
248 clntudp_call(
249         CLIENT  *cl,
250         uint32_t        proc,
251         xdrproc_t       xargs,
252         void *          argsp,
253         xdrproc_t       xresults,
254         void *          resultsp,
255         struct timeval  utimeout)
256 {
257         struct cu_data *cu = (struct cu_data *)cl->cl_private;
258         XDR *xdrs;
259         int outlen;
260         int inlen;
261 #if defined(AIX)
262         size_t fromlen;
263 #else
264         int fromlen;
265 #endif
266 #ifdef FD_SETSIZE
267         fd_set readfds;
268         fd_set mask;
269 #else
270         int readfds;
271         int mask;
272 #endif /* def FD_SETSIZE */
273         struct sockaddr_in from;
274         struct rpc_msg reply_msg;
275         XDR reply_xdrs;
276         struct timeval time_waited;
277         bool_t ok;
278         int nrefreshes = 2;     /* number of times to refresh cred */
279         struct timeval timeout;
280
281         if (cu->cu_total.tv_usec == -1) {
282                 timeout = utimeout;     /* use supplied timeout */
283         } else {
284                 timeout = cu->cu_total; /* use default timeout */
285         }
286
287         time_waited.tv_sec = 0;
288         time_waited.tv_usec = 0;
289 call_again:
290         xdrs = &(cu->cu_outxdrs);
291         xdrs->x_op = XDR_ENCODE;
292         XDR_SETPOS(xdrs, cu->cu_xdrpos);
293         /*
294          * the transaction is the first thing in the out buffer
295          */
296         (*(uint32_t *)(cu->cu_outbuf))++;
297         if ((! XDR_PUTLONG(xdrs, (int32_t *)&proc)) ||
298             (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
299             (! (*xargs)(xdrs, argsp)))
300                 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
301         outlen = (int)XDR_GETPOS(xdrs);
302
303 send_again:
304         if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0,
305             (struct sockaddr *)&(cu->cu_raddr), cu->cu_rlen)
306             != outlen) {
307                 cu->cu_error.re_errno = errno;
308                 return (cu->cu_error.re_status = RPC_CANTSEND);
309         }
310
311         /*
312          * Hack to provide rpc-based message passing
313          */
314         if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
315                 return (cu->cu_error.re_status = RPC_TIMEDOUT);
316         }
317         /*
318          * sub-optimal code appears here because we have
319          * some clock time to spare while the packets are in flight.
320          * (We assume that this is actually only executed once.)
321          */
322         reply_msg.acpted_rply.ar_verf = _null_auth;
323         reply_msg.acpted_rply.ar_results.where = resultsp;
324         reply_msg.acpted_rply.ar_results.proc = xresults;
325 #ifdef FD_SETSIZE
326         FD_ZERO(&mask);
327         FD_SET(cu->cu_sock, &mask);
328 #else
329         mask = 1 << cu->cu_sock;
330 #endif /* def FD_SETSIZE */
331         for (;;) {
332                 readfds = mask;
333                 switch (select(_rpc_dtablesize(), &readfds, NULL, 
334                                NULL, &(cu->cu_wait))) {
335
336                 case 0:
337                         time_waited.tv_sec += cu->cu_wait.tv_sec;
338                         time_waited.tv_usec += cu->cu_wait.tv_usec;
339                         while (time_waited.tv_usec >= 1000000) {
340                                 time_waited.tv_sec++;
341                                 time_waited.tv_usec -= 1000000;
342                         }
343                         if ((time_waited.tv_sec < timeout.tv_sec) ||
344                                 ((time_waited.tv_sec == timeout.tv_sec) &&
345                                 (time_waited.tv_usec < timeout.tv_usec)))
346                                 goto send_again;        
347                         return (cu->cu_error.re_status = RPC_TIMEDOUT);
348
349                 /*
350                  * buggy in other cases because time_waited is not being
351                  * updated.
352                  */
353                 case -1:
354                         if (errno == EINTR)
355                                 continue;       
356                         cu->cu_error.re_errno = errno;
357                         return (cu->cu_error.re_status = RPC_CANTRECV);
358                 }
359                 do {
360                         fromlen = sizeof(struct sockaddr);
361                         inlen = recvfrom(cu->cu_sock, cu->cu_inbuf, 
362                                 (int) cu->cu_recvsz, 0,
363                                 (struct sockaddr *)&from, &fromlen);
364                 } while (inlen < 0 && errno == EINTR);
365                 if (inlen < 0) {
366                         if (errno == EWOULDBLOCK)
367                                 continue;       
368                         cu->cu_error.re_errno = errno;
369                         return (cu->cu_error.re_status = RPC_CANTRECV);
370                 }
371                 if (inlen < sizeof(uint32_t))
372                         continue;       
373                 /* see if reply transaction id matches sent id */
374                 if (*((uint32_t *)(cu->cu_inbuf)) != *((uint32_t *)(cu->cu_outbuf)))
375                         continue;       
376                 /* we now assume we have the proper reply */
377                 break;
378         }
379
380         /*
381          * now decode and validate the response
382          */
383         xdrmem_create(&reply_xdrs, cu->cu_inbuf, (uint_t)inlen, XDR_DECODE);
384         ok = xdr_replymsg(&reply_xdrs, &reply_msg);
385         /* XDR_DESTROY(&reply_xdrs);  save a few cycles on noop destroy */
386         if (ok) {
387                 _seterr_reply(&reply_msg, &(cu->cu_error));
388                 if (cu->cu_error.re_status == RPC_SUCCESS) {
389                         if (! AUTH_VALIDATE(cl->cl_auth,
390                                 &reply_msg.acpted_rply.ar_verf)) {
391                                 cu->cu_error.re_status = RPC_AUTHERROR;
392                                 cu->cu_error.re_why = AUTH_INVALIDRESP;
393                         }
394                         if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
395                                 xdrs->x_op = XDR_FREE;
396                                 (void)xdr_opaque_auth(xdrs,
397                                     &(reply_msg.acpted_rply.ar_verf));
398                         } 
399                 }  /* end successful completion */
400                 else {
401                         /* maybe our credentials need to be refreshed ... */
402                         if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth)) {
403                                 nrefreshes--;
404                                 goto call_again;
405                         }
406                 }  /* end of unsuccessful completion */
407         }  /* end of valid reply message */
408         else {
409                 cu->cu_error.re_status = RPC_CANTDECODERES;
410         }
411         return (cu->cu_error.re_status);
412 }
413
414 static void
415 clntudp_geterr(
416         CLIENT *cl,
417         struct rpc_err *errp)
418 {
419         struct cu_data *cu = (struct cu_data *)cl->cl_private;
420
421         *errp = cu->cu_error;
422 }
423
424
425 static bool_t
426 clntudp_freeres(
427         CLIENT *cl,
428         xdrproc_t xdr_res,
429         void * res_ptr)
430 {
431         struct cu_data *cu = (struct cu_data *)cl->cl_private;
432         XDR *xdrs = &(cu->cu_outxdrs);
433
434         xdrs->x_op = XDR_FREE;
435         return ((*xdr_res)(xdrs, res_ptr));
436 }
437
438 /* ARGSUSED */
439 static void 
440 clntudp_abort(
441         CLIENT *h)
442 {
443 }
444
445 static bool_t
446 clntudp_control(
447         CLIENT *cl,
448         uint_t request,
449         void *info)
450 {
451         struct cu_data *cu = (struct cu_data *)cl->cl_private;
452
453         switch (request) {
454         case CLSET_TIMEOUT:
455                 cu->cu_total = *(struct timeval *)info;
456                 break;
457         case CLGET_TIMEOUT:
458                 *(struct timeval *)info = cu->cu_total;
459                 break;
460         case CLSET_RETRY_TIMEOUT:
461                 cu->cu_wait = *(struct timeval *)info;
462                 break;
463         case CLGET_RETRY_TIMEOUT:
464                 *(struct timeval *)info = cu->cu_wait;
465                 break;
466         case CLGET_SERVER_ADDR:
467                 *(struct sockaddr_in *)info = cu->cu_raddr;
468                 break;
469         default:
470                 return (FALSE);
471         }
472         return (TRUE);
473 }
474         
475 static void
476 clntudp_destroy(
477         CLIENT *cl)
478 {
479         struct cu_data *cu = (struct cu_data *)cl->cl_private;
480
481         if (cu->cu_closeit) {
482                 (void)close(cu->cu_sock);
483         }
484         XDR_DESTROY(&(cu->cu_outxdrs));
485         mem_free((void *)cu, (sizeof(struct cu_data) + cu->cu_sendsz + cu->cu_recvsz));
486         mem_free((void *)cl, sizeof(CLIENT));
487 }
488
489 /* ARGSUSED */
490 static bool_t
491 clntudp_getreply(
492         CLIENT *cl,
493         xdrproc_t xproc,
494         void *xres,
495         int cnt,
496         uint32_t *xids,
497         uint32_t *xid,
498         struct timeval *tv)
499 {
500         return (FALSE);
501 }
502
503 /* ARGSUSED */
504 static int
505 clntudp_poll(
506         CLIENT *cl,
507         uint32_t usec)
508 {
509         return (-1);
510 }