]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp
More std::jthread stuff.
[stg.git] / projects / stargazer / plugins / capture / divert_freebsd / divert_cap.cpp
index 98bf1b6c0c3e4340e1e81a462a8e318aed97773a..e3a1d497e357d5c6aa508773fa4ff8fc5de2caaa 100644 (file)
 $Revision: 1.13 $
 $Date: 2010/09/10 06:43:03 $
 */
+
+#include "divert_cap.h"
+
+#include "stg/traffcounter.h"
+#include "stg/raw_ip_packet.h"
+#include "stg/common.h"
+
+#include <algorithm>
+#include <vector>
+
+#include <cstdio>
+#include <cstring>
+#include <cerrno>
+#include <cstdlib>
+#include <csignal>
+
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
@@ -34,27 +50,12 @@ $Date: 2010/09/10 06:43:03 $
 #include <fcntl.h>
 #include <unistd.h>
 
-#include <cstdio>
-#include <cstring>
-#include <cerrno>
-#include <cstdlib>
-#include <csignal>
-
-#include <algorithm>
-#include <vector>
-
-#include "stg/common.h"
-#include "stg/traffcounter.h"
-#include "sg/plugin_creator.h"
-#include "divert_cap.h"
-
 #define BUFF_LEN (16384) /* max mtu -> lo=16436  TODO why?*/
 
 //-----------------------------------------------------------------------------
 struct DIVERT_DATA {
 int sock;
 short int port;
-unsigned char buffer[BUFF_LEN];
 char iface[10];
 };
 //-----------------------------------------------------------------------------
@@ -63,30 +64,26 @@ DIVERT_DATA cddiv;  //capture data
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-PLUGIN_CREATOR<DIVERT_CAP> dcc;
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-PLUGIN * GetPlugin()
+
+extern "C" STG::Plugin* GetPlugin()
 {
-return dcc.GetPlugin();
+    static DIVERT_CAP plugin;
+    return &plugin;
 }
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-const std::string DIVERT_CAP::GetVersion() const
+std::string DIVERT_CAP::GetVersion() const
 {
-return "Divert_cap v.1.0";
+return "cap_divert v.1.0";
 }
 //-----------------------------------------------------------------------------
 DIVERT_CAP::DIVERT_CAP()
-    : settings(),
-      port(0),
-      errorStr(),
-      thread(),
-      nonstop(false),
+    : port(0),
+      disableForwarding(false),
       isRunning(false),
-      traffCnt(NULL)
+      traffCnt(NULL),
+      logger(STG::PluginLogger::get("cap_divert"))
 {
 }
 //-----------------------------------------------------------------------------
@@ -102,16 +99,9 @@ if (DivertCapOpen() < 0)
     return -1;
     }
 
-nonstop = true;
-
-if (pthread_create(&thread, NULL, Run, this) == 0)
-    {
-    return 0;
-    }
+m_thread = std::jthread([](auto token){ Run(std::move(token)); });
 
-errorStr = "Cannot create thread.";
-printfd(__FILE__, "Cannot create thread\n");
-return -1;
+return 0;
 }
 //-----------------------------------------------------------------------------
 int DIVERT_CAP::Stop()
@@ -121,53 +111,49 @@ if (!isRunning)
 
 DivertCapClose();
 
-nonstop = false;
-
+m_thread.request_stop();
 //5 seconds to thread stops itself
-int i;
-for (i = 0; i < 25; i++)
+for (size_t i = 0; i < 25; i++)
     {
     if (!isRunning)
         break;
 
-    usleep(200000);
+    struct timespec ts = {0, 200000000};
+    nanosleep(&ts, NULL);
     }
 
 //after 5 seconds waiting thread still running. now killing it
 if (isRunning)
-    {
-    if (pthread_kill(thread, SIGINT))
-        {
-        errorStr = "Cannot kill thread.";
-        printfd(__FILE__, "Cannot kill thread\n");
-        return -1;
-        }
-    }
+    m_thread.detach();
+else
+    m_thread.join();
 
 return 0;
 }
 //-----------------------------------------------------------------------------
