Add proper per-file copyright notices/licenses and top-level license.
[bluesky.git] / TBBT / trace_play / rpc / sfs_cudp.c
1 #ifndef lint
2 static char sfs_cudp_id[] = "@(#)sfs_cudp.c     2.1     97/10/23";
3 #endif
4
5 /* @(#)clnt_udp.c       2.2 88/08/01 4.0 RPCSRC */
6 /*
7  *   Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
8  *      All rights reserved.
9  *              Standard Performance Evaluation Corporation (SPEC)
10  *              6585 Merchant Place, Suite 100
11  *              Warrenton, VA 20187
12  *
13  *      This product contains benchmarks acquired from several sources who
14  *      understand and agree with SPEC's goal of creating fair and objective
15  *      benchmarks to measure computer performance.
16  *
17  *      This copyright notice is placed here only to protect SPEC in the
18  *      event the source is misused in any manner that is contrary to the
19  *      spirit, the goals and the intent of SPEC.
20  *
21  *      The source code is provided to the user or company under the license
22  *      agreement for the SPEC Benchmark Suite for this product.
23  */
24 /*
25  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
26  * unrestricted use provided that this legend is included on all tape
27  * media and as a part of the software program in whole or part.  Users
28  * may copy or modify Sun RPC without charge, but are not authorized
29  * to license or distribute it to anyone else except as part of a product or
30  * program developed by the user.
31  * 
32  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
33  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
34  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
35  * 
36  * Sun RPC is provided with no support and without any obligation on the
37  * part of Sun Microsystems, Inc. to assist in its use, correction,
38  * modification or enhancement.
39  * 
40  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
41  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
42  * OR ANY PART THEREOF.
43  * 
44  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
45  * or profits or other special, indirect and consequential damages, even if
46  * Sun has been advised of the possibility of such damages.
47  * 
48  * Sun Microsystems, Inc.
49  * 2550 Garcia Avenue
50  * Mountain View, California  94043
51  */
52
53 /*
54  * clnt_udp.c, Implements a UDP/IP based, client side RPC.
55  *
56  * Copyright (C) 1984, Sun Microsystems, Inc.
57  */
58
59
60 #include <stdio.h>
61 #include <unistd.h>
62 #include <stdlib.h>
63 #ifndef FreeBSD
64 #include <stropts.h>
65 #endif  /* ndef FreeBSD */
66 #include <string.h>
67 #include "rpc/rpc.h"
68 #include "rpc/osdep.h"
69 #include <netdb.h>
70 #include "rpc/pmap_clnt.h"
71 #include <errno.h>
72 #include "rfs_c_def.h"  /* Just for the define of RFS */
73 /*
74  * UDP bases client side rpc operations
75  */
76 static enum clnt_stat sfs_cudp_call(CLIENT *, uint32_t, xdrproc_t,
77                                         void *, xdrproc_t, void *,
78                                         struct timeval);
79 static void             sfs_cudp_abort(CLIENT *h);
80 static void             sfs_cudp_geterr(CLIENT *, struct rpc_err *);
81 static bool_t           sfs_cudp_freeres(CLIENT *, xdrproc_t, void *);
82 static bool_t           sfs_cudp_control(CLIENT *, uint_t, void *);
83 static void             sfs_cudp_destroy(CLIENT *);
84 static bool_t           sfs_cudp_getreply(CLIENT *, xdrproc_t, void *,
85                                 int, uint32_t *, uint32_t *, struct timeval *);
86 static int              sfs_cudp_poll(CLIENT *, uint32_t);
87 static enum clnt_stat
88 get_areply(
89         CLIENT *cl,
90         uint32_t *xid,
91         struct timeval *timeout);
92
93 static struct clnt_ops sfs_cudp_ops = {
94         sfs_cudp_call,
95         sfs_cudp_abort,
96         sfs_cudp_geterr,
97         sfs_cudp_freeres,
98         sfs_cudp_destroy,
99         sfs_cudp_control,
100         sfs_cudp_getreply,
101         sfs_cudp_poll
102 };
103
104 /* 
105  * Private data kept per client handle
106  */
107 struct cu_data {
108         int                cu_sock;
109         bool_t             cu_closeit;
110         struct sockaddr_in cu_raddr;
111         int                cu_rlen;
112         struct rpc_err cu_error;
113         XDR                cu_outxdrs;
114         uint_t             cu_xdrpos;
115         uint_t             cu_sendsz;
116         char               *cu_outbuf;
117         uint_t             cu_recvsz;
118         char               cu_inbuf[1];
119 };
120
121 /*
122  * Create a UDP based client handle.
123  * If *sockp<0, *sockp is set to a newly created UPD socket.
124  * If raddr->sin_port is 0 a binder on the remote machine
125  * is consulted for the correct port number.
126  * NB: It is the clients responsibility to close *sockp.
127  * NB: The rpch->cl_auth is initialized to null authentication.
128  *     Caller may wish to set this something more useful.
129  *
130  * wait is the amount of time used between retransmitting a call if
131  * no response has been heard;  retransmition occurs until the actual
132  * rpc call times out.
133  *
134  * sendsz and recvsz are the maximum allowable packet sizes that can be
135  * sent and received.
136  */
137 /* ARGSUSED */
138 CLIENT *
139 sfs_cudp_bufcreate(
140         struct sockaddr_in *raddr,
141         uint32_t program,
142         uint32_t version,
143         struct timeval wait,
144         int *sockp,
145         uint_t sendsz,
146         uint_t recvsz)
147 {
148         CLIENT *cl;
149         struct cu_data *cu;
150         struct timeval now;
151         struct rpc_msg call_msg;
152         int min_buf_sz;
153         int new_buf_sz;
154         int type;
155         int i;
156         int error;
157 #if defined(UNIXWARE) || defined(AIX)
158         size_t optlen;
159 #else
160         int optlen;
161 #endif
162
163         cl = (CLIENT *)mem_alloc(sizeof(CLIENT));
164         if (cl == NULL) {
165                 (void) fprintf(stderr, "sfs_cudp_create: out of memory\n");
166                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
167                 rpc_createerr.cf_error.re_errno = errno;
168                 goto fooy;
169         }
170         sendsz = ((sendsz + 3) / 4) * 4;
171         recvsz = ((recvsz + 3) / 4) * 4;
172         cu = (struct cu_data *)mem_alloc(sizeof(struct cu_data) +
173                                                         sendsz + recvsz);
174         if (cu == NULL) {
175                 (void) fprintf(stderr, "sfs_cudp_create: out of memory\n");
176                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
177                 rpc_createerr.cf_error.re_errno = errno;
178                 goto fooy;
179         }
180         cu->cu_outbuf = &cu->cu_inbuf[recvsz];
181
182         (void)gettimeofday(&now, (struct timezone *)0);
183         if (raddr->sin_port == 0) {
184                 uint16_t port;
185                 if ((port =
186                     pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
187                         goto fooy;
188                 }
189                 raddr->sin_port = htons(port);
190         }
191         cl->cl_ops = &sfs_cudp_ops;
192         cl->cl_private = (void *)cu;
193         cu->cu_raddr = *raddr;
194         cu->cu_rlen = sizeof (cu->cu_raddr);
195         cu->cu_sendsz = sendsz;
196         cu->cu_recvsz = recvsz;
197         call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec;
198         call_msg.rm_direction = CALL;
199         call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
200         call_msg.rm_call.cb_prog = program;
201         call_msg.rm_call.cb_vers = version;
202         xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf,
203             sendsz, XDR_ENCODE);
204         if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
205                 goto fooy;
206         }
207         cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
208         if (*sockp < 0) {
209 #if defined(O_NONBLOCK)
210                 int flags;
211 #elif defined(FIONBIO)
212                 int dontblock = 1;
213 #endif
214
215                 *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
216                 if (*sockp < 0) {
217                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
218                         rpc_createerr.cf_error.re_errno = errno;
219                         goto fooy;
220                 }
221                 /* attempt to bind to prov port */
222                 (void)bindresvport(*sockp, (struct sockaddr_in *)0);
223                 /* the sockets rpc controls are non-blocking */
224 #if defined(O_NONBLOCK)
225                 flags = fcntl(*sockp, F_GETFL, 0) | O_NONBLOCK;
226                 (void)fcntl(*sockp, F_SETFL, flags);
227 #elif defined(FIONBIO)
228                 (void)ioctl(*sockp, FIONBIO, (char *) &dontblock);
229 #endif
230                 cu->cu_closeit = TRUE;
231         } else {
232                 cu->cu_closeit = FALSE;
233         }
234         cu->cu_sock = *sockp;
235         /*
236          * Need to try to size the socket buffers based on the number of
237          * outstanding requests desired.  NFS reads and writes can do as
238          * much as 8K per request which can quickly run us out of space
239          * on the socket buffer queue.  Use the maximum number of bio style
240          * requests * NFS_MAXDATA plus a pad as a starting point for desired
241          * socket buffer size and then back off by NFS_MAXDATA until the buffer
242          * sizes are successfully set.  Note, the algorithm never sets the
243          * buffer size to less than the OS default.
244          */  
245         type = SO_SNDBUF;
246         for (i = 0; i < 2; i++) {
247                 optlen = sizeof(min_buf_sz);
248 #if defined(UNIXWARE)
249                 if (getsockopt(cu->cu_sock, SOL_SOCKET, type,
250                                 (void *)&min_buf_sz, &optlen) < 0) {
251                         /* guess the default */
252                         min_buf_sz = 18 * 1024;
253                 }
254 #else
255                 if (getsockopt(cu->cu_sock, SOL_SOCKET, type,
256                                 (char *)&min_buf_sz, &optlen) < 0) {
257                         /* guess the default */
258                         min_buf_sz = 18 * 1024;
259                 }
260 #endif
261
262                 new_buf_sz = 512 * 1024;
263                 if (new_buf_sz > min_buf_sz) {
264                         do {
265                                 error = setsockopt(cu->cu_sock, SOL_SOCKET,
266                                                 type, (char *)&new_buf_sz,
267                                                 sizeof(int));
268                                 new_buf_sz -= (8 * 1024);
269                         } while (error != 0 && new_buf_sz > min_buf_sz);
270                 }
271
272                 type = SO_RCVBUF;
273         }
274
275         cl->cl_auth = authnone_create();
276         return (cl);
277 fooy:
278         if (cu)
279                 mem_free((void *)cu, sizeof(struct cu_data) + sendsz + recvsz);
280         if (cl)
281                 mem_free((void *)cl, sizeof(CLIENT));
282         return ((CLIENT *)NULL);
283 }
284
285 CLIENT *
286 sfs_cudp_create(
287         struct sockaddr_in *raddr,
288         uint32_t program,
289         uint32_t version,
290         struct timeval wait,
291         int *sockp)
292 {
293
294         return(sfs_cudp_bufcreate(raddr, program, version, wait, sockp,
295             UDPMSGSIZE, UDPMSGSIZE));
296 }
297
298 #ifdef RFS
299 enum clnt_stat get_areply_udp (
300                 CLIENT * cl,
301                 uint32_t *xid,
302                 struct timeval *timeout) 
303 {
304         return get_areply (cl, xid, timeout);
305 }
306 #endif
307
308 static enum clnt_stat
309 get_areply(
310         CLIENT *cl,
311         uint32_t *xid,
312         struct timeval *timeout)
313 {
314         struct cu_data *cu = (struct cu_data *)cl->cl_private;
315         int inlen;
316 #if defined(AIX)
317         size_t fromlen;
318 #else
319         int fromlen;
320 #endif
321         struct sockaddr_in from;
322 #ifdef FD_SETSIZE
323         fd_set readfds;
324         fd_set mask;
325 #else
326         int readfds;
327         int mask;
328 #endif /* def FD_SETSIZE */
329
330 #ifdef FD_SETSIZE
331         FD_ZERO(&mask);
332         FD_SET(cu->cu_sock, &mask);
333 #else
334         mask = 1 << cu->cu_sock;
335 #endif /* def FD_SETSIZE */
336
337         for (;;) {
338                 readfds = mask;
339                 switch (select(_rpc_dtablesize(), &readfds, NULL, 
340                                NULL, timeout)) {
341
342                 case 0:
343                         return (cu->cu_error.re_status = RPC_TIMEDOUT);
344
345                 case -1:
346                         if (errno == EINTR)
347                                 continue;       
348                         cu->cu_error.re_errno = errno;
349                         return (cu->cu_error.re_status = RPC_CANTRECV);
350                 }
351                 do {
352                         fromlen = sizeof(struct sockaddr);
353                         inlen = recvfrom(cu->cu_sock, cu->cu_inbuf, 
354                                 (int) cu->cu_recvsz, 0,
355                                 (struct sockaddr *)&from, &fromlen);
356                 } while (inlen < 0 && errno == EINTR);
357                 if (inlen < 0) {
358                         if (errno == EWOULDBLOCK)
359                                 continue;       
360                         cu->cu_error.re_errno = errno;
361                         return (cu->cu_error.re_status = RPC_CANTRECV);
362                 }
363
364                 if (inlen < sizeof(uint32_t))
365                         continue;       
366
367                 *xid = ntohl(*((uint32_t *)(cu->cu_inbuf)));
368                 return (RPC_SUCCESS);
369         }
370 }
371
372 enum clnt_stat
373 proc_header(
374         CLIENT *cl,
375         xdrproc_t xdr_results,
376         void *results_ptr)
377 {
378         struct cu_data *cu = (struct cu_data *)cl->cl_private;
379         XDR *xdrs = &(cu->cu_outxdrs);
380         struct rpc_msg reply_msg;
381         XDR reply_xdrs;
382         bool_t ok;
383
384         /*
385          * now decode and validate the response
386          */
387         xdrmem_create(&reply_xdrs, cu->cu_inbuf, cu->cu_recvsz, XDR_DECODE);
388
389         reply_msg.acpted_rply.ar_verf = _null_auth;
390         reply_msg.acpted_rply.ar_results.where = results_ptr;
391         reply_msg.acpted_rply.ar_results.proc = xdr_results;
392
393         ok = xdr_replymsg(&reply_xdrs, &reply_msg);
394         /* XDR_DESTROY(&reply_xdrs);  save a few cycles on noop destroy */
395         if (!ok) {
396                 return (cu->cu_error.re_status = RPC_CANTDECODERES);
397         }
398
399         _seterr_reply(&reply_msg, &(cu->cu_error));
400
401         if (cu->cu_error.re_status == RPC_SUCCESS) {
402                 if (! AUTH_VALIDATE(cl->cl_auth,
403                                 &reply_msg.acpted_rply.ar_verf)) {
404                         cu->cu_error.re_status = RPC_AUTHERROR;
405                         cu->cu_error.re_why = AUTH_INVALIDRESP;
406                 }
407                 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
408                         xdrs->x_op = XDR_FREE;
409                         (void)xdr_opaque_auth(xdrs,
410                                 &(reply_msg.acpted_rply.ar_verf));
411                 }
412         }
413
414         return (cu->cu_error.re_status);
415 }
416
417 /*
418  * Non-standard changes.  Make a call an at-most-once with a per call
419  * timer.  Ignore the timeout set at creation. Never retransmit.
420  */
421 static enum clnt_stat 
422 sfs_cudp_call(
423         CLIENT          *cl,            /* client handle */
424         uint32_t        proc,           /* procedure number */
425         xdrproc_t       xargs,          /* xdr routine for args */
426         void *          argsp,          /* pointer to args */
427         xdrproc_t       xresults,       /* xdr routine for results */
428         void *          resultsp,       /* pointer to results */
429         struct timeval  timeout)        /* seconds to wait before giving up */
430 {
431         struct cu_data *cu = (struct cu_data *)cl->cl_private;
432         XDR *xdrs = &(cu->cu_outxdrs);
433         int outlen;
434         uint32_t x_id, r_xid;
435
436         xdrs->x_op = XDR_ENCODE;
437         XDR_SETPOS(xdrs, cu->cu_xdrpos);
438
439         /*
440          * the transaction is the first thing in the out buffer
441          */
442         (*(uint32_t *)(cu->cu_outbuf))++;
443         x_id = ntohl(*(uint32_t *)(cu->cu_outbuf));
444
445         if ((! XDR_PUTLONG(xdrs, (int32_t *)&proc)) ||
446             (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
447             (! (*xargs)(xdrs, argsp)))
448                 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
449
450         outlen = (int)XDR_GETPOS(xdrs);
451
452         if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0,
453             (struct sockaddr *)&(cu->cu_raddr), cu->cu_rlen) != outlen) {
454                 cu->cu_error.re_errno = errno;
455                 return (cu->cu_error.re_status = RPC_CANTSEND);
456         }
457
458         /*
459          * Hack to provide rpc-based message passing
460          */
461         if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
462                 /* 
463                  * Double hack, send back xid in results_prt if non-NULL
464                  */ 
465                 if (resultsp != NULL) 
466                         *(uint32_t *)resultsp = x_id; 
467
468                 return (cu->cu_error.re_status = RPC_TIMEDOUT);
469         }
470
471         /* CONSTCOND */
472         while (TRUE) {
473                 enum clnt_stat res;
474
475                 if ((res = get_areply(cl, &r_xid, &timeout)) != RPC_SUCCESS)
476                         return (res);
477
478                 if (r_xid == x_id)
479                         break;
480         }
481
482         /*
483          * process header
484          */
485         return (proc_header(cl, xresults, resultsp));
486 }
487
488 static void
489 sfs_cudp_geterr(CLIENT *cl, struct rpc_err *errp)
490 {
491         struct cu_data *cu = (struct cu_data *)cl->cl_private;
492
493         *errp = cu->cu_error;
494 }
495
496
497 static bool_t
498 sfs_cudp_freeres(
499         CLIENT *cl,
500         xdrproc_t xdr_res,
501         void * res_ptr)
502 {
503         struct cu_data *cu = (struct cu_data *)cl->cl_private;
504         XDR *xdrs = &(cu->cu_outxdrs);
505
506         xdrs->x_op = XDR_FREE;
507         return ((*xdr_res)(xdrs, res_ptr));
508 }
509
510 /* ARGSUSED */
511 static void 
512 sfs_cudp_abort(CLIENT *h)
513 {
514 }
515
516 static bool_t
517 sfs_cudp_control(
518         CLIENT *cl,
519         uint_t request,
520         void *info)
521 {
522         struct cu_data *cu = (struct cu_data *)cl->cl_private;
523
524         switch (request) {
525         case CLGET_SERVER_ADDR:
526                 *(struct sockaddr_in *)info = cu->cu_raddr;
527                 break;
528         default:
529                 return (FALSE);
530         }
531         return (TRUE);
532 }
533         
534 static void
535 sfs_cudp_destroy(CLIENT *cl)
536 {
537         struct cu_data *cu = (struct cu_data *)cl->cl_private;
538
539         if (cu->cu_closeit) {
540                 (void)close(cu->cu_sock);
541         }
542         XDR_DESTROY(&(cu->cu_outxdrs));
543         mem_free((void *)cu, (sizeof(struct cu_data) + cu->cu_sendsz + cu->cu_recvsz));
544         mem_free((void *)cl, sizeof(CLIENT));
545 }
546
547 /* ARGSUSED */
548 static bool_t
549 sfs_cudp_getreply(
550         CLIENT *cl,
551         xdrproc_t xproc,
552         void *xres,
553         int cnt,
554         uint32_t *xids,
555         uint32_t *xid,
556         struct timeval *tv)
557 {
558         struct cu_data *cu = (struct cu_data *)cl->cl_private;
559         bool_t res;
560         int i;
561
562         cu->cu_error.re_status = RPC_SUCCESS;
563
564         if ((res = get_areply(cl, xid, tv)) != RPC_SUCCESS)
565                 return (res);
566
567         /*
568          * Check to make sure xid matchs one that we are interested in
569          */
570         for (i = 0; i < cnt; i++) {
571                 if (xids[i] == *xid)
572                         break;
573         }
574
575         if (i == cnt)
576                 return (RPC_CANTDECODERES);
577
578         /*
579          * process header
580          */
581         return (proc_header(cl, xproc, xres));
582 }
583  
584 static int
585 sfs_cudp_poll(
586         CLIENT *cl,
587         uint32_t usecs)
588 {
589         struct cu_data *cu = (struct cu_data *)cl->cl_private;
590         struct timeval tv;
591 #ifdef FD_SETSIZE
592         fd_set mask;
593         fd_set readfds;
594  
595         FD_ZERO(&mask);
596         FD_SET(cu->cu_sock, &mask);
597 #else
598         int mask = 1 << (cu->cu_sock);
599         int readfds;
600 #endif /* def FD_SETSIZE */
601  
602         tv.tv_sec = 0; 
603         if (usecs > 1000000) 
604                 tv.tv_sec = usecs / 1000000; 
605         tv.tv_usec = usecs % 1000000; 
606  
607         readfds = mask;
608         return (select(_rpc_dtablesize(), &readfds, NULL, NULL, &tv));
609 }