Implementation of the ping command, built from scratch using Ethernet frames. It performs MAC address resolution via ARP, then constructs and sends/receives IPv4 and ICMP headers. It bypasses the kernel's IP stack, directly handling packets using raw sockets (AF_PACKET).
- Ethernet frame construction and parsing
- Default route resolution
- ARP:
- Packet format implementation
- Send/receive handling
- ARP cache with timeout and aging
- Re-ARP and conflict handling
- IPv4:
- Header construction
- Checksum calculation
- DF bit handling
- Fragment reassembly
- ICMP Fragmentation Needed (Type 3 Code 4) generation
- ICMP Echo:
- Echo request construction
- Reply parsing
- Ping loop:
- 1-second interval
- Packet loss statistics
- Graceful termination with Ctrl+C
gcc -o ping ping.c
sudo ./ping <target_ip>[2026-02-23 10:43:50] Default gateway: 172.17.0.1 (via eth0)
[2026-02-23 10:43:50] My MAC: XX:XX:XX:XX:XX:XX
[2026-02-23 10:43:50] My IP: 172.17.0.2
[2026-02-23 10:43:50] ARP request sent
[2026-02-23 10:43:50] ARP reply from 172.17.0.1 MAC=XX:XX:XX:XX:XX:XX
[2026-02-23 10:43:50] PING 8.8.8.8: 1 sec interval, Ctrl+C to stop
[2026-02-23 10:43:50] 64 bytes from 8.8.8.8: seq=1 ttl=63 time=111.8 ms
[2026-02-23 10:43:51] 64 bytes from 8.8.8.8: seq=2 ttl=63 time=22.3 ms
[2026-02-23 10:43:52] 64 bytes from 8.8.8.8: seq=3 ttl=63 time=41.2 ms
[2026-02-23 10:43:53] 64 bytes from 8.8.8.8: seq=4 ttl=63 time=37.9 ms
[2026-02-23 10:43:54] 64 bytes from 8.8.8.8: seq=5 ttl=63 time=36.2 ms
[2026-02-23 10:43:55] 64 bytes from 8.8.8.8: seq=6 ttl=63 time=40.5 ms
[2026-02-23 10:43:56] 64 bytes from 8.8.8.8: seq=7 ttl=63 time=45.8 ms
[2026-02-23 10:43:57] 64 bytes from 8.8.8.8: seq=8 ttl=63 time=41.6 ms
[2026-02-23 10:43:58] 64 bytes from 8.8.8.8: seq=9 ttl=63 time=47.2 ms
[2026-02-23 10:43:59] 64 bytes from 8.8.8.8: seq=10 ttl=63 time=37.6 ms
[2026-02-23 10:44:00] 64 bytes from 8.8.8.8: seq=11 ttl=63 time=45.2 ms
[2026-02-23 10:44:01] 64 bytes from 8.8.8.8: seq=12 ttl=63 time=82.6 ms
^C
--- 8.8.8.8 ping statistics ---
12 packets transmitted, 12 received, 0% packet loss