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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
/*
* ndpi_geoip.c
*
* Copyright (C) 2021 - ntop.org
*
* This file is part of nDPI, an open source deep packet inspection
* library.
*
* nDPI is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* nDPI 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with nDPI. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifdef HAVE_CONFIG_H
#include "ndpi_config.h"
#endif
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <stdint.h>
#include "ndpi_api.h"
#include "ndpi_config.h"
/* ********************************************************************************* */
int ndpi_load_geeoip(struct ndpi_detection_module_struct *ndpi_str,
const char *ip_city_data, const char *ip_as_data) {
#ifdef HAVE_MAXMINDDB
int status;
/* Open the MMDB files */
if((status = MMDB_open(ip_city_data, MMDB_MODE_MMAP, &ndpi_str->mmdb_city)) != MMDB_SUCCESS)
return(-1);
else
ndpi_str->mmdb_city_loaded = 1;
if((status = MMDB_open(ip_as_data, MMDB_MODE_MMAP, &ndpi_str->mmdb_as)) != MMDB_SUCCESS)
return(-2);
else
ndpi_str->mmdb_as_loaded = 1;
return(0);
#else
return(-1);
#endif
}
/* ********************************************************************************* */
void ndpi_free_geeoip(struct ndpi_detection_module_struct *ndpi_str) {
#ifdef HAVE_MAXMINDDB
if(ndpi_str->mmdb_city_loaded) MMDB_close(&ndpi_str->mmdb_city);
if(ndpi_str->mmdb_as_loaded) MMDB_close(&ndpi_str->mmdb_as);
#endif
}
/* ********************************************************************************* */
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;
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);
}
}
}
#endif
return(-2);
}
|