A custom, low-level implementation of the classic network utility ping, written in C. This project explores raw sockets, network protocols, the Internet Control Message Protocol (ICMP), and precise packet parsing, benchmarking its output and behavior against the inetutils-2.0 standard.
- Network Diagnosis: Sends ICMP
ECHO_REQUESTpackets to network hosts (IPv4 addresses or FQDNs) and reports the round-trip time (RTT). - Raw Socket Integration: Directly interfaces with the network layer to craft, send, and intercept network packets.
- Robust Packet Parsing: Validates incoming IP headers and ICMP
ECHO_REPLYpayloads, handling network errors gracefully without crashing. - DNS Resolution: Fully resolves domain names and handles Fully Qualified Domain Names (FQDN) seamlessly without delaying the packet response flow.
The program operates at a system-level depth, interacting directly with network layers.
To send and receive ICMP packets, the program initializes a raw network socket using:
socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);Note: Due to the use of raw sockets, the binary requires superuser privileges (sudo) or specific capabilities (CAP_NET_RAW) to run.
- Constructing the Request: The program builds an IP packet payload enclosing an ICMP header structure with
type = ICMP_ECHOand a valid internet checksum. - The Event Loop: A periodic interval loop dispatches a packet per second, initiating a precise structural timer to track the exact departure time.
- Interception & Verification: Incoming packets are intercepted. The program strips the IP header, isolates the ICMP response, checks if the sequence ID matches its own process, and calculates the elapsed Round-Trip Time (RTT).
The application supports standard target arguments along with specific diagnostic flags designed to align identically with inetutils-2.0.
<destination>: Accepts either an IPv4 address (e.g.,8.8.8.8) or a hostname/FQDN (e.g.,google.com).
-v(Verbose): Enhances output transparency by printing detailed error logs, anomalies, or descriptive telemetry whenever network packets encounter issues (such as an expired TTL).-?(Help): Outputs a detailed usage summary outlining command line syntax.
The codebase is designed for environments running a Linux kernel (>= 3.14), optimized specifically for Debian systems.
Compile the project using the provided Makefile:
make
Run the binary with root privileges, targeting a network host:
sudo ./ft_ping google.com
To run with verbose output:
sudo ./ft_ping -v 1.1.1.1
- Zero-Crash Policy: Built to absorb volatile network anomalies. Malformed packets, lost connections, dropped packets, or host unreachable errors are caught and logged cleanly without triggering segmentation faults or memory leaks.
- Resource Cleanup: All opened sockets, allocated network structures, and memory maps are systematically freed upon termination (e.g., when receiving an interrupt signal like
Ctrl+C), ensuring a clean exit state.