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