17273ec43Smmehari 2*b53d54cfSJiao Xianjun // Author: Michael Mehari 3*b53d54cfSJiao Xianjun // SPDX-FileCopyrightText: 2020 UGent 4a6085186SLina Ceballos // SPDX-License-Identifier: GPL-2.0-or-later 57273ec43Smmehari 67273ec43Smmehari /* 77273ec43Smmehari * This program is free software; you can redistribute it and/or modify 87273ec43Smmehari * it under the terms of the GNU General Public License as published by 97273ec43Smmehari * the Free Software Foundation; version 2. 107273ec43Smmehari * 117273ec43Smmehari * This program is distributed in the hope that it will be useful, 127273ec43Smmehari * but WITHOUT ANY WARRANTY; without even the implied warranty of 137273ec43Smmehari * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 147273ec43Smmehari * GNU General Public License for more details. 157273ec43Smmehari * 167273ec43Smmehari * You should have received a copy of the GNU General Public License along 177273ec43Smmehari * with this program; if not, write to the Free Software Foundation, Inc., 187273ec43Smmehari * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 197273ec43Smmehari */ 207273ec43Smmehari 217273ec43Smmehari #include "inject_80211.h" 227273ec43Smmehari #include "radiotap.h" 237273ec43Smmehari #include "uthash.h" 247273ec43Smmehari 257273ec43Smmehari #define HEADERLEN_80211 24 267273ec43Smmehari 277273ec43Smmehari struct RECORD_t 287273ec43Smmehari { 297273ec43Smmehari char id[16]; // hw_mode-rate-sgi_flag-packet_size 307273ec43Smmehari uint16_t pkt_cnt; // number of packets received 317273ec43Smmehari uint64_t ts_begin; // beginning timestamp 327273ec43Smmehari uint64_t ts_end; // ending timestamp 337273ec43Smmehari 347273ec43Smmehari UT_hash_handle hh; // hash function handler 357273ec43Smmehari }; 367273ec43Smmehari 377273ec43Smmehari /* 802.11n bitrates x 2 */ 387273ec43Smmehari static const uint8_t rates_11n[] = {13, 26, 39, 52, 78, 104, 117, 130}; 397273ec43Smmehari 407273ec43Smmehari int main(int argc, char **argv) 417273ec43Smmehari { 427273ec43Smmehari struct pcap_pkthdr pcap_hdr; 437273ec43Smmehari const u_char *packet; 447273ec43Smmehari char hw_mode, id[16]; 457273ec43Smmehari int rate, sgi_flag, packet_size; 467273ec43Smmehari 477273ec43Smmehari int n, hdr_len; 487273ec43Smmehari struct ieee80211_radiotap_iterator rti; 497273ec43Smmehari 507273ec43Smmehari struct RECORD_t *RECORD_ptr, *tmp_ptr, *hash_ptr = NULL; 517273ec43Smmehari 527273ec43Smmehari if (argc < 2) 537273ec43Smmehari { 547273ec43Smmehari fprintf(stderr, "Usage: %s <pcap>\n", argv[0]); 557273ec43Smmehari exit(1); 567273ec43Smmehari } 577273ec43Smmehari 587273ec43Smmehari pcap_t *handle; 597273ec43Smmehari char errbuf[PCAP_ERRBUF_SIZE]; 607273ec43Smmehari handle = pcap_open_offline(argv[1], errbuf); 617273ec43Smmehari 627273ec43Smmehari if (handle == NULL) 637273ec43Smmehari { 647273ec43Smmehari fprintf(stderr,"Couldn't open pcap file %s: %s\n", argv[1], errbuf); 657273ec43Smmehari return(2); 667273ec43Smmehari } 677273ec43Smmehari 687273ec43Smmehari while ((packet = pcap_next(handle, &pcap_hdr))) 697273ec43Smmehari { 707273ec43Smmehari hdr_len = (packet[2] + (packet[3] << 8)); 717273ec43Smmehari if (pcap_hdr.len < (hdr_len + HEADERLEN_80211)) 727273ec43Smmehari continue; 737273ec43Smmehari 747273ec43Smmehari packet_size = pcap_hdr.len - (hdr_len + HEADERLEN_80211); 757273ec43Smmehari if (packet_size < 0) 767273ec43Smmehari continue; 777273ec43Smmehari 787273ec43Smmehari if (ieee80211_radiotap_iterator_init(&rti, (struct ieee80211_radiotap_header *)packet, packet_size) < 0) 797273ec43Smmehari continue; 807273ec43Smmehari 817273ec43Smmehari while ((n = ieee80211_radiotap_iterator_next(&rti)) == 0) 827273ec43Smmehari { 837273ec43Smmehari switch (rti.this_arg_index) 847273ec43Smmehari { 857273ec43Smmehari case IEEE80211_RADIOTAP_RATE: 867273ec43Smmehari rate = (rti.this_arg)[0]; 877273ec43Smmehari sgi_flag = 0; 887273ec43Smmehari hw_mode = 'a'; 897273ec43Smmehari break; 907273ec43Smmehari 917273ec43Smmehari case IEEE80211_RADIOTAP_MCS: 927273ec43Smmehari rate = rates_11n[((rti.this_arg)[2])]; 937273ec43Smmehari sgi_flag = (rti.this_arg)[1] & 0x40; 947273ec43Smmehari hw_mode = 'n'; 957273ec43Smmehari break; 967273ec43Smmehari } 977273ec43Smmehari } 987273ec43Smmehari 997273ec43Smmehari // create hash table index 1007273ec43Smmehari sprintf(id, "%c-%d-%d-%d", hw_mode, rate, sgi_flag, packet_size); 1017273ec43Smmehari 1027273ec43Smmehari // Hash table implementation for c : https://github.com/troydhanson/uthash 1037273ec43Smmehari HASH_FIND_STR(hash_ptr, id, RECORD_ptr); 1047273ec43Smmehari if(RECORD_ptr == NULL) 1057273ec43Smmehari { 1067273ec43Smmehari RECORD_ptr = (struct RECORD_t*)malloc(sizeof(struct RECORD_t)); 1077273ec43Smmehari if(RECORD_ptr == NULL) 1087273ec43Smmehari { 1097273ec43Smmehari fprintf(stderr, "Unable to create record!\n"); 1107273ec43Smmehari return 1; 1117273ec43Smmehari } 1127273ec43Smmehari 1137273ec43Smmehari strcpy(RECORD_ptr->id, id); 1147273ec43Smmehari RECORD_ptr->pkt_cnt = 1; 1157273ec43Smmehari RECORD_ptr->ts_begin = 1e6*pcap_hdr.ts.tv_sec + pcap_hdr.ts.tv_usec; 1167273ec43Smmehari 1177273ec43Smmehari // Add the new record to the hash table 1187273ec43Smmehari HASH_ADD_STR(hash_ptr, id, RECORD_ptr); 1197273ec43Smmehari } 1207273ec43Smmehari else 1217273ec43Smmehari { 1227273ec43Smmehari RECORD_ptr->pkt_cnt++; 1237273ec43Smmehari RECORD_ptr->ts_end = 1e6*pcap_hdr.ts.tv_sec + pcap_hdr.ts.tv_usec; 1247273ec43Smmehari } 1257273ec43Smmehari } 1267273ec43Smmehari pcap_close(handle); 1277273ec43Smmehari 1287273ec43Smmehari 1297273ec43Smmehari // Iterate through the hash table 1307273ec43Smmehari printf("HW MODE\tRATE(Mbps)\tSGI\tSIZE(bytes)\tCOUNT\tDELAY(sec)\n"); 1317273ec43Smmehari printf("=======\t==========\t===\t===========\t=====\t=========\n"); 1327273ec43Smmehari HASH_ITER(hh, hash_ptr, RECORD_ptr, tmp_ptr) 1337273ec43Smmehari { 1347273ec43Smmehari sscanf(RECORD_ptr->id, "%c-%d-%d-%d", &hw_mode, &rate, &sgi_flag, &packet_size); 1357273ec43Smmehari 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)); 1367273ec43Smmehari } 1377273ec43Smmehari fflush(stdout); 1387273ec43Smmehari 1397273ec43Smmehari return 0; 1407273ec43Smmehari } 141