OSDN Git Service

Bug ugly formatting update
[uclinux-h8/uClibc.git] / libc / inet / rpc / 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 <stdio.h>
52 #include <rpc/types.h>
53 #include <rpc/xdr.h>
54 #include <netinet/in.h>
55
56 extern long lseek();
57
58 static u_int fix_buf_size();
59
60 static bool_t xdrrec_getlong();
61 static bool_t xdrrec_putlong();
62 static bool_t xdrrec_getbytes();
63 static bool_t xdrrec_putbytes();
64 static u_int xdrrec_getpos();
65 static bool_t xdrrec_setpos();
66 static long *xdrrec_inline();
67 static void xdrrec_destroy();
68
69 static struct xdr_ops xdrrec_ops = {
70         xdrrec_getlong,
71         xdrrec_putlong,
72         xdrrec_getbytes,
73         xdrrec_putbytes,
74         xdrrec_getpos,
75         xdrrec_setpos,
76         xdrrec_inline,
77         xdrrec_destroy
78 };
79
80 /*
81  * A record is composed of one or more record fragments.
82  * A record fragment is a two-byte header followed by zero to
83  * 2**32-1 bytes.  The header is treated as a long unsigned and is
84  * encode/decoded to the network via htonl/ntohl.  The low order 31 bits
85  * are a byte count of the fragment.  The highest order bit is a boolean:
86  * 1 => this fragment is the last fragment of the record,
87  * 0 => this fragment is followed by more fragment(s).
88  *
89  * The fragment/record machinery is not general;  it is constructed to
90  * meet the needs of xdr and rpc based on tcp.
91  */
92
93 #define LAST_FRAG ((u_long)(1 << 31))
94
95 typedef struct rec_strm {
96         caddr_t tcp_handle;
97         caddr_t the_buffer;
98         /*
99          * out-goung bits
100          */
101         int (*writeit) ();
102         caddr_t out_base;                       /* output buffer (points to frag header) */
103         caddr_t out_finger;                     /* next output position */
104         caddr_t out_boundry;            /* data cannot up to this address */
105         u_long *frag_header;            /* beginning of curren fragment */
106         bool_t frag_sent;                       /* true if buffer sent in middle of record */
107         /*
108          * in-coming bits
109          */
110         int (*readit) ();
111         u_long in_size;                         /* fixed size of the input buffer */
112         caddr_t in_base;
113         caddr_t in_finger;                      /* location of next byte to be had */
114         caddr_t in_boundry;                     /* can read up to this location */
115         long fbtbc;                                     /* fragment bytes to be consumed */
116         bool_t last_frag;
117         u_int sendsize;
118         u_int recvsize;
119 } RECSTREAM;
120
121
122 /*
123  * Create an xdr handle for xdrrec
124  * xdrrec_create fills in xdrs.  Sendsize and recvsize are
125  * send and recv buffer sizes (0 => use default).
126  * tcp_handle is an opaque handle that is passed as the first parameter to
127  * the procedures readit and writeit.  Readit and writeit are read and
128  * write respectively.   They are like the system
129  * calls expect that they take an opaque handle rather than an fd.
130  */
131 void xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
132 register XDR *xdrs;
133 register u_int sendsize;
134 register u_int recvsize;
135 caddr_t tcp_handle;
136 int (*readit) ();                               /* like read, but pass it a tcp_handle, not sock */
137 int (*writeit) ();                              /* like write, but pass it a tcp_handle, not sock */
138 {
139         register RECSTREAM *rstrm = (RECSTREAM *) mem_alloc(sizeof(RECSTREAM));
140
141         if (rstrm == NULL) {
142                 (void) fprintf(stderr, "xdrrec_create: out of memory\n");
143                 /* 
144                  *  This is bad.  Should rework xdrrec_create to 
145                  *  return a handle, and in this case return NULL
146                  */
147                 return;
148         }
149         /*
150          * adjust sizes and allocate buffer quad byte aligned
151          */
152         rstrm->sendsize = sendsize = fix_buf_size(sendsize);
153         rstrm->recvsize = recvsize = fix_buf_size(recvsize);
154         rstrm->the_buffer =
155                 mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
156         if (rstrm->the_buffer == NULL) {
157                 (void) fprintf(stderr, "xdrrec_create: out of memory\n");
158                 return;
159         }
160         for (rstrm->out_base = rstrm->the_buffer;
161                  (u_int) rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
162                  rstrm->out_base++);
163         rstrm->in_base = rstrm->out_base + sendsize;
164         /*
165          * now the rest ...
166          */
167         xdrs->x_ops = &xdrrec_ops;
168         xdrs->x_private = (caddr_t) rstrm;
169         rstrm->tcp_handle = tcp_handle;
170         rstrm->readit = readit;
171         rstrm->writeit = writeit;
172         rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
173         rstrm->frag_header = (u_long *) rstrm->out_base;
174         rstrm->out_finger += sizeof(u_long);
175         rstrm->out_boundry += sendsize;
176         rstrm->frag_sent = FALSE;
177         rstrm->in_size = recvsize;
178         rstrm->in_boundry = rstrm->in_base;
179         rstrm->in_finger = (rstrm->in_boundry += recvsize);
180         rstrm->fbtbc = 0;
181         rstrm->last_frag = TRUE;
182 }
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_getlong(xdrs, lp)
191 XDR *xdrs;
192 long *lp;
193 {
194         register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
195         register long *buflp = (long *) (rstrm->in_finger);
196         long mylong;
197
198         /* first try the inline, fast case */
199         if ((rstrm->fbtbc >= sizeof(long)) &&
200                 (((int) rstrm->in_boundry - (int) buflp) >= sizeof(long))) {
201                 *lp = (long) ntohl((u_long) (*buflp));
202                 rstrm->fbtbc -= sizeof(long);
203                 rstrm->in_finger += sizeof(long);
204         } else {
205                 if (!xdrrec_getbytes(xdrs, (caddr_t) & mylong, sizeof(long)))
206                         return (FALSE);
207
208                 *lp = (long) ntohl((u_long) mylong);
209         }
210         return (TRUE);
211 }
212
213 static bool_t xdrrec_putlong(xdrs, lp)
214 XDR *xdrs;
215 long *lp;
216 {
217         register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
218         register long *dest_lp = ((long *) (rstrm->out_finger));
219
220         if ((rstrm->out_finger += sizeof(long)) > rstrm->out_boundry) {
221                 /*
222                  * this case should almost never happen so the code is
223                  * inefficient
224                  */
225                 rstrm->out_finger -= sizeof(long);
226
227                 rstrm->frag_sent = TRUE;
228                 if (!flush_out(rstrm, FALSE))
229                         return (FALSE);
230                 dest_lp = ((long *) (rstrm->out_finger));
231                 rstrm->out_finger += sizeof(long);
232         }
233         *dest_lp = (long) htonl((u_long) (*lp));
234         return (TRUE);
235 }
236
237 static bool_t
238 /* must manage buffers, fragments, and records */
239 xdrrec_getbytes(xdrs, addr, len)
240 XDR *xdrs;
241 register caddr_t addr;
242 register u_int len;
243 {
244         register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
245         register int current;
246
247         while (len > 0) {
248                 current = rstrm->fbtbc;
249                 if (current == 0) {
250                         if (rstrm->last_frag)
251                                 return (FALSE);
252                         if (!set_input_fragment(rstrm))
253                                 return (FALSE);
254                         continue;
255                 }
256                 current = (len < current) ? len : current;
257                 if (!get_input_bytes(rstrm, addr, current))
258                         return (FALSE);
259                 addr += current;
260                 rstrm->fbtbc -= current;
261                 len -= current;
262         }
263         return (TRUE);
264 }
265
266 static bool_t xdrrec_putbytes(xdrs, addr, len)
267 XDR *xdrs;
268 register caddr_t addr;
269 register u_int len;
270 {
271         register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
272         register int current;
273
274         while (len > 0) {
275                 current = (u_int) rstrm->out_boundry - (u_int) rstrm->out_finger;
276                 current = (len < current) ? len : current;
277                 bcopy(addr, rstrm->out_finger, current);
278                 rstrm->out_finger += current;
279                 addr += current;
280                 len -= current;
281                 if (rstrm->out_finger == rstrm->out_boundry) {
282                         rstrm->frag_sent = TRUE;
283                         if (!flush_out(rstrm, FALSE))
284                                 return (FALSE);
285                 }
286         }
287         return (TRUE);
288 }
289
290 static u_int xdrrec_getpos(xdrs)
291 register XDR *xdrs;
292 {
293         register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
294         register long pos;
295
296         pos = lseek((int) rstrm->tcp_handle, (long) 0, 1);
297         if (pos != -1)
298                 switch (xdrs->x_op) {
299
300                 case XDR_ENCODE:
301                         pos += rstrm->out_finger - rstrm->out_base;
302                         break;
303
304                 case XDR_DECODE:
305                         pos -= rstrm->in_boundry - rstrm->in_finger;
306                         break;
307
308                 default:
309                         pos = (u_int) - 1;
310                         break;
311                 }
312         return ((u_int) pos);
313 }
314
315 static bool_t xdrrec_setpos(xdrs, pos)
316 register XDR *xdrs;
317 u_int pos;
318 {
319         register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
320         u_int currpos = xdrrec_getpos(xdrs);
321         int delta = currpos - pos;
322         caddr_t newpos;
323
324         if ((int) currpos != -1)
325                 switch (xdrs->x_op) {
326
327                 case XDR_ENCODE:
328                         newpos = rstrm->out_finger - delta;
329                         if ((newpos > (caddr_t) (rstrm->frag_header)) &&
330                                 (newpos < rstrm->out_boundry)) {
331                                 rstrm->out_finger = newpos;
332                                 return (TRUE);
333                         }
334                         break;
335
336                 case XDR_DECODE:
337                         newpos = rstrm->in_finger - delta;
338                         if ((delta < (int) (rstrm->fbtbc)) &&
339                                 (newpos <= rstrm->in_boundry) &&
340                                 (newpos >= rstrm->in_base)) {
341                                 rstrm->in_finger = newpos;
342                                 rstrm->fbtbc -= delta;
343                                 return (TRUE);
344                         }
345                         break;
346                 }
347         return (FALSE);
348 }
349
350 static long *xdrrec_inline(xdrs, len)
351 register XDR *xdrs;
352 int len;
353 {
354         register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
355         long *buf = NULL;
356
357         switch (xdrs->x_op) {
358
359         case XDR_ENCODE:
360                 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
361                         buf = (long *) rstrm->out_finger;
362                         rstrm->out_finger += len;
363                 }
364                 break;
365
366         case XDR_DECODE:
367                 if ((len <= rstrm->fbtbc) &&
368                         ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
369                         buf = (long *) rstrm->in_finger;
370                         rstrm->fbtbc -= len;
371                         rstrm->in_finger += len;
372                 }
373                 break;
374         }
375         return (buf);
376 }
377
378 static void xdrrec_destroy(xdrs)
379 register XDR *xdrs;
380 {
381         register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
382
383         mem_free(rstrm->the_buffer,
384                          rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
385         mem_free((caddr_t) rstrm, sizeof(RECSTREAM));
386 }
387
388
389 /*
390  * Exported routines to manage xdr records
391  */
392
393 /*
394  * Before reading (deserializing from the stream, one should always call
395  * this procedure to guarantee proper record alignment.
396  */
397 bool_t 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 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 xdrrec_endofrecord(xdrs, sendnow)
442 XDR *xdrs;
443 bool_t sendnow;
444 {
445         register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
446         register u_long len;            /* fragment length */
447
448         if (sendnow || rstrm->frag_sent ||
449                 ((u_long) rstrm->out_finger + sizeof(u_long) >=
450                  (u_long) rstrm->out_boundry)) {
451                 rstrm->frag_sent = FALSE;
452                 return (flush_out(rstrm, TRUE));
453         }
454         len = (u_long) (rstrm->out_finger) - (u_long) (rstrm->frag_header) -
455                 sizeof(u_long);
456         *(rstrm->frag_header) = htonl((u_long) len | LAST_FRAG);
457         rstrm->frag_header = (u_long *) rstrm->out_finger;
458         rstrm->out_finger += sizeof(u_long);
459         return (TRUE);
460 }
461
462
463 /*
464  * Internal useful routines
465  */
466 static bool_t flush_out(rstrm, eor)
467 register RECSTREAM *rstrm;
468 bool_t eor;
469 {
470         register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
471         register u_long len = (u_long) (rstrm->out_finger) -
472                 (u_long) (rstrm->frag_header) - sizeof(u_long);
473
474         *(rstrm->frag_header) = htonl(len | eormask);
475         len = (u_long) (rstrm->out_finger) - (u_long) (rstrm->out_base);
476         if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
477                 != (int) len)
478                 return (FALSE);
479         rstrm->frag_header = (u_long *) rstrm->out_base;
480         rstrm->out_finger = (caddr_t) rstrm->out_base + sizeof(u_long);
481         return (TRUE);
482 }
483
484 static bool_t
485 /* knows nothing about records!  Only about input buffers */
486 fill_input_buf(rstrm)
487 register RECSTREAM *rstrm;
488 {
489         register caddr_t where;
490         u_int i;
491         register int len;
492
493         where = rstrm->in_base;
494         i = (u_int) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
495         where += i;
496         len = rstrm->in_size - i;
497         if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
498                 return (FALSE);
499         rstrm->in_finger = where;
500         where += len;
501         rstrm->in_boundry = where;
502         return (TRUE);
503 }
504
505 static bool_t
506 /* 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
531 /* next two bytes of the input stream are treated as a header */
532 set_input_fragment(rstrm)
533 register RECSTREAM *rstrm;
534 {
535         u_long header;
536
537         if (!get_input_bytes(rstrm, (caddr_t) & header, sizeof(header)))
538                 return (FALSE);
539         header = (long) 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
546 /* consumes input bytes; knows nothing about records! */
547 skip_input_bytes(rstrm, cnt)
548 register RECSTREAM *rstrm;
549 long cnt;
550 {
551         register int current;
552
553         while (cnt > 0) {
554                 current = (int) rstrm->in_boundry - (int) rstrm->in_finger;
555                 if (current == 0) {
556                         if (!fill_input_buf(rstrm))
557                                 return (FALSE);
558                         continue;
559                 }
560                 current = (cnt < current) ? cnt : current;
561                 rstrm->in_finger += current;
562                 cnt -= current;
563         }
564         return (TRUE);
565 }
566
567 static u_int fix_buf_size(s)
568 register u_int s;
569 {
570
571         if (s < 100)
572                 s = 4000;
573         return (RNDUP(s));
574 }