885e6beed41f60353051cc775fbf8bb0724517eb
[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     /* Windows does not support labeled assignments */
122     xdrrec_getint32,    /* deserialize an afs_int32 */
123     xdrrec_putint32,    /* serialize an afs_int32 */
124     xdrrec_getbytes,    /* deserialize counted bytes */
125     xdrrec_putbytes,    /* serialize counted bytes */
126     xdrrec_getpos,      /* get offset in the stream: not supported. */
127     xdrrec_setpos,      /* set offset in the stream: not supported. */
128     xdrrec_inline,      /* prime stream for inline macros */
129     xdrrec_destroy      /* destroy stream */
130 #else
131     .x_getint32 = xdrrec_getint32,
132     .x_putint32 = xdrrec_putint32,
133     .x_getbytes = xdrrec_getbytes,
134     .x_putbytes = xdrrec_putbytes,
135     .x_getpos = xdrrec_getpos,
136     .x_setpos = xdrrec_setpos,
137     .x_inline = xdrrec_inline,
138     .x_destroy = xdrrec_destroy
139 #endif
140 };
141
142 /*  * Create an xdr handle for xdrrec
143  * xdrrec_create fills in xdrs.  Sendsize and recvsize are
144  * send and recv buffer sizes (0 => use default).
145  * tcp_handle is an opaque handle that is passed as the first parameter to
146  * the procedures readit and writeit.  Readit and writeit are read and
147  * write respectively.   They are like the system
148  * calls expect that they take an opaque handle rather than an fd.
149  */
150 /*
151         int (*readit)();  * like read, but pass it a tcp_handle, not sock *
152         int (*writeit)();  * like write, but pass it a tcp_handle, not sock *
153 */
154 void
155 xdrrec_create(XDR * xdrs, u_int sendsize, u_int recvsize,
156               caddr_t tcp_handle, int (*readit) (caddr_t tcp_handle,
157                                                  caddr_t out_base, int len),
158               int (*writeit) (caddr_t tcp_handle, caddr_t out_base, int len))
159 {
160     RECSTREAM *rstrm = (RECSTREAM *) osi_alloc(sizeof(RECSTREAM));
161
162     if (rstrm == NULL) {
163         /* 
164          *  This is bad.  Should rework xdrrec_create to 
165          *  return a handle, and in this case return NULL
166          */
167         return;
168     }
169     xdrs->x_ops = &xdrrec_ops;
170     xdrs->x_private = (caddr_t) rstrm;
171     rstrm->tcp_handle = tcp_handle;
172     rstrm->readit = readit;
173     rstrm->writeit = writeit;
174     sendsize = fix_buf_size(sendsize);
175     if ((rstrm->out_base = rstrm->out_finger = rstrm->out_boundry =
176          osi_alloc(sendsize)) == NULL) {
177         return;
178     }
179     rstrm->frag_header = (afs_uint32 *) rstrm->out_base;
180     rstrm->out_finger += sizeof(afs_uint32);
181     rstrm->out_boundry += sendsize;
182     rstrm->frag_sent = FALSE;
183     rstrm->in_size = recvsize = fix_buf_size(recvsize);
184     if ((rstrm->in_base = rstrm->in_boundry = osi_alloc(recvsize)) == NULL) {
185         return;
186     }
187     rstrm->in_finger = (rstrm->in_boundry += recvsize);
188     rstrm->fbtbc = 0;
189     rstrm->last_frag = TRUE;
190     rstrm->sendsize = sendsize;
191     rstrm->recvsize = recvsize;
192 }
193
194
195 /*  * The reoutines defined below are the xdr ops which will go into the
196  * xdr handle filled in by xdrrec_create.
197  */
198
199 static bool_t
200 xdrrec_getint32(XDR * xdrs, afs_int32 * lp)
201 {
202     RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
203     afs_int32 *buflp = (afs_int32 *) (rstrm->in_finger);
204     afs_int32 myint32;
205
206     /* first try the inline, fast case */
207     if ((rstrm->fbtbc >= sizeof(afs_int32))
208         && (((int)((char *)rstrm->in_boundry - (char *)buflp)) >= sizeof(afs_int32))) {
209         *lp = ntohl(*buflp);
210         rstrm->fbtbc -= sizeof(afs_int32);
211         rstrm->in_finger += sizeof(afs_int32);
212     } else {
213         if (!xdrrec_getbytes(xdrs, (caddr_t) & myint32, sizeof(afs_int32)))
214             return (FALSE);
215         *lp = ntohl(myint32);
216     }
217     return (TRUE);
218 }
219
220 static bool_t
221 xdrrec_putint32(XDR * xdrs, afs_int32 * lp)
222 {
223     RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
224     afs_int32 *dest_lp = ((afs_int32 *) (rstrm->out_finger));
225
226     if ((rstrm->out_finger += sizeof(afs_int32)) > rstrm->out_boundry) {
227         /*
228          * this case should almost never happen so the code is
229          * inefficient
230          */
231         rstrm->out_finger -= sizeof(afs_int32);
232         rstrm->frag_sent = TRUE;
233         if (!flush_out(rstrm, FALSE))
234             return (FALSE);
235         dest_lp = ((afs_int32 *) (rstrm->out_finger));
236         rstrm->out_finger += sizeof(afs_int32);
237     }
238     *dest_lp = htonl(*lp);
239     return (TRUE);
240 }
241
242 static bool_t
243 xdrrec_getbytes(XDR * xdrs, caddr_t addr, u_int len)
244 {
245     RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
246     int current;
247
248     while (len > 0) {
249         current = rstrm->fbtbc;
250         if (current == 0) {
251             if (rstrm->last_frag)
252                 return (FALSE);
253             if (!set_input_fragment(rstrm))
254                 return (FALSE);
255             continue;
256         }
257         current = (len < current) ? len : current;
258         if (!get_input_bytes(rstrm, addr, current))
259             return (FALSE);
260         addr += current;
261         rstrm->fbtbc -= current;
262         len -= current;
263     }
264     return (TRUE);
265 }
266
267 static bool_t
268 xdrrec_putbytes(XDR * xdrs, caddr_t addr, u_int len)
269 {
270     RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
271     int current;
272
273     while (len > 0) {
274         current = (u_int) (rstrm->out_boundry - rstrm->out_finger);
275         current = (len < current) ? len : current;
276         memcpy(rstrm->out_finger, addr, current);
277         rstrm->out_finger += current;
278         addr += current;
279         len -= current;
280         if (rstrm->out_finger == rstrm->out_boundry) {
281             rstrm->frag_sent = TRUE;
282             if (!flush_out(rstrm, FALSE))
283                 return (FALSE);
284         }
285     }
286     return (TRUE);
287 }
288
289 static u_int
290 xdrrec_getpos(XDR * xdrs)
291 {
292     RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
293     u_int pos;
294
295     pos = (u_int) lseek((int)rstrm->tcp_handle, 0, 1);
296     if ((int)pos != -1)
297         switch (xdrs->x_op) {
298
299         case XDR_ENCODE:
300             pos += (u_int)(rstrm->out_finger - rstrm->out_base);
301             break;
302
303         case XDR_DECODE:
304             pos -= (u_int)(rstrm->in_boundry - rstrm->in_finger);
305             break;
306
307         default:
308             pos = (u_int) - 1;
309             break;
310         }
311     return (pos);
312 }
313
314 static bool_t
315 xdrrec_setpos(XDR * xdrs, u_int pos)
316 {
317     RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
318     u_int currpos = xdrrec_getpos(xdrs);
319     int delta = currpos - pos;
320     caddr_t newpos;
321
322     if ((int)currpos != -1)
323         switch (xdrs->x_op) {
324
325         case XDR_ENCODE:
326             newpos = rstrm->out_finger - delta;
327             if ((newpos > (caddr_t) (rstrm->frag_header))
328                 && (newpos < rstrm->out_boundry)) {
329                 rstrm->out_finger = newpos;
330                 return (TRUE);
331             }
332             break;
333
334         case XDR_DECODE:
335             newpos = rstrm->in_finger - delta;
336             if ((delta < (int)(rstrm->fbtbc)) && (newpos <= rstrm->in_boundry)
337                 && (newpos >= rstrm->in_base)) {
338                 rstrm->in_finger = newpos;
339                 rstrm->fbtbc -= delta;
340                 return (TRUE);
341             }
342             break;
343         }
344     return (FALSE);
345 }
346
347 static afs_int32 *
348 xdrrec_inline(XDR * xdrs, u_int len)
349 {
350     RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
351     afs_int32 *buf = NULL;
352
353     switch (xdrs->x_op) {
354
355     case XDR_ENCODE:
356         if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
357             buf = (afs_int32 *) rstrm->out_finger;
358             rstrm->out_finger += len;
359         }
360         break;
361
362     case XDR_DECODE:
363         if ((len <= rstrm->fbtbc)
364             && ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
365             buf = (afs_int32 *) rstrm->in_finger;
366             rstrm->fbtbc -= len;
367             rstrm->in_finger += len;
368         }
369         break;
370     }
371     return (buf);
372 }
373
374 static void
375 xdrrec_destroy(XDR * xdrs)
376 {
377     RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
378
379     osi_free(rstrm->out_base, rstrm->sendsize);
380     osi_free(rstrm->in_base, rstrm->recvsize);
381     osi_free((caddr_t) rstrm, sizeof(RECSTREAM));
382 }
383
384
385 /*
386  * Exported routines to manage xdr records
387  */
388
389 /*
390  * Before reading (deserializing from the stream, one should always call
391  * this procedure to guarantee proper record alignment.
392  */
393 bool_t
394 xdrrec_skiprecord(XDR * xdrs)
395 {
396     RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
397
398     while (rstrm->fbtbc > 0 || (!rstrm->last_frag)) {
399         if (!skip_input_bytes(rstrm, rstrm->fbtbc))
400             return (FALSE);
401         rstrm->fbtbc = 0;
402         if ((!rstrm->last_frag) && (!set_input_fragment(rstrm)))
403             return (FALSE);
404     }
405     rstrm->last_frag = FALSE;
406     return (TRUE);
407 }
408
409 /*
410  * Look ahead fuction.
411  * Returns TRUE iff there is no more input in the buffer 
412  * after consuming the rest of the current record.
413  */
414 bool_t
415 xdrrec_eof(XDR * xdrs)
416 {
417     RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
418
419     while (rstrm->fbtbc > 0 || (!rstrm->last_frag)) {
420         if (!skip_input_bytes(rstrm, rstrm->fbtbc))
421             return (TRUE);
422         rstrm->fbtbc = 0;
423         if ((!rstrm->last_frag) && (!set_input_fragment(rstrm)))
424             return (TRUE);
425     }
426     if (rstrm->in_finger == rstrm->in_boundry)
427         return (TRUE);
428     return (FALSE);
429 }
430
431 /*
432  * The client must tell the package when an end-of-record has occurred.
433  * The second paraemters tells whether the record should be flushed to the
434  * (output) tcp stream.  (This let's the package support batched or
435  * pipelined procedure calls.)  TRUE => immmediate flush to tcp connection.
436  */
437 bool_t
438 xdrrec_endofrecord(XDR * xdrs, bool_t sendnow)
439 {
440     RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
441     afs_uint32 len;     /* fragment length */
442
443     if (sendnow || rstrm->frag_sent
444         || ((afs_uint32) (rstrm->out_finger + sizeof(afs_uint32)) >=
445             (afs_uint32) rstrm->out_boundry)) {
446         rstrm->frag_sent = FALSE;
447         return (flush_out(rstrm, TRUE));
448     }
449     len =
450         (afs_uint32) (rstrm->out_finger - (caddr_t)rstrm->frag_header) -
451         sizeof(afs_uint32);
452     *(rstrm->frag_header) = htonl(len | LAST_FRAG);
453     rstrm->frag_header = (afs_uint32 *) rstrm->out_finger;
454     rstrm->out_finger += sizeof(afs_uint32);
455     return (TRUE);
456 }
457
458
459 /*
460  * Internal useful routines
461  */
462 static bool_t
463 flush_out(RECSTREAM * rstrm, bool_t eor)
464 {
465     afs_uint32 eormask = (eor == TRUE) ? LAST_FRAG : 0;
466     afs_uint32 len =
467         (afs_uint32) (rstrm->out_finger - (caddr_t)rstrm->frag_header) -
468         sizeof(afs_uint32);
469
470     *(rstrm->frag_header) = htonl(len | eormask);
471     len = (afs_uint32) (rstrm->out_finger) - (afs_uint32) (rstrm->out_base);
472     if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int)len)
473         != (int)len)
474         return (FALSE);
475     rstrm->frag_header = (afs_uint32 *) rstrm->out_base;
476     rstrm->out_finger = (caddr_t) rstrm->out_base + sizeof(afs_uint32);
477     return (TRUE);
478 }
479
480 static bool_t
481 fill_input_buf(RECSTREAM * rstrm)
482 {
483     caddr_t where = rstrm->in_base;
484     int len = rstrm->in_size;
485     u_int adjust = (u_int) ((size_t)rstrm->in_boundry % BYTES_PER_XDR_UNIT);
486
487     /* Bump the current position out to the next alignment boundary */
488     where += adjust;
489     len -= adjust;
490
491     if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
492         return (FALSE);
493     rstrm->in_finger = where;
494     where += len;
495     rstrm->in_boundry = where;
496     return (TRUE);
497 }
498
499 static bool_t
500 get_input_bytes(RECSTREAM * rstrm, caddr_t addr,
501                 int len)
502 {
503     int current;
504
505     while (len > 0) {
506         current = (int)(rstrm->in_boundry - rstrm->in_finger);
507         if (current == 0) {
508             if (!fill_input_buf(rstrm))
509                 return (FALSE);
510             continue;
511         }
512         current = (len < current) ? len : current;
513         memcpy(addr, rstrm->in_finger, current);
514         rstrm->in_finger += current;
515         addr += current;
516         len -= current;
517     }
518     return (TRUE);
519 }
520
521 /* next two bytes of the input stream are treated as a header */
522 static bool_t
523 set_input_fragment(RECSTREAM * rstrm)
524 {
525     afs_uint32 header;
526
527     if (!get_input_bytes(rstrm, (caddr_t) & header, sizeof(header)))
528         return (FALSE);
529     header = ntohl(header);
530     rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
531     rstrm->fbtbc = header & (~LAST_FRAG);
532     return (TRUE);
533 }
534
535 /* consumes input bytes; knows nothing about records! */
536 static bool_t
537 skip_input_bytes(RECSTREAM * rstrm, int cnt)
538 {
539     int current;
540
541     while (cnt > 0) {
542         current = (int)(rstrm->in_boundry - rstrm->in_finger);
543         if (current == 0) {
544             if (!fill_input_buf(rstrm))
545                 return (FALSE);
546             continue;
547         }
548         current = (cnt < current) ? cnt : current;
549         rstrm->in_finger += current;
550         cnt -= current;
551     }
552     return (TRUE);
553 }
554
555 static u_int
556 fix_buf_size(u_int s)
557 {
558
559     if (s < 100)
560         s = 4000;
561     return ((s + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT)
562         * BYTES_PER_XDR_UNIT;
563
564 }
565
566 #endif /* NeXT */