home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / TCPHDR.C < prev    next >
C/C++ Source or Header  |  1991-03-15  |  4KB  |  160 lines

  1. /* TCP header conversion routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "tcp.h"
  7. #include "ip.h"
  8. #include "internet.h"
  9.  
  10. /* Convert TCP header in host format into mbuf ready for transmission,
  11.  * link in data (if any). If ph != NULL, compute checksum, otherwise
  12.  * take checksum from tcph->checksum
  13.  */
  14. struct mbuf *
  15. htontcp(tcph,data,ph)
  16. register struct tcp *tcph;
  17. struct mbuf *data;
  18. struct pseudo_header *ph;
  19. {
  20.     int16 hdrlen;
  21.     struct mbuf *bp;
  22.     register char *cp;
  23.  
  24.     hdrlen =  TCPLEN;
  25.     if(tcph->optlen > 0 && tcph->optlen <= TCP_MAXOPT){
  26.         hdrlen += tcph->optlen;
  27.     } else if(tcph->mss != 0){
  28.         hdrlen += MSS_LENGTH;
  29.     }
  30.     if((bp = pushdown(data,hdrlen)) == NULLBUF){
  31.         free_p(data);
  32.         return NULLBUF;
  33.     }
  34.     cp = bp->data;
  35.     cp = put16(cp,tcph->source);
  36.     cp = put16(cp,tcph->dest);
  37.     cp = put32(cp,tcph->seq);
  38.     cp = put32(cp,tcph->ack);
  39.     *cp++ = hdrlen << 2;    /* Offset field */
  40.     *cp = 0;
  41.     if(tcph->flags.congest)
  42.         *cp |= 64;
  43.     if(tcph->flags.urg)
  44.         *cp |= 32;
  45.     if(tcph->flags.ack)
  46.         *cp |= 16;
  47.     if(tcph->flags.psh)
  48.         *cp |= 8;
  49.     if(tcph->flags.rst)
  50.         *cp |= 4;
  51.     if(tcph->flags.syn)
  52.         *cp |= 2;
  53.     if(tcph->flags.fin)
  54.         *cp |= 1;
  55.     cp++;
  56.     cp = put16(cp,tcph->wnd);
  57.     if(ph == NULLHEADER){
  58.         /* Use user-supplied checksum */
  59.         cp = put16(cp,tcph->checksum);
  60.     } else {
  61.         /* Zero out checksum field for later recalculation */
  62.         *cp++ = 0;
  63.         *cp++ = 0;
  64.     }
  65.     cp = put16(cp,tcph->up);
  66.  
  67.     /* Write options, if any */
  68.     if(hdrlen > TCPLEN){
  69.         if(tcph->mss != 0){
  70.             *cp++ = MSS_KIND;
  71.             *cp++ = MSS_LENGTH;
  72.             cp = put16(cp,tcph->mss);
  73.         } else
  74.             memcpy(cp,tcph->options,tcph->optlen);
  75.     }
  76.     /* Recompute checksum, if requested */
  77.     if(ph != NULLHEADER)
  78.         put16(&bp->data[16],cksum(ph,bp,ph->length));
  79.  
  80.     return bp;
  81. }
  82. /* Pull TCP header off mbuf */
  83. int
  84. ntohtcp(tcph,bpp)
  85. register struct tcp *tcph;
  86. struct mbuf **bpp;
  87. {
  88.     int hdrlen,i,optlen,kind;
  89.     register int flags;
  90.     char hdrbuf[TCPLEN],*cp;
  91.  
  92.     i = pullup(bpp,hdrbuf,TCPLEN);
  93.     /* Note that the results will be garbage if the header is too short.
  94.      * We don't check for this because returned ICMP messages will be
  95.      * truncated, and we at least want to get the port numbers.
  96.      */
  97.     tcph->source = get16(&hdrbuf[0]);
  98.     tcph->dest = get16(&hdrbuf[2]);
  99.     tcph->seq = get32(&hdrbuf[4]);
  100.     tcph->ack = get32(&hdrbuf[8]);
  101.     hdrlen = (hdrbuf[12] & 0xf0) >> 2;
  102.     flags = hdrbuf[13];
  103.     tcph->flags.congest = flags & 64;
  104.     tcph->flags.urg = flags & 32;
  105.     tcph->flags.ack = flags & 16;
  106.     tcph->flags.psh = flags & 8;
  107.     tcph->flags.rst = flags & 4;
  108.     tcph->flags.syn = flags & 2;
  109.     tcph->flags.fin = flags & 1;
  110.     tcph->wnd = get16(&hdrbuf[14]);
  111.     tcph->checksum = get16(&hdrbuf[16]);
  112.     tcph->up = get16(&hdrbuf[18]);
  113.     tcph->mss = 0;
  114.     tcph->optlen = hdrlen - TCPLEN;
  115.  
  116.     /* Check for option field. Only space for one is allowed, but
  117.      * since there's only one TCP option (MSS) this isn't a problem
  118.      */
  119.     if(i < TCPLEN || hdrlen < TCPLEN)
  120.         return -1;    /* Header smaller than legal minimum */
  121.     if(tcph->optlen == 0)
  122.         return (int)hdrlen;    /* No options, all done */
  123.  
  124.     if(tcph->optlen > len_p(*bpp)){
  125.         /* Remainder too short for options length specified */
  126.         return -1;
  127.     }
  128.     pullup(bpp,tcph->options,tcph->optlen);    /* "Can't fail" */
  129.     /* Process options */
  130.     for(cp=tcph->options,i=tcph->optlen; i > 0;){
  131.         kind = *cp++;
  132.         /* Process single-byte options */
  133.         switch(kind){
  134.         case EOL_KIND:
  135.             i--;
  136.             cp++;
  137.             return (int)hdrlen;    /* End of options list */
  138.         case NOOP_KIND:
  139.             i--;
  140.             cp++;
  141.             continue;    /* Go look for next option */
  142.         }
  143.         /* All other options have a length field */
  144.         optlen = uchar(*cp++);
  145.  
  146.         /* Process valid multi-byte options */
  147.         switch(kind){
  148.         case MSS_KIND:
  149.             if(optlen == MSS_LENGTH){
  150.                 tcph->mss = get16(cp);
  151.             }
  152.             break;
  153.         }
  154.         optlen = max(2,optlen);    /* Enforce legal minimum */
  155.         i -= optlen;
  156.         cp += optlen - 2;
  157.     }
  158.     return (int)hdrlen;
  159. }
  160.