xref: /openwifi/user_space/inject_80211/analyze_80211.c (revision 855b59fd6a93401097234a1948a6fe61b591cced)
1 
2 // (c)2020 Michael Tetemke Mehari <[email protected]>
3 
4 /*
5  *   This program is free software; you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; version 2.
8  *
9  *   This program is distributed in the hope that it will be useful,
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *   GNU General Public License for more details.
13  *
14  *   You should have received a copy of the GNU General Public License along
15  *   with this program; if not, write to the Free Software Foundation, Inc.,
16  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #include "inject_80211.h"
20 #include "radiotap.h"
21 #include "uthash.h"
22 
23 #define HEADERLEN_80211   24
24 
25 struct RECORD_t
26 {
27 	char		id[16];		// hw_mode-rate-sgi_flag-packet_size
28 	uint16_t	pkt_cnt;	// number of packets received
29 	uint64_t	ts_begin;	// beginning timestamp
30 	uint64_t	ts_end;		// ending timestamp
31 
32 	UT_hash_handle	hh;		// hash function handler
33 };
34 
35 /* 802.11n bitrates x 2 */
36 static const uint8_t rates_11n[] = {13, 26, 39, 52, 78, 104, 117, 130};
37 
38 int main(int argc, char **argv)
39 {
40 	struct pcap_pkthdr pcap_hdr;
41 	const u_char *packet;
42 	char hw_mode, id[16];
43 	int rate, sgi_flag, packet_size;
44 
45 	int n, hdr_len;
46 	struct ieee80211_radiotap_iterator rti;
47 
48 	struct RECORD_t *RECORD_ptr, *tmp_ptr, *hash_ptr = NULL;
49 
50 	if (argc < 2)
51 	{
52 		fprintf(stderr, "Usage: %s <pcap>\n", argv[0]);
53 		exit(1);
54 	}
55 
56 	pcap_t *handle;
57 	char errbuf[PCAP_ERRBUF_SIZE];
58 	handle = pcap_open_offline(argv[1], errbuf);
59 
60 	if (handle == NULL)
61 	{
62 		fprintf(stderr,"Couldn't open pcap file %s: %s\n", argv[1], errbuf);
63 		return(2);
64 	}
65 
66 	while ((packet = pcap_next(handle, &pcap_hdr)))
67 	{
68 		hdr_len = (packet[2] + (packet[3] << 8));
69 		if (pcap_hdr.len < (hdr_len + HEADERLEN_80211))
70 			continue;
71 
72 		packet_size = pcap_hdr.len - (hdr_len + HEADERLEN_80211);
73 		if (packet_size < 0)
74 			continue;
75 
76 		if (ieee80211_radiotap_iterator_init(&rti, (struct ieee80211_radiotap_header *)packet, packet_size) < 0)
77 			continue;
78 
79 		while ((n = ieee80211_radiotap_iterator_next(&rti)) == 0)
80 		{
81 			switch (rti.this_arg_index)
82 			{
83 				case IEEE80211_RADIOTAP_RATE:
84 					rate = (rti.this_arg)[0];
85 					sgi_flag = 0;
86 					hw_mode = 'a';
87 					break;
88 
89 				case IEEE80211_RADIOTAP_MCS:
90 					rate = rates_11n[((rti.this_arg)[2])];
91 					sgi_flag = (rti.this_arg)[1] & 0x40;
92 					hw_mode = 'n';
93 					break;
94 			}
95 		}
96 
97 		// create hash table index
98 		sprintf(id, "%c-%d-%d-%d", hw_mode, rate, sgi_flag, packet_size);
99 
100 		// Hash table implementation for c : https://github.com/troydhanson/uthash
101 		HASH_FIND_STR(hash_ptr, id, RECORD_ptr);
102 		if(RECORD_ptr == NULL)
103 		{
104 			RECORD_ptr = (struct RECORD_t*)malloc(sizeof(struct RECORD_t));
105 			if(RECORD_ptr == NULL)
106 			{
107 				fprintf(stderr, "Unable to create record!\n");
108 				return 1;
109 			}
110 
111 			strcpy(RECORD_ptr->id, id);
112 			RECORD_ptr->pkt_cnt = 1;
113 			RECORD_ptr->ts_begin = 1e6*pcap_hdr.ts.tv_sec + pcap_hdr.ts.tv_usec;
114 
115 			// Add the new record to the hash table
116 			HASH_ADD_STR(hash_ptr, id, RECORD_ptr);
117 		}
118 		else
119 		{
120 			RECORD_ptr->pkt_cnt++;
121 			RECORD_ptr->ts_end = 1e6*pcap_hdr.ts.tv_sec + pcap_hdr.ts.tv_usec;
122 		}
123 	}
124 	pcap_close(handle);
125 
126 
127 	// Iterate through the hash table
128 	printf("HW MODE\tRATE(Mbps)\tSGI\tSIZE(bytes)\tCOUNT\tDELAY(sec)\n");
129 	printf("=======\t==========\t===\t===========\t=====\t=========\n");
130 	HASH_ITER(hh, hash_ptr, RECORD_ptr, tmp_ptr)
131 	{
132 		sscanf(RECORD_ptr->id, "%c-%d-%d-%d", &hw_mode, &rate, &sgi_flag, &packet_size);
133 		printf("802.11%c\t%.1f\t\t%s\t%d\t\t%d\t%.5f\n", hw_mode, rate/2.0, (sgi_flag == 0 ? "OFF" : "ON"), packet_size, RECORD_ptr->pkt_cnt, 1e-6*(RECORD_ptr->ts_end - RECORD_ptr->ts_begin));
134 	}
135 	fflush(stdout);
136 
137 	return 0;
138 }
139