A lightweight packet sniffer written in C using libpcap that captures, analyzes, and prints details of Ethernet, IP, TCP, UDP, ARP, and ICMP packets. The project provides insights into how data flows across the network by parsing protocol headers and displaying field-level details.
- Captures Ethernet, IP, TCP, UDP, ARP, and ICMP packets.
- Parses headers and extracts all important fields.
- Provides human-readable explanations for each field.
- Flowchart for code execution to understand packet parsing logic.
- Structured packet dissection for learning and debugging.
- Linux (Ubuntu recommended)
- gcc (C compiler)
- libpcap library
Install dependencies:
sudo apt-get update
sudo apt-get install gcc libpcap-dev -yCompile:
gcc sniffer.c -lpcap -o snifferRun with sudo (required for raw packet capture):
sudo ./sniffer+-------------------+----------------+----------------+----------------+------------------+
| Ethernet Header | IP Header | TCP/UDP Header | (Optional) | Application Data |
| (Layer 2) | (Layer 3) | (Layer 4) | Payload Data | (Layer 7 Payload)|
+-------------------+----------------+----------------+----------------+------------------+
|<---- 14 bytes --->|<- 20-60 bytes->|<- 20-24 bytes->|<-- Varies ---->|<---- Varies ---->|
+-----------------------------------+--------------------------------+---------------------+
| Destination MAC Address (6 bytes) | Source MAC Address (6 bytes) | EtherType (2 bytes) |
+-----------------------------------+--------------------------------+---------------------+
-
Destination MAC β Physical address of the recipient device.
-
Source MAC β Physical address of the sender.
-
EtherType β Protocol encapsulated in payload:
0x0800β IPv40x0806β ARP0x86DDβ IPv6
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version| IHL |Type of Service| Total Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Identification |Flags| Fragment Offset |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Time to Live | Protocol | Header Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source IP Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination IP Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options + Padding (if any) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
Version β IPv4 (4) or IPv6 (6).
-
IHL (Header Length) β Number of 32-bit words in the header.
-
Type of Service (TOS/DSCP) β Defines service priority.
-
Total Length β Entire IP packet size (header + data).
-
Identification β Unique packet ID for fragmentation.
-
Flags β Fragmentation control (DF, MF).
-
Fragment Offset β Position of fragment in original datagram.
-
Time to Live (TTL) β Max hops allowed.
-
Protocol β Upper layer protocol:
6β TCP17β UDP1β ICMP
-
Header Checksum β Error checking.
-
Source/Destination IP Address β Sender/Receiver IP.
-
Options β Optional, for security/routing.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Port | Destination Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Acknowledgment Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Header Len |Resv| Flags | Window Size |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | Urgent Pointer |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options (if Data Offset > 5) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
Source Port β Senderβs port.
-
Destination Port β Receiverβs port.
-
Sequence Number β Byte ordering in stream.
-
Acknowledgment Number β Confirms receipt of data.
-
Header Length β TCP header size.
-
Flags:
- URG: Urgent pointer valid
- ACK: Acknowledgment valid
- PSH: Push data immediately
- RST: Reset connection
- SYN: Synchronize sequence numbers
- FIN: Terminate connection
-
Window Size β Flow control.
-
Checksum β Error detection.
-
Urgent Pointer β Data priority indicator.
-
Options β Extra features (e.g., MSS).
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Port | Dest Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Length | Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- Source Port β Senderβs port.
- Destination Port β Receiverβs port.
- Length β UDP header + data length.
- Checksum β Error detection.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Hardware Type | Protocol Type |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Hardware Len | Protocol Len | Operation |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sender Hardware Address (MAC) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sender IP Address (4 bytes) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Target Hardware Address (MAC) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Target IP Address (4 bytes) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- Hardware Type β Network type (1 = Ethernet).
- Protocol Type β Protocol used (IPv4 =
0x0800). - Hardware Length β Length of MAC address (6).
- Protocol Length β Length of IP address (4).
- Operation β Request (1) or Reply (2).
- Sender Hardware Address β Senderβs MAC.
- Sender IP Address β Senderβs IP.
- Target Hardware Address β Receiverβs MAC.
- Target IP Address β Receiverβs IP.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type | Code | Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Rest of Header |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- Type β ICMP message type (e.g., 0 = Echo Reply, 8 = Echo Request).
- Code β Further info about Type.
- Checksum β Error detection.
- Rest of Header β Depends on Type/Code (e.g., for Echo: Identifier & Sequence Number).
graph TD
Start([Start pcap_loop, calls packet_handler])
--> PKTINC["Increment packet counters (global, per-packet)"]
PKTINC --> DevCheck{Check header->len >= ETHER_HDR_LEN}
DevCheck -- No --> Short1[Too short for Ethernet header: Discard; Return]
DevCheck -- Yes --> Eth1[Cast to Ethernet Header Struct]
Eth1 --> EthType["Extract EtherType (ntohs)"]
EthType --> ETCheck{EtherType == ETHERTYPE_IP?}
ETCheck -- No --> ARPCheck{EtherType == ETHERTYPE_ARP?}
ARPCheck -- Yes --> ARP1[parse_arp_packet]
ARP1 --> WriteEnd[Write, Flush, Return]
ARPCheck -- No --> Other[Increment other_ether_packets; print Other EtherType; Return]
Other --> WriteEnd
ETCheck -- Yes --> IP1[parse_ip_packet]
%% IP Packet Path
IP1 --> IPShort{"Check packet_len >= ETHER_HDR_LEN + sizeof(struct ip_header)"}
IPShort -- No --> IPMalformed[Print error, return]
IPMalformed --> WriteEnd
IPShort -- Yes --> IPHead[Cast to IP Header Struct]
IPHead --> IPHL["Extract IP Header Length (IHL), total len"]
IPHL --> IPFit{Check IP_HDR_LEN >= 20, offset + IHL <= packet_len}
IPFit -- No --> IPMalformed
IPFit -- Yes --> Proto["Extract Protocol (ip_p)"]
Proto --> TCPCheck{ip_p == IPPROTO_TCP?}
TCPCheck -- Yes --> TCPShort{Check packet_len >= offsets + min TCP header}
TCPShort -- No --> TCPMalformed[Print error, return]
TCPMalformed --> WriteEnd
TCPShort -- Yes --> TCPH[Cast to TCP Header Struct; Compute th_off]
TCPH --> TCPFit{Check TCP_HDR_LEN >= 20, offset+len in bounds}
TCPFit -- No --> TCPMalformed
TCPFit -- Yes --> TCPParse[Print TCP, extract ports, flags, seq, ack, payload len]
TCPParse --> WriteEnd
TCPCheck -- No --> UDPCheck{ip_p == IPPROTO_UDP?}
UDPCheck -- Yes --> UDPShort{Check packet_len >= offsets + UDP header}
UDPShort -- No --> UDPMalformed[Print error, return]
UDPMalformed --> WriteEnd
UDPShort -- Yes --> UDPH[Cast to UDP Header Struct, extract ports, lengths]
UDPH --> UDPParse["Extract UDP payload (lengths check)"]
UDPParse --> WriteEnd
UDPCheck -- No --> ICMPCheck{ip_p == IPPROTO_ICMP?}
ICMPCheck -- Yes --> ICMPShort{Check packet_len >= offsets + ICMP header}
ICMPShort -- No --> ICMCMalformed[Print error, return]
ICMCMalformed --> WriteEnd
ICMPShort -- Yes --> ICMPParse[Cast, extract type/code, possibly echo/frag/gateway, print details]
ICMPParse --> WriteEnd
ICMPCheck -- No --> OIPProto[Increment other_ip_protocols, print unknown protocol]
OIPProto --> WriteEnd
WriteEnd["Done Write & Flush (output to file)"]
- RFC 791 β IP Protocol
- RFC 793 β TCP Protocol
- RFC 768 β UDP Protocol
- RFC 826 β ARP Protocol
- RFC 792 β ICMP Protocol
This tool is for educational purposes only. Unauthorized packet sniffing on networks you do not own or have explicit permission to monitor may violate laws.