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