-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_packets.cpp
327 lines (263 loc) · 10.4 KB
/
read_packets.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "read_packets.hpp"
#include "packet_sniffer.h"
#include "packets_util.hpp"
#include <iostream>
#define DEVICE_FILE "/dev/packet_sniffer"
#define PKTS_BATCH 50
#define ID_BLOCK_SRC 0x1122
#define ID_BLOCK_DST 0x1123
inline wxColour color_by_protocol(int protocol) {
switch(protocol) {
case IPPROTO_TCP:
return wxColour(203, 245, 221);
case IPPROTO_UDP:
return wxColour(213, 255, 255);
}
return wxColour(255,255,255);
}
bool PacketReader::OnInit()
{
PacketReaderWindow *window = new PacketReaderWindow(wxT("Packet sniffer"));
window->SetSize(wxSize(1920, 1080));
window->SetAutoLayout(true);
window->Show(true);
return true;
}
void PacketReaderWindow::OnAbout(wxCommandEvent& event)
{
wxString msg;
msg.Printf(wxT("Packet sniffer and analyser"));
wxMessageBox(msg, wxT("About"), wxOK | wxICON_INFORMATION, this);
}
void PacketReaderWindow::OnQuit(wxCommandEvent& event)
{
running = false;
Close(true);
}
void PacketReaderWindow::OnPopupClick(wxCommandEvent& event)
{
struct net_packet pkt;
void *data = static_cast<wxMenu*>(event.GetEventObject())->GetClientData();
int index = static_cast<int>(reinterpret_cast<std::intptr_t>(data));
if(index > packets.size())
return;
{
std::unique_lock<std::mutex> lock(packetMutex);
pkt = packets[index];
}
auto [src, dst] = pkt_get_ips(pkt);
switch(event.GetId()) {
case ID_BLOCK_SRC:
wxMessageBox(wxString::Format("Block source: %s", src), wxT("Ban address"), wxOK | wxICON_INFORMATION, this);
break;
case ID_BLOCK_DST:
wxMessageBox(wxString::Format("Block destination: %s", dst), wxT("Ban address"), wxOK | wxICON_INFORMATION, this);
break;
}
}
void PacketReaderWindow::ShowContextMenu(wxListEvent& event)
{
int index = event.GetItem();
void *data = reinterpret_cast<void*>(index);
wxMenu popup;
popup.SetClientData(data);
popup.Append(ID_BLOCK_SRC, "Ban source IP");
popup.Append(ID_BLOCK_DST, "Ban destination IP");
popup.Connect(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(PacketReaderWindow::OnPopupClick), NULL, this);
PopupMenu(&popup);
}
void PacketReaderWindow::OnMouseDownEvent(wxListEvent& event)
{
wxMouseState mouseState = wxGetMouseState();
if(mouseState.RightIsDown()) {
return;
}
struct net_packet pkt;
int index = event.GetItem();
if(index < packets.size())
{
{
std::unique_lock<std::mutex> lock(packetMutex);
pkt = packets[index];
}
detailsTree->DeleteAllItems();
auto [eth_src, eth_dst] = pkt_get_eth_addr(pkt);
wxTreeItemId eth_root = detailsTree->AddRoot("Ethernet Frame");
detailsTree->AppendItem(eth_root, wxString::Format("Source: %s", eth_src));
detailsTree->AppendItem(eth_root, wxString::Format("Destination: %s", eth_dst));
detailsTree->AppendItem(eth_root, wxString::Format("Transport Protocol: %s", pkt_get_protocol(pkt)));
detailsTree->Expand(eth_root);
wxTreeItemId ip;
auto [src, dst] = pkt_get_ips(pkt);
switch(htons(pkt.eth_protocol)) {
case ETH_P_IP: {
ip = detailsTree->InsertItem(eth_root, 3, "IPv4 Header");
detailsTree->AppendItem(ip, wxString::Format("src: %s", src));
detailsTree->AppendItem(ip, wxString::Format("dest: %s", dst));
detailsTree->AppendItem(ip, wxString::Format("TOS: %u", pkt.network.ipv4h.tos));
detailsTree->AppendItem(ip, wxString::Format("TTL: %u", pkt.network.ipv4h.ttl));
detailsTree->Expand(ip);
break;
}
case ETH_P_IPV6: {
ip = detailsTree->InsertItem(eth_root, 3, "IPv6 Header");
detailsTree->AppendItem(ip, wxString::Format("src: %s", src));
detailsTree->AppendItem(ip, wxString::Format("dest: %s", dst));
detailsTree->AppendItem(ip, wxString::Format("payload len: %u", pkt.network.ipv6h.payload_len));
detailsTree->AppendItem(ip, wxString::Format("hop limit: %u", pkt.network.ipv6h.hop_limit));
detailsTree->Expand(ip);
break;
}
}
switch(pkt.protocol) {
case NEXTHDR_ICMP: {
wxTreeItemId icmp = detailsTree->InsertItem(ip, 4, "ICMPv6 Header");
detailsTree->AppendItem(icmp, wxString::Format("Type: %s", pkt_icmpv6_get_type(pkt)));
detailsTree->AppendItem(icmp, wxString::Format("Sequence: %u", pkt.transport.icmph.icmpv6h.icmp6_dataun.u_echo.sequence));
detailsTree->Expand(icmp);
break;
}
case IPPROTO_TCP: {
wxTreeItemId root = detailsTree->InsertItem(ip, 4, "TCP Header");
detailsTree->AppendItem(root, wxString::Format("src port: %u", pkt.transport.tcph.source));
detailsTree->AppendItem(root, wxString::Format("dst port: %u", pkt.transport.tcph.dest));
detailsTree->AppendItem(root, wxString::Format("seq: %u", ntohl(pkt.transport.tcph.seq)));
detailsTree->AppendItem(root, wxString::Format("ack_seq: %u", ntohl(pkt.transport.tcph.ack_seq)));
detailsTree->AppendItem(root, wxString::Format("check: %u", ntohs(pkt.transport.tcph.check)));
wxTreeItemId flags = detailsTree->InsertItem(root, 4, "Flags");
detailsTree->AppendItem(flags, wxString::Format("doff: %u", pkt.transport.tcph.doff << 2));
detailsTree->AppendItem(flags, wxString::Format("fin: %u", pkt.transport.tcph.fin));
detailsTree->AppendItem(flags, wxString::Format("syn: %u", pkt.transport.tcph.syn));
detailsTree->AppendItem(flags, wxString::Format("rst: %u", pkt.transport.tcph.rst));
detailsTree->AppendItem(flags, wxString::Format("ack: %u", pkt.transport.tcph.ack));
detailsTree->Expand(root);
break;
}
case IPPROTO_UDP: {
wxTreeItemId root = detailsTree->InsertItem(eth_root, 4, "UDP Header");
detailsTree->AppendItem(root, wxString::Format("src port: %u", pkt.transport.udph.source));
detailsTree->AppendItem(root, wxString::Format("dst port: %u", pkt.transport.udph.dest));
detailsTree->AppendItem(root, wxString::Format("len: %u", ntohs(pkt.transport.udph.len)));
detailsTree->AppendItem(root, wxString::Format("check: %u", ntohs(pkt.transport.udph.check)));
detailsTree->Expand(root);
break;
}
case IPPROTO_IGMP:{
wxTreeItemId root = detailsTree->InsertItem(eth_root, 4, "IGMP Header");
detailsTree->AppendItem(root, wxString::Format("type: %s", pkt_igmp_get_type(pkt)));
detailsTree->Expand(root);
break;
}
}
}
}
void PacketReaderWindow::StartPacketReader()
{
running = true;
readerThread = std::thread([this]() {
int fd = open(DEVICE_FILE, O_RDONLY);
if(fd < 0) {
wxMessageBox("Failed to open device file", "Error", wxICON_ERROR);
return;
}
while(running)
{
struct net_packet pkts[PKTS_BATCH] = {0};
ssize_t bytes_read = read(fd, pkts, sizeof(struct net_packet) * PKTS_BATCH);
if(bytes_read > 0) {
int num_packets = bytes_read / sizeof(struct net_packet);
std::thread([this, pkts, num_packets]() {
std::scoped_lock lock(packetMutex, queueMutex);
for(int i=0; i < num_packets; i++) {
packets.emplace_back(pkts[i]);
incomingPackets.emplace(pkts[i]);
}
}).detach();
wxThreadEvent event(wxEVT_THREAD, wxID_ANY);
wxQueueEvent(this, event.Clone());
}
}
close(fd);
});
Bind(wxEVT_THREAD, [this](wxThreadEvent&) {
struct net_packet pkt;
int itemCount;
{
std::scoped_lock lock(queueMutex);
if(incomingPackets.empty()) return;
pkt = incomingPackets.front();
incomingPackets.pop();
itemCount = pktList->GetItemCount();
}
wxString cpuid_str = pkt_get_cpuid(pkt);
wxString skb_len_str = pkt_get_len(pkt);
wxString protocol = pkt_get_protocol(pkt);;
wxString timestamp = pkt_get_time(pkt);
auto [src, dst] = pkt_get_ips(pkt);
auto [src_port, dst_port] = pkt_get_ports(pkt);
long index = pktList->InsertItem(itemCount, wxString::Format("%d", itemCount+1));
pktList->SetItem(index, 1, cpuid_str);
pktList->SetItem(index, 2, src);
pktList->SetItem(index, 3, src_port);
pktList->SetItem(index, 4, dst);
pktList->SetItem(index, 5, dst_port);
pktList->SetItem(index, 6, timestamp);
pktList->SetItem(index, 7, protocol);
pktList->SetItem(index, 8, skb_len_str);
pktList->SetItemBackgroundColour(index, color_by_protocol(pkt.protocol));
});
}
PacketReaderWindow::~PacketReaderWindow() {
running = false;
if (readerThread.joinable()) {
readerThread.join();
}
}
PacketReaderWindow::PacketReaderWindow(const wxString& title) : wxFrame(NULL, wxID_ANY, title)
{
/* MENU */
wxMenu *fileMenu = new wxMenu;
wxMenu *helpMenu = new wxMenu;
helpMenu->Append(wxID_ABOUT, wxT("&About...\tF1"), wxT("Show About dialog"));
fileMenu->Append(wxID_EXIT, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append(fileMenu, wxT("&File"));
menuBar->Append(helpMenu, wxT("&Help"));
SetMenuBar(menuBar);
/* STATUS BAR */
CreateStatusBar(2);
SetStatusText(wxT("Packet Sniffer"));
/* WINDOW CONTENT */
wxBoxSizer *contSizer = new wxBoxSizer(wxVERTICAL);
wxSplitterWindow *splitter = new wxSplitterWindow(this, wxID_ANY);
// packet and detail panels
wxPanel *packetsPanel = new wxPanel(splitter, wxID_ANY, wxDefaultPosition, GetSize());
wxPanel *detailsPanel = new wxPanel(splitter, wxID_ANY);
// top panel used to show packets
pktList = new wxListCtrl(packetsPanel, wxID_ANY, wxDefaultPosition, wxSize(GetSize().GetWidth(), -1), wxLC_REPORT | wxLC_SINGLE_SEL | wxBORDER_SUNKEN);
pktList->InsertColumn(0, "Pkt #", wxLIST_FORMAT_LEFT);
pktList->InsertColumn(1, "CPU #", wxLIST_FORMAT_LEFT);
pktList->InsertColumn(2, "Source IP", wxLIST_FORMAT_LEFT, 300);
pktList->InsertColumn(3, "Port", wxLIST_FORMAT_LEFT);
pktList->InsertColumn(4, "Destination IP", wxLIST_FORMAT_LEFT, 300);
pktList->InsertColumn(5, "Port", wxLIST_FORMAT_LEFT);
pktList->InsertColumn(6, "Timestamp", wxLIST_FORMAT_LEFT, 250);
pktList->InsertColumn(7, "Protocol", wxLIST_FORMAT_LEFT, 150);
pktList->InsertColumn(8, "Length", wxLIST_FORMAT_LEFT);
wxBoxSizer *pktListSizer = new wxBoxSizer(wxVERTICAL);
pktListSizer->Add(pktList, 1, wxEXPAND | wxALL, 5);
packetsPanel->SetSizer(pktListSizer);
// bottom panel, used to show details
detailsTree = new wxTreeCtrl(detailsPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_DEFAULT_STYLE | wxBORDER_SUNKEN);
wxBoxSizer *detailSizer = new wxBoxSizer(wxVERTICAL);
detailSizer->Add(detailsTree, 1, wxEXPAND | wxALL, 5);
detailsPanel->SetSizer(detailSizer);
splitter->SplitHorizontally(packetsPanel, detailsPanel, (int)(0.80 * GetSize().GetHeight()));
contSizer->Add(splitter, 1, wxEXPAND);
SetSizerAndFit(contSizer);
/* LIST(S) EVENTS */
pktList->Bind(wxEVT_LIST_ITEM_SELECTED, &PacketReaderWindow::OnMouseDownEvent, this);
/* CONTEXT MENU, wxFrame */
pktList->Bind(wxEVT_LIST_ITEM_RIGHT_CLICK, &PacketReaderWindow::ShowContextMenu, this);
StartPacketReader();
}