Skip to content

Grsaiago/my_ping

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ft_ping

Recreating the inettools-2.0 ping in C, one ICMP ECHO_REQUEST packet at a time xD.

usage gif

📂 Project Structure

.
├── Makefile                # Makefile for building and managing the project
├── README.md               # This file
├── include/                # Header files
│   └── my_ping.h           # Main header file for my_ping
├── obj/                    # Compiled object files (generated during build)
└── src/                    # Source code files
    ├── icmp_messages.c     # Handles ICMP message creation and parsing
    ├── initialization.c    # Initializes the ping utility
    ├── main.c              # Entry point of the program
    ├── message_record.c    # Tracks sent and received messages
    ├── parse.c             # Parses command-line arguments
    ├── print.c             # Handles output and logging
    ├── signal.c            # Manages signal handling (e.g., Ctrl+C)
    ├── socket.c            # Manages socket creation and communication
    └── validation.c        # Validates input and network parameters

🛠️ Features

  • GNU-compliant CLI: Uses parse_argp to provide a standard command-line interface.

  • Cli Options:

-c, --count=NUMBER         Stop after sending NUMBER packets
-d, --debug                Set the SO_DEBUG option
-i, --interval=NUMBER      Wait NUMBER seconds between sending each packet
-t, --ttl=N                specify N as time-to-live
-v, --verbose              Verbose output
-W, --linger=N             Number of seconds to wait for response
-?, --help                 Give this help list
--usage                Give a short usage message
-V, --version              Print program version
  • ICMP Protocol Implementation: Handles ICMP ECHO_REQUEST, ECHO_REPLY, and ICMP_TIMXCEED messages.

  • Signal Handling: Gracefully handles interruptions (e.g., Ctrl+C).

  • Final metrics: min, max and average round trip time (rtt) measurement, as well as standard deviation.

Examples

  • Ping a host 5 times:
sudo ./my_ping -c 5 google.com
  • Set a custom interval between packets
sudo ./my_ping -i 2 google.com
  • Enable verbose output
sudo ./my_ping -v google.com

How It Works

Initialization:

  1. The program parses command-line arguments and validates the target host.
  2. A raw socket is created to send and receive ICMP packets.
  3. The hostname is resolved with DNS or validated (in case of an ipaddr input).

ICMP Packet Handling:

  1. ICMP ECHO_REQUEST packets are constructed and sent to the target host.

  2. The program listens for ECHO_REPLY packets and calculates round-trip times.

Signal Handling:

  1. The program handles interruptions (e.g., Ctrl+C) and displays statistics before exiting.

Output:

  1. The program prints detailed information about each packet, including sequence numbers, round-trip times, and TTL values.

References

RFCS

  1. Ip Packet Definition
  2. ICMP RFC

Concepts

  1. Raw sockets
  2. icmp

Functions

  1. getaddrinfo: dns/name host ip validation
  2. getnameinfo: reverse dns

🙏 Acknowledgments

Inspired by the inettools 2.0 ping utility.

Built with 💖 and a healthy amount of coffee.

About

A reimplementation of inettools 2.0 Ping utility in C

Topics

Resources

Stars

Watchers

Forks

Contributors