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 + 8

Dim iAsInteger

Dim chksmAsInteger = 0

For i = 0To packetsize - 1Step 2

chksm += Convert.ToInt32(data(i) * &H100 + data(i + 1))

Next

chcksm = (chcksm >> 16) + (chcksm & 0xffff);
chcksm += (chcksm >> 16);
chksm =Not (chksm)

Thanks

[2029 byte] By [Christos1978] at [2007-12-24]
# 1
The

way you combine the upper 16 bits, twice, with the lower 16 bits

doesn't look right. Try to find some open source Linux or BSD

code as a sample, there are special rules too for IPV6 checksums

(translate 0 to 0xffff).
Of course, you are re-inventing the wheel big time. Windows has the Icmp6SendEcho2

API function to properly format and dispatch an echo. Not easy to

use from VB.NET though. But then, nothing will be easy from

VB.NET when you deal with low-level IP protocols...

nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

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 Integer

Dim i As Integer = 0

Dim chk As Integer = 0

Do While i < data_len

i += 1

If i Mod 2 = 0 Then

chk += Convert.ToInt64(data(i))

Else

chk += Convert.ToInt64((data(i) << 8))

End If

Loop

chk = (chk & &HFFFF) + (chk >> 16)

chk = htons(Not chk)

Return chk

End Function

Christos1978 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...