Network Protocol 5 - ICMP and Ping

Daily development, we often encounter the smooth flow of the network and whether the query domain names corresponding IP addresses and other small needs, this time should be the most used the ping command. Do you know the ping command is how it works? Today, we come together and recognize the ping command under the corresponding ICMP protocol.

ICMP protocol

    ICMP stands for Internet Control Message Protocol, refers to the Internet Control Message Protocol .

    The network itself is unreliable, packets during transmission, may be a lot of unexpected events occur and cause data transmission failure. And the network layer IP protocol is a connectionless protocol, it does not process the failure of the network layer, therefore, we need other protocols, when packet transmission fails, the failure information can pass back, so as to correspond to the processing related problem.

    As in the television show to see the ancient war, the war, when the war situation needs to pass through scouts, then better control of the war. The ICMP packets in the online world to act as such a role, "Scout."

    ICMP packets are encapsulated in IP packets inside. Because the transmission instruction time, certainly need the source and destination addresses. Format itself is very simple, as shown below:

    ICMP packets have a lot of types, different types have different codes, the most common type of request is active, code 8, the active request response, code 0 . From the big side it can be divided into the query packet type and the error message type .

Query message type

    Come, how the war front: We often this is the case in the drama hear? Scout did not come back? There is a case, informed immediately.

    This coach is similar to initiate, take the initiative to see the situation of the enemy, it corresponds to the ICMP packet type of query . For example, the ping command is a common query message, is an active request and obtain the active response of the ICMP protocol . Therefore, the package is in line with the ping command issued by the ICMP protocol format, but it later increased its own format.

    Ping request for the active, capture network, called the ICMP ECHO the REQUEST . Similarly, the initiative to request a reply, called ICMP ECHO a REPLY . Compared to native ICMP, there is more than two fields, a is an identifier, the other is the sequence number. It is not difficult to understand, Marshal sent to scout two teams, a team of whom are wanted, a team of investigators is fighting, there must be a logo to distinguish.

    On the other hand, sent to scout, I have compiled a number. If sent to the 10, back 10, it shows a good fighting ahead. If sent to the 10, two back, it is possible to explain the situation is not good.

    In the option data, will store the time value of ping transmission request, it calculates a round trip time, length of journey described.

The error message type

    Error packets is mainly used to return an error message to the source device information, for the source device determines if more retransmission of a failed packet transmission .

    Or take our "Commander" for instance.

    When the coach is seen map large accounts, thinking the war, the soldier suddenly cried out: Marshal, not okay, General Chang ambush, annihilated.

    This is an abnormal situation initiated to report the occurrence of the bad things, the corresponding ICMP error messages.

    Error messages have the following common types:

  • 3: End unreachable
  • 4: inhibition Source
  • 5: Redirect
  • 11: Timeout

    The first case is not up to the end . Report soldier, Marshal, General Chang gave the forage is not sent.

    That Marshal will surely ask, why not sent? This ICMP corresponding to the following code.

  • Network unreachable code: 0
  • Host Unreachable code: 1
  • Protocol unreachable: 2
  • Port Unreachable: 3
  • Fragmentation needed but the do not fragment: 4

    Specific scenes like this:

  • Network unreachable: Marshal, find a place
  • Host unreachable: Marshal, find a place, could not find General Chang
  • Protocol unreachable: Marshal, find a place, but also to find people, but not for the password.
  • Port Unreachable: Marshal, find a place to find the people, but also for the password, but things did not pair. I went to send food supplies, he said waiting for reinforcements.
  • Fragmentation needed but do not fragment set: Marshal, halfway, narrow mountain road, want to change gab, but before leaving the car you ordered is prohibited to change, no way to put.

    The second is the source quench . That is, let the source station to slow transmission speed (Soldier: Commander, sent forage eat too much, you can slow delivery point).

    The third is a timeout . That is more than the survival time of network packets to the destination or not (Marshal, send forage people put food eaten, not to the place, had starved to death).

    The fourth is the route redirection . Is next sent to another router (Marshal, who would have been the last to send forage as long as the king walked the village, one km to go, the results have to pass Zhangjiajie, more than five kilometers to go next time remember King Village).

    Error message structure is relatively complicated. In addition to front or IP, ICMP the first 8 bytes of the same, to keep back the first 8 bytes of the IP header of the IP packet error and IP body.

    And such particular scout responsible, not only return the bytes to the messenger, also part of the relics back.

  • Scout: Marshal, General Chang has been killed in battle, it is his seals and saber.
  • Marshal: General Chang is how to die (you can view the first 8 bytes of ICMP)? Yes, this is General Chang sword (IP packet header and the body of the first 8 bytes).

