Code cleanup

This commit is contained in:
Luca Deri
2019-12-09 15:28:44 +01:00
parent 56eeb965bf
commit 65d526d8f6
7 changed files with 32 additions and 30 deletions
+1 -1
View File
@@ -45,4 +45,4 @@ cat configure | sed "s/#define PACKAGE/#define NDPI_PACKAGE/g" | sed "s/#define
cat configure.tmp > configure cat configure.tmp > configure
chmod +x configure chmod +x configure
./configure $* ./configure "$*"
+5 -5
View File
@@ -2164,7 +2164,7 @@ static void printFlowsStats() {
num_ja3_server = HASH_COUNT(ja3ByHost_element->host_server_info_hasht); num_ja3_server = HASH_COUNT(ja3ByHost_element->host_server_info_hasht);
if(num_ja3_client > 0) { if(num_ja3_client > 0) {
fprintf(out, "\t%d\t %-24s \t %-7d\n", fprintf(out, "\t%d\t %-24s \t %-7u\n",
i, i,
ja3ByHost_element->ip_string, ja3ByHost_element->ip_string,
num_ja3_client num_ja3_client
@@ -2985,9 +2985,9 @@ void test_lib() {
/* *********************************************** */ /* *********************************************** */
void automataUnitTest() { void automataUnitTest() {
void *automa; void *automa = ndpi_init_automa();
assert((automa = ndpi_init_automa())); assert(automa);
assert(ndpi_add_string_to_automa(automa, "hello") == 0); assert(ndpi_add_string_to_automa(automa, "hello") == 0);
assert(ndpi_add_string_to_automa(automa, "world") == 0); assert(ndpi_add_string_to_automa(automa, "world") == 0);
ndpi_finalize_automa(automa); ndpi_finalize_automa(automa);
@@ -3008,8 +3008,8 @@ void serializerUnitTest() {
char kbuf[32], vbuf[32]; char kbuf[32], vbuf[32];
assert(ndpi_serialize_uint32_uint32(&serializer, i, i*i) != -1); assert(ndpi_serialize_uint32_uint32(&serializer, i, i*i) != -1);
snprintf(kbuf, sizeof(kbuf), "Hello %u", i); snprintf(kbuf, sizeof(kbuf), "Hello %d", i);
snprintf(vbuf, sizeof(vbuf), "World %u", i); snprintf(vbuf, sizeof(vbuf), "World %d", i);
assert(ndpi_serialize_uint32_string(&serializer, i, "Hello") != -1); assert(ndpi_serialize_uint32_string(&serializer, i, "Hello") != -1);
assert(ndpi_serialize_string_string(&serializer, kbuf, vbuf) != -1); assert(ndpi_serialize_string_string(&serializer, kbuf, vbuf) != -1);
assert(ndpi_serialize_string_uint32(&serializer, kbuf, i*i) != -1); assert(ndpi_serialize_string_uint32(&serializer, kbuf, i*i) != -1);
+16 -15
View File
@@ -439,7 +439,8 @@ static int ndpi_string_to_automa(struct ndpi_detection_module_struct *ndpi_str,
ndpi_protocol_breed_t breed) { ndpi_protocol_breed_t breed) {
AC_PATTERN_t ac_pattern; AC_PATTERN_t ac_pattern;
if(protocol_id >= (NDPI_MAX_SUPPORTED_PROTOCOLS+NDPI_MAX_NUM_CUSTOM_PROTOCOLS)) { if((value == NULL)
|| (protocol_id >= (NDPI_MAX_SUPPORTED_PROTOCOLS+NDPI_MAX_NUM_CUSTOM_PROTOCOLS))) {
NDPI_LOG_ERR(ndpi_str, "[NDPI] protoId=%d: INTERNAL ERROR\n", protocol_id); NDPI_LOG_ERR(ndpi_str, "[NDPI] protoId=%d: INTERNAL ERROR\n", protocol_id);
return(-1); return(-1);
} }
@@ -1800,7 +1801,7 @@ static void ndpi_init_protocol_defaults(struct ndpi_detection_module_struct *ndp
static int ac_match_handler(AC_MATCH_t *m, AC_TEXT_t *txt, AC_REP_t *match) { static int ac_match_handler(AC_MATCH_t *m, AC_TEXT_t *txt, AC_REP_t *match) {
int min_len = (txt->length < m->patterns->length) ? txt->length : m->patterns->length; int min_len = (txt->length < m->patterns->length) ? txt->length : m->patterns->length;
char buf[64] = { '\0' }; char buf[64] = { '\0' }, *whatfound;
int min_buf_len = (txt->length > 63 /* sizeof(buf)-1 */) ? 63 : txt->length; int min_buf_len = (txt->length > 63 /* sizeof(buf)-1 */) ? 63 : txt->length;
u_int buf_len = strlen(buf); u_int buf_len = strlen(buf);
@@ -1809,27 +1810,26 @@ static int ac_match_handler(AC_MATCH_t *m, AC_TEXT_t *txt, AC_REP_t *match) {
#ifdef MATCH_DEBUG #ifdef MATCH_DEBUG
printf("Searching [to search: %s/%u][pattern: %s/%u] [len: %d][match_num: %u][%s]\n", printf("Searching [to search: %s/%u][pattern: %s/%u] [len: %d][match_num: %u][%s]\n",
buf, (unigned int)txt->length, m->patterns->astring, m->patterns->length, min_len, buf, (unigned int)txt->length, m->patterns->astring, (unigned int)m->patterns->length,
m->match_num, m->patterns->astring); min_len, m->match_num, m->patterns->astring);
#endif #endif
{ whatfound = strstr(buf, m->patterns->astring);
char *whatfound = strstr(buf, m->patterns->astring);
#ifdef MATCH_DEBUG #ifdef MATCH_DEBUG
printf("[NDPI] %s() [searching=%s][pattern=%s][%s][%c]\n", printf("[NDPI] %s() [searching=%s][pattern=%s][%s][%c]\n",
__FUNCTION__, buf, m->patterns->astring, __FUNCTION__, buf, m->patterns->astring,
whatfound ? whatfound : "<NULL>", whatfound ? whatfound : "<NULL>",
whatfound[-1]); whatfound[-1]);
#endif #endif
if(whatfound) {
/* /*
The patch below allows in case of pattern ws.amazon.com The patch below allows in case of pattern ws.amazon.com
to avoid matching aws.amazon.com whereas a.ws.amazon.com to avoid matching aws.amazon.com whereas a.ws.amazon.com
has to match has to match
*/ */
if(whatfound if((whatfound != buf)
&& (whatfound != buf)
&& (m->patterns->astring[0] != '.') /* The searched pattern does not start with . */ && (m->patterns->astring[0] != '.') /* The searched pattern does not start with . */
&& strchr(m->patterns->astring, '.') /* The matched pattern has a . (e.g. numeric or sym IPs) */) { && strchr(m->patterns->astring, '.') /* The matched pattern has a . (e.g. numeric or sym IPs) */) {
if(whatfound[-1] != '.') { if(whatfound[-1] != '.') {
@@ -1968,7 +1968,7 @@ int ndpi_load_ipv4_ptree(struct ndpi_detection_module_struct *ndpi_str,
return(-1); return(-1);
} }
while(fd) { while(1) {
line = fgets(buffer, sizeof(buffer), fd); line = fgets(buffer, sizeof(buffer), fd);
if(line == NULL) if(line == NULL)
@@ -2851,7 +2851,7 @@ int ndpi_load_categories_file(struct ndpi_detection_module_struct *ndpi_str, con
return(-1); return(-1);
} }
while(fd) { while(1) {
line = fgets(buffer, sizeof(buffer), fd); line = fgets(buffer, sizeof(buffer), fd);
if(line == NULL) if(line == NULL)
@@ -3658,7 +3658,8 @@ static u_int8_t ndpi_detection_get_l4_internal(struct ndpi_detection_module_stru
if(l3 == NULL || l3_len < sizeof(struct ndpi_iphdr)) if(l3 == NULL || l3_len < sizeof(struct ndpi_iphdr))
return(1); return(1);
iph = (const struct ndpi_iphdr *) l3; if((iph = (const struct ndpi_iphdr *) l3) == NULL)
return(1);
if(iph->version == IPVERSION && iph->ihl >= 5) { if(iph->version == IPVERSION && iph->ihl >= 5) {
NDPI_LOG_DBG2(ndpi_str, "ipv4 header\n"); NDPI_LOG_DBG2(ndpi_str, "ipv4 header\n");
+1 -1
View File
@@ -1134,7 +1134,7 @@ static int ndpi_url_decode(const char *s, char *out) {
if(c == '+') c = ' '; if(c == '+') c = ' ';
else if(c == '%' && (!ishex(*s++)|| else if(c == '%' && (!ishex(*s++)||
!ishex(*s++)|| !ishex(*s++)||
!sscanf(s - 2, "%2x", &c))) !sscanf(s - 2, "%2x", (unsigned int*)&c)))
return(-1); return(-1);
if(out) *o = c; if(out) *o = c;
+2 -1
View File
@@ -107,7 +107,8 @@ void ndpi_search_dhcp_udp(struct ndpi_detection_module_struct *ndpi_struct, stru
#if 1 #if 1
offset += snprintf((char*)&flow->protos.dhcp.fingerprint[offset], offset += snprintf((char*)&flow->protos.dhcp.fingerprint[offset],
sizeof(flow->protos.dhcp.fingerprint) - offset, sizeof(flow->protos.dhcp.fingerprint) - offset,
"%s%u", (idx > 0) ? "," : "", dhcp->options[i+2+idx] & 0xFF); "%s%u", (idx > 0) ? "," : "",
(unsigned int)dhcp->options[i+2+idx] & 0xFF);
#else #else
offset += snprintf((char*)&flow->protos.dhcp.fingerprint[offset], offset += snprintf((char*)&flow->protos.dhcp.fingerprint[offset],
sizeof(flow->protos.dhcp.fingerprint) - offset, sizeof(flow->protos.dhcp.fingerprint) - offset,
+2 -2
View File
@@ -186,12 +186,12 @@ void ndpi_search_telnet_tcp(struct ndpi_detection_module_struct *ndpi_struct,
if(((flow->packet_counter < 12) && (flow->l4.tcp.telnet_stage > 0)) || (flow->packet_counter < 6)) { if(((flow->packet_counter < 12) && (flow->l4.tcp.telnet_stage > 0)) || (flow->packet_counter < 6)) {
#ifdef TELNET_DEBUG #ifdef TELNET_DEBUG
printf("==> [%s:%u] %s()\n", __FILE__, __LINE__, __FUNCTION__); printf("==> [%s:%d] %s()\n", __FILE__, __LINE__, __FUNCTION__);
#endif #endif
return; return;
} else { } else {
#ifdef TELNET_DEBUG #ifdef TELNET_DEBUG
printf("==> [%s:%u] %s()\n", __FILE__, __LINE__, __FUNCTION__); printf("==> [%s:%d] %s()\n", __FILE__, __LINE__, __FUNCTION__);
#endif #endif
NDPI_EXCLUDE_PROTO(ndpi_struct, flow); NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
} }
+1 -1
View File
@@ -525,7 +525,7 @@ static size_t parse_slash(struct libinjection_sqli_state * sf)
* are an automatic black ban! * are an automatic black ban!
*/ */
if (memchr2(cur + 2, (size_t)(ptr - (cur + 1)), '/', '*') != NULL) { if(ptr && (memchr2(cur + 2, (size_t)(ptr - (cur + 1)), '/', '*') != NULL)) {
ctype = TYPE_EVIL; ctype = TYPE_EVIL;
} else if (is_mysql_comment(cs, slen, pos)) { } else if (is_mysql_comment(cs, slen, pos)) {
ctype = TYPE_EVIL; ctype = TYPE_EVIL;