68 lines
2.3 KiB
Markdown
68 lines
2.3 KiB
Markdown
# sping
|
|
|
|
A simple **ping** implementation to briefly details how it works under the hood.
|
|
|
|
## Overview
|
|
|
|
The **Internet Control Message Protocol (ICMP)** is a network layer protocol in the IP suite used for diagnostics, error reporting, and network management. It supports tools like `ping` by sending messages to test connectivity and measure round-trip time.
|
|
|
|
How ICMP Works ?
|
|
* **Purpose**: Facilitates error reporting (e.g., "destination unreachable") and diagnostic queries (e.g., Echo Request/Reply for ping).
|
|
* **Operation**: ICMP packets are encapsulated in IP packets (IPv4/IPv6) and sent between hosts or routers. No TCP/UDP is used.
|
|
* **Key Messages**:
|
|
* Echo Request (Type 8): Sent by ping to test if a host is reachable.
|
|
* Echo Reply (Type 0): Response from the target confirming reachability.
|
|
* Other types: Destination Unreachable (Type 3), Time Exceeded (Type 11).
|
|
|
|
Below, the ICMP packet structure:
|
|
```ascii
|
|
+-------------------------------+
|
|
| Ethernet Frame (optional) |
|
|
|-------------------------------|
|
|
| Destination MAC (6 bytes) |
|
|
| Source MAC (6 bytes) |
|
|
| EtherType (2 bytes) = 0x0800 |
|
|
+-------------------------------+
|
|
| IPv4 Header (20 bytes) |
|
|
|-------------------------------|
|
|
| Version (4b) = 4 |
|
|
| Header Len (4b) = 5 |
|
|
| Type of Service (8b) = 0 |
|
|
| Total Length (16b) = 84 |
|
|
| Identification (16b) |
|
|
| Flags/Offset (16b) = 0 |
|
|
| TTL (8b) = e.g., 64 |
|
|
| Protocol (8b) = 1 (ICMP) |
|
|
| Checksum (16b) |
|
|
| Source IP (32b) |
|
|
| Destination IP (32b) |
|
|
+-------------------------------+
|
|
| ICMP Packet (64 bytes def) |
|
|
|-------------------------------|
|
|
| Type (8b) = 8 / 0 |
|
|
| Code (8b) = 0 |
|
|
| Checksum (16b) |
|
|
| Identifier (16b) |
|
|
| Sequence Number (16b) |
|
|
| Payload (56 bytes, e.g., 0s) |
|
|
+-------------------------------+
|
|
| Ethernet FCS (4 bytes) |
|
|
+-------------------------------+
|
|
```
|
|
|
|
|
|
## Build
|
|
Of course a C compiler must be installed.
|
|
```bash
|
|
make build
|
|
```
|
|
|
|
## Run
|
|
Run the build and launch the program, you must have the superuser rights to run it (raw socket access).
|
|
```bash
|
|
make run
|
|
```
|
|
By default, packets are sent to [google.com](google.com), if you want to change the destination update **HOST** variable:
|
|
```bash
|
|
make run HOST=anotherhost.com
|
|
``` |