-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
230 lines (196 loc) · 6.24 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QObject>
#include "packet.h"
#include <QString>
#include <iostream>
#include <pthread.h>
#include <QMessageBox>
int nums = 0;
pthread_t pid;
void packet_table_init(Ui::MainWindow *ui);
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
sniffer = 0;
setWindowTitle(tr("my sniffer"));
packet_table_init(ui);
ui->packet_table->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->packet_table->setSelectionMode(QAbstractItemView::SingleSelection);
connect(ui->packet_table, SIGNAL(cellClicked(int, int)), this, SLOT(show_raw(int, int)));
connect(ui->packet_table, SIGNAL(cellClicked(int, int)), this, SLOT(show_hex(int, int)));
connect(ui->filter, SIGNAL(released()), this, SLOT(setFilter()));
connect(ui->start, SIGNAL(released()), this, SLOT(start_capture_wrapper()));
connect(ui->stop, SIGNAL(released()), this, SLOT(stop_capture()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setFilter()
{
QString filter = ui->filter_edit->text();
std::cout<< filter.toStdString();
if(sniffer)
sniffer->set_filter(filter.toStdString().c_str());
//start_capture("en0", filter.toStdString().c_str());
}
void MainWindow::show_raw(int a, int b)
{
std::cout<<"called"<<std::endl;
packet *p = &packets[a];
std::cout<<p->pdu->rfind_pdu<Tins::IP>().src_addr();
std::vector<uint8_t> s = p->pdu->serialize();
std::string str;
for(auto i : s)
{
if(i >= 0x20 && i <= 0x7e)
str += i;
else
str +='.';
}
ui->raw_table->setText(QString::fromStdString(str));
w_ptr->update();
}
void MainWindow::show_hex(int a, int b)
{
std::cout<<"called"<<std::endl;
packet *p = &packets[a];
std::cout<<p->pdu->rfind_pdu<Tins::IP>().src_addr();
std::vector<uint8_t> s = p->pdu->serialize();
std::string str;
int num = 0;
for(auto i : s)
{
char temp[4];
sprintf(temp, "%02X ", i);
str += temp;
num++;
if(num == 16)
{
str+="\n";
num = 0;
}
}
ui->format_table->setText(QString::fromStdString(str));
w_ptr->update();
}
void packet_table_init(Ui::MainWindow *ui)
{
ui->packet_table->setRowCount(0);
ui->packet_table->setColumnCount(7);
ui->packet_table->setHorizontalHeaderLabels(QStringList() << QObject::tr("序号")
<< QObject::tr("源MAC地址") << QObject::tr("目的MAC地址")
<< QObject::tr("帧类型")
<< QObject::tr("源IP地址") << QObject::tr("目的IP地址")
<< QObject::tr("协议类型"));
}
void MainWindow::insert_packet(packet* p)
{
QTableWidget* table = ui->packet_table;
int row_id = table->rowCount();
QTableWidgetItem *item_p = NULL;
uint32_t s = 0;
QString hw;
switch(p->pdu->inner_pdu()->pdu_type())
{
case Tins::PDU::ARP:
ui->packet_table->insertRow(row_id);
ui->packet_table->setItem(row_id, 0, int2tableItem(table->rowCount()-1));
hw = QString::fromStdString(p->pdu->rfind_pdu<Tins::ARP>().sender_hw_addr().to_string());
item_p = new QTableWidgetItem(hw);
table->setItem(row_id, 2, item_p);
delete item_p;
break;
case Tins::PDU::IP:
ui->packet_table->insertRow(row_id);
ui->packet_table->setItem(row_id, 0, int2tableItem(table->rowCount()-1));
s = p->pdu->rfind_pdu<Tins::IP>().src_addr();
item_p = int2ip(s);
table->setItem(row_id, 4, item_p);
//delete item_p;
s = p->pdu->rfind_pdu<Tins::IP>().dst_addr();
item_p = int2ip(s);
table->setItem(row_id, 5, item_p);
//delete item_p;
item_p = new QTableWidgetItem(QString("IP"));
table->setItem(row_id, 3, item_p);
//delete item_p;
hw = QString::fromStdString(p->pdu->rfind_pdu<Tins::EthernetII>().src_addr().to_string());
item_p = new QTableWidgetItem(hw);
table->setItem(row_id, 1, item_p);
//delete item_p;
hw = QString::fromStdString(p->pdu->rfind_pdu<Tins::EthernetII>().dst_addr().to_string());
item_p = new QTableWidgetItem(hw);
table->setItem(row_id, 2, item_p);
switch(p->pdu->inner_pdu()->inner_pdu()->pdu_type())
{
case Tins::PDU::TCP:
item_p = new QTableWidgetItem(QString("TCP"));
table->setItem(row_id, 6, item_p);
break;
case Tins::PDU::UDP:
item_p = new QTableWidgetItem(QString("UDP"));
table->setItem(row_id, 6, item_p);
break;
default:
break;
}
break;
default:
return;
}
}
void* loop_func(void* sniffer)
{
//sniffer->sniff_loop(packet_h);
std::cout<<"start loop"<<std::endl;
Tins::Sniffer* sp = (Tins::Sniffer*)sniffer;
while(SNIFFER_CONTINUE)
{
if(!sniffer)
{
std::cout<<"no sniffer"<<std::endl;
}
Tins::Packet p = sp->next_packet();
if(!p||!p.pdu()->find_pdu<Tins::IP>())
{
return NULL;
}
Tins::PDU* pdu = p.pdu();
packet_h(pdu);
}
return NULL;
}
void MainWindow::start_capture_wrapper()
{
SNIFFER_CONTINUE = true;
QString interface = ui->interfaceEdit->text();
QString filter = ui->filter_edit->text();
std::cout<<"start!"<<std::endl;
start_capture(interface.toStdString().c_str(), filter.toStdString().c_str());
}
void MainWindow::stop_capture()
{
SNIFFER_CONTINUE = false;
pthread_kill(pid, NULL);
}
void start_capture(const char* interface, const char* filter )
{
Tins::SnifferConfiguration config;
config.set_filter(filter);
config.set_promisc_mode(true);
try
{
sniffer = new Tins::Sniffer(interface, config);
}
catch(...)
{
w_ptr->warn();
sniffer = NULL;
return;
}
pthread_create(&pid, NULL, loop_func, sniffer);
}