Main Page | Directories | File List | Globals

ip_checksum.c

Go to the documentation of this file.
00001 /*
00002  * kernel_net/layer_3/ip/ip_checksum.c
00003  *
00004  * Copyright (c) 2003-2005 Lukasz Dembinski <dembol@nasa.com.pl>
00005  * All Rights Reserved
00006  * 
00007  * Date:        2005/06
00008  * Author:      Lukasz Dembinski
00009  * Info:        ip_checksum.c core file
00010  * Contact:     mailto: <dembol@nasa.com.pl>
00011  *
00012  */
00013 
00014 #include <agnix/agnix.h>
00015 #include <agnix/net/net.h>
00016 
00017 u16 ip_checksum(struct ip_hdr_s *ip)
00018 {
00019     u32 sum;       
00020     u16 word;
00021     u16 *ptr;
00022     int len;
00023     
00024     ptr = (u16 *)ip;
00025     
00026     sum = 0;
00027     for (len = sizeof(struct ip_hdr_s); len > 1; len -= 2, ptr++) {
00028         sum += *ptr;
00029     }
00030 
00031     if (len == 1) {
00032         word = 0;     
00033         *((u8 *) &word) = *(u8 *)ptr;
00034         sum += word;
00035     }
00036 
00037     sum  = (sum >> 16) + (sum & 0xffff);
00038     sum += (sum >> 16);
00039    
00040     return (u16)(~sum);
00041 };
00042 
Dokumentacje wygenerowano programem Doxygen 1.4.2 dla projektu Agnix