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