aboutsummaryrefslogtreecommitdiff
path: root/src/lib/ndpi_geoip.c
diff options
context:
space:
mode:
authorLuca Deri <deri@ntop.org>2021-04-19 21:39:55 +0200
committerLuca Deri <deri@ntop.org>2021-04-19 21:39:55 +0200
commitc6a835746ee991f996e7c4d7709db722a321aa51 (patch)
tree62c1be125c497b4562c660a917e0d78cd1701467 /src/lib/ndpi_geoip.c
parent8c3674e9a30a0870effdb4c7eb5ad792f7ba6b6a (diff)
Reworked GeoIP API
int ndpi_get_geoip_asn(struct ndpi_detection_module_struct *ndpi_str, char *ip, u_int32_t *asn); int ndpi_get_geoip_country_continent(struct ndpi_detection_module_struct *ndpi_str, char *ip, char *country_code, u_int8_t country_code_len, char *continent, u_int8_t continent_len);
Diffstat (limited to 'src/lib/ndpi_geoip.c')
-rw-r--r--src/lib/ndpi_geoip.c57
1 files changed, 44 insertions, 13 deletions
diff --git a/src/lib/ndpi_geoip.c b/src/lib/ndpi_geoip.c
index 50b1d4b3e..23fb0b752 100644
--- a/src/lib/ndpi_geoip.c
+++ b/src/lib/ndpi_geoip.c
@@ -68,8 +68,7 @@ void ndpi_free_geoip(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) {
+int ndpi_get_geoip_asn(struct ndpi_detection_module_struct *ndpi_str, char *ip, u_int32_t *asn) {
#ifdef HAVE_MAXMINDDB
int gai_error, mmdb_error, status;
MMDB_lookup_result_s result;
@@ -91,9 +90,26 @@ int ndpi_get_geoip(struct ndpi_detection_module_struct *ndpi_str, char *ip,
*asn = 0;
}
}
+
+ return(0);
}
+#endif
+
+ return(-2);
+}
+
+/* ********************************************************************************* */
- if(ndpi_str->mmdb_city_loaded && (country_code_len > 0)) {
+int ndpi_get_geoip_country_continent(struct ndpi_detection_module_struct *ndpi_str, char *ip,
+ char *country_code, u_int8_t country_code_len,
+ char *continent, u_int8_t continent_len) {
+#ifdef HAVE_MAXMINDDB
+ int gai_error, mmdb_error;
+ MMDB_lookup_result_s result;
+ MMDB_entry_data_s entry_data;
+
+
+ if(ndpi_str->mmdb_city_loaded) {
int status;
result = MMDB_lookup_string(&ndpi_str->mmdb_city, ip, &gai_error, &mmdb_error);
@@ -103,20 +119,35 @@ int ndpi_get_geoip(struct ndpi_detection_module_struct *ndpi_str, char *ip,
|| (!result.found_entry))
country_code[0] = '\0';
else {
- 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);
+ if(country_code_len > 0) {
+ 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';
+ }
+ }
- memcpy(country_code, entry_data.utf8_string, str_len);
- country_code[str_len] = '\0';
+ if(continent_len > 0) {
+ status = MMDB_get_value(&result.entry, &entry_data, "continent", "names", "en", NULL);
+
+ if((status != MMDB_SUCCESS) || (!entry_data.has_data))
+ continent[0] = '\0';
+ else {
+ int str_len = ndpi_min(entry_data.data_size, continent_len);
+
+ memcpy(continent, entry_data.utf8_string, str_len);
+ continent[str_len] = '\0';
+ }
}
}
- }
- return(0);
+ return(0);
+ }
#endif
return(-2);