aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLuca Deri <deri@ntop.org>2021-02-18 23:14:55 +0100
committerLuca Deri <deri@ntop.org>2021-02-18 23:15:27 +0100
commit92e8d95f383f377fa787a916a35558729f674a8b (patch)
tree5df322c67b13588d315f76b6ffe01d419b6091fc /src
parentbdf95a5c2268adc2e1a4a2bbdcb5dd489c8f8cf0 (diff)
Added ndpi_get_geoip() APi call
Diffstat (limited to 'src')
-rw-r--r--src/include/ndpi_api.h.in6
-rw-r--r--src/lib/ndpi_geoip.c63
2 files changed, 57 insertions, 12 deletions
diff --git a/src/include/ndpi_api.h.in b/src/include/ndpi_api.h.in
index 81a1df925..e701c51d4 100644
--- a/src/include/ndpi_api.h.in
+++ b/src/include/ndpi_api.h.in
@@ -1469,8 +1469,10 @@ extern "C" {
int ndpi_load_geeoip(struct ndpi_detection_module_struct *ndpi_str,
const char *ip_city_data, const char *ip_as_data);
- int ndpi_free_geeoip(struct ndpi_detection_module_struct *ndpi_str);
-
+ void ndpi_free_geeoip(struct ndpi_detection_module_struct *ndpi_str);
+ int ndpi_get_geoip(struct ndpi_detection_module_struct *ndpi_str, char *ip,
+ u_int32_t *asn, char *country_code, u_int8_t country_code_len);
+
#ifdef __cplusplus
}
#endif
diff --git a/src/lib/ndpi_geoip.c b/src/lib/ndpi_geoip.c
index e0a300125..e85a9043d 100644
--- a/src/lib/ndpi_geoip.c
+++ b/src/lib/ndpi_geoip.c
@@ -1,7 +1,7 @@
/*
- * ndpi_analyze.c
+ * ndpi_geoip.c
*
- * Copyright (C) 2019 - ntop.org
+ * Copyright (C) 2021 - ntop.org
*
* This file is part of nDPI, an open source deep packet inspection
* library.
@@ -29,8 +29,7 @@
#include <errno.h>
#include <sys/types.h>
#include <stdint.h>
-#include <math.h>
-#include <float.h> /* FLT_EPSILON */
+
#include "ndpi_api.h"
#include "ndpi_config.h"
@@ -40,18 +39,18 @@ int ndpi_load_geeoip(struct ndpi_detection_module_struct *ndpi_str,
const char *ip_city_data, const char *ip_as_data) {
#ifdef HAVE_MAXMINDDB
int status;
-
+
/* Open the MMDB files */
if((status = MMDB_open(ip_city_data, MMDB_MODE_MMAP, &ndpi_str->mmdb_city)) != MMDB_SUCCESS)
- return(-1);
+ return(-1);
else
ndpi_str->mmdb_city_loaded = 1;
-
+
if((status = MMDB_open(ip_as_data, MMDB_MODE_MMAP, &ndpi_str->mmdb_as)) != MMDB_SUCCESS)
return(-2);
else
ndpi_str->mmdb_as_loaded = 1;
-
+
return(0);
#else
return(-1);
@@ -60,9 +59,53 @@ int ndpi_load_geeoip(struct ndpi_detection_module_struct *ndpi_str,
/* ********************************************************************************* */
-int ndpi_free_geeoip(struct ndpi_detection_module_struct *ndpi_str) {
+void ndpi_free_geeoip(struct ndpi_detection_module_struct *ndpi_str) {
#ifdef HAVE_MAXMINDDB
if(ndpi_str->mmdb_city_loaded) MMDB_close(&ndpi_str->mmdb_city);
- if(ndpi_str->mmdb_as_loaded) MMDB_close(&ndpi_str->mmdb_as);
+ if(ndpi_str->mmdb_as_loaded) MMDB_close(&ndpi_str->mmdb_as);
+#endif
+}
+
+/* ********************************************************************************* */
+
+int ndpi_get_geoip(struct ndpi_detection_module_struct *ndpi_str, char *ip,
+ u_int32_t *asn, char *country_code, u_int8_t country_code_len) {
+#ifdef HAVE_MAXMINDDB
+ if(ndpi_str->mmdb_as_loaded) {
+ int gai_error, mmdb_error, status;
+ MMDB_lookup_result_s result;
+ MMDB_entry_data_s entry_data;
+
+ result = MMDB_lookup_string(&ndpi_str->mmdb_as, ip, &gai_error, &mmdb_error);
+ if((gai_error != 0)
+ || (mmdb_error != MMDB_SUCCESS)
+ || (!result.found_entry))
+ return(-1);
+
+ /* Get the ASN */
+ if((status = MMDB_get_value(&result.entry, &entry_data, "autonomous_system_number", NULL)) == MMDB_SUCCESS) {
+ if(entry_data.has_data && entry_data.type == MMDB_DATA_TYPE_UINT32) {
+ *asn = entry_data.uint32;
+
+ if(country_code_len > 0) {
+ int status = MMDB_get_value(&result.entry, &entry_data, "country", "iso_code", NULL);
+
+ if((status != MMDB_SUCCESS) || (!entry_data.has_data))
+ country_code[0] = '\0';
+ else {
+ int str_len = ndpi_min(entry_data.data_size, country_code_len);
+
+ memcpy(country_code, entry_data.utf8_string, str_len);
+ country_code[str_len] = '\0';
+ }
+ }
+
+ return(0);
+ }
+ }
+ }
#endif
+
+ return(-2);
}
+