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