Minor changes in ndpi_detection_giveup() (#1861)

Check the mining cache at the end of the function, like all the others
LRU caches.
Rewrite the STUN checks: same identical semantic but in a cleaner way,
hopefully
This commit is contained in:
Ivan Nardi
2023-01-19 21:45:35 +01:00
committed by GitHub
parent f5c2f9280c
commit c4274a4aec
3 changed files with 41 additions and 53 deletions
+39 -51
View File
@@ -6000,6 +6000,7 @@ static void ndpi_add_connection_as_zoom(struct ndpi_detection_module_struct *ndp
ndpi_protocol ndpi_detection_giveup(struct ndpi_detection_module_struct *ndpi_str, struct ndpi_flow_struct *flow, ndpi_protocol ndpi_detection_giveup(struct ndpi_detection_module_struct *ndpi_str, struct ndpi_flow_struct *flow,
u_int8_t enable_guess, u_int8_t *protocol_was_guessed) { u_int8_t enable_guess, u_int8_t *protocol_was_guessed) {
ndpi_protocol ret = NDPI_PROTOCOL_NULL; ndpi_protocol ret = NDPI_PROTOCOL_NULL;
u_int16_t cached_proto;
/* *** We can't access ndpi_str->packet from this function!! *** */ /* *** We can't access ndpi_str->packet from this function!! *** */
@@ -6009,7 +6010,8 @@ ndpi_protocol ndpi_detection_giveup(struct ndpi_detection_module_struct *ndpi_st
return(ret); return(ret);
/* Init defaults */ /* Init defaults */
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];
ret.protocol_by_ip = flow->guessed_protocol_id_by_ip; ret.protocol_by_ip = flow->guessed_protocol_id_by_ip;
ret.category = flow->category; ret.category = flow->category;
@@ -6017,60 +6019,46 @@ ndpi_protocol ndpi_detection_giveup(struct ndpi_detection_module_struct *ndpi_st
if(ret.app_protocol != NDPI_PROTOCOL_UNKNOWN) if(ret.app_protocol != NDPI_PROTOCOL_UNKNOWN)
return(ret); return(ret);
/* TODO: this lookup seems in the wrong place here... if((flow->guessed_protocol_id == NDPI_PROTOCOL_STUN) ||
Move it somewhere else (?) or setting flow->guessed_protocol_id directly in the mining dissector? */ (enable_guess &&
if(ndpi_str->mining_cache) { flow->stun.num_binding_requests > 0 &&
u_int16_t cached_proto; flow->stun.num_processed_pkts > 0)) {
if(ndpi_lru_find_cache(ndpi_str->mining_cache, make_mining_key(flow),
&cached_proto, 0 /* Don't remove it as it can be used for other connections */,
ndpi_get_current_time(flow))) {
ndpi_set_detected_protocol(ndpi_str, flow, cached_proto, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI_PARTIAL_CACHE);
ret.master_protocol = flow->detected_protocol_stack[1], ret.app_protocol = flow->detected_protocol_stack[0];
ndpi_fill_protocol_category(ndpi_str, flow, &ret);
return(ret);
}
}
if(flow->guessed_protocol_id == NDPI_PROTOCOL_STUN)
goto check_stun_export;
else if(enable_guess) {
if(flow->stun.num_binding_requests > 0 &&
flow->stun.num_processed_pkts > 0) {
*protocol_was_guessed = 1;
ndpi_set_detected_protocol(ndpi_str, flow, NDPI_PROTOCOL_STUN, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI_PARTIAL);
}
}
if((flow->detected_protocol_stack[0] == NDPI_PROTOCOL_UNKNOWN) &&
(flow->guessed_protocol_id == NDPI_PROTOCOL_STUN)) {
check_stun_export:
*protocol_was_guessed = 1;
ndpi_set_detected_protocol(ndpi_str, flow, NDPI_PROTOCOL_STUN, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI_PARTIAL); ndpi_set_detected_protocol(ndpi_str, flow, NDPI_PROTOCOL_STUN, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI_PARTIAL);
ret.app_protocol = flow->detected_protocol_stack[0];
} }
ret.master_protocol = flow->detected_protocol_stack[1], ret.app_protocol = flow->detected_protocol_stack[0]; /* Check some caches */
if((ret.master_protocol == NDPI_PROTOCOL_UNKNOWN) /* Does it looks like BitTorrent? */
&& (ret.app_protocol == NDPI_PROTOCOL_UNKNOWN)) { if(ret.app_protocol == NDPI_PROTOCOL_UNKNOWN &&
/* Check some caches */ ndpi_search_into_bittorrent_cache(ndpi_str, flow,
if(ndpi_search_into_bittorrent_cache(ndpi_str, flow, flow->c_address.v4, flow->c_port,
flow->c_address.v4, flow->c_port, flow->s_address.v4, flow->s_port)) {
flow->s_address.v4, flow->s_port)) { ndpi_set_detected_protocol(ndpi_str, flow, NDPI_PROTOCOL_BITTORRENT, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI_PARTIAL_CACHE);
/* This looks like BitTorrent */ ret.app_protocol = flow->detected_protocol_stack[0];
ndpi_set_detected_protocol(ndpi_str, flow, NDPI_PROTOCOL_BITTORRENT, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI_PARTIAL_CACHE); }
ret.app_protocol = NDPI_PROTOCOL_BITTORRENT; /* Does it looks like some Mining protocols? */
} else if((flow->l4_proto == IPPROTO_UDP) && /* Zoom/UDP used for video */ if(ret.app_protocol == NDPI_PROTOCOL_UNKNOWN &&
((ntohs(flow->s_port) == 8801 && ndpi_search_into_zoom_cache(ndpi_str, flow, 1)) || ndpi_str->mining_cache &&
(ntohs(flow->c_port) == 8801 && ndpi_search_into_zoom_cache(ndpi_str, flow, 0)))) { ndpi_lru_find_cache(ndpi_str->mining_cache, make_mining_key(flow),
/* This looks like Zoom */ &cached_proto, 0 /* Don't remove it as it can be used for other connections */,
ndpi_set_detected_protocol(ndpi_str, flow, NDPI_PROTOCOL_ZOOM, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI_PARTIAL_CACHE); ndpi_get_current_time(flow))) {
ret.app_protocol = NDPI_PROTOCOL_ZOOM; ndpi_set_detected_protocol(ndpi_str, flow, cached_proto, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI_PARTIAL_CACHE);
} else if(stun_search_into_zoom_cache(ndpi_str, flow)) { ret.app_protocol = flow->detected_protocol_stack[0];
/* This looks like Zoom */ }
ndpi_set_detected_protocol(ndpi_str, flow, NDPI_PROTOCOL_ZOOM, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI_PARTIAL_CACHE); /* Does it looks like Zoom? */
ret.app_protocol = flow->detected_protocol_stack[0]; if(ret.app_protocol == NDPI_PROTOCOL_UNKNOWN &&
} flow->l4_proto == IPPROTO_UDP && /* Zoom/UDP used for video */
((ntohs(flow->s_port) == 8801 && ndpi_search_into_zoom_cache(ndpi_str, flow, 1)) ||
(ntohs(flow->c_port) == 8801 && ndpi_search_into_zoom_cache(ndpi_str, flow, 0)))) {
ndpi_set_detected_protocol(ndpi_str, flow, NDPI_PROTOCOL_ZOOM, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI_PARTIAL_CACHE);
ret.app_protocol = flow->detected_protocol_stack[0];
}
/* Does it looks like Zoom (via STUN)? */
if(ret.app_protocol == NDPI_PROTOCOL_UNKNOWN &&
stun_search_into_zoom_cache(ndpi_str, flow)) {
ndpi_set_detected_protocol(ndpi_str, flow, NDPI_PROTOCOL_ZOOM, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI_PARTIAL_CACHE);
ret.app_protocol = flow->detected_protocol_stack[0];
} }
/* Classification by-port is the last resort */ /* Classification by-port is the last resort */
+1 -1
View File
@@ -10,7 +10,7 @@ LRU cache bittorrent: 0/24/0 (insert/search/found)
LRU cache zoom: 0/0/0 (insert/search/found) LRU cache zoom: 0/0/0 (insert/search/found)
LRU cache stun: 6/164/24 (insert/search/found) LRU cache stun: 6/164/24 (insert/search/found)
LRU cache tls_cert: 0/0/0 (insert/search/found) LRU cache tls_cert: 0/0/0 (insert/search/found)
LRU cache mining: 0/1/0 (insert/search/found) LRU cache mining: 0/0/0 (insert/search/found)
LRU cache msteams: 0/0/0 (insert/search/found) LRU cache msteams: 0/0/0 (insert/search/found)
LRU cache stun_zoom: 0/0/0 (insert/search/found) LRU cache stun_zoom: 0/0/0 (insert/search/found)
Automa host: 0/0 (search/found) Automa host: 0/0 (search/found)
+1 -1
View File
@@ -13,7 +13,7 @@ LRU cache bittorrent: 0/9/0 (insert/search/found)
LRU cache zoom: 0/0/0 (insert/search/found) LRU cache zoom: 0/0/0 (insert/search/found)
LRU cache stun: 6/42/18 (insert/search/found) LRU cache stun: 6/42/18 (insert/search/found)
LRU cache tls_cert: 0/8/0 (insert/search/found) LRU cache tls_cert: 0/8/0 (insert/search/found)
LRU cache mining: 0/4/0 (insert/search/found) LRU cache mining: 0/3/0 (insert/search/found)
LRU cache msteams: 24/13/9 (insert/search/found) LRU cache msteams: 24/13/9 (insert/search/found)
LRU cache stun_zoom: 0/0/0 (insert/search/found) LRU cache stun_zoom: 0/0/0 (insert/search/found)
Automa host: 85/71 (search/found) Automa host: 85/71 (search/found)