Fixed partial string matches
This commit is contained in:
@@ -3462,8 +3462,7 @@ void automataUnitTest() {
|
|||||||
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);
|
||||||
assert(ndpi_match_string(automa, "This is the wonderful world of nDPI") == 0);
|
assert(ndpi_match_string(automa, "This is the wonderful world of nDPI") == 1);
|
||||||
|
|
||||||
ndpi_free_automa(automa);
|
ndpi_free_automa(automa);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+47
-22
@@ -455,7 +455,7 @@ static int ndpi_string_to_automa(struct ndpi_detection_module_struct *ndpi_struc
|
|||||||
/*
|
/*
|
||||||
TODO
|
TODO
|
||||||
This function should free the memory during program termination
|
This function should free the memory during program termination
|
||||||
|
|
||||||
*/
|
*/
|
||||||
static void ndpi_free_memory_at_termination(struct ndpi_detection_module_struct *ndpi_struct, char *buf) {
|
static void ndpi_free_memory_at_termination(struct ndpi_detection_module_struct *ndpi_struct, char *buf) {
|
||||||
|
|
||||||
@@ -471,7 +471,7 @@ static int ndpi_add_host_url_subprotocol(struct ndpi_detection_module_struct *nd
|
|||||||
char *value = ndpi_strdup(_value);
|
char *value = ndpi_strdup(_value);
|
||||||
|
|
||||||
if(!value) return(-1); else ndpi_free_memory_at_termination(ndpi_struct, value);
|
if(!value) return(-1); else ndpi_free_memory_at_termination(ndpi_struct, value);
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
NDPI_LOG_DEBUG2(ndpi_struct, "[NDPI] Adding [%s][%d]\n", value, protocol_id);
|
NDPI_LOG_DEBUG2(ndpi_struct, "[NDPI] Adding [%s][%d]\n", value, protocol_id);
|
||||||
#endif
|
#endif
|
||||||
@@ -1794,10 +1794,14 @@ static int ac_match_handler(AC_MATCH_t *m, AC_TEXT_t *txt, AC_REP_t *match) {
|
|||||||
if(whatfound
|
if(whatfound
|
||||||
&& (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) */) {
|
||||||
&& (whatfound[-1] != '.')
|
if(whatfound[-1] != '.') {
|
||||||
)
|
return(0);
|
||||||
return(0);
|
} else {
|
||||||
|
memcpy(match, &m->patterns[0].rep, sizeof(AC_REP_t)); /* Partial match? */
|
||||||
|
return(0); /* Keep searching as probably there is a better match */
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -2215,6 +2219,13 @@ int ndpi_match_string(void *_automa, char *string_to_match) {
|
|||||||
rc = ac_automata_search(automa, &ac_input_text, &match);
|
rc = ac_automata_search(automa, &ac_input_text, &match);
|
||||||
ac_automata_reset(automa);
|
ac_automata_reset(automa);
|
||||||
|
|
||||||
|
/*
|
||||||
|
As ac_automata_search can detect partial matches and continue the search process
|
||||||
|
in case rc == 0 (i.e. no match), we need to check if there is a partial match
|
||||||
|
and in this case return it
|
||||||
|
*/
|
||||||
|
if((rc == 0) && (match.number != 0)) rc = 1;
|
||||||
|
|
||||||
return(rc ? match.number : 0);
|
return(rc ? match.number : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2236,6 +2247,13 @@ int ndpi_match_string_id(void *_automa, char *string_to_match, unsigned long *id
|
|||||||
rc = ac_automata_search(automa, &ac_input_text, &match);
|
rc = ac_automata_search(automa, &ac_input_text, &match);
|
||||||
ac_automata_reset(automa);
|
ac_automata_reset(automa);
|
||||||
|
|
||||||
|
/*
|
||||||
|
As ac_automata_search can detect partial matches and continue the search process
|
||||||
|
in case rc == 0 (i.e. no match), we need to check if there is a partial match
|
||||||
|
and in this case return it
|
||||||
|
*/
|
||||||
|
if((rc == 0) && (match.number != 0)) rc = 1;
|
||||||
|
|
||||||
*id = rc ? match.number : NDPI_PROTOCOL_UNKNOWN;
|
*id = rc ? match.number : NDPI_PROTOCOL_UNKNOWN;
|
||||||
|
|
||||||
return(*id != NDPI_PROTOCOL_UNKNOWN ? 0 : -1);
|
return(*id != NDPI_PROTOCOL_UNKNOWN ? 0 : -1);
|
||||||
@@ -3543,7 +3561,7 @@ static int ndpi_init_packet_header(struct ndpi_detection_module_struct *ndpi_str
|
|||||||
u_int8_t l4_result;
|
u_int8_t l4_result;
|
||||||
|
|
||||||
if (!flow)
|
if (!flow)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
/* reset payload_packet_len, will be set if ipv4 tcp or udp */
|
/* reset payload_packet_len, will be set if ipv4 tcp or udp */
|
||||||
flow->packet.payload_packet_len = 0;
|
flow->packet.payload_packet_len = 0;
|
||||||
@@ -3606,7 +3624,7 @@ static int ndpi_init_packet_header(struct ndpi_detection_module_struct *ndpi_str
|
|||||||
flow->packet.l4_protocol = l4protocol;
|
flow->packet.l4_protocol = l4protocol;
|
||||||
flow->packet.l4_packet_len = l4len;
|
flow->packet.l4_packet_len = l4len;
|
||||||
flow->l4_proto = l4protocol;
|
flow->l4_proto = l4protocol;
|
||||||
|
|
||||||
/* tcp / udp detection */
|
/* tcp / udp detection */
|
||||||
if(l4protocol == IPPROTO_TCP && flow->packet.l4_packet_len >= 20 /* min size of tcp */ ) {
|
if(l4protocol == IPPROTO_TCP && flow->packet.l4_packet_len >= 20 /* min size of tcp */ ) {
|
||||||
/* tcp */
|
/* tcp */
|
||||||
@@ -3860,7 +3878,7 @@ void check_ndpi_udp_flow_func(struct ndpi_detection_module_struct *ndpi_struct,
|
|||||||
&& NDPI_BITMASK_COMPARE(ndpi_struct->callback_buffer_udp[a].detection_bitmask,
|
&& NDPI_BITMASK_COMPARE(ndpi_struct->callback_buffer_udp[a].detection_bitmask,
|
||||||
detection_bitmask) != 0) {
|
detection_bitmask) != 0) {
|
||||||
ndpi_struct->callback_buffer_udp[a].func(ndpi_struct, flow);
|
ndpi_struct->callback_buffer_udp[a].func(ndpi_struct, flow);
|
||||||
|
|
||||||
// NDPI_LOG_DBG(ndpi_struct, "[UDP,CALL] dissector of protocol as callback_buffer idx = %d\n",a);
|
// NDPI_LOG_DBG(ndpi_struct, "[UDP,CALL] dissector of protocol as callback_buffer idx = %d\n",a);
|
||||||
if(flow->detected_protocol_stack[0] != NDPI_PROTOCOL_UNKNOWN)
|
if(flow->detected_protocol_stack[0] != NDPI_PROTOCOL_UNKNOWN)
|
||||||
break; /* Stop after detecting the first protocol */
|
break; /* Stop after detecting the first protocol */
|
||||||
@@ -4528,7 +4546,7 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
|
|||||||
|
|
||||||
/* Init default */
|
/* Init default */
|
||||||
ret.master_protocol = flow->detected_protocol_stack[1], ret.app_protocol = flow->detected_protocol_stack[0];
|
ret.master_protocol = flow->detected_protocol_stack[1], ret.app_protocol = flow->detected_protocol_stack[0];
|
||||||
|
|
||||||
if(flow->server_id == NULL) flow->server_id = dst; /* Default */
|
if(flow->server_id == NULL) flow->server_id = dst; /* Default */
|
||||||
if(flow->detected_protocol_stack[0] != NDPI_PROTOCOL_UNKNOWN) {
|
if(flow->detected_protocol_stack[0] != NDPI_PROTOCOL_UNKNOWN) {
|
||||||
/*
|
/*
|
||||||
@@ -4541,7 +4559,7 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
|
|||||||
*/
|
*/
|
||||||
) {
|
) {
|
||||||
ndpi_process_extra_packet(ndpi_struct, flow, packet, packetlen, current_tick_l, src, dst);
|
ndpi_process_extra_packet(ndpi_struct, flow, packet, packetlen, current_tick_l, src, dst);
|
||||||
|
|
||||||
return(ret);
|
return(ret);
|
||||||
} else
|
} else
|
||||||
goto ret_protocols;
|
goto ret_protocols;
|
||||||
@@ -4747,7 +4765,7 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
|
|||||||
*/
|
*/
|
||||||
flow->packet.iph = NULL, flow->packet.tcp = NULL, flow->packet.udp = NULL, flow->packet.payload = NULL;
|
flow->packet.iph = NULL, flow->packet.tcp = NULL, flow->packet.udp = NULL, flow->packet.payload = NULL;
|
||||||
ndpi_reset_packet_line_info(&flow->packet);
|
ndpi_reset_packet_line_info(&flow->packet);
|
||||||
|
|
||||||
return(ret);
|
return(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4910,7 +4928,7 @@ void ndpi_parse_packet_line_info(struct ndpi_detection_module_struct *ndpi_struc
|
|||||||
|
|
||||||
packet->packet_lines_parsed_complete = 1;
|
packet->packet_lines_parsed_complete = 1;
|
||||||
ndpi_reset_packet_line_info(packet);
|
ndpi_reset_packet_line_info(packet);
|
||||||
|
|
||||||
if((packet->payload_packet_len < 3)
|
if((packet->payload_packet_len < 3)
|
||||||
|| (packet->payload == NULL))
|
|| (packet->payload == NULL))
|
||||||
return;
|
return;
|
||||||
@@ -5875,7 +5893,7 @@ int ndpi_get_category_id(struct ndpi_detection_module_struct *ndpi_mod, char *ca
|
|||||||
|
|
||||||
void ndpi_dump_protocols(struct ndpi_detection_module_struct *ndpi_mod) {
|
void ndpi_dump_protocols(struct ndpi_detection_module_struct *ndpi_mod) {
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for(i=0; i<(int)ndpi_mod->ndpi_num_supported_protocols; i++)
|
for(i=0; i<(int)ndpi_mod->ndpi_num_supported_protocols; i++)
|
||||||
printf("%3d %-22s %-12s %s\n", i,
|
printf("%3d %-22s %-12s %s\n", i,
|
||||||
ndpi_mod->proto_defaults[i].protoName,
|
ndpi_mod->proto_defaults[i].protoName,
|
||||||
@@ -6115,6 +6133,13 @@ int ndpi_match_bigram(struct ndpi_detection_module_struct *ndpi_struct,
|
|||||||
rc = ac_automata_search(((AC_AUTOMATA_t*)automa->ac_automa), &ac_input_text, &match);
|
rc = ac_automata_search(((AC_AUTOMATA_t*)automa->ac_automa), &ac_input_text, &match);
|
||||||
ac_automata_reset(((AC_AUTOMATA_t*)automa->ac_automa));
|
ac_automata_reset(((AC_AUTOMATA_t*)automa->ac_automa));
|
||||||
|
|
||||||
|
/*
|
||||||
|
As ac_automata_search can detect partial matches and continue the search process
|
||||||
|
in case rc == 0 (i.e. no match), we need to check if there is a partial match
|
||||||
|
and in this case return it
|
||||||
|
*/
|
||||||
|
if((rc == 0) && (match.number != 0)) rc = 1;
|
||||||
|
|
||||||
return(rc ? match.number : 0);
|
return(rc ? match.number : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -6129,7 +6154,7 @@ void ndpi_free_flow(struct ndpi_flow_struct *flow) {
|
|||||||
if(flow->l4.tcp.tls_srv_cert_fingerprint_ctx)
|
if(flow->l4.tcp.tls_srv_cert_fingerprint_ctx)
|
||||||
ndpi_free(flow->l4.tcp.tls_srv_cert_fingerprint_ctx);
|
ndpi_free(flow->l4.tcp.tls_srv_cert_fingerprint_ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
ndpi_free(flow);
|
ndpi_free(flow);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6290,7 +6315,7 @@ int ndpi_flowv6_flow_hash(u_int8_t l4_proto, struct ndpi_in6_addr *src_ip, struc
|
|||||||
|
|
||||||
/* ******************************************************************** */
|
/* ******************************************************************** */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
This function tells if it's possible to further dissect a given flow
|
This function tells if it's possible to further dissect a given flow
|
||||||
0 - All possible dissection has been completed
|
0 - All possible dissection has been completed
|
||||||
1 - Additional dissection is possible
|
1 - Additional dissection is possible
|
||||||
@@ -6303,28 +6328,28 @@ u_int8_t ndpi_extra_dissection_possible(struct ndpi_detection_module_struct *ndp
|
|||||||
flow->detected_protocol_stack[0],
|
flow->detected_protocol_stack[0],
|
||||||
flow->detected_protocol_stack[1]);
|
flow->detected_protocol_stack[1]);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if(flow->check_extra_packets) return(1);
|
if(flow->check_extra_packets) return(1);
|
||||||
|
|
||||||
switch(flow->detected_protocol_stack[0]) {
|
switch(flow->detected_protocol_stack[0]) {
|
||||||
case NDPI_PROTOCOL_TLS:
|
case NDPI_PROTOCOL_TLS:
|
||||||
if(!flow->l4.tcp.tls_srv_cert_fingerprint_processed)
|
if(!flow->l4.tcp.tls_srv_cert_fingerprint_processed)
|
||||||
return(1);
|
return(1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case NDPI_PROTOCOL_HTTP:
|
case NDPI_PROTOCOL_HTTP:
|
||||||
if(flow->host_server_name[0] == '\0')
|
if(flow->host_server_name[0] == '\0')
|
||||||
return(1);
|
return(1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case NDPI_PROTOCOL_DNS:
|
case NDPI_PROTOCOL_DNS:
|
||||||
if((ndpi_struct->dns_dont_dissect_response == 0)
|
if((ndpi_struct->dns_dont_dissect_response == 0)
|
||||||
&& (flow->host_server_name[0] == '\0'))
|
&& (flow->host_server_name[0] == '\0'))
|
||||||
return(1);
|
return(1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ******************************************************************** */
|
/* ******************************************************************** */
|
||||||
|
|||||||
@@ -263,12 +263,14 @@ void ndpi_search_dns(struct ndpi_detection_module_struct *ndpi_struct, struct nd
|
|||||||
|
|
||||||
if(j > 0) {
|
if(j > 0) {
|
||||||
ndpi_protocol_match_result ret_match;
|
ndpi_protocol_match_result ret_match;
|
||||||
|
u_int32_t subproto = ndpi_match_host_subprotocol(ndpi_struct, flow,
|
||||||
|
(char *)flow->host_server_name,
|
||||||
|
strlen((const char*)flow->host_server_name),
|
||||||
|
&ret_match,
|
||||||
|
NDPI_PROTOCOL_DNS);
|
||||||
|
|
||||||
ndpi_match_host_subprotocol(ndpi_struct, flow,
|
if(ret_match.protocol_category != NDPI_PROTOCOL_CATEGORY_UNSPECIFIED)
|
||||||
(char *)flow->host_server_name,
|
flow->category = ret_match.protocol_category;
|
||||||
strlen((const char*)flow->host_server_name),
|
|
||||||
&ret_match,
|
|
||||||
NDPI_PROTOCOL_DNS);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DNS_DEBUG
|
#ifdef DNS_DEBUG
|
||||||
|
|||||||
Reference in New Issue
Block a user