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