nDPI/fuzz/fuzz_ds_hash.cpp
Ivan Nardi efccc7d5e4
Rework flow breed (#2926)
Right now, there is, in essence, a static mapping between flow protocols
and flow breeds.
Make it dynamic: allow to have different flows, with the same
classification but differents breeds. This is the same logic that we
already have for categories....

Preliminary work to support breed in category lists.

API change from the app POV: to get the flow breed don't use anymore
`ndpi_get_proto_breed()`, but access directly `struct ndpi_proto->breed`

The functions `ndpi_domain_classify_*()` and
`ndpi_get_host_domain_suffix()` now have a `u_int32_t` parameter as
`class_id` (instead of `u_int_16_t`), with the following logic:
```
class_id = (breed << 16) | category
```
instead of the old:
```
class_id = category
```
Please note that this change is back-compatible: if you are not
interested into breeds, you don't need to update the application code.
2025-09-02 16:54:34 +02:00

61 lines
1.6 KiB
C++

#include "ndpi_api.h"
#include "fuzz_common_code.h"
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include "fuzzer/FuzzedDataProvider.h"
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
FuzzedDataProvider fuzzed_data(data, size);
u_int16_t i, rc, num_iteration, data_len, is_added = 0;
u_int32_t value32;
std::vector<char>value_added;
ndpi_str_hash *h = NULL;
/* Just to have some data */
if (fuzzed_data.remaining_bytes() < 1024)
return -1;
/* To allow memory allocation failures */
fuzz_set_alloc_callbacks_and_seed(size);
if (fuzzed_data.ConsumeBool())
ndpi_hash_init(&h);
else
ndpi_hash_init(NULL);
num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
for (i = 0; i < num_iteration; i++) {
data_len = fuzzed_data.ConsumeIntegralInRange(0, 127);
std::vector<char>data = fuzzed_data.ConsumeBytes<char>(data_len);
rc = ndpi_hash_add_entry(&h, data.data(), data.size(), i);
/* Keep one random entry really added */
if (rc == 0 && fuzzed_data.ConsumeBool()) {
value_added = data;
is_added = 1;
}
}
/* "Random" search */
num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
for (i = 0; i < num_iteration; i++) {
data_len = fuzzed_data.ConsumeIntegralInRange(0, 127);
std::vector<char>data = fuzzed_data.ConsumeBytes<char>(data_len);
ndpi_hash_find_entry(h, data.data(), data.size(), &value32);
}
/* Search of an added entry */
if (is_added) {
ndpi_hash_find_entry(h, value_added.data(), value_added.size(), &value32);
}
if (fuzzed_data.ConsumeBool())
ndpi_hash_free(NULL);
ndpi_hash_free(&h);
return 0;
}