1 /* $NetBSD: xdr_rec.c,v 1.33 2012/03/20 17:14:50 matt 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.33 2012/03/20 17:14:50 matt 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(XDR *, long *); 88 static bool_t xdrrec_putlong(XDR *, const long *); 89 static bool_t xdrrec_getbytes(XDR *, char *, u_int); 90 91 static bool_t xdrrec_putbytes(XDR *, const char *, u_int); 92 static u_int xdrrec_getpos(XDR *); 93 static bool_t xdrrec_setpos(XDR *, u_int); 94 static int32_t *xdrrec_inline(XDR *, u_int); 95 static void xdrrec_destroy(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 ((uint32_t)(1 << 31)) 123 124 typedef struct rec_strm { 125 char *tcp_handle; 126 /* 127 * out-goung bits 128 */ 129 int (*writeit)(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 uint32_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)(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 uint32_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(u_int); 159 static bool_t flush_out(RECSTREAM *, bool_t); 160 static bool_t fill_input_buf(RECSTREAM *); 161 static bool_t get_input_bytes(RECSTREAM *, char *, u_int); 162 static bool_t set_input_fragment(RECSTREAM *); 163 static bool_t skip_input_bytes(RECSTREAM *, long); 164 static bool_t realloc_stream(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( 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)(char *, char *, int), 184 /* like write, but pass it a tcp_handle, not sock */ 185 int (*writeit)(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 = (uint32_t *)(void *)rstrm->out_base; 224 rstrm->out_finger += sizeof(uint32_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(XDR *xdrs, 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 >= (long)sizeof(int32_t)) && 255 (((uintptr_t)rstrm->in_boundry - (uintptr_t)buflp) >= sizeof(int32_t))) { 256 *lp = (long)ntohl((uint32_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 (u_int)sizeof(int32_t))) 262 return (FALSE); 263 *lp = (long)ntohl((uint32_t)mylong); 264 } 265 return (TRUE); 266 } 267 268 static bool_t 269 xdrrec_putlong(XDR *xdrs, const long *lp) 270 { 271 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); 272 int32_t *dest_lp = ((int32_t *)(void *)(rstrm->out_finger)); 273 274 if ((rstrm->out_finger += sizeof(int32_t)) > rstrm->out_boundry) { 275 /* 276 * this case should almost never happen so the code is 277 * inefficient 278 */ 279 rstrm->out_finger -= sizeof(int32_t); 280 rstrm->frag_sent = TRUE; 281 if (! flush_out(rstrm, FALSE)) 282 return (FALSE); 283 dest_lp = ((int32_t *)(void *)(rstrm->out_finger)); 284 rstrm->out_finger += sizeof(int32_t); 285 } 286 *dest_lp = (int32_t)htonl((uint32_t)(*lp)); 287 return (TRUE); 288 } 289 290 static bool_t /* must manage buffers, fragments, and records */ 291 xdrrec_getbytes(XDR *xdrs, char *addr, u_int len) 292 { 293 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); 294 u_int current; 295 296 while (len > 0) { 297 current = (u_int)rstrm->fbtbc; 298 if (current == 0) { 299 if (rstrm->last_frag) 300 return (FALSE); 301 if (! set_input_fragment(rstrm)) 302 return (FALSE); 303 continue; 304 } 305 current = (len < current) ? len : current; 306 if (! get_input_bytes(rstrm, addr, current)) 307 return (FALSE); 308 addr += current; 309 rstrm->fbtbc -= current; 310 len -= current; 311 } 312 return (TRUE); 313 } 314 315 static bool_t 316 xdrrec_putbytes(XDR *xdrs, const char *addr, u_int len) 317 { 318 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); 319 size_t current; 320 321 while (len > 0) { 322 current = (size_t)((u_long)rstrm->out_boundry - 323 (u_long)rstrm->out_finger); 324 current = (len < current) ? len : current; 325 memmove(rstrm->out_finger, addr, current); 326 rstrm->out_finger += current; 327 addr += current; 328 _DIAGASSERT(__type_fit(u_int, current)); 329 len -= (u_int)current; 330 if (rstrm->out_finger == rstrm->out_boundry) { 331 rstrm->frag_sent = TRUE; 332 if (! flush_out(rstrm, FALSE)) 333 return (FALSE); 334 } 335 } 336 return (TRUE); 337 } 338 339 static u_int 340 xdrrec_getpos(XDR *xdrs) 341 { 342 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private; 343 off_t pos; 344 345 pos = lseek((int)(u_long)rstrm->tcp_handle, (off_t)0, 1); 346 if (pos != -1) 347 switch (xdrs->x_op) { 348 349 case XDR_ENCODE: 350 pos += rstrm->out_finger - rstrm->out_base; 351 break; 352 353 case XDR_DECODE: 354 pos -= rstrm->in_boundry - rstrm->in_finger; 355 break; 356 357 default: 358 pos = (off_t) -1; 359 break; 360 } 361 return ((u_int) pos); 362 } 363 364 static bool_t 365 xdrrec_setpos(XDR *xdrs, u_int pos) 366 { 367 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private; 368 u_int currpos = xdrrec_getpos(xdrs); 369 int delta = currpos - pos; 370 char *newpos; 371 372 if ((int)currpos != -1) 373 switch (xdrs->x_op) { 374 375 case XDR_ENCODE: 376 newpos = rstrm->out_finger - delta; 377 if ((newpos > (char *)(void *)(rstrm->frag_header)) && 378 (newpos < rstrm->out_boundry)) { 379 rstrm->out_finger = newpos; 380 return (TRUE); 381 } 382 break; 383 384 case XDR_DECODE: 385 newpos = rstrm->in_finger - delta; 386 if ((delta < (int)(rstrm->fbtbc)) && 387 (newpos <= rstrm->in_boundry) && 388 (newpos >= rstrm->in_base)) { 389 rstrm->in_finger = newpos; 390 rstrm->fbtbc -= delta; 391 return (TRUE); 392 } 393 break; 394 395 case XDR_FREE: 396 break; 397 } 398 return (FALSE); 399 } 400 401 static int32_t * 402 xdrrec_inline(XDR *xdrs, 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 <= (u_int)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(XDR *xdrs) 433 { 434 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private; 435 436 mem_free(rstrm->out_base, rstrm->sendsize); 437 mem_free(rstrm->in_base, rstrm->recvsize); 438 mem_free(rstrm, sizeof(RECSTREAM)); 439 } 440 441 442 /* 443 * Exported routines to manage xdr records 444 */ 445 446 /* 447 * Before reading (deserializing from the stream, one should always call 448 * this procedure to guarantee proper record alignment. 449 */ 450 bool_t 451 xdrrec_skiprecord(XDR *xdrs) 452 { 453 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); 454 enum xprt_stat xstat; 455 456 if (rstrm->nonblock) { 457 if (__xdrrec_getrec(xdrs, &xstat, FALSE)) { 458 rstrm->fbtbc = 0; 459 return TRUE; 460 } 461 if (rstrm->in_finger == rstrm->in_boundry && 462 xstat == XPRT_MOREREQS) { 463 rstrm->fbtbc = 0; 464 return TRUE; 465 } 466 return FALSE; 467 } 468 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) { 469 if (! skip_input_bytes(rstrm, rstrm->fbtbc)) 470 return (FALSE); 471 rstrm->fbtbc = 0; 472 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm))) 473 return (FALSE); 474 } 475 rstrm->last_frag = FALSE; 476 return (TRUE); 477 } 478 479 /* 480 * Look ahead fuction. 481 * Returns TRUE iff there is no more input in the buffer 482 * after consuming the rest of the current record. 483 */ 484 bool_t 485 xdrrec_eof(XDR *xdrs) 486 { 487 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); 488 489 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) { 490 if (!skip_input_bytes(rstrm, rstrm->fbtbc)) 491 return (TRUE); 492 rstrm->fbtbc = 0; 493 if ((!rstrm->last_frag) && (!set_input_fragment(rstrm))) 494 return (TRUE); 495 } 496 if (rstrm->in_finger == rstrm->in_boundry) 497 return (TRUE); 498 return (FALSE); 499 } 500 501 /* 502 * The client must tell the package when an end-of-record has occurred. 503 * The second paraemters tells whether the record should be flushed to the 504 * (output) tcp stream. (This let's the package support batched or 505 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection. 506 */ 507 bool_t 508 xdrrec_endofrecord(XDR *xdrs, bool_t sendnow) 509 { 510 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); 511 u_long len; /* fragment length */ 512 513 if (sendnow || rstrm->frag_sent || 514 ((u_long)rstrm->out_finger + sizeof(uint32_t) >= 515 (u_long)rstrm->out_boundry)) { 516 rstrm->frag_sent = FALSE; 517 return (flush_out(rstrm, TRUE)); 518 } 519 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) - 520 sizeof(uint32_t); 521 *(rstrm->frag_header) = htonl((uint32_t)len | LAST_FRAG); 522 rstrm->frag_header = (uint32_t *)(void *)rstrm->out_finger; 523 rstrm->out_finger += sizeof(uint32_t); 524 return (TRUE); 525 } 526 527 /* 528 * Fill the stream buffer with a record for a non-blocking connection. 529 * Return true if a record is available in the buffer, false if not. 530 */ 531 bool_t 532 __xdrrec_getrec(XDR *xdrs, enum xprt_stat *statp, bool_t expectdata) 533 { 534 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); 535 ssize_t n; 536 int fraglen; 537 538 if (!rstrm->in_haveheader) { 539 n = rstrm->readit(rstrm->tcp_handle, rstrm->in_hdrp, 540 (int)sizeof (rstrm->in_header) - rstrm->in_hdrlen); 541 if (n == 0) { 542 *statp = expectdata ? XPRT_DIED : XPRT_IDLE; 543 return FALSE; 544 } 545 if (n < 0) { 546 *statp = XPRT_DIED; 547 return FALSE; 548 } 549 rstrm->in_hdrp += n; 550 _DIAGASSERT(__type_fit(int, n)); 551 rstrm->in_hdrlen += (int)n; 552 if (rstrm->in_hdrlen < (int)sizeof(rstrm->in_header)) { 553 *statp = XPRT_MOREREQS; 554 return FALSE; 555 } 556 rstrm->in_header = ntohl(rstrm->in_header); 557 fraglen = (int)(rstrm->in_header & ~LAST_FRAG); 558 if (fraglen == 0 || fraglen > rstrm->in_maxrec || 559 (rstrm->in_reclen + fraglen) > rstrm->in_maxrec) { 560 *statp = XPRT_DIED; 561 return FALSE; 562 } 563 rstrm->in_reclen += fraglen; 564 if ((u_int)rstrm->in_reclen > rstrm->recvsize) { 565 if (!realloc_stream(rstrm, rstrm->in_reclen)) { 566 *statp = XPRT_DIED; 567 return FALSE; 568 } 569 } 570 if (rstrm->in_header & LAST_FRAG) { 571 rstrm->in_header &= ~LAST_FRAG; 572 rstrm->last_frag = TRUE; 573 } 574 } 575 576 n = rstrm->readit(rstrm->tcp_handle, 577 rstrm->in_base + rstrm->in_received, 578 (rstrm->in_reclen - rstrm->in_received)); 579 580 if (n < 0) { 581 *statp = XPRT_DIED; 582 return FALSE; 583 } 584 585 if (n == 0) { 586 *statp = expectdata ? XPRT_DIED : XPRT_IDLE; 587 return FALSE; 588 } 589 590 _DIAGASSERT(__type_fit(int, n)); 591 rstrm->in_received += (int)n; 592 593 if (rstrm->in_received == rstrm->in_reclen) { 594 rstrm->in_haveheader = FALSE; 595 rstrm->in_hdrp = (char *)(void *)&rstrm->in_header; 596 rstrm->in_hdrlen = 0; 597 if (rstrm->last_frag) { 598 rstrm->fbtbc = rstrm->in_reclen; 599 rstrm->in_boundry = rstrm->in_base + rstrm->in_reclen; 600 rstrm->in_finger = rstrm->in_base; 601 rstrm->in_reclen = rstrm->in_received = 0; 602 *statp = XPRT_MOREREQS; 603 return TRUE; 604 } 605 } 606 607 *statp = XPRT_MOREREQS; 608 return FALSE; 609 } 610 611 bool_t 612 __xdrrec_setnonblock(XDR *xdrs, int maxrec) 613 { 614 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); 615 616 rstrm->nonblock = TRUE; 617 if (maxrec == 0) 618 maxrec = rstrm->recvsize; 619 rstrm->in_maxrec = maxrec; 620 return TRUE; 621 } 622 623 624 /* 625 * Internal useful routines 626 */ 627 static bool_t 628 flush_out(RECSTREAM *rstrm, bool_t eor) 629 { 630 uint32_t eormask = (eor == TRUE) ? LAST_FRAG : 0; 631 uint32_t len = (uint32_t)((u_long)(rstrm->out_finger) - 632 (u_long)(rstrm->frag_header) - sizeof(uint32_t)); 633 634 *(rstrm->frag_header) = htonl(len | eormask); 635 len = (uint32_t)((u_long)(rstrm->out_finger) - 636 (u_long)(rstrm->out_base)); 637 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len) 638 != (int)len) 639 return (FALSE); 640 rstrm->frag_header = (uint32_t *)(void *)rstrm->out_base; 641 rstrm->out_finger = (char *)rstrm->out_base + sizeof(uint32_t); 642 return (TRUE); 643 } 644 645 static bool_t /* knows nothing about records! Only about input buffers */ 646 fill_input_buf(RECSTREAM *rstrm) 647 { 648 char *where; 649 uint32_t i; 650 int len; 651 652 if (rstrm->nonblock) 653 return FALSE; 654 where = rstrm->in_base; 655 i = (uint32_t)((u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT); 656 where += i; 657 len = (uint32_t)(rstrm->in_size - i); 658 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1) 659 return (FALSE); 660 rstrm->in_finger = where; 661 where += len; 662 rstrm->in_boundry = where; 663 return (TRUE); 664 } 665 666 static bool_t /* knows nothing about records! Only about input buffers */ 667 get_input_bytes(RECSTREAM *rstrm, char *addr, u_int len) 668 { 669 u_int current; 670 671 if (rstrm->nonblock) { 672 if (len > ((uintptr_t)rstrm->in_boundry - (uintptr_t)rstrm->in_finger)) 673 return FALSE; 674 memcpy(addr, rstrm->in_finger, len); 675 rstrm->in_finger += len; 676 return TRUE; 677 } 678 679 while (len > 0) { 680 uintptr_t d = ((uintptr_t)rstrm->in_boundry - 681 (uintptr_t)rstrm->in_finger); 682 _DIAGASSERT(__type_fit(u_int, d)); 683 current = (u_int)d; 684 if (current == 0) { 685 if (! fill_input_buf(rstrm)) 686 return (FALSE); 687 continue; 688 } 689 current = (len < current) ? len : current; 690 memmove(addr, rstrm->in_finger, current); 691 rstrm->in_finger += current; 692 addr += current; 693 len -= current; 694 } 695 return (TRUE); 696 } 697 698 static bool_t /* next two bytes of the input stream are treated as a header */ 699 set_input_fragment(RECSTREAM *rstrm) 700 { 701 uint32_t header; 702 703 if (rstrm->nonblock) 704 return FALSE; 705 if (! get_input_bytes(rstrm, (char *)(void *)&header, 706 (u_int)sizeof(header))) 707 return (FALSE); 708 header = ntohl(header); 709 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE; 710 /* 711 * Sanity check. Try not to accept wildly incorrect 712 * record sizes. Unfortunately, the only record size 713 * we can positively identify as being 'wildly incorrect' 714 * is zero. Ridiculously large record sizes may look wrong, 715 * but we don't have any way to be certain that they aren't 716 * what the client actually intended to send us. 717 */ 718 if (header == 0) 719 return(FALSE); 720 rstrm->fbtbc = header & (~LAST_FRAG); 721 return (TRUE); 722 } 723 724 static bool_t /* consumes input bytes; knows nothing about records! */ 725 skip_input_bytes(RECSTREAM *rstrm, long cnt) 726 { 727 uint32_t current; 728 729 while (cnt > 0) { 730 current = (uint32_t)((long)rstrm->in_boundry - 731 (long)rstrm->in_finger); 732 if (current == 0) { 733 if (! fill_input_buf(rstrm)) 734 return (FALSE); 735 continue; 736 } 737 current = ((uint32_t)cnt < current) ? (uint32_t)cnt : current; 738 rstrm->in_finger += current; 739 cnt -= current; 740 } 741 return (TRUE); 742 } 743 744 static u_int 745 fix_buf_size(u_int s) 746 { 747 748 if (s < 100) 749 s = 4000; 750 return (RNDUP(s)); 751 } 752 753 /* 754 * Reallocate the input buffer for a non-block stream. 755 */ 756 static bool_t 757 realloc_stream(RECSTREAM *rstrm, int size) 758 { 759 ptrdiff_t diff; 760 char *buf; 761 762 if ((u_int)size > rstrm->recvsize) { 763 buf = realloc(rstrm->in_base, (size_t)size); 764 if (buf == NULL) 765 return FALSE; 766 diff = buf - rstrm->in_base; 767 rstrm->in_finger += diff; 768 rstrm->in_base = buf; 769 rstrm->in_boundry = buf + size; 770 rstrm->recvsize = size; 771 rstrm->in_size = size; 772 } 773 774 return TRUE; 775 } 776