OSDN Git Service

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