Fix static linking / lack of htonl problem. Release 4.0.10.
[portablexdr.git] / xdr_rec.c
1 /* @(#)xdr_rec.c        2.2 88/08/01 4.0 RPCSRC */
2 /*
3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4  * unrestricted use provided that this legend is included on all tape
5  * media and as a part of the software program in whole or part.  Users
6  * may copy or modify Sun RPC without charge, but are not authorized
7  * to license or distribute it to anyone else except as part of a product or
8  * program developed by the user.
9  * 
10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13  * 
14  * Sun RPC is provided with no support and without any obligation on the
15  * part of Sun Microsystems, Inc. to assist in its use, correction,
16  * modification or enhancement.
17  * 
18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20  * OR ANY PART THEREOF.
21  * 
22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23  * or profits or other special, indirect and consequential damages, even if
24  * Sun has been advised of the possibility of such damages.
25  * 
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
32 #endif
33
34 /*
35  * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
36  * layer above tcp (for rpc's use).
37  *
38  * Copyright (C) 1984, Sun Microsystems, Inc.
39  *
40  * These routines interface XDRSTREAMS to a tcp/ip connection.
41  * There is a record marking layer between the xdr stream
42  * and the tcp transport level.  A record is composed on one or more
43  * record fragments.  A record fragment is a thirty-two bit header followed
44  * by n bytes of data, where n is contained in the header.  The header
45  * is represented as a htonl(u_long).  Thegh order bit encodes
46  * whether or not the fragment is the last fragment of the record
47  * (1 => fragment is last, 0 => more fragments to follow. 
48  * The other 31 bits encode the byte length of the fragment.
49  */
50
51 #include <config.h>
52
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56
57 #ifdef HAVE_ARPA_INET_H
58 #include <arpa/inet.h>
59 #endif
60
61 #include <rpc/types.h>
62 #include <rpc/xdr.h>
63
64 #include "byteswap.h"
65
66 extern long     lseek();
67
68 static u_int    fix_buf_size();
69
70 static bool_t   xdrrec_getlong();
71 static bool_t   xdrrec_putlong();
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 long *   xdrrec_inline();
77 static void     xdrrec_destroy();
78
79 static struct  xdr_ops xdrrec_ops = {
80         xdrrec_getlong,
81         xdrrec_putlong,
82         xdrrec_getbytes,
83         xdrrec_putbytes,
84         xdrrec_getpos,
85         xdrrec_setpos,
86         xdrrec_inline,
87         xdrrec_destroy
88 };
89
90 /*
91  * A record is composed of one or more record fragments.
92  * A record fragment is a two-byte header followed by zero to
93  * 2**32-1 bytes.  The header is treated as a long unsigned and is
94  * encode/decoded to the network via htonl/ntohl.  The low order 31 bits
95  * are a byte count of the fragment.  The highest order bit is a boolean:
96  * 1 => this fragment is the last fragment of the record,
97  * 0 => this fragment is followed by more fragment(s).
98  *
99  * The fragment/record machinery is not general;  it is constructed to
100  * meet the needs of xdr and rpc based on tcp.
101  */
102
103 #define LAST_FRAG ((u_long)(1 << 31))
104
105 typedef struct rec_strm {
106         caddr_t tcp_handle;
107         caddr_t the_buffer;
108         /*
109          * out-goung bits
110          */
111         int (*writeit)();
112         caddr_t out_base;       /* output buffer (points to frag header) */
113         caddr_t out_finger;     /* next output position */
114         caddr_t out_boundry;    /* data cannot up to this address */
115         u_long *frag_header;    /* beginning of curren fragment */
116         bool_t frag_sent;       /* true if buffer sent in middle of record */
117         /*
118          * in-coming bits
119          */
120         int (*readit)();
121         u_long in_size; /* fixed size of the input buffer */
122         caddr_t in_base;
123         caddr_t in_finger;      /* location of next byte to be had */
124         caddr_t in_boundry;     /* can read up to this location */
125         long fbtbc;             /* fragment bytes to be consumed */
126         bool_t last_frag;
127         u_int sendsize;
128         u_int recvsize;
129 } RECSTREAM;
130
131 static bool_t flush_out(register RECSTREAM *, bool_t );
132 static bool_t get_input_bytes(register RECSTREAM *, register caddr_t, register int );
133 static bool_t set_input_fragment(register RECSTREAM *);
134 static bool_t skip_input_bytes(register RECSTREAM *, long);
135
136 /*
137  * Create an xdr handle for xdrrec
138  * xdrrec_create fills in xdrs.  Sendsize and recvsize are
139  * send and recv buffer sizes (0 => use default).
140  * tcp_handle is an opaque handle that is passed as the first parameter to
141  * the procedures readit and writeit.  Readit and writeit are read and
142  * write respectively.   They are like the system
143  * calls expect that they take an opaque handle rather than an fd.
144  */
145 void
146 xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
147         register XDR *xdrs;
148         register u_int sendsize;
149         register u_int recvsize;
150         caddr_t tcp_handle;
151         int (*readit)();  /* like read, but pass it a tcp_handle, not sock */
152         int (*writeit)();  /* like write, but pass it a tcp_handle, not sock */
153 {
154         register RECSTREAM *rstrm =
155                 (RECSTREAM *)mem_alloc(sizeof(RECSTREAM));
156
157         if (rstrm == NULL) {
158                 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
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         /*
166          * adjust sizes and allocate buffer quad byte aligned
167          */
168         rstrm->sendsize = sendsize = fix_buf_size(sendsize);
169         rstrm->recvsize = recvsize = fix_buf_size(recvsize);
170         rstrm->the_buffer = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
171         if (rstrm->the_buffer == NULL) {
172                 (void)fprintf(stderr, "xdrrec_create: out of memory\n");
173                 return;
174         }
175         for (rstrm->out_base = rstrm->the_buffer;
176              (long) rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
177                 rstrm->out_base++);
178         rstrm->in_base = rstrm->out_base + sendsize;
179         /*
180          * now the rest ...
181          */
182         xdrs->x_ops = &xdrrec_ops;
183         xdrs->x_private = (caddr_t)rstrm;
184         rstrm->tcp_handle = tcp_handle;
185         rstrm->readit = readit;
186         rstrm->writeit = writeit;
187         rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
188         rstrm->frag_header = (u_long *)rstrm->out_base;
189         rstrm->out_finger += sizeof(u_long);
190         rstrm->out_boundry += sendsize;
191         rstrm->frag_sent = FALSE;
192         rstrm->in_size = recvsize;
193         rstrm->in_boundry = rstrm->in_base;
194         rstrm->in_finger = (rstrm->in_boundry += recvsize);
195         rstrm->fbtbc = 0;
196         rstrm->last_frag = TRUE;
197 }
198
199
200 /*
201  * The reoutines defined below are the xdr ops which will go into the
202  * xdr handle filled in by xdrrec_create.
203  */
204
205 static bool_t
206 xdrrec_getlong(xdrs, lp)
207         XDR *xdrs;
208         long *lp;
209 {
210         register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
211         register long *buflp = (long *)(rstrm->in_finger);
212         long mylong;
213
214         /* first try the inline, fast case */
215         if ((rstrm->fbtbc >= sizeof(long)) &&
216             ((rstrm->in_boundry - (caddr_t) buflp) >= sizeof(long))) {
217                 *lp = (long)ntohl((u_long)(*buflp));
218                 rstrm->fbtbc -= sizeof(long);
219                 rstrm->in_finger += sizeof(long);
220         } else {
221                 if (! xdrrec_getbytes(xdrs, (caddr_t)&mylong, sizeof(long)))
222                         return (FALSE);
223                 *lp = (long)ntohl((u_long)mylong);
224         }
225         return (TRUE);
226 }
227
228 static bool_t
229 xdrrec_putlong(xdrs, lp)
230         XDR *xdrs;
231         long *lp;
232 {
233         register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
234         register long *dest_lp = ((long *)(rstrm->out_finger));
235
236         if ((rstrm->out_finger += sizeof(long)) > rstrm->out_boundry) {
237                 /*
238                  * this case should almost never happen so the code is
239                  * inefficient
240                  */
241                 rstrm->out_finger -= sizeof(long);
242                 rstrm->frag_sent = TRUE;
243                 if (! flush_out(rstrm, FALSE))
244                         return (FALSE);
245                 dest_lp = ((long *)(rstrm->out_finger));
246                 rstrm->out_finger += sizeof(long);
247         }
248         *dest_lp = (long)htonl((u_long)(*lp));
249         return (TRUE);
250 }
251
252 static bool_t  /* must manage buffers, fragments, and records */
253 xdrrec_getbytes(xdrs, addr, len)
254         XDR *xdrs;
255         register caddr_t addr;
256         register u_int len;
257 {
258         register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
259         register int current;
260
261         while (len > 0) {
262                 current = rstrm->fbtbc;
263                 if (current == 0) {
264                         if (rstrm->last_frag)
265                                 return (FALSE);
266                         if (! set_input_fragment(rstrm))
267                                 return (FALSE);
268                         continue;
269                 }
270                 current = (len < current) ? len : current;
271                 if (! get_input_bytes(rstrm, addr, current))
272                         return (FALSE);
273                 addr += current; 
274                 rstrm->fbtbc -= current;
275                 len -= current;
276         }
277         return (TRUE);
278 }
279
280 static bool_t
281 xdrrec_putbytes(xdrs, addr, len)
282         XDR *xdrs;
283         register caddr_t addr;
284         register u_int len;
285 {
286         register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
287         register int current;
288
289         while (len > 0) {
290                 current = rstrm->out_boundry - rstrm->out_finger;
291                 current = (len < current) ? len : current;
292                 memcpy (rstrm->out_finger, addr, current);
293                 rstrm->out_finger += current;
294                 addr += current;
295                 len -= current;
296                 if (rstrm->out_finger == rstrm->out_boundry) {
297                         rstrm->frag_sent = TRUE;
298                         if (! flush_out(rstrm, FALSE))
299                                 return (FALSE);
300                 }
301         }
302         return (TRUE);
303 }
304
305 static u_int
306 xdrrec_getpos(xdrs)
307         register XDR *xdrs;
308 {
309         register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
310         register long pos;
311
312         pos = lseek(rstrm->tcp_handle, (long) 0, 1);
313         if (pos != -1)
314                 switch (xdrs->x_op) {
315
316                 case XDR_ENCODE:
317                         pos += rstrm->out_finger - rstrm->out_base;
318                         break;
319
320                 case XDR_DECODE:
321                         pos -= rstrm->in_boundry - rstrm->in_finger;
322                         break;
323
324                 default:
325                         pos = (u_int) -1;
326                         break;
327                 }
328         return ((u_int) pos);
329 }
330
331 static bool_t
332 xdrrec_setpos(xdrs, pos)
333         register XDR *xdrs;
334         u_int pos;
335 {
336         register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
337         u_int currpos = xdrrec_getpos(xdrs);
338         int delta = currpos - pos;
339         caddr_t newpos;
340
341         if ((int)currpos != -1)
342                 switch (xdrs->x_op) {
343
344                 case XDR_FREE: break; /* keep gcc happy */
345
346                 case XDR_ENCODE:
347                         newpos = rstrm->out_finger - delta;
348                         if ((newpos > (caddr_t)(rstrm->frag_header)) &&
349                                 (newpos < rstrm->out_boundry)) {
350                                 rstrm->out_finger = newpos;
351                                 return (TRUE);
352                         }
353                         break;
354
355                 case XDR_DECODE:
356                         newpos = rstrm->in_finger - delta;
357                         if ((delta < (int)(rstrm->fbtbc)) &&
358                                 (newpos <= rstrm->in_boundry) &&
359                                 (newpos >= rstrm->in_base)) {
360                                 rstrm->in_finger = newpos;
361                                 rstrm->fbtbc -= delta;
362                                 return (TRUE);
363                         }
364                         break;
365                 }
366         return (FALSE);
367 }
368
369 static long *
370 xdrrec_inline(xdrs, len)
371         register XDR *xdrs;
372         int len;
373 {
374         register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
375         long * buf = NULL;
376
377         switch (xdrs->x_op) {
378
379         case XDR_FREE: break; /* keep gcc happy */
380
381         case XDR_ENCODE:
382                 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
383                         buf = (long *) rstrm->out_finger;
384                         rstrm->out_finger += len;
385                 }
386                 break;
387
388         case XDR_DECODE:
389                 if ((len <= rstrm->fbtbc) &&
390                         ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
391                         buf = (long *) rstrm->in_finger;
392                         rstrm->fbtbc -= len;
393                         rstrm->in_finger += len;
394                 }
395                 break;
396         }
397         return (buf);
398 }
399
400 static void
401 xdrrec_destroy(xdrs)
402         register XDR *xdrs;
403 {
404         register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
405
406         mem_free(rstrm->the_buffer,
407                 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
408         mem_free((caddr_t)rstrm, sizeof(RECSTREAM));
409 }
410
411
412 /*
413  * Exported routines to manage xdr records
414  */
415
416 /*
417  * Before reading (deserializing from the stream, one should always call
418  * this procedure to guarantee proper record alignment.
419  */
420 bool_t
421 xdrrec_skiprecord(xdrs)
422         XDR *xdrs;
423 {
424         register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
425
426         while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
427                 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
428                         return (FALSE);
429                 rstrm->fbtbc = 0;
430                 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
431                         return (FALSE);
432         }
433         rstrm->last_frag = FALSE;
434         return (TRUE);
435 }
436
437 /*
438  * Look ahead fuction.
439  * Returns TRUE iff there is no more input in the buffer 
440  * after consuming the rest of the current record.
441  */
442 bool_t
443 xdrrec_eof(xdrs)
444         XDR *xdrs;
445 {
446         register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
447
448         while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
449                 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
450                         return (TRUE);
451                 rstrm->fbtbc = 0;
452                 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
453                         return (TRUE);
454         }
455         if (rstrm->in_finger == rstrm->in_boundry)
456                 return (TRUE);
457         return (FALSE);
458 }
459
460 /*
461  * The client must tell the package when an end-of-record has occurred.
462  * The second paraemters tells whether the record should be flushed to the
463  * (output) tcp stream.  (This let's the package support batched or
464  * pipelined procedure calls.)  TRUE => immmediate flush to tcp connection.
465  */
466 bool_t
467 xdrrec_endofrecord(xdrs, sendnow)
468         XDR *xdrs;
469         bool_t sendnow;
470 {
471         register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
472         register u_long len;  /* fragment length */
473
474         if (sendnow || rstrm->frag_sent ||
475                 ((u_long)rstrm->out_finger + sizeof(u_long) >=
476                 (u_long)rstrm->out_boundry)) {
477                 rstrm->frag_sent = FALSE;
478                 return (flush_out(rstrm, TRUE));
479         }
480         len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
481            sizeof(u_long);
482         *(rstrm->frag_header) = htonl((u_long)len | LAST_FRAG);
483         rstrm->frag_header = (u_long *)rstrm->out_finger;
484         rstrm->out_finger += sizeof(u_long);
485         return (TRUE);
486 }
487
488
489 /*
490  * Internal useful routines
491  */
492 static bool_t
493 flush_out(rstrm, eor)
494         register RECSTREAM *rstrm;
495         bool_t eor;
496 {
497         register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
498         register u_long len = (u_long)(rstrm->out_finger) - 
499                 (u_long)(rstrm->frag_header) - sizeof(u_long);
500
501         *(rstrm->frag_header) = htonl(len | eormask);
502         len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->out_base);
503         if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
504                 != (int)len)
505                 return (FALSE);
506         rstrm->frag_header = (u_long *)rstrm->out_base;
507         rstrm->out_finger = (caddr_t)rstrm->out_base + sizeof(u_long);
508         return (TRUE);
509 }
510
511 static bool_t  /* knows nothing about records!  Only about input buffers */
512 fill_input_buf(rstrm)
513         register RECSTREAM *rstrm;
514 {
515         register caddr_t where;
516         u_int i;
517         register int len;
518
519         where = rstrm->in_base;
520         i = (long) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
521         where += i;
522         len = rstrm->in_size - i;
523         if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
524                 return (FALSE);
525         rstrm->in_finger = where;
526         where += len;
527         rstrm->in_boundry = where;
528         return (TRUE);
529 }
530
531 static bool_t  /* knows nothing about records!  Only about input buffers */
532 get_input_bytes(rstrm, addr, len)
533         register RECSTREAM *rstrm;
534         register caddr_t addr;
535         register int len;
536 {
537         register int current;
538
539         while (len > 0) {
540                 current = rstrm->in_boundry - rstrm->in_finger;
541                 if (current == 0) {
542                         if (! fill_input_buf(rstrm))
543                                 return (FALSE);
544                         continue;
545                 }
546                 current = (len < current) ? len : current;
547                 memcpy (addr, rstrm->in_finger, current);
548                 rstrm->in_finger += current;
549                 addr += current;
550                 len -= current;
551         }
552         return (TRUE);
553 }
554
555 static bool_t  /* next two bytes of the input stream are treated as a header */
556 set_input_fragment(rstrm)
557         register RECSTREAM *rstrm;
558 {
559         u_long header;
560
561         if (! get_input_bytes(rstrm, (caddr_t)&header, sizeof(header)))
562                 return (FALSE);
563         header = (long)ntohl(header);
564         rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
565         rstrm->fbtbc = header & (~LAST_FRAG);
566         return (TRUE);
567 }
568
569 static bool_t  /* consumes input bytes; knows nothing about records! */
570 skip_input_bytes(rstrm, cnt)
571         register RECSTREAM *rstrm;
572         long cnt;
573 {
574         register int current;
575
576         while (cnt > 0) {
577                 current = rstrm->in_boundry - rstrm->in_finger;
578                 if (current == 0) {
579                         if (! fill_input_buf(rstrm))
580                                 return (FALSE);
581                         continue;
582                 }
583                 current = (cnt < current) ? cnt : current;
584                 rstrm->in_finger += current;
585                 cnt -= current;
586         }
587         return (TRUE);
588 }
589
590 static u_int
591 fix_buf_size(s)
592         register u_int s;
593 {
594
595         if (s < 100)
596                 s = 4000;
597         return (RNDUP(s));
598 }