From fea52d98ca7039adf287400e925ef43bfac48abf Mon Sep 17 00:00:00 2001 From: Toni Uhlig Date: Wed, 27 Nov 2024 11:51:53 +0100 Subject: [PATCH] Added nDPId decryption example Signed-off-by: Toni Uhlig --- CMakeLists.txt | 11 ++++- examples/c-decrypt/c-decrypt.c | 74 ++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 examples/c-decrypt/c-decrypt.c diff --git a/CMakeLists.txt b/CMakeLists.txt index c6dd8ef53..1e5256418 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,8 @@ if("${PROJECT_SOURCE_DIR}" STREQUAL "${PROJECT_BINARY_DIR}") "Please remove ${PROJECT_SOURCE_DIR}/CMakeCache.txt\n" "and\n" "${PROJECT_SOURCE_DIR}/CMakeFiles\n" - "Create a build directory somewhere and run CMake again.") + "Create a build directory somewhere and run CMake again.\n" + "Or run: 'cmake -S ${PROJECT_SOURCE_DIR} -B ./your-custom-build-dir [CMAKE-OPTIONS]'") endif() set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) find_package(PkgConfig REQUIRED) @@ -562,6 +563,14 @@ if(BUILD_EXAMPLES) install(TARGETS nDPIsrvd-influxd DESTINATION bin) endif() + if(ENABLE_CRYPTO) + add_executable(nDPId-decrypt examples/c-decrypt/c-decrypt.c utils.c ncrypt.c) + target_compile_definitions(nDPId-decrypt PRIVATE ${NDPID_DEFS} ${OSSL_DEFS}) + target_include_directories(nDPId-decrypt PRIVATE ${NDPID_DEPS_INC}) + target_link_libraries(nDPId-decrypt ${OSSL_LIBRARY}) + install(TARGETS nDPId-decrypt DESTINATION bin) + endif() + install(TARGETS nDPIsrvd-analysed nDPIsrvd-collectd nDPIsrvd-captured nDPIsrvd-simple DESTINATION bin) install(FILES examples/c-collectd/plugin_nDPIsrvd.conf examples/c-collectd/rrdgraph.sh DESTINATION share/nDPId/nDPIsrvd-collectd) install(DIRECTORY examples/c-collectd/www DESTINATION share/nDPId/nDPIsrvd-collectd) diff --git a/examples/c-decrypt/c-decrypt.c b/examples/c-decrypt/c-decrypt.c new file mode 100644 index 000000000..905dbf622 --- /dev/null +++ b/examples/c-decrypt/c-decrypt.c @@ -0,0 +1,74 @@ +#include +#include + +#include "utils.h" + +static void print_usage(char const * const arg0) +{ + static char const usage[] = + "Usage: %s " + "[-L listen-address] [-k private-key-file] [-K public-key-file]\n" + "\t \t" + "[-h]\n\n" + "\t-L\tThe address on which this example will listen for incoming\n" + "\t \t(encrypted) UDP packets sent by nDPId\n" + "\t-k\tThe path to the local private X25519 key file (PEM format)\n" + "\t-K\tThe path to the remote public X25519 key file (PEM format)\n" + "\t-h\tthis\n"; + + fprintf(stderr, usage, arg0); +} + +static int parse_options(int argc, char ** argv) +{ + int opt; + + while ((opt = getopt(argc, argv, "hk:K:s:")) != -1) + { + switch (opt) + { + case 'h': + print_usage(argv[0]); + return 1; + case 'k': + break; + case 'K': + break; + case 's': + break; + default: + print_usage(argv[0]); + return 1; + } + } + + if (optind < argc) + { + if (optind > 0) { + logger_early(1, "Unexpected argument(s) after %s\n\n", argv[optind]); + } else { + logger_early(1, "%s\n\n", "Unexpected argument(s)"); + } + print_usage(argv[0]); + + return 1; + } + + return 0; +} + +int main(int argc, char ** argv) +{ + if (argc == 0 || argv == NULL || stdout == NULL || stderr == NULL) + { + return 1; + } + + init_logging("nDPId-decrypt"); + + if (parse_options(argc, argv) != 0) + { + } + + return 0; +}