Headers play a crucial role in various fields of computer science, including computer networking. They contain control information that ensures data is transmitted efficiently, accurately, and reaches its destination reliably.

In computer networking, IP datagrams (also known as packets) consist of two parts: the header and the payload (data). Each protocol follows a distinct header format for reliable data transmission and reception. This article examines the IPv4 header format and its components.

Header format for IPv4

The IPv4 header has a length of 32 bits and consists of multiple layers, as illustrated below:

IPv4 Header format
IPv4 Header Layers (Note: These layers should not be confused with OSI layers)

The header of IPv4 is 32-bit in length and its multiples for each layer as shown above. The different terms in header are explained below:

Layer 1 of Header: 32 bit in length

  • Version: This field indicates the version of IP being used, such as 4 for IPv4.
  • Internet Header Length (IHL) (4 bits): This field specifies the total length of the header in 32-bit multiples, including any options fields and padding. When no options are used, the typical value is 5 (5 32-bit words = 5*4 = 20 bytes or 20 octets).
  • DS: This field determines what service is used and measures its quality etc. also known as Differentiated Services. This is 6 bits in length.
  • ECN: The Explicit Congestion Notification field is 2 bits in length and are used to detect congestions in the network and tells the end node packets that there are congestions ahead.
  • Total Length: This tells the total length of the IP datagram including header + data. Since this is 16 bits in length so the maximum length of the IP datagram comes out to be 65,536.

Layer 2 of Header: 32 bit in length

  • Identification: This is 16 bits sequence number that is allocated to each of the fragments belonging to a particular message. This field is used by the recipient to reassemble messages without accidentally mixing fragments from different messages. This is needed because fragments may arrive from multiple messages mixed together
  • Flags: Flags control data fragmentations. It is 3 bits in length and contains 3 flags out of which two controls fragmentation and one is reserved.
  • Fragment Offset: When fragmentation of a message occurs, this field specifies the offset, or position, in the overall message where the data in this fragment goes. It is 13 bits in length.

Layer 3 of Header: 32 bit in length

  • Time to Live (8 bits): Specifies how long the datagram is allowed to “live” on the network, in terms of router hops. Each router decrements the value of the TTL field (reduces it by one) prior to transmitting it. If the TTL field drops to zero, the datagram is assumed to have taken too long a route and is discarded.
  • Protocol: Identifies what protocol is used e.g. TCP (6) or UDP (17). It is 8 bits in length.
  • Header Checksum: It is 16 bits long and use as an error checking code.

Layer 4 of Header: 32 bit in length

  • Source Address: IPv4 is 32 bit address and this layer of the header tells the source address of the originating IP datagram.

Layer 5 of Header: 32 bit in length

  • Destination Address: IPv4 is 32 bit address and this layer of the header tells the destination address of the IP datagram.

Layer 6 of Header: 32 bit in length

  • Options: Certain IP datagrams may include one or more types of options after the standard headers.
  • Padding: Padding is used to complete the 32-bit profile for options or its multiples by adding zeroes.

Layer 7 of Header:

  • Data: This field contains the data to be transmitted in the datagram.