Table of Contents
This article was made for a youtube video, if you want to check that out: https://www.youtube.com/watch?v=aCoUot8xfrY
Before your browser can load a page, before a packet even finds its destination, there’s something watching silently in the background, ready to speak up when things go wrong.
It doesn’t carry your data, it doesn’t need a handshake, and you don’t notice it… until there’s a problem.
This is ICMP, the protocol behind every “ping”, unreachable destination and silent network failure.
In this video, we’ll break down how ICMP works, why it’s essential for the internet to function smoothly.
And at the end, we’ll build a simple latency checker in Go - using ICMP messages to ping hosts and measure response times ourselves.
Let ‘s jump in.
What is ICMP
ICMP, or Internet Control Message Protocol, is a support protocol used by many network devices to send diagnostic or error messages. It helps report issues like unreachable hosts, network congestion or packet timeouts. Problems that the IP protocol alone can’t handle.
Rather than carrying user data, ICMP messages are used to keep the network running smoothly by helping devices identify and respond to connectivity issues.
It’s the protocol behind tools like ping and traceroute and while it’s essential for maintenance, it’s also widely used to perform DDoS attacks.
ICMP Message Schema
When we talk that, it’s important to notice that the messages are transmitted using IP datagrams or IP packets**.** The whole IP datagram will have an IP header of 20 bytes, and the ICMP Message, whose sizes vary.
The ICMPv4 message has basically 4 fields, type, code, checksum and the content.The type can have up to 8 bits, the code 8 bits, checksum 16 bits and the message content will vary. As the type can have up to 8 bits, 2^8 = 256, so we can have up to 256 types, however in reality there are much less ICMP protocol types that are used internally, the same logic applies to the Code of the message.
Type
The type field defines the purpose of the message - what type of message is being sent, like a ping request, a reply or an error report.
Code
While the type field tells us what kind of message it is, the code field gives extra detail, like subcategory or specific reason within that message type. It adds context to the type.
For example, a type 3 is used for Destination Unreachable, with a code 0would be for Network unreachable, code 1 for host unreachable, 2 for Protocol Unreachable, and so on…
Checksum
The checksum is a small piece of data used for error detection. When a device receives an ICMP message, the protocol recalculates this value to check if the data got corrupted. Unlike TCP checksum, on ICMP it’s used only for error detection, not for error correction, so if the data is somehow corrupted, it will be discarded.
Message content
When something goes wrong, the destination device sends back an ICMP message. This message includes the original IP header and the first 8 bytes of the data that caused the issue. That’s usually enough to identify the protocol used (like TCP or UDP) and figure out what went wrong.
Now that we understand more about this powerful protocol, let’s get our hands dirty and implement a latency checker tool using Go.
If you want to see the full implementation check:
Implementation
video implementation: https://www.youtube.com/watch?v=aCoUot8xfrY
repository: https://github.com/viquitorreis/network_latency