-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
55 lines (47 loc) · 1.42 KB
/
main.cpp
File metadata and controls
55 lines (47 loc) · 1.42 KB
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
#include "connection.h"
#include "connections.h"
#include "packet.h"
#include <cassert>
#include <pcap/pcap.h>
int main(int argc, char **argv) {
using namespace std;
pcap_t *pcap;
const u_char * mpacket;
char errbuf[1000];
struct pcap_pkthdr header;
struct bpf_program fp;
char filter_exp[] = "tcp and ip";
if (argc < 1) {
cout << "Not enough arguements." << endl;
return 1;
}
char * file = argv[1];
cout << file << endl;
if ((pcap = pcap_open_offline(file, errbuf)) == NULL) {
cout << "Could not open pcap file: " << errbuf << endl;
return 1;
}
if (pcap_compile(pcap, &fp, filter_exp, 0, 0) == -1) {
cout << "Could not compile expression: " << filter_exp << pcap_geterr(pcap) << endl;
}
if (pcap_setfilter(pcap, &fp) == -1) {
cout << "Could not install filter: " << filter_exp << pcap_geterr(pcap) << endl;
}
connections conns;
while ((mpacket = pcap_next(pcap, &header)) != NULL) {
auto new_packet = packet(mpacket, header.ts, header.caplen);
cout << new_packet << endl;
if (conns.empty()) {
conns.start_time(new_packet.ts_milli() + new_packet.ts_sec()*1000000);
}
if (new_packet.syn() && !new_packet.ack()) {
conns.add_connection(new connection(new_packet));
}
if (new_packet.rst()) {
conns.add_connection(new connection(new_packet));
}
conns.recv_packet(new_packet);
}
cout << conns << endl;
return 0;
}