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