From 310b2d1c3ef9edab43348afbfb1445ee611a2c73 Mon Sep 17 00:00:00 2001 From: JBTing Date: Tue, 7 Feb 2023 12:31:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4PCAP=E6=BA=90=E3=80=81?= =?UTF-8?q?=E5=AE=BF=E3=80=82=E7=BB=9F=E4=B8=80=E5=BC=95=E7=94=A8PCAPHub?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/modules.pro | 2 - modules/sinks/CMakeLists.txt | 1 - modules/sinks/sink_pcap/CMakeLists.txt | 62 ------- modules/sinks/sink_pcap/README.md | 5 + modules/sinks/sink_pcap/dialogsinkpcap.cpp | 97 ---------- modules/sinks/sink_pcap/dialogsinkpcap.h | 46 ----- modules/sinks/sink_pcap/dialogsinkpcap.ui | 103 ----------- modules/sinks/sink_pcap/main.cpp | 48 ----- modules/sinks/sink_pcap/pcapio.cpp | 170 ----------------- modules/sinks/sink_pcap/pcapio.h | 42 ----- modules/sinks/sink_pcap/sink_pcap.json | 31 ---- modules/sinks/sink_pcap/sink_pcap.pro | 53 ------ modules/sinks/sink_pcap/sink_pcap.qrc | 5 - modules/sources/CMakeLists.txt | 1 - modules/sources/source_pcap/CMakeLists.txt | 64 ------- modules/sources/source_pcap/README.md | 5 + .../sources/source_pcap/dialogsourcepcap.cpp | 105 ----------- .../sources/source_pcap/dialogsourcepcap.h | 48 ----- .../sources/source_pcap/dialogsourcepcap.ui | 117 ------------ modules/sources/source_pcap/listen_thread.cpp | 44 ----- modules/sources/source_pcap/listen_thread.h | 20 -- modules/sources/source_pcap/main.cpp | 48 ----- modules/sources/source_pcap/pcapio.cpp | 171 ------------------ modules/sources/source_pcap/pcapio.h | 34 ---- modules/sources/source_pcap/source_pcap.json | 37 ---- modules/sources/source_pcap/source_pcap.pro | 55 ------ modules/sources/source_pcap/source_pcap.qrc | 5 - 27 files changed, 10 insertions(+), 1409 deletions(-) delete mode 100644 modules/sinks/sink_pcap/CMakeLists.txt create mode 100644 modules/sinks/sink_pcap/README.md delete mode 100644 modules/sinks/sink_pcap/dialogsinkpcap.cpp delete mode 100644 modules/sinks/sink_pcap/dialogsinkpcap.h delete mode 100644 modules/sinks/sink_pcap/dialogsinkpcap.ui delete mode 100644 modules/sinks/sink_pcap/main.cpp delete mode 100644 modules/sinks/sink_pcap/pcapio.cpp delete mode 100644 modules/sinks/sink_pcap/pcapio.h delete mode 100644 modules/sinks/sink_pcap/sink_pcap.json delete mode 100644 modules/sinks/sink_pcap/sink_pcap.pro delete mode 100644 modules/sinks/sink_pcap/sink_pcap.qrc delete mode 100644 modules/sources/source_pcap/CMakeLists.txt create mode 100644 modules/sources/source_pcap/README.md delete mode 100644 modules/sources/source_pcap/dialogsourcepcap.cpp delete mode 100644 modules/sources/source_pcap/dialogsourcepcap.h delete mode 100644 modules/sources/source_pcap/dialogsourcepcap.ui delete mode 100644 modules/sources/source_pcap/listen_thread.cpp delete mode 100644 modules/sources/source_pcap/listen_thread.h delete mode 100644 modules/sources/source_pcap/main.cpp delete mode 100644 modules/sources/source_pcap/pcapio.cpp delete mode 100644 modules/sources/source_pcap/pcapio.h delete mode 100644 modules/sources/source_pcap/source_pcap.json delete mode 100644 modules/sources/source_pcap/source_pcap.pro delete mode 100644 modules/sources/source_pcap/source_pcap.qrc diff --git a/modules/modules.pro b/modules/modules.pro index 9535b0b..fe8df60 100644 --- a/modules/modules.pro +++ b/modules/modules.pro @@ -5,7 +5,6 @@ SUBDIRS += \ pannel/control_pannel \ sources/source_plutosdr \ sources/source_files \ - sources/source_pcap \ transforms/transform_fft \ transforms/mod_fm \ transforms/mod_fm_dem \ @@ -13,7 +12,6 @@ SUBDIRS += \ transforms/resample_pqfraction \ sinks/sink_file \ sinks/sink_plutosdr \ - sinks/sink_pcap \ sinks/sink_SQL \ network/network_p2p \ wrappers/wrapper_scripts \ diff --git a/modules/sinks/CMakeLists.txt b/modules/sinks/CMakeLists.txt index d10c5da..9123aee 100644 --- a/modules/sinks/CMakeLists.txt +++ b/modules/sinks/CMakeLists.txt @@ -3,7 +3,6 @@ add_subdirectory(sink_file) add_subdirectory(sink_plots) add_subdirectory(sink_SQL) add_subdirectory(sink_soundcard) -add_subdirectory(sink_pcap) if (TB_PLUTO) add_subdirectory(sink_plutosdr) endif() diff --git a/modules/sinks/sink_pcap/CMakeLists.txt b/modules/sinks/sink_pcap/CMakeLists.txt deleted file mode 100644 index d9a8617..0000000 --- a/modules/sinks/sink_pcap/CMakeLists.txt +++ /dev/null @@ -1,62 +0,0 @@ -cmake_minimum_required(VERSION 3.5) - -set(CMAKE_INCLUDE_CURRENT_DIR ON) -set(CMAKE_AUTOUIC ON) -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) - -set(CMAKE_CXX_STANDARD 17) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - -find_package(QT NAMES Qt5 COMPONENTS Core Widgets REQUIRED) -find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Widgets REQUIRED) - -include_directories(${TASKBUS_INTERFACEDIR}) - -if(CMAKE_SYSTEM_NAME MATCHES "Windows") - include_directories(${TASKBUS_THIRDIR}/win32/npcap/Include) - set(PCAP_LIB wpcap) - if(TB64BIT) - link_directories(${TASKBUS_THIRDIR}/win32/npcap/lib/x64) - else() - link_directories(${TASKBUS_THIRDIR}/win32/npcap/lib) - endif() -else() - set(PCAP_LIB pcap) -endif() - -set(PRJ_HEADERS - dialogsinkpcap.h - pcapio.h -) -set(PRJ_SOURCES - main.cpp - dialogsinkpcap.cpp - pcapio.cpp -) -set(PRJ_FORMS - dialogsinkpcap.ui -) -set(PRJ_RESOURCES - sink_pcap.qrc -) -#############Target====================== -add_executable(sink_pcap - ${PRJ_HEADERS} - ${PRJ_SOURCES} - ${PRJ_FORMS} - ${PRJ_RESOURCES} -) - -target_link_libraries(sink_pcap PRIVATE - Qt${QT_VERSION_MAJOR}::Widgets - ${PCAP_LIB} -) - -set_target_properties(sink_pcap PROPERTIES - MACOSX_BUNDLE_GUI_IDENTIFIER taskBus.modules.sink_pcap - MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} - MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} - MACOSX_BUNDLE TRUE - WIN32_EXECUTABLE TRUE -) diff --git a/modules/sinks/sink_pcap/README.md b/modules/sinks/sink_pcap/README.md new file mode 100644 index 0000000..67fb4c6 --- /dev/null +++ b/modules/sinks/sink_pcap/README.md @@ -0,0 +1,5 @@ +# PCAP Source and Sink + +为了保证收发数据的唯一性和去重,需要在一个公共队列里完成包的收发。原有分离的源、宿,在配置不正确时,会导致重发风暴。 + +目前,PCAP源和宿整合为 [PCAPHub 项目](https://gitcode.net/coloreaglestdio/pcaphub),直接单独编译后可引入taskBus。 PCAPHub项目已经兼容taskBus。 diff --git a/modules/sinks/sink_pcap/dialogsinkpcap.cpp b/modules/sinks/sink_pcap/dialogsinkpcap.cpp deleted file mode 100644 index fce331c..0000000 --- a/modules/sinks/sink_pcap/dialogsinkpcap.cpp +++ /dev/null @@ -1,97 +0,0 @@ -#include "dialogsinkpcap.h" -#include "ui_dialogsinkpcap.h" -#include -#include -#include -#include -#include -#include "pcapio.h" -DialogSinkPcap::DialogSinkPcap(QWidget *parent) : - QDialog(parent), - ui(new Ui::DialogSinkPcap), - m_devInputModule(new QStandardItemModel(this)), - cap_thread(new pcap_sink(this)) -{ - ui->setupUi(this); - std::string errstring = PCAPIO::pcapio_interfaces(m_devmap); - if (errstring.size()) - ui->label_count->setText(QString::fromLocal8Bit(errstring.c_str())); - - for (auto & p : m_devmap) - { - m_names.push_back(p.first); - m_devInputModule->appendRow(new QStandardItem(QString::fromLocal8Bit(p.second.c_str()))); - } - ui->comboBox_Devices->setModel(m_devInputModule); - ui->btn_stop->setDisabled(true); - - connect(ui->btn_start, SIGNAL(clicked()), this,SLOT(OnRecordStart())); - connect(ui->btn_stop, SIGNAL(clicked()), this,SLOT(OnRecordStop())); - connect(cap_thread,&pcap_sink::sig_message,ui->label_msg,&QLabel::setText); - - m_nTimerID = startTimer(1000); -} - -DialogSinkPcap::DialogSinkPcap(const TASKBUS::cmdlineParser * pline,QWidget *parent ): - DialogSinkPcap(parent) -{ - m_cmdline = pline; - if (m_cmdline) - { - setInstance(m_cmdline->toInt("instance",0)); - cap_thread->setCmdLine(m_cmdline); - //Listen thread to recieve messages from platform - connect(cap_thread,&pcap_sink::quit_app,this,&DialogSinkPcap::close); - m_n_device = m_cmdline->toInt("device",0); - m_n_subject = m_cmdline->toInt("package",0); - //ui->lineEdit_filter->setText(QString::fromLocal8Bit(m_filter.c_str())); - ui->comboBox_Devices->setCurrentIndex(m_n_device); - int hiden = m_cmdline->toInt("hide",0); - int autostart = m_cmdline->toInt("autostart",0); - if (hiden || autostart) - OnRecordStart(); - } -} - -DialogSinkPcap::~DialogSinkPcap() -{ - if (cap_thread->isRunning()) - { - cap_thread->stopCap(); - } - delete ui; -} -void DialogSinkPcap::timerEvent(QTimerEvent *event) -{ - if (event->timerId()==m_nTimerID) - { - if (cap_thread->isRunning()) - { - QString str = QString("%1").arg((unsigned long long)cap_thread->m_tx); - ui->label_count->setText(str); - } - } -} -void DialogSinkPcap::OnRecordStart() -{ - ui->btn_start->setDisabled(true); - ui->btn_stop->setDisabled(false); - cap_thread->m_nInstance = m_n_instance; - int nDev = ui->comboBox_Devices->currentIndex(); - if (nDev < 0 || nDev >= m_names.size() ) - { - return; - } - cap_thread->m_device = m_names[nDev]; - cap_thread->m_nSubject = m_n_subject; - cap_thread->startCap(); -} - - -void DialogSinkPcap::OnRecordStop() -{ - ui->btn_start->setDisabled(false); - ui->btn_stop->setDisabled(true); - cap_thread->stopCap(); -} - diff --git a/modules/sinks/sink_pcap/dialogsinkpcap.h b/modules/sinks/sink_pcap/dialogsinkpcap.h deleted file mode 100644 index afaefa0..0000000 --- a/modules/sinks/sink_pcap/dialogsinkpcap.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef DIALOGSINKPCAP_H -#define DIALOGSINKPCAP_H - -#include -#include -#include -#include "cmdlineparser.h" -#include "tb_interface.h" -#include "pcapio.h" -namespace Ui { - class DialogSinkPcap; -} - -class DialogSinkPcap : public QDialog -{ - Q_OBJECT - -public: - explicit DialogSinkPcap(QWidget *parent = nullptr); - explicit DialogSinkPcap(const TASKBUS::cmdlineParser * pline,QWidget *parent = nullptr); - ~DialogSinkPcap(); -public: - void setInstance(const int i){m_n_instance = i;} - void timerEvent(QTimerEvent *event); -private: - Ui::DialogSinkPcap *ui; - QStandardItemModel * m_devInputModule = nullptr; -protected: - int m_n_instance = 0; - int m_n_subject = 0; - int m_n_device = 0; - std::string m_filter; -private slots: - void OnRecordStart(); - void OnRecordStop(); -private: - const TASKBUS::cmdlineParser * m_cmdline = nullptr; - pcap_sink * cap_thread = 0; - int m_nTimerID = -1; -protected: - std::map m_devmap; - std::vector m_names; - -}; - -#endif // DIALOGSINKPCAP_H diff --git a/modules/sinks/sink_pcap/dialogsinkpcap.ui b/modules/sinks/sink_pcap/dialogsinkpcap.ui deleted file mode 100644 index c437d0f..0000000 --- a/modules/sinks/sink_pcap/dialogsinkpcap.ui +++ /dev/null @@ -1,103 +0,0 @@ - - - DialogSinkPcap - - - - 0 - 0 - 640 - 128 - - - - DialogSinkPcap - - - - - - - - - 0 - 0 - - - - devices - - - - - - - - 0 - 0 - - - - - 1024 - 16777215 - - - - QComboBox::AdjustToMinimumContentsLengthWithIcon - - - - - - - - - - - Ok - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - 0 - - - - - - - 开始 - - - - - - - 停止 - - - - - - - - - - - diff --git a/modules/sinks/sink_pcap/main.cpp b/modules/sinks/sink_pcap/main.cpp deleted file mode 100644 index efbd2c3..0000000 --- a/modules/sinks/sink_pcap/main.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "dialogsinkpcap.h" -#include -#include -#include -#include "cmdlineparser.h" -#include "tb_interface.h" - -const bool TASKBUSDBG = false; - -using namespace TASKBUS; -int main(int argc, char *argv[]) -{ - QApplication a(argc, argv); - init_client(); - cmdlineParser args; - //Debug - if (TASKBUSDBG) - { - auto cmdline = TASKBUS::debug("d:/log/pid2837",0,0); - args.parser(cmdline); - } - else - args.parser(argc,argv); - - int ret = 0; - //Arg--information参数,打印自己的功能定义字符串。 - if (args.contains("information")) - { - QFile fp(":/json/sink_pcap.json"); - if (fp.open(QIODevice::ReadOnly)) - { - QByteArray arr = fp.readAll(); - arr.push_back('\0'); - puts(arr.constData()); - fflush(stdout); - } - } - else - { - DialogSinkPcap w(&args); - int hiden = args.toInt("hide",0); - if (hiden==0) - w.show(); - //用于接收消息的线程 - ret = a.exec(); - } - return ret; -} diff --git a/modules/sinks/sink_pcap/pcapio.cpp b/modules/sinks/sink_pcap/pcapio.cpp deleted file mode 100644 index 84b4f79..0000000 --- a/modules/sinks/sink_pcap/pcapio.cpp +++ /dev/null @@ -1,170 +0,0 @@ -#include "pcapio.h" -#include -#include -#include "tb_interface.h" -namespace PCAPIO{ - - std::string ifaddresses(pcap_if_t *d); - - std::string pcapio_interfaces(std::map & devmap) - { - std::string res; - pcap_if_t *alldevs; - char errbuf[PCAP_ERRBUF_SIZE]; - if(pcap_findalldevs(&alldevs, errbuf) == -1){ - res = errbuf; - return res; - } - for(auto d = alldevs; d != NULL; d = d->next){ - std::string d_name = d->name; - std::string d_des = ifaddresses(d); - devmap[d_name] = d_des; - } - - pcap_freealldevs(alldevs); - return res; - } - std::string address_print(unsigned char * v) - { - std::string res; - char buf[1024]; - - int left_start = 0, right_end = sizeof(sockaddr::sa_data) - 1; - while (left_start + 3 < right_end && v[left_start]==0) - ++left_start; - while (left_start + 3< right_end && v[right_end]==0) - --right_end; - res += "HEX("; - for (unsigned char i =left_start; i<=right_end;++i) - { - if (i-left_start) res += ":"; - snprintf(buf,1024,"%02X", (unsigned int)v[i]); - res += buf; - } - res += ")"; - res += "DEC("; - for (unsigned char i =left_start; i<=right_end;++i) - { - if (i-left_start) res += ":"; - snprintf(buf,1024,"%u", (unsigned int)v[i] ); - res += buf; - } - res += ")"; - - return res; - } - /* Print all the available information on the given interface */ - std::string ifaddresses(pcap_if_t *d) - { - char buf[1024]; - pcap_addr_t *a; - std::string res ; - res+=d->name; - res += ":"; - if(d->description) - { - res += d->description; - res += ":"; - } - - for(a=d->addresses;a;a=a->next) { - snprintf(buf,1024,"\nAF_0x%02X",(unsigned int)a->addr->sa_family); - res += buf; - if (a->addr) - { - snprintf(buf,1024,"_%s",address_print((unsigned char *)a->addr->sa_data).c_str()); - res += buf; - } - if (a->netmask) - { - snprintf(buf,1024,"/%s ",address_print((unsigned char *)a->netmask->sa_data).c_str()); - res += buf; - } - } - return res; - } -} - -pcap_sink::pcap_sink (QObject * parent) - :QThread(parent) -{ - m_bStop = true; -} - -void pcap_sink::startCap() -{ - if (this->isRunning()) - stopCap(); - m_bStop =false; - start(QThread::TimeCriticalPriority); -} -void pcap_sink::stopCap() -{ - m_bStop = true; - if (this->isRunning()) - { - this->wait(2000); - this->terminate(); - } -} - -void pcap_sink::run() -{ - if (m_bStop) - return; - bool bQuit = false; - using namespace TASKBUS; - while (false==m_bStop) - { - pcap_t *handle = NULL; - char errbuf[PCAP_ERRBUF_SIZE]; - handle = pcap_open_live(m_device.c_str(), 65535, 1, 10, errbuf); - if(handle == NULL) - { - QString err = QString("pcap_open_live return err,errbuf:%1...").arg(errbuf); - emit sig_message(err); - return ; - } - m_tx = 0; - while (false==m_bStop) - { - subject_package_header header; - std::vector packagedta = pull_subject(&header); - if (!is_valid_header(header)) - { - msleep(100); - continue; - } - if (!packagedta.empty()) - { - if ( is_control_subject(header)) - { - //收到命令进程退出的广播消息,退出 - if (strstr((const char *)packagedta.data(),"function=quit;")!=nullptr) - { - fprintf(stderr,"Recived Quit Command."); - fflush(stderr); - m_bStop = true; - bQuit = true; - } - } - else if (header.subject_id==m_nSubject) - { - if (PCAP_ERROR ==pcap_sendpacket(handle,packagedta.data(),packagedta.size())) - { - fprintf(stderr,pcap_geterr(handle)); - fprintf(stderr,"\n"); - fflush(stderr); - break; - } - ++m_tx; - } - } - } - pcap_close(handle); - } - - emit sig_message("Stopped"); - if (bQuit) - emit quit_app(); -} diff --git a/modules/sinks/sink_pcap/pcapio.h b/modules/sinks/sink_pcap/pcapio.h deleted file mode 100644 index 3f69c47..0000000 --- a/modules/sinks/sink_pcap/pcapio.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef PCAPIO_H -#define PCAPIO_H -#include -#include -#include -#include -#include -#include "cmdlineparser.h" -namespace PCAPIO{ - -std::string pcapio_interfaces(std::map & devmap); -} - -class pcap_sink : public QThread{ - Q_OBJECT -public: - explicit pcap_sink (QObject * parent = nullptr); - void startCap(); - void stopCap(); - void setCmdLine(const TASKBUS::cmdlineParser * cmdline) - { - m_cmd = cmdline; - } -protected: - void run() override; - -protected: - std::atomic m_bStop; - static QAtomicInteger m_refTms; - const TASKBUS::cmdlineParser * m_cmd = nullptr; -public: - std::string m_device; - std::atomic m_tx; - int m_nInstance; - int m_nSubject; -signals: - void sig_message(QString msg); - void quit_app(); -}; - - -#endif // PCAPIO_H diff --git a/modules/sinks/sink_pcap/sink_pcap.json b/modules/sinks/sink_pcap/sink_pcap.json deleted file mode 100644 index 95b7d57..0000000 --- a/modules/sinks/sink_pcap/sink_pcap.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "sink_pcap":{ - "name":"pcap_sink", - "parameters":{ - "device":{ - "type":"string", - "tooltip":"device list index", - "default":"0" - }, - "hide":{ - "type":"int", - "tooltip":"hide window when start", - "default":0 - }, - "autostart":{ - "type":"int", - "tooltip":"auto start", - "default":0 - } - }, - "input_subjects": - { - "package":{ - "type":"bytes", - "tooltip":"Package" - } - }, - "output_subjects":{ - } - } -} diff --git a/modules/sinks/sink_pcap/sink_pcap.pro b/modules/sinks/sink_pcap/sink_pcap.pro deleted file mode 100644 index 401a998..0000000 --- a/modules/sinks/sink_pcap/sink_pcap.pro +++ /dev/null @@ -1,53 +0,0 @@ -#------------------------------------------------- -# -# Project created by QtCreator 2018-10-26T07:14:47 -# -#------------------------------------------------- - -QT += core gui - -greaterThan(QT_MAJOR_VERSION, 4): QT += widgets -INCLUDEPATH += ../../../tb_interface -DESTDIR = $$OUT_PWD/../../../bin/modules -TARGET = sink_pcap -TEMPLATE = app - -# The following define makes your compiler emit warnings if you use -# any feature of Qt which has been marked as deprecated (the exact warnings -# depend on your compiler). Please consult the documentation of the -# deprecated API in order to know how to port your code away from it. -DEFINES += QT_DEPRECATED_WARNINGS - -# You can also make your code fail to compile if you use deprecated APIs. -# In order to do so, uncomment the following line. -# You can also select to disable deprecated APIs only up to a certain version of Qt. -#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 - -CONFIG += c++17 - -SOURCES += \ - main.cpp \ - dialogsinkpcap.cpp \ - pcapio.cpp - -HEADERS += \ - dialogsinkpcap.h \ - pcapio.h - -FORMS += \ - dialogsinkpcap.ui -win32{ - INCLUDEPATH +="$$PWD/../../3rdlibs/win32/npcap/Include" - contains(QT_ARCH, i386) { - LIBS+=-L"$$PWD/../../3rdlibs/win32/npcap/lib" -lwpcap - } else { - LIBS+=-L"$$PWD/../../3rdlibs/win32/npcap/lib/x64" -lwpcap - } -}else: LIBS+=-lpcap -# Default rules for deployment. -qnx: target.path = /tmp/$${TARGET}/bin -else: unix:!android: target.path = /opt/$${TARGET}/bin -!isEmpty(target.path): INSTALLS += target - -RESOURCES += \ - sink_pcap.qrc diff --git a/modules/sinks/sink_pcap/sink_pcap.qrc b/modules/sinks/sink_pcap/sink_pcap.qrc deleted file mode 100644 index ab59a00..0000000 --- a/modules/sinks/sink_pcap/sink_pcap.qrc +++ /dev/null @@ -1,5 +0,0 @@ - - - sink_pcap.json - - diff --git a/modules/sources/CMakeLists.txt b/modules/sources/CMakeLists.txt index b18c13c..3deaffa 100644 --- a/modules/sources/CMakeLists.txt +++ b/modules/sources/CMakeLists.txt @@ -2,7 +2,6 @@ cmake_minimum_required(VERSION 3.5) add_subdirectory(source_files) add_subdirectory(source_soundcard) -add_subdirectory(source_pcap) if (TB_PLUTO) add_subdirectory(source_plutosdr) endif() diff --git a/modules/sources/source_pcap/CMakeLists.txt b/modules/sources/source_pcap/CMakeLists.txt deleted file mode 100644 index 6886a9f..0000000 --- a/modules/sources/source_pcap/CMakeLists.txt +++ /dev/null @@ -1,64 +0,0 @@ -cmake_minimum_required(VERSION 3.5) - -set(CMAKE_INCLUDE_CURRENT_DIR ON) -set(CMAKE_AUTOUIC ON) -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) - -set(CMAKE_CXX_STANDARD 17) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - -find_package(QT NAMES Qt5 COMPONENTS Core Widgets REQUIRED) -find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Widgets REQUIRED) - -include_directories(${TASKBUS_INTERFACEDIR}) - -if(CMAKE_SYSTEM_NAME MATCHES "Windows") - include_directories(${TASKBUS_THIRDIR}/win32/npcap/Include) - set(PCAP_LIB wpcap) - if(TB64BIT) - link_directories(${TASKBUS_THIRDIR}/win32/npcap/lib/x64) - else() - link_directories(${TASKBUS_THIRDIR}/win32/npcap/lib) - endif() -else() - set(PCAP_LIB pcap) -endif() - -set(PRJ_HEADERS - dialogsourcepcap.h - listen_thread.h - pcapio.h -) -set(PRJ_SOURCES - main.cpp - dialogsourcepcap.cpp - listen_thread.cpp - pcapio.cpp -) -set(PRJ_FORMS - dialogsourcepcap.ui -) -set(PRJ_RESOURCES - source_pcap.qrc -) -#############Target====================== -add_executable(source_pcap - ${PRJ_HEADERS} - ${PRJ_SOURCES} - ${PRJ_FORMS} - ${PRJ_RESOURCES} -) - -target_link_libraries(source_pcap PRIVATE - Qt${QT_VERSION_MAJOR}::Widgets - ${PCAP_LIB} -) - -set_target_properties(source_pcap PROPERTIES - MACOSX_BUNDLE_GUI_IDENTIFIER taskBus.modules.source_pcap - MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} - MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} - MACOSX_BUNDLE TRUE - WIN32_EXECUTABLE TRUE -) diff --git a/modules/sources/source_pcap/README.md b/modules/sources/source_pcap/README.md new file mode 100644 index 0000000..67fb4c6 --- /dev/null +++ b/modules/sources/source_pcap/README.md @@ -0,0 +1,5 @@ +# PCAP Source and Sink + +为了保证收发数据的唯一性和去重,需要在一个公共队列里完成包的收发。原有分离的源、宿,在配置不正确时,会导致重发风暴。 + +目前,PCAP源和宿整合为 [PCAPHub 项目](https://gitcode.net/coloreaglestdio/pcaphub),直接单独编译后可引入taskBus。 PCAPHub项目已经兼容taskBus。 diff --git a/modules/sources/source_pcap/dialogsourcepcap.cpp b/modules/sources/source_pcap/dialogsourcepcap.cpp deleted file mode 100644 index 31d116d..0000000 --- a/modules/sources/source_pcap/dialogsourcepcap.cpp +++ /dev/null @@ -1,105 +0,0 @@ -#include "dialogsourcepcap.h" -#include "ui_dialogsourcepcap.h" -#include -#include -#include -#include -#include -#include "pcapio.h" -DialogSourcePcap::DialogSourcePcap(QWidget *parent) : - QDialog(parent), - ui(new Ui::DialogSourcePcap), - m_devInputModule(new QStandardItemModel(this)), - cap_thread(new pcap_source(this)) -{ - ui->setupUi(this); - std::string errstring = PCAPIO::pcapio_interfaces(m_devmap); - if (errstring.size()) - ui->label_count->setText(QString::fromLocal8Bit(errstring.c_str())); - - for (auto & p : m_devmap) - { - m_names.push_back(p.first); - m_devInputModule->appendRow(new QStandardItem(QString::fromLocal8Bit(p.second.c_str()))); - } - ui->comboBox_Devices->setModel(m_devInputModule); - ui->btn_stop->setDisabled(true); - - connect(ui->btn_start, SIGNAL(clicked()), this,SLOT(OnRecordStart())); - connect(ui->btn_stop, SIGNAL(clicked()), this,SLOT(OnRecordStop())); - connect(cap_thread,&pcap_source::sig_message,ui->label_msg,&QLabel::setText); - - m_nTimerID = startTimer(1000); -} - -DialogSourcePcap::DialogSourcePcap(const TASKBUS::cmdlineParser * pline,QWidget *parent ): - DialogSourcePcap(parent) -{ - m_cmdline = pline; - if (m_cmdline) - { - setInstance(m_cmdline->toInt("instance",0)); - //Listen thread to recieve messages from platform - m_pListenThread = new listen_thread(m_cmdline,this); - connect(m_pListenThread,&listen_thread::quit_app,this,&DialogSourcePcap::close); - m_pListenThread->start(); - m_n_device = m_cmdline->toInt("device",0); - m_n_subject = m_cmdline->toInt("package",0); - m_filter = m_cmdline->toString("filter",""); - ui->lineEdit_filter->setText(QString::fromLocal8Bit(m_filter.c_str())); - - ui->comboBox_Devices->setCurrentIndex(m_n_device); - - int hiden = m_cmdline->toInt("hide",0); - int autostart = m_cmdline->toInt("autostart",0); - if (hiden || autostart) - OnRecordStart(); - - } -} - -DialogSourcePcap::~DialogSourcePcap() -{ - if (m_pListenThread) - m_pListenThread->terminate(); - if (cap_thread->isRunning()) - { - cap_thread->stopCap(); - } - delete ui; -} -void DialogSourcePcap::timerEvent(QTimerEvent *event) -{ - if (event->timerId()==m_nTimerID) - { - if (cap_thread->isRunning()) - { - QString str = QString("%1").arg((unsigned long long)cap_thread->m_rx); - ui->label_count->setText(str); - } - } -} -void DialogSourcePcap::OnRecordStart() -{ - ui->btn_start->setDisabled(true); - ui->btn_stop->setDisabled(false); - cap_thread->m_nInstance = m_n_instance; - int nDev = ui->comboBox_Devices->currentIndex(); - if (nDev < 0 || nDev >= m_names.size() ) - { - return; - } - cap_thread->m_device = m_names[nDev]; - cap_thread->m_filter = ui->lineEdit_filter->text().toStdString(); - cap_thread->m_nSubject = m_n_subject; - cap_thread->startCap(); -} - - -void DialogSourcePcap::OnRecordStop() -{ - ui->btn_start->setDisabled(false); - ui->btn_stop->setDisabled(true); - cap_thread->stopCap(); -} - diff --git a/modules/sources/source_pcap/dialogsourcepcap.h b/modules/sources/source_pcap/dialogsourcepcap.h deleted file mode 100644 index 580733c..0000000 --- a/modules/sources/source_pcap/dialogsourcepcap.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef DIALOGSOURCEPCAP_H -#define DIALOGSOURCEPCAP_H - -#include -#include -#include -#include "cmdlineparser.h" -#include "tb_interface.h" -#include "listen_thread.h" -#include "pcapio.h" -namespace Ui { - class DialogSourcePcap; -} - -class DialogSourcePcap : public QDialog -{ - Q_OBJECT - -public: - explicit DialogSourcePcap(QWidget *parent = nullptr); - explicit DialogSourcePcap(const TASKBUS::cmdlineParser * pline,QWidget *parent = nullptr); - ~DialogSourcePcap(); -public: - void setInstance(const int i){m_n_instance = i;} - void timerEvent(QTimerEvent *event); -private: - Ui::DialogSourcePcap *ui; - QStandardItemModel * m_devInputModule = nullptr; -protected: - int m_n_instance = 0; - int m_n_subject = 0; - int m_n_device = 0; - std::string m_filter; -private slots: - void OnRecordStart(); - void OnRecordStop(); -private: - const TASKBUS::cmdlineParser * m_cmdline = nullptr; - listen_thread * m_pListenThread = nullptr; - pcap_source * cap_thread = 0; - int m_nTimerID = -1; -protected: - std::map m_devmap; - std::vector m_names; - -}; - -#endif // DIALOGSOURCEPCAP_H diff --git a/modules/sources/source_pcap/dialogsourcepcap.ui b/modules/sources/source_pcap/dialogsourcepcap.ui deleted file mode 100644 index 5843da5..0000000 --- a/modules/sources/source_pcap/dialogsourcepcap.ui +++ /dev/null @@ -1,117 +0,0 @@ - - - DialogSourcePcap - - - - 0 - 0 - 640 - 128 - - - - DialogSourcePcap - - - - - - - - - 0 - 0 - - - - devices - - - - - - - - 0 - 0 - - - - - 1024 - 16777215 - - - - QComboBox::AdjustToMinimumContentsLengthWithIcon - - - - - - - - - - - filter - - - - - - - - - - - - - - Ok - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - 0 - - - - - - - 开始 - - - - - - - 停止 - - - - - - - - - - - diff --git a/modules/sources/source_pcap/listen_thread.cpp b/modules/sources/source_pcap/listen_thread.cpp deleted file mode 100644 index 0c84b88..0000000 --- a/modules/sources/source_pcap/listen_thread.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "listen_thread.h" -#include "tb_interface.h" -listen_thread::listen_thread( - const TASKBUS::cmdlineParser * cmdline, - QObject * parent) - :QThread(parent) - ,m_cmd(cmdline) -{ - -} - -QAtomicInteger listen_thread::m_refTms = 0; - -void listen_thread::run() -{ - using namespace TASKBUS; - bool bfinished = false; - while (false==bfinished) - { - subject_package_header header; - std::vector packagedta = pull_subject(&header); - if (!is_valid_header(header)) - { - fprintf(stderr,"Recived BAD Command."); - fflush(stderr); - msleep(100); - continue; - } - if (!packagedta.empty()) - { - if ( is_control_subject(header)) - { - //收到命令进程退出的广播消息,退出 - if (strstr((const char *)packagedta.data(),"function=quit;")!=nullptr) - { - fprintf(stderr,"Recived Quit Command."); - fflush(stderr); - bfinished = true; - } - } - } - } - emit quit_app(); -} diff --git a/modules/sources/source_pcap/listen_thread.h b/modules/sources/source_pcap/listen_thread.h deleted file mode 100644 index bc04dac..0000000 --- a/modules/sources/source_pcap/listen_thread.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef LISTEN_THREAD_H -#define LISTEN_THREAD_H - -#include -#include -#include "cmdlineparser.h" -class listen_thread: public QThread -{ - Q_OBJECT -public: - explicit listen_thread(const TASKBUS::cmdlineParser * cmdline, QObject * parent); - static QAtomicInteger m_refTms; -protected: - const TASKBUS::cmdlineParser * m_cmd = nullptr; - void run() override; -signals: - void quit_app(); -}; - -#endif // LISTEN_THREAD_H diff --git a/modules/sources/source_pcap/main.cpp b/modules/sources/source_pcap/main.cpp deleted file mode 100644 index 38fd4cc..0000000 --- a/modules/sources/source_pcap/main.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "dialogsourcepcap.h" -#include -#include -#include -#include "cmdlineparser.h" -#include "tb_interface.h" - -const bool TASKBUSDBG = false; - -using namespace TASKBUS; -int main(int argc, char *argv[]) -{ - QApplication a(argc, argv); - init_client(); - cmdlineParser args; - //Debug - if (TASKBUSDBG) - { - auto cmdline = TASKBUS::debug("d:/log/pid2837",0,0); - args.parser(cmdline); - } - else - args.parser(argc,argv); - - int ret = 0; - //Arg--information参数,打印自己的功能定义字符串。 - if (args.contains("information")) - { - QFile fp(":/json/source_pcap.json"); - if (fp.open(QIODevice::ReadOnly)) - { - QByteArray arr = fp.readAll(); - arr.push_back('\0'); - puts(arr.constData()); - fflush(stdout); - } - } - else - { - DialogSourcePcap w(&args); - int hiden = args.toInt("hide",0); - if (hiden==0) - w.show(); - //用于接收消息的线程 - ret = a.exec(); - } - return ret; -} diff --git a/modules/sources/source_pcap/pcapio.cpp b/modules/sources/source_pcap/pcapio.cpp deleted file mode 100644 index 1aabc30..0000000 --- a/modules/sources/source_pcap/pcapio.cpp +++ /dev/null @@ -1,171 +0,0 @@ -#include "pcapio.h" -#include -#include -#include "tb_interface.h" -namespace PCAPIO{ - -std::string ifaddresses(pcap_if_t *d); - -std::string pcapio_interfaces(std::map & devmap) -{ - std::string res; - pcap_if_t *alldevs; - char errbuf[PCAP_ERRBUF_SIZE]; - if(pcap_findalldevs(&alldevs, errbuf) == -1){ - res = errbuf; - return res; - } - for(auto d = alldevs; d != NULL; d = d->next){ - std::string d_name = d->name; - std::string d_des = ifaddresses(d); - devmap[d_name] = d_des; - } - - pcap_freealldevs(alldevs); - return res; -} -std::string address_print(unsigned char * v) -{ - std::string res; - char buf[1024]; - - int left_start = 0, right_end = sizeof(sockaddr::sa_data) - 1; - while (left_start + 3 < right_end && v[left_start]==0) - ++left_start; - while (left_start + 3< right_end && v[right_end]==0) - --right_end; - res += "HEX("; - for (unsigned char i =left_start; i<=right_end;++i) - { - if (i-left_start) res += ":"; - snprintf(buf,1024,"%02X", (unsigned int)v[i]); - res += buf; - } - res += ")"; - res += "DEC("; - for (unsigned char i =left_start; i<=right_end;++i) - { - if (i-left_start) res += ":"; - snprintf(buf,1024,"%u", (unsigned int)v[i] ); - res += buf; - } - res += ")"; - - return res; -} -/* Print all the available information on the given interface */ -std::string ifaddresses(pcap_if_t *d) -{ - char buf[1024]; - pcap_addr_t *a; - std::string res ; - res+=d->name; - res += ":"; - if(d->description) - { - res += d->description; - res += ":"; - } - - for(a=d->addresses;a;a=a->next) { - snprintf(buf,1024,"\nAF_0x%02X",(unsigned int)a->addr->sa_family); - res += buf; - if (a->addr) - { - snprintf(buf,1024,"_%s",address_print((unsigned char *)a->addr->sa_data).c_str()); - res += buf; - } - if (a->netmask) - { - snprintf(buf,1024,"/%s ",address_print((unsigned char *)a->netmask->sa_data).c_str()); - res += buf; - } - } - return res; -} -} - -pcap_source::pcap_source (QObject * parent) - :QThread(parent) -{ - m_bStop = true; -} - -void pcap_source::startCap() -{ - if (this->isRunning()) - stopCap(); - m_bStop =false; - start(QThread::TimeCriticalPriority); -} -void pcap_source::stopCap() -{ - m_bStop = true; - if (this->isRunning()) - { - this->wait(2000); - this->terminate(); - } -} - -void pcap_source::run() -{ - if (m_bStop) - return; - while (!m_bStop) - { - pcap_t *handle = NULL; - char errbuf[PCAP_ERRBUF_SIZE]; - handle = pcap_open_live(m_device.c_str(), 65535, 1, 10, errbuf); - if(handle == NULL) - { - QString err = QString("pcap_open_live return err,errbuf:%1...").arg(errbuf); - emit sig_message(err); - return ; - } - struct bpf_program filter; - bpf_u_int32 net = 0; - int ret32 = pcap_compile(handle, &filter, m_filter.c_str(), 0, net); - if(ret32 < 0) - { - QString err = QString("pcap_compile return %1, errbuf:%2").arg(ret32).arg(errbuf); - emit sig_message(err); - pcap_close(handle); - return ; - } - ret32 = pcap_setfilter(handle, &filter); - if(ret32 < 0) - { - QString err = QString("pcap_setfilter return %1, errbuf:%2").arg(ret32).arg(errbuf); - emit sig_message(err); - pcap_close(handle); - return ; - } - const u_char *packet; - struct pcap_pkthdr header; - //注意,要设置较大的缓存 - pcap_set_buffer_size(handle,256*1024*1024); - emit sig_message("Cap Started"); - while (!m_bStop) - { - packet = pcap_next(handle, &header); - if(packet) - { - ++m_rx; - if (m_nSubject) - TASKBUS::push_subject(m_nSubject,m_nInstance, - header.len,(unsigned char *)packet); - } - else - { - fprintf(stderr,pcap_geterr(handle)); - fprintf(stderr,"\n"); - fflush(stderr); - break; - } - } - pcap_close(handle); - } - - emit sig_message("Cap Stopped"); -} diff --git a/modules/sources/source_pcap/pcapio.h b/modules/sources/source_pcap/pcapio.h deleted file mode 100644 index f319825..0000000 --- a/modules/sources/source_pcap/pcapio.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef PCAPIO_H -#define PCAPIO_H -#include -#include -#include -#include -namespace PCAPIO{ - -std::string pcapio_interfaces(std::map & devmap); -} - -class pcap_source : public QThread{ - Q_OBJECT -public: - explicit pcap_source (QObject * parent = nullptr); - void startCap(); - void stopCap(); -protected: - void run() override; - -protected: - std::atomic m_bStop; -public: - std::string m_filter; - std::string m_device; - std::atomic m_rx = 0; - int m_nInstance = 0; - int m_nSubject = 0; -signals: - void sig_message(QString msg); -}; - - -#endif // PCAPIO_H diff --git a/modules/sources/source_pcap/source_pcap.json b/modules/sources/source_pcap/source_pcap.json deleted file mode 100644 index d1d5e86..0000000 --- a/modules/sources/source_pcap/source_pcap.json +++ /dev/null @@ -1,37 +0,0 @@ -{ - "source_pcap":{ - "name":"pcap_src", - "parameters":{ - "device":{ - "type":"string", - "tooltip":"device list index", - "default":"0" - }, - "filter":{ - "type":"string", - "tooltip":"filter of cap", - "default":"icmp and ip host 127.0.0.1" - }, - "hide":{ - "type":"int", - "tooltip":"hide window when start", - "default":0 - }, - "autostart":{ - "type":"int", - "tooltip":"auto start", - "default":0 - } - }, - "input_subjects": - { - }, - "output_subjects":{ - "package":{ - "type":"bytes", - "tooltip":"Package" - } - - } - } -} diff --git a/modules/sources/source_pcap/source_pcap.pro b/modules/sources/source_pcap/source_pcap.pro deleted file mode 100644 index 303a269..0000000 --- a/modules/sources/source_pcap/source_pcap.pro +++ /dev/null @@ -1,55 +0,0 @@ -#------------------------------------------------- -# -# Project created by QtCreator 2018-10-26T07:14:47 -# -#------------------------------------------------- - -QT += core gui - -greaterThan(QT_MAJOR_VERSION, 4): QT += widgets -INCLUDEPATH += ../../../tb_interface -DESTDIR = $$OUT_PWD/../../../bin/modules -TARGET = source_pcap -TEMPLATE = app - -# The following define makes your compiler emit warnings if you use -# any feature of Qt which has been marked as deprecated (the exact warnings -# depend on your compiler). Please consult the documentation of the -# deprecated API in order to know how to port your code away from it. -DEFINES += QT_DEPRECATED_WARNINGS - -# You can also make your code fail to compile if you use deprecated APIs. -# In order to do so, uncomment the following line. -# You can also select to disable deprecated APIs only up to a certain version of Qt. -#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 - -CONFIG += c++17 - -SOURCES += \ - main.cpp \ - dialogsourcepcap.cpp \ - listen_thread.cpp \ - pcapio.cpp - -HEADERS += \ - dialogsourcepcap.h \ - listen_thread.h \ - pcapio.h - -FORMS += \ - dialogsourcepcap.ui -win32{ - INCLUDEPATH +="$$PWD/../../3rdlibs/win32/npcap/Include" - contains(QT_ARCH, i386) { - LIBS+=-L"$$PWD/../../3rdlibs/win32/npcap/lib" -lwpcap - } else { - LIBS+=-L"$$PWD/../../3rdlibs/win32/npcap/lib/x64" -lwpcap - } -}else: LIBS+=-lpcap -# Default rules for deployment. -qnx: target.path = /tmp/$${TARGET}/bin -else: unix:!android: target.path = /opt/$${TARGET}/bin -!isEmpty(target.path): INSTALLS += target - -RESOURCES += \ - source_pcap.qrc diff --git a/modules/sources/source_pcap/source_pcap.qrc b/modules/sources/source_pcap/source_pcap.qrc deleted file mode 100644 index 381754a..0000000 --- a/modules/sources/source_pcap/source_pcap.qrc +++ /dev/null @@ -1,5 +0,0 @@ - - - source_pcap.json - - -- GitLab