-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_5.cpp
195 lines (164 loc) · 5.21 KB
/
exercise_5.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#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>
#include <stdio.h>
#include <string.h>
#include <unordered_map>
using namespace std;
/*
* 802.1Q header structure.
*/
struct vlan_ethhdr {
u_char h_dest[ETHER_ADDR_LEN];
u_char h_source[ETHER_ADDR_LEN];
u_int16_t h_vlan_proto;
u_int16_t h_vlan_TCI;
u_int16_t h_vlan_encapsulated_proto;
};
class PCAP_IP_tap
{
private:
char m_errbuf[PCAP_ERRBUF_SIZE];
pcap_t *handle;
unordered_map<uint64_t,u_int> flow_table;
static void callback(
u_char *state,
const struct pcap_pkthdr *metadata,
const u_char *data){
auto tmp = reinterpret_cast<PCAP_IP_tap*>(state);
tmp->packet_handler(metadata,data);
}
public:
//Constructor
PCAP_IP_tap(const char* iface, int snaplen,
bool promisc, int to_ms){
int promisc_integer = promisc ? 1 : 0;
this->handle = pcap_open_live(iface,snaplen,promisc_integer,to_ms,this->m_errbuf);
this->flow_table = unordered_map<uint64_t,u_int>();
if(handle==NULL){
cerr << "Couldn't reach interface: " << this->m_errbuf << endl;
}
}
PCAP_IP_tap(const char* iface){
this->handle = pcap_open_offline(iface,this->m_errbuf);
if(handle==NULL){
cerr << "Couldn't open file: " << this->m_errbuf << endl;
}
}
~PCAP_IP_tap(){
pcap_close(this->handle);
}
// Member Functions()
void loop(int n)
{
auto ref = reinterpret_cast<u_char*>(this);
if(pcap_loop(this->handle,n,PCAP_IP_tap::callback,ref) == -1){
cerr << "<some error message should be displayed here...>" << endl;
return;
}
}
void packet_handler(const struct pcap_pkthdr *metadata,const u_char *data){
char ip_src[16], ip_dst[16];
auto eh = reinterpret_cast <const struct ether_header*>(data);
const struct vlan_ethhdr *vlan_eh = nullptr;
if(ntohs(eh->ether_type) == ETHERTYPE_VLAN){
vlan_eh = reinterpret_cast <const struct vlan_ethhdr*>(data);
if(ntohs(vlan_eh->h_vlan_encapsulated_proto) != ETHERTYPE_IP){
cout << "not an IP packet..." << endl;
return;
}
}else{
if(ntohs(eh->ether_type) != ETHERTYPE_IP){
cout << "not an IP packet..." << endl;
return;
}
}
const struct iphdr* ih = (vlan_eh) ?
reinterpret_cast <const struct iphdr*>(vlan_eh+1) :
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)
);
uint64_t tmp = 0 | (ih->saddr);
uint64_t key = (tmp << 32) | (ih->daddr);
this->flow_table[key]++;
//cout << this->flow_table[key] << endl;
//auto protocol = (ih->protocol == IPPROTO_TCP) ? "TCP" : "";
//cout << "ip source: " << ip_src << ", ip destination: " << ip_dst << " " << protocol << endl;
}//end handler method
friend ostream& operator<<(ostream& os, PCAP_IP_tap &pit);
};
ostream& operator<< (ostream& os, PCAP_IP_tap &pit)
{
char ip_src[16], ip_dst[16];
for(auto elem : pit.flow_table){
int saddr, daddr;
saddr = 0 | (elem.first >> 32);
daddr = 0 | (elem.first);
//src addr
inet_ntop(
AF_INET,
reinterpret_cast<const void *>(&saddr),
ip_src,
sizeof(ip_src)
);
//dst addr
inet_ntop(
AF_INET,
reinterpret_cast<const void *>(&daddr),
ip_dst,
sizeof(ip_dst)
);
os << "src: " << ip_src << ", dst: " << ip_dst << ": " << elem.second << endl;
}
return os;
}
int main(int argc, char *argv[]){
if(argc!=3+1){
cout << argc << endl;
cout << "Usage is: " << argv[0] << " (-i|-f) <iface><filename> <num_pkts>" << endl;
return -1;
}
//args
bool fromFile;
if(strcmp(argv[1],"-f")==0){
fromFile = true;
}else if (strcmp(argv[1],"-i")==0)
{
fromFile = false;
}else{
cout << "Usage is: " << argv[0] << " (-i|-f) <inf_name> <num_pkts>" << endl;
return -1;
}
auto if_name = argv[2];
int num_pkts = atoi(argv[3]);
//pars
int snaplen = 34;
bool promisc = true;
int to_ms = 1000;
PCAP_IP_tap *device = NULL;
if(fromFile){
device = new PCAP_IP_tap(if_name);
}else{
device = new PCAP_IP_tap(if_name,snaplen,promisc,to_ms);
}
device->loop(num_pkts);
cout << (*device) << endl;
}