ledset增加地址映射

This commit is contained in:
gangphon 2022-12-16 16:16:12 +08:00
parent 5f89e6d8b8
commit bfb8e549e4
12 changed files with 42 additions and 188 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 254 B

After

Width:  |  Height:  |  Size: 962 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 962 B

After

Width:  |  Height:  |  Size: 254 B

View File

@ -36,7 +36,6 @@ SOURCES += \
gqt.cpp \
main.cpp \
mainwin.cpp \
pcapthread.cpp \
pcapwin.cpp \
screenunit.cpp \
table.cpp \
@ -53,7 +52,6 @@ HEADERS += \
globalfunc.h \
gqt.h \
mainwin.h \
pcapthread.h \
pcapwin.h \
screenunit.h \
table.h \

View File

@ -114,71 +114,6 @@ QSlider::handle:horizontal {width: 8px;}
return a.exec();
}
#define HAVE_REMOTE
#include "pcapthread.h"
void test() {
pcap_if_t *devs;
char errbuf[PCAP_ERRBUF_SIZE];
if(pcap_findalldevs(&devs, errbuf) == -1) {
qDebug() << "pcap_findalldevs" << errbuf;
return;
}
pcap_if_t *device = 0;
for(pcap_if_t *dev = devs; dev; dev = dev->next) {
qDebug() << dev->name << "[desc]" << dev->description << "Loopback" << ((dev->flags & PCAP_IF_LOOPBACK)?"yes":"no");
pcap_addr_t *a;
for(a=dev->addresses; a; a=a->next) {
auto sa_family = a->addr->sa_family;
if(sa_family==AF_INET) {
u_char *ip = (u_char *) &((sockaddr_in *) a->addr)->sin_addr.s_addr;
qDebug(" INET %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
if(a->netmask) {
u_char *ip = (u_char *) &((sockaddr_in *) a->netmask)->sin_addr.s_addr;
qDebug(" netmask %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
}
if(a->broadaddr) {
u_char *ip = (u_char *) &((sockaddr_in *) a->broadaddr)->sin_addr.s_addr;
qDebug(" Broadcast Address %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
}
if(a->dstaddr) {
u_char *ip = (u_char *) &((sockaddr_in *) a->dstaddr)->sin_addr.s_addr;
qDebug(" Destination Address %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
}
if(device==0) device = dev;
} else if(sa_family==AF_INET6) {
qDebug() << " INET6";
//printf("\tAddress: %s\n", ip6tos(a->addr, ip6str, sizeof(ip6str)));
} else qDebug() << " Address Family" << sa_family;
}
qDebug();
}
auto pcap = pcap_open_live(device->name, 65536, 1, 1000, errbuf);
if(pcap == 0) {
qDebug("Error openning adapter: %s\n", errbuf);
return;
}
//检查链路层的类型
if (pcap_datalink(pcap) != DLT_EN10MB) {
qDebug("This program only run on Ethernet networks\n");
// pcap_close(fp);
// pcap_freealldevs(alldevs);
}
auto capthd = new CapThread(pcap);
capthd->start();
QThread::sleep(1);
u_char data[] = {0x55, 0x55,
0x9A, 0x20, 0x00, 0x20,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00};
if(pcap_sendpacket(pcap, data, sizeof(data))) {
qDebug()<<pcap_geterr(pcap);
return;
}
//pcap_close(pcap);
//pcap_freealldevs(devs);
}

View File

@ -16,7 +16,6 @@
#include <QDebug>
#include "table.h"
#include "globalfunc.h"
#include <QMessageBox>
#include "basewin.h"
#include <QSettings>
#include <QDir>
@ -102,11 +101,8 @@ MainWin::MainWin() {
});
btnImg = addImg(imgsBar, QPixmap(":/imgs/video.png").scaledToWidth(128, Qt::SmoothTransformation), tr("协议调试"));
connect(btnImg, &ImgBtn::clicked, this, [=](){
if(pcapRe != 0 && pcapSend != 0)
{
auto ins = new PcapWin(pcapRe, pcapSend, this);
auto ins = PcapWin::newIns(net_name, this);
if(ins) ins->show();
}
});
btnImg = addImg(imgsBar, QPixmap(":/imgs/idea.png").scaledToWidth(128, Qt::SmoothTransformation), tr("模拟同步"));
connect(btnImg, &ImgBtn::clicked, this, [=](){

View File

@ -2,9 +2,11 @@
#define MAINWIN_H
#include "basewin.h"
#define HAVE_REMOTE
#include "pcapthread.h"
#include "table.h"
#define HAVE_REMOTE
#include <winsock2.h>
#include "pcap.h"
#include <QThread>
class MainPcapThread : public QThread {
Q_OBJECT

View File

@ -1,45 +0,0 @@
#include "pcapthread.h"
#include <QDateTime>
#include <QDebug>
CapThread::CapThread(struct pcap *pcap, QObject *parent) : QThread(parent), pcap(pcap) {
connect(this, &QThread::finished, this, &QThread::deleteLater);
}
void CapThread::run() {
pcap_pkthdr *header;
const u_char *data;
int res;
while((res = pcap_next_ex(pcap, &header, &data)) >= 0) {
if(res == 0) continue; //超时
packet.time = QDateTime::fromSecsSinceEpoch(header->ts.tv_sec).toString("yy-MM-dd HH:mm:ss.")+QString::number(header->ts.tv_usec);
packet.len = header->len;
packet.caplen = header->caplen;
//输出编号、时间戳和包长度
qDebug()<<packet.time<<" len:"<<header->caplen<<"/"<<header->len;
QString data_str;
char line[LINE_LEN + 1];
int l = 0;
for(uint i=0; i<header->caplen; i++) {
data_str.append(QString::asprintf("%.2x ", data[i]));
if(i > 13) {
if(isgraph(data[i]) || data[i] == ' ') line[l] = data[i];
else line[l] = '.';
if(l==15) {
line[16] = '\0';
data_str.append(" ");
data_str.append(line);
data_str.append("\n");
memset(line, 0, LINE_LEN);
l = 0;
} else l++;
} else if(i == 13) {
data_str.append("\n");
}
}
packet.data = data_str;
qDebug()<<packet.data.toUtf8().constData();
emit onMsg();
}
qDebug("Error reading the packets: %s\n", pcap_geterr(pcap));
}

View File

@ -1,35 +0,0 @@
#ifndef PCAPTHREAD_H
#define PCAPTHREAD_H
#include <QThread>
#include <winsock2.h>
#include "pcap.h"
#include <QVector>
#pragma comment(lib, "wpcap.lib")
#pragma comment(lib, "Ws2_32.lib")
#define LINE_LEN 16
#define MAX_ADDR_LEN 16
struct Packet {
QString time;
u_int len;
u_int caplen;
QString data;
};
class CapThread : public QThread {
Q_OBJECT
public:
explicit CapThread(pcap *pcap, QObject *parent = nullptr);
pcap *pcap{0};
Packet packet;
protected:
void run();
signals:
void onMsg();
};
#endif // PCAPTHREAD_H

View File

@ -7,22 +7,22 @@
#include <QRadioButton>
#include <QDebug>
#include "globalfunc.h"
//PcapWin *PcapWin::newIns(QWidget *parent) {
// auto name = getNetDev(parent);
// if(name.isEmpty()) return 0;
// char errbuf[PCAP_ERRBUF_SIZE]{'\0'};
// auto pcapRe = pcap_open_live(name.data(), 65536, PCAP_OPENFLAG_PROMISCUOUS, 50, errbuf);
// if(pcapRe == 0) {
// QMessageBox::critical(parent, "Error", QString(tr("打开网卡失败"))+errbuf);
// return 0;
// }
// auto pcapSend = pcap_open_live(name.data(), 65536, 0, 50, errbuf);
// if(pcapSend == 0) {
// QMessageBox::critical(parent, "Error", QString(tr("打开网卡失败"))+errbuf);
// return 0;
// }
// return new PcapWin(pcapRe, pcapSend, parent);
//}
PcapWin *PcapWin::newIns(QByteArray &name, QWidget *parent) {
if(name.isEmpty()) return 0;
char errbuf[PCAP_ERRBUF_SIZE]{'\0'};
auto pcapRe = pcap_open_live(name.data(), 65536, PCAP_OPENFLAG_PROMISCUOUS, 50, errbuf);
if(pcapRe == 0) {
QMessageBox::critical(parent, "Error", QString(tr("打开网卡失败"))+errbuf);
return 0;
}
auto pcapSend = pcap_open_live(name.data(), 65536, 0, 50, errbuf);
if(pcapSend == 0) {
QMessageBox::critical(parent, "Error", QString(tr("打开网卡失败"))+errbuf);
return 0;
}
return new PcapWin(pcapRe, pcapSend, parent);
}
PcapWin::PcapWin(pcap_t *pcapRece, pcap_t *pcapSend, QWidget *parent) : BaseWin{parent}, pcap(pcapSend) {
setWindowModality(Qt::WindowModal);

View File

@ -3,14 +3,18 @@
#include "basewin.h"
#define HAVE_REMOTE
#include "pcapthread.h"
#include <winsock2.h>
#include "pcap.h"
#include <QThread>
#define LINE_LEN 16
class PcapThread : public QThread {
Q_OBJECT
public:
explicit PcapThread(pcap *pcap);
~PcapThread() {
//pcap_close(pcap);
pcap_close(pcap);
}
pcap *pcap;
std::atomic<char> status{0};
@ -24,11 +28,11 @@ signals:
class PcapWin : public BaseWin {
Q_OBJECT
public:
// static PcapWin *newIns(QWidget *);
static PcapWin *newIns(QByteArray &, QWidget *);
explicit PcapWin(pcap *, pcap *, QWidget *parent = nullptr);
~PcapWin() {
//pcap_close(pcap);
pcap_close(pcap);
thd->status = 2;
}
PcapThread *thd{0};

View File

@ -63,9 +63,9 @@ VideoWin::VideoWin(pcap_t *pcapRece, pcap_t *pcapSend, QWidget *parent) : BaseWi
hBox->addWidget(fdMsecCapScr);
hBox->addSpacing(20);
fdShowImg = new QRadioButton("关闭显示");
fdShowImg->setChecked(showImg);
hBox->addWidget(fdShowImg);
fdNoReview = new QCheckBox("关闭显示");
fdNoReview->setChecked(noReview);
hBox->addWidget(fdNoReview);
hBox->addSpacing(20);
@ -122,11 +122,10 @@ VideoWin::VideoWin(pcap_t *pcapRece, pcap_t *pcapSend, QWidget *parent) : BaseWi
auto fdCanvas = new Canvas;
vBox->addWidget(fdCanvas, 1);
connect(fdShowImg, &QRadioButton::toggled, [=](bool checked) {
qDebug()<<"fdShowImg"<<checked;
showImg = checked;
if(thdRece) thdRece->showImg = checked;
if(! checked) {
connect(fdNoReview, &QRadioButton::toggled, [=](bool checked) {
noReview = checked;
if(thdRece) thdRece->noReview = checked;
if(noReview) {
fdCanvas->img = QImage();
fdCanvas->update();
}
@ -137,7 +136,7 @@ VideoWin::VideoWin(pcap_t *pcapRece, pcap_t *pcapSend, QWidget *parent) : BaseWi
thdRece = 0;
});
connect(thdRece, &VideoRecThread::onMsg, fdCanvas, [this, fdCanvas](QImage img, int lostTimes, int lostPkts) {
if(! showImg) return;
if(noReview) return;
fdCanvas->img = img;
fdCanvas->update();
if(lostPkts==0) return;
@ -295,7 +294,7 @@ void VideoRecThread::run() {
int lostTimes{0}, lostPkts{0};
while((res = pcap_next_ex(pcap, &header, &data)) >= 0) {
if(status==2) return;
if(status==1 || res == 0 || !showImg) continue; //超时
if(status==1 || res == 0 || noReview) continue; //超时
if(header->caplen<24) continue;
if(data[0]!=0x55 || data[1]!=0x55) continue;
if(data[2]==0x9a && data[3]==0x3d) {

View File

@ -40,7 +40,7 @@ public:
}
pcap *pcap;
std::atomic<char> status{0};
bool showImg{true};
bool noReview{false};
protected:
void run();
signals:
@ -70,8 +70,8 @@ public:
QScreen *screen;
int timerId{0};
QSpinBox *fdWidth, *fdHeight, *fdMsecCapScr,*fdYxj;
QRadioButton *fdShowImg;
bool showImg{true};
QCheckBox *fdNoReview;
bool noReview{false};
QRadioButton *fdEnd;
QLabel *fdInfo, *fdLostTimes, *fdLostPkts;
QString info;