Files
nDPI/tests/dga/dga_evaluate.c
T
Ivan Nardi 400cd516b5 Allow multiple struct ndpi_detection_module_struct to share some state (#2271)
Add the concept of "global context".

Right now every instance of `struct ndpi_detection_module_struct` (we
will call it "local context" in this description) is completely
independent from each other. This provide optimal performances in
multithreaded environment, where we pin each local context to a thread,
and each thread to a specific CPU core: we don't have any data shared
across the cores.

Each local context has, internally, also some information correlating
**different** flows; something like:
```
if flow1 (PeerA <-> Peer B) is PROTOCOL_X; then
  flow2 (PeerC <-> PeerD) will be PROTOCOL_Y
```
To get optimal classification results, both flow1 and flow2 must be
processed by the same local context. This is not an issue at all in the far
most common scenario where there is only one local context, but it might
be impractical in some more complex scenarios.

Create the concept of "global context": multiple local contexts can use
the same global context and share some data (structures) using it.
This way the data correlating multiple flows can be read/write from
different local contexts.
This is an optional feature, disabled by default.

Obviously data structures shared in a global context must be thread safe.
This PR updates the code of the LRU implementation to be, optionally,
thread safe.

Right now, only the LRU caches can be shared; the other main structures
(trees and automas) are basically read-only: there is little sense in
sharing them. Furthermore, these structures don't have any information
correlating multiple flows.

Every LRU cache can be shared, independently from the others, via
`ndpi_set_config(ndpi_struct, NULL, "lru.$CACHE_NAME.scope", "1")`.

It's up to the user to find the right trade-off between performances
(i.e. without shared data) and classification results (i.e. with some
shared data among the local contexts), depending on the specific traffic
patterns and on the algorithms used to balance the flows across the
threads/cores/local contexts.

Add some basic examples of library initialization in
`doc/library_initialization.md`.

This code needs libpthread as external dependency. It shouldn't be a big
issue; however a configure flag has been added to disable global context
support. A new CI job has been added to test it.

TODO: we should need to find a proper way to add some tests on
multithreaded enviroment... not an easy task...

*** API changes ***

If you are not interested in this feature, simply add a NULL parameter to
any `ndpi_init_detection_module()` calls.
2024-02-01 15:33:11 +01:00

125 lines
3.2 KiB
C

/*
* dga.c
*
* Copyright (C) 2019-20 - ntop.org
*
* nDPI is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* nDPI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with nDPI. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdint.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <ndpi_api.h>
#include <ndpi_main.h>
#include <ndpi_typedefs.h>
#include "ndpi_config.h"
void help() {
printf("dga_evaluate <file name> [<verbose>]\n");
exit(0);
}
/* *********************************************** */
static int verbose_dga_detection = 0;
static void ndpi_dbg_fn(u_int32_t protocol,
struct ndpi_detection_module_struct *module_struct,
ndpi_log_level_t log_level, const char *file,
const char *func, unsigned line,
const char *format, ...)
{
assert(protocol == NDPI_PROTOCOL_UNKNOWN);
assert(module_struct != NULL);
/* While this program always logs at NDPI_LOG_DEBUG_EXTRA level, the
initialization of the library may log at any level */
(void)file;
(void)func;
(void)line;
if (verbose_dga_detection) {
va_list vl;
va_start(vl, format);
vprintf(format, vl);
va_end(vl);
}
}
int main(int argc, char **argv) {
FILE *fd;
char buffer[512];
int verbose = 0;
int num_detections = 0;
if(argc < 2) help();
fd = fopen(argv[1], "r");
if(fd == NULL) {
printf("Unable to open file %s\n", argv[1]);
exit(0);
}
if(argv[2] != NULL) {
verbose = 1;
if(argv[3] != NULL)
verbose_dga_detection = 1;
}
if (ndpi_get_api_version() != NDPI_API_VERSION) {
printf("nDPI Library version mismatch: please make sure this code and the nDPI library are in sync\n");
return -1;
}
/* Initialize nDPI detection module*/
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_set_config(ndpi_str, NULL, "log.level", "3"); /* NDPI_LOG_DEBUG_EXTRA */
set_ndpi_debug_function(ndpi_str, ndpi_dbg_fn);
ndpi_finalize_initialization(ndpi_str);
assert(ndpi_str != NULL);
while(fgets(buffer, sizeof(buffer), fd) != NULL) {
char *hostname = strtok(buffer, "\n");
if(ndpi_check_dga_name(ndpi_str, NULL, hostname, 1, 1)) {
if(verbose)
printf("%10s\t%s\n", "[DGA]", hostname);
num_detections++;
} else {
if(verbose)
printf("%10s\t%s\n", "[NON DGA]", hostname);
}
}
fclose(fd);
ndpi_exit_detection_module(ndpi_str);
printf("%i", num_detections);
return 0;
}