🎓 Do your best and obey the destiny. I am a postgraduate student in Southeast University and a summer intern in Java background development in Ctrip. I love fitness and basketball, and I am willing to share what I have seen and gained related to technology. I follow the public account @flying Veal and get the update of the article as soon as possible

🎁 This article has been included in the “CS-Wiki” Gitee official recommended project, has accumulated 1.6K + STAR, is committed to creating a perfect back-end knowledge system, in the road of technology to avoid detours, welcome friends to come to exchange and study

🍉 If you do not have a good project, you can refer to a project I wrote “Open source community system Echo” Gitee official recommended project, so far has accumulated 700+ STAR, SpringBoot + MyBatis + Redis + Kafka + Elasticsearch + Spring Security +… And provide detailed development documents and supporting tutorials. Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo

What is ICMP

We are on this IP protocol that article mentioned a mouth, IP protocol provide unreliable data as a delivery network layer protocol, in the process of transmission, the IP datagram loss may occur, repetition, and out-of-order delays, but IP protocol for these bad things do not have valid inspection and measures to make up for, Of course, neither party will be notified of the results.

Therefore, in view of the above reasons, we need to pay special attention to the following two points when constructing IP networks:

  • Verify that the network is working properly
  • Even when the diagnosis is abnormal

Then came the Internet Control Message Protocol (ICMP).

Figuratively, IP is like a general, and ICMP is his intelligence agent. The general planned from a thousand miles away, and casualties were inevitable on the front lines.

The general who cannot go to the front wants to know at least two things: first, whether my men are marching in the direction I direct them, and not rushing and finding themselves in the wrong place; Second, the number of casualties among our soldiers and the injuries they were wounded by. Only when we know the causes of our own casualties can we make strategic plans for the next step. Needless to say, this is what the ICMP Courier does.

Of course, the above is only for example, may not be very rigorous, you know what the meaning of the line, do not be too critical.

In academic terms, ICMP has two main functions:

1) Confirm whether the IP datagram is successfully delivered to the target address

2) If an IP datagram fails to reach its destination for any reason, ICMP is responsible for notifying the specific reason

Study on ICMP message

The specific cause of the error is that the ICMP protocol is responsible for the notification. The scientific name of the notification is ICMP message. Is the ICMP message sent by the sender or the receiver?

Neither.

ICMP packets are sent by routers.

For example, host A unknowingly sends A packet to host B, while host B is fast asleep. Host A and host B are not in the same local network. Suppose they pass through two routers, as shown in the following figure:

As we all know, in addition to the IP address, we also need the MAC address to ensure that the packet can accurately find the direction. Therefore, in order to know the MAC address of host B, Router 2 broadcasts an ARP request, hoping to obtain the MAC address of host B. Host B is powered off and cannot respond to the request.

For this reason, Router 2 resends ARP request packets over and over again. After several unsuccessful attempts, Router 2 returns an ICMP Destination Unreachable packet to host A (more on ICMP packet types below), informing host A, regrettably, The packet you sent to host B failed to arrive.

Then, how are ICMP packets transmitted to host A?

This is very simple. ICMP packets are transmitted the same way TCP/UDP packets are transmitted.

In other words, real data is first added with an ICMP header and encapsulated into ICMP packets. Then, it is encapsulated into IP datagrams for plaintext transmission. The SOURCE IP address and destination IP address are specified by THE IP protocol. Host A received data will be A layer of A layer of unsealing, so as to obtain the real data that the cause of the abnormal, then A great anger: stupid host B.

ICMP Message Format

As you know, ICMP packets are encapsulated in IP datagrams. Let’s take a look at the following figure:

Well, there seems to be nothing to say here, the picture is very Nice, I watched the king’s way video on THE B website when I took the entrance exam, you can see the picture, understand the ICMP header what things, know the type and code of these two fields is very important, especially the type, next we will focus on it.

ICMP Packet Type

As mentioned above, ICMP Destination Unreachable packets are ICMP packets whose Destination is Unreachable.

ICMP packets are generally divided into two types: error packets and query packets.

Query messages are query messages used by the host for diagnosis.

