]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
livedev: shorten devname at registration
authorVictor Julien <victor@inliniac.net>
Wed, 8 Jun 2016 06:17:13 +0000 (08:17 +0200)
committerVictor Julien <victor@inliniac.net>
Wed, 8 Jun 2016 08:02:07 +0000 (10:02 +0200)
src/respond-reject-libnet11.c
src/runmode-af-packet.c
src/runmode-netmap.c
src/source-mpipe.c
src/suricata.c
src/util-device.c
src/util-device.h
src/util-runmodes.c

index b27b74e9d98fba8a2a713b85c179ca4b976ccc23..c52e340c5b65306a275b880fed4faf2a1dfb3abf 100644 (file)
@@ -79,7 +79,7 @@ int RejectSendLibnet11L3IPv4TCP(ThreadVars *tv, Packet *p, void *data, int dir)
     libnet_t *c; /* libnet context */
     char ebuf[LIBNET_ERRBUF_SIZE];
     int result;
-    char *devname = NULL;
+    const char *devname = NULL;
 
     /* fill in struct defaults */
     lpacket.ttl = 0;
@@ -203,7 +203,7 @@ int RejectSendLibnet11L3IPv4ICMP(ThreadVars *tv, Packet *p, void *data, int dir)
     libnet_t *c; /* libnet context */
     char ebuf[LIBNET_ERRBUF_SIZE];
     int result;
-    char *devname = NULL;
+    const char *devname = NULL;
 
     /* fill in struct defaults */
     lpacket.ttl = 0;
@@ -291,7 +291,7 @@ int RejectSendLibnet11L3IPv6TCP(ThreadVars *tv, Packet *p, void *data, int dir)
     libnet_t *c; /* libnet context */
     char ebuf[LIBNET_ERRBUF_SIZE];
     int result;
-    char *devname = NULL;
+    const char *devname = NULL;
 
     /* fill in struct defaults */
     lpacket.ttl = 0;
@@ -414,7 +414,7 @@ int RejectSendLibnet11L3IPv6ICMP(ThreadVars *tv, Packet *p, void *data, int dir)
     libnet_t *c; /* libnet context */
     char ebuf[LIBNET_ERRBUF_SIZE];
     int result;
-    char *devname = NULL;
+    const char *devname = NULL;
 
     /* fill in struct defaults */
     lpacket.ttl = 0;
index 34b1a3872b3aff6e779cbac289a39096129c39fc..f485f5b23c2f3b28fac91865236d671e4aae4c9b 100644 (file)
@@ -479,7 +479,7 @@ int AFPRunModeIsIPS()
     if_default = ConfNodeLookupKeyValue(af_packet_node, "interface", "default");
 
     for (ldev = 0; ldev < nlive; ldev++) {
-        char *live_dev = LiveGetDeviceName(ldev);
+        const char *live_dev = LiveGetDeviceName(ldev);
         if (live_dev == NULL) {
             SCLogError(SC_ERR_INVALID_VALUE, "Problem with config file");
             return 0;
@@ -509,7 +509,7 @@ int AFPRunModeIsIPS()
     if (has_ids && has_ips) {
         SCLogInfo("AF_PACKET mode using IPS and IDS mode");
         for (ldev = 0; ldev < nlive; ldev++) {
-            char *live_dev = LiveGetDeviceName(ldev);
+            const char *live_dev = LiveGetDeviceName(ldev);
             if (live_dev == NULL) {
                 SCLogError(SC_ERR_INVALID_VALUE, "Problem with config file");
                 return 0;
index 8403d0843ebff0c2985e13446c6e12bc19a83a5e..e476dd04033e65879a56ec40dc206cd469f2ae27 100644 (file)
@@ -300,7 +300,7 @@ int NetmapRunModeIsIPS()
     if_default = ConfNodeLookupKeyValue(netmap_node, "interface", "default");
 
     for (ldev = 0; ldev < nlive; ldev++) {
-        char *live_dev = LiveGetDeviceName(ldev);
+        const char *live_dev = LiveGetDeviceName(ldev);
         if (live_dev == NULL) {
             SCLogError(SC_ERR_INVALID_VALUE, "Problem with config file");
             return 0;
@@ -330,7 +330,7 @@ int NetmapRunModeIsIPS()
     if (has_ids && has_ips) {
         SCLogInfo("Netmap mode using IPS and IDS mode");
         for (ldev = 0; ldev < nlive; ldev++) {
-            char *live_dev = LiveGetDeviceName(ldev);
+            const char *live_dev = LiveGetDeviceName(ldev);
             if (live_dev == NULL) {
                 SCLogError(SC_ERR_INVALID_VALUE, "Problem with config file");
                 return 0;
index d304b4a8b108b6f4ffea6c0129b375870afeb1fa..0310115a0401e65b53771792b44fe9035b193685 100644 (file)
@@ -895,8 +895,8 @@ TmEcode ReceiveMpipeThreadInit(ThreadVars *tv, void *initdata, void **data)
     ptv->tv = tv;
 
     int result;
-    char *link_name = (char *)initdata;
-  
+    const char *link_name = (char *)initdata;
+
     MpipeRegisterPerfCounters(ptv, tv);
 
     *data = (void *)ptv;
index 198420b3e602a0214e338456ce03a10546ee2c19..a6e3e1f7b1a0fa5b0afc05f012afd83b0cd13542 100644 (file)
@@ -2233,7 +2233,7 @@ static int ConfigGetCaptureValue(SCInstance *suri)
             case RUNMODE_PFRING:
                 nlive = LiveGetDeviceCount();
                 for (lthread = 0; lthread < nlive; lthread++) {
-                    char *live_dev = LiveGetDeviceName(lthread);
+                    const char *live_dev = LiveGetDeviceName(lthread);
                     char dev[32];
                     (void)strlcpy(dev, live_dev, sizeof(dev));
 
index 409f8732cc62133321b4a59d8d82cfd90d2ac5da..582746ac126fc7a80eccc789410594aaea6e1fe3 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2011-2012 Open Information Security Foundation
+/* Copyright (C) 2011-2016 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -36,6 +36,9 @@ static TAILQ_HEAD(, LiveDevice_) live_devices =
 /** if set to 0 when we don't have real devices */
 static int live_devices_stats = 1;
 
+static int LiveSafeDeviceName(const char *devname,
+                              char *newdevname, size_t destlen);
+
 /**
  *  \brief Add a pcap device for monitoring
  *
@@ -56,6 +59,13 @@ int LiveRegisterDevice(const char *dev)
         SCFree(pd);
         return -1;
     }
+    /* create a short version to be used in thread names */
+    if (strlen(pd->dev) > MAX_DEVNAME) {
+        LiveSafeDeviceName(pd->dev, pd->dev_short, sizeof(pd->dev_short));
+    } else {
+        (void)strlcpy(pd->dev_short, pd->dev, sizeof(pd->dev_short));
+    }
+
     SC_ATOMIC_INIT(pd->pkts);
     SC_ATOMIC_INIT(pd->drop);
     SC_ATOMIC_INIT(pd->invalid_checksums);
@@ -91,7 +101,7 @@ int LiveGetDeviceCount(void)
  *  \retval ptr pointer to the string containing the device
  *  \retval NULL on error
  */
-char *LiveGetDeviceName(int number)
+const char *LiveGetDeviceName(int number)
 {
     int i = 0;
     LiveDevice *pd;
@@ -107,14 +117,14 @@ char *LiveGetDeviceName(int number)
     return NULL;
 }
 
-/**
+/** \internal
  *  \brief Shorten a device name that is to long
  *
  *  \param device name from config and destination for modified
  *
  *  \retval None, is added to destination char *newdevname
  */
-int LiveSafeDeviceName(const char *devname, char *newdevname, size_t destlen)
+static int LiveSafeDeviceName(const char *devname, char *newdevname, size_t destlen)
 {
     size_t devnamelen = strlen(devname);
 
@@ -192,7 +202,13 @@ LiveDevice *LiveGetDevice(const char *name)
     return NULL;
 }
 
-
+const char *LiveGetShortName(const char *dev)
+{
+    LiveDevice *live_dev = LiveGetDevice(dev);
+    if (live_dev == NULL)
+        return NULL;
+    return live_dev->dev_short;
+}
 
 int LiveBuildDeviceList(const char *runmode)
 {
index a6f78dd50b1a4f8968c63c25a9a7d492b584900a..ca3d91331a35484ae40116a63fcb94b100a15520 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2011-2012 Open Information Security Foundation
+/* Copyright (C) 2011-2016 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
 #include "queue.h"
 #include "unix-manager.h"
 
+#define MAX_DEVNAME 10
+
 /** storage for live device names */
 typedef struct LiveDevice_ {
     char *dev;  /**< the device (e.g. "eth0") */
+    char dev_short[MAX_DEVNAME + 1];
     int ignore_checksum;
     SC_ATOMIC_DECLARE(uint64_t, pkts);
     SC_ATOMIC_DECLARE(uint64_t, drop);
@@ -34,9 +37,9 @@ typedef struct LiveDevice_ {
 
 int LiveRegisterDevice(const char *dev);
 int LiveGetDeviceCount(void);
-char *LiveGetDeviceName(int number);
-int LiveSafeDeviceName(const char *devname, char *newdevname, size_t destlen);
+const char *LiveGetDeviceName(int number);
 LiveDevice *LiveGetDevice(const char *dev);
+const char *LiveGetShortName(const char *dev);
 int LiveBuildDeviceList(const char *base);
 void LiveDeviceHasNoStats(void);
 int LiveDeviceListClean(void);
index d2789f0f47e2365cbe280681036ba026b2bdca7b..8f01983ab41bb358997fda4d02e83a8c9134a747 100644 (file)
@@ -169,9 +169,8 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser,
         int lthread;
 
         for (lthread = 0; lthread < nlive; lthread++) {
-            char *live_dev = LiveGetDeviceName(lthread);
-            char visual_devname[11] = "";
-            int shortening_result;
+            const char *live_dev = LiveGetDeviceName(lthread);
+            const char *visual_devname = LiveGetShortName(live_dev);
             void *aconf;
             int threads_count;
 
@@ -190,12 +189,6 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser,
 
             threads_count = ModThreadsCount(aconf);
             for (thread = 0; thread < threads_count; thread++) {
-                shortening_result = LiveSafeDeviceName(live_dev, visual_devname, sizeof(visual_devname));
-                if (shortening_result != 0) {
-                    SCLogError(SC_ERR_INVALID_VALUE, "Could not shorten long devicename: %s", live_dev);
-                    exit(EXIT_FAILURE);
-                }
-
                 snprintf(tname, sizeof(tname), "%s#%02d-%s", thread_name,
                          thread+1, visual_devname);
 
@@ -298,16 +291,9 @@ static int RunModeSetLiveCaptureWorkersForDevice(ConfigIfaceThreadsCountFunc Mod
     /* create the threads */
     for (thread = 0; thread < threads_count; thread++) {
         char tname[TM_THREAD_NAME_MAX];
-        char visual_devname[11] = "";
-        int shortening_result;
         ThreadVars *tv = NULL;
         TmModule *tm_module = NULL;
-
-        shortening_result = LiveSafeDeviceName(live_dev, visual_devname, sizeof(visual_devname));
-        if (shortening_result != 0) {
-            SCLogError(SC_ERR_INVALID_VALUE, "Could not shorten long devicename: %s", live_dev);
-            exit(EXIT_FAILURE);
-        }
+        const char *visual_devname = LiveGetShortName(live_dev);
 
         if (single_mode) {
             snprintf(tname, sizeof(tname), "%s#01-%s", thread_name, visual_devname);
@@ -447,7 +433,7 @@ int RunModeSetIPSAutoFp(ConfigIPSParserFunc ConfigParser,
     char tname[TM_THREAD_NAME_MAX];
     char qname[TM_QUEUE_NAME_MAX];
     TmModule *tm_module ;
-    char *cur_queue = NULL;
+    const char *cur_queue = NULL;
     char *queues = NULL;
     int thread;
 
@@ -593,7 +579,7 @@ int RunModeSetIPSWorker(ConfigIPSParserFunc ConfigParser,
     char tname[TM_THREAD_NAME_MAX];
     ThreadVars *tv = NULL;
     TmModule *tm_module = NULL;
-    char *cur_queue = NULL;
+    const char *cur_queue = NULL;
 
     int nqueue = LiveGetDeviceCount();