home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / s920603.zip / IPHDR.C < prev    next >
C/C++ Source or Header  |  1992-04-08  |  4KB  |  178 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,bp,cflag)
  15. register struct ip *ip;
  16. struct mbuf *bp;
  17. int cflag;
  18. {
  19.     int16 hdr_len;
  20.     register char *cp;
  21.     int16 fl_offs;
  22.  
  23.     hdr_len = IPLEN + ip->optlen;
  24.     if(hdr_len > IPLEN + IP_MAXOPT)
  25.         hdr_len = IPLEN + IP_MAXOPT;
  26.     bp = pushdown(bp,hdr_len);
  27.     cp = bp->data;
  28.     
  29.     *cp++ = (ip->version << 4) | (hdr_len >> 2);
  30.     *cp++ = ip->tos;
  31.     cp = put16(cp,ip->length);
  32.     cp = put16(cp,ip->id);
  33.     fl_offs = ip->offset >> 3;
  34.     if(ip->flags.congest)
  35.         fl_offs |= 0x8000;
  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,min(ip->optlen,IP_MAXOPT));
  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.     int 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->flags.congest = (fl_offs & 0x8000) ? 1 : 0;
  85.     ip->ttl = ipbuf[8];
  86.     ip->protocol = ipbuf[9];
  87.     ip->checksum = get16(&ipbuf[10]);
  88.     ip->source = get32(&ipbuf[12]);
  89.     ip->dest = get32(&ipbuf[16]);
  90.  
  91.     ihl = (ipbuf[0] & 0xf) << 2;
  92.     if(ihl < IPLEN){
  93.         /* Bogus packet; header is too short */
  94.         ip->optlen = 0;
  95.         return -1;
  96.     }
  97.     if ( (ip->optlen = ihl - IPLEN) != 0 ) {
  98.         if ( pullup(bpp,ip->options,ip->optlen) < ip->optlen )
  99.             return -1;
  100.     }
  101.     return ihl;
  102. }
  103. /* Perform end-around-carry adjustment */
  104. int16
  105. eac(sum)
  106. register int32 sum;    /* Carries in high order 16 bits */
  107. {
  108.     register int16 csum;
  109.  
  110.     while((csum = sum >> 16) != 0)
  111.         sum = csum + (sum & 0xffffL);
  112.     return (int16) (sum & 0xffffl);    /* Chops to 16 bits */
  113. }
  114. /* Checksum a mbuf chain, with optional pseudo-header */
  115. int16
  116. cksum(ph,m,len)
  117. struct pseudo_header *ph;
  118. register struct mbuf *m;
  119. int16 len;
  120. {
  121.     register int16 cnt, total;
  122.     register int32 sum, csum;
  123.     register char *up;
  124.     int16 csum1;
  125.     int swap = 0;
  126.  
  127.     sum = 0l;
  128.  
  129.     /* Sum pseudo-header, if present */
  130.     if(ph != NULLHEADER){
  131.         sum = hiword(ph->source);
  132.         sum += loword(ph->source);
  133.         sum += hiword(ph->dest);
  134.         sum += loword(ph->dest);
  135.         sum += uchar(ph->protocol);
  136.         sum += ph->length;
  137.     }
  138.     /* Now do each mbuf on the chain */
  139.     for(total = 0; m != NULLBUF && total < len; m = m->next) {
  140.         cnt = min(m->cnt, len - total);
  141.         up = (char *)m->data;
  142.         csum = 0;
  143.  
  144.         if(((long)up) & 1){
  145.             /* Handle odd leading byte */
  146.             if(swap)
  147.                 csum = uchar(*up++);
  148.             else
  149.                 csum = (int16)(uchar(*up++) << 8);
  150.             cnt--;
  151.             swap = !swap;
  152.         }
  153.         if(cnt > 1){
  154.             /* Have the primitive checksumming routine do most of
  155.              * the work. At this point, up is guaranteed to be on
  156.              * a short boundary
  157.              */
  158.             csum1 = lcsum((unsigned short *)up, (int16)(cnt >> 1));
  159.             if(swap)
  160.                 csum1 = (csum1 << 8) | (csum1 >> 8);
  161.             csum += csum1;
  162.         }
  163.         /* Handle odd trailing byte */
  164.         if(cnt & 1){
  165.             if(swap)
  166.                 csum += uchar(up[--cnt]);
  167.             else
  168.                 csum += (int16)(uchar(up[--cnt]) << 8);
  169.             swap = !swap;
  170.         }
  171.         sum += csum;
  172.         total += m->cnt;
  173.     }
  174.     /* Do final end-around carry, complement and return */
  175.     return (int16)(~eac(sum) & 0xffff);
  176. }
  177.  
  178.