diff options
author | Ivan Nardi <12729895+IvanNardi@users.noreply.github.com> | 2025-06-03 09:45:46 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-06-03 09:45:46 +0200 |
commit | 70a72f163800dd37dca1ec586ae0a58a6cef8206 (patch) | |
tree | 42e08401cc3ec36d6c83961ed6894c698559eedf /example/ndpiReader.c | |
parent | 40fe26b2f165b6a42d07bf53671c99c85e2d243d (diff) |
New API to enable/disable protocols; remove `ndpi_set_protocol_detection_bitmask2()` (#2853)
The main goal is not to have the bitmask depending on the total number
of protocols anymore: `NDPI_INTERNAL_PROTOCOL_BITMASK` depends only on
internal protocols, i.e. on `NDPI_MAX_INTERNAL_PROTOCOLS`, i.e.
custom-defined protocols are not counted.
See #2136
Keep the old data structure `NDPI_PROTOCOL_BITMASK` with the old
semantic.
Since we need to change the API (and all the application code...)
anyway, simplify the API: by default all the protocols are enabled.
If you need otherwise, please use `ndpi_init_detection_module_ext()`
instead of `ndpi_init_detection_module()` (you can find an example in
the `ndpiReader` code).
To update the application code you likely only need to remove these 3
lines from your code:
```
- NDPI_PROTOCOL_BITMASK all;
- NDPI_BITMASK_SET_ALL(all);
- ndpi_set_protocol_detection_bitmask2(ndpi_str, &all);
```
Removed an unused field and struct definition.
Diffstat (limited to 'example/ndpiReader.c')
-rw-r--r-- | example/ndpiReader.c | 56 |
1 files changed, 7 insertions, 49 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c index 909cc3a67..290a55f91 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -302,7 +302,7 @@ static int dpdk_port_id = 0, dpdk_run_capture = 1; void test_lib(); /* Forward */ extern void ndpi_report_payload_stats(FILE *out); -extern int parse_proto_name_list(char *str, NDPI_PROTOCOL_BITMASK *bitmask, +extern int parse_proto_name_list(char *str, NDPI_INTERNAL_PROTOCOL_BITMASK *bitmask, int inverted_logic); extern u_int8_t is_ndpi_proto(struct ndpi_flow_info *flow, u_int16_t id); @@ -407,14 +407,11 @@ void ndpiCheckHostStringMatch(char *testChar) { char appBufStr[64]; ndpi_protocol detected_protocol; struct ndpi_detection_module_struct *ndpi_str; - NDPI_PROTOCOL_BITMASK all; if(!testChar) return; ndpi_str = ndpi_init_detection_module(NULL); - NDPI_BITMASK_SET_ALL(all); - ndpi_set_protocol_detection_bitmask2(ndpi_str, &all); ndpi_finalize_initialization(ndpi_str); testRes = ndpi_match_string_subprotocol(ndpi_str, @@ -476,14 +473,11 @@ static void ndpiCheckIPMatch(char *testChar) { ndpi_protocol detected_protocol; int i; ndpi_cfg_error rc; - NDPI_PROTOCOL_BITMASK all; if(!testChar) return; ndpi_str = ndpi_init_detection_module(NULL); - NDPI_BITMASK_SET_ALL(all); - ndpi_set_protocol_detection_bitmask2(ndpi_str, &all); if(_protoFilePath != NULL) ndpi_load_protocols_file(ndpi_str, _protoFilePath); @@ -727,12 +721,8 @@ static void help(u_int long_help) { min_pattern_len, max_pattern_len, max_num_packets_per_flow, max_packet_payload_dissection, max_num_reported_top_payloads, max_num_tcp_dissected_pkts, max_num_udp_dissected_pkts); - NDPI_PROTOCOL_BITMASK all; struct ndpi_detection_module_struct *ndpi_str = ndpi_init_detection_module(NULL); - NDPI_BITMASK_SET_ALL(all); - ndpi_set_protocol_detection_bitmask2(ndpi_str, &all); - if(_protoFilePath != NULL) ndpi_load_protocols_file(ndpi_str, _protoFilePath); @@ -905,14 +895,10 @@ void extcap_config() { u_int ndpi_num_supported_protocols; int i; ndpi_proto_defaults_t *proto_defaults; - NDPI_PROTOCOL_BITMASK all; struct ndpi_detection_module_struct *ndpi_str = ndpi_init_detection_module(NULL); if(!ndpi_str) exit(0); - NDPI_BITMASK_SET_ALL(all); - ndpi_set_protocol_detection_bitmask2(ndpi_str, &all); - ndpi_finalize_initialization(ndpi_str); ndpi_num_supported_protocols = ndpi_get_ndpi_num_supported_protocols(ndpi_str); @@ -1495,10 +1481,7 @@ static void parse_parameters(int argc, char **argv) case '9': { struct ndpi_detection_module_struct *ndpi_str = ndpi_init_detection_module(NULL); - NDPI_PROTOCOL_BITMASK all; - NDPI_BITMASK_SET_ALL(all); - ndpi_set_protocol_detection_bitmask2(ndpi_str, &all); ndpi_finalize_initialization(ndpi_str); extcap_packet_filter = ndpi_get_proto_by_name(ndpi_str, optarg); @@ -3017,7 +3000,7 @@ static void on_protocol_discovered(struct ndpi_workflow * workflow, */ static void setupDetection(u_int16_t thread_id, pcap_t * pcap_handle, struct ndpi_global_context *g_ctx) { - NDPI_PROTOCOL_BITMASK enabled_bitmask; + NDPI_INTERNAL_PROTOCOL_BITMASK enabled_bitmask; struct ndpi_workflow_prefs prefs; int i, ret; ndpi_cfg_error rc; @@ -3029,17 +3012,17 @@ static void setupDetection(u_int16_t thread_id, pcap_t * pcap_handle, prefs.quiet_mode = quiet_mode; prefs.ignore_vlanid = ignore_vlanid; - memset(&ndpi_thread_info[thread_id], 0, sizeof(ndpi_thread_info[thread_id])); - ndpi_thread_info[thread_id].workflow = ndpi_workflow_init(&prefs, pcap_handle, 1, - serialization_format, g_ctx); - /* Protocols to enable/disable. Default: everything is enabled */ - NDPI_BITMASK_SET_ALL(enabled_bitmask); + NDPI_INTERNAL_PROTOCOL_SET_ALL(enabled_bitmask); if(_disabled_protocols != NULL) { if(parse_proto_name_list(_disabled_protocols, &enabled_bitmask, 1)) exit(-1); } + memset(&ndpi_thread_info[thread_id], 0, sizeof(ndpi_thread_info[thread_id])); + ndpi_thread_info[thread_id].workflow = ndpi_workflow_init(&prefs, pcap_handle, 1, + serialization_format, g_ctx, &enabled_bitmask); + if(_categoriesDirPath) { int failed_files = ndpi_load_categories_dir(ndpi_thread_info[thread_id].workflow->ndpi_struct, _categoriesDirPath); if (failed_files < 0) { @@ -3080,9 +3063,6 @@ static void setupDetection(u_int16_t thread_id, pcap_t * pcap_handle, ndpi_workflow_set_flow_callback(ndpi_thread_info[thread_id].workflow, on_protocol_discovered, NULL); - /* Make sure to load lists before finalizing the initialization */ - ndpi_set_protocol_detection_bitmask2(ndpi_thread_info[thread_id].workflow->ndpi_struct, &enabled_bitmask); - if(_protoFilePath != NULL) ndpi_load_protocols_file(ndpi_thread_info[thread_id].workflow->ndpi_struct, _protoFilePath); @@ -5228,14 +5208,10 @@ static void dgaUnitTest() { NULL }; int debug = 0, i; - NDPI_PROTOCOL_BITMASK all; struct ndpi_detection_module_struct *ndpi_str = ndpi_init_detection_module(NULL); assert(ndpi_str != NULL); - NDPI_BITMASK_SET_ALL(all); - ndpi_set_protocol_detection_bitmask2(ndpi_str, &all); - ndpi_finalize_initialization(ndpi_str); assert(ndpi_str != NULL); @@ -6392,14 +6368,10 @@ void outlierUnitTest() { void loadStressTest() { struct ndpi_detection_module_struct *ndpi_struct_shadow = ndpi_init_detection_module(NULL); - NDPI_PROTOCOL_BITMASK all; if(ndpi_struct_shadow) { int i; - NDPI_BITMASK_SET_ALL(all); - ndpi_set_protocol_detection_bitmask2(ndpi_struct_shadow, &all); - for(i=1; i<100000; i++) { char name[32]; ndpi_protocol_category_t id = CUSTOM_CATEGORY_MALWARE; @@ -6526,7 +6498,6 @@ void cryptDecryptUnitTest() { /* *********************************************** */ void encodeDomainsUnitTest() { - NDPI_PROTOCOL_BITMASK all; struct ndpi_detection_module_struct *ndpi_str = ndpi_init_detection_module(NULL); const char *lists_path = "../lists/public_suffix_list.dat"; struct stat st; @@ -6537,9 +6508,6 @@ void encodeDomainsUnitTest() { char *str; ndpi_protocol_category_t id; - NDPI_BITMASK_SET_ALL(all); - ndpi_set_protocol_detection_bitmask2(ndpi_str, &all); - assert(ndpi_load_domain_suffixes(ndpi_str, (char*)lists_path) == 0); ndpi_get_host_domain_suffix(ndpi_str, "lcb.it", &suffix_id); @@ -6579,7 +6547,6 @@ void checkProtocolIDsUnitTest() { /* *********************************************** */ void domainsUnitTest() { - NDPI_PROTOCOL_BITMASK all; struct ndpi_detection_module_struct *ndpi_str = ndpi_init_detection_module(NULL); const char *lists_path = "../lists/public_suffix_list.dat"; struct stat st; @@ -6587,9 +6554,6 @@ void domainsUnitTest() { if(stat(lists_path, &st) == 0) { u_int16_t suffix_id; - NDPI_BITMASK_SET_ALL(all); - ndpi_set_protocol_detection_bitmask2(ndpi_str, &all); - assert(ndpi_load_domain_suffixes(ndpi_str, (char*)lists_path) == 0); assert(strcmp(ndpi_get_host_domain(ndpi_str, "1.0.0.127.in-addr.arpa"), "in-addr.arpa") == 0); @@ -6621,13 +6585,10 @@ void domainSearchUnitTest() { u_int16_t class_id; struct ndpi_detection_module_struct *ndpi_str = ndpi_init_detection_module(NULL); u_int8_t trace = 0; - NDPI_PROTOCOL_BITMASK all; assert(ndpi_str); assert(sc); - NDPI_BITMASK_SET_ALL(all); - ndpi_set_protocol_detection_bitmask2(ndpi_str, &all); ndpi_finalize_initialization(ndpi_str); ndpi_domain_classify_add(ndpi_str, sc, NDPI_PROTOCOL_NTOP, ".ntop.org"); @@ -6657,13 +6618,10 @@ void domainSearchUnitTest2() { struct ndpi_detection_module_struct *ndpi_str = ndpi_init_detection_module(NULL); ndpi_domain_classify *c = ndpi_domain_classify_alloc(); u_int16_t class_id = 9; - NDPI_PROTOCOL_BITMASK all; assert(ndpi_str); assert(c); - NDPI_BITMASK_SET_ALL(all); - ndpi_set_protocol_detection_bitmask2(ndpi_str, &all); ndpi_finalize_initialization(ndpi_str); ndpi_domain_classify_add(ndpi_str, c, class_id, "ntop.org"); |