forked from Battleforg/hello-openwrt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
executable file
·98 lines (86 loc) · 3.04 KB
/
parser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "parser.h"
pcap_t *handle = 0; // global pcap header
void getPacket(u_char * arg, const struct pcap_pkthdr * pkthdr, const u_char * packet) {
RADIOTAP_C_HEADER * rHeader = (RADIOTAP_C_HEADER*)packet;
// calculate radiotap header length
int l1= rHeader->len[1];
int l2 = rHeader->len[0];
int radiotap_len = (l1 << 8) + l2;
// skip to 802.11frame header
const IEEE80211BEACON_FRAME * bHeader = (IEEE80211BEACON_FRAME*)(packet + radiotap_len);
// if this frame is a 802.11 beacon frame
if ((bHeader->frame_control[0]) == BEACON_FRAME && bHeader->frame_control[1] == 0x00) {
if(fillHotspotData(rHeader, packet, &raw, pkthdr)){
while (1) {
if (!flag) {
save_hotspot(&raw);
break;
}
}
}
// if it some frame like RTS transmitted by station
// fillStaData(rHeader, packet, &raw_sta, pkthdr);
// if station is a new record
} else if (fillStaData(rHeader, packet, &raw_sta, pkthdr)) {
while (1) {
if (!flag) {
save_sta(&raw_sta);
break;
}
}
}
}
int myPcapCatchAndAnaly() {
int status=0;
int header_type;
char errbuf[PCAP_ERRBUF_SIZE];
/* openwrt && linux */
char *dev=(char *)"wlan0";
/* mac os */
//test
// char* dev=(char *)"en0";
handle=pcap_create(dev,errbuf); //为抓取器打开一个句柄
if (handle == NULL) {
fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
return 0;
}
// 由于在该路由器测试时,发现在该openwrt系统上不支持libpcap设置monitor模式,在激活的时候会产生错误
// 将采用手动设置并检测网卡是否为monitor模式
// if(pcap_can_set_rfmon(handle)) {
// //查看是否能设置为监控模式
// printf("Device %s can be opened in monitor mode\n",dev);
// }
// else {
// printf("Device %s can't be opened in monitor mode!!!\n",dev);
// }
// 若是mac os系统,则可以支持
// test
if(pcap_set_rfmon(handle,1)!=0) {
fprintf(stderr, "Device %s couldn't be opened in monitor mode\n", dev);
return 0;
} else {
printf("Device %s has been opened in monitor mode\n", dev);
}
pcap_set_promisc(handle,0); //不设置混杂模式
pcap_set_snaplen(handle,65535); //设置最大捕获包的长度
status=pcap_activate(handle); //激活
if(status!=0) {
pcap_perror(handle,(char*)"pcap error: ");
return 0;
}
header_type=pcap_datalink(handle); //返回链路层的类型
if(header_type!=DLT_IEEE802_11_RADIO) {
printf("Error: incorrect header type - %d\n",header_type);
return 0;
}
int id = 0;
pcap_loop(handle, -1, getPacket, (u_char*)&id);
return 1;
}
void terminate() {
if (handle != 0) {
pcap_breakloop(handle);
printf("terminate!\n");
}
pcap_close(handle);
}