So academic writing may not be very good understanding, in this way, the za image, the query message and communication abnormal actually do not have what relation, general query message like led hammering came to a quiet canyon, it is a easy to the place of ambush, the general did not dare to rush forward, then sent several agents to find out the situation of the enemy, an action immediately returns.

The common ICMP query packet types are as follows:

  • Echo Reply: The value of the ICMP header type field is 0
  • Echo Request: The value of the ICMP header type field is 8

And error messages are used to inform the host of the cause of the error. Obviously, ICMP error report packets are generated with error data. Once the IP protocol detects an error in an IP packet, it resolutely discards the error IP packet and sends an ICMP error message.

Common ICMP error packet types are as follows:

  • Destination Unreachable. The value of the type field in the ICMP packet header is 3
  • Source Quench: the value of the type field of the ICMP header is 4
  • Redirect: The value of the type field in the ICMP header is 5
  • Timeout (Time Exceeded) : the value of the ICMP header type field is 11

The following describes the common ICMP packet types in detail.

ICMP Echo message (types 0 and 8)

A message between a host or a router that communicates to determine whether the sent packet has successfully reached the peer end.

You can send an ICMP Echo Request message (Type 8) to the peer host or receive an ICMP Echo Reply message (type 0) from the peer host.

The ping command is based on ICMP echo message.

The word ping comes from sonar location, and this command does exactly that. It sends an ICMP Echo Request message of type 0, and the host that receives the Request responds with an ICMP Echo Reply message of type 8. Ping calculates the time between sending Requenst and receiving Reply messages, how many packets were delivered, how many packets were lost, and so on. The user can then judge the general condition of the network.

Ping Github:

Ping didn’t do nothing. It added two more fields to the ICMP message format: identifier and serial number. These two are actually pretty easy to understand:

1) The identifier is used to distinguish which application sends ICMP packets.

Figuratively speaking, the general sends two intelligence agents, one is for the situation, one is for the rescue, there must be a logo to distinguish the two intelligence agents. That’s what identifiers do. The easiest thing to think of as an identifier, and I don’t have to say anything, is the PROCESS PID.

2) Serial number is used to confirm whether the network packet is lost.

Figuratively speaking, the general sends out 10 agents, giving each agent a number. In this way, if ten soldiers are dispatched and ten return, the situation on the front will be good. If 10 go out and none or only one comes back, it’s not a good sign.

ICMP Target Unreachable message (Type 3)

If the router fails to send an IP packet to the Destination ADDRESS, it returns an ICMP Message with Destination Unreachable Message to the sending host.

There are many possible reasons for the unreachable destination, such as network problems, destination host problems, etc., so the unreachable destination message also needs to specify the specific cause of the unreachable destination, which is recorded in the code field of the ICMP header.

So let’s take a look at the code for common unreachable types using the marching example:

1) the front of the war is tight, the general (host A) sent A team of soldiers back to the capital to find the emperor (host B) to move reinforcements, midway intelligence officers galloped to report: general, we lost the direction on the way, can not find the capital where. This is network unreachable, code 0

2) Suppose the soldiers managed to return to the capital, but the emperor was out of town, and all the officials in the court did not dare to agree to send troops to rescue them. This is the host is unreachable, but no one is found at the place, code 1

3) Suppose the soldiers succeeded in finding the capital, but the young guards did not know the famous soldiers because they had been fighting outside the city all the year round, so they needed the password to enter the city to prove their identity. However, the soldiers who had been on the battlefield for a long time would not remember these things, so they could not enter the city. This is protocol unreachable, code 2

4) Suppose that the soldiers make it to the city and meet the Emperor, but the Emperor says that the Secret Service has given him information that is different from what you say. You say that you need reinforcements, but I have been told that you only need provisions. This is called port unreachable, and its code is 3

5) assume that the soldiers were successfully obtained reinforcements, and obtained the firearm over ten box, but midway through narrow trail, carriage of firearms is too big a hard, just need to change a little bit small for this carriage, loading, each carriage but the firearm technology is not yet mature, consider the security issues, the general would have prohibited the firearm. As a result, vast reinforcements blocked in the narrow mountain road. So this is sharding but with the non-sharding bit set, code 4

ICMP Redirection message (Type 5)

