Completed custom category implementation
This commit is contained in:
+51
-30
@@ -111,7 +111,6 @@ struct flow_info {
|
||||
|
||||
static struct flow_info *all_flows;
|
||||
|
||||
|
||||
struct info_pair {
|
||||
u_int32_t addr;
|
||||
u_int8_t version; /* IP version */
|
||||
@@ -135,16 +134,15 @@ struct port_stats {
|
||||
u_int32_t cumulative_addr; /*cumulative some of IP addresses */
|
||||
addr_node *addr_tree; /* tree of distinct IP addresses */
|
||||
struct info_pair top_ip_addrs[MAX_NUM_IP_ADDRESS];
|
||||
u_int8_t hasTopHost; /* as boolean flag*/
|
||||
u_int32_t top_host; /*host that is contributed to > 95% of traffic*/
|
||||
u_int8_t version; /* top host's ip version */
|
||||
char proto[16]; /*application level protocol of top host */
|
||||
UT_hash_handle hh; /* makes this structure hashable */
|
||||
u_int8_t hasTopHost; /* as boolean flag */
|
||||
u_int32_t top_host; /* host that is contributed to > 95% of traffic */
|
||||
u_int8_t version; /* top host's ip version */
|
||||
char proto[16]; /* application level protocol of top host */
|
||||
UT_hash_handle hh; /* makes this structure hashable */
|
||||
};
|
||||
|
||||
struct port_stats *srcStats = NULL, *dstStats = NULL;
|
||||
|
||||
|
||||
// struct to hold count of flows received by destination ports
|
||||
struct port_flow_info {
|
||||
u_int32_t port; /* key */
|
||||
@@ -349,6 +347,8 @@ struct ndpi_proto_sorter {
|
||||
char name[16];
|
||||
};
|
||||
|
||||
/* ********************************** */
|
||||
|
||||
int cmpProto(const void *_a, const void *_b) {
|
||||
struct ndpi_proto_sorter *a = (struct ndpi_proto_sorter*)_a;
|
||||
struct ndpi_proto_sorter *b = (struct ndpi_proto_sorter*)_b;
|
||||
@@ -356,6 +356,8 @@ int cmpProto(const void *_a, const void *_b) {
|
||||
return(strcmp(a->name, b->name));
|
||||
}
|
||||
|
||||
/* ********************************** */
|
||||
|
||||
int cmpFlows(const void *_a, const void *_b) {
|
||||
struct ndpi_flow_info *fa = ((struct flow_info*)_a)->flow;
|
||||
struct ndpi_flow_info *fb = ((struct flow_info*)_b)->flow;
|
||||
@@ -375,6 +377,8 @@ int cmpFlows(const void *_a, const void *_b) {
|
||||
return(0);
|
||||
}
|
||||
|
||||
/* ********************************** */
|
||||
|
||||
void extcap_config() {
|
||||
int i, argidx = 0;
|
||||
struct ndpi_proto_sorter *protos;
|
||||
@@ -656,12 +660,12 @@ static void parseOptions(int argc, char **argv) {
|
||||
#endif
|
||||
}
|
||||
|
||||
/* ********************************** */
|
||||
|
||||
/**
|
||||
* @brief From IPPROTO to string NAME
|
||||
*/
|
||||
static char* ipProto2Name(u_int16_t proto_id) {
|
||||
|
||||
static char proto[8];
|
||||
|
||||
switch(proto_id) {
|
||||
@@ -689,12 +693,12 @@ static char* ipProto2Name(u_int16_t proto_id) {
|
||||
return(proto);
|
||||
}
|
||||
|
||||
/* ********************************** */
|
||||
|
||||
/**
|
||||
* @brief A faster replacement for inet_ntoa().
|
||||
*/
|
||||
char* intoaV4(u_int32_t addr, char* buf, u_int16_t bufLen) {
|
||||
|
||||
char *cp, *retStr;
|
||||
uint byte;
|
||||
int n;
|
||||
@@ -723,6 +727,8 @@ char* intoaV4(u_int32_t addr, char* buf, u_int16_t bufLen) {
|
||||
return(retStr);
|
||||
}
|
||||
|
||||
/* ********************************** */
|
||||
|
||||
/**
|
||||
* @brief Print the flow
|
||||
*/
|
||||
@@ -792,16 +798,20 @@ static void printFlow(u_int16_t id, struct ndpi_flow_info *flow, u_int16_t threa
|
||||
json_object_object_add(jObj,"host_b.port",json_object_new_int(ntohs(flow->dst_port)));
|
||||
|
||||
if(flow->detected_protocol.master_protocol)
|
||||
json_object_object_add(jObj,"detected.master_protocol",json_object_new_int(flow->detected_protocol.master_protocol));
|
||||
json_object_object_add(jObj,"detected.master_protocol",
|
||||
json_object_new_int(flow->detected_protocol.master_protocol));
|
||||
|
||||
json_object_object_add(jObj,"detected.app_protocol",json_object_new_int(flow->detected_protocol.app_protocol));
|
||||
json_object_object_add(jObj,"detected.app_protocol",
|
||||
json_object_new_int(flow->detected_protocol.app_protocol));
|
||||
|
||||
if(flow->detected_protocol.master_protocol) {
|
||||
char tmp[256];
|
||||
|
||||
snprintf(tmp, sizeof(tmp), "%s.%s",
|
||||
ndpi_get_proto_name(ndpi_thread_info[thread_id].workflow->ndpi_struct, flow->detected_protocol.master_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.master_protocol),
|
||||
ndpi_get_proto_name(ndpi_thread_info[thread_id].workflow->ndpi_struct,
|
||||
flow->detected_protocol.app_protocol));
|
||||
|
||||
json_object_object_add(jObj,"detected.protocol.name",
|
||||
json_object_new_string(tmp));
|
||||
@@ -836,12 +846,13 @@ static void printFlow(u_int16_t id, struct ndpi_flow_info *flow, u_int16_t threa
|
||||
}
|
||||
}
|
||||
|
||||
/* ********************************** */
|
||||
|
||||
/**
|
||||
* @brief Unknown Proto Walker
|
||||
*/
|
||||
static void node_print_unknown_proto_walker(const void *node, ndpi_VISIT which, int depth, void *user_data) {
|
||||
|
||||
static void node_print_unknown_proto_walker(const void *node,
|
||||
ndpi_VISIT which, int depth, void *user_data) {
|
||||
struct ndpi_flow_info *flow = *(struct ndpi_flow_info**)node;
|
||||
u_int16_t thread_id = *((u_int16_t*)user_data);
|
||||
|
||||
@@ -854,11 +865,13 @@ static void node_print_unknown_proto_walker(const void *node, ndpi_VISIT which,
|
||||
}
|
||||
}
|
||||
|
||||
/* ********************************** */
|
||||
|
||||
/**
|
||||
* @brief Known Proto Walker
|
||||
*/
|
||||
static void node_print_known_proto_walker(const void *node, ndpi_VISIT which, int depth, void *user_data) {
|
||||
|
||||
static void node_print_known_proto_walker(const void *node,
|
||||
ndpi_VISIT which, int depth, void *user_data) {
|
||||
struct ndpi_flow_info *flow = *(struct ndpi_flow_info**)node;
|
||||
u_int16_t thread_id = *((u_int16_t*)user_data);
|
||||
|
||||
@@ -871,6 +884,7 @@ static void node_print_known_proto_walker(const void *node, ndpi_VISIT which, in
|
||||
}
|
||||
}
|
||||
|
||||
/* ********************************** */
|
||||
|
||||
/**
|
||||
* @brief Guess Undetected Protocol
|
||||
@@ -890,6 +904,7 @@ static u_int16_t node_guess_undetected_protocol(u_int16_t thread_id, struct ndpi
|
||||
return(flow->detected_protocol.app_protocol);
|
||||
}
|
||||
|
||||
/* ********************************** */
|
||||
|
||||
/**
|
||||
* @brief Proto Guess Walker
|
||||
@@ -909,6 +924,7 @@ static void node_proto_guess_walker(const void *node, ndpi_VISIT which, int dept
|
||||
}
|
||||
|
||||
process_ndpi_collected_info(ndpi_thread_info[thread_id].workflow, flow);
|
||||
|
||||
ndpi_thread_info[thread_id].workflow->stats.protocol_counter[flow->detected_protocol.app_protocol] += flow->src2dst_packets + flow->dst2src_packets;
|
||||
ndpi_thread_info[thread_id].workflow->stats.protocol_counter_bytes[flow->detected_protocol.app_protocol] += flow->src2dst_bytes + flow->dst2src_bytes;
|
||||
ndpi_thread_info[thread_id].workflow->stats.protocol_flows[flow->detected_protocol.app_protocol]++;
|
||||
@@ -1354,7 +1370,6 @@ static void port_stats_walker(const void *node, ndpi_VISIT which, int depth, voi
|
||||
* @brief Idle Scan Walker
|
||||
*/
|
||||
static void node_idle_scan_walker(const void *node, ndpi_VISIT which, int depth, void *user_data) {
|
||||
|
||||
struct ndpi_flow_info *flow = *(struct ndpi_flow_info **) node;
|
||||
u_int16_t thread_id = *((u_int16_t *) user_data);
|
||||
|
||||
@@ -1386,7 +1401,6 @@ static void node_idle_scan_walker(const void *node, ndpi_VISIT which, int depth,
|
||||
static void on_protocol_discovered(struct ndpi_workflow * workflow,
|
||||
struct ndpi_flow_info * flow,
|
||||
void * udata) {
|
||||
|
||||
const u_int16_t thread_id = (uintptr_t) udata;
|
||||
|
||||
if(verbose > 1) {
|
||||
@@ -1446,7 +1460,6 @@ static void debug_printf(u_int32_t protocol, void *id_struct,
|
||||
* @brief Setup for detection begin
|
||||
*/
|
||||
static void setupDetection(u_int16_t thread_id, pcap_t * pcap_handle) {
|
||||
|
||||
NDPI_PROTOCOL_BITMASK all;
|
||||
struct ndpi_workflow_prefs prefs;
|
||||
|
||||
@@ -1468,16 +1481,20 @@ static void setupDetection(u_int16_t thread_id, pcap_t * pcap_handle) {
|
||||
ndpi_pref_enable_category_substring_match, 1);
|
||||
|
||||
ndpi_workflow_set_flow_detected_callback(ndpi_thread_info[thread_id].workflow,
|
||||
on_protocol_discovered, (void *)(uintptr_t)thread_id);
|
||||
on_protocol_discovered,
|
||||
(void *)(uintptr_t)thread_id);
|
||||
|
||||
// enable all protocols
|
||||
NDPI_BITMASK_SET_ALL(all);
|
||||
ndpi_set_protocol_detection_bitmask2(ndpi_thread_info[thread_id].workflow->ndpi_struct, &all);
|
||||
|
||||
// clear memory for results
|
||||
memset(ndpi_thread_info[thread_id].workflow->stats.protocol_counter, 0, sizeof(ndpi_thread_info[thread_id].workflow->stats.protocol_counter));
|
||||
memset(ndpi_thread_info[thread_id].workflow->stats.protocol_counter_bytes, 0, sizeof(ndpi_thread_info[thread_id].workflow->stats.protocol_counter_bytes));
|
||||
memset(ndpi_thread_info[thread_id].workflow->stats.protocol_flows, 0, sizeof(ndpi_thread_info[thread_id].workflow->stats.protocol_flows));
|
||||
memset(ndpi_thread_info[thread_id].workflow->stats.protocol_counter, 0,
|
||||
sizeof(ndpi_thread_info[thread_id].workflow->stats.protocol_counter));
|
||||
memset(ndpi_thread_info[thread_id].workflow->stats.protocol_counter_bytes, 0,
|
||||
sizeof(ndpi_thread_info[thread_id].workflow->stats.protocol_counter_bytes));
|
||||
memset(ndpi_thread_info[thread_id].workflow->stats.protocol_flows, 0,
|
||||
sizeof(ndpi_thread_info[thread_id].workflow->stats.protocol_flows));
|
||||
|
||||
if(_protoFilePath != NULL)
|
||||
ndpi_load_protocols_file(ndpi_thread_info[thread_id].workflow->ndpi_struct, _protoFilePath);
|
||||
@@ -1608,7 +1625,9 @@ static void json_open_stats_file() {
|
||||
|
||||
static void json_close_stats_file() {
|
||||
json_object *jObjFinal = json_object_new_object();
|
||||
json_object_object_add(jObjFinal,"duration.in.seconds",json_object_new_int(pcap_analysis_duration));
|
||||
|
||||
json_object_object_add(jObjFinal,"duration.in.seconds",
|
||||
json_object_new_int(pcap_analysis_duration));
|
||||
json_object_object_add(jObjFinal,"statistics", jArray_topStats);
|
||||
fprintf(stats_fp,"%s\n",json_object_to_json_string(jObjFinal));
|
||||
fclose(stats_fp);
|
||||
@@ -1910,8 +1929,10 @@ static void printResults(u_int64_t tot_usec) {
|
||||
continue;
|
||||
|
||||
for(i=0; i<NUM_ROOTS; i++) {
|
||||
ndpi_twalk(ndpi_thread_info[thread_id].workflow->ndpi_flows_root[i], node_proto_guess_walker, &thread_id);
|
||||
if(verbose == 3 || stats_flag) ndpi_twalk(ndpi_thread_info[thread_id].workflow->ndpi_flows_root[i], port_stats_walker, &thread_id);
|
||||
ndpi_twalk(ndpi_thread_info[thread_id].workflow->ndpi_flows_root[i],
|
||||
node_proto_guess_walker, &thread_id);
|
||||
if(verbose == 3 || stats_flag) ndpi_twalk(ndpi_thread_info[thread_id].workflow->ndpi_flows_root[i],
|
||||
port_stats_walker, &thread_id);
|
||||
}
|
||||
|
||||
/* Stats aggregation */
|
||||
@@ -2396,11 +2417,11 @@ static void pcap_process_packet(u_char *args,
|
||||
if(live_capture) {
|
||||
if(ndpi_thread_info[thread_id].last_idle_scan_time + IDLE_SCAN_PERIOD < ndpi_thread_info[thread_id].workflow->last_time) {
|
||||
/* scan for idle flows */
|
||||
ndpi_twalk(ndpi_thread_info[thread_id].workflow->ndpi_flows_root[ndpi_thread_info[thread_id].idle_scan_idx], node_idle_scan_walker, &thread_id);
|
||||
ndpi_twalk(ndpi_thread_info[thread_id].workflow->ndpi_flows_root[ndpi_thread_info[thread_id].idle_scan_idx],
|
||||
node_idle_scan_walker, &thread_id);
|
||||
|
||||
/* remove idle flows (unfortunately we cannot do this inline) */
|
||||
while (ndpi_thread_info[thread_id].num_idle_flows > 0) {
|
||||
|
||||
while(ndpi_thread_info[thread_id].num_idle_flows > 0) {
|
||||
/* search and delete the idle flow from the "ndpi_flow_root" (see struct reader thread) - here flows are the node of a b-tree */
|
||||
ndpi_tdelete(ndpi_thread_info[thread_id].idle_flows[--ndpi_thread_info[thread_id].num_idle_flows],
|
||||
&ndpi_thread_info[thread_id].workflow->ndpi_flows_root[ndpi_thread_info[thread_id].idle_scan_idx],
|
||||
|
||||
Reference in New Issue
Block a user