A high-fidelity re-implementation of the classic ICMP network utility in C, focusing on low-level network programming.
Key Features • Architecture • Logic Flow • System Flow Diagram • Prerequisites • Installation • 1. Clone the Repository
ft-ping is a high-fidelity re-implementation of the classic ping utility, written in C. This project is a deep dive into low-level network programming, focusing on the Internet Control Message Protocol (ICMP) as defined in RFC 792.
The tool verifies the reachability of a remote host and measures the round-trip time (RTT) for packets sent to a destination. It handles packet construction, checksum calculation (RFC 1071), signal management, and precise statistical analysis of network latency.
- ICMP Echo Logic: Implements the core ICMP echo request/reply mechanism to determine host availability.
- DNS Resolution: Resolves hostnames to IPv4 addresses using
getaddrinfo, accepting both IP addresses and domain names as targets. - Dual Socket Support: Intelligently attempts to open a
SOCK_RAWsocket (requiring root privileges) and gracefully falls back toSOCK_DGRAMif run as a standard user (where supported by the OS). - Precision RTT Statistics: Calculates minimum, maximum, average, and mean deviation (mdev) of round-trip times using
gettimeofday. - Robust Checksumming: A manual implementation of the 16-bit one's complement sum algorithm ensures ICMP header integrity.
- Graceful Shutdown: Intercepts
SIGINT(Ctrl+C) to terminate the process cleanly and display a comprehensive statistical summary.
- Initialization: Parse command-line arguments.
- Resolution: Resolve the target hostname into an IPv4 address and
sockaddr_instructure. - Socket Creation: Initialize an ICMP socket. Attempt to use
SOCK_RAWwith root privileges, falling back toSOCK_DGRAMif necessary. Drop root privileges immediately after socket binding for enhanced security. - Main Loop:
- Construct and timestamp an ICMP echo request packet.
- Calculate the packet's checksum.
- Transmit the packet using
sendto.
- Reception:
- Wait for an incoming packet using
selectwith a timeout. - Receive the packet and parse the IP/ICMP headers.
- Validate the packet's process ID and sequence number.
- Calculate and display the RTT.
- Wait for an incoming packet using
- Termination: Upon receiving
SIGINT, calculate the final statistics (including variance and standard deviation) and print a summary before exiting.
graph TD
A[Start] --> B[Parse Command-Line Arguments]
B --> C[DNS Resolution via getaddrinfo]
C --> D[Initialize Socket: RAW/DGRAM]
D --> E[Set Signal Handler: SIGINT]
E --> F[Send ICMP Echo Request]
F --> G[Wait for Response: select/recvmsg]
G --> H{Valid Reply?}
H -- Yes --> I[Update Stats & Print Info]
H -- No/Timeout --> J[Handle Error/Wait]
I --> K{SIGINT Received?}
J --> K
K -- No --> L[Sleep 1s] --> F
K -- Yes --> M[Calculate Final Stats]
M --> N[Display Summary & Exit]
- Operating System: Linux (developed and tested on Debian/Ubuntu)
- Compiler: GCC (C99 standard or later)
- Build Tools:
make - Libraries:
libc6-dev - Permissions: Root privileges (
sudo) orcap_net_rawcapabilities are required forSOCK_RAWfunctionality.
git clone https://github.com/jdecorte-be/ft-ping.git
cd ft-pingUse the provided Makefile to compile the source code.
makeThe compiled binary ft_ping will be created in the root directory.
To run ft_ping without sudo, you can grant the executable the required network capabilities:
sudo setcap cap_net_raw+ep ./ft_pingTo build and run the project in an isolated Docker container:
docker-compose up --buildThe command syntax is designed to be similar to the standard ping utility.
# Using sudo
sudo ./ft_ping google.com
# Or with capabilities set
./ft_ping 8.8.8.8| Flag | Description | Example |
|---|---|---|
-v |
Verbose mode. Prints detailed packet information. | ./ft_ping -v google.com |
-h |
Help. Displays the usage message and options. | ./ft_ping -h |
| Issue | Probable Cause | Resolution |
|---|---|---|
Lacking privilege for icmp socket |
Missing root privileges or capabilities. | Run with sudo or set capabilities using setcap cap_net_raw+ep ft_ping. |
unknown host |
DNS resolution failed or no network access. | Check your internet connection and DNS settings (e.g., /etc/resolv.conf). |
| Checksum mismatch in received packet | Packet corruption or a logic error. | Ensure the network is stable. If persistent, it may indicate a bug. |
| Packet reception timeout | Host is down, or a firewall is blocking ICMP. | Verify the destination host is online and check firewall rules on both ends. |
- IPv6 Support: Add support for
ICMPv6andAF_INET6. - Custom Payload Size: Implement a
-sflag to specify the data payload size. - Custom Interval: Implement a
-iflag to change the interval between packets. - Flood Mode: Add a
-fflag for high-frequency sending (superuser only). - Time To Live (TTL): Implement a
-tflag to set the packet's TTL value. - Packet Count: Add a
-cflag to stop after sending a specific number of packets.
This project is licensed under the MIT License. See the LICENSE file for details.
