Skip to content
2cat1vimPublic

About

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.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

24 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ft_ping

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.

Features

  • 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

Requirements

  • Linux (uses raw sockets, netinet/ip_icmp.h, sys/socket.h, etc.)
  • cc / clang or any C99-compatible compiler
  • Root privileges — raw ICMP sockets require elevated permissions

Build

make          # builds the ft_ping binary
make clean    # removes object files
make fclean   # removes object files and the binary
make re       # fclean + all

This produces an ft_ping executable in the project root.

Usage

sudo ./ft_ping [OPTION...] HOST...

Options

Option Description
-v, --verbose Enable verbose output
-?, --help Show the full help message
--usage Show a short usage message

Examples

Ping a single host:

sudo ./ft_ping google.com

Ping with verbose output:

sudo ./ft_ping -v google.com

Stop 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

Project Structure

.
├── 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)

Testing & Debugging

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> root

The included log.txt shows a clean Valgrind run with all heap blocks freed and no memory errors.

Notes

  • 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.

About

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.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages