home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1 / HamRadio.cdr / misc / tcpipsrc / iphdr.c < prev    next >
C/C++ Source or Header  |  1991-01-29  |  4KB  |  176 lines

  1. /* IP header conversion routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "ip.h"
  7. #include "internet.h"
  8.  
  9. /* Convert IP header in host format to network mbuf
  10.  * If cflag != 0, take checksum from structure,
  11.  * otherwise compute it automatically.
  12.  */
  13. struct mbuf *
  14. htonip(ip,data,cflag)
  15. register struct ip *ip;
  16. struct mbuf *data;
  17. int cflag;
  18. {
  19.     int16 hdr_len;
  20.     struct mbuf *bp;
  21.     register char *cp;
  22.     int16 fl_offs;
  23.  
  24.     hdr_len = IPLEN + ip->optlen;
  25.     if((bp = pushdown(data,hdr_len)) == NULLBUF){
  26.         free_p(data);
  27.         return NULLBUF;
  28.     }
  29.     cp = bp->data;
  30.     
  31.     *cp++ = (ip->version << 4) | (hdr_len >> 2);
  32.     *cp++ = ip->tos;
  33.     cp = put16(cp,ip->length);
  34.     cp = put16(cp,ip->id);
  35.     fl_offs = ip->offset >> 3;
  36.     if(ip->flags.df)
  37.         fl_offs |= 0x4000;
  38.     if(ip->flags.mf)
  39.         fl_offs |= 0x2000;
  40.  
  41.     cp = put16(cp,fl_offs);
  42.     *cp++ = ip->ttl;
  43.     *cp++ = ip->protocol;
  44.     if(cflag){
  45.         /* Use checksum from host structure */
  46.         cp = put16(cp,ip->checksum);
  47.     } else {
  48.         /* Clear checksum for later recalculation */
  49.         *cp++ = 0;
  50.         *cp++ = 0;
  51.     }
  52.     cp = put32(cp,ip->source);
  53.     cp = put32(cp,ip->dest);
  54.     if(ip->optlen != 0)
  55.         memcpy(cp,ip->options,ip->optlen);
  56.  
  57.     /* If requested, recompute checksum and insert into header */
  58.     if(!cflag)
  59.         put16(&bp->data[10],cksum(NULLHEADER,bp,hdr_len));
  60.  
  61.     return bp;
  62. }
  63. /* Extract an IP header from mbuf */
  64. int
  65. ntohip(ip,bpp)
  66. register struct ip *ip;
  67. struct mbuf **bpp;
  68. {
  69.     int16 ihl;
  70.     int16 fl_offs;
  71.     char ipbuf[IPLEN];
  72.  
  73.     if(pullup(bpp,ipbuf,IPLEN) != IPLEN)
  74.         return -1;
  75.  
  76.     ip->version = (ipbuf[0] >> 4) & 0xf;
  77.     ip->tos = ipbuf[1];
  78.     ip->length = get16(&ipbuf[2]);
  79.     ip->id = get16(&ipbuf[4]);
  80.     fl_offs = get16(&ipbuf[6]);
  81.     ip->offset = (fl_offs & 0x1fff) << 3;
  82.     ip->flags.mf = (fl_offs & 0x2000) ? 1 : 0;
  83.     ip->flags.df = (fl_offs & 0x4000) ? 1 : 0;
  84.     ip->ttl = ipbuf[8];
  85.     ip->protocol = ipbuf[9];
  86.     ip->checksum = get16(&ipbuf[10]);
  87.     ip->source = get32(&ipbuf[12]);
  88.     ip->dest = get32(&ipbuf[16]);
  89.  
  90.     ihl = (ipbuf[0] & 0xf) << 2;
  91.     if(ihl < IPLEN){
  92.         /* Bogus packet; header is too short */
  93.         return -1;
  94.     }
  95.     ip->optlen = ihl - IPLEN;
  96.     if(ip->optlen != 0)
  97.         pullup(bpp,ip->options,ip->optlen);
  98.  
  99.     return ip->optlen + IPLEN;
  100. }
  101. /* Perform end-around-carry adjustment */
  102. int16
  103. eac(sum)
  104. register int32 sum;    /* Carries in high order 16 bits */
  105. {
  106.     register int16 csum;
  107.  
  108.     while((csum = sum >> 16) != 0)
  109.         sum = csum + (sum & 0xffffL);
  110.     return (int16) (sum & 0xffffl);    /* Chops to 16 bits */
  111. }
  112. /* Checksum a mbuf chain, with optional pseudo-header */
  113. int16
  114. cksum(ph,m,len)
  115. struct pseudo_header *ph;
  116. register struct mbuf *m;
  117. int16 len;
  118. {
  119.     register int16 cnt, total;
  120.     register int32 sum, csum;
  121.     register char *up;
  122.     int16 csum1;
  123.     int swap = 0;
  124.  
  125.     sum = 0l;
  126.  
  127.     /* Sum pseudo-header, if present */
  128.     if(ph != NULLHEADER){
  129.         sum = hiword(ph->source);
  130.         sum += loword(ph->source);
  131.         sum += hiword(ph->dest);
  132.         sum += loword(ph->dest);
  133.         sum += uchar(ph->protocol);
  134.         sum += ph->length;
  135.     }
  136.     /* Now do each mbuf on the chain */
  137.     for(total = 0; m != NULLBUF && total < len; m = m->next) {
  138.         cnt = min(m->cnt, len - total);
  139.         up = (char *)m->data;
  140.         csum = 0;
  141.  
  142.         if(((long)up) & 1){
  143.             /* Handle odd leading byte */
  144.             if(swap)
  145.                 csum = uchar(*up++);
  146.             else
  147.                 csum = (int16)(uchar(*up++) << 8);
  148.             cnt--;
  149.             swap = !swap;
  150.         }
  151.         if(cnt > 1){
  152.             /* Have the primitive checksumming routine do most of
  153.              * the work. At this point, up is guaranteed to be on
  154.              * a short boundary
  155.              */
  156.             csum1 = lcsum((unsigned short *)up, (int16)(cnt >> 1));
  157.             if(swap)
  158.                 csum1 = (csum1 << 8) | (csum1 >> 8);
  159.             csum += csum1;
  160.         }
  161.         /* Handle odd trailing byte */
  162.         if(cnt & 1){
  163.             if(swap)
  164.                 csum += uchar(up[--cnt]);
  165.             else
  166.                 csum += (int16)(uchar(up[--cnt]) << 8);
  167.             swap = !swap;
  168.         }
  169.         sum += csum;
  170.         total += m->cnt;
  171.     }
  172.     /* Do final end-around carry, complement and return */
  173.     return (int16)(~eac(sum) & 0xffff);
  174. }
  175.  
  176.