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