home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / comm / tcp / amitcp-sdk / src / rpclib / xdr_rec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-29  |  14.8 KB  |  573 lines

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