xdr_rec.c revision 1.9 1 /* $NetBSD: xdr_rec.c,v 1.9 1998/02/10 04:55:02 lukem 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.9 1998/02/10 04:55:02 lukem 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_int32_t). 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 *, int32_t *));
81 static bool_t xdrrec_putlong __P((XDR *, int32_t *));
82 static bool_t xdrrec_getbytes __P((XDR *, caddr_t, size_t));
83
84 static bool_t xdrrec_putbytes __P((XDR *, caddr_t, size_t));
85 static size_t xdrrec_getpos __P((XDR *));
86 static bool_t xdrrec_setpos __P((XDR *, size_t));
87 static int32_t *xdrrec_inline __P((XDR *, size_t));
88 static void xdrrec_destroy __P((XDR *));
89
90 static 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 caddr_t tcp_handle;
118 caddr_t the_buffer;
119 /*
120 * out-goung bits
121 */
122 int (*writeit) __P((caddr_t, caddr_t, size_t));
123 caddr_t out_base; /* output buffer (points to frag header) */
124 caddr_t out_finger; /* next output position */
125 caddr_t out_boundry; /* data cannot up to this address */
126 u_int32_t *frag_header; /* beginning of current fragment */
127 bool_t frag_sent; /* true if buffer sent in middle of record */
128 /*
129 * in-coming bits
130 */
131 int (*readit) __P((caddr_t , caddr_t , size_t));
132 u_int32_t in_size; /* fixed size of the input buffer */
133 caddr_t in_base;
134 caddr_t in_finger; /* location of next byte to be had */
135 caddr_t in_boundry; /* can read up to this location */
136 u_int32_t fbtbc; /* fragment bytes to be consumed */
137 bool_t last_frag;
138 size_t sendsize;
139 size_t recvsize;
140 } RECSTREAM;
141
142 static size_t fix_buf_size __P((size_t));
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 *, caddr_t, int));
146 static bool_t set_input_fragment __P((RECSTREAM *));
147 static bool_t skip_input_bytes __P((RECSTREAM *, int32_t));
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 size_t sendsize;
163 size_t recvsize;
164 caddr_t tcp_handle;
165 /* like read, but pass it a tcp_handle, not sock */
166 int (*readit) __P((caddr_t, caddr_t, size_t));
167 /* like write, but pass it a tcp_handle, not sock */
168 int (*writeit) __P((caddr_t, caddr_t, size_t));
169 {
170 RECSTREAM *rstrm =
171 (RECSTREAM *)mem_alloc(sizeof(RECSTREAM));
172
173 if (rstrm == NULL) {
174 warnx("xdrrec_create: out of memory");
175 /*
176 * This is bad. Should rework xdrrec_create to
177 * return a handle, and in this case return NULL
178 */
179 return;
180 }
181 /*
182 * adjust sizes and allocate buffer quad byte aligned
183 */
184 rstrm->sendsize = sendsize = fix_buf_size(sendsize);
185 rstrm->recvsize = recvsize = fix_buf_size(recvsize);
186 rstrm->the_buffer =
187 mem_alloc((size_t)(sendsize + recvsize + BYTES_PER_XDR_UNIT));
188 if (rstrm->the_buffer == NULL) {
189 warnx("xdrrec_create: out of memory");
190 return;
191 }
192 for (rstrm->out_base = rstrm->the_buffer;
193 (u_int32_t)rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
194 rstrm->out_base++);
195 rstrm->in_base = rstrm->out_base + sendsize;
196 /*
197 * now the rest ...
198 */
199 xdrs->x_ops = &xdrrec_ops;
200 xdrs->x_private = (caddr_t)rstrm;
201 rstrm->tcp_handle = tcp_handle;
202 rstrm->readit = readit;
203 rstrm->writeit = writeit;
204 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
205 rstrm->frag_header = (u_int32_t *)rstrm->out_base;
206 rstrm->out_finger += sizeof(u_int32_t);
207 rstrm->out_boundry += sendsize;
208 rstrm->frag_sent = FALSE;
209 rstrm->in_size = recvsize;
210 rstrm->in_boundry = rstrm->in_base;
211 rstrm->in_finger = (rstrm->in_boundry += recvsize);
212 rstrm->fbtbc = 0;
213 rstrm->last_frag = TRUE;
214 }
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_getlong(xdrs, lp)
224 XDR *xdrs;
225 int32_t *lp;
226 {
227 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
228 int32_t *buflp = (int32_t *)(rstrm->in_finger);
229 int32_t mylong;
230
231 /* first try the inline, fast case */
232 if ((rstrm->fbtbc >= sizeof(int32_t))
233 && (((int32_t)rstrm->in_boundry - (int32_t)buflp)
234 >= sizeof(int32_t))) {
235 *lp = (int32_t)ntohl((u_int32_t)(*buflp));
236 rstrm->fbtbc -= sizeof(int32_t);
237 rstrm->in_finger += sizeof(int32_t);
238 } else {
239 if (! xdrrec_getbytes(xdrs, (caddr_t)&mylong, sizeof(int32_t)))
240 return (FALSE);
241 *lp = (int32_t)ntohl((u_int32_t)mylong);
242 }
243 return (TRUE);
244 }
245
246 static bool_t
247 xdrrec_putlong(xdrs, lp)
248 XDR *xdrs;
249 int32_t *lp;
250 {
251 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
252 int32_t *dest_lp = ((int32_t *)(rstrm->out_finger));
253
254 if ((rstrm->out_finger += sizeof(int32_t)) > rstrm->out_boundry) {
255 /*
256 * this case should almost never happen so the code is
257 * inefficient
258 */
259 rstrm->out_finger -= sizeof(int32_t);
260 rstrm->frag_sent = TRUE;
261 if (! flush_out(rstrm, FALSE))
262 return (FALSE);
263 dest_lp = ((int32_t *)(rstrm->out_finger));
264 rstrm->out_finger += sizeof(int32_t);
265 }
266 *dest_lp = (int32_t)htonl((u_int32_t)(*lp));
267 return (TRUE);
268 }
269
270 static bool_t /* must manage buffers, fragments, and records */
271 xdrrec_getbytes(xdrs, addr, len)
272 XDR *xdrs;
273 caddr_t addr;
274 size_t len;
275 {
276 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
277 int current;
278
279 while (len > 0) {
280 current = rstrm->fbtbc;
281 if (current == 0) {
282 if (rstrm->last_frag)
283 return (FALSE);
284 if (! set_input_fragment(rstrm))
285 return (FALSE);
286 continue;
287 }
288 current = (len < current) ? len : current;
289 if (! get_input_bytes(rstrm, addr, current))
290 return (FALSE);
291 addr += current;
292 rstrm->fbtbc -= current;
293 len -= current;
294 }
295 return (TRUE);
296 }
297
298 static bool_t
299 xdrrec_putbytes(xdrs, addr, len)
300 XDR *xdrs;
301 caddr_t addr;
302 size_t len;
303 {
304 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
305 int32_t current;
306
307 while (len > 0) {
308 current = (u_int32_t)rstrm->out_boundry -
309 (u_int32_t)rstrm->out_finger;
310 current = (len < current) ? len : current;
311 memmove(rstrm->out_finger, addr, current);
312 rstrm->out_finger += current;
313 addr += current;
314 len -= current;
315 if (rstrm->out_finger == rstrm->out_boundry) {
316 rstrm->frag_sent = TRUE;
317 if (! flush_out(rstrm, FALSE))
318 return (FALSE);
319 }
320 }
321 return (TRUE);
322 }
323
324 static size_t
325 xdrrec_getpos(xdrs)
326 XDR *xdrs;
327 {
328 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
329 int32_t pos;
330
331 pos = lseek((off_t)(int32_t)rstrm->tcp_handle, 0, 1);
332 if (pos != -1)
333 switch (xdrs->x_op) {
334
335 case XDR_ENCODE:
336 pos += rstrm->out_finger - rstrm->out_base;
337 break;
338
339 case XDR_DECODE:
340 pos -= rstrm->in_boundry - rstrm->in_finger;
341 break;
342
343 default:
344 pos = (size_t) -1;
345 break;
346 }
347 return ((size_t) pos);
348 }
349
350 static bool_t
351 xdrrec_setpos(xdrs, pos)
352 XDR *xdrs;
353 size_t pos;
354 {
355 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
356 size_t currpos = xdrrec_getpos(xdrs);
357 int delta = currpos - pos;
358 caddr_t newpos;
359
360 if ((int)currpos != -1)
361 switch (xdrs->x_op) {
362
363 case XDR_ENCODE:
364 newpos = rstrm->out_finger - delta;
365 if ((newpos > (caddr_t)(rstrm->frag_header)) &&
366 (newpos < rstrm->out_boundry)) {
367 rstrm->out_finger = newpos;
368 return (TRUE);
369 }
370 break;
371
372 case XDR_DECODE:
373 newpos = rstrm->in_finger - delta;
374 if ((delta < (int)(rstrm->fbtbc)) &&
375 (newpos <= rstrm->in_boundry) &&
376 (newpos >= rstrm->in_base)) {
377 rstrm->in_finger = newpos;
378 rstrm->fbtbc -= delta;
379 return (TRUE);
380 }
381 break;
382
383 case XDR_FREE:
384 break;
385 }
386 return (FALSE);
387 }
388
389 static int32_t *
390 xdrrec_inline(xdrs, len)
391 XDR *xdrs;
392 size_t len;
393 {
394 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
395 int32_t *buf = NULL;
396
397 switch (xdrs->x_op) {
398
399 case XDR_ENCODE:
400 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
401 buf = (int32_t *) rstrm->out_finger;
402 rstrm->out_finger += len;
403 }
404 break;
405
406 case XDR_DECODE:
407 if ((len <= rstrm->fbtbc) &&
408 ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
409 buf = (int32_t *) rstrm->in_finger;
410 rstrm->fbtbc -= len;
411 rstrm->in_finger += len;
412 }
413 break;
414
415 case XDR_FREE:
416 break;
417 }
418 return (buf);
419 }
420
421 static void
422 xdrrec_destroy(xdrs)
423 XDR *xdrs;
424 {
425 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
426
427 mem_free(rstrm->the_buffer,
428 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
429 mem_free(rstrm, sizeof(RECSTREAM));
430 }
431
432
433 /*
434 * Exported routines to manage xdr records
435 */
436
437 /*
438 * Before reading (deserializing from the stream, one should always call
439 * this procedure to guarantee proper record alignment.
440 */
441 bool_t
442 xdrrec_skiprecord(xdrs)
443 XDR *xdrs;
444 {
445 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
446
447 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
448 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
449 return (FALSE);
450 rstrm->fbtbc = 0;
451 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
452 return (FALSE);
453 }
454 rstrm->last_frag = FALSE;
455 return (TRUE);
456 }
457
458 /*
459 * Look ahead fuction.
460 * Returns TRUE iff there is no more input in the buffer
461 * after consuming the rest of the current record.
462 */
463 bool_t
464 xdrrec_eof(xdrs)
465 XDR *xdrs;
466 {
467 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
468
469 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
470 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
471 return (TRUE);
472 rstrm->fbtbc = 0;
473 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
474 return (TRUE);
475 }
476 if (rstrm->in_finger == rstrm->in_boundry)
477 return (TRUE);
478 return (FALSE);
479 }
480
481 /*
482 * The client must tell the package when an end-of-record has occurred.
483 * The second paraemters tells whether the record should be flushed to the
484 * (output) tcp stream. (This let's the package support batched or
485 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
486 */
487 bool_t
488 xdrrec_endofrecord(xdrs, sendnow)
489 XDR *xdrs;
490 bool_t sendnow;
491 {
492 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
493 u_int32_t len; /* fragment length */
494
495 if (sendnow || rstrm->frag_sent ||
496 ((u_int32_t)rstrm->out_finger + sizeof(u_int32_t) >=
497 (u_int32_t)rstrm->out_boundry)) {
498 rstrm->frag_sent = FALSE;
499 return (flush_out(rstrm, TRUE));
500 }
501 len = (u_int32_t)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
502 sizeof(u_int32_t);
503 *(rstrm->frag_header) = htonl((u_int32_t)len | LAST_FRAG);
504 rstrm->frag_header = (u_int32_t *)rstrm->out_finger;
505 rstrm->out_finger += sizeof(u_int32_t);
506 return (TRUE);
507 }
508
509
510 /*
511 * Internal useful routines
512 */
513 static bool_t
514 flush_out(rstrm, eor)
515 RECSTREAM *rstrm;
516 bool_t eor;
517 {
518 u_int32_t eormask = (eor == TRUE) ? LAST_FRAG : 0;
519 u_int32_t len = (u_long)(rstrm->out_finger) -
520 (u_long)(rstrm->frag_header) - sizeof(u_int32_t);
521
522 *(rstrm->frag_header) = htonl(len | eormask);
523 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->out_base);
524 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
525 != (int)len)
526 return (FALSE);
527 rstrm->frag_header = (u_int32_t *)rstrm->out_base;
528 rstrm->out_finger = (caddr_t)rstrm->out_base + sizeof(u_int32_t);
529 return (TRUE);
530 }
531
532 static bool_t /* knows nothing about records! Only about input buffers */
533 fill_input_buf(rstrm)
534 RECSTREAM *rstrm;
535 {
536 caddr_t where;
537 u_int32_t i;
538 long len;
539
540 where = rstrm->in_base;
541 i = (u_int32_t)rstrm->in_boundry % BYTES_PER_XDR_UNIT;
542 where += i;
543 len = rstrm->in_size - i;
544 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
545 return (FALSE);
546 rstrm->in_finger = where;
547 where += len;
548 rstrm->in_boundry = where;
549 return (TRUE);
550 }
551
552 static bool_t /* knows nothing about records! Only about input buffers */
553 get_input_bytes(rstrm, addr, len)
554 RECSTREAM *rstrm;
555 caddr_t addr;
556 int len;
557 {
558 long current;
559
560 while (len > 0) {
561 current = (long)rstrm->in_boundry - (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, (caddr_t)&header, sizeof(header)))
583 return (FALSE);
584 header = (long)ntohl(header);
585 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
586 rstrm->fbtbc = header & (~LAST_FRAG);
587 return (TRUE);
588 }
589
590 static bool_t /* consumes input bytes; knows nothing about records! */
591 skip_input_bytes(rstrm, cnt)
592 RECSTREAM *rstrm;
593 int32_t cnt;
594 {
595 int32_t current;
596
597 while (cnt > 0) {
598 current = (size_t)rstrm->in_boundry - (size_t)rstrm->in_finger;
599 if (current == 0) {
600 if (! fill_input_buf(rstrm))
601 return (FALSE);
602 continue;
603 }
604 current = (cnt < current) ? cnt : current;
605 rstrm->in_finger += current;
606 cnt -= current;
607 }
608 return (TRUE);
609 }
610
611 static size_t
612 fix_buf_size(s)
613 size_t s;
614 {
615
616 if (s < 100)
617 s = 4000;
618 return (RNDUP(s));
619 }
620