Windows: build src/rx/tests
[openafs.git] / src / rx / xdr_rec.c
1 /*  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
2  * unrestricted use provided that this legend is included on all tape
3  * media and as a part of the software program in whole or part.  Users
4  * may copy or modify Sun RPC without charge, but are not authorized
5  * to license or distribute it to anyone else except as part of a product or
6  * program developed by the user.
7  *
8  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
9  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
10  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
11  *
12  * Sun RPC is provided with no support and without any obligation on the
13  * part of Sun Microsystems, Inc. to assist in its use, correction,
14  * modification or enhancement.
15  *
16  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
17  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
18  * OR ANY PART THEREOF.
19  *
20  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
21  * or profits or other special, indirect and consequential damages, even if
22  * Sun has been advised of the possibility of such damages.
23  *
24  * Sun Microsystems, Inc.
25  * 2550 Garcia Avenue
26  * Mountain View, California  94043
27  */
28 #ifndef NeXT
29
30 /*  * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
31  * layer above tcp (for rpc's use).
32  *
33  * Copyright (C) 1984, Sun Microsystems, Inc.
34  *
35  * These routines interface XDRSTREAMS to a tcp/ip connection.
36  * There is a record marking layer between the xdr stream
37  * and the tcp transport level.  A record is composed on one or more
38  * record fragments.  A record fragment is a thirty-two bit header followed
39  * by n bytes of data, where n is contained in the header.  The header
40  * is represented as a htonl(afs_uint32).  Thegh order bit encodes
41  * whether or not the fragment is the last fragment of the record
42  * (1 => fragment is last, 0 => more fragments to follow.
43  * The other 31 bits encode the byte length of the fragment.
44  */
45
46 #include <afsconfig.h>
47 #include <afs/param.h>
48
49
50 #include <stdio.h>
51 #ifdef HAVE_STDLIB_H
52 #include <stdlib.h>
53 #endif
54 #include "xdr.h"
55 #ifndef AFS_NT40_ENV
56 #include <sys/time.h>
57 #include <netinet/in.h>
58 #endif
59
60 #include <string.h>
61
62
63 /*  * A record is composed of one or more record fragments.
64  * A record fragment is a two-byte header followed by zero to
65  * 2**32-1 bytes.  The header is treated as an afs_int32 unsigned and is
66  * encode/decoded to the network via htonl/ntohl.  The low order 31 bits
67  * are a byte count of the fragment.  The highest order bit is a boolean:
68  * 1 => this fragment is the last fragment of the record,
69  * 0 => this fragment is followed by more fragment(s).
70  *
71  * The fragment/record machinery is not general;  it is constructed to
72  * meet the needs of xdr and rpc based on tcp.
73  */
74
75 #define LAST_FRAG ((afs_uint32)(1 << 31))
76
77 typedef struct rec_strm {
78     caddr_t tcp_handle;
79     /*       * out-goung bits
80      */
81     int (*writeit) (caddr_t tcp_handle, caddr_t out_base, int len);
82     caddr_t out_base;           /* output buffer (points to frag header) */
83     caddr_t out_finger;         /* next output position */
84     caddr_t out_boundry;        /* data cannot up to this address */
85     afs_uint32 *frag_header;    /* beginning of curren fragment */
86     bool_t frag_sent;           /* true if buffer sent in middle of record */
87     /*       * in-coming bits
88      */
89     int (*readit) (caddr_t tcp_handle, caddr_t out_base, int len);
90     afs_uint32 in_size;         /* fixed size of the input buffer */
91     caddr_t in_base;
92     caddr_t in_finger;          /* location of next byte to be had */
93     caddr_t in_boundry;         /* can read up to this location */
94     afs_int32 fbtbc;            /* fragment bytes to be consumed */
95     bool_t last_frag;
96     u_int sendsize;
97     u_int recvsize;
98 } RECSTREAM;
99
100 /* Prototypes for static routines */
101 static bool_t xdrrec_getint32(XDR * xdrs, afs_int32 * lp);
102 static bool_t xdrrec_putint32(XDR * xdrs, afs_int32 * lp);
103 static bool_t xdrrec_getbytes(XDR * xdrs, caddr_t addr,
104                               u_int len);
105 static bool_t xdrrec_putbytes(XDR * xdrs, caddr_t addr,
106                               u_int len);
107 static u_int xdrrec_getpos(XDR * xdrs);
108 static bool_t xdrrec_setpos(XDR * xdrs, u_int pos);
109 static afs_int32 *xdrrec_inline(XDR * xdrs, u_int len);
110 static void xdrrec_destroy(XDR * xdrs);
111 static bool_t flush_out(RECSTREAM * rstrm, bool_t eor);
112 static bool_t fill_input_buf(RECSTREAM * rstrm);
113 static bool_t get_input_bytes(RECSTREAM * rstrm,
114                               caddr_t addr, int len);
115 static bool_t set_input_fragment(RECSTREAM * rstrm);
116 static bool_t skip_input_bytes(RECSTREAM * rstrm, int cnt);
117 static u_int fix_buf_size(u_int s);
118
119 static struct xdr_ops xdrrec_ops = {
120 #ifdef AFS_NT40_ENV
121 #ifdef AFS_XDR_64BITOPS
122     NULL,
123     NULL,
124 #endif
125     /* Windows does not support labeled assignments */
126 #if !(defined(KERNEL) && defined(AFS_SUN57_ENV))
127     xdrrec_getint32,    /* deserialize an afs_int32 */
128     xdrrec_putint32,    /* serialize an afs_int32 */
129 #endif
130     xdrrec_getbytes,    /* deserialize counted bytes */
131     xdrrec_putbytes,    /* serialize counted bytes */
132     xdrrec_getpos,      /* get offset in the stream: not supported. */
133     xdrrec_setpos,      /* set offset in the stream: not supported. */
134     xdrrec_inline,      /* prime stream for inline macros */
135     xdrrec_destroy,     /* destroy stream */
136 #if (defined(KERNEL) && defined(AFS_SUN57_ENV))
137     NULL,
138     xdrrec_getint32,    /* deserialize an afs_int32 */
139     xdrrec_putint32,    /* serialize an afs_int32 */
140 #endif
141 #else
142 #ifdef AFS_XDR_64BITOPS
143     .x_getint64 = NULL,
144     .x_putint64 = NULL,
145 #endif
146     .x_getint32 = xdrrec_getint32,
147     .x_putint32 = xdrrec_putint32,
148     .x_getbytes = xdrrec_getbytes,
149     .x_putbytes = xdrrec_putbytes,
150     .x_getpos = xdrrec_getpos,
151     .x_setpos = xdrrec_setpos,
152     .x_inline = xdrrec_inline,
153     .x_destroy = xdrrec_destroy
154 #endif
155 };
156
157 /*  * Create an xdr handle for xdrrec
158  * xdrrec_create fills in xdrs.  Sendsize and recvsize are
159  * send and recv buffer sizes (0 => use default).
160  * tcp_handle is an opaque handle that is passed as the first parameter to
161  * the procedures readit and writeit.  Readit and writeit are read and
162  * write respectively.   They are like the system
163  * calls expect that they take an opaque handle rather than an fd.
164  */
165 /*
166         int (*readit)();  * like read, but pass it a tcp_handle, not sock *
167         int (*writeit)();  * like write, but pass it a tcp_handle, not sock *
168 */
169 void
170 xdrrec_create(XDR * xdrs, u_int sendsize, u_int recvsize,
171               caddr_t tcp_handle, int (*readit) (caddr_t tcp_handle,
172                                                  caddr_t out_base, int len),
173               int (*writeit) (caddr_t tcp_handle, caddr_t out_base, int len))
174 {
175     RECSTREAM *rstrm = (RECSTREAM *) osi_alloc(sizeof(RECSTREAM));
176
177     if (rstrm == NULL) {
178         /*
179          *  This is bad.  Should rework xdrrec_create to
180          *  return a handle, and in this case return NULL
181          */
182         return;
183     }
184     xdrs->x_ops = &xdrrec_ops;
185     xdrs->x_private = (caddr_t) rstrm;
186     rstrm->tcp_handle = tcp_handle;
187     rstrm->readit = readit;
188     rstrm->writeit = writeit;
189     sendsize = fix_buf_size(sendsize);
190     if ((rstrm->out_base = rstrm->out_finger = rstrm->out_boundry =
191          osi_alloc(sendsize)) == NULL) {
192         return;
193     }
194     rstrm->frag_header = (afs_uint32 *) rstrm->out_base;
195     rstrm->out_finger += sizeof(afs_uint32);
196     rstrm->out_boundry += sendsize;
197     rstrm->frag_sent = FALSE;
198     rstrm->in_size = recvsize = fix_buf_size(recvsize);
199     if ((rstrm->in_base = rstrm->in_boundry = osi_alloc(recvsize)) == NULL) {
200         return;
201     }
202     rstrm->in_finger = (rstrm->in_boundry += recvsize);
203     rstrm->fbtbc = 0;
204     rstrm->last_frag = TRUE;
205     rstrm->sendsize = sendsize;
206     rstrm->recvsize = recvsize;
207 }
208
209
210 /*  * The reoutines defined below are the xdr ops which will go into the
211  * xdr handle filled in by xdrrec_create.
212  */
213
214 static bool_t
215 xdrrec_getint32(XDR * xdrs, afs_int32 * lp)
216 {
217     RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
218     afs_int32 *buflp = (afs_int32 *) (rstrm->in_finger);
219     afs_int32 myint32;
220
221     /* first try the inline, fast case */
222     if ((rstrm->fbtbc >= sizeof(afs_int32))
223         && (((int)((char *)rstrm->in_boundry - (char *)buflp)) >= sizeof(afs_int32))) {
224         *lp = ntohl(*buflp);
225         rstrm->fbtbc -= sizeof(afs_int32);
226         rstrm->in_finger += sizeof(afs_int32);
227     } else {
228         if (!xdrrec_getbytes(xdrs, (caddr_t) & myint32, sizeof(afs_int32)))
229             return (FALSE);
230         *lp = ntohl(myint32);
231     }
232     return (TRUE);
233 }
234
235 static bool_t
236 xdrrec_putint32(XDR * xdrs, afs_int32 * lp)
237 {
238     RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
239     afs_int32 *dest_lp = ((afs_int32 *) (rstrm->out_finger));
240
241     if ((rstrm->out_finger += sizeof(afs_int32)) > rstrm->out_boundry) {
242         /*
243          * this case should almost never happen so the code is
244          * inefficient
245          */
246         rstrm->out_finger -= sizeof(afs_int32);
247         rstrm->frag_sent = TRUE;
248         if (!flush_out(rstrm, FALSE))
249             return (FALSE);
250         dest_lp = ((afs_int32 *) (rstrm->out_finger));
251         rstrm->out_finger += sizeof(afs_int32);
252     }
253     *dest_lp = htonl(*lp);
254     return (TRUE);
255 }
256
257 static bool_t
258 xdrrec_getbytes(XDR * xdrs, caddr_t addr, u_int len)
259 {
260     RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
261     int current;
262
263     while (len > 0) {
264         current = rstrm->fbtbc;
265         if (current == 0) {
266             if (rstrm->last_frag)
267                 return (FALSE);
268             if (!set_input_fragment(rstrm))
269                 return (FALSE);
270             continue;
271         }
272         current = (len < current) ? len : current;
273         if (!get_input_bytes(rstrm, addr, current))
274             return (FALSE);
275         addr += current;
276         rstrm->fbtbc -= current;
277         len -= current;
278     }
279     return (TRUE);
280 }
281
282 static bool_t
283 xdrrec_putbytes(XDR * xdrs, caddr_t addr, u_int len)
284 {
285     RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
286     int current;
287
288     while (len > 0) {
289         current = (u_int) (rstrm->out_boundry - rstrm->out_finger);
290         current = (len < current) ? len : current;
291         memcpy(rstrm->out_finger, addr, current);
292         rstrm->out_finger += current;
293         addr += current;
294         len -= current;
295         if (rstrm->out_finger == rstrm->out_boundry) {
296             rstrm->frag_sent = TRUE;
297             if (!flush_out(rstrm, FALSE))
298                 return (FALSE);
299         }
300     }
301     return (TRUE);
302 }
303
304 static u_int
305 xdrrec_getpos(XDR * xdrs)
306 {
307     RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
308     u_int pos;
309
310     pos = (u_int) lseek((int)rstrm->tcp_handle, 0, 1);
311     if ((int)pos != -1)
312         switch (xdrs->x_op) {
313
314         case XDR_ENCODE:
315             pos += (u_int)(rstrm->out_finger - rstrm->out_base);
316             break;
317
318         case XDR_DECODE:
319             pos -= (u_int)(rstrm->in_boundry - rstrm->in_finger);
320             break;
321
322         default:
323             pos = (u_int) - 1;
324             break;
325         }
326     return (pos);
327 }
328
329 static bool_t
330 xdrrec_setpos(XDR * xdrs, u_int pos)
331 {
332     RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
333     u_int currpos = xdrrec_getpos(xdrs);
334     int delta = currpos - pos;
335     caddr_t newpos;
336
337     if ((int)currpos != -1)
338         switch (xdrs->x_op) {
339
340         case XDR_ENCODE:
341             newpos = rstrm->out_finger - delta;
342             if ((newpos > (caddr_t) (rstrm->frag_header))
343                 && (newpos < rstrm->out_boundry)) {
344                 rstrm->out_finger = newpos;
345                 return (TRUE);
346             }
347             break;
348
349         case XDR_DECODE:
350             newpos = rstrm->in_finger - delta;
351             if ((delta < (int)(rstrm->fbtbc)) && (newpos <= rstrm->in_boundry)
352                 && (newpos >= rstrm->in_base)) {
353                 rstrm->in_finger = newpos;
354                 rstrm->fbtbc -= delta;
355                 return (TRUE);
356             }
357             break;
358         }
359     return (FALSE);
360 }
361
362 static afs_int32 *
363 xdrrec_inline(XDR * xdrs, u_int len)
364 {
365     RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
366     afs_int32 *buf = NULL;
367
368     switch (xdrs->x_op) {
369
370     case XDR_ENCODE:
371         if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
372             buf = (afs_int32 *) rstrm->out_finger;
373             rstrm->out_finger += len;
374         }
375         break;
376
377     case XDR_DECODE:
378         if ((len <= rstrm->fbtbc)
379             && ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
380             buf = (afs_int32 *) rstrm->in_finger;
381             rstrm->fbtbc -= len;
382             rstrm->in_finger += len;
383         }
384         break;
385     }
386     return (buf);
387 }
388
389 static void
390 xdrrec_destroy(XDR * xdrs)
391 {
392     RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
393
394     osi_free(rstrm->out_base, rstrm->sendsize);
395     osi_free(rstrm->in_base, rstrm->recvsize);
396     osi_free((caddr_t) rstrm, sizeof(RECSTREAM));
397 }
398
399
400 /*
401  * Exported routines to manage xdr records
402  */
403
404 /*
405  * Before reading (deserializing from the stream, one should always call
406  * this procedure to guarantee proper record alignment.
407  */
408 bool_t
409 xdrrec_skiprecord(XDR * xdrs)
410 {
411     RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
412
413     while (rstrm->fbtbc > 0 || (!rstrm->last_frag)) {
414         if (!skip_input_bytes(rstrm, rstrm->fbtbc))
415             return (FALSE);
416         rstrm->fbtbc = 0;
417         if ((!rstrm->last_frag) && (!set_input_fragment(rstrm)))
418             return (FALSE);
419     }
420     rstrm->last_frag = FALSE;
421     return (TRUE);
422 }
423
424 /*
425  * Look ahead fuction.
426  * Returns TRUE iff there is no more input in the buffer
427  * after consuming the rest of the current record.
428  */
429 bool_t
430 xdrrec_eof(XDR * xdrs)
431 {
432     RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
433
434     while (rstrm->fbtbc > 0 || (!rstrm->last_frag)) {
435         if (!skip_input_bytes(rstrm, rstrm->fbtbc))
436             return (TRUE);
437         rstrm->fbtbc = 0;
438         if ((!rstrm->last_frag) && (!set_input_fragment(rstrm)))
439             return (TRUE);
440     }
441     if (rstrm->in_finger == rstrm->in_boundry)
442         return (TRUE);
443     return (FALSE);
444 }
445
446 /*
447  * The client must tell the package when an end-of-record has occurred.
448  * The second paraemters tells whether the record should be flushed to the
449  * (output) tcp stream.  (This let's the package support batched or
450  * pipelined procedure calls.)  TRUE => immmediate flush to tcp connection.
451  */
452 bool_t
453 xdrrec_endofrecord(XDR * xdrs, bool_t sendnow)
454 {
455     RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
456     afs_uint32 len;     /* fragment length */
457
458     if (sendnow || rstrm->frag_sent
459         || ((afs_uint32) (rstrm->out_finger + sizeof(afs_uint32)) >=
460             (afs_uint32) rstrm->out_boundry)) {
461         rstrm->frag_sent = FALSE;
462         return (flush_out(rstrm, TRUE));
463     }
464     len =
465         (afs_uint32) (rstrm->out_finger - (caddr_t)rstrm->frag_header) -
466         sizeof(afs_uint32);
467     *(rstrm->frag_header) = htonl(len | LAST_FRAG);
468     rstrm->frag_header = (afs_uint32 *) rstrm->out_finger;
469     rstrm->out_finger += sizeof(afs_uint32);
470     return (TRUE);
471 }
472
473
474 /*
475  * Internal useful routines
476  */
477 static bool_t
478 flush_out(RECSTREAM * rstrm, bool_t eor)
479 {
480     afs_uint32 eormask = (eor == TRUE) ? LAST_FRAG : 0;
481     afs_uint32 len =
482         (afs_uint32) (rstrm->out_finger - (caddr_t)rstrm->frag_header) -
483         sizeof(afs_uint32);
484
485     *(rstrm->frag_header) = htonl(len | eormask);
486     len = (afs_uint32) (rstrm->out_finger) - (afs_uint32) (rstrm->out_base);
487     if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int)len)
488         != (int)len)
489         return (FALSE);
490     rstrm->frag_header = (afs_uint32 *) rstrm->out_base;
491     rstrm->out_finger = (caddr_t) rstrm->out_base + sizeof(afs_uint32);
492     return (TRUE);
493 }
494
495 static bool_t
496 fill_input_buf(RECSTREAM * rstrm)
497 {
498     caddr_t where = rstrm->in_base;
499     int len = rstrm->in_size;
500     u_int adjust = (u_int) ((size_t)rstrm->in_boundry % BYTES_PER_XDR_UNIT);
501
502     /* Bump the current position out to the next alignment boundary */
503     where += adjust;
504     len -= adjust;
505
506     if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
507         return (FALSE);
508     rstrm->in_finger = where;
509     where += len;
510     rstrm->in_boundry = where;
511     return (TRUE);
512 }
513
514 static bool_t
515 get_input_bytes(RECSTREAM * rstrm, caddr_t addr,
516                 int len)
517 {
518     int current;
519
520     while (len > 0) {
521         current = (int)(rstrm->in_boundry - rstrm->in_finger);
522         if (current == 0) {
523             if (!fill_input_buf(rstrm))
524                 return (FALSE);
525             continue;
526         }
527         current = (len < current) ? len : current;
528         memcpy(addr, rstrm->in_finger, current);
529         rstrm->in_finger += current;
530         addr += current;
531         len -= current;
532     }
533     return (TRUE);
534 }
535
536 /* next two bytes of the input stream are treated as a header */
537 static bool_t
538 set_input_fragment(RECSTREAM * rstrm)
539 {
540     afs_uint32 header;
541
542     if (!get_input_bytes(rstrm, (caddr_t) & header, sizeof(header)))
543         return (FALSE);
544     header = ntohl(header);
545     rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
546     rstrm->fbtbc = header & (~LAST_FRAG);
547     return (TRUE);
548 }
549
550 /* consumes input bytes; knows nothing about records! */
551 static bool_t
552 skip_input_bytes(RECSTREAM * rstrm, int cnt)
553 {
554     int current;
555
556     while (cnt > 0) {
557         current = (int)(rstrm->in_boundry - rstrm->in_finger);
558         if (current == 0) {
559             if (!fill_input_buf(rstrm))
560                 return (FALSE);
561             continue;
562         }
563         current = (cnt < current) ? cnt : current;
564         rstrm->in_finger += current;
565         cnt -= current;
566     }
567     return (TRUE);
568 }
569
570 static u_int
571 fix_buf_size(u_int s)
572 {
573
574     if (s < 100)
575         s = 4000;
576     return ((s + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT)
577         * BYTES_PER_XDR_UNIT;
578
579 }
580
581 #endif /* NeXT */