CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2017
    Posts
    9

    [RESOLVED] ICMPv6 Checksum

    Hey there,
    I am trying to figure out how to calculate the [ICMPv6 checksum](https://tools.ietf.org/html/rfc4443#section-2.3).

    Let's assume I have an IPv6 packet like this:

    Source IP: fe 80 00 00 00 00 00 00 be ae c5 ff fe df 31 d1

    Destination IP: ff 02 00 00 00 00 00 00 00 00 00 00 00 00 00 01

    Payload length: 00 18

    Next header: 3a

    ICMPv6: 80 00 00 c8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

    What I am doing wrong when performing this calculation:
    sum of:
    fe80
    0
    0
    0
    beae
    c5ff
    fedf
    31d1
    ff02
    0
    0
    0
    0
    0
    0
    1
    18
    3a
    8000
    now moving the bytes and adding them to the end

    Code:
    sum += sum >> 16;
    result now is 3337
    inverting the value

    Code:
    sum = ~sum;
    result is c800
    wireshark says it should be ccc8

    I can't find the mistake here, can anyone please take a look at it?
    Thanks and have a nice day!

    Here is the wireshark screenshot:
    http://i.imgur.com/Y7HaDZS.jpg

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: ICMPv6 Checksum

    What is the type of sum? Consider
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	short int chk = 0x3337;
    
    	short int inv = ~chk;
    
    	cout << hex << "0x" << chk << " 0x" << inv << endl;
    }
    which displays
    Code:
    0x3337 0xccc8
    Last edited by 2kaud; March 2nd, 2017 at 06:49 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: ICMPv6 Checksum

    From the data in post #1,
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	const unsigned short int bytes[] = { 
    		0xfe80, 0x0000, 0x0000, 0x0000, 0xbeae, 0xc5ff, 0xfedf, 0x31d1,
    		0xff02, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001,
    		0x0018,
    		0x003a,
    		0x8000};
    
    	unsigned int sum = 0;
    
    	for (auto i : bytes)
    		sum += i;
    
    	while (sum & 0xffff0000) 
    		sum = (sum >> 16) + (sum & 0xffff);
    
    	sum = ~sum;
    
    	cout << hex << "0x" << (unsigned short) sum << endl;
    }
    displays
    Code:
    0xccc8
    Last edited by 2kaud; March 2nd, 2017 at 11:29 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Mar 2017
    Posts
    9

    Re: ICMPv6 Checksum

    Quote Originally Posted by 2kaud View Post
    From the data in post #1,
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	const unsigned short int bytes[] = { 
    		0xfe80, 0x0000, 0x0000, 0x0000, 0xbeae, 0xc5ff, 0xfedf, 0x31d1,
    		0xff02, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001,
    		0x0018,
    		0x003a,
    		0x8000};
    ....

    Thank you so much for this check. The problem was in the data type but this has really helped me!

  5. #5
    Join Date
    Mar 2017
    Posts
    9

    Re: ICMPv6 Checksum

    Quote Originally Posted by 2kaud View Post
    What is the type of sum? Consider
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	short int chk = 0x3337;
    
    	short int inv = ~chk;
    
    	cout << hex << "0x" << chk << " 0x" << inv << endl;
    }
    which displays
    Code:
    0x3337 0xccc8
    Many thanks. Now I feel really dumb. It was the data type after all...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured