How can ping know if my host is down
Solution 1:
Differences between responses are not actually determined by ICMP itself but rather indirectly.
ICMP can distingush between the following:
0 = net unreachable;
1 = host unreachable;
2 = protocol unreachable;
3 = port unreachable;
4 = fragmentation needed and DF set;
5 = source route failed.
But it does so with other network resources. Codes 0, 1, 4, and 5 may be received from a gateway. Codes 2 and 3 may be received from a host.
If, according to the information in the gateway's routing tables, destination network is unreachable, (e.g., the distance to the network is infinity), the gateway may send a destination unreachable message to the internet source host of the datagram. In addition, in some networks, the gateway may be able to determine if the internet destination host is unreachable. It is the Gateways in these networks that can send destination unreachable messages to the source host when the destination host is unreachable, so it's not actually ICMP doing the determinations.
In the case that in the destination host, the IP module cannot deliver the datagram because the indicated protocol module or process port is not active, then the destination host may send a 'destination unreachable' message to the source host.
Finally, if a datagram must be fragmented to be forwarded by a gateway yet the 'Do not Fragment' flag is on, the gateway will discard the datagram and will return a 'destination unreachable' message.
Now to difference between the 2 separate cases: Request Timed Out means that no Echo Reply messages were received within the set time. This can be due to many different causes: ARP request failure, network congestion, packet filtering, routing error, or a even silent discard.
When you get a Reply From [IP address]: 'Destination Host Unreachable,' then the problem occurred at/after a remote router, whose address is indicated by the [IP address]. So it's a router telling you that there is a problem between it and the destination address.
Solution 2:
Host is down message:
This indicates that you don't know a route to the desired destination, or a remote router reports that it has no route to the destination.
Timeout:
Indicates the absense of Echo Reply messages. No package were received within the default time.