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