-void * DIVERT_CAP::Run(void * d)
+void DIVERT_CAP::Run(std::stop_token token) noexcept
 {
-DIVERT_CAP * dc = (DIVERT_CAP *)d;
-dc->isRunning = true;
+sigset_t signalSet;
+sigfillset(&signalSet);
+pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
+
+isRunning = true;
 
-char buffer[64];
-while (dc->nonstop)
+char buffer[STG::packetSize + 14];
+while (!token.stop_requested())
     {
-    RAW_PACKET rp;
-    dc->DivertCapRead(buffer, 64, NULL);
+    STG::RawPacket rp;
+    DivertCapRead(buffer, sizeof(buffer), NULL);
 
     if (buffer[12] != 0x8)
         continue;
 
-    memcpy(rp.pckt, &buffer[14], pcktSize);
+    memcpy(&rp.rawPacket, &buffer[14], STG::packetSize);
 
-    dc->traffCnt->Process(rp);
+    traffCnt->process(rp);
     }
 
-dc->isRunning = false;
-return NULL;
+isRunning = false;
 }
 //-----------------------------------------------------------------------------
 int DIVERT_CAP::DivertCapOpen()
@@ -192,6 +178,7 @@ cddiv.sock = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT);
 if (cddiv.sock < 0)
     {
     errorStr = "Create divert socket error.";
+    logger("Cannot create a socket: %s", strerror(errno));
     printfd(__FILE__, "Cannot create divert socket\n");
     return -1;
     }
@@ -209,6 +196,7 @@ ret = bind(cddiv.sock, (struct sockaddr *)&divAddr, sizeof(divAddr));
 if (ret < 0)
     {
     errorStr = "Bind divert socket error.";
+    logger("Cannot bind the scoket: %s", strerror(errno));
     printfd(__FILE__, "Cannot bind divert socket\n");
     return -1;
     }
@@ -246,7 +234,16 @@ if ((bytes = recvfrom (cddiv.sock, buf, BUFF_LEN,
     if (iface)
         *iface = cddiv.iface;
 
-    sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize);
+    if (!disableForwarding)
+        {
+        if (sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize) < 0)
+            logger("sendto error: %s", strerror(errno));
+        }
+    }
+else
+    {
+    if (bytes < 0)
+        logger("recvfrom error: %s", strerror(errno));
     }
 
 return 0;
@@ -261,18 +258,16 @@ return 0;
 int DIVERT_CAP::ParseSettings()
 {
 int p;
-PARAM_VALUE pv;
-std::vector<PARAM_VALUE>::const_iterator pvi;
+STG::ParamValue pv;
+std::vector<STG::ParamValue>::const_iterator pvi;
 
 pv.param = "Port";
 pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
-if (pvi == settings.moduleParams.end())
+if (pvi == settings.moduleParams.end() || pvi->value.empty())
     {
-    port = 15701;
-    return 0;
+    p = 15701;
     }
-
-if (ParseIntInRange(pvi->value[0], 1, 65535, &p))
+else if (ParseIntInRange(pvi->value[0], 1, 65535, &p))
     {
     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
@@ -281,6 +276,22 @@ if (ParseIntInRange(pvi->value[0], 1, 65535, &p))
 
 port = p;
 
+bool d = false;
+pv.param = "DisableForwarding";
+pvi = std::find(settings.moduleParams.begin(), settings.moduleParams.end(), pv);
+if (pvi == settings.moduleParams.end() || pvi->value.empty())
+    {
+    disableForwarding = false;
+    }
+else if (ParseYesNo(pvi->value[0], &d))
+    {
+    errorStr = "Cannot parse parameter \'DisableForwarding\': " + errorStr;
+    printfd(__FILE__, "Cannot parse parameter 'DisableForwarding'\n");
+    return -1;
+    }
+
+disableForwarding = d;
+
 return 0;
 }
 //-----------------------------------------------------------------------------