Import TBBT (NFS trace replay).
[bluesky.git] / TBBT / trace_play / rpc / xdr_rec.c
1 #ifndef lint
2 static char sfs_xdr_rec_id[] = "@(#)xdr_rec.c     2.1     97/10/23";
3 #endif
4 /* @(#)xdr_rec.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[] = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
53 #endif
54
55 /*
56  * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
57  * layer above tcp (for rpc's use).
58  *
59  * Copyright (C) 1984, Sun Microsystems, Inc.
60  *
61  * These routines interface XDRSTREAMS to a tcp/ip connection.
62  * There is a record marking layer between the xdr stream
63  * and the tcp transport level.  A record is composed on one or more
64  * record fragments.  A record fragment is a thirty-two bit header followed
65  * by n bytes of data, where n is contained in the header.  The header
66  * is represented as a htonl(uint32_t).  Thegh order bit encodes
67  * whether or not the fragment is the last fragment of the record
68  * (1 => fragment is last, 0 => more fragments to follow. 
69  * The other 31 bits encode the byte length of the fragment.
70  */
71
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <unistd.h>
75 #include <string.h>
76 #include "rpc/types.h"
77 #include "rpc/xdr.h"
78 #include "rpc/osdep.h"
79
80 static bool_t   xdrrec_getlong(XDR *, int32_t *);
81 static bool_t   xdrrec_putlong(XDR *, int32_t *);
82 static bool_t   xdrrec_getbytes(XDR *, void *, uint_t);
83 static bool_t   xdrrec_putbytes(XDR *, void *, uint_t);
84 static uint_t   xdrrec_getpos(XDR *);
85 static bool_t   xdrrec_setpos(XDR *, uint_t);
86 static int32_t *xdrrec_inline(XDR *, uint_t);
87 static void     xdrrec_destroy(XDR *);
88
89 static struct  xdr_ops xdrrec_ops = {
90         xdrrec_getlong,
91         xdrrec_putlong,
92         xdrrec_getbytes,
93         xdrrec_putbytes,
94         xdrrec_getpos,
95         xdrrec_setpos,
96         xdrrec_inline,
97         xdrrec_destroy
98 };
99
100 /*
101  * A record is composed of one or more record fragments.
102  * A record fragment is a two-byte header followed by zero to
103  * 2**32-1 bytes.  The header is treated as a long unsigned and is
104  * encode/decoded to the network via htonl/ntohl.  The low order 31 bits
105  * are a byte count of the fragment.  The highest order bit is a boolean:
106  * 1 => this fragment is the last fragment of the record,
107  * 0 => this fragment is followed by more fragment(s).
108  *
109  * The fragment/record machinery is not general;  it is constructed to
110  * meet the needs of xdr and rpc based on tcp.
111  */
112
113 #define LAST_FRAG (0x80000000)
114
115 typedef struct rec_strm {
116         void * tcp_handle;
117         void * the_buffer;
118         /*
119          * out-goung bits
120          */
121         int (*writeit)();
122         char *out_base;         /* output buffer (points to frag header) */
123         char *out_finger;       /* next output position */
124         char *out_boundry;      /* data cannot up to this address */
125         uint32_t *frag_header;  /* beginning of curren fragment */
126         bool_t frag_sent;       /* true if buffer sent in middle of record */
127         /*
128          * in-coming bits
129          */
130         int (*readit)();
131         uint32_t in_size;       /* fixed size of the input buffer */
132         char *in_base;
133         char *in_finger;        /* location of next byte to be had */
134         char *in_boundry;       /* can read up to this location */
135         int32_t fbtbc;          /* fragment bytes to be consumed */
136         bool_t last_frag;
137         uint_t sendsize;
138         uint_t recvsize;
139 } RECSTREAM;
140
141
142 static uint_t   fix_buf_size(uint_t);
143 static bool_t   flush_out(RECSTREAM *, bool_t);
144 static bool_t   set_input_fragment(RECSTREAM *);
145 static bool_t   skip_input_bytes(RECSTREAM *, int32_t);
146 static bool_t   get_input_bytes(RECSTREAM *, char *, int);
147
148 /*
149  * Create an xdr handle for xdrrec
150  * xdrrec_create fills in xdrs.  Sendsize and recvsize are
151  * send and recv buffer sizes (0 => use default).
152  * tcp_handle is an opaque handle that is passed as the first parameter to
153  * the procedures readit and writeit.  Readit and writeit are read and
154  * write respectively.   They are like the system
155  * calls expect that they take an opaque handle rather than an fd.
156  */
157 void
158 xdrrec_create(
159         XDR *xdrs,
160         uint_t sendsize,
161         uint_t recvsize,
162         void * tcp_handle,
163         int (*readit)(),  /* like read, but pass it a tcp_handle, not sock */
164         int (*writeit)())  /* like write, but pass it a tcp_handle, not sock */
165 {
166         RECSTREAM *rstrm =
167                 (RECSTREAM *)mem_alloc(sizeof(RECSTREAM));
168
169         if (rstrm == NULL) {
170                 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
171                 /* 
172                  *  This is bad.  Should rework xdrrec_create to 
173                  *  return a handle, and in this case return NULL
174                  */
175                 return;
176         }
177         /*
178          * adjust sizes and allocate buffer quad byte aligned
179          */
180         rstrm->sendsize = sendsize = fix_buf_size(sendsize);
181         rstrm->recvsize = recvsize = fix_buf_size(recvsize);
182         rstrm->the_buffer = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
183         if (rstrm->the_buffer == NULL) {
184                 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
185                 return;
186         }
187         for (rstrm->out_base = rstrm->the_buffer;
188                 (uint_t)rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
189                 rstrm->out_base++);
190         rstrm->in_base = rstrm->out_base + sendsize;
191         /*
192          * now the rest ...
193          */
194         xdrs->x_ops = &xdrrec_ops;
195         xdrs->x_private = (void *)rstrm;
196         rstrm->tcp_handle = tcp_handle;
197         rstrm->readit = readit;
198         rstrm->writeit = writeit;
199         rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
200         rstrm->frag_header = (uint32_t *)rstrm->out_base;
201         rstrm->out_finger += sizeof(uint32_t);
202         rstrm->out_boundry += sendsize;
203         rstrm->frag_sent = FALSE;
204         rstrm->in_size = recvsize;
205         rstrm->in_boundry = rstrm->in_base;
206         rstrm->in_finger = (rstrm->in_boundry += recvsize);
207         rstrm->fbtbc = 0;
208         rstrm->last_frag = TRUE;
209 }
210
211
212 /*
213  * The reoutines defined below are the xdr ops which will go into the
214  * xdr handle filled in by xdrrec_create.
215  */
216
217 static bool_t
218 xdrrec_getlong(
219         XDR *xdrs,
220         int32_t *lp)
221 {
222         RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
223         int32_t *buflp = (int32_t *)(rstrm->in_finger);
224         int32_t mylong;
225
226         /* first try the inline, fast case */
227         if ((rstrm->fbtbc >= sizeof(int32_t)) &&
228                 (((int)rstrm->in_boundry - (int)buflp) >= sizeof(int32_t))) {
229                 *lp = (int32_t)ntohl((uint32_t)(*buflp));
230                 rstrm->fbtbc -= sizeof(int32_t);
231                 rstrm->in_finger += sizeof(int32_t);
232         } else {
233                 if (! xdrrec_getbytes(xdrs, (void *)&mylong, sizeof(int32_t)))
234                         return (FALSE);
235                 *lp = (int32_t)ntohl((uint32_t)mylong);
236         }
237         return (TRUE);
238 }
239
240 static bool_t
241 xdrrec_putlong(
242         XDR *xdrs,
243         int32_t *lp)
244 {
245         RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
246         int32_t *dest_lp = ((int32_t *)(rstrm->out_finger));
247
248         if ((rstrm->out_finger += sizeof(int32_t)) > rstrm->out_boundry) {
249                 /*
250                  * this case should almost never happen so the code is
251                  * inefficient
252                  */
253                 rstrm->out_finger -= sizeof(int32_t);
254                 rstrm->frag_sent = TRUE;
255                 if (! flush_out(rstrm, FALSE))
256                         return (FALSE);
257                 dest_lp = ((int32_t *)(rstrm->out_finger));
258                 rstrm->out_finger += sizeof(int32_t);
259         }
260         *dest_lp = (int32_t)htonl((uint32_t)(*lp));
261         return (TRUE);
262 }
263
264 static bool_t  /* must manage buffers, fragments, and records */
265 xdrrec_getbytes(
266         XDR *xdrs,
267         void *baddr,
268         uint_t len)
269 {
270         char *addr = baddr;
271         RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
272         int current;
273
274         while (len > 0) {
275                 current = rstrm->fbtbc;
276                 if (current == 0) {
277                         if (rstrm->last_frag)
278                                 return (FALSE);
279                         if (! set_input_fragment(rstrm))
280                                 return (FALSE);
281                         continue;
282                 }
283                 current = (len < current) ? len : current;
284                 if (! get_input_bytes(rstrm, addr, current))
285                         return (FALSE);
286                 addr += current; 
287                 rstrm->fbtbc -= current;
288                 len -= current;
289         }
290         return (TRUE);
291 }
292
293 static bool_t
294 xdrrec_putbytes(
295         XDR *xdrs,
296         void *baddr,
297         uint_t len)
298 {
299         char *addr = baddr;
300         RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
301         int current;
302
303         while (len > 0) {
304                 current = (uint_t)rstrm->out_boundry - (uint_t)rstrm->out_finger;
305                 current = (len < current) ? len : current;
306                 memmove(rstrm->out_finger, addr, current);
307                 rstrm->out_finger += current;
308                 addr += current;
309                 len -= current;
310                 if (rstrm->out_finger == rstrm->out_boundry) {
311                         rstrm->frag_sent = TRUE;
312                         if (! flush_out(rstrm, FALSE))
313                                 return (FALSE);
314                 }
315         }
316         return (TRUE);
317 }
318
319 static uint_t
320 xdrrec_getpos(
321         XDR *xdrs)
322 {
323         RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
324         int32_t pos;
325
326         pos = lseek((int)rstrm->tcp_handle, (int32_t) 0, 1);
327         if (pos != -1)
328                 switch (xdrs->x_op) {
329
330                 case XDR_ENCODE:
331                         pos += rstrm->out_finger - rstrm->out_base;
332                         break;
333
334                 case XDR_DECODE:
335                         pos -= rstrm->in_boundry - rstrm->in_finger;
336                         break;
337
338                 default:
339                         pos = (uint_t) -1;
340                         break;
341                 }
342         return ((uint_t) pos);
343 }
344
345 static bool_t
346 xdrrec_setpos(
347         XDR *xdrs,
348         uint_t pos)
349 {
350         RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
351         uint_t currpos = xdrrec_getpos(xdrs);
352         int delta = currpos - pos;
353         char *newpos;
354
355         if ((int)currpos != -1)
356                 switch (xdrs->x_op) {
357
358                 case XDR_ENCODE:
359                         newpos = rstrm->out_finger - delta;
360                         if ((newpos > (char *)(rstrm->frag_header)) &&
361                                 (newpos < rstrm->out_boundry)) {
362                                 rstrm->out_finger = newpos;
363                                 return (TRUE);
364                         }
365                         break;
366
367                 case XDR_DECODE:
368                         newpos = rstrm->in_finger - delta;
369                         if ((delta < (int)(rstrm->fbtbc)) &&
370                                 (newpos <= rstrm->in_boundry) &&
371                                 (newpos >= rstrm->in_base)) {
372                                 rstrm->in_finger = newpos;
373                                 rstrm->fbtbc -= delta;
374                                 return (TRUE);
375                         }
376                         break;
377                 }
378         return (FALSE);
379 }
380
381 static int32_t *
382 xdrrec_inline(
383         XDR *xdrs,
384         uint_t len)
385 {
386         RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
387         int32_t * buf = NULL;
388
389         switch (xdrs->x_op) {
390
391         case XDR_ENCODE:
392                 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
393                         buf = (int32_t *) rstrm->out_finger;
394                         rstrm->out_finger += len;
395                 }
396                 break;
397
398         case XDR_DECODE:
399                 if ((len <= rstrm->fbtbc) &&
400                         ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
401                         buf = (int32_t *) rstrm->in_finger;
402                         rstrm->fbtbc -= len;
403                         rstrm->in_finger += len;
404                 }
405                 break;
406         }
407         return (buf);
408 }
409
410 static void
411 xdrrec_destroy(
412         XDR *xdrs)
413 {
414         RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
415
416         mem_free(rstrm->the_buffer,
417                 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
418         mem_free((void *)rstrm, sizeof(RECSTREAM));
419 }
420
421
422 /*
423  * Exported routines to manage xdr records
424  */
425
426 /*
427  * Before reading (deserializing from the stream, one should always call
428  * this procedure to guarantee proper record alignment.
429  */
430 bool_t
431 xdrrec_skiprecord(
432         XDR *xdrs)
433 {
434         RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
435
436         while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
437                 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
438                         return (FALSE);
439                 rstrm->fbtbc = 0;
440                 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
441                         return (FALSE);
442         }
443         rstrm->last_frag = FALSE;
444         return (TRUE);
445 }
446
447 /*
448  * Look ahead fuction.
449  * Returns TRUE iff there is no more input in the buffer 
450  * after consuming the rest of the current record.
451  */
452 bool_t
453 xdrrec_eof(
454         XDR *xdrs)
455 {
456         RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
457
458         while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
459                 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
460                         return (TRUE);
461                 rstrm->fbtbc = 0;
462                 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
463                         return (TRUE);
464         }
465         if (rstrm->in_finger == rstrm->in_boundry)
466                 return (TRUE);
467         return (FALSE);
468 }
469
470 /*
471  * The client must tell the package when an end-of-record has occurred.
472  * The second paraemters tells whether the record should be flushed to the
473  * (output) tcp stream.  (This let's the package support batched or
474  * pipelined procedure calls.)  TRUE => immmediate flush to tcp connection.
475  */
476 bool_t
477 xdrrec_endofrecord(
478         XDR *xdrs,
479         bool_t sendnow)
480 {
481         RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
482         uint32_t len;  /* fragment length */
483
484         if (sendnow || rstrm->frag_sent ||
485                 ((uint32_t)rstrm->out_finger + sizeof(uint32_t) >=
486                 (uint32_t)rstrm->out_boundry)) {
487                 rstrm->frag_sent = FALSE;
488                 return (flush_out(rstrm, TRUE));
489         }
490         len = (uint32_t)(rstrm->out_finger) - (uint32_t)(rstrm->frag_header) -
491            sizeof(uint32_t);
492         *(rstrm->frag_header) = htonl((uint32_t)len | (uint32_t)LAST_FRAG);
493         rstrm->frag_header = (uint32_t *)rstrm->out_finger;
494         rstrm->out_finger += sizeof(uint32_t);
495         return (TRUE);
496 }
497
498
499 /*
500  * Internal useful routines
501  */
502 static bool_t
503 flush_out(
504         RECSTREAM *rstrm,
505         bool_t eor)
506 {
507         uint32_t eormask = (eor == TRUE) ? LAST_FRAG : 0;
508         uint32_t len = (uint32_t)(rstrm->out_finger) - 
509                 (uint32_t)(rstrm->frag_header) - sizeof(uint32_t);
510
511         *(rstrm->frag_header) = htonl(len | eormask);
512         len = (uint32_t)(rstrm->out_finger) - (uint32_t)(rstrm->out_base);
513         if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
514                 != (int)len)
515                 return (FALSE);
516         rstrm->frag_header = (uint32_t *)rstrm->out_base;
517         rstrm->out_finger = (char *)rstrm->out_base + sizeof(uint32_t);
518         return (TRUE);
519 }
520
521 static bool_t  /* knows nothing about records!  Only about input buffers */
522 fill_input_buf(
523         RECSTREAM *rstrm)
524 {
525         char *where;
526         uint_t i;
527         int len;
528
529         where = rstrm->in_base;
530         i = (uint_t)rstrm->in_boundry % BYTES_PER_XDR_UNIT;
531         where += i;
532         len = rstrm->in_size - i;
533         if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
534                 return (FALSE);
535         rstrm->in_finger = where;
536         where += len;
537         rstrm->in_boundry = where;
538         return (TRUE);
539 }
540
541 static bool_t  /* knows nothing about records!  Only about input buffers */
542 get_input_bytes(
543         RECSTREAM *rstrm,
544         char *addr,
545         int len)
546 {
547         int current;
548
549         while (len > 0) {
550                 current = (int)rstrm->in_boundry - (int)rstrm->in_finger;
551                 if (current == 0) {
552                         if (! fill_input_buf(rstrm))
553                                 return (FALSE);
554                         continue;
555                 }
556                 current = (len < current) ? len : current;
557                 memmove(addr, rstrm->in_finger, current);
558                 rstrm->in_finger += current;
559                 addr += current;
560                 len -= current;
561         }
562         return (TRUE);
563 }
564
565 static bool_t  /* next two bytes of the input stream are treated as a header */
566 set_input_fragment(
567         RECSTREAM *rstrm)
568 {
569         uint32_t header;
570
571         if (! get_input_bytes(rstrm, (void *)&header, sizeof(header)))
572                 return (FALSE);
573         header = (int32_t)ntohl(header);
574         rstrm->last_frag = ((header & (uint32_t)LAST_FRAG) == 0) ? FALSE : TRUE;
575         rstrm->fbtbc = header & (~LAST_FRAG);
576         return (TRUE);
577 }
578
579 static bool_t  /* consumes input bytes; knows nothing about records! */
580 skip_input_bytes(
581         RECSTREAM *rstrm,
582         int32_t cnt)
583 {
584         int current;
585
586         while (cnt > 0) {
587                 current = (int)rstrm->in_boundry - (int)rstrm->in_finger;
588                 if (current == 0) {
589                         if (! fill_input_buf(rstrm))
590                                 return (FALSE);
591                         continue;
592                 }
593                 current = (cnt < current) ? cnt : current;
594                 rstrm->in_finger += current;
595                 cnt -= current;
596         }
597         return (TRUE);
598 }
599
600 static uint_t
601 fix_buf_size(
602         uint_t s)
603 {
604
605         if (s < 100)
606                 s = 4000;
607         return (RNDUP(s));
608 }