xdr_rec.c revision 1.14 1 /* $NetBSD: xdr_rec.c,v 1.14 1998/07/26 12:47:39 mycroft 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.14 1998/07/26 12:47:39 mycroft 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 =
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 = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
187 if (rstrm->the_buffer == NULL) {
188 warnx("xdrrec_create: out of memory");
189 return;
190 }
191 for (rstrm->out_base = rstrm->the_buffer;
192 (u_long)rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
193 rstrm->out_base++);
194 rstrm->in_base = rstrm->out_base + sendsize;
195 /*
196 * now the rest ...
197 */
198 xdrs->x_ops = &xdrrec_ops;
199 xdrs->x_private = (char *)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 += sizeof(u_int32_t);
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
215
216 /*
217 * The reoutines defined below are the xdr ops which will go into the
218 * xdr handle filled in by xdrrec_create.
219 */
220
221 static bool_t
222 xdrrec_getlong(xdrs, lp)
223 XDR *xdrs;
224 long *lp;
225 {
226 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
227 int32_t *buflp = (int32_t *)(rstrm->in_finger);
228 int32_t mylong;
229
230 /* first try the inline, fast case */
231 if ((rstrm->fbtbc >= sizeof(int32_t)) &&
232 (((long)rstrm->in_boundry - (long)buflp) >= sizeof(int32_t))) {
233 *lp = (long)ntohl((u_int32_t)(*buflp));
234 rstrm->fbtbc -= sizeof(int32_t);
235 rstrm->in_finger += sizeof(int32_t);
236 } else {
237 if (! xdrrec_getbytes(xdrs, (char *)&mylong, 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 *)(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 *)(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 = 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 long current;
304
305 while (len > 0) {
306 current = (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 long pos;
328
329 pos = lseek((off_t)(long)rstrm->tcp_handle, 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 = (u_int) -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 *)(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 *) 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 *) 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((char *)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_long)len | LAST_FRAG);
502 rstrm->frag_header = (u_int32_t *)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_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
517 u_int32_t len = (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_long)(rstrm->out_finger) - (u_long)(rstrm->out_base);
522 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
523 != (int)len)
524 return (FALSE);
525 rstrm->frag_header = (u_int32_t *)rstrm->out_base;
526 rstrm->out_finger = (char *)rstrm->out_base + sizeof(u_int32_t);
527 return (TRUE);
528 }
529
530 static bool_t /* knows nothing about records! Only about input buffers */
531 fill_input_buf(rstrm)
532 RECSTREAM *rstrm;
533 {
534 char *where;
535 u_long i;
536 long len;
537
538 where = rstrm->in_base;
539 i = (u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT;
540 where += i;
541 len = rstrm->in_size - i;
542 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
543 return (FALSE);
544 rstrm->in_finger = where;
545 where += len;
546 rstrm->in_boundry = where;
547 return (TRUE);
548 }
549
550 static bool_t /* knows nothing about records! Only about input buffers */
551 get_input_bytes(rstrm, addr, len)
552 RECSTREAM *rstrm;
553 char *addr;
554 int len;
555 {
556 long current;
557
558 while (len > 0) {
559 current = (long)rstrm->in_boundry - (long)rstrm->in_finger;
560 if (current == 0) {
561 if (! fill_input_buf(rstrm))
562 return (FALSE);
563 continue;
564 }
565 current = (len < current) ? len : current;
566 memmove(addr, rstrm->in_finger, current);
567 rstrm->in_finger += current;
568 addr += current;
569 len -= current;
570 }
571 return (TRUE);
572 }
573
574 static bool_t /* next two bytes of the input stream are treated as a header */
575 set_input_fragment(rstrm)
576 RECSTREAM *rstrm;
577 {
578 u_int32_t header;
579
580 if (! get_input_bytes(rstrm, (char *)&header, sizeof(header)))
581 return (FALSE);
582 header = (long)ntohl(header);
583 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
584 rstrm->fbtbc = header & (~LAST_FRAG);
585 return (TRUE);
586 }
587
588 static bool_t /* consumes input bytes; knows nothing about records! */
589 skip_input_bytes(rstrm, cnt)
590 RECSTREAM *rstrm;
591 long cnt;
592 {
593 long current;
594
595 while (cnt > 0) {
596 current = (long)rstrm->in_boundry - (long)rstrm->in_finger;
597 if (current == 0) {
598 if (! fill_input_buf(rstrm))
599 return (FALSE);
600 continue;
601 }
602 current = (cnt < current) ? cnt : current;
603 rstrm->in_finger += current;
604 cnt -= current;
605 }
606 return (TRUE);
607 }
608
609 static u_int
610 fix_buf_size(s)
611 u_int s;
612 {
613
614 if (s < 100)
615 s = 4000;
616 return (RNDUP(s));
617 }
618