detect: refactoring parsing of ip range

To optimize first netmask
pull/4886/head
Philippe Antoine 5 years ago committed by Victor Julien
parent 8ca9c0e8f0
commit ae102ca096

@ -221,39 +221,39 @@ static int IPOnlyCIDRItemParseSingle(IPOnlyCIDRItem **pdd, const char *str)
if (first > last) if (first > last)
goto error; goto error;
SCLogDebug("Creating CIDR range for [%s - %s]", ip, ip2);
dd->netmask = 32; dd->netmask = 32;
dd->ip[0] =htonl(first); /* Find the maximum netmask starting from current address first
* and not crossing last.
if (first < last) { * To extend the mask, we need to start from a power of 2.
SCLogDebug("Creating CIDR range for [%s - %s]", ip, ip2); * And we need to pay attention to unsigned overflow back to 0.0.0.0
while ( first <= last) { */
IPOnlyCIDRItem *new = IPOnlyCIDRItemNew(); while (dd->netmask > 0 &&
if (new == NULL) (first & (1UL << (32-dd->netmask))) == 0 &&
goto error; first + (1UL << (32-(dd->netmask-1))) - 1 <= last) {
new->negated = dd->negated; dd->netmask--;
new->family= dd->family; }
new->netmask = 32; dd->ip[0] = htonl(first);
/* Find the maximum netmask starting from current address first first += 1UL << (32-dd->netmask);
* and not crossing last. //case whatever-255.255.255.255 looping to 0.0.0.0/0
* To extend the mask, we need to start from a power of 2. while ( first <= last && first != 0 ) {
* And we need to pay attention to unsigned overflow back to 0.0.0.0 IPOnlyCIDRItem *new = IPOnlyCIDRItemNew();
*/ if (new == NULL)
while (new->netmask > 0 && goto error;
(first & (1UL << (32-new->netmask))) == 0 && new->negated = dd->negated;
first + (1UL << (32-(new->netmask-1))) - 1 <= last) { new->family= dd->family;
new->netmask--; new->netmask = 32;
} while (new->netmask > 0 &&
new->ip[0] = htonl(first); (first & (1UL << (32-new->netmask))) == 0 &&
dd = IPOnlyCIDRItemInsert(dd, new); first + (1UL << (32-(new->netmask-1))) - 1 <= last) {
first += 1UL << (32-new->netmask); new->netmask--;
if (first == 0) {
//case whatever-255.255.255.255 looping to 0.0.0.0/0
break;
}
} }
//update head of list new->ip[0] = htonl(first);
*pdd = dd; first += 1UL << (32-new->netmask);
dd = IPOnlyCIDRItemInsert(dd, new);
} }
//update head of list
*pdd = dd;
} else { } else {
/* 1.2.3.4 format */ /* 1.2.3.4 format */

Loading…
Cancel
Save