diff options
author | Ivan Nardi <12729895+IvanNardi@users.noreply.github.com> | 2025-06-23 11:24:18 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-06-23 11:24:18 +0200 |
commit | 978ca1ba1ab0f9d3f7d3c46e6f80a829b08205db (patch) | |
tree | f7748c2d810c75c0155fa3f81e3146a797f6fdba /example/ndpiReader.c | |
parent | 6cbc8d1471be221766fac49ed73f5b0e837917be (diff) |
New API to enable/disable protocols. Removed `NDPI_LAST_IMPLEMENTED_PROTOCOL` (#2894)
Change the API to enable/disable protocols: you can set that via the
standard `ndpi_set_config()` function, as every configuration
parameters. By default, all protocols are enabled.
Split the (local) context initialization into two phases:
* `ndpi_init_detection_module()`: generic part. It does not depend on the
configuration and on the protocols being enabled or not. It also
calculates the real number of internal protocols
* `ndpi_finalize_initialization()`: apply the configuration. All the
initialization stuff that depend on protocols being enabled or not
must be put here
This is the last step to have the protocols number fully calculated at
runtime
Remove a (now) useless fuzzer.
Important API changes:
* remove `NDPI_LAST_IMPLEMENTED_PROTOCOL` define
* remove `ndpi_get_num_internal_protocols()`. To get the number of
configured protocols (internal and custom) you must use
`ndpi_get_num_protocols()` after having called `ndpi_finalize_initialization()`
Diffstat (limited to 'example/ndpiReader.c')
-rw-r--r-- | example/ndpiReader.c | 54 |
1 files changed, 40 insertions, 14 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c index 63d05f9db..85ebac478 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -304,9 +304,8 @@ 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, struct ndpi_bitmask *bitmask, - int inverted_logic); extern u_int8_t is_ndpi_proto(struct ndpi_flow_info *flow, u_int16_t id); +static char const *ndpi_cfg_error2string(ndpi_cfg_error const err); /* ********************************** */ @@ -402,6 +401,40 @@ static u_int check_bin_doh_similarity(struct ndpi_bin *bin, float *similarity) { /* *********************************************** */ +static char _proto_delim[] = " \t,:;"; +static int enable_disable_protocols_list(struct ndpi_detection_module_struct *ndpi_str, char *str, int inverted_logic) { + char *n; + char op; + ndpi_cfg_error rc; + + if(!inverted_logic) + op = 1; /* Default action: enable protocol */ + else + op = 0; /* Default action: disable protocol */ + + for(n = strtok(str,_proto_delim); n && *n; n = strtok(NULL,_proto_delim)) { + if(*n == '-') { + op = !inverted_logic ? 0 : 1; + n++; + } else if(*n == '+') { + op = !inverted_logic ? 1 : 0; + n++; + } + if(op) + rc = ndpi_set_config(ndpi_str, n, "enable", "1"); + else + rc = ndpi_set_config(ndpi_str, n, "enable", "0"); + if(rc != NDPI_CFG_OK) { + LOG(NDPI_LOG_ERROR, "Error enabling/disabling protocol [%s]: %s (%d)\n", + n, ndpi_cfg_error2string(rc), rc); + } + } + + return 0; +} + +/* *********************************************** */ + void ndpiCheckHostStringMatch(char *testChar) { ndpi_protocol_match_result match = { NDPI_PROTOCOL_UNKNOWN, NDPI_PROTOCOL_CATEGORY_UNSPECIFIED, NDPI_PROTOCOL_UNRATED }; @@ -3002,7 +3035,6 @@ 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) { - struct ndpi_bitmask enabled_bitmask, *enabled_bitmask_ptr = NULL; struct ndpi_workflow_prefs prefs; int i, ret; ndpi_cfg_error rc; @@ -3014,21 +3046,15 @@ 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 */ if(_disabled_protocols != NULL) { - if(ndpi_bitmask_alloc(&enabled_bitmask, ndpi_get_num_internal_protocols()) != 0) - exit(-1); - ndpi_bitmask_set_all(&enabled_bitmask); - if(parse_proto_name_list(_disabled_protocols, &enabled_bitmask, 1)) - exit(-1); - enabled_bitmask_ptr = &enabled_bitmask; + enable_disable_protocols_list(ndpi_thread_info[thread_id].workflow->ndpi_struct, _disabled_protocols, 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_ptr); - ndpi_bitmask_free(enabled_bitmask_ptr); - if(_categoriesDirPath) { int failed_files = ndpi_load_categories_dir(ndpi_thread_info[thread_id].workflow->ndpi_struct, _categoriesDirPath); if (failed_files < 0) { |