2

Hi i'm stuck on getting ICMPv6 header from IPv6 packet.

        size = sizeof(sockaddr_in6);
        if ((lenght = recvfrom(socd, buffer, BUFSIZE, 0, (sockaddr *)&receiveSockAddr, &size)) < 0) {
          cerr << "recvfrom error" << endl;
        }

        ip = (ip6_hdr *) buffer;

        cout << "Payload length: " << ip->ip6_ctlun.ip6_un1.ip6_un1_plen << endl;
        cout << "Payload length ntohs: " << ntohs(ip->ip6_ctlun.ip6_un1.ip6_un1_plen) << endl;

        //icmpRec = (icmp6_hdr *) (buffer + ???offset???);

I need find out how to set the offset. Only way I know is to get payload length and then the: packet length - payload length = my offset. But in ip->ip6_ctlun.ip6_un1.ip6_un1_plen are some weird numbers.

From the code above im receiving:

Payload length: 25332
Payload length ntohs: 62562 

after sending ICMP6_ECHO_REQUEST to ipv6.google.com

But if I look in wireshark the Payload length is 8 bytes. How to correctly decode the payload number from uint16_t to correct decimal number 8? And how get the whole size of IPv6 packet to calculate the offset? Why IPv6 haven't IHL as IPv4? This very complicate the work aroud IPv6 I think.

macken88
  • 125
  • 11
Petr Přikryl
  • 1,641
  • 4
  • 22
  • 34
  • yes: socd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6) – Petr Přikryl Nov 06 '12 at 11:43
  • Can you please check the return value of recv_from. It tells how many bytes are actually read. We need to ensure we are reading the entire header. – fkl Nov 06 '12 at 12:07

1 Answers1

2

This might be the issue though it was a little hard for me to digest.

Appearently, the IPv6 header is automatically cropped off when recvfrom() is used.

Quote

Outgoing packets automatically have an IPv6 header prepended to them (based on the destination address). Incoming packets on the socket are received with the IPv6 header and any extension headers removed.

ipv6 man page

So basically your buffer starts from data and the bytes you are offsetting contain some random higher layer data.

Community
  • 1
  • 1
fkl
  • 5,412
  • 4
  • 28
  • 68
  • Well the man page is of ICMPv6 so it appears on raw sockets too, though i must say i don't seem to imagine why – fkl Nov 06 '12 at 12:18
  • No problem - this was really valuable for me too as i didn't suspect it to be the cause – fkl Nov 06 '12 at 12:20