A summary of IPv4 and IPv6 is that they provide the user with unique IP addresses, which enable devices to communicate through the internet. Let's discuss IPv4 and IPv6 headers in detail.
The IPv4 header consists of 13 fields, and there is the data that needs to be sent. A standard IPv4 header is shown below:
Version: This is a 4-bit field describing the IP type used in the packet.
Header length: Header length, also called IHL (Internet Header Length), is a 4-bit field that tells us about the length of the IPv4 header. The size of the IPv4 header is not always the same, which is why this field is used.
TOS: TOS stands for Type of Service. It is an 8-bit field. This field explains the type of service required for the packet. The most common type of services is reliability, precedence, throughput, and delay.
Total Length: It is a 16-bit field that indicates the total size of the packet. The highest value a 16-bit field can store is 65535, which can be the maximum size of the packet.
Identification: It is a 16-bit field that tells us about the identification number of a packet. If the data being sent cannot be sent in one packet, the source device fragments the packet and assigns the pieces the same identification number so the device which receives the packets can reassemble the fragment to get the entire data.
Flag: This is a 3-bit field that enables fragmentation. This first bit in this field is always set to 0. The second bit explains whether the packet should be fragmented. The last bit indicates whether more fragments are left after the current fragment.
Offset: This is known as the fragmentation offset. It is a 13-bit field that determines to which part of the original packet this fragment belongs.
TTL: TTL stands for Time to Live. It is an 8-bit field that indicates the amount of time or the number of hops a packet can have before it is discarded.
Protocol: This is an 8-bit field that specifies the upper layer protocol. It tells the receiver device how to process this packet. An example of an upper-layer protocol would be UDP.
Checksum: This is a 16-bit field. The checksum is calculated at every router hop. It is used to check the validity of the header. So, if the checksum verification at any router fails, that means the header is corrupted, and so the packet is discarded.
Source address: This field is used to specify the 32-bit IP address of the source device.
Destination address: This field is used to specify the 32-bit IP address of the destination device.
Options: This is an optional field in the header, and its max size is 40 bytes. It is not commonly used. It can be used to specify options like the path the packet needs to take.
The IPv6 header consists of 8 fields. The address size being used in the IPv6 header is about four times bigger than the IPv4 header, but the header size is just double the size of the IPv4 header. A standard IPv6 header is shown below:
Version: This is a 4-bit field that describes the IP type that is being used in the packet.
Traffic class: This is an 8-bit field. This is similar to the TOS field in the IPv4 header. The first 6-bits represent the service required for this packet, and the last 2-bits are used for ECN (Explicit Congestion Notification).
Flow label: This is a 20-bits field. It is used to label the packets belonging to the same sequence. It's to ensure that the packets are delivered in order. This field is mainly used to deliver real-time data such as audio or video. So, this field ensures that they are delivered in order.
Payload length: This is a 16-bit field that indicates the size of the packet. The highest value a 16-bit field can store is 65535, which can be the maximum size of the packet. If the packet exceeds that size, this field is set to 0.
Next header: This is an 8-bit field that indicates the type of the first extension header, or in some cases, it just specifies the upper layer protocol, such as UDP or TCP.
Hop limit: This is the same as the TTL field in the IPv4 header. It indicates the number of hops a packet can take before being discarded.
Source address: This field is used to specify the 128-bit IP address of the source device.
Destination address: This field is used to specify the 128-bit IP address of the destination device.
There is a difference in the number of fields used in IPv4 and IPv6, which indicates that IPv6 doesn't use fields like flags or checksums in its header. Another difference is the size of the header. In IPv4, the header size can range from 20 to 60 bytes depending on the options field, but in IPv6, the header size is fixed at 40 bytes. But the main difference between both of them is the address size. IPv4 has 32-bit addresses so it can generate 4.29 * 10^9 addresses. IPv6 has 128-bit addresses, so it can generate 3.4 * 10^38 addresses. There are other differences in the protocols, but these are the main differences in the headers of both protocols.