-->

Get exact size of IPv6 header including the extens

2019-06-14 03:22发布

问题:

If IPv4 is in question and I want to extract IP and ICMP header out of std::istream, first I get the initial 20 bytes, then check if the header lenght provided in the IPv4 header is larger than 20 bytes in order to extract any options. The next bytes are the ICMP packet. Using the header lenght value carried inside the IPv4 header I can see the expect size of the IP header.

However how to get the exact size of IPv6 header? There is a payload value inside IPv6 header that includes the size of header extensions plus higher level data such as ICMP. I need to know the size of IPv6 header including the header extensions but without higher level data, in order to know at what position the ICMPv6 header starts in the std::istream.

Thank you!

回答1:

The size of the IPv6 header is fixed at 40 bytes - though as you know there may be extensions that follow the initial IPv6 header that aren't part of the transport layer datagram. To determine whether the IPv6 header is followed by header extensions, check the 'next header' field. The value stored in this field will tell you whether the next header is a transport level header (IE TCP/UDP header), a IP level header extension (IE ICMP), or anything in between. This list gives a list of all the possible values for the next header field.

Once you determine the type of the next header, you can then deal with it accordingly - the first byte of all extension headers should be the 'next header' field so that they can be chained together, and if the extension header is variable length its second byte should be the 'hdr ext field' which can be used to determine its size.