Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/xdr_rec.c

raw symbols SCIP
1
2 /*
3 * Copyright (c) 2009, Sun Microsystems, Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * - Neither the name of Sun Microsystems, Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30
31 /*
32 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
33 * layer above tcp (for rpc's use).
34 *
35 * Copyright (C) 1984, Sun Microsystems, Inc.
36 *
37 * These routines interface XDRSTREAMS to a tcp/ip connection.
38 * There is a record marking layer between the xdr stream
39 * and the tcp transport level. A record is composed on one or more
40 * record fragments. A record fragment is a thirty-two bit header followed
41 * by n bytes of data, where n is contained in the header. The header
42 * is represented as a htonl(u_long). Thegh order bit encodes
43 * whether or not the fragment is the last fragment of the record
44 * (1 => fragment is last, 0 => more fragments to follow.
45 * The other 31 bits encode the byte length of the fragment.
46 */
47
48 #include <sys/types.h>
49
50 #include <netinet/in.h>
51
52 #include <err.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56
57 #include <rpc/types.h>
58 #include <rpc/xdr.h>
59 #include <rpc/auth.h>
60 #include <rpc/svc_auth.h>
61 #include <rpc/svc.h>
62 #include <rpc/clnt.h>
63 #include <stddef.h>
64 #include <errno.h>
65 #include "rpc_com.h"
66 static bool_t xdrrec_getlong(XDR *, long *);
67 static bool_t xdrrec_putlong(XDR *, const long *);
68 static bool_t xdrrec_getbytes(XDR *, char *, u_int);
69
70 static bool_t xdrrec_putbytes(XDR *, const char *, u_int);
71 static u_int xdrrec_getpos(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 xdrrec_getlong,
78 xdrrec_putlong,
79 xdrrec_getbytes,
80 xdrrec_putbytes,
81 xdrrec_getpos,
82 xdrrec_setpos,
83 xdrrec_inline,
84 xdrrec_destroy
85 };
86
87 /*
88 * A record is composed of one or more record fragments.
89 * A record fragment is a four-byte header followed by zero to
90 * 2**32-1 bytes. The header is treated as a long unsigned and is
91 * encode/decoded to the network via htonl/ntohl. The low order 31 bits
92 * are a byte count of the fragment. The highest order bit is a boolean:
93 * 1 => this fragment is the last fragment of the record,
94 * 0 => this fragment is followed by more fragment(s).
95 *
96 * The fragment/record machinery is not general; it is constructed to
97 * meet the needs of xdr and rpc based on tcp.
98 */
99
100 #define LAST_FRAG ((u_int32_t)(1 << 31))
101
102 typedef struct rec_strm {
103 char *tcp_handle;
104 /*
105 * out-goung bits
106 */
107 int (*writeit)(void *, void *, int);
108 char *out_base; /* output buffer (points to frag header) */
109 char *out_finger; /* next output position */
110 char *out_boundry; /* data cannot up to this address */
111 u_int32_t *frag_header; /* beginning of curren fragment */
112 bool_t frag_sent; /* true if buffer sent in middle of record */
113 /*
114 * in-coming bits
115 */
116 int (*readit)(void *, void *, int);
117 u_long in_size; /* fixed size of the input buffer */
118 char *in_base;
119 char *in_finger; /* location of next byte to be had */
120 char *in_boundry; /* can read up to this location */
121 long fbtbc; /* fragment bytes to be consumed */
122 bool_t last_frag;
123 u_int sendsize;
124 u_int recvsize;
125
126 bool_t nonblock;
127 bool_t in_haveheader;
128 u_int32_t in_header;
129 char *in_hdrp;
130 int in_hdrlen;
131 int in_reclen;
132 int in_received;
133 int in_maxrec;
134 } RECSTREAM;
135
136 static u_int fix_buf_size(u_int);
137 static bool_t flush_out(RECSTREAM *, bool_t);
138 static bool_t fill_input_buf(RECSTREAM *);
139 static bool_t get_input_bytes(RECSTREAM *, char *, int);
140 static bool_t set_input_fragment(RECSTREAM *);
141 static bool_t skip_input_bytes(RECSTREAM *, long);
142 static bool_t realloc_stream(RECSTREAM *, int);
143
144
145 /*
146 * Create an xdr handle for xdrrec
147 * xdrrec_create fills in xdrs. Sendsize and recvsize are
148 * send and recv buffer sizes (0 => use default).
149 * tcp_handle is an opaque handle that is passed as the first parameter to
150 * the procedures readit and writeit. Readit and writeit are read and
151 * write respectively. They are like the system
152 * calls expect that they take an opaque handle rather than an fd.
153 */
154 void
155 xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
156 XDR *xdrs;
157 u_int sendsize;
158 u_int recvsize;
159 void *tcp_handle;
160 /* like read, but pass it a tcp_handle, not sock */
161 int (*readit)(void *, void *, int);
162 /* like write, but pass it a tcp_handle, not sock */
163 int (*writeit)(void *, void *, int);
164 {
165 RECSTREAM *rstrm = mem_alloc(sizeof(RECSTREAM));
166
167 if (rstrm == NULL) {
168 warnx("xdrrec_create: out of memory");
169 /*
170 * This is bad. Should rework xdrrec_create to
171 * return a handle, and in this case return NULL
172 */
173 return;
174 }
175 rstrm->sendsize = sendsize = fix_buf_size(sendsize);
176 rstrm->out_base = mem_alloc(rstrm->sendsize);
177 if (rstrm->out_base == NULL) {
178 warnx("xdrrec_create: out of memory");
179 mem_free(rstrm, sizeof(RECSTREAM));
180 return;
181 }
182 rstrm->recvsize = recvsize = fix_buf_size(recvsize);
183 rstrm->in_base = mem_alloc(recvsize);
184 if (rstrm->in_base == NULL) {
185 warnx("xdrrec_create: out of memory");
186 mem_free(rstrm->out_base, sendsize);
187 mem_free(rstrm, sizeof(RECSTREAM));
188 return;
189 }
190 /*
191 * now the rest ...
192 */
193 xdrs->x_ops = &xdrrec_ops;
194 xdrs->x_private = rstrm;
195 rstrm->tcp_handle = tcp_handle;
196 rstrm->readit = readit;
197 rstrm->writeit = writeit;
198 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
199 rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
200 rstrm->out_finger += sizeof(u_int32_t);
201 rstrm->out_boundry += sendsize;
202 rstrm->frag_sent = FALSE;
203 rstrm->in_size = recvsize;
204 rstrm->in_boundry = rstrm->in_base;
205 rstrm->in_finger = (rstrm->in_boundry += recvsize);
206 rstrm->fbtbc = 0;
207 rstrm->last_frag = TRUE;
208 rstrm->in_haveheader = FALSE;
209 rstrm->in_hdrlen = 0;
210 rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
211 rstrm->nonblock = FALSE;
212 rstrm->in_reclen = 0;
213 rstrm->in_received = 0;
214 }
215
216
217 /*
218 * The reoutines 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 long *lp;
226 {
227 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
228 int32_t *buflp = (int32_t *)(void *)(rstrm->in_finger);
229 int32_t mylong;
230
231 /* first try the inline, fast case */
232 if ((rstrm->fbtbc >= sizeof(int32_t)) &&
233 (((long)rstrm->in_boundry - (long)buflp) >= sizeof(int32_t))) {
234 *lp = (long)ntohl((u_int32_t)(*buflp));
235 rstrm->fbtbc -= sizeof(int32_t);
236 rstrm->in_finger += sizeof(int32_t);
237 } else {
238 if (! xdrrec_getbytes(xdrs, (char *)(void *)&mylong,
239 sizeof(int32_t)))
240 return (FALSE);
241 *lp = (long)ntohl((u_int32_t)mylong);
242 }
243 return (TRUE);
244 }
245
246 static bool_t
247 xdrrec_putlong(xdrs, lp)
248 XDR *xdrs;
249 const long *lp;
250 {
251 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
252 int32_t *dest_lp = ((int32_t *)(void *)(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 *)(void *)(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 char *addr;
274 u_int len;
275 {
276 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
277 int current;
278
279 while (len > 0) {
280 current = (int)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 const char *addr;
302 u_int len;
303 {
304 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
305 size_t current;
306
307 while (len > 0) {
308 current = (size_t)((u_long)rstrm->out_boundry -
309 (u_long)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 u_int
325 xdrrec_getpos(xdrs)
326 XDR *xdrs;
327 {
328 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
329 off_t pos;
330
331 switch (xdrs->x_op) {
332
333 case XDR_ENCODE:
334 pos = rstrm->out_finger - rstrm->out_base
335 - BYTES_PER_XDR_UNIT;
336 break;
337
338 case XDR_DECODE:
339 pos = rstrm->in_boundry - rstrm->in_finger
340 - BYTES_PER_XDR_UNIT;
341 break;
342
343 default:
344 pos = (off_t) -1;
345 break;
346 }
347 return ((u_int) pos);
348 }
349
350 static bool_t
351 xdrrec_setpos(xdrs, pos)
352 XDR *xdrs;
353 u_int pos;
354 {
355 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private;
356 u_int currpos = xdrrec_getpos(xdrs);
357 int delta = currpos - pos;
358 char *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 > (char *)(void *)(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 u_int 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 *)(void *)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 *)(void *)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->out_base, rstrm->sendsize);
428 mem_free(rstrm->in_base, rstrm->recvsize);
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 enum xprt_stat xstat;
447
448 if (rstrm->nonblock) {
449 if (__xdrrec_getrec(xdrs, &xstat, FALSE)) {
450 rstrm->fbtbc = 0;
451 return TRUE;
452 }
453 if (rstrm->in_finger == rstrm->in_boundry &&
454 xstat == XPRT_MOREREQS) {
455 rstrm->fbtbc = 0;
456 return TRUE;
457 }
458 return FALSE;
459 }
460
461 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
462 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
463 return (FALSE);
464 rstrm->fbtbc = 0;
465 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
466 return (FALSE);
467 }
468 rstrm->last_frag = FALSE;
469 return (TRUE);
470 }
471
472 /*
473 * Look ahead function.
474 * Returns TRUE iff there is no more input in the buffer
475 * after consuming the rest of the current record.
476 */
477 bool_t
478 xdrrec_eof(xdrs)
479 XDR *xdrs;
480 {
481 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
482
483 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) {
484 if (! skip_input_bytes(rstrm, rstrm->fbtbc))
485 return (TRUE);
486 rstrm->fbtbc = 0;
487 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm)))
488 return (TRUE);
489 }
490 if (rstrm->in_finger == rstrm->in_boundry)
491 return (TRUE);
492 return (FALSE);
493 }
494
495 /*
496 * The client must tell the package when an end-of-record has occurred.
497 * The second paraemters tells whether the record should be flushed to the
498 * (output) tcp stream. (This let's the package support batched or
499 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
500 */
501 bool_t
502 xdrrec_endofrecord(xdrs, sendnow)
503 XDR *xdrs;
504 bool_t sendnow;
505 {
506 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
507 u_long len; /* fragment length */
508
509 if (sendnow || rstrm->frag_sent ||
510 ((u_long)rstrm->out_finger + sizeof(u_int32_t) >=
511 (u_long)rstrm->out_boundry)) {
512 rstrm->frag_sent = FALSE;
513 return (flush_out(rstrm, TRUE));
514 }
515 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) -
516 sizeof(u_int32_t);
517 *(rstrm->frag_header) = htonl((u_int32_t)len | LAST_FRAG);
518 rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_finger;
519 rstrm->out_finger += sizeof(u_int32_t);
520 return (TRUE);
521 }
522
523 /*
524 * Fill the stream buffer with a record for a non-blocking connection.
525 * Return true if a record is available in the buffer, false if not.
526 */
527 bool_t
528 __xdrrec_getrec(xdrs, statp, expectdata)
529 XDR *xdrs;
530 enum xprt_stat *statp;
531 bool_t expectdata;
532 {
533 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
534 ssize_t n;
535 int fraglen;
536
537 if (!rstrm->in_haveheader) {
538 n = rstrm->readit(rstrm->tcp_handle, rstrm->in_hdrp,
539 (int)sizeof (rstrm->in_header) - rstrm->in_hdrlen);
540 if (n == 0) {
541 /* EAGAIN or EWOULDBLOCK means a zero length
542 * read not an EOF.
543 */
544 if (errno == EAGAIN || errno == EWOULDBLOCK)
545 *statp = XPRT_IDLE;
546 else
547 *statp = expectdata ? XPRT_DIED : XPRT_IDLE;
548 return FALSE;
549 }
550 if (n < 0) {
551 *statp = XPRT_DIED;
552 return FALSE;
553 }
554 rstrm->in_hdrp += n;
555 rstrm->in_hdrlen += n;
556 if (rstrm->in_hdrlen < sizeof (rstrm->in_header)) {
557 *statp = XPRT_MOREREQS;
558 return FALSE;
559 }
560 rstrm->in_header = ntohl(rstrm->in_header);
561 fraglen = (int)(rstrm->in_header & ~LAST_FRAG);
562 if (fraglen == 0 || fraglen > rstrm->in_maxrec ||
563 (rstrm->in_reclen + fraglen) > rstrm->in_maxrec) {
564 *statp = XPRT_DIED;
565 return FALSE;
566 }
567 rstrm->in_reclen += fraglen;
568 if (rstrm->in_reclen > rstrm->recvsize)
569 realloc_stream(rstrm, rstrm->in_reclen);
570 if (rstrm->in_header & LAST_FRAG) {
571 rstrm->in_header &= ~LAST_FRAG;
572 rstrm->last_frag = TRUE;
573 }
574 rstrm->in_haveheader = 1;
575 }
576
577 n = rstrm->readit(rstrm->tcp_handle,
578 rstrm->in_base + rstrm->in_received,
579 (rstrm->in_reclen - rstrm->in_received));
580
581 if (n < 0) {
582 *statp = XPRT_DIED;
583 return FALSE;
584 }
585
586 if (n == 0) {
587 /* EAGAIN or EWOULDBLOCK means a zero length
588 * read not an EOF.
589 */
590 if (errno == EAGAIN || errno == EWOULDBLOCK)
591 *statp = XPRT_IDLE;
592 else
593 *statp = expectdata ? XPRT_DIED : XPRT_IDLE;
594 return FALSE;
595 }
596
597 rstrm->in_received += n;
598
599 if (rstrm->in_received == rstrm->in_reclen) {
600 rstrm->in_haveheader = FALSE;
601 rstrm->in_hdrp = (char *)(void *)&rstrm->in_header;
602 rstrm->in_hdrlen = 0;
603 if (rstrm->last_frag) {
604 rstrm->fbtbc = rstrm->in_reclen;
605 rstrm->in_boundry = rstrm->in_base + rstrm->in_reclen;
606 rstrm->in_finger = rstrm->in_base;
607 rstrm->in_reclen = rstrm->in_received = 0;
608 *statp = XPRT_MOREREQS;
609 return TRUE;
610 }
611 }
612
613 *statp = XPRT_MOREREQS;
614 return FALSE;
615 }
616
617 bool_t
618 __xdrrec_setnonblock(xdrs, maxrec)
619 XDR *xdrs;
620 int maxrec;
621 {
622 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
623
624 rstrm->nonblock = TRUE;
625 if (maxrec == 0)
626 maxrec = rstrm->recvsize;
627 rstrm->in_maxrec = maxrec;
628 return TRUE;
629 }
630
631 /*
632 * Internal useful routines
633 */
634 static bool_t
635 flush_out(rstrm, eor)
636 RECSTREAM *rstrm;
637 bool_t eor;
638 {
639 u_int32_t eormask = (eor == TRUE) ? LAST_FRAG : 0;
640 u_int32_t len = (u_int32_t)((u_long)(rstrm->out_finger) -
641 (u_long)(rstrm->frag_header) - sizeof(u_int32_t));
642
643 *(rstrm->frag_header) = htonl(len | eormask);
644 len = (u_int32_t)((u_long)(rstrm->out_finger) -
645 (u_long)(rstrm->out_base));
646 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len)
647 != (int)len)
648 return (FALSE);
649 rstrm->frag_header = (u_int32_t *)(void *)rstrm->out_base;
650 rstrm->out_finger = (char *)rstrm->out_base + sizeof(u_int32_t);
651 return (TRUE);
652 }
653
654 static bool_t /* knows nothing about records! Only about input buffers */
655 fill_input_buf(rstrm)
656 RECSTREAM *rstrm;
657 {
658 char *where;
659 u_int32_t i;
660 int len;
661
662 if (rstrm->nonblock)
663 return FALSE;
664
665 where = rstrm->in_base;
666 i = (u_int32_t)((u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT);
667 where += i;
668 len = (u_int32_t)(rstrm->in_size - i);
669 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1)
670 return (FALSE);
671 rstrm->in_finger = where;
672 where += len;
673 rstrm->in_boundry = where;
674 return (TRUE);
675 }
676
677 static bool_t /* knows nothing about records! Only about input buffers */
678 get_input_bytes(rstrm, addr, len)
679 RECSTREAM *rstrm;
680 char *addr;
681 int len;
682 {
683 size_t current;
684
685 if (rstrm->nonblock) {
686 if (len > (int)(rstrm->in_boundry - rstrm->in_finger))
687 return FALSE;
688 memcpy(addr, rstrm->in_finger, (size_t)len);
689 rstrm->in_finger += len;
690 return TRUE;
691 }
692
693 while (len > 0) {
694 current = (size_t)((long)rstrm->in_boundry -
695 (long)rstrm->in_finger);
696 if (current == 0) {
697 if (! fill_input_buf(rstrm))
698 return (FALSE);
699 continue;
700 }
701 current = (len < current) ? len : current;
702 memmove(addr, rstrm->in_finger, current);
703 rstrm->in_finger += current;
704 addr += current;
705 len -= current;
706 }
707 return (TRUE);
708 }
709
710 static bool_t /* next two bytes of the input stream are treated as a header */
711 set_input_fragment(rstrm)
712 RECSTREAM *rstrm;
713 {
714 u_int32_t header;
715
716 if (rstrm->nonblock)
717 return FALSE;
718 if (! get_input_bytes(rstrm, (char *)(void *)&header, sizeof(header)))
719 return (FALSE);
720 header = ntohl(header);
721 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
722 /*
723 * Sanity check. Try not to accept wildly incorrect
724 * record sizes. Unfortunately, the only record size
725 * we can positively identify as being 'wildly incorrect'
726 * is zero. Ridiculously large record sizes may look wrong,
727 * but we don't have any way to be certain that they aren't
728 * what the client actually intended to send us.
729 */
730 if (header == 0)
731 return(FALSE);
732 rstrm->fbtbc = header & (~LAST_FRAG);
733 return (TRUE);
734 }
735
736 static bool_t /* consumes input bytes; knows nothing about records! */
737 skip_input_bytes(rstrm, cnt)
738 RECSTREAM *rstrm;
739 long cnt;
740 {
741 u_int32_t current;
742
743 while (cnt > 0) {
744 current = (size_t)((long)rstrm->in_boundry -
745 (long)rstrm->in_finger);
746 if (current == 0) {
747 if (! fill_input_buf(rstrm))
748 return (FALSE);
749 continue;
750 }
751 current = (u_int32_t)((cnt < current) ? cnt : current);
752 rstrm->in_finger += current;
753 cnt -= current;
754 }
755 return (TRUE);
756 }
757
758 static u_int
759 fix_buf_size(s)
760 u_int s;
761 {
762
763 if (s < 100)
764 s = 4000;
765 return (RNDUP(s));
766 }
767
768 /*
769 * Reallocate the input buffer for a non-block stream.
770 */
771 static bool_t
772 realloc_stream(rstrm, size)
773 RECSTREAM *rstrm;
774 int size;
775 {
776 ptrdiff_t diff;
777 char *buf;
778
779 if (size > rstrm->recvsize) {
780 buf = realloc(rstrm->in_base, (size_t)size);
781 if (buf == NULL)
782 return FALSE;
783 diff = buf - rstrm->in_base;
784 rstrm->in_finger += diff;
785 rstrm->in_base = buf;
786 rstrm->in_boundry = buf + size;
787 rstrm->recvsize = size;
788 rstrm->in_size = size;
789 }
790
791 return TRUE;
792 }