]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
lib: take pointer to LiveDevice, not name
authorJason Ish <jason.ish@oisf.net>
Mon, 3 Jun 2024 23:04:10 +0000 (17:04 -0600)
committerVictor Julien <victor@inliniac.net>
Tue, 1 Apr 2025 08:17:05 +0000 (10:17 +0200)
In the library capture example, show how the packet counter can be
updated.

Ticket: #7240

examples/lib/custom/main.c
src/source-lib.c
src/source-lib.h

index 11c10ab6ff86fbcb630691ea9d17cd6c2496ed50..3a6a81e44af6d7cc4db122ef3b97690f31a3158f 100644 (file)
@@ -49,13 +49,21 @@ static void *SimpleWorker(void *arg)
         pthread_exit(NULL);
     }
 
+    LiveDevice *device = LiveGetDevice("lib0");
+    assert(device != NULL);
+
     int datalink = pcap_datalink(fp);
+    int count = 0;
     struct pcap_pkthdr pkthdr;
     const u_char *packet;
     while ((packet = pcap_next(fp, &pkthdr)) != NULL) {
-        if (TmModuleLibHandlePacket(tv, packet, datalink, pkthdr.ts, pkthdr.len, 0, 0, NULL) != 0) {
+        if (TmModuleLibHandlePacket(tv, device, packet, datalink, pkthdr.ts, pkthdr.len, 0, 0) !=
+                0) {
             pthread_exit(NULL);
         }
+
+        (void)SC_ATOMIC_ADD(device->pkts, 1);
+        count++;
     }
     pcap_close(fp);
 
@@ -120,6 +128,11 @@ int main(int argc, char **argv)
     /* Force logging to the current directory. */
     ConfSetFromString("default-log-dir=.", 1);
 
+    if (LiveRegisterDevice("lib0") < 0) {
+        fprintf(stderr, "LiveRegisterDevice failed");
+        exit(1);
+    }
+
     SuricataInit();
 
     /* Create and start worker on its own thread, passing the PCAP
index 6c304fc30069465acf05cb8c6f8b5134539d09c7..7e8f55415ecda1b7bb1cbcbd46e14c5d44333d25 100644 (file)
@@ -122,17 +122,17 @@ TmEcode DecodeLib(ThreadVars *tv, Packet *p, void *data)
 /** \brief process a single packet.
  *
  * \param tv                    Pointer to the per-thread structure.
+ * \param device                Pionter to LiveDevice instance
  * \param data                  Pointer to the raw packet.
  * \param datalink              Datalink type.
  * \param ts                    Timeval structure.
  * \param len                   Packet length.
  * \param tenant_id             Tenant id of the detection engine to use.
  * \param flags                 Packet flags (packet checksum, rule profiling...).
- * \param iface                 Sniffing interface this packet comes from (can be NULL).
  * \return                      Error code.
  */
-int TmModuleLibHandlePacket(ThreadVars *tv, const uint8_t *data, int datalink, struct timeval ts,
-        uint32_t len, uint32_t tenant_id, uint32_t flags, const char *iface)
+int TmModuleLibHandlePacket(ThreadVars *tv, LiveDevice *device, const uint8_t *data, int datalink,
+        struct timeval ts, uint32_t len, uint32_t tenant_id, uint32_t flags)
 {
 
     /* If the packet is NULL, consider it as a read timeout. */
@@ -159,11 +159,7 @@ int TmModuleLibHandlePacket(ThreadVars *tv, const uint8_t *data, int datalink, s
     p->datalink = datalink;
     p->tenant_id = tenant_id;
     p->flags |= flags;
-
-    /* Set the sniffing interface. */
-    if (iface) {
-        p->livedev = LiveGetDevice(iface);
-    }
+    p->livedev = device;
 
     if (PacketSetData(p, data, len) == -1) {
         TmqhOutputPacketpool(tv, p);
index 3f0594bd8eade65dc9bb4c999e1ff13a8df805d4..432f12aa505b931b88a155b210c0ba1172bbb5ea 100644 (file)
@@ -27,6 +27,7 @@
 #define SURICATA_SOURCE_LIB_H
 
 #include "tm-threads.h"
+#include "util-device.h"
 
 /** \brief register a "Decode" module for suricata as a library.
  *
@@ -45,7 +46,7 @@ void TmModuleDecodeLibRegister(void);
  * \param iface                 Sniffing interface this packet comes from (can be NULL).
  * \return                      Error code.
  */
-int TmModuleLibHandlePacket(ThreadVars *tv, const uint8_t *data, int datalink, struct timeval ts,
-        uint32_t len, uint32_t tenant_id, uint32_t flags, const char *iface);
+int TmModuleLibHandlePacket(ThreadVars *tv, LiveDevice *device, const uint8_t *data, int datalink,
+        struct timeval ts, uint32_t len, uint32_t tenant_id, uint32_t flags);
 
 #endif /* SURICATA_SOURCE_LIB_H */