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