Libinfo-517.200.9.tar.gz
[apple/libinfo.git] / rpc.subproj / xdr_rec.c
1 /*
2 * Copyright (c) 1999-2018 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25 /* $NetBSD: xdr_rec.c,v 1.18 2000/07/06 03:10:35 christos Exp $ */
26
27 /*
28 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
29 * unrestricted use provided that this legend is included on all tape
30 * media and as a part of the software program in whole or part. Users
31 * may copy or modify Sun RPC without charge, but are not authorized
32 * to license or distribute it to anyone else except as part of a product or
33 * program developed by the user.
34 *
35 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
36 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
37 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
38 *
39 * Sun RPC is provided with no support and without any obligation on the
40 * part of Sun Microsystems, Inc. to assist in its use, correction,
41 * modification or enhancement.
42 *
43 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
44 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
45 * OR ANY PART THEREOF.
46 *
47 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
48 * or profits or other special, indirect and consequential damages, even if
49 * Sun has been advised of the possibility of such damages.
50 *
51 * Sun Microsystems, Inc.
52 * 2550 Garcia Avenue
53 * Mountain View, California 94043
54 */
55
56 #include "libinfo_common.h"
57
58 #include <sys/cdefs.h>
59 #if defined(LIBC_SCCS) && !defined(lint)
60 static char *sccsid = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
61 static char *sccsid = "@(#)xdr_rec.c 2.2 88/08/01 4.0 RPCSRC";
62 #endif
63 #include <sys/cdefs.h>
64
65 /*
66 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
67 * layer above tcp (for rpc's use).
68 *
69 * Copyright (C) 1984, Sun Microsystems, Inc.
70 *
71 * These routines interface XDRSTREAMS to a tcp/ip connection.
72 * There is a record marking layer between the xdr stream
73 * and the tcp transport level. A record is composed on one or more
74 * record fragments. A record fragment is a thirty-two bit header followed
75 * by n bytes of data, where n is contained in the header. The header
76 * is in network byte order. Thegh order bit encodes
77 * whether or not the fragment is the last fragment of the record
78 * (1 => fragment is last, 0 => more fragments to follow.
79 * The other 31 bits encode the byte length of the fragment.
80 */
81
82 #include <sys/types.h>
83
84 #include <netinet/in.h>
85
86 #include <err.h>
87 #include <stdio.h>
88 #include <stdlib.h>
89 #include <string.h>
90 #include <unistd.h>
91
92 #include <rpc/types.h>
93 #include <rpc/xdr.h>
94 #include <rpc/auth.h>
95 #include <rpc/svc.h>
96 #include <rpc/clnt.h>
97
98 #ifdef __LP64__
99 static bool_t xdrrec_getlong(XDR *, int *);
100 static bool_t xdrrec_putlong(XDR *, const int *);
101 #else
102 static bool_t xdrrec_getlong(XDR *, long *);
103 static bool_t xdrrec_putlong(XDR *, const long *);
104 #endif
105
106 static bool_t xdrrec_getbytes(XDR *, char *, u_int);
107
108 static bool_t xdrrec_putbytes(XDR *, const char *, u_int);
109 static u_int xdrrec_getpos(XDR *);
110 static bool_t xdrrec_setpos(XDR *, u_int);
111 static int32_t *xdrrec_inline(XDR *, u_int);
112 static void xdrrec_destroy(XDR *);
113
114 bool_t __xdrrec_getrec(XDR *, enum xprt_stat *, bool_t);
115
116 static const struct xdr_ops xdrrec_ops = {
117 xdrrec_getlong,
118 xdrrec_putlong,
119 xdrrec_getbytes,
120 xdrrec_putbytes,
121 xdrrec_getpos,
122 xdrrec_setpos,
123 xdrrec_inline,
124 xdrrec_destroy
125 };
126
127 /*
128 * A record is composed of one or more record fragments.
129 * A record fragment is a four-byte header followed by zero to
130 * 2**32-1 bytes. The header is treated as an unsigned 32-bit integer and is
131 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
132 * are a byte count of the fragment. The highest order bit is a boolean:
133 * 1 => this fragment is the last fragment of the record,
134 * 0 => this fragment is followed by more fragment(s).
135 *
136 * The fragment/record machinery is not general; it is constructed to
137 * meet the needs of xdr and rpc based on tcp.
138 */
139
140 #define LAST_FRAG ((u_int32_t)(1 << 31))
141
142 typedef struct rec_strm {
143 void *tcp_handle;
144 /*
145 * out-goung bits
146 */
147 int (*writeit)(void *, void *, int);
148 char *out_base; /* output buffer (points to frag header) */
149 char *out_finger; /* next output position */
150 char *out_boundry; /* data cannot up to this address */
151 u_int32_t *frag_header; /* beginning of curren fragment */
152 bool_t frag_sent; /* true if buffer sent in middle of record */
153 /*
154 * in-coming bits
155 */
156 int (*readit)(void *, void *, int);
157 size_t in_size; /* fixed size of the input buffer */
158 char *in_base;
159 char *in_finger; /* location of next byte to be had */
160 char *in_boundry; /* can read up to this location */
161 int fbtbc; /* fragment bytes to be consumed */
162 bool_t last_frag;
163 u_int sendsize;
164 u_int recvsize;
165
166 bool_t nonblock;
167 bool_t in_haveheader;
168 u_int32_t in_header;
169 char *in_hdrp;
170 int in_hdrlen;
171 int in_reclen;
172 int in_received;
173 int in_maxrec;
174 } RECSTREAM;
175
176 static u_int fix_buf_size(u_int);
177 static bool_t flush_out(RECSTREAM *, bool_t);
178 static bool_t fill_input_buf(RECSTREAM *);
179 static bool_t get_input_bytes(RECSTREAM *, char *, int);
180 static bool_t set_input_fragment(RECSTREAM *);
181 static bool_t skip_input_bytes(RECSTREAM *, int);
182 static bool_t realloc_stream(RECSTREAM *, int);
183
184
185 /*
186 * Create an xdr handle for xdrrec
187 * xdrrec_create fills in xdrs. Sendsize and recvsize are
188 * send and recv buffer sizes (0 => use default).
189 * tcp_handle is an opaque handle that is passed as the first parameter to
190 * the procedures readit and writeit. Readit and writeit are read and
191 * write respectively. They are like the system
192 * calls expect that they take an opaque handle rather than an fd.
193 */
194 LIBINFO_EXPORT
195 void
196 xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
197 XDR *xdrs;
198 u_int sendsize;
199 u_int recvsize;
200 void *tcp_handle;
201 /* like read, but pass it a tcp_handle, not sock */
202 int (*readit)(void *, void *, int);
203 /* like write, but pass it a tcp_handle, not sock */
204 int (*writeit)(void *, void *, int);
205 {
206 RECSTREAM *rstrm = mem_alloc(sizeof(RECSTREAM));
207
208 if (rstrm == NULL) {
209 warnx("xdrrec_create: out of memory");
210 /*
211 * This is bad. Should rework xdrrec_create to
212 * return a handle, and in this case return NULL
213 */
214 return;
215 }
216 rstrm->sendsize = sendsize = fix_buf_size(sendsize);
217 rstrm->out_base = mem_alloc(rstrm->sendsize);
218 if (rstrm->out_base == NULL) {
219 warnx("xdrrec_create: out of memory");
220 mem_free(rstrm, sizeof(RECSTREAM));
221 return;
222 }
223 rstrm->recvsize = recvsize = fix_buf_size(recvsize);
224 rstrm->in_base = mem_alloc(recvsize);
225 if (rstrm->in_base == NULL) {
226 warnx("xdrrec_create: out of memory");
227 mem_free(rstrm->out_base, sendsize);
228 mem_free(rstrm, sizeof(RECSTREAM));
229 return;
230 }
231 /*
232 * now the rest ...
233 */
234 xdrs->x_ops = &xdrrec_ops;
235 xdrs->x_private = rstrm;
236 rstrm->tcp_handle = tcp_handle;
237 rstrm->readit = readit;
238 rstrm->writeit = writeit;
239 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
240 rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
241 rstrm->out_finger += sizeof(u_int32_t);
242 rstrm->out_boundry += sendsize;
243 rstrm->frag_sent = FALSE;
244 rstrm->in_size = recvsize;
245 rstrm->in_boundry = rstrm->in_base;
246 rstrm->in_finger = (rstrm->in_boundry += recvsize);
247 rstrm->fbtbc = 0;
248 rstrm->last_frag = TRUE;
249 rstrm->in_haveheader = FALSE;
250 rstrm->in_hdrlen = 0;
251 rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
252 rstrm->nonblock = FALSE;
253 rstrm->in_reclen = 0;
254 rstrm->in_received = 0;
255 }
256
257
258 /*
259 * The reoutines defined below are the xdr ops which will go into the
260 * xdr handle filled in by xdrrec_create.
261 */
262
263 static bool_t
264 xdrrec_getlong(xdrs, lp)
265 XDR *xdrs;
266 #ifdef __LP64__
267 int *lp;
268 #else
269 long *lp;
270 #endif
271 {
272 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
273 int32_t *buflp = (int32_t *)(void *)(rstrm->in_finger);
274 int32_t mylong;
275
276 /* first try the inline, fast case */
277 if ((rstrm->fbtbc >= sizeof(int32_t)) && ((rstrm->in_boundry - (char *)buflp) >= sizeof(int32_t)))
278 {
279 *lp = ntohl(*buflp);
280 rstrm->fbtbc -= sizeof(int32_t);
281 rstrm->in_finger += sizeof(int32_t);
282 }
283 else
284 {
285 if (! xdrrec_getbytes(xdrs, (char *)(void *)&mylong, sizeof(int32_t))) return (FALSE);
286 *lp = ntohl(mylong);
287 }
288
289 return (TRUE);
290 }
291
292 static bool_t
293 xdrrec_putlong(xdrs, lp)
294 XDR *xdrs;
295 #ifdef __LP64__
296 const int *lp;
297 #else
298 const long *lp;
299 #endif
300 {
301 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
302 int32_t *dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
303
304 if ((rstrm->out_finger += sizeof(int32_t)) > rstrm->out_boundry)
305 {
306 /*
307 * this case should almost never happen so the code is
308 * inefficient
309 */
310 rstrm->out_finger -= sizeof(int32_t);
311 rstrm->frag_sent = TRUE;
312 if (! flush_out(rstrm, FALSE)) return (FALSE);
313 dest_lp = ((int32_t *)(void *)(rstrm->out_finger));
314 rstrm->out_finger += sizeof(int32_t);
315 }
316
317 *dest_lp = htonl(*lp);
318 return (TRUE);
319 }
320
321 static bool_t /* must manage buffers, fragments, and records */
322 xdrrec_getbytes(xdrs, addr, len)
323 XDR *xdrs;
324 char *addr;
325 u_int len;
326 {
327 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
328 int current;
329
330 while (len > 0) {
331 current = (int)rstrm->fbtbc;
332 if (current == 0) {
333 if (rstrm->last_frag)
334 return (FALSE);
335 if (! set_input_fragment(rstrm))
336 return (FALSE);
337 continue;
338 }
339 current = (len < current) ? len : current;
340 if (! get_input_bytes(rstrm, addr, current))
341 return (FALSE);
342 addr += current;
343 rstrm->fbtbc -= current;
344 len -= current;
345 }
346 return (TRUE);
347 }
348
349 static bool_t
350 xdrrec_putbytes(xdrs, addr, len)
351 XDR *xdrs;
352 const char *addr;
353 u_int len;
354 {
355 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
356 size_t current;
357
358 while (len > 0) {
359 current = (size_t)(rstrm->out_boundry - rstrm->out_finger);
360 current = (len < current) ? len : current;
361 memmove(rstrm->out_finger, addr, current);
362 rstrm->out_finger += current;
363 addr += current;
364 len -= current;
365 if (rstrm->out_finger == rstrm->out_boundry) {
366 rstrm->frag_sent = TRUE;
367 if (! flush_out(rstrm, FALSE))
368 return (FALSE);
369 }
370 }
371 return (TRUE);
372 }
373
374 static u_int
375 xdrrec_getpos(xdrs)
376 XDR *xdrs;
377 {
378 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
379 off_t pos;
380 int hfd;
381
382 /* tcp_handle is in actual fact just a file descriptor */
383 hfd = 0;
384 memcpy(&hfd, rstrm->tcp_handle, sizeof(hfd));
385
386 pos = lseek(hfd, 0, 1);
387 if (pos != -1)
388 switch (xdrs->x_op) {
389
390 case XDR_ENCODE:
391 pos += rstrm->out_finger - rstrm->out_base;
392 break;
393
394 case XDR_DECODE:
395 pos -= rstrm->in_boundry - rstrm->in_finger;
396 break;
397
398 default:
399 pos = (off_t) -1;
400 break;
401 }
402 return ((u_int) pos);
403 }
404
405 static bool_t
406 xdrrec_setpos(xdrs, pos)
407 XDR *xdrs;
408 u_int pos;
409 {
410 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
411 u_int currpos = xdrrec_getpos(xdrs);
412 int delta = currpos - pos;
413 char *newpos;
414
415 if ((int)currpos != -1)
416 switch (xdrs->x_op) {
417
418 case XDR_ENCODE:
419 newpos = rstrm->out_finger - delta;
420 if ((newpos > (char *)(void *)(rstrm->frag_header)) &&
421 (newpos < rstrm->out_boundry)) {
422 rstrm->out_finger = newpos;
423 return (TRUE);
424 }
425 break;
426
427 case XDR_DECODE:
428 newpos = rstrm->in_finger - delta;
429 if ((delta < (int)(rstrm->fbtbc)) &&
430 (newpos <= rstrm->in_boundry) &&
431 (newpos >= rstrm->in_base)) {
432 rstrm->in_finger = newpos;
433 rstrm->fbtbc -= delta;
434 return (TRUE);
435 }
436 break;
437
438 case XDR_FREE:
439 break;
440 }
441 return (FALSE);
442 }
443
444 static int32_t *
445 xdrrec_inline(xdrs, len)
446 XDR *xdrs;
447 u_int len;
448 {
449 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
450 int32_t *buf = NULL;
451
452 switch (xdrs->x_op) {
453
454 case XDR_ENCODE:
455 if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
456 buf = (int32_t *)(void *)rstrm->out_finger;
457 rstrm->out_finger += len;
458 }
459 break;
460
461 case XDR_DECODE:
462 if ((len <= rstrm->fbtbc) &&
463 ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
464 buf = (int32_t *)(void *)rstrm->in_finger;
465 rstrm->fbtbc -= len;
466 rstrm->in_finger += len;
467 }
468 break;
469
470 case XDR_FREE:
471 break;
472 }
473 return (buf);
474 }
475
476 static void
477 xdrrec_destroy(xdrs)
478 XDR *xdrs;
479 {
480 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
481
482 mem_free(rstrm->out_base, rstrm->sendsize);
483 mem_free(rstrm->in_base, rstrm->recvsize);
484 mem_free(rstrm, sizeof(RECSTREAM));
485 }
486
487
488 /*
489 * Exported routines to manage xdr records
490 */
491
492 /*
493 * Before reading (deserializing from the stream, one should always call
494 * this procedure to guarantee proper record alignment.
495 */
496 LIBINFO_EXPORT
497 bool_t
498 xdrrec_skiprecord(xdrs)
499 XDR *xdrs;
500 {
501 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
502 enum xprt_stat xstat;
503
504 if (rstrm->nonblock) {
505 if (__xdrrec_getrec(xdrs, &xstat, FALSE)) {
506 rstrm->fbtbc = 0;
507 return TRUE;
508 }
509 if (rstrm->in_finger == rstrm->in_boundry &&
510 xstat == XPRT_MOREREQS) {
511 rstrm->fbtbc = 0;
512 return TRUE;
513 }
514 return FALSE;
515 }
516
517 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
518 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
519 return (FALSE);
520 rstrm->fbtbc = 0;
521 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
522 return (FALSE);
523 }
524 rstrm->last_frag = FALSE;
525 return (TRUE);
526 }
527
528 /*
529 * Look ahead function.
530 * Returns TRUE iff there is no more input in the buffer
531 * after consuming the rest of the current record.
532 */
533 LIBINFO_EXPORT
534 bool_t
535 xdrrec_eof(xdrs)
536 XDR *xdrs;
537 {
538 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
539
540 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
541 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
542 return (TRUE);
543 rstrm->fbtbc = 0;
544 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
545 return (TRUE);
546 }
547 if (rstrm->in_finger == rstrm->in_boundry)
548 return (TRUE);
549 return (FALSE);
550 }
551
552 /*
553 * The client must tell the package when an end-of-record has occurred.
554 * The second paraemters tells whether the record should be flushed to the
555 * (output) tcp stream. (This let's the package support batched or
556 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
557 */
558 LIBINFO_EXPORT
559 bool_t
560 xdrrec_endofrecord(xdrs, sendnow)
561 XDR *xdrs;
562 bool_t sendnow;
563 {
564 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
565 unsigned int len; /* fragment length */
566
567 if (sendnow || rstrm->frag_sent ||
568 (rstrm->out_finger + sizeof(u_int32_t) >= rstrm->out_boundry)) {
569 rstrm->frag_sent = FALSE;
570 return (flush_out(rstrm, TRUE));
571 }
572 len = rstrm->out_finger - (char *)(rstrm->frag_header) - sizeof(u_int32_t);
573 *(rstrm->frag_header) = htonl(len | LAST_FRAG);
574 rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_finger;
575 rstrm->out_finger += sizeof(u_int32_t);
576 return (TRUE);
577 }
578
579 /*
580 * Fill the stream buffer with a record for a non-blocking connection.
581 * Return true if a record is available in the buffer, false if not.
582 */
583 bool_t
584 __xdrrec_getrec(xdrs, statp, expectdata)
585 XDR *xdrs;
586 enum xprt_stat *statp;
587 bool_t expectdata;
588 {
589 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
590 ssize_t n;
591 int fraglen;
592
593 if (!rstrm->in_haveheader) {
594 n = rstrm->readit(rstrm->tcp_handle, rstrm->in_hdrp,
595 (int)sizeof (rstrm->in_header) - rstrm->in_hdrlen);
596 if (n == 0) {
597 *statp = expectdata ? XPRT_DIED : XPRT_IDLE;
598 return FALSE;
599 }
600 if (n < 0) {
601 *statp = XPRT_DIED;
602 return FALSE;
603 }
604 rstrm->in_hdrp += n;
605 rstrm->in_hdrlen += n;
606 if (rstrm->in_hdrlen < sizeof (rstrm->in_header)) {
607 *statp = XPRT_MOREREQS;
608 return FALSE;
609 }
610 rstrm->in_header = ntohl(rstrm->in_header);
611 fraglen = (int)(rstrm->in_header & ~LAST_FRAG);
612 if (fraglen == 0 || fraglen > rstrm->in_maxrec ||
613 (rstrm->in_reclen + fraglen) > rstrm->in_maxrec) {
614 *statp = XPRT_DIED;
615 return FALSE;
616 }
617 rstrm->in_reclen += fraglen;
618 if (rstrm->in_reclen > rstrm->recvsize)
619 realloc_stream(rstrm, rstrm->in_reclen);
620 if (rstrm->in_header & LAST_FRAG) {
621 rstrm->in_header &= ~LAST_FRAG;
622 rstrm->last_frag = TRUE;
623 }
624 }
625
626 n = rstrm->readit(rstrm->tcp_handle,
627 rstrm->in_base + rstrm->in_received,
628 (rstrm->in_reclen - rstrm->in_received));
629
630 if (n < 0) {
631 *statp = XPRT_DIED;
632 return FALSE;
633 }
634
635 if (n == 0) {
636 *statp = expectdata ? XPRT_DIED : XPRT_IDLE;
637 return FALSE;
638 }
639
640 rstrm->in_received += n;
641
642 if (rstrm->in_received == rstrm->in_reclen) {
643 rstrm->in_haveheader = FALSE;
644 rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
645 rstrm->in_hdrlen = 0;
646 if (rstrm->last_frag) {
647 rstrm->fbtbc = rstrm->in_reclen;
648 rstrm->in_boundry = rstrm->in_base + rstrm->in_reclen;
649 rstrm->in_finger = rstrm->in_base;
650 rstrm->in_reclen = rstrm->in_received = 0;
651 *statp = XPRT_MOREREQS;
652 return TRUE;
653 }
654 }
655
656 *statp = XPRT_MOREREQS;
657 return FALSE;
658 }
659
660 bool_t
661 __xdrrec_setnonblock(xdrs, maxrec)
662 XDR *xdrs;
663 int maxrec;
664 {
665 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
666
667 rstrm->nonblock = TRUE;
668 if (maxrec == 0)
669 maxrec = rstrm->recvsize;
670 rstrm->in_maxrec = maxrec;
671 return TRUE;
672 }
673
674 /*
675 * Internal useful routines
676 */
677 static bool_t
678 flush_out(rstrm, eor)
679 RECSTREAM *rstrm;
680 bool_t eor;
681 {
682 u_int32_t eormask = (eor == TRUE) ? LAST_FRAG : 0;
683 u_int32_t len = rstrm->out_finger - (char *)(rstrm->frag_header) - sizeof(u_int32_t);
684
685 *(rstrm->frag_header) = htonl(len | eormask);
686 len = rstrm->out_finger - rstrm->out_base;
687 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, len) != len)
688 return (FALSE);
689 rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
690 rstrm->out_finger = (char *)rstrm->out_base + sizeof(u_int32_t);
691 return (TRUE);
692 }
693
694 static bool_t /* knows nothing about records! Only about input buffers */
695 fill_input_buf(rstrm)
696 RECSTREAM *rstrm;
697 {
698 char *where;
699 u_int32_t i;
700 int len;
701
702 if (rstrm->nonblock)
703 return FALSE;
704
705 where = rstrm->in_base;
706 i = (size_t)(rstrm->in_boundry) % BYTES_PER_XDR_UNIT;
707 where += i;
708 len = rstrm->in_size - i;
709 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
710 return (FALSE);
711 rstrm->in_finger = where;
712 where += len;
713 rstrm->in_boundry = where;
714 return (TRUE);
715 }
716
717 static bool_t /* knows nothing about records! Only about input buffers */
718 get_input_bytes(rstrm, addr, len)
719 RECSTREAM *rstrm;
720 char *addr;
721 int len;
722 {
723 size_t current;
724
725 if (rstrm->nonblock) {
726 if (len > (int)(rstrm->in_boundry - rstrm->in_finger))
727 return FALSE;
728 memcpy(addr, rstrm->in_finger, (size_t)len);
729 rstrm->in_finger += len;
730 return TRUE;
731 }
732
733 while (len > 0) {
734 current = (size_t)(rstrm->in_boundry - rstrm->in_finger);
735 if (current == 0) {
736 if (! fill_input_buf(rstrm))
737 return (FALSE);
738 continue;
739 }
740 current = (len < current) ? len : current;
741 memmove(addr, rstrm->in_finger, current);
742 rstrm->in_finger += current;
743 addr += current;
744 len -= current;
745 }
746 return (TRUE);
747 }
748
749 static bool_t /* next two bytes of the input stream are treated as a header */
750 set_input_fragment(rstrm)
751 RECSTREAM *rstrm;
752 {
753 u_int32_t header;
754
755 if (rstrm->nonblock)
756 return FALSE;
757 if (! get_input_bytes(rstrm, (char *)(void *)&header, sizeof(header)))
758 return (FALSE);
759 header = ntohl(header);
760 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
761 /*
762 * Sanity check. Try not to accept wildly incorrect
763 * record sizes. Unfortunately, the only record size
764 * we can positively identify as being 'wildly incorrect'
765 * is zero. Ridiculously large record sizes may look wrong,
766 * but we don't have any way to be certain that they aren't
767 * what the client actually intended to send us.
768 */
769 if (header == 0)
770 return(FALSE);
771 rstrm->fbtbc = header & (~LAST_FRAG);
772 return (TRUE);
773 }
774
775 static bool_t /* consumes input bytes; knows nothing about records! */
776 skip_input_bytes(rstrm, cnt)
777 RECSTREAM *rstrm;
778 int cnt;
779 {
780 u_int32_t current;
781
782 while (cnt > 0) {
783 current = (size_t)(rstrm->in_boundry - rstrm->in_finger);
784 if (current == 0) {
785 if (! fill_input_buf(rstrm))
786 return (FALSE);
787 continue;
788 }
789 current = (cnt < current) ? cnt : current;
790 rstrm->in_finger += current;
791 cnt -= current;
792 }
793 return (TRUE);
794 }
795
796 static u_int
797 fix_buf_size(s)
798 u_int s;
799 {
800
801 if (s < 100)
802 s = 4000;
803 return (RNDUP(s));
804 }
805
806 /*
807 * Reallocate the input buffer for a non-block stream.
808 */
809 static bool_t
810 realloc_stream(rstrm, size)
811 RECSTREAM *rstrm;
812 int size;
813 {
814 int diff;
815 char *buf;
816
817 if (size > rstrm->recvsize) {
818 buf = realloc(rstrm->in_base, (size_t)size);
819 if (buf == NULL)
820 return FALSE;
821 diff = buf - rstrm->in_base;
822 rstrm->in_finger += diff;
823 rstrm->in_base = buf;
824 rstrm->in_boundry = buf + size;
825 rstrm->recvsize = size;
826 rstrm->in_size = size;
827 }
828
829 return TRUE;
830 }