-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_3.cpp
67 lines (56 loc) · 1.73 KB
/
exercise_3.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
#include <iostream>
#include <pcap/pcap.h>
#include <net/ethernet.h>
#include <netinet/ip.h>
//#include <netinet/udp.h>
//#include <netinet/tcp.h>
//#include <netinet/ip_icmp.h>
//...
#include <arpa/inet.h>
using namespace std;
void packet_handler(u_char *accumulator, const struct pcap_pkthdr *metadata, const u_char *data){
auto eh = reinterpret_cast <const struct ether_header*>(data);
//cout << hex << ntohs(eh->ether_type) << endl;
if(ntohs(eh->ether_type) != ETHERTYPE_IP){
cout << "not an IP packet..." << endl;
return;
}
char ip_src[16], ip_dst[16];
const struct iphdr* ih = reinterpret_cast <const struct iphdr*>(eh+1);
//src addr
inet_ntop(
AF_INET,
reinterpret_cast<const void *>(&ih->saddr),
ip_src,
sizeof(ip_src)
);
//dst addr
inet_ntop(
AF_INET,
reinterpret_cast<const void *>(&ih->daddr),
ip_dst,
sizeof(ip_dst)
);
//protocol field is judt 1 byte --> no issues with endianness.
auto protocol = (ih->protocol == IPPROTO_TCP) ? "TCP" : "";
cout << "ip source: " << ip_src << ", ip destination: " << ip_dst << " " << protocol << 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);
}
//-1 means: infinite loop!
if(pcap_loop(handle,-1,packet_handler,NULL) == -1){
cerr << "<some error message should be displayed here...>" << endl;
return(2);
}
return 0;
}