Fixed bug that was forcing the reader to pass several times the same flow

renamed some variables
This commit is contained in:
Luca
2017-05-24 18:53:51 +02:00
parent 904210e87c
commit 993605d4f3
3 changed files with 54 additions and 51 deletions
+21 -18
View File
@@ -643,10 +643,10 @@ static void printFlow(u_int16_t thread_id, struct ndpi_flow_info *flow) {
fprintf(out, "%s%s%s:%u %s %s%s%s:%u ", fprintf(out, "%s%s%s:%u %s %s%s%s:%u ",
(flow->ip_version == 6) ? "[" : "", (flow->ip_version == 6) ? "[" : "",
flow->lower_name, (flow->ip_version == 6) ? "]" : "", ntohs(flow->lower_port), flow->src_name, (flow->ip_version == 6) ? "]" : "", ntohs(flow->src_port),
flow->bidirectional ? "<->" : "->", flow->bidirectional ? "<->" : "->",
(flow->ip_version == 6) ? "[" : "", (flow->ip_version == 6) ? "[" : "",
flow->upper_name, (flow->ip_version == 6) ? "]" : "", ntohs(flow->upper_port) flow->dst_name, (flow->ip_version == 6) ? "]" : "", ntohs(flow->dst_port)
); );
if(flow->vlan_id > 0) fprintf(out, "[VLAN: %u]", flow->vlan_id); if(flow->vlan_id > 0) fprintf(out, "[VLAN: %u]", flow->vlan_id);
@@ -681,10 +681,10 @@ static void printFlow(u_int16_t thread_id, struct ndpi_flow_info *flow) {
jObj = json_object_new_object(); jObj = json_object_new_object();
json_object_object_add(jObj,"protocol",json_object_new_string(ipProto2Name(flow->protocol))); json_object_object_add(jObj,"protocol",json_object_new_string(ipProto2Name(flow->protocol)));
json_object_object_add(jObj,"host_a.name",json_object_new_string(flow->lower_name)); json_object_object_add(jObj,"host_a.name",json_object_new_string(flow->src_name));
json_object_object_add(jObj,"host_a.port",json_object_new_int(ntohs(flow->lower_port))); json_object_object_add(jObj,"host_a.port",json_object_new_int(ntohs(flow->src_port)));
json_object_object_add(jObj,"host_b.name",json_object_new_string(flow->upper_name)); json_object_object_add(jObj,"host_b.name",json_object_new_string(flow->dst_name));
json_object_object_add(jObj,"host_b.port",json_object_new_int(ntohs(flow->upper_port))); json_object_object_add(jObj,"host_b.port",json_object_new_int(ntohs(flow->dst_port)));
if(flow->detected_protocol.master_protocol) 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));
@@ -768,10 +768,10 @@ static u_int16_t node_guess_undetected_protocol(u_int16_t thread_id, struct ndpi
flow->detected_protocol = ndpi_guess_undetected_protocol(ndpi_thread_info[thread_id].workflow->ndpi_struct, flow->detected_protocol = ndpi_guess_undetected_protocol(ndpi_thread_info[thread_id].workflow->ndpi_struct,
flow->protocol, flow->protocol,
ntohl(flow->lower_ip), ntohl(flow->src_ip),
ntohs(flow->lower_port), ntohs(flow->src_port),
ntohl(flow->upper_ip), ntohl(flow->dst_ip),
ntohs(flow->upper_port)); ntohs(flow->dst_port));
// printf("Guess state: %u\n", flow->detected_protocol); // printf("Guess state: %u\n", flow->detected_protocol);
if(flow->detected_protocol.app_protocol != NDPI_PROTOCOL_UNKNOWN) if(flow->detected_protocol.app_protocol != NDPI_PROTOCOL_UNKNOWN)
ndpi_thread_info[thread_id].workflow->stats.guessed_flow_protocols++; ndpi_thread_info[thread_id].workflow->stats.guessed_flow_protocols++;
@@ -806,6 +806,7 @@ static void node_proto_guess_walker(const void *node, ndpi_VISIT which, int dept
} }
/* *********************************************** */ /* *********************************************** */
int updateIpTree(const char *key, addr_node **vrootp) { int updateIpTree(const char *key, addr_node **vrootp) {
addr_node *q; addr_node *q;
addr_node **rootp = vrootp; addr_node **rootp = vrootp;
@@ -962,16 +963,18 @@ static void deletePortsStats(struct port_stats *stats) {
* @brief Ports stats * @brief Ports stats
*/ */
static void port_stats_walker(const void *node, ndpi_VISIT which, int depth, void *user_data) { static void port_stats_walker(const void *node, ndpi_VISIT which, int depth, void *user_data) {
struct ndpi_flow_info *flow = *(struct ndpi_flow_info **) node; if((which == ndpi_preorder) || (which == ndpi_leaf)) { /* Avoid walking the same node multiple times */
u_int16_t sport, dport; struct ndpi_flow_info *flow = *(struct ndpi_flow_info **) node;
char saddr[48], daddr[48]; u_int16_t sport, dport;
char saddr[48], daddr[48];
sport = ntohs(flow->lower_port), dport = ntohs(flow->upper_port); sport = ntohs(flow->src_port), dport = ntohs(flow->dst_port);
strncpy(saddr, flow->lower_name, sizeof(saddr)); strncpy(saddr, flow->src_name, sizeof(saddr));
strncpy(daddr, flow->upper_name, sizeof(daddr)); strncpy(daddr, flow->dst_name, sizeof(daddr));
updatePortStats(&srcStats, sport, saddr, flow->src2dst_packets, flow->src2dst_bytes); updatePortStats(&srcStats, sport, saddr, flow->src2dst_packets, flow->src2dst_bytes);
if(flow->dst2src_packets > 0) updatePortStats(&dstStats, dport, daddr, flow->dst2src_packets, flow->dst2src_bytes); if(flow->dst2src_packets > 0) updatePortStats(&dstStats, dport, daddr, flow->dst2src_packets, flow->dst2src_bytes);
}
} }
/* *********************************************** */ /* *********************************************** */
+26 -26
View File
@@ -164,25 +164,25 @@ int ndpi_workflow_node_cmp(const void *a, const void *b) {
if( if(
( (
(fa->lower_ip == fb->lower_ip ) (fa->src_ip == fb->src_ip )
&& (fa->lower_port == fb->lower_port) && (fa->src_port == fb->src_port)
&& (fa->upper_ip == fb->upper_ip ) && (fa->dst_ip == fb->dst_ip )
&& (fa->upper_port == fb->upper_port) && (fa->dst_port == fb->dst_port)
) )
|| ||
( (
(fa->lower_ip == fb->upper_ip ) (fa->src_ip == fb->dst_ip )
&& (fa->lower_port == fb->upper_port) && (fa->src_port == fb->dst_port)
&& (fa->upper_ip == fb->lower_ip ) && (fa->dst_ip == fb->src_ip )
&& (fa->upper_port == fb->lower_port) && (fa->dst_port == fb->src_port)
) )
) )
return(0); return(0);
if(fa->lower_ip < fb->lower_ip ) return(-1); else { if(fa->lower_ip > fb->lower_ip ) return(1); } if(fa->src_ip < fb->src_ip ) return(-1); else { if(fa->src_ip > fb->src_ip ) return(1); }
if(fa->lower_port < fb->lower_port) return(-1); else { if(fa->lower_port > fb->lower_port) return(1); } if(fa->src_port < fb->src_port) return(-1); else { if(fa->src_port > fb->src_port) return(1); }
if(fa->upper_ip < fb->upper_ip ) return(-1); else { if(fa->upper_ip > fb->upper_ip ) return(1); } if(fa->dst_ip < fb->dst_ip ) return(-1); else { if(fa->dst_ip > fb->dst_ip ) return(1); }
if(fa->upper_port < fb->upper_port) return(-1); else { if(fa->upper_port > fb->upper_port) return(1); } if(fa->dst_port < fb->dst_port) return(-1); else { if(fa->dst_port > fb->dst_port) return(1); }
return(0); /* notreached */ return(0); /* notreached */
} }
@@ -291,9 +291,9 @@ static struct ndpi_flow_info *get_ndpi_flow_info(struct ndpi_workflow * workflow
} }
flow.protocol = iph->protocol, flow.vlan_id = vlan_id; flow.protocol = iph->protocol, flow.vlan_id = vlan_id;
flow.lower_ip = iph->saddr, flow.upper_ip = iph->daddr; flow.src_ip = iph->saddr, flow.dst_ip = iph->daddr;
flow.lower_port = htons(*sport), flow.upper_port = htons(*dport); flow.src_port = htons(*sport), flow.dst_port = htons(*dport);
flow.hashval = hashval = flow.protocol + flow.vlan_id + flow.lower_ip + flow.upper_ip + flow.lower_port + flow.upper_port; flow.hashval = hashval = flow.protocol + flow.vlan_id + flow.src_ip + flow.dst_ip + flow.src_port + flow.dst_port;
idx = hashval % workflow->prefs.num_roots; idx = hashval % workflow->prefs.num_roots;
ret = ndpi_tfind(&flow, &workflow->ndpi_flows_root[idx], ndpi_workflow_node_cmp); ret = ndpi_tfind(&flow, &workflow->ndpi_flows_root[idx], ndpi_workflow_node_cmp);
@@ -314,18 +314,18 @@ static struct ndpi_flow_info *get_ndpi_flow_info(struct ndpi_workflow * workflow
memset(newflow, 0, sizeof(struct ndpi_flow_info)); memset(newflow, 0, sizeof(struct ndpi_flow_info));
newflow->hashval = hashval; newflow->hashval = hashval;
newflow->protocol = iph->protocol, newflow->vlan_id = vlan_id; newflow->protocol = iph->protocol, newflow->vlan_id = vlan_id;
newflow->lower_ip = iph->saddr, newflow->upper_ip = iph->daddr; newflow->src_ip = iph->saddr, newflow->dst_ip = iph->daddr;
newflow->lower_port = htons(*sport), newflow->upper_port = htons(*dport); newflow->src_port = htons(*sport), newflow->dst_port = htons(*dport);
newflow->ip_version = version; newflow->ip_version = version;
if(version == IPVERSION) { if(version == IPVERSION) {
inet_ntop(AF_INET, &newflow->lower_ip, newflow->lower_name, sizeof(newflow->lower_name)); inet_ntop(AF_INET, &newflow->src_ip, newflow->src_name, sizeof(newflow->src_name));
inet_ntop(AF_INET, &newflow->upper_ip, newflow->upper_name, sizeof(newflow->upper_name)); inet_ntop(AF_INET, &newflow->dst_ip, newflow->dst_name, sizeof(newflow->dst_name));
} else { } else {
inet_ntop(AF_INET6, &iph6->ip6_src, newflow->lower_name, sizeof(newflow->lower_name)); inet_ntop(AF_INET6, &iph6->ip6_src, newflow->src_name, sizeof(newflow->src_name));
inet_ntop(AF_INET6, &iph6->ip6_dst, newflow->upper_name, sizeof(newflow->upper_name)); inet_ntop(AF_INET6, &iph6->ip6_dst, newflow->dst_name, sizeof(newflow->dst_name));
/* For consistency across platforms replace :0: with :: */ /* For consistency across platforms replace :0: with :: */
patchIPv6Address(newflow->lower_name), patchIPv6Address(newflow->upper_name); patchIPv6Address(newflow->src_name), patchIPv6Address(newflow->dst_name);
} }
if((newflow->ndpi_flow = ndpi_flow_malloc(SIZEOF_FLOW_STRUCT)) == NULL) { if((newflow->ndpi_flow = ndpi_flow_malloc(SIZEOF_FLOW_STRUCT)) == NULL) {
@@ -359,10 +359,10 @@ 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(flow->lower_ip == iph->saddr if(flow->src_ip == iph->saddr
&& flow->upper_ip == iph->daddr && flow->dst_ip == iph->daddr
&& flow->lower_port == htons(*sport) && flow->src_port == htons(*sport)
&& flow->upper_port == htons(*dport) && flow->dst_port == htons(*dport)
) )
*src = flow->src_id, *dst = flow->dst_id, *src_to_dst_direction = 1; *src = flow->src_id, *dst = flow->dst_id, *src_to_dst_direction = 1;
else else
+5 -5
View File
@@ -47,14 +47,14 @@
// flow tracking // flow tracking
typedef struct ndpi_flow_info { typedef struct ndpi_flow_info {
u_int32_t hashval; u_int32_t hashval;
u_int32_t lower_ip; u_int32_t src_ip;
u_int32_t upper_ip; u_int32_t dst_ip;
u_int16_t lower_port; u_int16_t src_port;
u_int16_t upper_port; u_int16_t dst_port;
u_int8_t detection_completed, protocol, bidirectional; u_int8_t detection_completed, protocol, bidirectional;
u_int16_t vlan_id; u_int16_t vlan_id;
struct ndpi_flow_struct *ndpi_flow; struct ndpi_flow_struct *ndpi_flow;
char lower_name[48], upper_name[48]; char src_name[48], dst_name[48];
u_int8_t ip_version; u_int8_t ip_version;
u_int64_t last_seen; u_int64_t last_seen;
u_int64_t src2dst_bytes, dst2src_bytes; u_int64_t src2dst_bytes, dst2src_bytes;