[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: ip checksum (offtopic)



Tim locke wrote:
> 
> Hey, Can someone show me an working example on how ip
> checksum is computed? I'm having a hard time figuring
> it out...
> 
> thanks
> 
> please CC me if you guys don't mind
> 
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Sports - live college hoops coverage
> http://sports.yahoo.com/
> 
> --
> To UNSUBSCRIBE, email to debian-user-request@lists.debian.org
> with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Here's one I pulled off Google:

/*
**************************************************************************
Function: ip_sum_calc
Description: Calculate the 16 bit IP sum.
***************************************************************************
*/
typedef unsigned short u16;
typedef unsigned long u32;

u16 ip_sum_calc(u16 len_ip_header, u16 buff[])
{
u16 word16;
u32 sum=0;
u16 i;
    
        // make 16 bit words out of every two adjacent 8 bit words in
the packet
        // and add them up
        for (i=0;i<len_ip_header;i=i+2){
                word16 =((buff[i]<<8)&0xFF00)+(buff[i+1]&0xFF);
                sum = sum + (u32) word16;       
        }
        
        // take only 16 bits out of the 32 bit sum
        while (sum>>16)
          sum = (sum & 0xFFFF)+(sum >> 16);

        // one's complement the result
        sum = ~sum;
        
return ((u16) sum);
}

Which has the URI:

http://www.netfor2.com/ipsum.htm
-- 
http://www.eskimo.com/~xeno
xeno@eskimo.com
Physically I'm at:  5101 N. 45th St., Tacoma, WA, 98407-3717, U.S.A.



Reply to: