2022-12-16 15:08:53 +08:00
|
|
|
#include "globalfunc.h"
|
2023-03-31 18:44:09 +08:00
|
|
|
#include "gutil/qgui.h"
|
2022-12-16 15:08:53 +08:00
|
|
|
#define HAVE_REMOTE
|
|
|
|
#include "pcap.h"
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QPushButton>
|
2023-05-27 17:43:57 +08:00
|
|
|
#include <QDialogButtonBox>
|
2022-12-16 15:08:53 +08:00
|
|
|
|
2023-05-27 17:43:57 +08:00
|
|
|
HeadMap headMap;
|
2023-03-20 12:11:58 +08:00
|
|
|
pcap_t *pcapSend{0};
|
|
|
|
PcapReThread *reThd{0};
|
|
|
|
|
2023-03-24 18:41:48 +08:00
|
|
|
QByteArray getNetDev(QWidget *parent, QByteArray def, bool justReturnIfHave) {
|
2022-12-16 15:08:53 +08:00
|
|
|
char errbuf[PCAP_ERRBUF_SIZE];
|
|
|
|
pcap_if_t *devs;
|
|
|
|
if(pcap_findalldevs(&devs, errbuf) == -1) {
|
|
|
|
QMessageBox::critical(parent, "Error", QString("寻找网卡失败")+errbuf);
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
auto table = new Table{
|
2023-05-27 17:43:57 +08:00
|
|
|
{"name", "网卡名称"},
|
|
|
|
{"desc", "网卡描述"},
|
|
|
|
{"loopback", "Loopback"},
|
|
|
|
{"ip", "IP"},
|
|
|
|
{"netmask", "Netmask"},
|
|
|
|
{"broadaddr", "Broad Addr"},
|
|
|
|
{"dstaddr", "Dst Addr"}
|
2022-12-16 15:08:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
pcap_if_t *device = 0;
|
|
|
|
for(pcap_if_t *dev = devs; dev; dev = dev->next) {
|
|
|
|
pcap_addr_t *a;
|
|
|
|
for(a=dev->addresses; a; a=a->next) {
|
|
|
|
auto sa_family = a->addr->sa_family;
|
|
|
|
if(sa_family==AF_INET) {
|
2023-03-24 18:41:48 +08:00
|
|
|
if(justReturnIfHave && ! def.isEmpty() && dev->name == def) {
|
2022-12-16 15:08:53 +08:00
|
|
|
pcap_freealldevs(devs);
|
|
|
|
table->deleteLater();
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
auto rr = table->rowCount();
|
|
|
|
table->setRowCount(rr+1);
|
2023-03-31 18:44:09 +08:00
|
|
|
table->setText(rr, "name", dev->name);
|
|
|
|
table->setText(rr, "desc", dev->description);
|
|
|
|
table->setText(rr, "loopback", (dev->flags & PCAP_IF_LOOPBACK)?"True":"False");
|
2022-12-16 15:08:53 +08:00
|
|
|
auto ip = (u_char *) &((sockaddr_in *) a->addr)->sin_addr.s_addr;
|
2023-03-31 18:44:09 +08:00
|
|
|
table->setText(rr, "ip", QString::asprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]));
|
2022-12-16 15:08:53 +08:00
|
|
|
if(a->netmask) {
|
|
|
|
ip = (u_char *) &((sockaddr_in *) a->netmask)->sin_addr.s_addr;
|
2023-03-31 18:44:09 +08:00
|
|
|
table->setText(rr, "netmask", QString::asprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]));
|
2022-12-16 15:08:53 +08:00
|
|
|
}
|
|
|
|
if(a->broadaddr) {
|
|
|
|
ip = (u_char *) &((sockaddr_in *) a->broadaddr)->sin_addr.s_addr;
|
2023-03-31 18:44:09 +08:00
|
|
|
table->setText(rr, "broadaddr", QString::asprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]));
|
2022-12-16 15:08:53 +08:00
|
|
|
}
|
|
|
|
if(a->dstaddr) {
|
|
|
|
ip = (u_char *) &((sockaddr_in *) a->dstaddr)->sin_addr.s_addr;
|
2023-03-31 18:44:09 +08:00
|
|
|
table->setText(rr, "dstaddr", QString::asprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]));
|
2022-12-16 15:08:53 +08:00
|
|
|
}
|
|
|
|
if(device==0) device = dev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pcap_freealldevs(devs);
|
|
|
|
|
|
|
|
auto rowCnt = table->rowCount();
|
|
|
|
if(rowCnt==0) {
|
|
|
|
table->deleteLater();
|
|
|
|
QMessageBox::critical(parent, "Error", "没找到 internet 设备");
|
|
|
|
return QByteArray();
|
|
|
|
} else if(rowCnt==1) {
|
|
|
|
table->deleteLater();
|
|
|
|
return table->item(0, "name")->text().toLocal8Bit();
|
|
|
|
} else {
|
2023-05-27 17:43:57 +08:00
|
|
|
QDialog dlg(parent);
|
|
|
|
dlg.setWindowFlag(Qt::WindowMaximizeButtonHint);
|
|
|
|
dlg.setWindowTitle("选择网卡");
|
|
|
|
dlg.resize(900, 300);
|
2022-12-16 15:08:53 +08:00
|
|
|
|
2023-05-27 17:43:57 +08:00
|
|
|
auto vBox = new QVBoxLayout(&dlg);
|
2022-12-16 15:08:53 +08:00
|
|
|
vBox->setContentsMargins(0,0,0,0);
|
|
|
|
vBox->setSpacing(3);
|
|
|
|
vBox->addSpacing(30);
|
|
|
|
|
|
|
|
table->setDefs();
|
|
|
|
table->setSelectionMode(QAbstractItemView::SingleSelection);
|
|
|
|
table->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
|
2023-03-24 18:41:48 +08:00
|
|
|
if(! def.isEmpty()) for(int r=0; r<table->rowCount(); r++) if(table->item(r, "name")->text()==def) table->selectRow(r);
|
2022-12-16 15:08:53 +08:00
|
|
|
QByteArray name;
|
2023-05-27 17:43:57 +08:00
|
|
|
QObject::connect(table, &Table::cellDoubleClicked, &dlg, [&, table](int row) {
|
2022-12-16 15:08:53 +08:00
|
|
|
name = table->item(row, "name")->text().toLocal8Bit();
|
2023-05-27 17:43:57 +08:00
|
|
|
dlg.accept();
|
2022-12-16 15:08:53 +08:00
|
|
|
});
|
|
|
|
vBox->addWidget(table);
|
|
|
|
|
2023-05-27 17:43:57 +08:00
|
|
|
|
|
|
|
auto btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Close);
|
|
|
|
QObject::connect(btnBox, &QDialogButtonBox::rejected, &dlg, &QDialog::reject);
|
|
|
|
QObject::connect(btnBox, &QDialogButtonBox::accepted, &dlg, [&, table] {
|
2022-12-16 15:08:53 +08:00
|
|
|
auto sels = table->selectedRanges();
|
|
|
|
if(sels.isEmpty()) {
|
2023-05-27 17:43:57 +08:00
|
|
|
QMessageBox::warning(&dlg, "Warning", "请选择网卡");
|
2022-12-16 15:08:53 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
name = table->item(sels[0].topRow(), "name")->text().toLocal8Bit();
|
2023-05-27 17:43:57 +08:00
|
|
|
dlg.accept();
|
2022-12-16 15:08:53 +08:00
|
|
|
});
|
2023-05-27 17:43:57 +08:00
|
|
|
vBox->addWidget(btnBox);
|
2022-12-16 15:08:53 +08:00
|
|
|
|
2023-05-27 17:43:57 +08:00
|
|
|
dlg.exec();
|
2022-12-16 15:08:53 +08:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|