net-tools/README.md
2026-04-23 16:08:20 +02:00

114 lines
4.2 KiB
Markdown

# net-tools
A collection of ICMPv4 network utilities as an alternative to standard
system commands (ping, ...).
## Code Style
- Allman braces: opening and closing braces each on their own line
- Yoda conditions: constant on the left (e.g. 0 == val, NULL != ptr)
- C99 compliant
- Tabs only, width 4
- snake_case for functions and variables, SCREAMING_SNAKE_CASE for macros
- System includes, then a blank line, then local includes
- Variable declarations aligned with tabs within the function body
- Static helper functions are forward-declared at the top of the file and
defined after the main public function
- Function definitions (not forward declarations or prototypes) have a newline
between the return type and the function name
- Line continuations are indented 2 tabs past the base scope indentation
## Dependencies
- clang (C99)
- make
- bash (configure script)
- POSIX-compliant OS
- libc
For tests only:
- criterion
## Binaries
### ft_ping
Alternative to inetutils ping. Sends ICMPv4 ECHO_REQUEST packets to network
hosts and reports RTT statistics. Requires root (raw socket).
```
ft_ping - Send ICMP ECHO_REQUEST to network hosts
Usage: ft_ping [options] <destination>
Options:
-h, --help Display this help and exit
-V, --version Display version information and exit
-v, --verbose Verbose output
-q, --quiet Quiet mode (only show summary)
-c, --count <NUM> Stop after sending N packets
-i, --interval <SEC> Wait N seconds between packets
-T, --tos <NUM> Set Type of Service (TOS)
-t, --ttl <TTL> Set Time To Live
-s, --size <SIZE> Packet size in bytes
-W, --linger <SEC> Timeout for replies in seconds
-w, --timeout <SEC> Exit after N seconds regardless of packets sent/received
-f, --flood Flood mode
-M, --dont-fragment Set the Don't Fragment bit
-l, --preload <NUM> Send N packets in burst before normal loop
-p, --pattern <HEX> Fill packet payload with hex pattern
```
## Modules
### libicmp
Handles everything ICMP at the socket level: open a raw socket, build and
send echo requests, receive and parse replies. No loop, no display. Just a
clean interface over the kernel socket.
### src/cli
Shared CLI parsing layer used by all binaries. Provides a generic getopt
wrapper and primitive parsers (int, float) that each binary plugs into.
### src/\<binary\>/cli
Per-binary argument parsing. Defines the accepted options, validates them,
and fills the binary-specific config struct.
### src/ping/core
Drives the ping session for one destination. ping_run() creates the socket
and calls ping_one() for each destination. ping_one() sets up state, runs
the loop, and prints the summary. ping_loop() uses select() to wait for
packets and reacts to send/stop flags. ping_send_one() builds the payload
and calls libicmp. ping_callback() handles incoming replies and errors.
### src/ping/scheduler
Manages timing and signals. Installs SIGALRM (triggers send) and SIGINT
(triggers stop). Arms a one-shot timer via setitimer() after each send.
Signal handlers only write a flag -- the loop does the actual work.
### src/ping/tracker
A 128-slot circular buffer indexed by (seq % 128). Records when each
packet was sent and marks it when a reply arrives. Used to compute RTT
and detect duplicates.
### src/ping/stats
Accumulates RTT samples (min, max, sum, sum of squares) in nanoseconds.
Computes min/avg/max/mdev in milliseconds on demand.
### src/ping/output
All user-facing output: the opening line, per-packet lines, ICMP error
messages, the final statistics summary, and flood mode dots.
## License
This project is licensed under the GNU General Public License v3.0 - see the [LICENSE](LICENSE) file for details.
## Authors
- **lohhiiccc** - [git](https://git.lohic.dev)
## See Also
- [RFC 792 - Internet Control Message Protocol](https://tools.ietf.org/html/rfc792)
- [RFX 1071 - Computing the Internet Checksum](https://datatracker.ietf.org/doc/html/rfc1071)
- [libicmp](https://github.com/lohhiiccc/icmp)
- [inetutils](https://www.gnu.org/software/inetutils/)