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 #if defined(LIBC_SCCS) && !defined(lint) 30 static char *rcsid = "$OpenBSD: xdr_rec.c,v 1.9 2002/02/16 21:27:24 millert Exp $"; 31 #endif /* LIBC_SCCS and not lint */ 32 33 /* 34 * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking" 35 * layer above tcp (for rpc's use). 36 * 37 * Copyright (C) 1984, Sun Microsystems, Inc. 38 * 39 * These routines interface XDRSTREAMS to a tcp/ip connection. 40 * There is a record marking layer between the xdr stream 41 * and the tcp transport level. A record is composed on one or more 42 * record fragments. A record fragment is a thirty-two bit header followed 43 * by n bytes of data, where n is contained in the header. The header 44 * is represented as a htonl(u_int32_t). The high order bit encodes 45 * whether or not the fragment is the last fragment of the record 46 * (1 => fragment is last, 0 => more fragments to follow. 47 * The other 31 bits encode the byte length of the fragment. 48 */ 49 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <rpc/types.h> 54 #include <rpc/xdr.h> 55 #include <netinet/in.h> 56 57 static bool_t xdrrec_getlong(XDR *, long *); 58 static bool_t xdrrec_putlong(XDR *, long *); 59 static bool_t xdrrec_getbytes(XDR *, caddr_t, u_int); 60 static bool_t xdrrec_putbytes(XDR *, caddr_t, u_int); 61 static u_int xdrrec_getpos(XDR *); 62 static bool_t xdrrec_setpos(XDR *, u_int); 63 static int32_t *xdrrec_inline(XDR *, u_int); 64 static void xdrrec_destroy(XDR *); 65 66 static struct xdr_ops xdrrec_ops = { 67 xdrrec_getlong, 68 xdrrec_putlong, 69 xdrrec_getbytes, 70 xdrrec_putbytes, 71 xdrrec_getpos, 72 xdrrec_setpos, 73 xdrrec_inline, 74 xdrrec_destroy 75 }; 76 77 /* 78 * A record is composed of one or more record fragments. 79 * A record fragment is a four-byte header followed by zero to 80 * 2**32-1 bytes. The header is treated as a long unsigned and is 81 * encode/decoded to the network via htonl/ntohl. The low order 31 bits 82 * are a byte count of the fragment. The highest order bit is a boolean: 83 * 1 => this fragment is the last fragment of the record, 84 * 0 => this fragment is followed by more fragment(s). 85 * 86 * The fragment/record machinery is not general; it is constructed to 87 * meet the needs of xdr and rpc based on tcp. 88 */ 89 90 #define LAST_FRAG ((u_int32_t)(1 << 31)) 91 92 typedef struct rec_strm { 93 caddr_t tcp_handle; 94 caddr_t the_buffer; 95 /* 96 * out-goung bits 97 */ 98 int (*writeit)(caddr_t, caddr_t, int); 99 caddr_t out_base; /* output buffer (points to frag header) */ 100 caddr_t out_finger; /* next output position */ 101 caddr_t out_boundry; /* data cannot up to this address */ 102 u_int32_t *frag_header; /* beginning of current fragment */ 103 bool_t frag_sent; /* true if buffer sent in middle of record */ 104 /* 105 * in-coming bits 106 */ 107 int (*readit)(caddr_t, caddr_t, int); 108 u_long in_size; /* fixed size of the input buffer */ 109 caddr_t in_base; 110 caddr_t in_finger; /* location of next byte to be had */ 111 caddr_t in_boundry; /* can read up to this location */ 112 long fbtbc; /* fragment bytes to be consumed */ 113 bool_t last_frag; 114 u_int sendsize; 115 u_int recvsize; 116 } RECSTREAM; 117 118 static u_int fix_buf_size(u_int); 119 static bool_t flush_out(RECSTREAM *, bool_t); 120 static bool_t get_input_bytes(RECSTREAM *, caddr_t, int); 121 static bool_t set_input_fragment(RECSTREAM *); 122 static bool_t skip_input_bytes(RECSTREAM *, long); 123 124 125 /* 126 * Create an xdr handle for xdrrec 127 * xdrrec_create fills in xdrs. Sendsize and recvsize are 128 * send and recv buffer sizes (0 => use default). 129 * tcp_handle is an opaque handle that is passed as the first parameter to 130 * the procedures readit and writeit. Readit and writeit are read and 131 * write respectively. They are like the system 132 * calls expect that they take an opaque handle rather than an fd. 133 */ 134 void 135 xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit) 136 XDR *xdrs; 137 u_int sendsize; 138 u_int recvsize; 139 caddr_t tcp_handle; 140 int (*readit)(); /* like read, but pass it a tcp_handle, not sock */ 141 int (*writeit)(); /* like write, but pass it a tcp_handle, not sock */ 142 { 143 RECSTREAM *rstrm = 144 (RECSTREAM *)mem_alloc(sizeof(RECSTREAM)); 145 146 if (rstrm == NULL) { 147 (void)fprintf(stderr, "xdrrec_create: out of memory\n"); 148 /* 149 * This is bad. Should rework xdrrec_create to 150 * return a handle, and in this case return NULL 151 */ 152 return; 153 } 154 /* 155 * adjust sizes and allocate buffer quad byte aligned 156 */ 157 rstrm->sendsize = sendsize = fix_buf_size(sendsize); 158 rstrm->recvsize = recvsize = fix_buf_size(recvsize); 159 rstrm->the_buffer = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT); 160 if (rstrm->the_buffer == NULL) { 161 (void)fprintf(stderr, "xdrrec_create: out of memory\n"); 162 free(rstrm); 163 return; 164 } 165 for (rstrm->out_base = rstrm->the_buffer; 166 (u_long)rstrm->out_base % BYTES_PER_XDR_UNIT != 0; 167 rstrm->out_base++); 168 rstrm->in_base = rstrm->out_base + sendsize; 169 /* 170 * now the rest ... 171 */ 172 xdrs->x_ops = &xdrrec_ops; 173 xdrs->x_private = (caddr_t)rstrm; 174 rstrm->tcp_handle = tcp_handle; 175 rstrm->readit = readit; 176 rstrm->writeit = writeit; 177 rstrm->out_finger = rstrm->out_boundry = rstrm->out_base; 178 rstrm->frag_header = (u_int32_t *)rstrm->out_base; 179 rstrm->out_finger += sizeof(u_int32_t); 180 rstrm->out_boundry += sendsize; 181 rstrm->frag_sent = FALSE; 182 rstrm->in_size = recvsize; 183 rstrm->in_boundry = rstrm->in_base; 184 rstrm->in_finger = (rstrm->in_boundry += recvsize); 185 rstrm->fbtbc = 0; 186 rstrm->last_frag = TRUE; 187 } 188 189 190 /* 191 * The reoutines defined below are the xdr ops which will go into the 192 * xdr handle filled in by xdrrec_create. 193 */ 194 195 static bool_t 196 xdrrec_getlong(xdrs, lp) 197 XDR *xdrs; 198 long *lp; 199 { 200 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); 201 int32_t *buflp = (int32_t *)(rstrm->in_finger); 202 int32_t mylong; 203 204 /* first try the inline, fast case */ 205 if ((rstrm->fbtbc >= sizeof(int32_t)) && 206 (((long)rstrm->in_boundry - (long)buflp) >= sizeof(int32_t))) { 207 *lp = (long)ntohl((u_int32_t)(*buflp)); 208 rstrm->fbtbc -= sizeof(int32_t); 209 rstrm->in_finger += sizeof(int32_t); 210 } else { 211 if (! xdrrec_getbytes(xdrs, (caddr_t)(void *)&mylong, 212 sizeof(int32_t))) 213 return (FALSE); 214 *lp = (long)ntohl((u_int32_t)mylong); 215 } 216 return (TRUE); 217 } 218 219 static bool_t 220 xdrrec_putlong(xdrs, lp) 221 XDR *xdrs; 222 long *lp; 223 { 224 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); 225 int32_t *dest_lp = ((int32_t *)(rstrm->out_finger)); 226 227 if ((rstrm->out_finger += sizeof(int32_t)) > rstrm->out_boundry) { 228 /* 229 * this case should almost never happen so the code is 230 * inefficient 231 */ 232 rstrm->out_finger -= sizeof(int32_t); 233 rstrm->frag_sent = TRUE; 234 if (! flush_out(rstrm, FALSE)) 235 return (FALSE); 236 dest_lp = ((int32_t *)(void *)(rstrm->out_finger)); 237 rstrm->out_finger += sizeof(int32_t); 238 } 239 *dest_lp = (int32_t)htonl((u_int32_t)(*lp)); 240 return (TRUE); 241 } 242 243 static bool_t /* must manage buffers, fragments, and records */ 244 xdrrec_getbytes(xdrs, addr, len) 245 XDR *xdrs; 246 caddr_t addr; 247 u_int len; 248 { 249 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); 250 int current; 251 252 while (len > 0) { 253 current = rstrm->fbtbc; 254 if (current == 0) { 255 if (rstrm->last_frag) 256 return (FALSE); 257 if (! set_input_fragment(rstrm)) 258 return (FALSE); 259 continue; 260 } 261 current = (len < current) ? len : current; 262 if (! get_input_bytes(rstrm, addr, current)) 263 return (FALSE); 264 addr += current; 265 rstrm->fbtbc -= current; 266 len -= current; 267 } 268 return (TRUE); 269 } 270 271 static bool_t 272 xdrrec_putbytes(xdrs, addr, len) 273 XDR *xdrs; 274 caddr_t addr; 275 u_int len; 276 { 277 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); 278 long current; 279 280 while (len > 0) { 281 current = (u_long)rstrm->out_boundry - 282 (u_long)rstrm->out_finger; 283 current = (len < current) ? len : current; 284 memcpy(rstrm->out_finger, addr, current); 285 rstrm->out_finger += current; 286 addr += current; 287 len -= current; 288 if (rstrm->out_finger == rstrm->out_boundry) { 289 rstrm->frag_sent = TRUE; 290 if (! flush_out(rstrm, FALSE)) 291 return (FALSE); 292 } 293 } 294 return (TRUE); 295 } 296 297 static u_int 298 xdrrec_getpos(xdrs) 299 XDR *xdrs; 300 { 301 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private; 302 long pos; 303 304 pos = lseek((int)(long)rstrm->tcp_handle, (off_t)0, 1); 305 if (pos != -1) 306 switch (xdrs->x_op) { 307 308 case XDR_ENCODE: 309 pos += rstrm->out_finger - rstrm->out_base; 310 break; 311 312 case XDR_DECODE: 313 pos -= rstrm->in_boundry - rstrm->in_finger; 314 break; 315 316 default: 317 pos = -1; 318 break; 319 } 320 return ((u_int) pos); 321 } 322 323 static bool_t 324 xdrrec_setpos(xdrs, pos) 325 XDR *xdrs; 326 u_int pos; 327 { 328 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private; 329 u_int currpos = xdrrec_getpos(xdrs); 330 int delta = currpos - pos; 331 caddr_t newpos; 332 333 if ((int)currpos != -1) 334 switch (xdrs->x_op) { 335 336 case XDR_ENCODE: 337 newpos = rstrm->out_finger - delta; 338 if ((newpos > (caddr_t)(rstrm->frag_header)) && 339 (newpos < rstrm->out_boundry)) { 340 rstrm->out_finger = newpos; 341 return (TRUE); 342 } 343 break; 344 345 case XDR_DECODE: 346 newpos = rstrm->in_finger - delta; 347 if ((delta < (int)(rstrm->fbtbc)) && 348 (newpos <= rstrm->in_boundry) && 349 (newpos >= rstrm->in_base)) { 350 rstrm->in_finger = newpos; 351 rstrm->fbtbc -= delta; 352 return (TRUE); 353 } 354 break; 355 } 356 return (FALSE); 357 } 358 359 static int32_t * 360 xdrrec_inline(xdrs, len) 361 XDR *xdrs; 362 u_int len; 363 { 364 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private; 365 int32_t *buf = NULL; 366 367 switch (xdrs->x_op) { 368 369 case XDR_ENCODE: 370 if ((rstrm->out_finger + len) <= rstrm->out_boundry) { 371 buf = (int32_t *) rstrm->out_finger; 372 rstrm->out_finger += len; 373 } 374 break; 375 376 case XDR_DECODE: 377 if ((len <= rstrm->fbtbc) && 378 ((rstrm->in_finger + len) <= rstrm->in_boundry)) { 379 buf = (int32_t *) rstrm->in_finger; 380 rstrm->fbtbc -= len; 381 rstrm->in_finger += len; 382 } 383 break; 384 } 385 return (buf); 386 } 387 388 static void 389 xdrrec_destroy(xdrs) 390 XDR *xdrs; 391 { 392 RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private; 393 394 mem_free(rstrm->the_buffer, 395 rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT); 396 mem_free((caddr_t)rstrm, sizeof(RECSTREAM)); 397 } 398 399 400 /* 401 * Exported routines to manage xdr records 402 */ 403 404 /* 405 * Before reading (deserializing from the stream, one should always call 406 * this procedure to guarantee proper record alignment. 407 */ 408 bool_t 409 xdrrec_skiprecord(xdrs) 410 XDR *xdrs; 411 { 412 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); 413 414 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) { 415 if (! skip_input_bytes(rstrm, rstrm->fbtbc)) 416 return (FALSE); 417 rstrm->fbtbc = 0; 418 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm))) 419 return (FALSE); 420 } 421 rstrm->last_frag = FALSE; 422 return (TRUE); 423 } 424 425 /* 426 * Look ahead fuction. 427 * Returns TRUE iff there is no more input in the buffer 428 * after consuming the rest of the current record. 429 */ 430 bool_t 431 xdrrec_eof(xdrs) 432 XDR *xdrs; 433 { 434 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); 435 436 while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) { 437 if (! skip_input_bytes(rstrm, rstrm->fbtbc)) 438 return (TRUE); 439 rstrm->fbtbc = 0; 440 if ((! rstrm->last_frag) && (! set_input_fragment(rstrm))) 441 return (TRUE); 442 } 443 if (rstrm->in_finger == rstrm->in_boundry) 444 return (TRUE); 445 return (FALSE); 446 } 447 448 /* 449 * The client must tell the package when an end-of-record has occurred. 450 * The second paraemters tells whether the record should be flushed to the 451 * (output) tcp stream. (This let's the package support batched or 452 * pipelined procedure calls.) TRUE => immmediate flush to tcp connection. 453 */ 454 bool_t 455 xdrrec_endofrecord(xdrs, sendnow) 456 XDR *xdrs; 457 bool_t sendnow; 458 { 459 RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); 460 u_long len; /* fragment length */ 461 462 if (sendnow || rstrm->frag_sent || 463 ((u_long)rstrm->out_finger + sizeof(u_int32_t) >= 464 (u_long)rstrm->out_boundry)) { 465 rstrm->frag_sent = FALSE; 466 return (flush_out(rstrm, TRUE)); 467 } 468 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) - 469 sizeof(u_int32_t); 470 *(rstrm->frag_header) = htonl((u_long)len | LAST_FRAG); 471 rstrm->frag_header = (u_int32_t *)rstrm->out_finger; 472 rstrm->out_finger += sizeof(u_int32_t); 473 return (TRUE); 474 } 475 476 477 /* 478 * Internal useful routines 479 */ 480 static bool_t 481 flush_out(rstrm, eor) 482 RECSTREAM *rstrm; 483 bool_t eor; 484 { 485 u_long eormask = (eor == TRUE) ? LAST_FRAG : 0; 486 u_int32_t len = (u_long)(rstrm->out_finger) - 487 (u_long)(rstrm->frag_header) - sizeof(u_int32_t); 488 489 *(rstrm->frag_header) = htonl(len | eormask); 490 len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->out_base); 491 if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len) 492 != (int)len) 493 return (FALSE); 494 rstrm->frag_header = (u_int32_t *)rstrm->out_base; 495 rstrm->out_finger = (caddr_t)rstrm->out_base + sizeof(u_int32_t); 496 return (TRUE); 497 } 498 499 static bool_t /* knows nothing about records! Only about input buffers */ 500 fill_input_buf(rstrm) 501 RECSTREAM *rstrm; 502 { 503 caddr_t where; 504 u_long i; 505 long len; 506 507 where = rstrm->in_base; 508 i = (u_long)rstrm->in_boundry % BYTES_PER_XDR_UNIT; 509 where += i; 510 len = rstrm->in_size - i; 511 if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1) 512 return (FALSE); 513 rstrm->in_finger = where; 514 where += len; 515 rstrm->in_boundry = where; 516 return (TRUE); 517 } 518 519 static bool_t /* knows nothing about records! Only about input buffers */ 520 get_input_bytes(rstrm, addr, len) 521 RECSTREAM *rstrm; 522 caddr_t addr; 523 int len; 524 { 525 long current; 526 527 while (len > 0) { 528 current = (long)rstrm->in_boundry - (long)rstrm->in_finger; 529 if (current == 0) { 530 if (! fill_input_buf(rstrm)) 531 return (FALSE); 532 continue; 533 } 534 current = (len < current) ? len : current; 535 memcpy(addr, rstrm->in_finger, current); 536 rstrm->in_finger += current; 537 addr += current; 538 len -= current; 539 } 540 return (TRUE); 541 } 542 543 static bool_t /* next four bytes of the input stream are treated as a header */ 544 set_input_fragment(rstrm) 545 RECSTREAM *rstrm; 546 { 547 u_int32_t header; 548 549 if (! get_input_bytes(rstrm, (caddr_t)&header, sizeof(header))) 550 return (FALSE); 551 header = (long)ntohl(header); 552 rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE; 553 if ((header & (~LAST_FRAG)) == 0) 554 return(FALSE); 555 rstrm->fbtbc = header & (~LAST_FRAG); 556 return (TRUE); 557 } 558 559 static bool_t /* consumes input bytes; knows nothing about records! */ 560 skip_input_bytes(rstrm, cnt) 561 RECSTREAM *rstrm; 562 long cnt; 563 { 564 long current; 565 566 while (cnt > 0) { 567 current = (long)rstrm->in_boundry - (long)rstrm->in_finger; 568 if (current == 0) { 569 if (! fill_input_buf(rstrm)) 570 return (FALSE); 571 continue; 572 } 573 current = (cnt < current) ? cnt : current; 574 rstrm->in_finger += current; 575 cnt -= current; 576 } 577 return (TRUE); 578 } 579 580 static u_int 581 fix_buf_size(s) 582 u_int s; 583 { 584 585 if (s < 100) 586 s = 4000; 587 return (RNDUP(s)); 588 } 589