mercredi 7 janvier 2015

Implementation of calculate ip checksum is different from my original exapmle

I want to implement IP header checksum and fount this:



public static ushort InternetChecksum(this IEnumerable<byte> value)
{
byte[] buffer = value.ToArray();
int length = buffer.Length;
int i = 0;
UInt32 sum = 0;
UInt32 data = 0;
while (length > 1)
{
data = 0;
data = (UInt32)(
((UInt32)(buffer[i]) << 8)
|
((UInt32)(buffer[i + 1]) & 0xFF)
);

sum += data;
if ((sum & 0xFFFF0000) > 0)
{
sum = sum & 0xFFFF;
sum += 1;
}

i += 2;
length -= 2;
}

if (length > 0)
{
sum += (UInt32)(buffer[i] << 8);
//sum += (UInt32)(buffer[i]);
if ((sum & 0xFFFF0000) > 0)
{
sum = sum & 0xFFFF;
sum += 1;
}
}
sum = ~sum;
sum = sum & 0xFFFF;
return (UInt16)sum;
}


So i took this implementation and calculate checksum of specific Packet from PCAP file. I send to this function all 20 bytes of the IPv4 layer and the result was 10 and different from the PCAP file (e2 5c) Maybe this calculation is wrong ?





Aucun commentaire:

Enregistrer un commentaire