aboutsummaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorIvan Nardi <12729895+IvanNardi@users.noreply.github.com>2025-06-09 09:00:17 +0200
committerGitHub <noreply@github.com>2025-06-09 09:00:17 +0200
commitcbd7136b3480774a10f18744d33d3694ffee221b (patch)
treea9981734e85c500ab4605e913cd6ee8b8197492d /example
parent75395cb264f9bfd38d27ac0ba506acc9eab22e34 (diff)
Remove `NDPI_PROTOCOL_BITMASK`; add a new generic bitmask data structure (#2871)
The main difference is that the memory is allocated at runtime Typical usercase: ``` struct ndpi_bitmask b; ndpi_bitmask_alloc(&b, ndpi_get_num_internal_protocols()); ndpi_bitmask_set(&b, $BIT); ndpi_bitmask_is_set(&b, $BIT); [...] ndpi_bitmask_dealloc(&b); ``` See #2136
Diffstat (limited to 'example')
-rw-r--r--example/ndpiReader.c43
-rw-r--r--example/reader_util.c12
-rw-r--r--example/reader_util.h4
3 files changed, 47 insertions, 12 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c
index b03dd32c7..c71a0398e 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_INTERNAL_PROTOCOL_BITMASK *bitmask,
+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);
@@ -2994,7 +2994,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_INTERNAL_PROTOCOL_BITMASK enabled_bitmask;
+ struct ndpi_bitmask enabled_bitmask, *enabled_bitmask_ptr = NULL;
struct ndpi_workflow_prefs prefs;
int i, ret;
ndpi_cfg_error rc;
@@ -3007,15 +3007,19 @@ static void setupDetection(u_int16_t thread_id, pcap_t * pcap_handle,
prefs.ignore_vlanid = ignore_vlanid;
/* Protocols to enable/disable. Default: everything is enabled */
- NDPI_INTERNAL_PROTOCOL_SET_ALL(enabled_bitmask);
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;
}
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);
+ serialization_format, g_ctx, enabled_bitmask_ptr);
+ ndpi_bitmask_dealloc(enabled_bitmask_ptr);
if(_categoriesDirPath) {
int failed_files = ndpi_load_categories_dir(ndpi_thread_info[thread_id].workflow->ndpi_struct, _categoriesDirPath);
@@ -6265,6 +6269,36 @@ void mahalanobisUnitTest()
/* *********************************************** */
+void bitmaskUnitTest()
+{
+ struct ndpi_bitmask b;
+ int i;
+
+ assert(ndpi_bitmask_alloc(&b, 512) == 0);
+ for(i = 0; i < b.max_bits; i++) {
+ ndpi_bitmask_set(&b, i);
+ assert(ndpi_bitmask_is_set(&b, i));
+ }
+ for(i = 0; i < b.max_bits; i++) {
+ ndpi_bitmask_clear(&b, i);
+ assert(!ndpi_bitmask_is_set(&b, i));
+ }
+ ndpi_bitmask_set_all(&b);
+ for(i = 0; i < b.max_bits; i++)
+ assert(ndpi_bitmask_is_set(&b, i));
+ ndpi_bitmask_reset(&b);
+ for(i = 0; i < b.max_bits; i++)
+ assert(!ndpi_bitmask_is_set(&b, i));
+ for(i = 0; i < b.max_bits; i++) {
+ ndpi_bitmask_set(&b, i);
+ assert(ndpi_bitmask_is_set(&b, i));
+ }
+
+ ndpi_bitmask_dealloc(&b);
+}
+
+/* *********************************************** */
+
void filterUnitTest() {
ndpi_filter* f = ndpi_filter_alloc();
u_int32_t v, i;
@@ -6798,6 +6832,7 @@ int main(int argc, char **argv) {
memmemUnitTest();
memcasecmpUnitTest();
mahalanobisUnitTest();
+ bitmaskUnitTest();
#endif
}
diff --git a/example/reader_util.c b/example/reader_util.c
index 3c0819b7e..37db86990 100644
--- a/example/reader_util.c
+++ b/example/reader_util.c
@@ -337,7 +337,7 @@ void ndpi_free_flow_info_half(struct ndpi_flow_info *flow) {
/* ***************************************************** */
static char _proto_delim[] = " \t,:;";
-int parse_proto_name_list(char *str, NDPI_INTERNAL_PROTOCOL_BITMASK *bitmask, int inverted_logic) {
+int parse_proto_name_list(char *str, struct ndpi_bitmask *bitmask, int inverted_logic) {
char *n;
uint16_t proto;
char op;
@@ -366,9 +366,9 @@ int parse_proto_name_list(char *str, NDPI_INTERNAL_PROTOCOL_BITMASK *bitmask, in
}
if(!strcmp(n,"all")) {
if(op)
- NDPI_INTERNAL_PROTOCOL_SET_ALL(*bitmask);
+ ndpi_bitmask_set_all(bitmask);
else
- NDPI_INTERNAL_PROTOCOL_RESET(*bitmask);
+ ndpi_bitmask_reset(bitmask);
continue;
}
proto = ndpi_get_proto_by_name(module, n);
@@ -378,9 +378,9 @@ int parse_proto_name_list(char *str, NDPI_INTERNAL_PROTOCOL_BITMASK *bitmask, in
return 1;
}
if(op)
- NDPI_INTERNAL_PROTOCOL_ADD(*bitmask,proto);
+ ndpi_bitmask_set(bitmask, proto);
else
- NDPI_INTERNAL_PROTOCOL_DEL(*bitmask,proto);
+ ndpi_bitmask_clear(bitmask, proto);
}
ndpi_exit_detection_module(module);
@@ -410,7 +410,7 @@ struct ndpi_workflow* ndpi_workflow_init(const struct ndpi_workflow_prefs * pref
pcap_t * pcap_handle, int do_init_flows_root,
ndpi_serialization_format serialization_format,
struct ndpi_global_context *g_ctx,
- NDPI_INTERNAL_PROTOCOL_BITMASK *enabled_bitmask) {
+ struct ndpi_bitmask *enabled_bitmask) {
struct ndpi_detection_module_struct * module;
struct ndpi_workflow * workflow;
diff --git a/example/reader_util.h b/example/reader_util.h
index 2b76812c1..b755d3ab6 100644
--- a/example/reader_util.h
+++ b/example/reader_util.h
@@ -435,7 +435,7 @@ typedef struct ndpi_workflow {
/* TODO: remove wrappers parameters and use ndpi global, when their initialization will be fixed... */
-struct ndpi_workflow * ndpi_workflow_init(const struct ndpi_workflow_prefs * prefs, pcap_t * pcap_handle, int do_init_flows_root, ndpi_serialization_format serialization_format, struct ndpi_global_context *g_ctx, NDPI_INTERNAL_PROTOCOL_BITMASK *enabled_bitmask);
+struct ndpi_workflow * ndpi_workflow_init(const struct ndpi_workflow_prefs * prefs, pcap_t * pcap_handle, int do_init_flows_root, ndpi_serialization_format serialization_format, struct ndpi_global_context *g_ctx, struct ndpi_bitmask *enabled_bitmask);
/* workflow main free function */
@@ -471,7 +471,7 @@ void process_ndpi_collected_info(struct ndpi_workflow * workflow, struct ndpi_fl
void ndpi_flow_info_free_data(struct ndpi_flow_info *flow);
void ndpi_flow_info_freer(void *node);
const char* print_cipher_id(u_int32_t cipher);
-int parse_proto_name_list(char *str, NDPI_INTERNAL_PROTOCOL_BITMASK *bitmask, int inverted_logic);
+int parse_proto_name_list(char *str, struct ndpi_bitmask *bitmask, int inverted_logic);
extern int reader_log_level;