Update.
[platform/upstream/glibc.git] / sunrpc / 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 #include <stdio.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <rpc/rpc.h>
51 #include <libintl.h>
52
53 #ifdef USE_IN_LIBIO
54 # include <libio/iolibio.h>
55 # define fputs(s, f) _IO_fputs (s, f)
56 #endif
57
58 static bool_t xdrrec_getlong (XDR *, long *);
59 static bool_t xdrrec_putlong (XDR *, const long *);
60 static bool_t xdrrec_getbytes (XDR *, caddr_t, u_int);
61 static bool_t xdrrec_putbytes (XDR *, const char *, u_int);
62 static u_int xdrrec_getpos (const XDR *);
63 static bool_t xdrrec_setpos (XDR *, u_int);
64 static int32_t *xdrrec_inline (XDR *, int);
65 static void xdrrec_destroy (XDR *);
66 static bool_t xdrrec_getint32 (XDR *, int32_t *);
67 static bool_t xdrrec_putint32 (XDR *, const int32_t *);
68
69 static const 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   xdrrec_getint32,
79   xdrrec_putint32
80 };
81
82 /*
83  * A record is composed of one or more record fragments.
84  * A record fragment is a two-byte header followed by zero to
85  * 2**32-1 bytes.  The header is treated as a long unsigned and is
86  * encode/decoded to the network via htonl/ntohl.  The low order 31 bits
87  * are a byte count of the fragment.  The highest order bit is a boolean:
88  * 1 => this fragment is the last fragment of the record,
89  * 0 => this fragment is followed by more fragment(s).
90  *
91  * The fragment/record machinery is not general;  it is constructed to
92  * meet the needs of xdr and rpc based on tcp.
93  */
94
95 #define LAST_FRAG (1UL << 31)
96
97 typedef struct rec_strm
98   {
99     caddr_t tcp_handle;
100     caddr_t the_buffer;
101     /*
102      * out-going bits
103      */
104     int (*writeit) (char *, char *, int);
105     caddr_t out_base;           /* output buffer (points to frag header) */
106     caddr_t out_finger;         /* next output position */
107     caddr_t out_boundry;        /* data cannot up to this address */
108     u_int32_t *frag_header;     /* beginning of curren fragment */
109     bool_t frag_sent;           /* true if buffer sent in middle of record */
110     /*
111      * in-coming bits
112      */
113     int (*readit) (char *, char *, int);
114     u_long in_size;             /* fixed size of the input buffer */
115     caddr_t in_base;
116     caddr_t in_finger;          /* location of next byte to be had */
117     caddr_t in_boundry;         /* can read up to this location */
118     long fbtbc;                 /* fragment bytes to be consumed */
119     bool_t last_frag;
120     u_int sendsize;
121     u_int recvsize;
122   }
123 RECSTREAM;
124
125 static u_int fix_buf_size (u_int) internal_function;
126 static bool_t skip_input_bytes (RECSTREAM *, long) internal_function;
127 static bool_t flush_out (RECSTREAM *, bool_t) internal_function;
128 static bool_t set_input_fragment (RECSTREAM *) internal_function;
129 static bool_t get_input_bytes (RECSTREAM *, caddr_t, int) internal_function;
130
131 /*
132  * Create an xdr handle for xdrrec
133  * xdrrec_create fills in xdrs.  Sendsize and recvsize are
134  * send and recv buffer sizes (0 => use default).
135  * tcp_handle is an opaque handle that is passed as the first parameter to
136  * the procedures readit and writeit.  Readit and writeit are read and
137  * write respectively.   They are like the system
138  * calls expect that they take an opaque handle rather than an fd.
139  */
140 void
141 xdrrec_create (XDR *xdrs, u_int sendsize,
142                u_int recvsize, caddr_t tcp_handle,
143                int (*readit) (char *, char *, int),
144                int (*writeit) (char *, char *, int))
145 {
146   RECSTREAM *rstrm = (RECSTREAM *) mem_alloc (sizeof (RECSTREAM));
147   caddr_t tmp;
148
149   if (rstrm == NULL)
150     {
151       (void) fputs (_("xdrrec_create: out of memory\n"), stderr);
152       /*
153        *  This is bad.  Should rework xdrrec_create to
154        *  return a handle, and in this case return NULL
155        */
156       return;
157     }
158   /*
159    * adjust sizes and allocate buffer quad byte aligned
160    */
161   rstrm->sendsize = sendsize = fix_buf_size (sendsize);
162   rstrm->recvsize = recvsize = fix_buf_size (recvsize);
163   rstrm->the_buffer = mem_alloc (sendsize + recvsize + BYTES_PER_XDR_UNIT);
164   if (rstrm->the_buffer == NULL)
165     {
166       (void) fputs (_("xdrrec_create: out of memory\n"), stderr);
167       return;
168     }
169   tmp = rstrm->the_buffer;
170   if ((size_t)tmp % BYTES_PER_XDR_UNIT)
171     tmp += BYTES_PER_XDR_UNIT - (size_t)tmp % BYTES_PER_XDR_UNIT;
172   rstrm->out_base = tmp;
173   rstrm->in_base = tmp + sendsize;
174   /*
175    * now the rest ...
176    */
177   /* We have to add the const since the `struct xdr_ops' in `struct XDR'
178      is not `const'.  */
179   xdrs->x_ops = (struct xdr_ops *) &xdrrec_ops;
180   xdrs->x_private = (caddr_t) rstrm;
181   rstrm->tcp_handle = tcp_handle;
182   rstrm->readit = readit;
183   rstrm->writeit = writeit;
184   rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
185   rstrm->frag_header = (u_int32_t *) rstrm->out_base;
186   rstrm->out_finger += 4;
187   rstrm->out_boundry += sendsize;
188   rstrm->frag_sent = FALSE;
189   rstrm->in_size = recvsize;
190   rstrm->in_boundry = rstrm->in_base;
191   rstrm->in_finger = (rstrm->in_boundry += recvsize);
192   rstrm->fbtbc = 0;
193   rstrm->last_frag = TRUE;
194 }
195
196
197 /*
198  * The routines defined below are the xdr ops which will go into the
199  * xdr handle filled in by xdrrec_create.
200  */
201
202 static bool_t
203 xdrrec_getlong (XDR *xdrs, long *lp)
204 {
205   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
206   int32_t *buflp = (int32_t *) rstrm->in_finger;
207   int32_t mylong;
208
209   /* first try the inline, fast case */
210   if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
211       rstrm->in_boundry - (char *) buflp >= BYTES_PER_XDR_UNIT)
212     {
213       *lp = (int32_t) ntohl (*buflp);
214       rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
215       rstrm->in_finger += BYTES_PER_XDR_UNIT;
216     }
217   else
218     {
219       if (!xdrrec_getbytes (xdrs, (caddr_t) & mylong,
220                             BYTES_PER_XDR_UNIT))
221         return FALSE;
222       *lp = (int32_t) ntohl (mylong);
223     }
224   return TRUE;
225 }
226
227 static bool_t
228 xdrrec_putlong (XDR *xdrs, const long *lp)
229 {
230   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
231   int32_t *dest_lp = (int32_t *) rstrm->out_finger;
232
233   if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
234     {
235       /*
236        * this case should almost never happen so the code is
237        * inefficient
238        */
239       rstrm->out_finger -= BYTES_PER_XDR_UNIT;
240       rstrm->frag_sent = TRUE;
241       if (!flush_out (rstrm, FALSE))
242         return FALSE;
243       dest_lp = (int32_t *) rstrm->out_finger;
244       rstrm->out_finger += BYTES_PER_XDR_UNIT;
245     }
246   *dest_lp = htonl (*lp);
247   return TRUE;
248 }
249
250 static bool_t      /* must manage buffers, fragments, and records */
251 xdrrec_getbytes (XDR *xdrs, caddr_t addr, u_int len)
252 {
253   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
254   u_int current;
255
256   while (len > 0)
257     {
258       current = rstrm->fbtbc;
259       if (current == 0)
260         {
261           if (rstrm->last_frag)
262             return FALSE;
263           if (!set_input_fragment (rstrm))
264             return FALSE;
265           continue;
266         }
267       current = (len < current) ? len : current;
268       if (!get_input_bytes (rstrm, addr, current))
269         return FALSE;
270       addr += current;
271       rstrm->fbtbc -= current;
272       len -= current;
273     }
274   return TRUE;
275 }
276
277 static bool_t
278 xdrrec_putbytes (XDR *xdrs, const char *addr, u_int len)
279 {
280   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
281   u_int current;
282
283   while (len > 0)
284     {
285       current = rstrm->out_boundry - rstrm->out_finger;
286       current = (len < current) ? len : current;
287       bcopy (addr, rstrm->out_finger, current);
288       rstrm->out_finger += current;
289       addr += current;
290       len -= current;
291       if (rstrm->out_finger == rstrm->out_boundry && len > 0)
292         {
293           rstrm->frag_sent = TRUE;
294           if (!flush_out (rstrm, FALSE))
295             return FALSE;
296         }
297     }
298   return TRUE;
299 }
300
301 static u_int
302 xdrrec_getpos (const XDR *xdrs)
303 {
304   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
305   long pos;
306
307   pos = __lseek ((int) (long) rstrm->tcp_handle, (long) 0, 1);
308   if (pos != -1)
309     switch (xdrs->x_op)
310       {
311
312       case XDR_ENCODE:
313         pos += rstrm->out_finger - rstrm->out_base;
314         break;
315
316       case XDR_DECODE:
317         pos -= rstrm->in_boundry - rstrm->in_finger;
318         break;
319
320       default:
321         pos = (u_int) - 1;
322         break;
323       }
324   return (u_int) pos;
325 }
326
327 static bool_t
328 xdrrec_setpos (XDR *xdrs, u_int pos)
329 {
330   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
331   u_int currpos = xdrrec_getpos (xdrs);
332   int delta = currpos - pos;
333   caddr_t newpos;
334
335   if ((int) currpos != -1)
336     switch (xdrs->x_op)
337       {
338
339       case XDR_ENCODE:
340         newpos = rstrm->out_finger - delta;
341         if (newpos > (caddr_t) rstrm->frag_header &&
342             newpos < rstrm->out_boundry)
343           {
344             rstrm->out_finger = newpos;
345             return TRUE;
346           }
347         break;
348
349       case XDR_DECODE:
350         newpos = rstrm->in_finger - delta;
351         if ((delta < (int) (rstrm->fbtbc)) &&
352             (newpos <= rstrm->in_boundry) &&
353             (newpos >= rstrm->in_base))
354           {
355             rstrm->in_finger = newpos;
356             rstrm->fbtbc -= delta;
357             return TRUE;
358           }
359         break;
360
361       default:
362         break;
363       }
364   return FALSE;
365 }
366
367 static int32_t *
368 xdrrec_inline (XDR *xdrs, int len)
369 {
370   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
371   int32_t *buf = NULL;
372
373   switch (xdrs->x_op)
374     {
375
376     case XDR_ENCODE:
377       if ((rstrm->out_finger + len) <= rstrm->out_boundry)
378         {
379           buf = (int32_t *) rstrm->out_finger;
380           rstrm->out_finger += len;
381         }
382       break;
383
384     case XDR_DECODE:
385       if ((len <= rstrm->fbtbc) &&
386           ((rstrm->in_finger + len) <= rstrm->in_boundry))
387         {
388           buf = (int32_t *) rstrm->in_finger;
389           rstrm->fbtbc -= len;
390           rstrm->in_finger += len;
391         }
392       break;
393
394     default:
395       break;
396     }
397   return buf;
398 }
399
400 static void
401 xdrrec_destroy (XDR *xdrs)
402 {
403   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
404
405   mem_free (rstrm->the_buffer,
406             rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
407   mem_free ((caddr_t) rstrm, sizeof (RECSTREAM));
408 }
409
410 static bool_t
411 xdrrec_getint32 (XDR *xdrs, int32_t *ip)
412 {
413   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
414   int32_t *bufip = (int32_t *) rstrm->in_finger;
415   int32_t mylong;
416
417   /* first try the inline, fast case */
418   if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
419       rstrm->in_boundry - (char *) bufip >= BYTES_PER_XDR_UNIT)
420     {
421       *ip = ntohl (*bufip);
422       rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
423       rstrm->in_finger += BYTES_PER_XDR_UNIT;
424     }
425   else
426     {
427       if (!xdrrec_getbytes (xdrs, (caddr_t) &mylong,
428                             BYTES_PER_XDR_UNIT))
429         return FALSE;
430       *ip = ntohl (mylong);
431     }
432   return TRUE;
433 }
434
435 static bool_t
436 xdrrec_putint32 (XDR *xdrs, const int32_t *ip)
437 {
438   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
439   int32_t *dest_ip = (int32_t *) rstrm->out_finger;
440
441   if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
442     {
443       /*
444        * this case should almost never happen so the code is
445        * inefficient
446        */
447       rstrm->out_finger -= BYTES_PER_XDR_UNIT;
448       rstrm->frag_sent = TRUE;
449       if (!flush_out (rstrm, FALSE))
450         return FALSE;
451       dest_ip = (int32_t *) rstrm->out_finger;
452       rstrm->out_finger += BYTES_PER_XDR_UNIT;
453     }
454   *dest_ip = htonl (*ip);
455   return TRUE;
456 }
457
458 /*
459  * Exported routines to manage xdr records
460  */
461
462 /*
463  * Before reading (deserializing from the stream, one should always call
464  * this procedure to guarantee proper record alignment.
465  */
466 bool_t
467 xdrrec_skiprecord (XDR *xdrs)
468 {
469   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
470
471   while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
472     {
473       if (!skip_input_bytes (rstrm, rstrm->fbtbc))
474         return FALSE;
475       rstrm->fbtbc = 0;
476       if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
477         return FALSE;
478     }
479   rstrm->last_frag = FALSE;
480   return TRUE;
481 }
482
483 /*
484  * Lookahead function.
485  * Returns TRUE iff there is no more input in the buffer
486  * after consuming the rest of the current record.
487  */
488 bool_t
489 xdrrec_eof (XDR *xdrs)
490 {
491   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
492
493   while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
494     {
495       if (!skip_input_bytes (rstrm, rstrm->fbtbc))
496         return TRUE;
497       rstrm->fbtbc = 0;
498       if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
499         return TRUE;
500     }
501   if (rstrm->in_finger == rstrm->in_boundry)
502     return TRUE;
503   return FALSE;
504 }
505
506 /*
507  * The client must tell the package when an end-of-record has occurred.
508  * The second parameter tells whether the record should be flushed to the
509  * (output) tcp stream.  (This lets the package support batched or
510  * pipelined procedure calls.)  TRUE => immediate flush to tcp connection.
511  */
512 bool_t
513 xdrrec_endofrecord (XDR *xdrs, bool_t sendnow)
514 {
515   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
516   u_long len;           /* fragment length */
517
518   if (sendnow || rstrm->frag_sent
519       || rstrm->out_finger + BYTES_PER_XDR_UNIT >= rstrm->out_boundry)
520     {
521       rstrm->frag_sent = FALSE;
522       return flush_out (rstrm, TRUE);
523     }
524   len = (rstrm->out_finger - (char *) rstrm->frag_header
525          - BYTES_PER_XDR_UNIT);
526   *rstrm->frag_header = htonl ((u_long) len | LAST_FRAG);
527   rstrm->frag_header = (u_int32_t *) rstrm->out_finger;
528   rstrm->out_finger += BYTES_PER_XDR_UNIT;
529   return TRUE;
530 }
531
532
533 /*
534  * Internal useful routines
535  */
536 static bool_t
537 internal_function
538 flush_out (RECSTREAM *rstrm, bool_t eor)
539 {
540   u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
541   u_long len = (rstrm->out_finger - (char *) rstrm->frag_header
542                 - BYTES_PER_XDR_UNIT);
543
544   *rstrm->frag_header = htonl (len | eormask);
545   len = rstrm->out_finger - rstrm->out_base;
546   if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
547       != (int) len)
548     return FALSE;
549   rstrm->frag_header = (u_int32_t *) rstrm->out_base;
550   rstrm->out_finger = (caddr_t) rstrm->out_base + BYTES_PER_XDR_UNIT;
551   return TRUE;
552 }
553
554 static bool_t   /* knows nothing about records!  Only about input buffers */
555 fill_input_buf (RECSTREAM *rstrm)
556 {
557   caddr_t where;
558   size_t i;
559   int len;
560
561   where = rstrm->in_base;
562   i = (size_t) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
563   where += i;
564   len = rstrm->in_size - i;
565   if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
566     return FALSE;
567   rstrm->in_finger = where;
568   where += len;
569   rstrm->in_boundry = where;
570   return TRUE;
571 }
572
573 static bool_t   /* knows nothing about records!  Only about input buffers */
574 internal_function
575 get_input_bytes (RECSTREAM *rstrm, caddr_t addr, int len)
576 {
577   int current;
578
579   while (len > 0)
580     {
581       current = rstrm->in_boundry - rstrm->in_finger;
582       if (current == 0)
583         {
584           if (!fill_input_buf (rstrm))
585             return FALSE;
586           continue;
587         }
588       current = (len < current) ? len : current;
589       bcopy (rstrm->in_finger, addr, current);
590       rstrm->in_finger += current;
591       addr += current;
592       len -= current;
593     }
594   return TRUE;
595 }
596
597 static bool_t /* next two bytes of the input stream are treated as a header */
598 internal_function
599 set_input_fragment (RECSTREAM *rstrm)
600 {
601   uint32_t header;
602
603   if (! get_input_bytes (rstrm, (caddr_t)&header, BYTES_PER_XDR_UNIT))
604     return FALSE;
605   header = ntohl (header);
606   rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
607   /*
608    * Sanity check. Try not to accept wildly incorrect fragment
609    * sizes. Unfortunately, only a size of zero can be identified as
610    * 'wildely incorrect', and this only, if it is not the last
611    * fragment of a message. Ridiculously large fragment sizes may look
612    * wrong, but we don't have any way to be certain that they aren't
613    * what the client actually intended to send us. Many existing RPC
614    * implementations may sent a fragment of size zero as the last
615    * fragment of a message.
616    */
617   if (header == 0)
618     return FALSE;
619   rstrm->fbtbc = header & ~LAST_FRAG;
620   return TRUE;
621 }
622
623 static bool_t   /* consumes input bytes; knows nothing about records! */
624 internal_function
625 skip_input_bytes (RECSTREAM *rstrm, long cnt)
626 {
627   int current;
628
629   while (cnt > 0)
630     {
631       current = rstrm->in_boundry - rstrm->in_finger;
632       if (current == 0)
633         {
634           if (!fill_input_buf (rstrm))
635             return FALSE;
636           continue;
637         }
638       current = (cnt < current) ? cnt : current;
639       rstrm->in_finger += current;
640       cnt -= current;
641     }
642   return TRUE;
643 }
644
645 static u_int
646 internal_function
647 fix_buf_size (u_int s)
648 {
649   if (s < 100)
650     s = 4000;
651   return RNDUP (s);
652 }