-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_2.cpp
66 lines (53 loc) · 1.51 KB
/
exercise_2.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
#include <iostream>
#include <pcap/pcap.h>
using namespace std;
void packet_handler(
u_char *accumulator,
const struct pcap_pkthdr *metadata,
const u_char *data
)
{
auto counter_ptr = reinterpret_cast <u_int*>(accumulator);
auto counter = ++(*counter_ptr);
cout << "pkt nr: " << counter << endl;
}
int main(int argc, char *argv[]){
if(argc!=2){
cout << "Usage is: " << argv[0] << " <inf_name>" << endl;
return -1;
}
pcap_t *handle;
char *errbuf = new char[1000];
handle = pcap_open_live(argv[1],64,1,1000,errbuf);
if(handle==NULL){
cerr << "Couldn't open device: " << errbuf << endl;
return(2);
}
u_int counter = 0;
if(pcap_loop(handle,10,packet_handler,(u_char*)&counter) == -1){
cerr << "Error!" << endl;
}
return 0;
}
int main2(int argc, char *argv[]){
if(argc!=2){
cout << "Usage is: " << argv[0] << " <inf_name>" << endl;
return -1;
}
pcap_t *handle;
char *errbuf = new char[1000];
handle = pcap_open_live(argv[1],64,1,1000,errbuf);
if(handle==NULL){
cerr << "Couldn't open device: " << errbuf << endl;
return(2);
}
auto metadata = new pcap_pkthdr();
const u_char *pkt = pcap_next(handle, metadata);
cout << (metadata->caplen) << endl;
cout << (metadata->len) << endl;
cout << (metadata->ts.tv_sec) << endl;
for(int i=0; i<64; i++)
cout << pkt[i];
cout << endl;
return 0;
}