GeoIP handlign fixes

This commit is contained in:
Luca Deri
2021-04-18 21:36:03 +02:00
parent 18c6c1c2d6
commit 9fcf98c067
+39 -27
View File
@@ -53,7 +53,7 @@ int ndpi_load_geoip(struct ndpi_detection_module_struct *ndpi_str,
return(0); return(0);
#else #else
return(-1); return(-3);
#endif #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, 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) { u_int32_t *asn, char *country_code, u_int8_t country_code_len) {
#ifdef HAVE_MAXMINDDB #ifdef HAVE_MAXMINDDB
if(ndpi_str->mmdb_as_loaded) { int gai_error, mmdb_error, status;
int gai_error, mmdb_error, status; MMDB_lookup_result_s result;
MMDB_lookup_result_s result; MMDB_entry_data_s entry_data;
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); result = MMDB_lookup_string(&ndpi_str->mmdb_as, ip, &gai_error, &mmdb_error);
if((gai_error != 0) if((gai_error != 0)
|| (mmdb_error != MMDB_SUCCESS) || (mmdb_error != MMDB_SUCCESS)
|| (!result.found_entry)) || (!result.found_entry))
return(-1); *asn = 0;
else {
/* Get the ASN */ /* Get the ASN */
if((status = MMDB_get_value(&result.entry, &entry_data, "autonomous_system_number", NULL)) == MMDB_SUCCESS) { 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) { if(entry_data.has_data && entry_data.type == MMDB_DATA_TYPE_UINT32)
*asn = entry_data.uint32; *asn = entry_data.uint32;
else
if(country_code_len > 0) { *asn = 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);
} }
} }
} }
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 #endif
return(-2); return(-2);
} }