Fix "make xcheck" in sunrpc.
[platform/upstream/glibc.git] / sunrpc / xdr_rec.c
1 /*
2  * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
3  * layer above tcp (for rpc's use).
4  *
5  * Copyright (c) 2010, Oracle America, Inc.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above
14  *       copyright notice, this list of conditions and the following
15  *       disclaimer in the documentation and/or other materials
16  *       provided with the distribution.
17  *     * Neither the name of the "Oracle America, Inc." nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * These routines interface XDRSTREAMS to a tcp/ip connection.
35  * There is a record marking layer between the xdr stream
36  * and the tcp transport level.  A record is composed on one or more
37  * record fragments.  A record fragment is a thirty-two bit header followed
38  * by n bytes of data, where n is contained in the header.  The header
39  * is represented as a htonl(u_long).  The high order bit encodes
40  * whether or not the fragment is the last fragment of the record
41  * (1 => fragment is last, 0 => more fragments to follow.
42  * The other 31 bits encode the byte length of the fragment.
43  */
44
45 #include <stdio.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <rpc/rpc.h>
49 #include <libintl.h>
50
51 #ifdef USE_IN_LIBIO
52 # include <wchar.h>
53 # include <libio/iolibio.h>
54 #endif
55
56 static bool_t xdrrec_getlong (XDR *, long *);
57 static bool_t xdrrec_putlong (XDR *, const long *);
58 static bool_t xdrrec_getbytes (XDR *, caddr_t, u_int);
59 static bool_t xdrrec_putbytes (XDR *, const char *, u_int);
60 static u_int xdrrec_getpos (const XDR *);
61 static bool_t xdrrec_setpos (XDR *, u_int);
62 static int32_t *xdrrec_inline (XDR *, u_int);
63 static void xdrrec_destroy (XDR *);
64 static bool_t xdrrec_getint32 (XDR *, int32_t *);
65 static bool_t xdrrec_putint32 (XDR *, const int32_t *);
66
67 static const struct xdr_ops xdrrec_ops = {
68   xdrrec_getlong,
69   xdrrec_putlong,
70   xdrrec_getbytes,
71   xdrrec_putbytes,
72   xdrrec_getpos,
73   xdrrec_setpos,
74   xdrrec_inline,
75   xdrrec_destroy,
76   xdrrec_getint32,
77   xdrrec_putint32
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 (1UL << 31)
94
95 typedef struct rec_strm
96   {
97     caddr_t tcp_handle;
98     caddr_t the_buffer;
99     /*
100      * out-going bits
101      */
102     int (*writeit) (char *, char *, int);
103     caddr_t out_base;           /* output buffer (points to frag header) */
104     caddr_t out_finger;         /* next output position */
105     caddr_t out_boundry;        /* data cannot up to this address */
106     u_int32_t *frag_header;     /* beginning of curren fragment */
107     bool_t frag_sent;           /* true if buffer sent in middle of record */
108     /*
109      * in-coming bits
110      */
111     int (*readit) (char *, char *, int);
112     u_long in_size;             /* fixed size of the input buffer */
113     caddr_t in_base;
114     caddr_t in_finger;          /* location of next byte to be had */
115     caddr_t in_boundry;         /* can read up to this location */
116     long fbtbc;                 /* fragment bytes to be consumed */
117     bool_t last_frag;
118     u_int sendsize;
119     u_int recvsize;
120   }
121 RECSTREAM;
122
123 static u_int fix_buf_size (u_int) internal_function;
124 static bool_t skip_input_bytes (RECSTREAM *, long) internal_function;
125 static bool_t flush_out (RECSTREAM *, bool_t) internal_function;
126 static bool_t set_input_fragment (RECSTREAM *) internal_function;
127 static bool_t get_input_bytes (RECSTREAM *, caddr_t, int) internal_function;
128
129 /*
130  * Create an xdr handle for xdrrec
131  * xdrrec_create fills in xdrs.  Sendsize and recvsize are
132  * send and recv buffer sizes (0 => use default).
133  * tcp_handle is an opaque handle that is passed as the first parameter to
134  * the procedures readit and writeit.  Readit and writeit are read and
135  * write respectively.   They are like the system
136  * calls expect that they take an opaque handle rather than an fd.
137  */
138 void
139 xdrrec_create (XDR *xdrs, u_int sendsize,
140                u_int recvsize, caddr_t tcp_handle,
141                int (*readit) (char *, char *, int),
142                int (*writeit) (char *, char *, int))
143 {
144   RECSTREAM *rstrm = (RECSTREAM *) mem_alloc (sizeof (RECSTREAM));
145   caddr_t tmp;
146   char *buf;
147
148   sendsize = fix_buf_size (sendsize);
149   recvsize = fix_buf_size (recvsize);
150   buf = mem_alloc (sendsize + recvsize + BYTES_PER_XDR_UNIT);
151
152   if (rstrm == NULL || buf == NULL)
153     {
154       (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
155       mem_free (rstrm, sizeof (RECSTREAM));
156       mem_free (buf, sendsize + recvsize + BYTES_PER_XDR_UNIT);
157       /*
158        *  This is bad.  Should rework xdrrec_create to
159        *  return a handle, and in this case return NULL
160        */
161       return;
162     }
163   /*
164    * adjust sizes and allocate buffer quad byte aligned
165    */
166   rstrm->sendsize = sendsize;
167   rstrm->recvsize = recvsize;
168   rstrm->the_buffer = buf;
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 cast 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 libc_hidden_nolink (xdrrec_create, GLIBC_2_0)
196
197
198 /*
199  * The routines defined below are the xdr ops which will go into the
200  * xdr handle filled in by xdrrec_create.
201  */
202
203 static bool_t
204 xdrrec_getlong (XDR *xdrs, long *lp)
205 {
206   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
207   int32_t *buflp = (int32_t *) rstrm->in_finger;
208   int32_t mylong;
209
210   /* first try the inline, fast case */
211   if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
212       rstrm->in_boundry - (char *) buflp >= BYTES_PER_XDR_UNIT)
213     {
214       *lp = (int32_t) ntohl (*buflp);
215       rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
216       rstrm->in_finger += BYTES_PER_XDR_UNIT;
217     }
218   else
219     {
220       if (!xdrrec_getbytes (xdrs, (caddr_t) & mylong,
221                             BYTES_PER_XDR_UNIT))
222         return FALSE;
223       *lp = (int32_t) ntohl (mylong);
224     }
225   return TRUE;
226 }
227
228 static bool_t
229 xdrrec_putlong (XDR *xdrs, const long *lp)
230 {
231   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
232   int32_t *dest_lp = (int32_t *) rstrm->out_finger;
233
234   if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
235     {
236       /*
237        * this case should almost never happen so the code is
238        * inefficient
239        */
240       rstrm->out_finger -= BYTES_PER_XDR_UNIT;
241       rstrm->frag_sent = TRUE;
242       if (!flush_out (rstrm, FALSE))
243         return FALSE;
244       dest_lp = (int32_t *) rstrm->out_finger;
245       rstrm->out_finger += BYTES_PER_XDR_UNIT;
246     }
247   *dest_lp = htonl (*lp);
248   return TRUE;
249 }
250
251 static bool_t      /* must manage buffers, fragments, and records */
252 xdrrec_getbytes (XDR *xdrs, caddr_t addr, u_int len)
253 {
254   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
255   u_int current;
256
257   while (len > 0)
258     {
259       current = rstrm->fbtbc;
260       if (current == 0)
261         {
262           if (rstrm->last_frag)
263             return FALSE;
264           if (!set_input_fragment (rstrm))
265             return FALSE;
266           continue;
267         }
268       current = (len < current) ? len : current;
269       if (!get_input_bytes (rstrm, addr, current))
270         return FALSE;
271       addr += current;
272       rstrm->fbtbc -= current;
273       len -= current;
274     }
275   return TRUE;
276 }
277
278 static bool_t
279 xdrrec_putbytes (XDR *xdrs, const char *addr, u_int len)
280 {
281   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
282   u_int current;
283
284   while (len > 0)
285     {
286       current = rstrm->out_boundry - rstrm->out_finger;
287       current = (len < current) ? len : current;
288       memcpy (rstrm->out_finger, addr, current);
289       rstrm->out_finger += current;
290       addr += current;
291       len -= current;
292       if (rstrm->out_finger == rstrm->out_boundry && len > 0)
293         {
294           rstrm->frag_sent = TRUE;
295           if (!flush_out (rstrm, FALSE))
296             return FALSE;
297         }
298     }
299   return TRUE;
300 }
301
302 static u_int
303 xdrrec_getpos (const XDR *xdrs)
304 {
305   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
306   long pos;
307
308   pos = __lseek ((int) (long) rstrm->tcp_handle, (long) 0, 1);
309   if (pos != -1)
310     switch (xdrs->x_op)
311       {
312
313       case XDR_ENCODE:
314         pos += rstrm->out_finger - rstrm->out_base;
315         break;
316
317       case XDR_DECODE:
318         pos -= rstrm->in_boundry - rstrm->in_finger;
319         break;
320
321       default:
322         pos = (u_int) - 1;
323         break;
324       }
325   return (u_int) pos;
326 }
327
328 static bool_t
329 xdrrec_setpos (XDR *xdrs, u_int pos)
330 {
331   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
332   u_int currpos = xdrrec_getpos (xdrs);
333   int delta = currpos - pos;
334   caddr_t newpos;
335
336   if ((int) currpos != -1)
337     switch (xdrs->x_op)
338       {
339
340       case XDR_ENCODE:
341         newpos = rstrm->out_finger - delta;
342         if (newpos > (caddr_t) rstrm->frag_header &&
343             newpos < rstrm->out_boundry)
344           {
345             rstrm->out_finger = newpos;
346             return TRUE;
347           }
348         break;
349
350       case XDR_DECODE:
351         newpos = rstrm->in_finger - delta;
352         if ((delta < (int) (rstrm->fbtbc)) &&
353             (newpos <= rstrm->in_boundry) &&
354             (newpos >= rstrm->in_base))
355           {
356             rstrm->in_finger = newpos;
357             rstrm->fbtbc -= delta;
358             return TRUE;
359           }
360         break;
361
362       default:
363         break;
364       }
365   return FALSE;
366 }
367
368 static int32_t *
369 xdrrec_inline (XDR *xdrs, u_int len)
370 {
371   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
372   int32_t *buf = NULL;
373
374   switch (xdrs->x_op)
375     {
376
377     case XDR_ENCODE:
378       if ((rstrm->out_finger + len) <= rstrm->out_boundry)
379         {
380           buf = (int32_t *) rstrm->out_finger;
381           rstrm->out_finger += len;
382         }
383       break;
384
385     case XDR_DECODE:
386       if ((len <= rstrm->fbtbc) &&
387           ((rstrm->in_finger + len) <= rstrm->in_boundry))
388         {
389           buf = (int32_t *) rstrm->in_finger;
390           rstrm->fbtbc -= len;
391           rstrm->in_finger += len;
392         }
393       break;
394
395     default:
396       break;
397     }
398   return buf;
399 }
400
401 static void
402 xdrrec_destroy (XDR *xdrs)
403 {
404   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 static bool_t
412 xdrrec_getint32 (XDR *xdrs, int32_t *ip)
413 {
414   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
415   int32_t *bufip = (int32_t *) rstrm->in_finger;
416   int32_t mylong;
417
418   /* first try the inline, fast case */
419   if (rstrm->fbtbc >= BYTES_PER_XDR_UNIT &&
420       rstrm->in_boundry - (char *) bufip >= BYTES_PER_XDR_UNIT)
421     {
422       *ip = ntohl (*bufip);
423       rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
424       rstrm->in_finger += BYTES_PER_XDR_UNIT;
425     }
426   else
427     {
428       if (!xdrrec_getbytes (xdrs, (caddr_t) &mylong,
429                             BYTES_PER_XDR_UNIT))
430         return FALSE;
431       *ip = ntohl (mylong);
432     }
433   return TRUE;
434 }
435
436 static bool_t
437 xdrrec_putint32 (XDR *xdrs, const int32_t *ip)
438 {
439   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
440   int32_t *dest_ip = (int32_t *) rstrm->out_finger;
441
442   if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry)
443     {
444       /*
445        * this case should almost never happen so the code is
446        * inefficient
447        */
448       rstrm->out_finger -= BYTES_PER_XDR_UNIT;
449       rstrm->frag_sent = TRUE;
450       if (!flush_out (rstrm, FALSE))
451         return FALSE;
452       dest_ip = (int32_t *) rstrm->out_finger;
453       rstrm->out_finger += BYTES_PER_XDR_UNIT;
454     }
455   *dest_ip = htonl (*ip);
456   return TRUE;
457 }
458
459 /*
460  * Exported routines to manage xdr records
461  */
462
463 /*
464  * Before reading (deserializing from the stream, one should always call
465  * this procedure to guarantee proper record alignment.
466  */
467 bool_t
468 xdrrec_skiprecord (XDR *xdrs)
469 {
470   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
471
472   while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
473     {
474       if (!skip_input_bytes (rstrm, rstrm->fbtbc))
475         return FALSE;
476       rstrm->fbtbc = 0;
477       if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
478         return FALSE;
479     }
480   rstrm->last_frag = FALSE;
481   return TRUE;
482 }
483 libc_hidden_nolink (xdrrec_skiprecord, GLIBC_2_0)
484
485 /*
486  * Lookahead function.
487  * Returns TRUE iff there is no more input in the buffer
488  * after consuming the rest of the current record.
489  */
490 bool_t
491 xdrrec_eof (XDR *xdrs)
492 {
493   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
494
495   while (rstrm->fbtbc > 0 || (!rstrm->last_frag))
496     {
497       if (!skip_input_bytes (rstrm, rstrm->fbtbc))
498         return TRUE;
499       rstrm->fbtbc = 0;
500       if ((!rstrm->last_frag) && (!set_input_fragment (rstrm)))
501         return TRUE;
502     }
503   if (rstrm->in_finger == rstrm->in_boundry)
504     return TRUE;
505   return FALSE;
506 }
507 libc_hidden_nolink (xdrrec_eof, GLIBC_2_0)
508
509 /*
510  * The client must tell the package when an end-of-record has occurred.
511  * The second parameter tells whether the record should be flushed to the
512  * (output) tcp stream.  (This lets the package support batched or
513  * pipelined procedure calls.)  TRUE => immediate flush to tcp connection.
514  */
515 bool_t
516 xdrrec_endofrecord (XDR *xdrs, bool_t sendnow)
517 {
518   RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
519   u_long len;           /* fragment length */
520
521   if (sendnow || rstrm->frag_sent
522       || rstrm->out_finger + BYTES_PER_XDR_UNIT >= rstrm->out_boundry)
523     {
524       rstrm->frag_sent = FALSE;
525       return flush_out (rstrm, TRUE);
526     }
527   len = (rstrm->out_finger - (char *) rstrm->frag_header
528          - BYTES_PER_XDR_UNIT);
529   *rstrm->frag_header = htonl ((u_long) len | LAST_FRAG);
530   rstrm->frag_header = (u_int32_t *) rstrm->out_finger;
531   rstrm->out_finger += BYTES_PER_XDR_UNIT;
532   return TRUE;
533 }
534 libc_hidden_nolink (xdrrec_endofrecord, GLIBC_2_0)
535
536
537 /*
538  * Internal useful routines
539  */
540 static bool_t
541 internal_function
542 flush_out (RECSTREAM *rstrm, bool_t eor)
543 {
544   u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
545   u_long len = (rstrm->out_finger - (char *) rstrm->frag_header
546                 - BYTES_PER_XDR_UNIT);
547
548   *rstrm->frag_header = htonl (len | eormask);
549   len = rstrm->out_finger - rstrm->out_base;
550   if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
551       != (int) len)
552     return FALSE;
553   rstrm->frag_header = (u_int32_t *) rstrm->out_base;
554   rstrm->out_finger = (caddr_t) rstrm->out_base + BYTES_PER_XDR_UNIT;
555   return TRUE;
556 }
557
558 static bool_t   /* knows nothing about records!  Only about input buffers */
559 fill_input_buf (RECSTREAM *rstrm)
560 {
561   caddr_t where;
562   size_t i;
563   int len;
564
565   where = rstrm->in_base;
566   i = (size_t) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
567   where += i;
568   len = rstrm->in_size - i;
569   if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
570     return FALSE;
571   rstrm->in_finger = where;
572   where += len;
573   rstrm->in_boundry = where;
574   return TRUE;
575 }
576
577 static bool_t   /* knows nothing about records!  Only about input buffers */
578 internal_function
579 get_input_bytes (RECSTREAM *rstrm, caddr_t addr, int len)
580 {
581   int current;
582
583   while (len > 0)
584     {
585       current = rstrm->in_boundry - rstrm->in_finger;
586       if (current == 0)
587         {
588           if (!fill_input_buf (rstrm))
589             return FALSE;
590           continue;
591         }
592       current = (len < current) ? len : current;
593       memcpy (addr, rstrm->in_finger, current);
594       rstrm->in_finger += current;
595       addr += current;
596       len -= current;
597     }
598   return TRUE;
599 }
600
601 static bool_t /* next two bytes of the input stream are treated as a header */
602 internal_function
603 set_input_fragment (RECSTREAM *rstrm)
604 {
605   uint32_t header;
606
607   if (! get_input_bytes (rstrm, (caddr_t)&header, BYTES_PER_XDR_UNIT))
608     return FALSE;
609   header = ntohl (header);
610   rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
611   /*
612    * Sanity check. Try not to accept wildly incorrect fragment
613    * sizes. Unfortunately, only a size of zero can be identified as
614    * 'wildely incorrect', and this only, if it is not the last
615    * fragment of a message. Ridiculously large fragment sizes may look
616    * wrong, but we don't have any way to be certain that they aren't
617    * what the client actually intended to send us. Many existing RPC
618    * implementations may sent a fragment of size zero as the last
619    * fragment of a message.
620    */
621   if (header == 0)
622     return FALSE;
623   rstrm->fbtbc = header & ~LAST_FRAG;
624   return TRUE;
625 }
626
627 static bool_t   /* consumes input bytes; knows nothing about records! */
628 internal_function
629 skip_input_bytes (RECSTREAM *rstrm, long cnt)
630 {
631   int current;
632
633   while (cnt > 0)
634     {
635       current = rstrm->in_boundry - rstrm->in_finger;
636       if (current == 0)
637         {
638           if (!fill_input_buf (rstrm))
639             return FALSE;
640           continue;
641         }
642       current = (cnt < current) ? cnt : current;
643       rstrm->in_finger += current;
644       cnt -= current;
645     }
646   return TRUE;
647 }
648
649 static u_int
650 internal_function
651 fix_buf_size (u_int s)
652 {
653   if (s < 100)
654     s = 4000;
655   return RNDUP (s);
656 }