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