The Internet Protocol header is the initial portion of an IP packet that contains information about the packet’s source and destination, as well as other data such as the packet’s time to live (TTL). The header is followed by the payload, which is the data being carried by the packet.
The header consists of a number of fields, each of which is a fixed length. The fields are used to indicate the packet’s source and destination IP addresses, as well as other data such as the TTL. The header also contains a checksum that is used to ensure that the packet has not been damaged in transit.
The format of the IP header is defined in RFC 791. The header is divided into a number of 32-bit words, each of which is represented by four bytes. The first word is the version field, which indicates the version of the IP protocol being used. The second word is the header length field, which indicates the length of the header in 32-bit words.
The third and fourth words are the source and destination IP addresses, respectively. The fifth word is the TTL field, which indicates how many hops the packet can take before it is discarded. The sixth word is the protocol field, which indicates the type of data that is carried in the payload.
The seventh and eighth words are the header checksum fields. The ninth word is the source port field, which indicates the port to which the packet was sent. The tenth word is the destination port field, which indicates the port to which the packet is to be delivered.
The eleventh and twelfth words are the sequence and acknowledgment number fields. These fields are used for error checking and flow control. The thirteenth word is the window size field, which indicates the number of bytes that can be sent in the payload.
The fourteenth word is the checksum field, which is used to verify the integrity of the header. The fifteenth and final word is the urgent pointer field, which is used to indicate where in the payload the urgent data is located.
The header checksum is calculated by taking the one’s complement of the one’s complement sum of all 16-bit words in the header. The urgent pointer is only used if the URG bit is set in the flags field. If this bit is not set, the urgent pointer field is ignored.
Detail of IP header components:
Version: The version field indicates the format of the IP header. The current version is 4.
Header Length: The header length field indicates the length of the header in 32-bit words. The minimum value for this field is 5, which indicates a header length of 20 bytes.
Flags: The flags field is used to control the fragmentation of IP datagrams. The three bits in this field are:
DF: This bit is set to 1 if the datagram should not be fragmented.
MF: This bit is set to 1 if the datagram is more fragments.
RF: This bit is set to 1 if the datagram should be routed.
fragment offset: This field indicates the offset of this fragment in the original datagram. The offset is measured in units of 8 bytes.
Time to Live: The time to live (TTL) field indicates how long the datagram can stay in the network before it is discarded. The TTL is decremented by one every time the datagram passes through a router. When the TTL reaches 0, the datagram is discarded and an error message is sent to the source.
Protocol: The protocol field indicates the type of data that is carried in the payload. The most common values for this field are 1 (ICMP) and 6 (TCP).
Header Checksum: The header checksum field is used to verify the integrity of the header. The checksum is calculated by taking the one’s complement of the one’s complement sum of all 16-bit words in the header.
Source IP Address: The source IP address field indicates the IP address of the datagram’s sender.
Destination IP Address: The destination IP address field indicates the IP address of the datagram’s intended recipient.
Options: The options field is used to control the way in which the datagram is routed. The most common option is the Record Route option, which causes each router that handles the datagram to insert its own IP address into the options field.
Padding: The padding field is used to ensure that the header is a multiple of 32 bits long. The padding is added at the end of the header and consists of zeros.
The payload is the data that is carried in the datagram. The payload can be anything from an ICMP message to a TCP segment.
IP Header Calculation:
Version: 4
Header Length: 5 (20 bytes)
Flags: 0
fragment offset: 0
Time to Live: 64
Protocol: 1 (ICMP)
Header Checksum: 0x4c59
Source IP Address: 192.168.1.100
Destination IP Address: 192.168.1.254
Options: 0x0
Padding: 0x0
The payload is the data that is carried in the datagram. The payload can be anything from an ICMP message to a TCP segment.
In this example, the payload is an ICMP message.
The header checksum is calculated by taking the one’s complement of the one’s complement sum of all 16-bit words in the header. The urgent pointer is only used if the URG bit is set in the flags field. If this bit is not set, the urgent pointer field is ignored.
ICMP Header:
Type: 8 (Echo Request)
Code: 0
Checksum: 0x4c59
Identifier: 0
Sequence Number: 0
Data: The data field is used to carry the data that is to be sent in the echo request. In this example, the data field contains the string “hello world”.
The checksum is calculated by taking the one’s complement of the one’s complement sum of all 16-bit words in the header and data.