nDPI/fuzz/fuzz_common_code.c
Nardi Ivan d72a760ac3 New API for library configuration
This is the first step into providing (more) configuration options in nDPI.

The idea is to have a simple way to configure (most of) nDPI: only one
function (`ndpi_set_config()`) to set any configuration parameters
(in the present or on in the future) and we try to keep this function
prototype as agnostic as possible.

You can configure the library:
* via API, using `ndpi_set_config()`
* via a configuration file, in a text format

This way, anytime we need to add a new configuration parameter:
* we don't need to add two public functions (a getter and a setter)
* we don't break API/ABI compatibility of the library; even changing
the parameter type (from integer to a list of integer, for example)
doesn't break the compatibility.

The complete list of configuration options is provided in
`doc/configuration_parameters.md`.

As a first example, two configuration knobs are provided:
* the ability to enable/disable the extraction of the sha1 fingerprint of
the TLS certificates.
* the upper limit on the number of packets per flow that will be subject
to inspection
2024-01-18 10:21:24 +01:00

69 lines
1.9 KiB
C

#include "fuzz_common_code.h"
static int mem_alloc_state = 0;
__attribute__((no_sanitize("integer")))
static int fastrand ()
{
if(!mem_alloc_state) return 1; /* No failures */
mem_alloc_state = (214013 * mem_alloc_state + 2531011);
return (mem_alloc_state >> 16) & 0x7FFF;
}
static void *malloc_wrapper(size_t size) {
return (fastrand () % 16) ? malloc (size) : NULL;
}
static void free_wrapper(void *freeable) {
free(freeable);
}
void fuzz_set_alloc_callbacks(void)
{
set_ndpi_malloc(malloc_wrapper);
set_ndpi_free(free_wrapper);
}
void fuzz_set_alloc_seed(int seed)
{
mem_alloc_state = seed;
}
void fuzz_set_alloc_callbacks_and_seed(int seed)
{
fuzz_set_alloc_callbacks();
fuzz_set_alloc_seed(seed);
}
void fuzz_init_detection_module(struct ndpi_detection_module_struct **ndpi_info_mod)
{
ndpi_init_prefs prefs = ndpi_no_prefs;
NDPI_PROTOCOL_BITMASK all;
NDPI_PROTOCOL_BITMASK debug_bitmask;
if(*ndpi_info_mod == NULL) {
*ndpi_info_mod = ndpi_init_detection_module(prefs);
NDPI_BITMASK_SET_ALL(debug_bitmask);
ndpi_set_log_level(*ndpi_info_mod, 4);
ndpi_set_debug_bitmask(*ndpi_info_mod, debug_bitmask);
ndpi_load_categories_dir(*ndpi_info_mod, "./lists/");
ndpi_load_protocols_file(*ndpi_info_mod, "protos.txt");
ndpi_load_categories_file(*ndpi_info_mod, "categories.txt", NULL);
ndpi_load_risk_domain_file(*ndpi_info_mod, "risky_domains.txt");
ndpi_load_malicious_ja3_file(*ndpi_info_mod, "ja3_fingerprints.csv");
ndpi_load_malicious_sha1_file(*ndpi_info_mod, "sha1_fingerprints.csv");
NDPI_BITMASK_SET_ALL(all);
ndpi_set_protocol_detection_bitmask2(*ndpi_info_mod, &all);
ndpi_set_config(*ndpi_info_mod, NULL, "filename.config", "config.txt");
ndpi_finalize_initialization(*ndpi_info_mod);
}
}
FILE *buffer_to_file(const uint8_t *data, size_t size)
{
return fmemopen((void *)data, size, "rw");
}