ping: query message type of use

    Next, we look at the focus of sending and receiving ping command.

    A assumes that the host IP address is, IP address of the host B is, they are in the same subnet. Then, after running "ping" on the host A, what happens?

  1. Construction of ICMP source host request packet . This data packet comprises a plurality of fields within. The most important are two, one is the type field , in terms of the corresponding request packet, the field is 8. Another is the sequence number , mainly for distinguishing a plurality of successive data packets sent ping time. Each sends a request packet, the sequence number is automatically added 1. To calculate the RTT round trip time, it is partially inserted into the transmission time of data packets.
  2. Construction of the IP layer IP packets . ICMP protocol packet together with the destination IP to IP layer, the IP layer as the destination address will be, local IP address as the source address, together with other control information, constructs an IP packet.
  3. MAC header is added . Find MAC address corresponding to the number of additional control information, based on Ethernet media access rules, transfer them out.

    After host B receives the data frame, the following steps will be performed:

  1. Check the MAC address, or discards the received data frame, extracts the IP packet . Check the packet destination MAC address, and compared with the native MAC address. Such as compliance, to receive the data frame, otherwise discarded. After checking the received packet, the IP packet extracting process from the frame to the IP layer of the present machine.
  2. Check IP IP layer . After the check is complete, extract useful information to the ICMP protocol.
  3. Construction of ICMP response packets . Reply packet type field is 0, the sequence number is the sequence number of the received request packet.
  4. The response packet to host A.

    Within the prescribed time, the source host if there is no response to the ICMP packets, then the target host is unreachable.

    If it has to be packaged, then the target host is reachable. In this case, the source host detects the time delay. Save the current time is a packet sent from the source host to the time.

    Of course, this is only the most simple case with a local area network. If the inter-network, it will also involve forwarding, forwarding router gateway.

    As can be seen, ping command is used inside the ICMP ECHO REQUEST and ECHO REPLY type.

    Other types that do? It is not only true when an error is encountered, in order to receive? the answer is negative. There is a Traceroute command, it will use ICMP rules, the deliberate creation of some scenes can produce erroneous.

Traceroute: error message type of use

    Traceroute commands There are two commonly used functions.

    The first function:

    By setting specific TTL, track elapsed time to the destination router

    Traceroute parameters to perform a certain destination IP address, sends a UDP packet.

    When the TTL set to 1, indicates that MP is a data packet, the first encountered "stumbling block" (typically a router or other type of points) will be killed, then it will return an ICMP packet, the packet It is the network packet errors , type timeout .

    Package by mistake, we will be able to get the IP address of the packet and the time spent in each level to the first level when (some hosts will not respond to ICMP, so request all * of happens).

    How do you know that there is no UDP destination host it? Traceroute program sends a UDP packet to the destination host, but it is impossible to select a value as the UDP port number (greater than 30,000). When the datagram to the destination host, unable to find the corresponding port number, it returns a "port unreachable" error message. In this way, we will know whether the UDP reach the host.

    The second function:

    Setting data packet without fragmentation, determining the path MTU

    Transmission packet, and set the "do not fragment" flag. A first length of the packet transmitted with exactly equal MTU outlet. If you encounter a narrow intermediate level will be the main card, the ICMP error packet network, the type is "fragmentation needed but the do not fragment." In this way, each time it receives ICMP "can not fragment" is reduced when the length of the packet error, to determine the entire path MTU.

to sum up

  • ICMP is equivalent to the online world of scouts. There are two common types of active probing error message query message and exception reporting.
  • ping command using a query message, Traceroute command with an error message.


Welcome to add a personal Micro Signal: Like if thoughts.

I welcome the attention of the public numbers, not only recommend the latest blog for you, there are more surprises waiting for you and resources! Learn together and common progress!



Guess you like