A recreation of the GNU inetutils ping utility, written in C.
ft_ping sends ICMP Echo Request packets to one or more network hosts and reports round-trip statistics, mirroring the behavior of the standard ping command.
- Raw ICMP socket implementation (
SOCK_RAW/IPPROTO_ICMP) - ICMP Echo Request/Reply exchange with checksum validation
- Per-packet round-trip time reporting (
icmp_seq,ttl,time) - End-of-run statistics: packets transmitted/received, packet loss %, and min/avg/max/stddev round-trip time
- Verbose mode (
-v/--verbose) - Graceful shutdown on
SIGINT(Ctrl+C) with a final statistics summary - Help / usage messages (
-?,--help,--usage) - Handles multiple destination hosts on the command line
- Linux (uses raw sockets,
netinet/ip_icmp.h,sys/socket.h, etc.) cc/clangor any C99-compatible compiler- Root privileges — raw ICMP sockets require elevated permissions
make # builds the ft_ping binary
make clean # removes object files
make fclean # removes object files and the binary
make re # fclean + allThis produces an ft_ping executable in the project root.
sudo ./ft_ping [OPTION...] HOST...| Option | Description |
|---|---|
-v, --verbose |
Enable verbose output |
-?, --help |
Show the full help message |
--usage |
Show a short usage message |
Ping a single host:
sudo ./ft_ping google.comPing with verbose output:
sudo ./ft_ping -v google.comStop the program at any time with Ctrl+C; it will print a summary before exiting:
--- google.com ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max/stddev = 12.345/14.210/16.842/1.203 ms
.
├── Makefile
├── include/
│ ├── header.h # central include, pulls in system + project headers
│ ├── prototype.h # function prototypes
│ └── struct.h # t_net / t_addr / t_icmp definitions
├── src/
│ ├── main.c # entry point, main send/receive loop
│ ├── parse.c # CLI argument parsing and help handling
│ ├── ping.c # socket creation and address resolution (getaddrinfo)
│ ├── packet.c # sendto/recvmsg wrappers and reply handling
│ ├── icmp.c # checksum computation and ICMP header generation
│ └── utils.c # error handling, help text, time/stat helpers, cleanup
├── cmds.txt # useful debugging/testing commands (tcpdump, valgrind, tc)
└── log.txt # sample valgrind memcheck run (0 leaks, 0 errors)
A few handy commands (see cmds.txt):
# Watch ICMP traffic in real time
sudo tcpdump -i any icmp -vv -n
# Check for memory leaks
sudo valgrind --leak-check=full --log-file=log.txt ./ft_ping google.com
# Simulate packet loss on an interface (useful for testing loss %/timeout handling)
sudo tc qdisc add dev <INTERFACE_NAME> root netem loss 50%
# Remove the simulated loss
sudo tc qdisc del dev <INTERFACE_NAME> rootThe included log.txt shows a clean Valgrind run with all heap blocks freed and no memory errors.
- Requires root (or
CAP_NET_RAW) to open a raw ICMP socket. - A 1-second receive timeout is set on the socket (
SO_RCVTIMEO); timeouts are counted as lost packets. - The program currently resolves and pings the first address in the list (
net.ad[0]) in its main loop.