-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
139 lines (119 loc) · 3.83 KB
/
main.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// main.cpp
// sniffer
//
// Created by jianqing.du on 18-11-12
//
// see http://www.tcpdump.org/pcap.html for tutorial of programming with pcap
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <list>
#include <string>
#include "SnifferThread.h"
#include "PacketHandler.h"
#include "Util.h"
using namespace std;
void print_usage(const char* progname)
{
printf("Usage: \n");
printf(" %s -h print help info\n", progname);
printf(" %s [-d] run as a deamon\n"
" [-i device_name] sniffer on network interface, any for all interface\n"
" [-f save_file] save packat in file\n"
" [-m max_file_size] max file size in MB\n"
" [-c content] only record packet that has this content\n"
" [-a] only record difference ip address\n"
" -r filter_rule filter rule, the same as tcpdump\n", progname);
}
int get_all_device(list<string>& device_list)
{
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t* all_devs;
pcap_if_t* d;
if (pcap_findalldevs(&all_devs, errbuf) == -1) {
printf("pcap_findalldevs failed: %s\n", errbuf);
return 1;
}
for (d = all_devs; d; d = d->next) {
pcap_t* handle = pcap_open_live(d->name, BUFSIZ, 1, 1000, errbuf);
if (handle) {
int device_type = pcap_datalink(handle);
if ((device_type == DLT_NULL) || (device_type == DLT_EN10MB)) {
//printf("name:%s, desc:%s, flags: 0x%x\n", d->name, d->description, d->flags);
device_list.push_back(d->name);
}
pcap_close(handle);
}
}
return 0;
}
int main(int argc, const char * argv[])
{
const char* progname = argv[0];
char* device_name = (char*)"any";
char* save_file = NULL;
char* filter_rule = NULL;
char* content = NULL;
int ch = 0;
int max_file_size = 8*1024; // MB
bool daemon = false;
bool only_record_ip = false;
while ((ch = getopt(argc, (char *const *)argv, "ahdi:f:m:r:c:")) != -1) {
switch (ch) {
case 'h':
print_usage(progname);
return 0;
case 'd':
daemon = true;
break;
case 'a':
only_record_ip = true;
break;
case 'i':
device_name = optarg;
break;
case 'f':
save_file = optarg;
break;
case 'r':
filter_rule = optarg;
break;
case 'm':
max_file_size = atoi(optarg);
break;
case 'c':
content = optarg;
break;
case '?':
default:
print_usage(progname);
return 1;
}
}
printf("device=%s, save_file=%s, max_file_size=%d, content=%s, filter=%s\n",
device_name, save_file, max_file_size, content, filter_rule);
if (daemon) {
daemonize();
}
if (CPacketHandler::Init(save_file, max_file_size, content, only_record_ip)) {
printf("init failed\n");
return -1;
}
if (!device_name || !strcmp(device_name, "any")) {
list<string> device_list;
get_all_device(device_list);
for (list<string>::iterator it = device_list.begin(); it != device_list.end(); ++it) {
CSnifferThread* pThread = new CSnifferThread();
pThread->StartSniffer(it->c_str(), filter_rule);
}
} else {
CSnifferThread* pThread = new CSnifferThread();
pThread->StartSniffer(device_name, filter_rule);
}
while (1) {
sleep(1);
}
return 0;
}