aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Deri <deri@ntop.org>2021-04-18 21:36:03 +0200
committerLuca Deri <deri@ntop.org>2021-04-18 21:36:03 +0200
commit9fcf98c06740585e2b44c193084175a855448afc (patch)
tree9a0cf8cd9c4cddfa89bdfd7cfb056c812e0a2e29
parent18c6c1c2d68c4cc185d4c2fa0583776edf523042 (diff)
GeoIP handlign fixes
-rw-r--r--src/lib/ndpi_geoip.c66
1 files changed, 39 insertions, 27 deletions
diff --git a/src/lib/ndpi_geoip.c b/src/lib/ndpi_geoip.c
index d689258c3..50b1d4b3e 100644
--- a/src/lib/ndpi_geoip.c
+++ b/src/lib/ndpi_geoip.c
@@ -53,7 +53,7 @@ int ndpi_load_geoip(struct ndpi_detection_module_struct *ndpi_str,
return(0);
#else
- return(-1);
+ return(-3);
#endif
}
@@ -71,41 +71,53 @@ 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) {
#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;
+ int gai_error, mmdb_error, status;
+ MMDB_lookup_result_s result;
+ MMDB_entry_data_s entry_data;
+ if(ndpi_str->mmdb_as_loaded) {
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);
+ *asn = 0;
+ else {
+ /* 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;
+ else
+ *asn = 0;
}
}
}
+
+ if(ndpi_str->mmdb_city_loaded && (country_code_len > 0)) {
+ int status;
+
+ result = MMDB_lookup_string(&ndpi_str->mmdb_city, ip, &gai_error, &mmdb_error);
+
+ if((gai_error != 0)
+ || (mmdb_error != MMDB_SUCCESS)
+ || (!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);
+
+ memcpy(country_code, entry_data.utf8_string, str_len);
+ country_code[str_len] = '\0';
+ }
+ }
+ }
+
+ return(0);
#endif
return(-2);
}
-