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