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