Analyze 80211.c
Function main(int argc, char **argv)
main: The function of main is to process a pcap file and analyze 802.11 packet data, generating a summary of the packets based on specific parameters.
parameters: The parameters of this Function. · parameter1: int argc - The number of command-line arguments passed to the program. · parameter2: char **argv - An array of strings representing the command-line arguments.
Code Description: The main function begins by declaring necessary variables for packet capture and analysis, including structures for packet headers and radiotap data. It checks if the user has provided a pcap file as an argument; if not, it prints usage information and exits. The function then attempts to open the specified pcap file using pcap_open_offline
. If the file cannot be opened, it reports an error and returns.
Once the pcap file is successfully opened, the function enters a loop where it retrieves packets one by one using pcap_next
. For each packet, it calculates the header length and checks if the packet is valid based on its length. If the packet is valid, it initializes a radiotap iterator to parse the packet's radiotap header.
The function processes the radiotap data to extract the transmission rate and short guard interval (SGI) flag, determining the hardware mode (either 'a' for 802.11a or 'n' for 802.11n) based on the data found. It constructs a unique identifier string for each packet based on these parameters.
Using the uthash library, the function checks if a record with the constructed identifier already exists in a hash table. If it does not exist, a new record is allocated, initialized, and added to the hash table. If it does exist, the packet count is incremented, and the end timestamp of the packet is updated.
After processing all packets, the function closes the pcap handle and prints a summary table that includes hardware mode, rate, SGI status, packet size, count of packets, and the delay in seconds between the first and last packet timestamps for each unique identifier in the hash table.
Note: It is essential to ensure that the pcap file provided as an argument is valid and accessible. The program relies on the uthash library for hash table operations, so it must be included in the project dependencies.
Output Example: HW MODE RATE(Mbps) SGI SIZE(bytes) COUNT DELAY(sec) ======= =========== === ============ ===== ========= 802.11a 6.0 OFF 1500 10 0.50000 802.11n 54.0 ON 1500 5 0.25000
graph TD
A[Start] --> B[Check argument count]
B -->|argc < 2| C[Print usage message and exit]
B -->|argc >= 2| D[Open pcap file]
D -->|handle == NULL| E[Print error message and return]
D -->|handle != NULL| F[Process packets in pcap file]
F --> G[Get next packet]
G -->|packet not found| H[Close pcap handle and finish]
G -->|packet found| I[Get header length]
I --> J[Check packet size]
J -->|packet size < 0| G
J -->|packet size >= 0| K[Initialize radiotap iterator]
K -->|Initialization failed| G
K -->|Initialization successful| L[Iterate through radiotap arguments]
L --> M[Check argument index]
M -->|Index is RATE| N[Set rate, sgi_flag, hw_mode]
M -->|Index is MCS| O[Set rate, sgi_flag, hw_mode]
M -->|Index not recognized| L
L --> P[Create hash table index]
P --> Q[Check if record exists in hash table]
Q -->|RECORD_ptr == NULL| R[Allocate memory for new record]
R -->|Memory allocation failed| S[Print error message and return]
R -->|Memory allocation successful| T[Initialize new record and add to hash table]
Q -->|RECORD_ptr != NULL| U[Update existing record]
U --> V[Get next packet]
T --> V
S --> V
V --> G
H --> W[Print results from hash table]
W --> X[End]