The original address

preface

In addition to whether the browser can access a web page, we often judge whether the local network is smooth, network delay, packet loss, etc., by Ping the terminal. For example:

Ping gets its name from the sound that sonar makes when it detects it.

So how exactly does Ping work?

About how Ping works

Before introducing Ping, it is necessary to introduce TCP/IP reference models, namely, application layer, transport layer, Internet layer (network layer), and network access layer (host-network layer). The network layer is mainly responsible for host-to-host communication. Among the protocols at the network layer, IP is the first one we think of. In fact, there is an important Protocol at the network layer, ICMP. The Ping program works on ICMP.

Of course, there are other more important network layer protocol (IGMP), interested in you can check, other protocols are not discussed in this article, hey hey

ICMP

The role in the network

In the network world, all kinds of transmission is very complex, so in this complex environment of network packet transmission, may often encounter problems, such as network unreachable, host unreachable, timeout and so on a series of problems, when the problem that how to do, directly destroy? If the client does not know what is happening, the client needs to be informed of the problem with the message, so that the client can adjust the transmission policy.

In the above mentioned ICMP return mechanism, there is a very important function, that is, detection, the purpose is to enable us to detect the network connection status, to ensure the accuracy of the connection.

ICMP is a very important protocol in the network hahaha.

ICMP Message Format

The first four bytes of all ICMP packets are the same, but the remaining bytes are different. Type fields can have up to 15 different values to describe a particular type of ICMP message, and some ICMP messages use code field values to further describe different conditions.

The checksum field overwrites the entire ICMP packet (this field is used to verify and prevent data forgery).

ICMP Packet type

The following table lists all types of ICMP packets. The types are determined by the type fields and code fields in the packets.

type code describe The query error
0 0 Echo response (Ping response) ,
3 Out of reach ,
4 0 The source is shut down. Procedure ,
5 redirect ,
8 0 Request Echo (Ping Response) ,
9 0 Router notification ,
10 0 Router request ,
11 timeout ,
12 Parameters of the problem ,
13 0 Timestamp request ,
14 0 Timestamp reply ,
15 0 Information request ,
16 0 Information reply ,
17 0 Address mask request ,
18 0 Address mask reply ,

The last two columns in the table indicate whether an ICMP packet is a query packet or an error packet. Because ICMP error packets sometimes require special processing, they need to be distinguished.

When an ICMP error packet is sent, the packet always contains the IP header and the first 8 bytes of the IP datagram that generated the ICMP error packet. In this way, the module that receives an ICMP error message associates it with a particular protocol and user process.

The Ping program mainly uses types 0 and 8, that is, echo response and request echo.

Relationship with other packets

ICMP is usually considered a part of the IP layer that transmits error messages and other information requiring attention. ICMP is commonly used by IP layer and higher protocols, such as ICMP transmitted within IP datagrams

View the Ping process by capturing packets

You can use the Wireshark to capture packets.

On the terminal, run ping blog.sayhe110.cn

In the Wireshark, you can view the request and response. The response type of each request is 8 (echo request) and the response type is 0 (echo response).

After the execution, there is a request (not my operation), in response to a network unreachable request, the specific information is that the network port is unreachable (what an unexpected surprise ha ha ha ha, just can see the exception scenario response type and code).

Use Go to implement a simple Ping program (EasyPing)

After knowing that Ping is based on ICMP protocol, we can also implement a simple Ping program. Go is chosen here because I am learning Go recently, as a training project. Of course, if you are interested, you can also implement it in other languages.

Because space is limited, you can directly go to see the complete source code (any questions welcome to issue) github.com/SayHe110/ea… Star!!!!!! Please give a Star 🙁

conclusion

At the network layer, ICMP is responsible for detecting the network and retrieving the information returned from the request. It uses the Type and Code to distinguish different ICMP packets. Ping program is a typical program based on ICMP protocol.

reference

  • TCP/IP Volume 1