Skip to content
greeshma-chPublic

About

A Linux character device driver with a /proc filesystem stats extension, built on WSL2 Ubuntu. Explores kernel-level I/O and embedded systems fundamentals — device registration, ioctl handling, and exposing runtime stats via procfs.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NetWatch

Lightweight Linux command-line network monitor that captures IPv4 packets and displays real-time TCP/UDP statistics.

Problem

Understanding network traffic patterns requires packet-level visibility. NetWatch provides a minimal, dependency-free tool that captures packets using Linux raw sockets and displays protocol distribution and top endpoints.

Architecture

AF_PACKET raw socket (kernel) → PacketCapture (parse) → Statistics (aggregate) → stdout (display)

Single-threaded capture loop with a 1-second socket timeout for periodic display refresh.

Three classes:

  • PacketCapture — RAII raw socket wrapper; captures and parses Ethernet/IPv4/TCP/UDP headers with bounds checks
  • Statistics — Accumulates packet/byte counts; tracks top endpoints via std::unordered_map
  • Packet — Simple struct holding parsed packet information

Key Features

  • Raw packet capture via AF_PACKET + SOCK_RAW (no libpcap)
  • IPv4/TCP/UDP header parsing with IHL-aware bounds checking
  • Real-time protocol distribution (packet counts, byte counts, percentages)
  • Top-5 endpoint tracking
  • RAII resource management for socket cleanup
  • Graceful Ctrl+C termination via std::atomic signal flag

Technologies

  • C++17 — std::optional, std::atomic, structured bindings
  • Linux APIs — AF_PACKET, SOCK_RAW, ETH_P_IP, SO_RCVTIMEO
  • STL — std::unordered_map, std::vector, std::partial_sort
  • CMake — build system

Build

cd NetWatch
mkdir build && cd build
cmake ..
make

Run

Requires CAP_NET_RAW capability or root:

# Option 1: run as root
sudo ./netwatch

# Option 2: set capability (preferred, one-time)
sudo setcap cap_net_raw+ep ./netwatch
./netwatch

Example Output

=== NetWatch ===

Packets captured : 1284
TCP packets      : 934
UDP packets      : 350
Total bytes      : 2.41 MB

TCP   : 72.74%
UDP   : 27.26%

--- Top Endpoints ---
192.168.1.10:52341 -> 142.250.190.46:443  (312 pkts)
192.168.1.10:52342 -> 142.250.190.46:443  (287 pkts)
192.168.1.10:43210 -> 8.8.8.8:53  (89 pkts)

Press Ctrl+C to stop.

Technical Concepts Demonstrated

  • Linux raw socket programming (AF_PACKET, SOCK_RAW)
  • Network protocol stack (Ethernet → IPv4 → TCP/UDP)
  • Packet header parsing with bounds checking and IHL field handling
  • RAII for system resource management
  • Signal-safe graceful shutdown (std::atomic)
  • STL containers for real-time data aggregation
  • const correctness and encapsulation

Future Improvements

  • Per-interface filtering via bind() + sockaddr_ll
  • IPv6 support
  • Configurable display refresh interval
  • Packet rate (packets/sec) tracking
  • BPF filtering for targeted capture

About

A Linux character device driver with a /proc filesystem stats extension, built on WSL2 Ubuntu. Explores kernel-level I/O and embedded systems fundamentals — device registration, ioctl handling, and exposing runtime stats via procfs.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages