From a17ac21077a9b3264020793e7a67518b339a3680 Mon Sep 17 00:00:00 2001 From: maxtors Date: Thu, 16 Apr 2015 22:22:54 +0200 Subject: [PATCH] Reworked how shortening should be performed. The shortening of the interfacenames is now dependent on the size of the destination buffer, so that this can be easily changed in the future. The process uses snprintf and strlcat. Also changed the buffer sizes in the util-runmodes to 12 so that they can hold 11 chars + null terminator. --- src/util-device.c | 41 +++++++++++++++++++++++++++++++++-------- src/util-runmodes.c | 8 ++++---- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/util-device.c b/src/util-device.c index 6d0e0d3d7d..b29583f574 100644 --- a/src/util-device.c +++ b/src/util-device.c @@ -19,8 +19,7 @@ #include "conf.h" #include "util-device.h" -#define MAX_DEVNAME 12 -#define DEVNAME_CHUNCK 5 +#define MAX_DEVNAME 11 /** * \file @@ -119,18 +118,44 @@ int LiveSafeDeviceName(const char *devname, char *newdevname, size_t destlen) { size_t devnamelen = strlen(devname); - // If we have to shorten the interface name + /* If we have to shorten the interface name */ if (devnamelen > MAX_DEVNAME) { - // We need 13 chars to do this shortening - if (destlen < 13) { + /* IF the dest length is over 11 chars long it will not do any + * good for the shortening. The shortening is done due to the + * max length of pthread names (15 chars) and we use 2 chars + * for the threadname indicator eg. "W-" and one-two chars for + * the thread number. And if the destination buffer is under + * 6 chars there is point in shortening it since we must at + * lest enter two periodes (.) into the string.. + */ + if ((destlen-1) > 11 && (destlen-1) < 6) { return 1; } size_t length; - length = strlcpy(newdevname, devname, DEVNAME_CHUNCK); - length = strlcat(newdevname, "...", DEVNAME_CHUNCK+3); - length = strlcat(newdevname, devname+(devnamelen-DEVNAME_CHUNCK), length+DEVNAME_CHUNCK); + size_t half; + size_t spaces; + + half = (destlen-1) / 2; + + /* If the destlen is an even number */ + if (half * 2 == (destlen-1)) { + half = half - 1; + } + + spaces = (destlen-1) - (half*2); + length = half; + + /* Add the first half to the new dev name */ + snprintf(newdevname, half+1, "%s", devname); + + /* Add the amount of spaces wanted */ + for (uint i = half; i < half+spaces; i++) { + length = strlcat(newdevname, ".", destlen); + } + + snprintf(newdevname+length, half+1, "%s", devname+(devnamelen-half)); SCLogInfo("Shortening device name to: %s", newdevname); } else { strlcpy(newdevname, devname, destlen); diff --git a/src/util-runmodes.c b/src/util-runmodes.c index 85c1d50190..30b410e2cd 100644 --- a/src/util-runmodes.c +++ b/src/util-runmodes.c @@ -186,7 +186,7 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser, for (lthread = 0; lthread < nlive; lthread++) { char *live_dev = LiveGetDeviceName(lthread); - char visual_devname[14] = ""; + char visual_devname[12] = ""; int shortening_result; void *aconf; int threads_count; @@ -206,7 +206,7 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser, threads_count = ModThreadsCount(aconf); for (thread = 0; thread < threads_count; thread++) { - shortening_result = LiveSafeDeviceName(live_dev, visual_devname, 13); + 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); @@ -329,7 +329,7 @@ static int RunModeSetLiveCaptureWorkersForDevice(ConfigIfaceThreadsCountFunc Mod for (thread = 0; thread < threads_count; thread++) { char tname[TM_THREAD_NAME_MAX]; char *n_thread_name = NULL; - char visual_devname[14] = ""; + char visual_devname[12] = ""; int shortening_result; ThreadVars *tv = NULL; TmModule *tm_module = NULL; @@ -337,7 +337,7 @@ static int RunModeSetLiveCaptureWorkersForDevice(ConfigIfaceThreadsCountFunc Mod if (single_mode) { snprintf(tname, sizeof(tname), "%s", thread_name); } else { - shortening_result = LiveSafeDeviceName(live_dev, visual_devname, 13); + 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);