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