-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGeoIP.cpp
97 lines (77 loc) · 2.7 KB
/
GeoIP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
*
* (C) 2021-24 - ntop.org
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include "include.h"
#ifndef min
#define min(a,b) (a < b ? a : b)
#endif
/* ********************************************************************************* */
bool GeoIP::loadCountry(const char *ip_country_data) {
if(MMDB_open(ip_country_data, MMDB_MODE_MMAP, &mmdb_country) != MMDB_SUCCESS) {
trace->traceEvent(TRACE_ERROR, "Unable to load %s", ip_country_data);
return(false);
} else
trace->traceEvent(TRACE_NORMAL, "Successfully loaded %s", ip_country_data);
loaded = true;
return(true);
}
/* ********************************************************************************* */
GeoIP::~GeoIP() {
if(loaded)
MMDB_close(&mmdb_country);
}
/* ********************************************************************************* */
bool GeoIP::lookup(char *ip,
char *country_code, u_int8_t country_code_len,
char *continent, u_int8_t continent_len) {
int gai_error, mmdb_error;
MMDB_lookup_result_s result;
MMDB_entry_data_s entry_data;
result = MMDB_lookup_string(&mmdb_country, ip, &gai_error, &mmdb_error);
if((gai_error != 0)
|| (mmdb_error != MMDB_SUCCESS)
|| (!result.found_entry)) {
country_code[0] = '\0';
return(false);
} else {
int status;
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 = min(entry_data.data_size, country_code_len);
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", "code", NULL);
if((status != MMDB_SUCCESS) || (!entry_data.has_data))
continent[0] = '\0';
else {
int str_len = min(entry_data.data_size, continent_len);
memcpy(continent, entry_data.utf8_string, str_len);
continent[str_len] = '\0';
}
}
}
return(true);
}