Problem calculating icmp packet checksum
Hi all,
I am trying to create code to generate the checksum for an Icmpv6 packet. I am using vb.net to manually create an echo request icmp message and then use a socket to send and receive the replies. My problem is that the resulting checksum is wrong according to captured packets from ethereal.
This code is part of my dissertation and I am desperate to find a solution. Any help will be really appreciated. The checksum code is below:
Public Sub getChecksum()Dim data()AsByte = getBytes()Dim packetsizeAsInteger = MessageSize + 8Dim iAsIntegerDim chksmAsInteger = 0
For i = 0To packetsize - 1Step 2chksm += Convert.ToInt32(data(i) * &H100 + data(i + 1))
Nextchcksm = (chcksm >> 16) + (chcksm & 0xffff);
chcksm += (chcksm >> 16);
chksm =Not (chksm)Thanks
Hi nobugz,
Thanks for the prompt answer. I have had a look around and managed to find the some code. Reading the IPv6 rfc, a read that the ICMPv6 checksum must also include a pseudo-header with a next-header value of 58. I couldn't find anywhere though information regarding the 0 to 0xffff translation. On the matter of reinventing the wheel, i know that .Net has some build in functions but my problem is that I have to create a series of different ICMPv6 packets (i.e router discovery, Pmtu, etc) on byte level. Any input on that or even links that could point me to the right direction will be highly appreciated. The code I was talking about is listed below:
Public Function calculate_checksum(ByVal data() As Byte, ByVal data_len As Integer) As IntegerDim i As Integer = 0Dim chk As Integer = 0Do While i < data_leni += 1
If i Mod 2 = 0 Thenchk += Convert.ToInt64(data(i))
Elsechk += Convert.ToInt64((data(i) << 8))
End IfLoopchk = (chk & &HFFFF) + (chk >> 16)
chk = htons(
Not chk)Return chkEnd Function