Repository navigation
Expand file tree
/
Copy pathnetwork_handle.cpp
More file actions
307 lines (274 loc) · 9.04 KB
/
Copy pathnetwork_handle.cpp
File metadata and controls
307 lines (274 loc) · 9.04 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include "network_handle.h"
#include "shared_storage.h"
#include "shared_storage_handle.h"
#include <qlogging.h>
#include <thread>
#include <tins/ethernetII.h>
#include <tins/exceptions.h>
#include <tins/packet_sender.h>
#include <tins/pdu.h>
void NetworkThreadHandle::start()
{
{
auto guard = storageHandle_m.guard();
guard.storage.interfaces[interface_m] = {};
guard.storage.interfaces[interface_m].control.running = true;
guard.storage.interfaces[interface_m].up = true;
}
// the actual start
thread_m = std::thread(&NetworkThreadHandle::thread, this);
}
void NetworkThreadHandle::signalStop()
{
auto guard = storageHandle_m.guard();
guard.storage.interfaces[interface_m].control.running = false;
}
// the network thread function itself
void NetworkThreadHandle::thread()
{
Tins::SnifferConfiguration config;
config.set_promisc_mode(true);
config.set_immediate_mode(true);
config.set_timeout(500);
Tins::Sniffer reader(interface_m.name(), config);
reader.sniff_loop([&](Tins::PDU & packet) {
qInfo("Received a packet!");
if (!packet.find_pdu<Tins::EthernetII>())
{
qInfo("Non-EthernetII packet");
auto guard = storageHandle_m.guard();
return guard.storage.interfaces[interface_m].control.running;
}
auto guard = storageHandle_m.guard();
SnifferHelper me(guard, interface_m);
auto eth = packet.rfind_pdu<Tins::EthernetII>();
// is this interface up?
if (!me.up())
{
qDebug("The interface %s is down, skipping", interface_m.hw_address().to_string().c_str());
return me.running();
}
// did we send this packet?
if (guard.storage.sentPackets.count(packet) == 1)
{
qDebug("Found a duplicate packet on interface %s, skipping",
interface_m.hw_address().to_string().c_str());
return me.running();
}
// record the packet as input
inputStatistics(packet, interface_m, guard);
// did our device send this?
if (eth.src_addr() == interface_m.hw_address())
{
qDebug("The packet on interface %s was sent by that interface, skipping",
interface_m.hw_address().to_string().c_str());
return me.running();
}
// is this is a multicast?
if (eth.dst_addr().is_multicast())
{
qDebug("Detected a multicast, sending it as broadcast");
broadcast(packet, guard);
return me.running();
}
// is this is a broadcast?
if (eth.dst_addr().is_broadcast())
{
qDebug("Detected a broadcast address");
broadcast(packet, guard);
return me.running();
}
// update MAC table
updateMac(eth.src_addr(), guard);
// did they send this packet to us?
if (eth.dst_addr() == interface_m.hw_address())
{
qDebug("The packet on interface %s was meant for that interface, skipping",
interface_m.hw_address().to_string().c_str());
return me.running();
}
// is the destination on this device?
for (const auto & entry : guard.storage.interfaces)
{
if (eth.dst_addr() == entry.first.hw_address())
{
qInfo("Switching packet to local device on interface %s", entry.first.hw_address().to_string().c_str());
send(packet, entry.first, guard);
return me.running();
}
}
// is destination address known?
if (me.macTable().count(eth.dst_addr()) == 1)
{
// did we get this packet on the same interface that we need to send
// it to?
if (me.macTable()[eth.dst_addr()].interface == interface_m)
{
qInfo("The recipient of the packet has already received it, skipping");
return me.running();
}
qInfo("Switching packet using MAC entry");
send(packet, me.macTable()[eth.dst_addr()].interface, guard);
return me.running();
}
// broadcasting
broadcast(packet, guard);
return me.running();
});
auto guard = storageHandle_m.guard();
guard.storage.interfaces[interface_m].control.finished = true;
qInfo("Thread %s is down", interface_m.hw_address().to_string().c_str());
}
void NetworkThreadHandle::send(Tins::PDU & packet, interface destination, storage_guard & guard)
{
Tins::PacketSender sender;
guard.storage.sentPackets.insert(packet);
if (packet.size() > 1500)
{
qDebug("Sending a big chungus!");
auto eth = packet.rfind_pdu<Tins::EthernetII>();
qDebug("%s -> %s", eth.src_addr().to_string().c_str(), eth.dst_addr().to_string().c_str());
if (destination.name().find("wlo") != string::npos)
{
qDebug("Cannot send jumbo to wifi!");
return;
}
}
if (packet.rfind_pdu<Tins::EthernetII>().dst_addr().is_broadcast())
{
qDebug("Sending a broadcast packet!");
}
if (!guard->interfaces[destination].up)
{
qDebug("The interface is down, cannot send anything through it.");
return;
}
outputStatistics(packet, destination, guard);
sender.send(packet, destination);
}
void NetworkThreadHandle::broadcast(Tins::PDU & packet, storage_guard & guard)
{
Tins::PacketSender sender;
guard.storage.sentPackets.insert(packet);
for (const auto & entry : guard.storage.interfaces)
{
if (entry.first == interface_m)
{
continue;
}
auto eth = packet.rfind_pdu<Tins::EthernetII>();
qInfo("Broadcasting the packet (src: %s, dst: %s) to interface %s", eth.src_addr().to_string().c_str(), eth.dst_addr().to_string().c_str(), entry.first.hw_address().to_string().c_str());
outputStatistics(packet, entry.first, guard);
sender.send(packet, entry.first);
}
}
void NetworkThreadHandle::inputStatistics(Tins::PDU & packet, interface net, storage_guard & guard)
{
if (packet.find_pdu<Tins::EthernetII>())
{
guard.storage.statisticsTable[{Protocol::EthernetII, net}].input++;
}
if (packet.find_pdu<Tins::ARP>())
{
guard.storage.statisticsTable[{Protocol::ARP, net}].input++;
}
if (packet.find_pdu<Tins::IP>())
{
guard.storage.statisticsTable[{Protocol::IP, net}].input++;
}
if (packet.find_pdu<Tins::TCP>())
{
guard.storage.statisticsTable[{Protocol::TCP, net}].input++;
}
if (packet.find_pdu<Tins::UDP>())
{
guard.storage.statisticsTable[{Protocol::UDP, net}].input++;
}
if (packet.find_pdu<Tins::ICMP>())
{
guard.storage.statisticsTable[{Protocol::ICMP, net}].input++;
}
try
{
auto tcp = packet.rfind_pdu<Tins::TCP>();
if (tcp.sport() == 80 || tcp.dport() == 80 || tcp.sport() == 443 || tcp.dport() == 443)
{
guard.storage.statisticsTable[{Protocol::HTTP, net}].input++;
}
}
catch (Tins::pdu_not_found & e)
{
}
}
void NetworkThreadHandle::outputStatistics(Tins::PDU & packet, interface net, storage_guard & guard)
{
if (packet.find_pdu<Tins::EthernetII>())
{
guard.storage.statisticsTable[{Protocol::EthernetII, net}].output++;
}
if (packet.find_pdu<Tins::ARP>())
{
guard.storage.statisticsTable[{Protocol::ARP, net}].output++;
}
if (packet.find_pdu<Tins::IP>())
{
guard.storage.statisticsTable[{Protocol::IP, net}].output++;
}
if (packet.find_pdu<Tins::TCP>())
{
guard.storage.statisticsTable[{Protocol::TCP, net}].output++;
}
if (packet.find_pdu<Tins::UDP>())
{
guard.storage.statisticsTable[{Protocol::UDP, net}].output++;
}
if (packet.find_pdu<Tins::ICMP>())
{
guard.storage.statisticsTable[{Protocol::ICMP, net}].output++;
}
try
{
auto tcp = packet.rfind_pdu<Tins::TCP>();
if (tcp.sport() == 80 || tcp.dport() == 80)
{
guard.storage.statisticsTable[{Protocol::HTTP, net}].output++;
}
}
catch (Tins::pdu_not_found & e)
{
}
}
void NetworkThreadHandle::updateMac(mac_address mac, storage_guard & guard)
{
if (mac.is_broadcast() || mac.is_multicast())
{
return;
}
guard.storage.macTable[mac] = {interface_m, guard.storage.deviceInfo.defaultMacTimeout};
}
NetworkThreadHandle::NetworkThreadHandle(SharedStorageHandle storageHandle, interface acceptingInterface)
: storageHandle_m(storageHandle),
interface_m(acceptingInterface),
thread_m{}
{
}
string NetworkThreadHandle::interfaceName() const
{
return interface_m.name();
}
interface NetworkThreadHandle::getInterface() const
{
return interface_m;
}
int32_t NetworkThreadHandle::id() const
{
return interface_m.id();
}
NetworkThreadHandle::~NetworkThreadHandle()
{
if (thread_m.joinable())
{
qDebug("Joining the network thread...");
thread_m.join();
}
}