[Network] detailed description and definition of IP header, TCP header, and UDP Header

Source: Internet
Author: User

 

An IP packet is also called an IP packet group. It is transmitted at the network layer in the layer-7 structure of the ISO network. It consists of an IP packet header and IP packet user data, the length of an IP packet header is generally between 20 and 60 bytes, and the maximum length of an IP packet cannot exceed 65535 bytes.
It is the packet header format of the IP Group. The first 20 bytes of the packet header are fixed and the latter is variable.


Version: occupies 4 bits, indicating the version number of the IP protocol. The current major version is IPv4, or 4th. Some education networks and research institutions are using IPV6. During communication, the version numbers of the IP protocol must be the same for both parties. Otherwise, direct communication cannot be performed.
Header Length: 4 bits, indicating the length of the IP packet header. The maximum length (when 4 bits are 1) is 15 length units, each of which is 4 bytes (TCP/IP standard, doubleword ), therefore, the maximum length of the IP header is 60 bytes, and the minimum length is 20 bytes.
service type: eight bits are used to obtain better services. The first three digits indicate the priority of the message, and the subsequent digits indicate that the request is subject to lower latency, higher throughput, higher reliability, and lower routing costs. If the bitwise is 1, there are corresponding requirements. If the bitwise is 0, there is no requirement.
total length: 16 bits, which indicates the total length of a message. Note that the unit here is byte rather than 4 bytes, so the maximum length of an IP packet is 65535 bytes.
Identification: this field marks the current part as the first divided part, which is useful in data report reorganization.
flag: this field is used to mark whether the message is sharded (some may not need to be sharded or do not want to be sharded ), whether there are still parts (whether it is the last part ).
slice offset: the offset of the current slice relative to the user data field in the original datagram (the datagram before the slice), that is, the relative position in the original datagram.
TTL (time to live ). This field indicates how long the current message can survive. After 1 ms or a gateway, the TTL value is automatically reduced by 1. When the survival time is 0, packets are considered to be inaccessible to the target host and discarded. Users who have used the ping command should be impressed. In Windows, enter the ping command, and the returned result contains the value of TTL.
Protocol: this field indicates the protocol used on the upper layer (Network Layer 7 structure or TCP/IP Transport Layer, possible protocols include UDP, TCP, ICMP, IGMP, and IGP.
header checksum: used to check whether an error occurs in the IP packet header during transmission. It mainly checks whether one or several bits in the packet header are contaminated or modified.
Source IP Address: 32-bit, 4 bytes, each of which is 0 ~ An integer between 255 and the IP address format we see on a daily basis.
Destination IP Address: 32-bit, 4 bytes, each of which is 0 ~ An integer between 255 and the IP address format we see on a daily basis.

Because delphi does not have the concept of the bitfield, the entire structure can only be defined in bytes. It is very easy to define and use the bitfield in C ++ or Erlang.
// IP package
Tipheader = packed record
Iph_verlen: byte; // version and length
Iph_tos: byte; // service type
Iph_length: word; // The total length. Only 65535 of 2 unsigned bytes can be entered.
Iph_id: word; // ID
Iph_offset: word; // flag and segment offset
Iph_ttl: byte; // survival time
Iph_protocol: byte; // Protocol
Iph_xsum: word; // header checksum
Iph_src: longword; // Source Address
Iph_dest: longword; // Destination Address
End;

What is the purpose of this struct? In fact, it is useful for sniffing.

TCP packet header

Typedef struct _ tcp_header {
Ushort nsourport; // The source port number is 16bit.
Ushort ndestport; // the destination port number is 16bit.
Uint nsequnum; // serial number 32bit
Uint nacknowledgenum; // Confirmation No. 32bit
Ushort nhlenandflag; // The first 4 digits: TCP Header Length; middle 6 digits: retained; last 6 digits: Flag 16bit
Ushort nwindowsize; // The window size is 16 bits.
Ushort nchecksum; // check 16 Bit
Ushort nrgentpointer; // The emergency data offset of 16bit.
} Tcp_header, * ptcp_header;

UDP packet header

Typedef struct _ udp_header {
Ushort nsourport; // The source port number is 16bit.
Ushort ndestport; // the destination port number is 16bit.
Ushort nlength; // The packet length is 16 bits.
Ushort nchecksum; // The Checksum is 16bit.
} Udp_header, * pudp_header;

The process of entering the Protocol Stack: (the opposite is true from the protocol stack)

Calculation of ICMP header and packet checksum

TCP/IP 11:13:51 read 305 comments 0 font size: large, medium, and small subscriptions

//// // Define the ICMP packet header
Typedef struct _ icmp_header {
Byte btype; // type 8bit
Byte bcode ;//Code8bit
Ushort nchecksum; // The Checksum is 16bit.
Ushort NID; // ID, id16bit OF THE PROCESS
Ushort nsequence; // serial number 16bit
Uint ntimestamp; // optional. Time is used to calculate the time of 32bit.
} Icmp_header, * picmp_header;

/////////////////////////////////

When sending ICMP packetsProgramCalculate the checksum by yourself and fill it in the domain corresponding to the ICMP header. The Checksum calculation method is as follows:

The data is accumulated into a double byte (strong conversion double byte type) in units of words (16 bits). If the data length is odd (odd byte ), the last byte is extended to the word, and the result is a double word,

Finally, after adding the high 16 bits and the low 16 bits of the double character, we get the checksum!

// Calculate the ICMP packet check Value
// Parameter 1: ICMP Packet Buffer
// Parameter 2: ICMP Packet Length
Ushort getchecksum (lpbyte lpbuf, DWORD dwsize)
{
DWORD dwchecksum = 0;
Ushort * lpword = (ushort *) lpbuf;

// Accumulate
While (dwsize> 1)
{
Dwchecksum + = * lpword ++;
Dwsize-= 2;
}

// If the length is odd
If (dwsize = 1)
Dwchecksum + = * (lpbyte) lpword );

// Add 16-bit high and 16-bit low
Dwchecksum = (dwchecksum> 16) + (dwchecksum & 0 xFFFF );

// Reverse
Return (ushort )(~ Dwchecksum );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.