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