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