Improved hyperscan support and category definition
This commit is contained in:
+10
-5
@@ -54,14 +54,19 @@ fi
|
|||||||
|
|
||||||
HS_LIB=
|
HS_LIB=
|
||||||
HS_INC=
|
HS_INC=
|
||||||
AC_ARG_WITH(hyperscan, [ --with-hyperscan Enable Intel Hyperscan (if available)])
|
BKP=$LIBS
|
||||||
|
LIBS="$LIBS -lstdc++ -lm"
|
||||||
|
AC_CHECK_LIB([hs], [hs_compile_multi], AC_DEFINE_UNQUOTED(HAVE_HYPERSCAN, 1, [Intel Hyperscan is present]))
|
||||||
|
LIBS=$BKP
|
||||||
|
|
||||||
if test "${with_hyperscan}" == "yes"; then :
|
if test "x$ac_cv_lib_hs_hs_compile_multi" = xyes; then :
|
||||||
AC_CHECK_LIB([hs], [hs_compile_multi], AC_DEFINE_UNQUOTED(HAVE_HYPERSCAN, 1, [Intel Hyperscan is present]))
|
AC_CHECK_LIB([m], [pow])
|
||||||
HS_INC=`pkg-config --cflags libhs`
|
AC_CHECK_LIB([stdc++], [main])
|
||||||
HS_LIB=`pkg-config --libs libhs`
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
HS_INC=`pkg-config --cflags libhs`
|
||||||
|
HS_LIB=`pkg-config --libs libhs`
|
||||||
|
|
||||||
if test -f $PCAP_HOME/libpcap/libpcap.a; then :
|
if test -f $PCAP_HOME/libpcap/libpcap.a; then :
|
||||||
echo "Using libpcap from $PCAP_HOME"
|
echo "Using libpcap from $PCAP_HOME"
|
||||||
PCAP_INC="-I $PCAP_HOME/libpcap"
|
PCAP_INC="-I $PCAP_HOME/libpcap"
|
||||||
|
|||||||
@@ -755,11 +755,17 @@ static void printFlow(u_int16_t id, struct ndpi_flow_info *flow, u_int16_t threa
|
|||||||
flow->detected_protocol.master_protocol, flow->detected_protocol.app_protocol,
|
flow->detected_protocol.master_protocol, flow->detected_protocol.app_protocol,
|
||||||
ndpi_protocol2name(ndpi_thread_info[thread_id].workflow->ndpi_struct,
|
ndpi_protocol2name(ndpi_thread_info[thread_id].workflow->ndpi_struct,
|
||||||
flow->detected_protocol, buf, sizeof(buf)));
|
flow->detected_protocol, buf, sizeof(buf)));
|
||||||
|
|
||||||
|
if(flow->detected_protocol.category != 0)
|
||||||
|
fprintf(out, "[cat: %u]", flow->detected_protocol.category);
|
||||||
} else
|
} else
|
||||||
fprintf(out, "[proto: %u/%s]",
|
fprintf(out, "[proto: %u/%s]",
|
||||||
flow->detected_protocol.app_protocol,
|
flow->detected_protocol.app_protocol,
|
||||||
ndpi_get_proto_name(ndpi_thread_info[thread_id].workflow->ndpi_struct, flow->detected_protocol.app_protocol));
|
ndpi_get_proto_name(ndpi_thread_info[thread_id].workflow->ndpi_struct, flow->detected_protocol.app_protocol));
|
||||||
|
|
||||||
|
if(flow->detected_protocol.category != 0)
|
||||||
|
fprintf(out, "[cat: %u]", flow->detected_protocol.category);
|
||||||
|
|
||||||
fprintf(out, "[%u pkts/%llu bytes ", flow->src2dst_packets, (long long unsigned int) flow->src2dst_bytes);
|
fprintf(out, "[%u pkts/%llu bytes ", flow->src2dst_packets, (long long unsigned int) flow->src2dst_bytes);
|
||||||
fprintf(out, "%s %u pkts/%llu bytes]",
|
fprintf(out, "%s %u pkts/%llu bytes]",
|
||||||
(flow->dst2src_packets > 0) ? "<->" : "->",
|
(flow->dst2src_packets > 0) ? "<->" : "->",
|
||||||
|
|||||||
+12
-12
@@ -453,7 +453,7 @@ static struct ndpi_flow_info *get_ndpi_flow_info(struct ndpi_workflow * workflow
|
|||||||
} else {
|
} else {
|
||||||
struct ndpi_flow_info *flow = *(struct ndpi_flow_info**)ret;
|
struct ndpi_flow_info *flow = *(struct ndpi_flow_info**)ret;
|
||||||
|
|
||||||
if (is_changed) {
|
if(is_changed) {
|
||||||
if(flow->src_ip == iph->saddr
|
if(flow->src_ip == iph->saddr
|
||||||
&& flow->dst_ip == iph->daddr
|
&& flow->dst_ip == iph->daddr
|
||||||
&& flow->src_port == htons(*sport)
|
&& flow->src_port == htons(*sport)
|
||||||
@@ -510,7 +510,8 @@ static struct ndpi_flow_info *get_ndpi_flow_info6(struct ndpi_workflow * workflo
|
|||||||
sizeof(struct ndpi_ipv6hdr),
|
sizeof(struct ndpi_ipv6hdr),
|
||||||
ntohs(iph6->ip6_hdr.ip6_un1_plen),
|
ntohs(iph6->ip6_hdr.ip6_un1_plen),
|
||||||
tcph, udph, sport, dport,
|
tcph, udph, sport, dport,
|
||||||
src, dst, proto, payload, payload_len, src_to_dst_direction));
|
src, dst, proto, payload,
|
||||||
|
payload_len, src_to_dst_direction));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ****************************************************** */
|
/* ****************************************************** */
|
||||||
@@ -518,8 +519,6 @@ static struct ndpi_flow_info *get_ndpi_flow_info6(struct ndpi_workflow * workflo
|
|||||||
void process_ndpi_collected_info(struct ndpi_workflow * workflow, struct ndpi_flow_info *flow) {
|
void process_ndpi_collected_info(struct ndpi_workflow * workflow, struct ndpi_flow_info *flow) {
|
||||||
if(!flow->ndpi_flow) return;
|
if(!flow->ndpi_flow) return;
|
||||||
|
|
||||||
/* printf("CATEGORY %u\n", flow->detected_protocol.category); */
|
|
||||||
|
|
||||||
snprintf(flow->host_server_name, sizeof(flow->host_server_name), "%s",
|
snprintf(flow->host_server_name, sizeof(flow->host_server_name), "%s",
|
||||||
flow->ndpi_flow->host_server_name);
|
flow->ndpi_flow->host_server_name);
|
||||||
|
|
||||||
@@ -528,7 +527,9 @@ void process_ndpi_collected_info(struct ndpi_workflow * workflow, struct ndpi_fl
|
|||||||
int i, j, n = 0;
|
int i, j, n = 0;
|
||||||
|
|
||||||
for(i=0, j = 0; j < sizeof(flow->bittorent_hash)-1; i++) {
|
for(i=0, j = 0; j < sizeof(flow->bittorent_hash)-1; i++) {
|
||||||
sprintf(&flow->bittorent_hash[j], "%02x", flow->ndpi_flow->protos.bittorrent.hash[i]);
|
sprintf(&flow->bittorent_hash[j], "%02x",
|
||||||
|
flow->ndpi_flow->protos.bittorrent.hash[i]);
|
||||||
|
|
||||||
j += 2, n += flow->ndpi_flow->protos.bittorrent.hash[i];
|
j += 2, n += flow->ndpi_flow->protos.bittorrent.hash[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -562,10 +563,10 @@ void process_ndpi_collected_info(struct ndpi_workflow * workflow, struct ndpi_fl
|
|||||||
|
|
||||||
if(flow->detection_completed && !flow->check_extra_packets) {
|
if(flow->detection_completed && !flow->check_extra_packets) {
|
||||||
if(flow->detected_protocol.app_protocol == NDPI_PROTOCOL_UNKNOWN) {
|
if(flow->detected_protocol.app_protocol == NDPI_PROTOCOL_UNKNOWN) {
|
||||||
if (workflow->__flow_giveup_callback != NULL)
|
if(workflow->__flow_giveup_callback != NULL)
|
||||||
workflow->__flow_giveup_callback(workflow, flow, workflow->__flow_giveup_udata);
|
workflow->__flow_giveup_callback(workflow, flow, workflow->__flow_giveup_udata);
|
||||||
} else {
|
} else {
|
||||||
if (workflow->__flow_detected_callback != NULL)
|
if(workflow->__flow_detected_callback != NULL)
|
||||||
workflow->__flow_detected_callback(workflow, flow, workflow->__flow_detected_udata);
|
workflow->__flow_detected_callback(workflow, flow, workflow->__flow_detected_udata);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -641,12 +642,12 @@ static struct ndpi_proto packet_processing(struct ndpi_workflow * workflow,
|
|||||||
ndpi_process_extra_packet(workflow->ndpi_struct, ndpi_flow,
|
ndpi_process_extra_packet(workflow->ndpi_struct, ndpi_flow,
|
||||||
iph ? (uint8_t *)iph : (uint8_t *)iph6,
|
iph ? (uint8_t *)iph : (uint8_t *)iph6,
|
||||||
ipsize, time, src, dst);
|
ipsize, time, src, dst);
|
||||||
if (ndpi_flow->check_extra_packets == 0) {
|
if(ndpi_flow->check_extra_packets == 0) {
|
||||||
flow->check_extra_packets = 0;
|
flow->check_extra_packets = 0;
|
||||||
process_ndpi_collected_info(workflow, flow);
|
process_ndpi_collected_info(workflow, flow);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (ndpi_flow != NULL) {
|
} else if(ndpi_flow != NULL) {
|
||||||
/* If this wasn't NULL we should do the half free */
|
/* If this wasn't NULL we should do the half free */
|
||||||
/* TODO: When half_free is deprecated, get rid of this */
|
/* TODO: When half_free is deprecated, get rid of this */
|
||||||
ndpi_free_flow_info_half(flow);
|
ndpi_free_flow_info_half(flow);
|
||||||
@@ -666,7 +667,7 @@ static struct ndpi_proto packet_processing(struct ndpi_workflow * workflow,
|
|||||||
/* New protocol detected or give up */
|
/* New protocol detected or give up */
|
||||||
flow->detection_completed = 1;
|
flow->detection_completed = 1;
|
||||||
/* Check if we should keep checking extra packets */
|
/* Check if we should keep checking extra packets */
|
||||||
if (ndpi_flow->check_extra_packets)
|
if(ndpi_flow->check_extra_packets)
|
||||||
flow->check_extra_packets = 1;
|
flow->check_extra_packets = 1;
|
||||||
|
|
||||||
if(flow->detected_protocol.app_protocol == NDPI_PROTOCOL_UNKNOWN)
|
if(flow->detected_protocol.app_protocol == NDPI_PROTOCOL_UNKNOWN)
|
||||||
@@ -780,7 +781,7 @@ datalink_check:
|
|||||||
|
|
||||||
if(check <= 1500)
|
if(check <= 1500)
|
||||||
pyld_eth_len = check;
|
pyld_eth_len = check;
|
||||||
else if (check >= 1536)
|
else if(check >= 1536)
|
||||||
type = check;
|
type = check;
|
||||||
|
|
||||||
if(pyld_eth_len != 0) {
|
if(pyld_eth_len != 0) {
|
||||||
@@ -1069,4 +1070,3 @@ u_int32_t ethernet_crc32(const void* data, size_t n_bytes) {
|
|||||||
__crc32(data, n_bytes, &crc);
|
__crc32(data, n_bytes, &crc);
|
||||||
return crc;
|
return crc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -751,9 +751,9 @@ extern "C" {
|
|||||||
|
|
||||||
void ndpi_load_ip_category(struct ndpi_detection_module_struct *ndpi_struct,
|
void ndpi_load_ip_category(struct ndpi_detection_module_struct *ndpi_struct,
|
||||||
char *ip_address_and_mask, ndpi_protocol_category_t category);
|
char *ip_address_and_mask, ndpi_protocol_category_t category);
|
||||||
void ndpi_load_hostname_category(struct ndpi_detection_module_struct *ndpi_struct,
|
int ndpi_load_hostname_category(struct ndpi_detection_module_struct *ndpi_struct,
|
||||||
char *name, ndpi_protocol_category_t category);
|
char *name, ndpi_protocol_category_t category);
|
||||||
void ndpi_enable_loaded_categories(struct ndpi_detection_module_struct *ndpi_struct);
|
int ndpi_enable_loaded_categories(struct ndpi_detection_module_struct *ndpi_struct);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a string to match to an automata
|
* Add a string to match to an automata
|
||||||
|
|||||||
@@ -828,6 +828,14 @@ typedef struct ndpi_proto {
|
|||||||
#define NUM_CUSTOM_CATEGORIES 5
|
#define NUM_CUSTOM_CATEGORIES 5
|
||||||
#define CUSTOM_CATEGORY_LABEL_LEN 32
|
#define CUSTOM_CATEGORY_LABEL_LEN 32
|
||||||
|
|
||||||
|
#ifdef HAVE_HYPERSCAN
|
||||||
|
struct hs_list {
|
||||||
|
char *expression;
|
||||||
|
unsigned int id;
|
||||||
|
struct hs_list *next;
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
struct ndpi_detection_module_struct {
|
struct ndpi_detection_module_struct {
|
||||||
NDPI_PROTOCOL_BITMASK detection_bitmask;
|
NDPI_PROTOCOL_BITMASK detection_bitmask;
|
||||||
NDPI_PROTOCOL_BITMASK generic_http_packet_bitmask;
|
NDPI_PROTOCOL_BITMASK generic_http_packet_bitmask;
|
||||||
@@ -886,7 +894,13 @@ struct ndpi_detection_module_struct {
|
|||||||
bigrams_automa, impossible_bigrams_automa; /* TOR */
|
bigrams_automa, impossible_bigrams_automa; /* TOR */
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
|
#ifdef HAVE_HYPERSCAN
|
||||||
|
struct hs *hostnames;
|
||||||
|
unsigned int num_to_load;
|
||||||
|
struct hs_list *to_load;
|
||||||
|
#else
|
||||||
ndpi_automa hostnames, hostnames_shadow;
|
ndpi_automa hostnames, hostnames_shadow;
|
||||||
|
#endif
|
||||||
void *ipAddresses, *ipAddresses_shadow; /* Patricia */
|
void *ipAddresses, *ipAddresses_shadow; /* Patricia */
|
||||||
} custom_categories;
|
} custom_categories;
|
||||||
|
|
||||||
|
|||||||
+216
-35
@@ -745,12 +745,36 @@ void ndpi_init_protocol_match(struct ndpi_detection_module_struct *ndpi_mod,
|
|||||||
|
|
||||||
#ifdef HAVE_HYPERSCAN
|
#ifdef HAVE_HYPERSCAN
|
||||||
|
|
||||||
|
static int hyperscan_load_patterns(struct hs *hs, u_int num_patterns,
|
||||||
|
const char **expressions, unsigned int *ids) {
|
||||||
|
hs_compile_error_t *compile_err;
|
||||||
|
|
||||||
|
if(hs_compile_multi(expressions, NULL, ids,
|
||||||
|
num_patterns, HS_MODE_BLOCK, NULL,
|
||||||
|
&hs->database, &compile_err) != HS_SUCCESS) {
|
||||||
|
NDPI_LOG_ERR(ndpi_mod, "Unable to initialize hyperscan database\n");
|
||||||
|
hs_free_compile_error(compile_err);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
hs->scratch = NULL;
|
||||||
|
if(hs_alloc_scratch(hs->database, &hs->scratch) != HS_SUCCESS) {
|
||||||
|
NDPI_LOG_ERR(ndpi_mod, "Unable to allocate hyperscan scratch space\n");
|
||||||
|
hs_free_database(hs->database);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ******************************************************************** */
|
||||||
|
|
||||||
static int init_hyperscan(struct ndpi_detection_module_struct *ndpi_mod) {
|
static int init_hyperscan(struct ndpi_detection_module_struct *ndpi_mod) {
|
||||||
u_int num_patterns = 0, i;
|
u_int num_patterns = 0, i;
|
||||||
const char **expressions;
|
const char **expressions;
|
||||||
unsigned int *ids;
|
unsigned int *ids;
|
||||||
hs_compile_error_t *compile_err;
|
|
||||||
struct hs *hs;
|
struct hs *hs;
|
||||||
|
int rc;
|
||||||
|
|
||||||
ndpi_mod->hyperscan = (void*)malloc(sizeof(struct hs));
|
ndpi_mod->hyperscan = (void*)malloc(sizeof(struct hs));
|
||||||
if(!ndpi_mod->hyperscan) return(-1);
|
if(!ndpi_mod->hyperscan) return(-1);
|
||||||
@@ -780,32 +804,26 @@ static int init_hyperscan(struct ndpi_detection_module_struct *ndpi_mod) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(hs_compile_multi(expressions, NULL, ids,
|
rc = hyperscan_load_patterns(hs, num_patterns, expressions, ids);
|
||||||
num_patterns, HS_MODE_BLOCK, NULL,
|
free(expressions), free(ids);
|
||||||
&hs->database, &compile_err) != HS_SUCCESS) {
|
|
||||||
NDPI_LOG_ERR(ndpi_mod, "Unable to initialize hyperscan database\n");
|
|
||||||
hs_free_compile_error(compile_err);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(hs_alloc_scratch(hs->database, &hs->scratch) != HS_SUCCESS) {
|
return(rc);
|
||||||
NDPI_LOG_ERR(ndpi_mod, "Unable to allocate hyperscan scratch space\n");
|
}
|
||||||
hs_free_database(hs->database);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
/* ******************************************************************** */
|
||||||
|
|
||||||
|
static void free_hyperscan_memory(struct hs *h) {
|
||||||
|
if(h) {
|
||||||
|
hs_free_scratch(h->scratch);
|
||||||
|
hs_free_database(h->database);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ******************************************************************** */
|
/* ******************************************************************** */
|
||||||
|
|
||||||
static void destroy_hyperscan(struct ndpi_detection_module_struct *ndpi_mod) {
|
static void destroy_hyperscan(struct ndpi_detection_module_struct *ndpi_mod) {
|
||||||
if(ndpi_mod->hyperscan) {
|
if(ndpi_mod->hyperscan)
|
||||||
struct hs *hs = (struct hs*)ndpi_mod->hyperscan;
|
free_hyperscan_memory((struct hs*)ndpi_mod->hyperscan);
|
||||||
|
|
||||||
hs_free_scratch(hs->scratch);
|
|
||||||
hs_free_database(hs->database);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -2048,8 +2066,14 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(void) {
|
|||||||
ndpi_str->bigrams_automa.ac_automa = ac_automata_init(ac_match_handler);
|
ndpi_str->bigrams_automa.ac_automa = ac_automata_init(ac_match_handler);
|
||||||
ndpi_str->impossible_bigrams_automa.ac_automa = ac_automata_init(ac_match_handler);
|
ndpi_str->impossible_bigrams_automa.ac_automa = ac_automata_init(ac_match_handler);
|
||||||
|
|
||||||
|
#ifdef HAVE_HYPERSCAN
|
||||||
|
ndpi_str->custom_categories.num_to_load = 0, ndpi_str->custom_categories.to_load = NULL;
|
||||||
|
ndpi_str->custom_categories.hostnames = NULL;
|
||||||
|
#else
|
||||||
ndpi_str->custom_categories.hostnames.ac_automa = ac_automata_init(ac_match_handler);
|
ndpi_str->custom_categories.hostnames.ac_automa = ac_automata_init(ac_match_handler);
|
||||||
ndpi_str->custom_categories.hostnames_shadow.ac_automa = ac_automata_init(ac_match_handler);
|
ndpi_str->custom_categories.hostnames_shadow.ac_automa = ac_automata_init(ac_match_handler);
|
||||||
|
#endif
|
||||||
|
|
||||||
ndpi_str->custom_categories.ipAddresses = ndpi_New_Patricia(32 /* IPv4 */);
|
ndpi_str->custom_categories.ipAddresses = ndpi_New_Patricia(32 /* IPv4 */);
|
||||||
ndpi_str->custom_categories.ipAddresses_shadow = ndpi_New_Patricia(32 /* IPv4 */);
|
ndpi_str->custom_categories.ipAddresses_shadow = ndpi_New_Patricia(32 /* IPv4 */);
|
||||||
|
|
||||||
@@ -2132,6 +2156,54 @@ int ndpi_match_string_id(void *_automa, char *string_to_match, unsigned long *id
|
|||||||
|
|
||||||
/* *********************************************** */
|
/* *********************************************** */
|
||||||
|
|
||||||
|
#ifdef HAVE_HYPERSCAN
|
||||||
|
|
||||||
|
static int hyperscanCustomEventHandler(unsigned int id,
|
||||||
|
unsigned long long from,
|
||||||
|
unsigned long long to,
|
||||||
|
unsigned int flags, void *ctx) {
|
||||||
|
*((unsigned long *)ctx) = (unsigned long)id;
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("[HS] Found category %u\n", id);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return HS_SCAN_TERMINATED;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* *********************************************** */
|
||||||
|
|
||||||
|
static int ndpi_match_custom_category(struct ndpi_detection_module_struct *ndpi_struct,
|
||||||
|
char *name, unsigned long *id) {
|
||||||
|
#ifdef HAVE_HYPERSCAN
|
||||||
|
if(ndpi_struct->custom_categories.hostnames == NULL)
|
||||||
|
return(-1);
|
||||||
|
else {
|
||||||
|
hs_error_t rc;
|
||||||
|
|
||||||
|
*id = (unsigned long)-1;
|
||||||
|
|
||||||
|
rc = hs_scan(ndpi_struct->custom_categories.hostnames->database,
|
||||||
|
name, strlen(name), 0,
|
||||||
|
ndpi_struct->custom_categories.hostnames->scratch,
|
||||||
|
hyperscanCustomEventHandler, id);
|
||||||
|
|
||||||
|
if(rc == HS_SCAN_TERMINATED) {
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("[HS] Found category %lu for %s\n", *id, name);
|
||||||
|
#endif
|
||||||
|
return(0);
|
||||||
|
} else
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
return(ndpi_match_string_id(ndpi_struct->custom_categories.hostnames.ac_automa, name, id));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/* *********************************************** */
|
||||||
|
|
||||||
static void free_ptree_data(void *data) { ; }
|
static void free_ptree_data(void *data) { ; }
|
||||||
|
|
||||||
/* ****************************************************** */
|
/* ****************************************************** */
|
||||||
@@ -2172,13 +2244,23 @@ void ndpi_exit_detection_module(struct ndpi_detection_module_struct *ndpi_struct
|
|||||||
|
|
||||||
#ifdef HAVE_HYPERSCAN
|
#ifdef HAVE_HYPERSCAN
|
||||||
destroy_hyperscan(ndpi_struct);
|
destroy_hyperscan(ndpi_struct);
|
||||||
#endif
|
|
||||||
|
|
||||||
|
while(ndpi_struct->custom_categories.to_load != NULL) {
|
||||||
|
struct hs_list *next = ndpi_struct->custom_categories.to_load->next;
|
||||||
|
|
||||||
|
free(ndpi_struct->custom_categories.to_load->expression);
|
||||||
|
free(ndpi_struct->custom_categories.to_load);
|
||||||
|
ndpi_struct->custom_categories.to_load = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
free_hyperscan_memory(ndpi_struct->custom_categories.hostnames);
|
||||||
|
#else
|
||||||
if(ndpi_struct->custom_categories.hostnames.ac_automa != NULL)
|
if(ndpi_struct->custom_categories.hostnames.ac_automa != NULL)
|
||||||
ac_automata_release((AC_AUTOMATA_t*)ndpi_struct->custom_categories.hostnames.ac_automa);
|
ac_automata_release((AC_AUTOMATA_t*)ndpi_struct->custom_categories.hostnames.ac_automa);
|
||||||
|
|
||||||
if(ndpi_struct->custom_categories.hostnames_shadow.ac_automa != NULL)
|
if(ndpi_struct->custom_categories.hostnames_shadow.ac_automa != NULL)
|
||||||
ac_automata_release((AC_AUTOMATA_t*)ndpi_struct->custom_categories.hostnames_shadow.ac_automa);
|
ac_automata_release((AC_AUTOMATA_t*)ndpi_struct->custom_categories.hostnames_shadow.ac_automa);
|
||||||
|
#endif
|
||||||
|
|
||||||
if(ndpi_struct->custom_categories.ipAddresses != NULL)
|
if(ndpi_struct->custom_categories.ipAddresses != NULL)
|
||||||
ndpi_Destroy_Patricia((patricia_tree_t*)ndpi_struct->custom_categories.ipAddresses, free_ptree_data);
|
ndpi_Destroy_Patricia((patricia_tree_t*)ndpi_struct->custom_categories.ipAddresses, free_ptree_data);
|
||||||
@@ -3764,38 +3846,135 @@ void ndpi_load_ip_category(struct ndpi_detection_module_struct *ndpi_struct,
|
|||||||
|
|
||||||
/* ********************************************************************************* */
|
/* ********************************************************************************* */
|
||||||
|
|
||||||
void ndpi_load_hostname_category(struct ndpi_detection_module_struct *ndpi_struct,
|
int ndpi_load_hostname_category(struct ndpi_detection_module_struct *ndpi_struct,
|
||||||
char *name, ndpi_protocol_category_t category) {
|
char *name, ndpi_protocol_category_t category) {
|
||||||
AC_PATTERN_t ac_pattern;
|
AC_PATTERN_t ac_pattern;
|
||||||
|
|
||||||
if(name == NULL) return;
|
if(name == NULL)
|
||||||
|
return(-1);
|
||||||
|
|
||||||
/* printf("===> Loading %s as %u\n", name, category); */
|
/* printf("===> Loading %s as %u\n", name, category); */
|
||||||
|
|
||||||
if(ndpi_struct->custom_categories.hostnames_shadow.ac_automa == NULL) return;
|
#ifdef HAVE_HYPERSCAN
|
||||||
|
{
|
||||||
|
struct hs_list *h = (struct hs_list*)malloc(sizeof(struct hs_list));
|
||||||
|
|
||||||
|
if(h) {
|
||||||
|
char tmp[256];
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
for(i=0, j=0; (j<sizeof(tmp)) && (name[i] != '\0'); i++) {
|
||||||
|
if(name[i] == '.')
|
||||||
|
tmp[j++] = '\\';
|
||||||
|
|
||||||
|
tmp[j++] = name[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp[j] = '\0';
|
||||||
|
|
||||||
|
h->expression = strdup(name), h->id = (unsigned int)category;
|
||||||
|
if(h->expression == NULL) {
|
||||||
|
free(h);
|
||||||
|
return(-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
h->next = ndpi_struct->custom_categories.to_load;
|
||||||
|
ndpi_struct->custom_categories.to_load = h;
|
||||||
|
ndpi_struct->custom_categories.num_to_load++;
|
||||||
|
} else
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
if(ndpi_struct->custom_categories.hostnames_shadow.ac_automa == NULL)
|
||||||
|
return(-1);
|
||||||
|
|
||||||
ac_pattern.astring = name, ac_pattern.length = strlen(ac_pattern.astring);
|
ac_pattern.astring = name, ac_pattern.length = strlen(ac_pattern.astring);
|
||||||
ac_pattern.rep.number = (int)category;
|
ac_pattern.rep.number = (int)category;
|
||||||
|
|
||||||
ac_automata_add(ndpi_struct->custom_categories.hostnames_shadow.ac_automa, &ac_pattern);
|
ac_automata_add(ndpi_struct->custom_categories.hostnames_shadow.ac_automa, &ac_pattern);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ********************************************************************************* */
|
/* ********************************************************************************* */
|
||||||
|
|
||||||
void ndpi_enable_loaded_categories(struct ndpi_detection_module_struct *ndpi_struct) {
|
int ndpi_enable_loaded_categories(struct ndpi_detection_module_struct *ndpi_str) {
|
||||||
|
#ifdef HAVE_HYPERSCAN
|
||||||
|
if(ndpi_str->custom_categories.num_to_load > 0) {
|
||||||
|
const char **expressions;
|
||||||
|
unsigned int *ids, i;
|
||||||
|
int rc;
|
||||||
|
struct hs_list *head = ndpi_str->custom_categories.to_load;
|
||||||
|
|
||||||
|
expressions = (const char**)calloc(sizeof(char*),
|
||||||
|
ndpi_str->custom_categories.num_to_load+1);
|
||||||
|
if(!expressions) return(-1);
|
||||||
|
|
||||||
|
ids = (unsigned int*)calloc(sizeof(unsigned int),
|
||||||
|
ndpi_str->custom_categories.num_to_load+1);
|
||||||
|
if(!ids) {
|
||||||
|
free(expressions);
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i=0; head != NULL; i++) {
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("[HS] Loading category %u for %s\n", head->id, head->expression);
|
||||||
|
#endif
|
||||||
|
expressions[i] = head->expression, ids[i] = head->id;
|
||||||
|
head = head->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
free_hyperscan_memory(ndpi_str->custom_categories.hostnames);
|
||||||
|
ndpi_str->custom_categories.hostnames = (struct hs*)malloc(sizeof(struct hs));
|
||||||
|
|
||||||
|
if(ndpi_str->custom_categories.hostnames == NULL) {
|
||||||
|
free(expressions), free(ids);
|
||||||
|
return(-1); /* Failed */
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = hyperscan_load_patterns(ndpi_str->custom_categories.hostnames,
|
||||||
|
ndpi_str->custom_categories.num_to_load,
|
||||||
|
expressions, ids);
|
||||||
|
free(expressions), free(ids);
|
||||||
|
|
||||||
|
head = ndpi_str->custom_categories.to_load;
|
||||||
|
while(head != NULL) {
|
||||||
|
struct hs_list *next = head->next;
|
||||||
|
|
||||||
|
free(head->expression);
|
||||||
|
free(head);
|
||||||
|
|
||||||
|
head = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
ndpi_str->custom_categories.to_load = NULL;
|
||||||
|
|
||||||
|
if(rc < 0) {
|
||||||
|
free(ndpi_str->custom_categories.hostnames);
|
||||||
|
ndpi_str->custom_categories.hostnames = NULL;
|
||||||
|
ndpi_str->custom_categories.hostnames = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
/* Free */
|
/* Free */
|
||||||
ac_automata_release((AC_AUTOMATA_t*)ndpi_struct->custom_categories.hostnames.ac_automa);
|
ac_automata_release((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames.ac_automa);
|
||||||
ndpi_Destroy_Patricia((patricia_tree_t*)ndpi_struct->custom_categories.ipAddresses, free_ptree_data);
|
ndpi_Destroy_Patricia((patricia_tree_t*)ndpi_str->custom_categories.ipAddresses, free_ptree_data);
|
||||||
|
|
||||||
/* Finalize */
|
/* Finalize */
|
||||||
ac_automata_finalize((AC_AUTOMATA_t*)ndpi_struct->custom_categories.hostnames_shadow.ac_automa);
|
ac_automata_finalize((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames_shadow.ac_automa);
|
||||||
|
|
||||||
/* Swap */
|
/* Swap */
|
||||||
ndpi_struct->custom_categories.hostnames.ac_automa = ndpi_struct->custom_categories.hostnames_shadow.ac_automa;
|
ndpi_str->custom_categories.hostnames.ac_automa = ndpi_str->custom_categories.hostnames_shadow.ac_automa;
|
||||||
ndpi_struct->custom_categories.ipAddresses = ndpi_struct->custom_categories.ipAddresses_shadow;
|
ndpi_str->custom_categories.ipAddresses = ndpi_str->custom_categories.ipAddresses_shadow;
|
||||||
|
|
||||||
/* Realloc */
|
/* Realloc */
|
||||||
ndpi_struct->custom_categories.hostnames_shadow.ac_automa = ac_automata_init(ac_match_handler);
|
ndpi_str->custom_categories.hostnames_shadow.ac_automa = ac_automata_init(ac_match_handler);
|
||||||
ndpi_struct->custom_categories.ipAddresses_shadow = ndpi_New_Patricia(32 /* IPv4 */);
|
ndpi_str->custom_categories.ipAddresses_shadow = ndpi_New_Patricia(32 /* IPv4 */);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ********************************************************************************* */
|
/* ********************************************************************************* */
|
||||||
@@ -3826,7 +4005,7 @@ static void ndpi_fill_protocol_category(struct ndpi_detection_module_struct *ndp
|
|||||||
|
|
||||||
if(flow->host_server_name[0] != '\0') {
|
if(flow->host_server_name[0] != '\0') {
|
||||||
unsigned long id;
|
unsigned long id;
|
||||||
int rc = ndpi_match_string_id(ndpi_struct->custom_categories.hostnames.ac_automa, (char *)flow->host_server_name, &id);
|
int rc =ndpi_match_custom_category(ndpi_struct, (char *)flow->host_server_name, &id);
|
||||||
|
|
||||||
if(rc == 0) {
|
if(rc == 0) {
|
||||||
ret->category = (ndpi_protocol_category_t)id;
|
ret->category = (ndpi_protocol_category_t)id;
|
||||||
@@ -3836,7 +4015,7 @@ static void ndpi_fill_protocol_category(struct ndpi_detection_module_struct *ndp
|
|||||||
|
|
||||||
if(flow->protos.ssl.server_certificate[0] != '\0') {
|
if(flow->protos.ssl.server_certificate[0] != '\0') {
|
||||||
unsigned long id;
|
unsigned long id;
|
||||||
int rc = ndpi_match_string_id(ndpi_struct->custom_categories.hostnames.ac_automa, (char *)flow->protos.ssl.server_certificate, &id);
|
int rc = ndpi_match_custom_category(ndpi_struct, (char *)flow->protos.ssl.server_certificate, &id);
|
||||||
|
|
||||||
if(rc == 0) {
|
if(rc == 0) {
|
||||||
ret->category = (ndpi_protocol_category_t)id;
|
ret->category = (ndpi_protocol_category_t)id;
|
||||||
@@ -5292,11 +5471,13 @@ static int ndpi_automa_match_string_subprotocol(struct ndpi_detection_module_str
|
|||||||
/* ******************************************************************** */
|
/* ******************************************************************** */
|
||||||
|
|
||||||
static int hyperscanEventHandler(unsigned int id, unsigned long long from,
|
static int hyperscanEventHandler(unsigned int id, unsigned long long from,
|
||||||
unsigned long long to, unsigned int flags, void *ctx) {
|
unsigned long long to, unsigned int flags, void *ctx) {
|
||||||
*((int *)ctx) = (int)id;
|
*((int *)ctx) = (int)id;
|
||||||
return HS_SCAN_TERMINATED;
|
return HS_SCAN_TERMINATED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* *********************************************** */
|
||||||
|
|
||||||
static int ndpi_automa_match_string_subprotocol(struct ndpi_detection_module_struct *ndpi_struct,
|
static int ndpi_automa_match_string_subprotocol(struct ndpi_detection_module_struct *ndpi_struct,
|
||||||
struct ndpi_flow_struct *flow,
|
struct ndpi_flow_struct *flow,
|
||||||
char *string_to_match, u_int string_to_match_len,
|
char *string_to_match, u_int string_to_match_len,
|
||||||
|
|||||||
Reference in New Issue
Block a user