From: Victor Julien Date: Tue, 1 Oct 2019 04:50:38 +0000 (+0200) Subject: detect/address: dead code removal and style cleanups X-Git-Tag: suricata-5.0.3~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b38485bd88fcb44c2b0b0ac5835d086c93601d64;p=thirdparty%2Fsuricata.git detect/address: dead code removal and style cleanups (cherry picked from commit 42d112e7b6db6c85102c1864905cca0ea2c05f88) --- diff --git a/src/detect-engine-address-ipv4.c b/src/detect-engine-address-ipv4.c index e5c3dcf21f..80d2901298 100644 --- a/src/detect-engine-address-ipv4.c +++ b/src/detect-engine-address-ipv4.c @@ -402,45 +402,6 @@ error: return -1; } -/** - * \brief Extends a target address range if the the source address range is - * wider than the target address range on either sides. - * - * Every address is a range, i.e. address->ip1....address->ip2. For - * example 1.2.3.4 to 192.168.1.1. - * if source->ip1 is smaller than target->ip1, it indicates that the - * source's left address limit is greater(range wise) than the target's - * left address limit, and hence we reassign the target's left address - * limit to source's left address limit. - * Similary if source->ip2 is greater than target->ip2, it indicates that - * the source's right address limit is greater(range wise) than the - * target's right address limit, and hence we reassign the target's right - * address limit to source's right address limit. - * - * \param de_ctx Pointer to the detection engine context. - * \param target Pointer to the target DetectAddress instance that has to be - * updated. - * \param source Pointer to the source DetectAddress instance that is used - * to decided whether we extend the target's address range. - * - * \retval 0 On success. - * \retval -1 On failure. - */ -int DetectAddressJoinIPv4(DetectEngineCtx *de_ctx, DetectAddress *target, - DetectAddress *source) -{ - if (source == NULL || target == NULL) - return -1; - - if (SCNtohl(source->ip.addr_data32[0]) < SCNtohl(target->ip.addr_data32[0])) - target->ip.addr_data32[0] = source->ip.addr_data32[0]; - - if (SCNtohl(source->ip2.addr_data32[0]) > SCNtohl(target->ip2.addr_data32[0])) - target->ip2.addr_data32[0] = source->ip2.addr_data32[0]; - - return 0; -} - /********************************Unittests*************************************/ #ifdef UNITTESTS @@ -1298,141 +1259,6 @@ static int DetectAddressIPv4CutNot09(void) return 0; } -static int DetectAddressIPv4Join10(void) -{ - struct in_addr in; - int result = 1; - - DetectAddress *source = DetectAddressInit(); - if (source == NULL) - return 0; - - DetectAddress *target = DetectAddressInit(); - if (target == NULL) { - DetectAddressFree(source); - return 0; - } - - if (inet_pton(AF_INET, "128.51.61.124", &in) < 0) - goto error; - target->ip.addr_data32[0] = in.s_addr; - if (inet_pton(AF_INET, "192.168.1.2", &in) < 0) - goto error; - target->ip2.addr_data32[0] = in.s_addr; - - if (inet_pton(AF_INET, "1.2.3.4", &in) < 0) - goto error; - source->ip.addr_data32[0] = in.s_addr; - if (inet_pton(AF_INET, "192.168.1.2", &in) < 0) - goto error; - source->ip2.addr_data32[0] = in.s_addr; - - result &= (DetectAddressJoinIPv4(NULL, target, source) == 0); - if (inet_pton(AF_INET, "1.2.3.4", &in) < 0) - goto error; - result &= (target->ip.addr_data32[0] == in.s_addr); - if (inet_pton(AF_INET, "192.168.1.2", &in) < 0) - goto error; - result &= (target->ip2.addr_data32[0] == in.s_addr); - - if (inet_pton(AF_INET, "1.2.3.4", &in) < 0) - goto error; - target->ip.addr_data32[0] = in.s_addr; - if (inet_pton(AF_INET, "192.168.1.2", &in) < 0) - goto error; - target->ip2.addr_data32[0] = in.s_addr; - - if (inet_pton(AF_INET, "1.2.3.5", &in) < 0) - goto error; - source->ip.addr_data32[0] = in.s_addr; - if (inet_pton(AF_INET, "192.168.1.1", &in) < 0) - goto error; - source->ip2.addr_data32[0] = in.s_addr; - - result &= (DetectAddressJoinIPv4(NULL, target, source) == 0); - if (inet_pton(AF_INET, "1.2.3.4", &in) < 0) - goto error; - result &= (target->ip.addr_data32[0] == in.s_addr); - if (inet_pton(AF_INET, "192.168.1.2", &in) < 0) - goto error; - result &= (target->ip2.addr_data32[0] == in.s_addr); - - if (inet_pton(AF_INET, "1.2.3.4", &in) < 0) - goto error; - target->ip.addr_data32[0] = in.s_addr; - if (inet_pton(AF_INET, "192.168.1.2", &in) < 0) - goto error; - target->ip2.addr_data32[0] = in.s_addr; - - if (inet_pton(AF_INET, "128.1.5.15", &in) < 0) - goto error; - source->ip.addr_data32[0] = in.s_addr; - if (inet_pton(AF_INET, "200.202.200.200", &in) < 0) - goto error; - source->ip2.addr_data32[0] = in.s_addr; - - result &= (DetectAddressJoinIPv4(NULL, target, source) == 0); - if (inet_pton(AF_INET, "1.2.3.4", &in) < 0) - goto error; - result &= (target->ip.addr_data32[0] == in.s_addr); - if (inet_pton(AF_INET, "200.202.200.200", &in) < 0) - goto error; - result &= (target->ip2.addr_data32[0] == in.s_addr); - - if (inet_pton(AF_INET, "128.51.61.124", &in) < 0) - goto error; - target->ip.addr_data32[0] = in.s_addr; - if (inet_pton(AF_INET, "192.168.1.2", &in) < 0) - goto error; - target->ip2.addr_data32[0] = in.s_addr; - - if (inet_pton(AF_INET, "1.2.3.4", &in) < 0) - goto error; - source->ip.addr_data32[0] = in.s_addr; - if (inet_pton(AF_INET, "192.168.1.2", &in) < 0) - goto error; - source->ip2.addr_data32[0] = in.s_addr; - - result &= (DetectAddressJoinIPv4(NULL, target, source) == 0); - if (inet_pton(AF_INET, "1.2.3.4", &in) < 0) - goto error; - result &= (target->ip.addr_data32[0] == in.s_addr); - if (inet_pton(AF_INET, "192.168.1.2", &in) < 0) - goto error; - result &= (target->ip2.addr_data32[0] == in.s_addr); - - if (inet_pton(AF_INET, "1.2.3.4", &in) < 0) - goto error; - target->ip.addr_data32[0] = in.s_addr; - if (inet_pton(AF_INET, "192.168.1.2", &in) < 0) - goto error; - target->ip2.addr_data32[0] = in.s_addr; - - if (inet_pton(AF_INET, "1.2.3.4", &in) < 0) - goto error; - source->ip.addr_data32[0] = in.s_addr; - if (inet_pton(AF_INET, "192.168.1.2", &in) < 0) - goto error; - source->ip2.addr_data32[0] = in.s_addr; - - result &= (DetectAddressJoinIPv4(NULL, target, source) == 0); - if (inet_pton(AF_INET, "1.2.3.4", &in) < 0) - goto error; - result &= (target->ip.addr_data32[0] == in.s_addr); - if (inet_pton(AF_INET, "192.168.1.2", &in) < 0) - goto error; - result &= (target->ip2.addr_data32[0] == in.s_addr); - - DetectAddressFree(source); - DetectAddressFree(target); - return result; - - error: - DetectAddressFree(source); - DetectAddressFree(target); - return 0; -} - #endif void DetectAddressIPv4Tests(void) @@ -1451,6 +1277,5 @@ void DetectAddressIPv4Tests(void) UtRegisterTest("DetectAddressIPv4CutNot07", DetectAddressIPv4CutNot07); UtRegisterTest("DetectAddressIPv4CutNot08", DetectAddressIPv4CutNot08); UtRegisterTest("DetectAddressIPv4CutNot09", DetectAddressIPv4CutNot09); - UtRegisterTest("DetectAddressIPv4Join10", DetectAddressIPv4Join10); #endif } diff --git a/src/detect-engine-address-ipv4.h b/src/detect-engine-address-ipv4.h index b8b7b344c3..5f2780a3c0 100644 --- a/src/detect-engine-address-ipv4.h +++ b/src/detect-engine-address-ipv4.h @@ -29,8 +29,6 @@ int DetectAddressCmpIPv4(DetectAddress *a, DetectAddress *b); int DetectAddressCutIPv4(DetectEngineCtx *, DetectAddress *, DetectAddress *, DetectAddress **); -int DetectAddressJoinIPv4(DetectEngineCtx *, DetectAddress *target, - DetectAddress *source); int DetectAddressIsCompleteIPSpaceIPv4(DetectAddress *); void DetectAddressIPv4Tests(void); diff --git a/src/detect-engine-address-ipv6.c b/src/detect-engine-address-ipv6.c index f4084c131f..00fcaddd53 100644 --- a/src/detect-engine-address-ipv6.c +++ b/src/detect-engine-address-ipv6.c @@ -769,44 +769,6 @@ error: return -1; } -/** - * \brief Extends a target address range if the the source address range is - * wider than the target address range on either sides. - * - * Every address is a range, i.e. address->ip1....address->ip2. For - * example 2000::-2010:: - * if source->ip1 is smaller than target->ip1, it indicates that the - * source's left address limit is greater(range wise) than the target's - * left address limit, and hence we reassign the target's left address - * limit to source's left address limit. - * Similary if source->ip2 is greater than target->ip2, it indicates that - * the source's right address limit is greater(range wise) than the - * target's right address limit, and hence we reassign the target's right - * address limit to source's right address limit. - * - * \param de_ctx Pointer to the detection engine context. - * \param target Pointer to the target DetectAddress instance that has to be - * updated. - * \param source Pointer to the source DetectAddress instance that is used - * to decided whether we extend the target's address range. - * - * \retval 0 On success. - * \retval -1 On failure. - */ -int DetectAddressJoinIPv6(DetectEngineCtx *de_ctx, DetectAddress *target, - DetectAddress *source) -{ - if (AddressIPv6Lt(&source->ip, &target->ip)) { - COPY_ADDRESS(&source->ip, &target->ip); - } - - if (AddressIPv6Gt(&source->ip, &target->ip)) { - COPY_ADDRESS(&source->ip2, &target->ip2); - } - - return 0; -} - /***************************************Unittests******************************/ @@ -1926,156 +1888,6 @@ static int AddressTestIPv6CutNot05(void) return 0; } -static int AddressTestIPv6Join01(void) -{ - DetectAddress *source = DetectAddressInit(); - DetectAddress *target = DetectAddressInit(); - DetectAddress *temp = DetectAddressInit(); - struct in6_addr in6; - int result = 1; - - if (source == NULL || target == NULL || temp == NULL) - goto error; - - /* case 1 */ - if (inet_pton(AF_INET6, "2000::10", &in6) != 1) - goto error; - memcpy(&target->ip.address, in6.s6_addr, sizeof(in6.s6_addr)); - if (inet_pton(AF_INET6, "2000::20", &in6) != 1) - goto error; - memcpy(&target->ip2.address, in6.s6_addr, sizeof(in6.s6_addr)); - - if (inet_pton(AF_INET6, "2000::1", &in6) != 1) - goto error; - memcpy(&source->ip.address, in6.s6_addr, sizeof(in6.s6_addr)); - if (inet_pton(AF_INET6, "2000::20", &in6) != 1) - goto error; - memcpy(&source->ip2.address, in6.s6_addr, sizeof(in6.s6_addr)); - - result &= (DetectAddressJoinIPv6(NULL, target, source) == 0); - if (inet_pton(AF_INET6, "2000::1", &in6) != 1) - goto error; - memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr)); - if (inet_pton(AF_INET6, "2000::20", &in6) != 1) - goto error; - memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr)); - result = (DetectAddressCmpIPv6(target, temp) == ADDRESS_EQ); - - /* case 2 */ - if (inet_pton(AF_INET6, "2000::1", &in6) != 1) - goto error; - memcpy(&target->ip.address, in6.s6_addr, sizeof(in6.s6_addr)); - if (inet_pton(AF_INET6, "2000::20", &in6) != 1) - goto error; - memcpy(&target->ip2.address, in6.s6_addr, sizeof(in6.s6_addr)); - - if (inet_pton(AF_INET6, "2000::2", &in6) != 1) - goto error; - memcpy(&source->ip.address, in6.s6_addr, sizeof(in6.s6_addr)); - if (inet_pton(AF_INET6, "2000::19", &in6) != 1) - goto error; - memcpy(&source->ip2.address, in6.s6_addr, sizeof(in6.s6_addr)); - - result &= (DetectAddressJoinIPv6(NULL, target, source) == 0); - if (inet_pton(AF_INET6, "2000::1", &in6) != 1) - goto error; - memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr)); - if (inet_pton(AF_INET6, "2000::20", &in6) != 1) - goto error; - memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr)); - result = (DetectAddressCmpIPv6(target, temp) == ADDRESS_EQ); - - /* case 3 */ - if (inet_pton(AF_INET6, "2000::1", &in6) != 1) - goto error; - memcpy(&target->ip.address, in6.s6_addr, sizeof(in6.s6_addr)); - if (inet_pton(AF_INET6, "2000::15", &in6) != 1) - goto error; - memcpy(&target->ip2.address, in6.s6_addr, sizeof(in6.s6_addr)); - - if (inet_pton(AF_INET6, "2000::10", &in6) != 1) - goto error; - memcpy(&source->ip.address, in6.s6_addr, sizeof(in6.s6_addr)); - if (inet_pton(AF_INET6, "2000::20", &in6) != 1) - goto error; - memcpy(&source->ip2.address, in6.s6_addr, sizeof(in6.s6_addr)); - - result &= (DetectAddressJoinIPv6(NULL, target, source) == 0); - if (inet_pton(AF_INET6, "2000::1", &in6) != 1) - goto error; - memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr)); - if (inet_pton(AF_INET6, "2000::20", &in6) != 1) - goto error; - memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr)); - result = (DetectAddressCmpIPv6(target, temp) == ADDRESS_EQ); - - /* case 4 */ - if (inet_pton(AF_INET6, "2000::10", &in6) != 1) - goto error; - memcpy(&target->ip.address, in6.s6_addr, sizeof(in6.s6_addr)); - if (inet_pton(AF_INET6, "2000::20", &in6) != 1) - goto error; - memcpy(&target->ip2.address, in6.s6_addr, sizeof(in6.s6_addr)); - - if (inet_pton(AF_INET6, "2000::1", &in6) != 1) - goto error; - memcpy(&source->ip.address, in6.s6_addr, sizeof(in6.s6_addr)); - if (inet_pton(AF_INET6, "2000::20", &in6) != 1) - goto error; - memcpy(&source->ip2.address, in6.s6_addr, sizeof(in6.s6_addr)); - - result &= (DetectAddressJoinIPv6(NULL, target, source) == 0); - if (inet_pton(AF_INET6, "2000::1", &in6) != 1) - goto error; - memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr)); - if (inet_pton(AF_INET6, "2000::20", &in6) != 1) - goto error; - memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr)); - result = (DetectAddressCmpIPv6(target, temp) == ADDRESS_EQ); - - /* case 5 */ - if (inet_pton(AF_INET6, "2000::1", &in6) != 1) - goto error; - memcpy(&target->ip.address, in6.s6_addr, sizeof(in6.s6_addr)); - if (inet_pton(AF_INET6, "2000::20", &in6) != 1) - goto error; - memcpy(&target->ip2.address, in6.s6_addr, sizeof(in6.s6_addr)); - - if (inet_pton(AF_INET6, "2000::1", &in6) != 1) - goto error; - memcpy(&source->ip.address, in6.s6_addr, sizeof(in6.s6_addr)); - if (inet_pton(AF_INET6, "2000::20", &in6) != 1) - goto error; - memcpy(&source->ip2.address, in6.s6_addr, sizeof(in6.s6_addr)); - - result &= (DetectAddressJoinIPv6(NULL, target, source) == 0); - if (inet_pton(AF_INET6, "2000::1", &in6) != 1) - goto error; - memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr)); - if (inet_pton(AF_INET6, "2000::20", &in6) != 1) - goto error; - memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr)); - result = (DetectAddressCmpIPv6(target, temp) == ADDRESS_EQ); - - if (source != NULL) - DetectAddressFree(source); - if (target != NULL) - DetectAddressFree(target); - if (temp != NULL) - DetectAddressFree(temp); - return result; - - error: - if (source != NULL) - DetectAddressFree(source); - if (target != NULL) - DetectAddressFree(target); - if (temp != NULL) - DetectAddressFree(temp); - - return 0; -} - #endif /* UNITTESTS */ void DetectAddressIPv6Tests(void) @@ -2122,8 +1934,6 @@ void DetectAddressIPv6Tests(void) UtRegisterTest("AddressTestIPv6CutNot03", AddressTestIPv6CutNot03); UtRegisterTest("AddressTestIPv6CutNot04", AddressTestIPv6CutNot04); UtRegisterTest("AddressTestIPv6CutNot05", AddressTestIPv6CutNot05); - - UtRegisterTest("AddressTestIPv6Join01", AddressTestIPv6Join01); #endif /* UNITTESTS */ return; diff --git a/src/detect-engine-address-ipv6.h b/src/detect-engine-address-ipv6.h index 2556e55b21..b8c573d0ce 100644 --- a/src/detect-engine-address-ipv6.h +++ b/src/detect-engine-address-ipv6.h @@ -41,7 +41,6 @@ int DetectAddressCmpIPv6(DetectAddress *a, DetectAddress *b); int DetectAddressCutIPv6(DetectEngineCtx *, DetectAddress *, DetectAddress *, DetectAddress **); -int DetectAddressJoinIPv6(DetectEngineCtx *, DetectAddress *, DetectAddress *); void DetectAddressIPv6Tests(void); diff --git a/src/detect-engine-address.c b/src/detect-engine-address.c index 265b0bec33..4d66d6a537 100644 --- a/src/detect-engine-address.c +++ b/src/detect-engine-address.c @@ -45,7 +45,11 @@ #include "util-var.h" /* prototypes */ -void DetectAddressPrint(DetectAddress *); +#ifdef DEBUG +static void DetectAddressPrint(DetectAddress *); +#else +#define DetectAddressPrint(...) +#endif static int DetectAddressCutNot(DetectAddress *, DetectAddress **); static int DetectAddressCut(DetectEngineCtx *, DetectAddress *, DetectAddress *, DetectAddress **); @@ -59,11 +63,9 @@ int DetectAddressMergeNot(DetectAddressHead *gh, DetectAddressHead *ghn); */ DetectAddress *DetectAddressInit(void) { - DetectAddress *ag = SCMalloc(sizeof(DetectAddress)); + DetectAddress *ag = SCCalloc(1, sizeof(DetectAddress)); if (unlikely(ag == NULL)) return NULL; - memset(ag, 0, sizeof(DetectAddress)); - return ag; } @@ -82,8 +84,35 @@ void DetectAddressFree(DetectAddress *ag) } /** - * \brief Copies the contents of one Address group in DetectAddress and returns - * a new instance of the DetectAddress that contains the copied address. + * \internal + * \brief Returns a new instance of DetectAddressHead. + * + * \retval gh Pointer to the new instance of DetectAddressHead. + */ +static DetectAddressHead *DetectAddressHeadInit(void) +{ + DetectAddressHead *gh = SCCalloc(1, sizeof(DetectAddressHead)); + if (unlikely(gh == NULL)) + return NULL; + return gh; +} + +/** + * \internal + * \brief Frees a DetectAddressHead instance. + * + * \param gh Pointer to the DetectAddressHead instance to be freed. + */ +static void DetectAddressHeadFree(DetectAddressHead *gh) +{ + if (gh != NULL) { + DetectAddressHeadCleanup(gh); + SCFree(gh); + } +} + +/** + * \brief copy a DetectAddress * * \param orig Pointer to the instance of DetectAddress that contains the * address data to be copied to the new instance. @@ -91,47 +120,19 @@ void DetectAddressFree(DetectAddress *ag) * \retval ag Pointer to the new instance of DetectAddress that contains the * copied address. */ -DetectAddress *DetectAddressCopy(DetectAddress *orig) +static DetectAddress *DetectAddressCopy(DetectAddress *orig) { DetectAddress *ag = DetectAddressInit(); if (ag == NULL) return NULL; ag->flags = orig->flags; - COPY_ADDRESS(&orig->ip, &ag->ip); COPY_ADDRESS(&orig->ip2, &ag->ip2); - return ag; } -/** - * \brief Used to check if a DetectAddress list contains an instance with - * a similar DetectAddress. The comparison done is not the one that - * checks the memory for the same instance, but one that checks that the - * two instances hold the same content. - * - * \param head Pointer to the DetectAddress list. - * \param ad Pointer to the DetectAddress that has to be checked for in - * the DetectAddress list. - * - * \retval cur Returns a pointer to the DetectAddress on a match; NULL if - * no match. - */ -DetectAddress *DetectAddressLookupInList(DetectAddress *head, DetectAddress *gr) -{ - DetectAddress *cur; - - if (head != NULL) { - for (cur = head; cur != NULL; cur = cur->next) { - if (DetectAddressCmp(cur, gr) == ADDRESS_EQ) - return cur; - } - } - - return NULL; -} - +#ifdef DEBUG /** * \brief Prints the address data information for all the DetectAddress * instances in the DetectAddress list sent as the argument. @@ -140,86 +141,28 @@ DetectAddress *DetectAddressLookupInList(DetectAddress *head, DetectAddress *gr) */ void DetectAddressPrintList(DetectAddress *head) { - DetectAddress *cur; - SCLogInfo("list:"); - if (head != NULL) { - for (cur = head; cur != NULL; cur = cur->next) { - DetectAddressPrint(cur); - } + for (DetectAddress *cur = head; cur != NULL; cur = cur->next) { + DetectAddressPrint(cur); } SCLogInfo("endlist"); - - return; } +#endif /** + * \internal * \brief Frees a list of DetectAddress instances. * * \param head Pointer to a list of DetectAddress instances to be freed. */ -void DetectAddressCleanupList(DetectAddress *head) +static void DetectAddressCleanupList(DetectAddress *head) { - DetectAddress *cur, *next; - - if (head == NULL) - return; - - for (cur = head; cur != NULL; ) { - next = cur->next; + for (DetectAddress *cur = head; cur != NULL; ) { + DetectAddress *next = cur->next; cur->next = NULL; DetectAddressFree(cur); cur = next; } - - return; -} - -/** - * \brief Do a sorted insert, where the top of the list should be the biggest - * network/range. - * - * XXX current sorting only works for overlapping nets - * - * \param head Pointer to the list of DetectAddress. - * \param ag Pointer to the DetectAddress that has to be added to the - * above list. - * - * \retval 0 On successfully inserting the DetectAddress. - * \retval -1 On failure. - */ - -int DetectAddressAdd(DetectAddress **head, DetectAddress *ag) -{ - DetectAddress *cur, *prev_cur = NULL; - int r = 0; - - if (*head != NULL) { - for (cur = *head; cur != NULL; cur = cur->next) { - prev_cur = cur; - r = DetectAddressCmp(ag, cur); - if (r == ADDRESS_EB) { - /* insert here */ - ag->prev = cur->prev; - ag->next = cur; - - cur->prev = ag; - if (*head == cur) - *head = ag; - else - ag->prev->next = ag; - - return 0; - } - } - ag->prev = prev_cur; - if (prev_cur != NULL) - prev_cur->next = ag; - } else { - *head = ag; - } - - return 0; } /** @@ -272,9 +215,8 @@ static DetectAddress *GetHeadPtr(DetectAddressHead *gh, DetectAddress *new) } /** - * \brief Same as DetectAddressInsert, but then for inserting a address group - * object. This also makes sure SigGroupContainer lists are handled - * correctly. + * \internal + * \brief insert DetectAddress into a DetectAddressHead * * \param de_ctx Pointer to the detection engine context. * \param gh Pointer to the DetectAddressHead list to which it has to @@ -285,7 +227,7 @@ static DetectAddress *GetHeadPtr(DetectAddressHead *gh, DetectAddress *new) * \retval -1 On error. * \retval 0 Not inserted, memory of new is freed. */ -int DetectAddressInsert(DetectEngineCtx *de_ctx, DetectAddressHead *gh, +static int DetectAddressInsert(DetectEngineCtx *de_ctx, DetectAddressHead *gh, DetectAddress *new) { DetectAddress *head = NULL; @@ -408,33 +350,6 @@ error: return -1; } -/** - * \brief Join two addresses groups together. - * - * \param de_ctx Pointer to the detection engine context. - * \param target Pointer to the target address group. - * \param source Pointer to the source address group. - * - * \retval 0 On success. - * \retval -1 On failure. - */ -int DetectAddressJoin(DetectEngineCtx *de_ctx, DetectAddress *target, - DetectAddress *source) -{ - if (target == NULL || source == NULL) - return -1; - - if (target->ip.family != source->ip.family) - return -1; - - if (target->ip.family == AF_INET) - return DetectAddressJoinIPv4(de_ctx, target, source); - else if (target->ip.family == AF_INET6) - return DetectAddressJoinIPv6(de_ctx, target, source); - - return -1; -} - /** * \brief Checks if two address group lists are equal. * @@ -517,7 +432,7 @@ static void DetectAddressParseIPv6CIDR(int cidr, struct in6_addr *in6) * \retval 0 On successfully parsing the address string. * \retval -1 On failure. */ -int DetectAddressParseString(DetectAddress *dd, const char *str) +static int DetectAddressParseString(DetectAddress *dd, const char *str) { char *ip = NULL; char *ip2 = NULL; @@ -693,25 +608,19 @@ error: */ static DetectAddress *DetectAddressParseSingle(const char *str) { - DetectAddress *dd; - SCLogDebug("str %s", str); - dd = DetectAddressInit(); + DetectAddress *dd = DetectAddressInit(); if (dd == NULL) - goto error; + return NULL; if (DetectAddressParseString(dd, str) < 0) { SCLogDebug("AddressParse failed"); - goto error; + DetectAddressFree(dd); + return NULL; } return dd; - -error: - if (dd != NULL) - DetectAddressFree(dd); - return NULL; } /** @@ -1487,26 +1396,24 @@ static const DetectAddressMap *DetectAddressMapLookup(DetectEngineCtx *de_ctx, int DetectAddressParse(const DetectEngineCtx *de_ctx, DetectAddressHead *gh, const char *str) { - int r; - DetectAddressHead *ghn = NULL; - SCLogDebug("gh %p, str %s", gh, str); if (str == NULL) { SCLogDebug("DetectAddressParse can not be run with NULL address"); - goto error; + return -1; } - ghn = DetectAddressHeadInit(); + DetectAddressHead *ghn = DetectAddressHeadInit(); if (ghn == NULL) { SCLogDebug("DetectAddressHeadInit for ghn failed"); - goto error; + return -1; } - r = DetectAddressParse2(de_ctx, gh, ghn, str, /* start with negate no */0, NULL); + int r = DetectAddressParse2(de_ctx, gh, ghn, str, /* start with negate no */0, NULL); if (r < 0) { SCLogDebug("DetectAddressParse2 returned %d", r); - goto error; + DetectAddressHeadFree(ghn); + return -1; } SCLogDebug("gh->ipv4_head %p, ghn->ipv4_head %p", gh->ipv4_head, @@ -1517,17 +1424,13 @@ int DetectAddressParse(const DetectEngineCtx *de_ctx, /* merge the 'not' address groups */ if (DetectAddressMergeNot(gh, ghn) < 0) { SCLogDebug("DetectAddressMergeNot failed"); - goto error; + DetectAddressHeadFree(ghn); + return -1; } /* free the temp negate head */ DetectAddressHeadFree(ghn); return contains_negation ? 1 : 0; - -error: - if (ghn != NULL) - DetectAddressHeadFree(ghn); - return -1; } const DetectAddressHead *DetectParseAddress(DetectEngineCtx *de_ctx, @@ -1561,21 +1464,6 @@ const DetectAddressHead *DetectParseAddress(DetectEngineCtx *de_ctx, return head; } -/** - * \brief Returns a new instance of DetectAddressHead. - * - * \retval gh Pointer to the new instance of DetectAddressHead. - */ -DetectAddressHead *DetectAddressHeadInit(void) -{ - DetectAddressHead *gh = SCMalloc(sizeof(DetectAddressHead)); - if (unlikely(gh == NULL)) - return NULL; - memset(gh, 0, sizeof(DetectAddressHead)); - - return gh; -} - /** * \brief Cleans a DetectAddressHead. The functions frees the address * group heads(ipv4 and ipv6) inside the DetectAddressHead @@ -1600,21 +1488,6 @@ void DetectAddressHeadCleanup(DetectAddressHead *gh) return; } -/** - * \brief Frees a DetectAddressHead instance. - * - * \param gh Pointer to the DetectAddressHead instance to be freed. - */ -void DetectAddressHeadFree(DetectAddressHead *gh) -{ - if (gh != NULL) { - DetectAddressHeadCleanup(gh); - SCFree(gh); - } - - return; -} - /** * \brief Dispatcher function that calls the ipv4 and ipv6 address cut functions. * Have a look at DetectAddressCutIPv4() and DetectAddressCutIPv6() for @@ -1723,8 +1596,7 @@ int DetectAddressMatchIPv4(const DetectMatchAddressIPv4 *addrs, SCReturnInt(0); } - uint16_t idx; - for (idx = 0; idx < addrs_cnt; idx++) { + for (uint16_t idx = 0; idx < addrs_cnt; idx++) { if (SCNtohl(a->addr_data32[0]) >= addrs[idx].ip && SCNtohl(a->addr_data32[0]) <= addrs[idx].ip2) { @@ -1758,15 +1630,11 @@ int DetectAddressMatchIPv6(const DetectMatchAddressIPv6 *addrs, SCReturnInt(0); } - uint16_t idx; - int i = 0; - uint16_t result1, result2; - /* See if the packet address is within the range of any entry in the * signature's address match array. */ - for (idx = 0; idx < addrs_cnt; idx++) { - result1 = result2 = 0; + for (uint16_t idx = 0; idx < addrs_cnt; idx++) { + uint16_t result1 = 0, result2 = 0; /* See if packet address equals either limit. Return 1 if true. */ if (SCNtohl(a->addr_data32[0]) == addrs[idx].ip[0] && @@ -1787,7 +1655,7 @@ int DetectAddressMatchIPv6(const DetectMatchAddressIPv6 *addrs, /* See if packet address is greater than lower limit * of the current signature address match pair. */ - for (i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { if (SCNtohl(a->addr_data32[i]) > addrs[idx].ip[i]) { result1 = 1; break; @@ -1805,7 +1673,7 @@ int DetectAddressMatchIPv6(const DetectMatchAddressIPv6 *addrs, /* See if packet address is less than upper limit * of the current signature address match pair. */ - for (i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { if (SCNtohl(a->addr_data32[i]) < addrs[idx].ip2[i]) { result2 = 1; break; @@ -1839,7 +1707,7 @@ int DetectAddressMatchIPv6(const DetectMatchAddressIPv6 *addrs, * \param 1 On a match. * \param 0 On no match. */ -int DetectAddressMatch(DetectAddress *dd, Address *a) +static int DetectAddressMatch(DetectAddress *dd, Address *a) { SCEnter(); @@ -1882,6 +1750,7 @@ int DetectAddressMatch(DetectAddress *dd, Address *a) SCReturnInt(0); } +#ifdef DEBUG /** * \brief Prints the address data held by the DetectAddress. If the address * data family is IPv4, we print the the ipv4 address and mask, and @@ -1890,7 +1759,7 @@ int DetectAddressMatch(DetectAddress *dd, Address *a) * * \param ad Pointer to the DetectAddress instance to be printed. */ -void DetectAddressPrint(DetectAddress *gr) +static void DetectAddressPrint(DetectAddress *gr) { if (gr == NULL) return; @@ -1921,6 +1790,7 @@ void DetectAddressPrint(DetectAddress *gr) return; } +#endif /** * \brief Find the group matching address in a group head. diff --git a/src/detect-engine-address.h b/src/detect-engine-address.h index ce6e8c79ba..c137f2b0f2 100644 --- a/src/detect-engine-address.h +++ b/src/detect-engine-address.h @@ -24,34 +24,17 @@ #ifndef __DETECT_ADDRESS_H__ #define __DETECT_ADDRESS_H__ -/* prototypes */ -void DetectAddressRegister (void); -DetectAddressHead *DetectAddressHeadInit(void); -void DetectAddressHeadFree(DetectAddressHead *); -void DetectAddressHeadCleanup(DetectAddressHead *); - -int DetectAddressParseString(DetectAddress *, const char *); -int DetectAddressParse(const DetectEngineCtx *, DetectAddressHead *, const char *); DetectAddress *DetectAddressInit(void); void DetectAddressFree(DetectAddress *); - -void DetectAddressCleanupList (DetectAddress *); -int DetectAddressAdd(DetectAddress **, DetectAddress *); -void DetectAddressPrintList(DetectAddress *); - -int DetectAddressInsert(DetectEngineCtx *, DetectAddressHead *, DetectAddress *); -int DetectAddressJoin(DetectEngineCtx *, DetectAddress *, DetectAddress *); +int DetectAddressParse(const DetectEngineCtx *, DetectAddressHead *, const char *); +void DetectAddressHeadCleanup(DetectAddressHead *); bool DetectAddressListsAreEqual(DetectAddress *list1, DetectAddress *list2); DetectAddress *DetectAddressLookupInHead(const DetectAddressHead *, Address *); -DetectAddress *DetectAddressLookupInList(DetectAddress *, DetectAddress *); -int DetectAddressMatch(DetectAddress *, Address *); -DetectAddress *DetectAddressCopy(DetectAddress *); -void DetectAddressPrint(DetectAddress *); int DetectAddressCmp(DetectAddress *, DetectAddress *); int DetectAddressMatchIPv4(const DetectMatchAddressIPv4 *, uint16_t, const Address *); @@ -66,4 +49,8 @@ void DetectAddressMapFree(DetectEngineCtx *de_ctx); const DetectAddressHead *DetectParseAddress(DetectEngineCtx *de_ctx, const char *string, bool *contains_negation); +#ifdef DEBUG +void DetectAddressPrintList(DetectAddress *); +#endif + #endif /* __DETECT_ADDRESS_H__ */