aboutsummaryrefslogtreecommitdiff
path: root/src/lib/ndpi_geoip.c
blob: c3308b48bba202fe76cd1160414ef3f06d659219 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * 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/>.
 *
 */

#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <stdint.h>

#include "ndpi_api.h"
#include "ndpi_config.h"

#ifdef HAVE_MAXMINDDB
#include <maxminddb.h>
#endif

/* ********************************************************************************* */

int ndpi_load_geoip(struct ndpi_detection_module_struct *ndpi_str,
		     const char *ip_city_data, const char *ip_as_data) {
#ifdef HAVE_MAXMINDDB
  int status;

  ndpi_str->mmdb_city = (void*)ndpi_malloc(sizeof(MMDB_s));
  ndpi_str->mmdb_as   = (void*)ndpi_malloc(sizeof(MMDB_s));
  
  if((ndpi_str->mmdb_city == NULL) || (ndpi_str->mmdb_as == NULL))
    return(-1);
  
  /* Open the MMDB files */
  if((status = MMDB_open(ip_city_data, MMDB_MODE_MMAP, (MMDB_s*)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, (MMDB_s*)ndpi_str->mmdb_as)) != MMDB_SUCCESS)
    return(-2);
  else
    ndpi_str->mmdb_as_loaded = 1;

  return(0);
#else
  return(-3);
#endif
}

/* ********************************************************************************* */

void ndpi_free_geoip(struct ndpi_detection_module_struct *ndpi_str) {
#ifdef HAVE_MAXMINDDB
  if(ndpi_str->mmdb_city_loaded) MMDB_close((MMDB_s*)ndpi_str->mmdb_city);
  if(ndpi_str->mmdb_as_loaded)   MMDB_close((MMDB_s*)ndpi_str->mmdb_as);

  ndpi_free(ndpi_str->mmdb_city);
  ndpi_free(ndpi_str->mmdb_as);
#endif
}

/* ********************************************************************************* */

int ndpi_get_geoip_asn(struct ndpi_detection_module_struct *ndpi_str, char *ip, u_int32_t *asn) {
#ifdef HAVE_MAXMINDDB
  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((MMDB_s*)ndpi_str->mmdb_as, ip, &gai_error, &mmdb_error);

    if((gai_error != 0)
       || (mmdb_error != MMDB_SUCCESS)
       || (!result.found_entry))
      *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;
      }
    }

    return(0);
  }
#endif

  return(-2);
}
    
/* ********************************************************************************* */

int ndpi_get_geoip_country_continent(struct ndpi_detection_module_struct *ndpi_str, char *ip,
				     char *country_code, u_int8_t country_code_len,
				     char *continent, u_int8_t continent_len) {
#ifdef HAVE_MAXMINDDB
  int gai_error, mmdb_error;
  MMDB_lookup_result_s result;
  MMDB_entry_data_s entry_data;

     
  if(ndpi_str->mmdb_city_loaded) {
    int status;

    result = MMDB_lookup_string((MMDB_s*)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 {
      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 = ndpi_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", "names", "en", NULL);
	
	if((status != MMDB_SUCCESS) || (!entry_data.has_data))
	  continent[0] = '\0';
	else {
	  int str_len = ndpi_min(entry_data.data_size, continent_len);
	  
	  memcpy(continent, entry_data.utf8_string, str_len);
	  continent[str_len] = '\0';
	}
      }
    }

    return(0);
  }
#endif

  return(-2);
}