Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining size of IP header. Why is it different from others?

Look at this code segment:

if(ip_header->protocol == IPPROTO_TCP)
 {
   tcp_header = (struct tcphdr*)(packet + sizeof(struct ethhdr) + ip_header->ihl*4);

    /* Print the Dest and Src ports */

   printf("Source Port: %d\n", ntohs(tcp_header->source));
   printf("Dest Port: %d\n", ntohs(tcp_header->dest));

  }

What i am confused is, in case of determining the size of other headers we normally do, sizeof(struct tcphdr) or sizeof(struct ethhdr) but for IP header size, we don't do sizeof instead we do ip_header->ihl*4. Why is it so? Also what is this 4 for?

Here is how the struct declaration of IP heder:

00116 struct iphdr {
00117 #if defined(__LITTLE_ENDIAN_BITFIELD)
00118         __u8    ihl:4,
00119                 version:4;
00120 #elif defined (__BIG_ENDIAN_BITFIELD)
00121         __u8    version:4,
00122                 ihl:4;
00123 #else
00124 #error  "Please fix <asm/byteorder.h>"
00125 #endif
00126         __u8    tos;
00127         __u16   tot_len;
00128         __u16   id;
00129         __u16   frag_off;
00130         __u8    ttl;
00131         __u8    protocol;
00132         __u16   check;
00133         __u32   saddr;
00134         __u32   daddr;
00135         /*The options start here. */
00136 };
like image 965
RajSanpui Avatar asked Jan 19 '12 12:01

RajSanpui


People also ask

How is IP header size calculated?

MAXIMUM HL value HL is a 4 bit field.so the maximum vale that can be accomadated in that field is 15(1111) or you can calculate using the formulae 2^4-1=15.So max no: of 4 bytes can be 15. Hence the Max header length=15*4=60bytes.

What are the most important fields in the IP header and why are they important?

Source and Destination IPv4 Address fields are the most important fields of IPv4 header. The "Source IP Address" is the 32-bit size IPv4 address of the device which sends this Internet Protocol (IPv4) Datagram.

What is the meaning of header size of an IP packet?

Each IP packet contains both a header (20 or 24 bytes long) and data (variable length). The header includes the IP addresses of the source and destination, plus other fields that help to route the packet. The data is the actual content, such as a string of letters or part of a webpage.


1 Answers

It's a problem of different units of measurement. The IHL field in the IP header is defined like this:

Internet Header Length is the length of the internet header in 32 bit words.

And this size isn't fixed (because of valid but discouraged IP options). So one packet could have IHL=5, the next could have IHL=6 and so on.

like image 166
cnicutar Avatar answered Sep 30 '22 16:09

cnicutar