From 908d4966b3578c419753e7a83c214adaec2327be Mon Sep 17 00:00:00 2001 From: Ivan Nardi <12729895+IvanNardi@users.noreply.github.com> Date: Tue, 25 Feb 2025 10:03:36 +0100 Subject: Add a basic example to show how to use geo API (#2747) Credits to @LTxAlves --- tests/performance/Makefile.in | 8 +++++++- tests/performance/geo.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/performance/geo.c diff --git a/tests/performance/Makefile.in b/tests/performance/Makefile.in index d342f8483..3041d22f5 100644 --- a/tests/performance/Makefile.in +++ b/tests/performance/Makefile.in @@ -2,7 +2,7 @@ INC=-I ../../src/include/ -I ../../src/lib/third_party/include/ LIB=../../src/lib/libndpi.a @ADDITIONAL_LIBS@ @LIBS@ TOOLS=substringsearch patriciasearch gcrypt-int gcrypt-gnu strnstr -TESTS=substring_test patricia_test strnstr_test +TESTS=substring_test patricia_test strnstr_test geo_test all: $(TESTS) @@ -21,12 +21,18 @@ substringsearch: substringsearch.c Makefile strnstr: strnstr.cpp Makefile $(CXX) $(INC) @CFLAGS@ strnstr.cpp -o strnstr +geo: geo.c Makefile + $(CC) $(INC) @CFLAGS@ geo.c -o geo $(LIB) + substring_test: substringsearch top-1m.csv ./substringsearch strnstr_test: strnstr ./strnstr +geo_test: geo + ./geo + # patriciasearch: patriciasearch.c Makefile diff --git a/tests/performance/geo.c b/tests/performance/geo.c new file mode 100644 index 000000000..f3b395b01 --- /dev/null +++ b/tests/performance/geo.c @@ -0,0 +1,35 @@ +#include +#include + +int main() +{ + struct ndpi_detection_module_struct *ndpi_info_mod; + int rc; + + ndpi_info_mod = ndpi_init_detection_module(NULL); + if (ndpi_info_mod == NULL) + return 1; + + ndpi_finalize_initialization(ndpi_info_mod); + + rc = ndpi_load_geoip(ndpi_info_mod, "GeoLite2-City.mmdb", "GeoLite2-ASN.mmdb"); + if(rc != 0) { + fprintf(stderr, "Error loading db files: %d\n", rc); + return 1; + } + + char country[50], continent[50], city[50]; + + ndpi_get_geoip_country_continent_city(ndpi_info_mod, "24.124.1.8", country, sizeof(country), continent, sizeof(continent), city, sizeof(city)); + printf("1\n\tCountry: %s\n\tContinent: %s\n\tCity: %s\n", country, continent, city); + ndpi_get_geoip_country_continent_city(ndpi_info_mod, "8.8.8.8", country, sizeof(country), continent, sizeof(continent), city, sizeof(city)); + printf("2\n\tCountry: %s\n\tContinent: %s\n\tCity: %s\n", country, continent, city); + ndpi_get_geoip_country_continent_city(ndpi_info_mod, "161.148.164.31", country, sizeof(country), continent, sizeof(continent), city, sizeof(city)); + printf("3\n\tCountry: %s\n\tContinent: %s\n\tCity: %s\n", country, continent, city); + ndpi_get_geoip_country_continent_city(ndpi_info_mod, "184.74.73.88", country, sizeof(country), continent, sizeof(continent), city, sizeof(city)); + printf("4\n\tCountry: %s\n\tContinent: %s\n\tCity: %s\n", country, continent, city); + + ndpi_exit_detection_module(ndpi_info_mod); + + return 0; +} -- cgit v1.2.3