Overview • Features • Architecture • Installation • Usage • Troubleshooting • Roadmap • License
ft_ping is a high-fidelity re-implementation of the classic ICMP network utility. Developed in C, this project serves as a deep dive into low-level network programming, specifically focusing on the Internet Control Message Protocol (ICMP) as defined in RFC 792.
The tool allows users to verify the reachability of a remote host and measure the round-trip time (RTT) for messages sent from the source host to a destination computer. It handles complex tasks such as packet construction, checksum calculation (RFC 1071), signal management, and precise statistical analysis of network latency.
- ICMP Echo Request/Reply Logic: Implements the fundamental handshake of the ICMP protocol to determine host availability.
- Precision RTT Calculation: Computes minimum, maximum, average, and standard deviation (mdev) of round-trip times using
gettimeofdayand advanced mathematical variance formulas. - DNS Resolution: Dynamically resolves hostnames to IPv4 addresses using
getaddrinfo, ensuring compatibility with both IP strings and domain names. - Dual Socket Support: Implements a smart fallback mechanism. It 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). - Signal Handling: Integrated
SIGINT(Ctrl+C) interception to gracefully terminate the process and display a comprehensive statistical summary. - Robust Checksumming: Manual implementation of the 16-bit one's complement sum algorithm required for ICMP header integrity.
- Initialization: Parse CLI arguments using
argp. - Resolution: Convert the target hostname into a
sockaddr_instructure. - Privilege Management: Initialize the ICMP socket. If root, use
SOCK_RAW. If not, attemptSOCK_DGRAM. Drop privileges immediately after socket creation for security. - Transmission Loop:
- Construct an ICMP header.
- Calculate the checksum.
- Transmit the packet and record the timestamp.
- Reception Logic:
- Monitor the socket for incoming data using
selectwith a timeout. - Parse the received IP/ICMP header.
- Validate the process ID and sequence number.
- Calculate the elapsed RTT.
- Monitor the socket for incoming data using
- Termination: Calculate final variance and standard deviation of the session and exit.
graph TD
A[Start] --> B[Parse Command Line Arguments]
B --> C[DNS Resolution: getaddrinfo]
C --> D[Initialize Socket: RAW/DGRAM]
D --> E[Set Signals: 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]
To build and run ft_ping, you need the following environment:
- OS: Linux (Debian/Ubuntu recommended)
- Compiler: GCC (support for C99 or later)
- Libraries:
libc6-dev - Permissions: Root privileges are required for
SOCK_RAWusage, orcap_net_rawcapabilities must be set on the executable.
git clone https://github.com/yourusername/ft_ping.git
cd ft_pingUse the provided Makefile to compile the source code.
makeExpected output:
Compiling: srcs/main.c
Compiling: srcs/ping.c
Linking: ft_ping
Build complete!
If you wish to test in a clean environment, use the provided Dockerfile:
docker-compose up --buildsudo ./ft_ping google.comTo see detailed packet information (including ID and sequence tracking):
./ft_ping -v 8.8.8.8int send_echo_icmp(t_ping *ping): Constructs the ICMP header. It setsicmp->type = ICMP_ECHOand maps theping->pidto theicmp->un.echo.idto identify returned packets.int recv_echo_icmp(t_ping *ping): UtilizesrecvmsgandCMSG_DATAto extract the TTL (Time To Live) from the packet's ancillary data when using raw sockets.int checksum(uint16_t *buf, int len): Performs a 16-bit sum of the header, handles odd-byte lengths, and returns the one's complement.
| Issue | Cause | Resolution |
|---|---|---|
Lacking privilege for icmp socket |
Missing root or capabilities | Run with sudo or use setcap cap_net_raw+ep ft_ping |
unknown host |
DNS failure or No Network | Verify /etc/resolv.conf and active internet connection |
checksum mismatch |
Corrupted packet or logic error | Ensure the data buffer is zeroed before checksum calculation |
packet too short |
Malformed ICMP response | Check network MTU or if a firewall is stripping headers |
- IPv6 Support: Integration of
ICMP6andAF_INET6for modern network compatibility. - Custom Payload: Allow users to specify the number of bytes in the data segment.
- Adaptive Interval: Implement a
-iflag to change the transmission frequency from the default 1 second. - Flood Mode: Add a high-frequency
-fmode for stress testing (superuser only).
This project is licensed under the MIT License - see the LICENSE file for details.
