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