qt/ledset/globalfunc.cpp
2023-09-01 16:17:33 +08:00

148 lines
5.4 KiB
C++

#include "globalfunc.h"
#include "gutil/qgui.h"
#include "pcap.h"
#include <QMessageBox>
#include <QPushButton>
#include <QDialogButtonBox>
#include <QEventLoop>
QString gFileHome;
HeadMap headMap;
HeadMap_ZRF headMap_zrf; //added by alahover 20230822
St_Zrf_Base_Param st_zrf_base_param; //added by alahover 20230823
St_Zrf_Cmd_Param st_zrf_cmd_param; //added by alahover 20230823
St_MiaoDian_Param st_miaodian_param; //added by alahover 20230823
byte *getSepas() {
auto sepas = new byte[headMap.end+4]{0};
sepas[headMap.ver] = ' ';
sepas[headMap.srv] = ' ';
sepas[headMap.len] = ' ';
sepas[headMap.tgtAddr] = ' ';
sepas[headMap.srcAddr] = ' ';
sepas[headMap.ptr] = ' ';
sepas[headMap.ans] = ' ';
sepas[headMap.chk] = ' ';
sepas[headMap.body] = '\n';
sepas[headMap.bodylen] = ' ';
sepas[headMap.end] = ' ';
return sepas;
}
byte *sepas = getSepas();
pcap_t *pcapSend{0};
PcapReThread *reThd{0};
int sendMsgSync(QByteArray &msg, int id, qint64 timeout, WaitingDlg *waitingDlg) {
QEventLoop loop;
auto res = sendMsg(msg, id, timeout, [=, &loop](int code, const QByteArray data) {
loop.exit(code);
}, waitingDlg);
if(res==0) res = loop.exec(QEventLoop::ExcludeUserInputEvents);
if(res && waitingDlg) waitingDlg->close();
return res;
}
QByteArray getNetDev(QWidget *parent, QByteArray def, bool justReturnIfHave) {
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{
{"name", "网卡名称"},
{"desc", "网卡描述"},
{"loopback", "Loopback"},
{"ip", "IP"},
{"netmask", "Netmask"},
{"broadaddr", "Broad Addr"},
{"dstaddr", "Dst Addr"}
};
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) {
if(justReturnIfHave && ! def.isEmpty() && dev->name == def) {
pcap_freealldevs(devs);
table->deleteLater();
return def;
}
auto rr = table->rowCount();
table->setRowCount(rr+1);
table->setText(rr, "name", dev->name);
table->setText(rr, "desc", dev->description);
table->setText(rr, "loopback", (dev->flags & PCAP_IF_LOOPBACK)?"True":"False");
auto ip = (u_char *) &((sockaddr_in *) a->addr)->sin_addr.s_addr;
table->setText(rr, "ip", QString::asprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]));
if(a->netmask) {
ip = (u_char *) &((sockaddr_in *) a->netmask)->sin_addr.s_addr;
table->setText(rr, "netmask", QString::asprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]));
}
if(a->broadaddr) {
ip = (u_char *) &((sockaddr_in *) a->broadaddr)->sin_addr.s_addr;
table->setText(rr, "broadaddr", QString::asprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]));
}
if(a->dstaddr) {
ip = (u_char *) &((sockaddr_in *) a->dstaddr)->sin_addr.s_addr;
table->setText(rr, "dstaddr", QString::asprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]));
}
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 {
QDialog dlg(parent);
dlg.setWindowFlag(Qt::WindowMaximizeButtonHint);
dlg.setWindowTitle("选择网卡");
dlg.resize(900, 300);
auto vBox = new QVBoxLayout(&dlg);
vBox->setContentsMargins(0,0,0,0);
vBox->setSpacing(3);
vBox->addSpacing(30);
table->setDefs();
table->setSelectionMode(QAbstractItemView::SingleSelection);
table->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
if(! def.isEmpty()) for(int r=0; r<table->rowCount(); r++) if(table->item(r, "name")->text()==def) table->selectRow(r);
QByteArray name;
QObject::connect(table, &Table::cellDoubleClicked, &dlg, [&, table](int row) {
name = table->item(row, "name")->text().toLocal8Bit();
dlg.accept();
});
vBox->addWidget(table);
auto btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Close);
QObject::connect(btnBox, &QDialogButtonBox::rejected, &dlg, &QDialog::reject);
QObject::connect(btnBox, &QDialogButtonBox::accepted, &dlg, [&, table] {
auto sels = table->selectedRanges();
if(sels.isEmpty()) {
QMessageBox::warning(&dlg, "Warning", "请选择网卡");
return;
}
name = table->item(sels[0].topRow(), "name")->text().toLocal8Bit();
dlg.accept();
});
vBox->addWidget(btnBox);
dlg.exec();
return name;
}
}