Skip to content

Commit bdcf802

Browse files
committed
Add city as a geoip possibility
1 parent 084a580 commit bdcf802

File tree

3 files changed

+161
-64
lines changed

3 files changed

+161
-64
lines changed

fuzz/fuzz_config.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
823823
/* TODO: stub for geo stuff */
824824
ndpi_get_geoip_asn(ndpi_info_mod, NULL, NULL);
825825
ndpi_get_geoip_country_continent(ndpi_info_mod, NULL, NULL, 0, NULL, 0);
826+
ndpi_get_geoip_country_continent_city(ndpi_info_mod, NULL, NULL, 0, NULL, 0, NULL, 0);
826827

827828
ndpi_free_flow_data(&flow);
828829

src/include/ndpi_api.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,10 @@ extern "C" {
20902090
int ndpi_get_geoip_country_continent(struct ndpi_detection_module_struct *ndpi_str, char *ip,
20912091
char *country_code, u_int8_t country_code_len,
20922092
char *continent, u_int8_t continent_len);
2093+
int ndpi_get_geoip_country_continent_city(struct ndpi_detection_module_struct *ndpi_str, char *ip,
2094+
char *country_code, u_int8_t country_code_len,
2095+
char *continent, u_int8_t continent_len,
2096+
char *city, u_int8_t city_len);
20932097

20942098
/* ******************************* */
20952099

src/lib/ndpi_geoip.c

Lines changed: 156 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -37,42 +37,46 @@
3737
/* ********************************************************************************* */
3838

3939
int ndpi_load_geoip(struct ndpi_detection_module_struct *ndpi_str,
40-
const char *ip_city_data, const char *ip_as_data) {
40+
const char *ip_city_data, const char *ip_as_data)
41+
{
4142
#ifdef HAVE_MAXMINDDB
4243
int status;
4344

44-
ndpi_str->mmdb_city = (void*)ndpi_malloc(sizeof(MMDB_s));
45-
ndpi_str->mmdb_as = (void*)ndpi_malloc(sizeof(MMDB_s));
46-
47-
if((ndpi_str->mmdb_city == NULL) || (ndpi_str->mmdb_as == NULL))
48-
return(-1);
49-
45+
ndpi_str->mmdb_city = (void *)ndpi_malloc(sizeof(MMDB_s));
46+
ndpi_str->mmdb_as = (void *)ndpi_malloc(sizeof(MMDB_s));
47+
48+
if ((ndpi_str->mmdb_city == NULL) || (ndpi_str->mmdb_as == NULL))
49+
return (-1);
50+
5051
/* Open the MMDB files */
51-
if((status = MMDB_open(ip_city_data, MMDB_MODE_MMAP, (MMDB_s*)ndpi_str->mmdb_city)) != MMDB_SUCCESS)
52-
return(-1);
52+
if ((status = MMDB_open(ip_city_data, MMDB_MODE_MMAP, (MMDB_s *)ndpi_str->mmdb_city)) != MMDB_SUCCESS)
53+
return (-1);
5354
else
5455
ndpi_str->mmdb_city_loaded = 1;
5556

56-
if((status = MMDB_open(ip_as_data, MMDB_MODE_MMAP, (MMDB_s*)ndpi_str->mmdb_as)) != MMDB_SUCCESS)
57-
return(-2);
57+
if ((status = MMDB_open(ip_as_data, MMDB_MODE_MMAP, (MMDB_s *)ndpi_str->mmdb_as)) != MMDB_SUCCESS)
58+
return (-2);
5859
else
5960
ndpi_str->mmdb_as_loaded = 1;
6061

61-
return(0);
62+
return (0);
6263
#else
6364
(void)ndpi_str;
6465
(void)ip_city_data;
6566
(void)ip_as_data;
66-
return(-3);
67+
return (-3);
6768
#endif
6869
}
6970

7071
/* ********************************************************************************* */
7172

72-
void ndpi_free_geoip(struct ndpi_detection_module_struct *ndpi_str) {
73+
void ndpi_free_geoip(struct ndpi_detection_module_struct *ndpi_str)
74+
{
7375
#ifdef HAVE_MAXMINDDB
74-
if(ndpi_str->mmdb_city_loaded) MMDB_close((MMDB_s*)ndpi_str->mmdb_city);
75-
if(ndpi_str->mmdb_as_loaded) MMDB_close((MMDB_s*)ndpi_str->mmdb_as);
76+
if (ndpi_str->mmdb_city_loaded)
77+
MMDB_close((MMDB_s *)ndpi_str->mmdb_city);
78+
if (ndpi_str->mmdb_as_loaded)
79+
MMDB_close((MMDB_s *)ndpi_str->mmdb_as);
7680

7781
ndpi_free(ndpi_str->mmdb_city);
7882
ndpi_free(ndpi_str->mmdb_as);
@@ -83,89 +87,175 @@ void ndpi_free_geoip(struct ndpi_detection_module_struct *ndpi_str) {
8387

8488
/* ********************************************************************************* */
8589

86-
int ndpi_get_geoip_asn(struct ndpi_detection_module_struct *ndpi_str, char *ip, u_int32_t *asn) {
90+
int ndpi_get_geoip_asn(struct ndpi_detection_module_struct *ndpi_str, char *ip, u_int32_t *asn)
91+
{
8792
#ifdef HAVE_MAXMINDDB
8893
int gai_error, mmdb_error, status;
8994
MMDB_lookup_result_s result;
9095
MMDB_entry_data_s entry_data;
9196

92-
if(ndpi_str->mmdb_as_loaded) {
93-
result = MMDB_lookup_string((MMDB_s*)ndpi_str->mmdb_as, ip, &gai_error, &mmdb_error);
97+
if (ndpi_str->mmdb_as_loaded)
98+
{
99+
result = MMDB_lookup_string((MMDB_s *)ndpi_str->mmdb_as, ip, &gai_error, &mmdb_error);
94100

95-
if((gai_error != 0)
96-
|| (mmdb_error != MMDB_SUCCESS)
97-
|| (!result.found_entry))
101+
if ((gai_error != 0) || (mmdb_error != MMDB_SUCCESS) || (!result.found_entry))
98102
*asn = 0;
99-
else {
103+
else
104+
{
100105
/* Get the ASN */
101-
if((status = MMDB_get_value(&result.entry, &entry_data, "autonomous_system_number", NULL)) == MMDB_SUCCESS) {
102-
if(entry_data.has_data && entry_data.type == MMDB_DATA_TYPE_UINT32)
103-
*asn = entry_data.uint32;
104-
else
105-
*asn = 0;
106+
if ((status = MMDB_get_value(&result.entry, &entry_data, "autonomous_system_number", NULL)) == MMDB_SUCCESS)
107+
{
108+
if (entry_data.has_data && entry_data.type == MMDB_DATA_TYPE_UINT32)
109+
*asn = entry_data.uint32;
110+
else
111+
*asn = 0;
106112
}
107113
}
108114

109-
return(0);
115+
return (0);
110116
}
111117
#else
112118
(void)ndpi_str;
113119
(void)ip;
114120
(void)asn;
115121
#endif
116122

117-
return(-2);
123+
return (-2);
118124
}
119-
125+
120126
/* ********************************************************************************* */
121127

122128
int ndpi_get_geoip_country_continent(struct ndpi_detection_module_struct *ndpi_str, char *ip,
123-
char *country_code, u_int8_t country_code_len,
124-
char *continent, u_int8_t continent_len) {
129+
char *country_code, u_int8_t country_code_len,
130+
char *continent, u_int8_t continent_len)
131+
{
125132
#ifdef HAVE_MAXMINDDB
126133
int gai_error, mmdb_error;
127134
MMDB_lookup_result_s result;
128135
MMDB_entry_data_s entry_data;
129136

130-
131-
if(ndpi_str->mmdb_city_loaded) {
137+
if (ndpi_str->mmdb_city_loaded)
138+
{
132139
int status;
133140

134-
result = MMDB_lookup_string((MMDB_s*)ndpi_str->mmdb_city, ip, &gai_error, &mmdb_error);
141+
result = MMDB_lookup_string((MMDB_s *)ndpi_str->mmdb_city, ip, &gai_error, &mmdb_error);
135142

136-
if((gai_error != 0)
137-
|| (mmdb_error != MMDB_SUCCESS)
138-
|| (!result.found_entry))
143+
if ((gai_error != 0) || (mmdb_error != MMDB_SUCCESS) || (!result.found_entry))
139144
country_code[0] = '\0';
140-
else {
141-
if(country_code_len > 0) {
142-
status = MMDB_get_value(&result.entry, &entry_data, "country", "iso_code", NULL);
143-
144-
if((status != MMDB_SUCCESS) || (!entry_data.has_data))
145-
country_code[0] = '\0';
146-
else {
147-
int str_len = ndpi_min(entry_data.data_size, country_code_len);
148-
149-
memcpy(country_code, entry_data.utf8_string, str_len);
150-
country_code[str_len] = '\0';
151-
}
145+
else
146+
{
147+
if (country_code_len > 0)
148+
{
149+
status = MMDB_get_value(&result.entry, &entry_data, "country", "iso_code", NULL);
150+
151+
if ((status != MMDB_SUCCESS) || (!entry_data.has_data))
152+
country_code[0] = '\0';
153+
else
154+
{
155+
int str_len = ndpi_min(entry_data.data_size, country_code_len);
156+
157+
memcpy(country_code, entry_data.utf8_string, str_len);
158+
country_code[str_len] = '\0';
159+
}
152160
}
153161

154-
if(continent_len > 0) {
155-
status = MMDB_get_value(&result.entry, &entry_data, "continent", "names", "en", NULL);
156-
157-
if((status != MMDB_SUCCESS) || (!entry_data.has_data))
158-
continent[0] = '\0';
159-
else {
160-
int str_len = ndpi_min(entry_data.data_size, continent_len);
161-
162-
memcpy(continent, entry_data.utf8_string, str_len);
163-
continent[str_len] = '\0';
164-
}
162+
if (continent_len > 0)
163+
{
164+
status = MMDB_get_value(&result.entry, &entry_data, "continent", "names", "en", NULL);
165+
166+
if ((status != MMDB_SUCCESS) || (!entry_data.has_data))
167+
continent[0] = '\0';
168+
else
169+
{
170+
int str_len = ndpi_min(entry_data.data_size, continent_len);
171+
172+
memcpy(continent, entry_data.utf8_string, str_len);
173+
continent[str_len] = '\0';
174+
}
165175
}
166176
}
167177

168-
return(0);
178+
return (0);
179+
}
180+
#else
181+
(void)ndpi_str;
182+
(void)ip;
183+
(void)country_code;
184+
(void)country_code_len;
185+
(void)continent;
186+
(void)continent_len;
187+
#endif
188+
189+
return (-2);
190+
}
191+
192+
int ndpi_get_geoip_country_continent_city(struct ndpi_detection_module_struct *ndpi_str, char *ip,
193+
char *country_code, u_int8_t country_code_len,
194+
char *continent, u_int8_t continent_len,
195+
char *city, u_int8_t city_len)
196+
{
197+
#ifdef HAVE_MAXMINDDB
198+
int gai_error, mmdb_error;
199+
MMDB_lookup_result_s result;
200+
MMDB_entry_data_s entry_data;
201+
202+
if (ndpi_str->mmdb_city_loaded)
203+
{
204+
int status;
205+
206+
result = MMDB_lookup_string((MMDB_s *)ndpi_str->mmdb_city, ip, &gai_error, &mmdb_error);
207+
208+
if ((gai_error != 0) || (mmdb_error != MMDB_SUCCESS) || (!result.found_entry))
209+
country_code[0] = '\0';
210+
else
211+
{
212+
if (country_code_len > 0)
213+
{
214+
status = MMDB_get_value(&result.entry, &entry_data, "country", "iso_code", NULL);
215+
216+
if ((status != MMDB_SUCCESS) || (!entry_data.has_data))
217+
country_code[0] = '\0';
218+
else
219+
{
220+
int str_len = ndpi_min(entry_data.data_size, country_code_len);
221+
222+
memcpy(country_code, entry_data.utf8_string, str_len);
223+
country_code[str_len] = '\0';
224+
}
225+
}
226+
227+
if (continent_len > 0)
228+
{
229+
status = MMDB_get_value(&result.entry, &entry_data, "continent", "names", "en", NULL);
230+
231+
if ((status != MMDB_SUCCESS) || (!entry_data.has_data))
232+
continent[0] = '\0';
233+
else
234+
{
235+
int str_len = ndpi_min(entry_data.data_size, continent_len);
236+
237+
memcpy(continent, entry_data.utf8_string, str_len);
238+
continent[str_len] = '\0';
239+
}
240+
}
241+
242+
if (city_len > 0)
243+
{
244+
status = MMDB_get_value(&result.entry, &entry_data, "city", "names", "en", NULL);
245+
246+
if ((status != MMDB_SUCCESS) || (!entry_data.has_data))
247+
city[0] = '\0';
248+
else
249+
{
250+
int len = ndpi_min(entry_data.data_size, city_len);
251+
252+
memcpy(city, entry_data.utf8_string, len);
253+
city[len] = 0;
254+
}
255+
}
256+
257+
return (0);
258+
}
169259
}
170260
#else
171261
(void)ndpi_str;
@@ -174,7 +264,9 @@ int ndpi_get_geoip_country_continent(struct ndpi_detection_module_struct *ndpi_s
174264
(void)country_code_len;
175265
(void)continent;
176266
(void)continent_len;
267+
(void)city;
268+
(void)city_len;
177269
#endif
178270

179-
return(-2);
271+
return (-2);
180272
}

0 commit comments

Comments
 (0)