X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/9a060830dcc1d1ae3e557b56e937fb2966dd6337..80aca6d78d4ff5d514216fe96c44e2ee75c4e933:/projects/stargazer/plugins/capture/nfqueue/nfqueue.cpp diff --git a/projects/stargazer/plugins/capture/nfqueue/nfqueue.cpp b/projects/stargazer/plugins/capture/nfqueue/nfqueue.cpp index 245fc649..0a5d8712 100644 --- a/projects/stargazer/plugins/capture/nfqueue/nfqueue.cpp +++ b/projects/stargazer/plugins/capture/nfqueue/nfqueue.cpp @@ -21,7 +21,6 @@ #include "nfqueue.h" #include "stg/traffcounter.h" -#include "stg/plugin_creator.h" #include "stg/common.h" #include "stg/raw_ip_packet.h" @@ -32,8 +31,12 @@ extern "C" { } +#include +#include + #include // ntohl -#include + +#include // read //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- @@ -41,9 +44,7 @@ extern "C" { namespace { -PLUGIN_CREATOR ncc; - -int callback(struct nfq_q_handle * /*queueHandle*/, struct nfgenmsg * /*msg*/, +int Callback(struct nfq_q_handle * queueHandle, struct nfgenmsg * /*msg*/, struct nfq_data * nfqData, void *data) { int id = 0; @@ -56,29 +57,26 @@ id = ntohl(packetHeader->packet_id); unsigned char * payload = NULL; -if (nfq_get_payload(nfqData) < 0) +if (nfq_get_payload(nfqData, &payload) < 0 || payload == NULL) return id; -RAW_PACKET packet; +STG::RawPacket packet; -memcpy(&packet.rawPacket, payload, sizeof(ip.rawPacket)); +memcpy(&packet.rawPacket, payload, sizeof(packet.rawPacket)); NFQ_CAP * cap = static_cast(data); cap->Process(packet); -return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL); +return nfq_set_verdict(queueHandle, id, NF_ACCEPT, 0, NULL); } } -extern "C" PLUGIN * GetPlugin(); -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -PLUGIN * GetPlugin() +extern "C" STG::Plugin* GetPlugin() { -return ncc.GetPlugin(); + static NFQ_CAP plugin; + return &plugin; } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- @@ -89,17 +87,25 @@ return "cap_nfqueue v.1.0"; } //----------------------------------------------------------------------------- NFQ_CAP::NFQ_CAP() - : errorStr(), - thread(), - nonstop(false), - isRunning(false), + : isRunning(false), + queueNumber(0), + nfqHandle(NULL), + queueHandle(NULL), traffCnt(NULL), - logger(GetPluginLogger(GetStgLogger(), "cap_nfqueue")) + logger(STG::PluginLogger::get("cap_nfqueue")) { } //----------------------------------------------------------------------------- int NFQ_CAP::ParseSettings() { +for (size_t i = 0; i < settings.moduleParams.size(); i++) + if (settings.moduleParams[i].param == "queueNumber" && !settings.moduleParams[i].value.empty()) + if (str2x(settings.moduleParams[i].value[0], queueNumber) < 0) + { + errorStr = "Queue number should be a number. Got: '" + settings.moduleParams[i].param + "'"; + logger(errorStr); + return -1; + } return 0; } //----------------------------------------------------------------------------- @@ -133,27 +139,19 @@ if (nfq_bind_pf(nfqHandle, AF_INET) < 0) queueHandle = nfq_create_queue(nfqHandle, queueNumber, &Callback, this); if (queueHandle == NULL) { - errorStr = "Failed to create queue " + x2str(queueNumber) + "."; + errorStr = "Failed to create queue " + std::to_string(queueNumber) + "."; logger(errorStr); return -1; } if (nfq_set_mode(queueHandle, NFQNL_COPY_PACKET, 0xffFF) < 0) { - errorStr = "Failed to set queue " + x2str(queueNumber) + " mode."; + errorStr = "Failed to set queue " + std::to_string(queueNumber) + " mode."; logger(errorStr); return -1; } -nonstop = true; - -if (pthread_create(&thread, NULL, Run, this)) - { - errorStr = "Cannot create thread."; - logger("Cannot create thread."); - printfd(__FILE__, "Cannot create thread\n"); - return -1; - } +m_thread = std::jthread([this](auto token){ Run(std::move(token)); }); return 0; } @@ -163,7 +161,7 @@ int NFQ_CAP::Stop() if (!isRunning) return 0; -nonstop = false; +m_thread.request_stop(); //5 seconds to thread stops itself for (int i = 0; i < 25 && isRunning; i++) @@ -173,28 +171,9 @@ for (int i = 0; i < 25 && isRunning; i++) } //after 5 seconds waiting thread still running. now killing it if (isRunning) - { - if (pthread_kill(thread, SIGUSR1)) - { - errorStr = "Cannot kill thread."; - logger("Cannot send signal to thread."); - return -1; - } - for (int i = 0; i < 25 && isRunning; ++i) - { - struct timespec ts = {0, 200000000}; - nanosleep(&ts, NULL); - } - if (isRunning) - { - errorStr = "NFQ_CAP not stopped."; - logger("Cannot stop thread."); - printfd(__FILE__, "Cannot stop thread\n"); - return -1; - } - } - -pthread_join(thread, NULL); + m_thread.detach(); +else + m_thread.join(); nfq_destroy_queue(queueHandle); nfq_close(nfqHandle); @@ -202,33 +181,36 @@ nfq_close(nfqHandle); return 0; } //----------------------------------------------------------------------------- -void * NFQ_CAP::Run(void * d) +void NFQ_CAP::Run(std::stop_token token) { sigset_t signalSet; sigfillset(&signalSet); pthread_sigmask(SIG_BLOCK, &signalSet, NULL); -NFQ_CAP * dc = static_cast(d); -dc->isRunning = true; +isRunning = true; int fd = nfq_fd(nfqHandle); char buf[4096]; -while (dc->nonstop) +while (!token.stop_requested()) { if (!WaitPackets(fd)) continue; - int rv = read(fd, buf, sizeof(buf), 0); + int rv = read(fd, buf, sizeof(buf)); if (rv < 0) { - errorStr = "Read error: " + strerror(errno); + errorStr = std::string("Read error: ") + strerror(errno); logger(errorStr); break; } nfq_handle_packet(nfqHandle, buf, rv); } -dc->isRunning = false; -return NULL; +isRunning = false; +} +//----------------------------------------------------------------------------- +void NFQ_CAP::Process(const STG::RawPacket & packet) +{ +traffCnt->process(packet); }