Socket programming practices (16) and socket programming practices 16
Ethernet frame format
Note 1: data packets at the link layer are called Ethernet frames.
Note 2: The Link Layer does not recognize the IP Address [because the IP address is a logical address]. The link layer recognizes the MAC address of the physical Nic [hardware address].
NOTE 3: You need to find the target MAC address (ARP Address Resolution Protocol) [MAC-> ip address direction Address Resolution: RARP Reverse Address Resolution Protocol] based on the IP address.
Note 4: the application layer communicates Based on the IP address of the peer. During data encapsulation, where does the MAC address of the target address of the Link Layer come from? You need to convert the IP address to the MAC address, that is, address resolution.
Ethernet header code:
struct ethernet_hdr{ char dest_mac[6]; char src_mac[6]; short protocol;};
ARP Address Resolution Protocol
ARP header code:
Struct arp_hdr {unsigned short hwtype; // fixed 1 unsigned short protype; // fixed 0x0800 (representing requests for IP protocol) unsigned char hwaddrlen; // fixed 6 (MAC address length) unsigned char proaddrlen; // fixed 4 (IP address length) unsigned short opcode; // Request-1, reply-0x0002 unsigned char sender_mac [6]; // the sender's MAC unsigned char sender_ip [4]; // the sender's IP unsigned char dest_mac [6]; // receiver MAC unsigned char dest_ip [4]; // receiver IP };Ethernet MTU and path MTU
Both Ethernet and IEEE 802.3 have limits on the length of data frames. The Maximum values are 1500 and 1492 bytes respectively. These limits are called the Maximum Transmission Unit (MTU, Maximum Transmission Unit)
If there is a datagram to be transmitted on the IP layer and the data length is greater than the MTU on the link layer, Fragmentation is required on the IP layer to split the datagram into several slices, in this way, each piece is smaller than MTU.
When two hosts on the network communicate with each other, the two hosts need to go through multiple networks, and the link layer of each network may have different MTU, the minimum MTU in the two communication host paths is called the path MTU.
IP Datagram
Version number. Currently, the value is 4.
Length of the header, in the unit of 4 bytes. value range: 5 ~ 15
Service type, which specifies the transmission priority, transmission speed, reliability, and throughput.
Total message length, up to 65535 bytes
The message identifier, which uniquely identifies a datagram. If the datagram is segmented, the identifiers of each segment are the same.
Mark, the highest bit is not used, defined as 0, the other two are DF (not segmented) and MF (more segments)
Segment offset, in 8 bytes, indicates the offset position of the first data word in the original datagram.
TTL value: 0 ~ 255. Each route node minus 1 and is discarded when it is 0 (implementation principle of the Tranceroute Program)
Indicates the protocol type of the datagram, for example, 1 is ICMP, 4 is IP, 6 is TCP, and 17 is UDP.
The first checksum. This value must be re-calculated every time the gateway passes through it to ensure the integrity of the IP header.
Option, variable length, provides the control function required in some cases, the length of the IP header must be an integer multiple of 4 bytes, if the length of the option is not an integer multiple of 4, you must fill the data
IP header code:
struct ip_hdr{ char ver_hl; char tos; unsigned short len; unsigned short id; unsigned short fragment; char ttl; char protocol; unsigned short hdr_chksum; char src_ip [4]; char dest_ip [4];};ICMP protocol
The ICMP protocol is used to transmit control data such as error information, time, ECHO, and network information.
When we ping a host to check whether it is running, an ICMP message is generated. The remote host uses its own ICMP information to respond to the ping request.
ICMP protocol description:
It is a part of the IP.
Purpose: The gateway or target machine uses ICMP to communicate with the source, and provides feedback when a problem occurs.
Must be implemented in the IP protocol stack
The protocol type in the IP header is 1 in the net_load data of the IP packet contained in ICMP.
The first byte of ICMP data represents the type of ICMP message, which determines the format of subsequent data
Features:
Its control capability is not used to ensure the reliability of transmission.
It is not reliable in itself
ICMP Packet
The protocol type in the IP header is 1 in the net_load data of the IP packet contained in ICMP.
The first byte of ICMP data represents the type of ICMP message, which determines the format of subsequent data
ICMP header code:
Struct icmp_hdr {chartype; // ICMP message type char code; // "subtype" unsigned short icmpchksum; // checksum };
ICMP Message Type
Message type description
2. 0 Echo Reply
2. 3 Destination Unreachable
2. 4 Source Quench
2. 5. Redirect
2. 8 Echo
2. 11 Time Exceeded
2. 12 Parameter Problem
2. 13 Timestamp
2. 14 Timestamp Reply
2. 15 Information Request
2. 16 Information Reply
2. 17 Address Mask Request
2 18 Address Mask Reply
Packet Classification
2. ICMP error message
2. Destination inaccessible packets (Type 3)
2. Timeout message (Type 11)
2. parameter error message (Type 12)
2. ICMP control packets
2. Source message suppression (Type 4)
2. Redirection (type 5)
2. ICMP request/response packets
2. Send back request and Response Message (Type 0 and 8)
2. timestamp request and response packets (types 13 and 14)
2. Address Mask Request and response packets (type 17 and 18)
2. Others
ICMP Echo packets
Type: 0 indicates Echo Reply, 8 indicates Echo
Code: 0
Identifier: identifies a session, for example, process ID
Serial number: for example, add 1 for each request
Option data: Echo
ICMP Destination inaccessible packets
Type: 3
Code: 0 indicates that the network is inaccessible, 1 indicates that the host is inaccessible, 2 indicates that the Protocol is inaccessible, 3 indicates that the port is inaccessible, and so on.
The IP header of the wrong IP packet + the first eight bytes of the original IP packet