Speaking of this, we need to understand what the IP protocol or network layer is responsible for, is to select the appropriate network routing and switching nodes, to ensure that the data transfer in a timely manner.

For this reason, we always prefer to transfer on the shortest and most optimal path.

If the router finds that the sending host is sending data over a less-than-optimal path, it sends an ICMP Redirect Message back to the host containing the optimal routing information and source data.

Write addicted to ha ha, for example: the general learned ICMP intelligence after the rage, sent to move the team leader actually took one hundred thousand reinforcements in a roundabout, the general quickly ordered the killing of the team leader and immediately take the nearest road back.

ICMP Timeout message (Type 11)

An IP packet has a field called TTL (Time To Live), whose value decreases by 1 with each pass through the router until the packet is discarded when it reaches 0.

In this case, the IP router will send an ICMP Time Exceeded Message to the sending host and notify the host that the packet has been discarded.

Figuratively speaking, the team sent by the general to move the rescue troops was unable to find the direction of the capital, the road began to bring only enough for three days of provisions, the whole team died.

The purpose of setting the IP packet lifetime is to prevent IP packets from being forwarded endlessly on the network when routing control problems occur.

The application of ICMP

One application, the ping command, which we have already mentioned, is based on ICMP query messages.

Another command, based on ICMP error messages, is traceroute on Linux and tracert on Windows.

You may think that ICMP error packets are generated only when communication is abnormal. In fact, the traceroute command is an exception. It uses ICMP rules to deliberately create exceptions.

The traceroute command has two functions:

1) Deliberately set a special TTL to track routers on the way to the destination host.

To be specific, the host of the sending end continuously sends UDP packets to the host of the receiving end. The UDP packets are encapsulated into IP datagrams and the TTL increases from 1 to 1.

For example, if the TTL is set to 1, the IP datagram is discarded on the first router and an ICMP error message of the type ICMP timeout message is returned.

Next, set the TTL to 2. If the first router passes, the IP datagram is discarded when it encounters the second router and an ICMP error message is returned.

.

Traceroute then gets all the router IP addresses.

In fact, there is a problem here, how to know whether the data has reached the destination host?

Traceroute is based on UDP transport, so of course you need to specify a port number. Traceroute will select an impossible value for UDP port number.

In this way, when the data reaches the destination host, the ports are not matched. Then, the router generates an ICMP destination unreachable message with the code 3, that is, the port is unreachable.

When the sending host receives the ICMP packet indicating that the port is unreachable, it knows that the destination host successfully receives the data.

2) Deliberately set non-sharding to determine the maximum transmission unit MTU of the path.

In some cases we do not know the MTU size of the path, so we need some way to obtain the MTU to control the size of the packets that are sent.

The sending host simply sends IP datagrams as usual, but with the sharding forbidden flag at the IP header set to 1.

In this way, if the length of an IP packet exceeds the MTU, the router directly discards the PACKET and sends an ICMP target unreachable message to the sending host. The code is 4, which indicates that fragmentation is required but the non-fragmentation bit is set

In this way, the sending host reduces the length of the IP datagram every time it receives an ICMP fragment but sets a non-fragment bit message until it reaches the target host successfully.

| flying veal 🎉 pay close attention to the public, get updates immediately

  • I am a postgraduate student in Southeast University and a summer intern in Java background development of Ctrip. I run a public account “Flying Veal” in my spare time, which was opened on 2020/12/29. Focus on sharing computer fundamentals (data structure + algorithm + computer network + database + operating system + Linux), Java technology stack and other related original technology good articles. The purpose of this public account is to let you can quickly grasp the key knowledge, targeted. Pay attention to the public number for the first time to get the article update, we progress together on the way to growth
  • And recommend personal maintenance of the open source tutorial project: CS-Wiki (Gitee recommended project, has accumulated 1.6K + STAR), is committed to creating a perfect back-end knowledge system, in the road of technology to avoid detdettions, welcome friends to come to exchange learning ~ 😊
  • If you don’t have any outstanding projects, you can refer to the Gitee official recommended project of “Open Source Community System Echo” written by me, which has accumulated 700+ star so far. SpringBoot + MyBatis + Redis + Kafka + Elasticsearch + Spring Security +… And provide detailed development documents and supporting tutorials. Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo Echo