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