Geoip: Update libmaxminddb to 1.0.4

This commit is contained in:
Arkshine 2015-01-11 19:15:19 +01:00
parent b4399f42da
commit 16874378f1
5 changed files with 159 additions and 119 deletions

View File

@ -3,17 +3,22 @@
#endif
#include "maxminddb.h"
#include "maxminddb-compat-util.h"
#include <errno.h>
#include <fcntl.h>
#define __STDC_FORMAT_MACROS /* Arkshine: Force to use C99 format macros */
#if defined(_WIN32) && _MSC_VER < 1800 /* Arkshine: C99 supported only from VS++ 2013 */
# include "msvc10-c99-headers/inttypes.h"
# define va_copy(d,s) ((d) = (s))
#else
# include <inttypes.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#define __STDC_FORMAT_MACROS /* Arkshine: Force to use C99 format macros */
#include <inttypes.h> /* MSVC doesn't fully support C99, so we provide the file */
#ifdef _WIN32
#include <Windows.h>
#include <Ws2ipdef.h>
#include <windows.h>
#include <ws2ipdef.h>
#else
#include <arpa/inet.h>
#include <sys/mman.h>
@ -140,6 +145,7 @@ LOCAL int populate_result(MMDB_s *mmdb, uint32_t node_count, uint32_t value,
uint16_t netmask, MMDB_lookup_result_s *result);
LOCAL uint32_t get_left_28_bit_record(const uint8_t *record);
LOCAL uint32_t get_right_28_bit_record(const uint8_t *record);
LOCAL int path_length(va_list va_path);
LOCAL int lookup_path_in_array(const char *path_elem, MMDB_s *mmdb,
MMDB_entry_data_s *entry_data);
LOCAL int lookup_path_in_map(const char *path_elem, MMDB_s *mmdb,
@ -189,6 +195,8 @@ LOCAL char *bytes_to_hex(uint8_t *bytes, uint32_t size);
} \
} while (0)
#define FREE_AND_SET_NULL(p) { free((void *)(p)); (p) = NULL; }
int MMDB_open(const char *const filename, uint32_t flags, MMDB_s *const mmdb)
{
mmdb->file_content = NULL;
@ -212,6 +220,11 @@ int MMDB_open(const char *const filename, uint32_t flags, MMDB_s *const mmdb)
return MMDB_FILE_OPEN_ERROR;
}
size = GetFileSize(fd, NULL);
if (size == INVALID_FILE_SIZE) {
free_mmdb_struct(mmdb);
CloseHandle(fd);
return MMDB_FILE_OPEN_ERROR;
}
#else
int fd = open(filename, O_RDONLY);
if (fd < 0) {
@ -220,7 +233,11 @@ int MMDB_open(const char *const filename, uint32_t flags, MMDB_s *const mmdb)
}
struct stat s;
fstat(fd, &s);
if (fstat(fd, &s) ) {
free_mmdb_struct(mmdb);
close(fd);
return MMDB_FILE_OPEN_ERROR;
}
size = s.st_size;
#endif
@ -231,18 +248,19 @@ int MMDB_open(const char *const filename, uint32_t flags, MMDB_s *const mmdb)
mmdb->file_size = size;
#ifdef _WIN32
HANDLE mmh = CreateFileMappingA(fd, NULL, PAGE_READONLY, 0, size, filename);
HANDLE mmh = CreateFileMappingA(fd, NULL, PAGE_READONLY, 0, size, NULL);
uint8_t *file_content =
(uint8_t *)MapViewOfFile(mmh, FILE_MAP_READ, 0, 0, 0);
CloseHandle(fd);
if (file_content == NULL) {
CloseHandle(mmh);
CloseHandle(fd);
free_mmdb_struct(mmdb);
return MMDB_IO_ERROR;
}
#else
uint8_t *file_content =
(uint8_t *)mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
if (MAP_FAILED == file_content) {
free_mmdb_struct(mmdb);
return MMDB_IO_ERROR;
@ -273,9 +291,6 @@ int MMDB_open(const char *const filename, uint32_t flags, MMDB_s *const mmdb)
#ifdef _WIN32
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
CloseHandle(fd);
#else
close(fd);
#endif
uint32_t search_tree_size = mmdb->metadata.node_count *
@ -864,41 +879,44 @@ int MMDB_vget_value(MMDB_entry_s *const start,
MMDB_entry_data_s *const entry_data,
va_list va_path)
{
const char **path = NULL;
int i = 0;
int length = path_length(va_path);
const char *path_elem;
while (NULL != (path_elem = va_arg(va_path, char *))) {
path = (const char **)realloc(path, sizeof(const char *) * (i + 1));
if (NULL == path) {
return MMDB_OUT_OF_MEMORY_ERROR;
}
int i = 0;
path[i] = mmdb_strdup(path_elem);
if (NULL == path[i]) {
return MMDB_OUT_OF_MEMORY_ERROR;
}
i++;
}
path = (const char **)realloc(path, sizeof(char *) * (i + 1));
const char **path = (const char **)malloc((length + 1) * sizeof(const char *));
if (NULL == path) {
return MMDB_OUT_OF_MEMORY_ERROR;
}
while (NULL != (path_elem = va_arg(va_path, char *))) {
path[i] = path_elem;
i++;
}
path[i] = NULL;
int status = MMDB_aget_value(start, entry_data, path);
i = 0;
while (NULL != path[i]) {
free((void *)path[i]);
i++;
}
free(path);
free((char **)path);
return status;
}
LOCAL int path_length(va_list va_path)
{
int i = 0;
const char *ignore;
va_list path_copy;
va_copy(path_copy, va_path);
while (NULL != (ignore = va_arg(path_copy, char *))) {
i++;
}
va_end(path_copy);
return i;
}
int MMDB_aget_value(MMDB_entry_s *const start,
MMDB_entry_data_s *const entry_data,
const char *const *const path)
@ -960,13 +978,18 @@ LOCAL int lookup_path_in_array(const char *path_elem, MMDB_s *mmdb,
MMDB_entry_data_s *entry_data)
{
uint32_t size = entry_data->data_size;
int array_index = strtol(path_elem, NULL, 10);
if (array_index < 0) {
char *first_invalid;
int saved_errno = errno;
errno = 0;
int array_index = strtol(path_elem, &first_invalid, 10);
if (array_index < 0 || ERANGE == errno) {
errno = saved_errno;
return MMDB_INVALID_LOOKUP_PATH_ERROR;
}
errno = saved_errno;
if ((uint32_t)array_index >= size) {
memset(entry_data, 0, sizeof(MMDB_entry_data_s));
if (*first_invalid || (uint32_t)array_index >= size) {
return MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR;
}
@ -1276,7 +1299,7 @@ int MMDB_get_entry_data_list(
MMDB_entry_s *start, MMDB_entry_data_list_s **const entry_data_list)
{
*entry_data_list = new_entry_data_list();
if (NULL == entry_data_list) {
if (NULL == *entry_data_list) {
return MMDB_OUT_OF_MEMORY_ERROR;
}
return get_entry_data_list(start->mmdb, start->offset, *entry_data_list);
@ -1484,7 +1507,7 @@ LOCAL void free_mmdb_struct(MMDB_s *const mmdb)
}
if (NULL != mmdb->filename) {
free((void *)mmdb->filename);
FREE_AND_SET_NULL(mmdb->filename);
}
if (NULL != mmdb->file_content) {
#ifdef _WIN32
@ -1498,7 +1521,7 @@ LOCAL void free_mmdb_struct(MMDB_s *const mmdb)
}
if (NULL != mmdb->metadata.database_type) {
free((void *)mmdb->metadata.database_type);
FREE_AND_SET_NULL(mmdb->metadata.database_type);
}
free_languages_metadata(mmdb);
@ -1512,9 +1535,9 @@ LOCAL void free_languages_metadata(MMDB_s *mmdb)
}
for (size_t i = 0; i < mmdb->metadata.languages.count; i++) {
free((char *)mmdb->metadata.languages.names[i]);
FREE_AND_SET_NULL(mmdb->metadata.languages.names[i]);
}
free(mmdb->metadata.languages.names);
FREE_AND_SET_NULL(mmdb->metadata.languages.names);
}
LOCAL void free_descriptions_metadata(MMDB_s *mmdb)
@ -1527,22 +1550,20 @@ LOCAL void free_descriptions_metadata(MMDB_s *mmdb)
if (NULL != mmdb->metadata.description.descriptions[i]) {
if (NULL !=
mmdb->metadata.description.descriptions[i]->language) {
free(
(char *)mmdb->metadata.description.descriptions[i]->
language);
FREE_AND_SET_NULL(
mmdb->metadata.description.descriptions[i]->language);
}
if (NULL !=
mmdb->metadata.description.descriptions[i]->description) {
free(
(char *)mmdb->metadata.description.descriptions[i]->
description);
FREE_AND_SET_NULL(
mmdb->metadata.description.descriptions[i]->description);
}
free(mmdb->metadata.description.descriptions[i]);
FREE_AND_SET_NULL(mmdb->metadata.description.descriptions[i]);
}
}
free(mmdb->metadata.description.descriptions);
FREE_AND_SET_NULL(mmdb->metadata.description.descriptions);
}
const char *MMDB_lib_version(void)

View File

@ -1,3 +1,7 @@
#ifdef __cplusplus
extern "C" {
#endif
#ifndef MAXMINDDB_H
#define MAXMINDDB_H
@ -7,83 +11,33 @@
#include "maxminddb_config.h"
#include <stdarg.h>
#if defined(_WIN32) && _MSC_VER < 1800 /* Arkshine: C99 supported only from VS 2013 */
# include "msvc10-c99-headers/stdbool.h"
#else
# include <stdbool.h>
#endif
#include <stdint.h>
#include <stdio.h>
#include <sys/types.h>
#ifdef _WIN32
#include "stdbool.h" /* Arksnine: Not supported by MSVC */
#include <BaseTsd.h> /* Arkshine: ssize_t replacement */
typedef SSIZE_T ssize_t;
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <winsock2.h>
#include <ws2tcpip.h>
typedef ADDRESS_FAMILY sa_family_t;
#if defined(_MSC_VER)
/* MSVC doesn't define signed size_t, copy it from configure */
#define ssize_t int
#endif
#else
#include <stdbool.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#endif
const char GeoIPCountryCode[][3] =
{
"AP", "EU", "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AN",
"AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AZ", "BA", "BB",
"BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BM", "BN", "BO",
"BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC", "CD",
"CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR",
"CU", "CV", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO",
"DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ",
"FK", "FM", "FO", "FR", "FX", "GA", "GB", "GD", "GE", "GF",
"GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT",
"GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID",
"IE", "IL", "IN", "IO", "IQ", "IR", "IS", "IT", "JM", "JO",
"JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW",
"KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT",
"LU", "LV", "LY", "MA", "MC", "MD", "MG", "MH", "MK", "ML",
"MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV",
"MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NF", "NG", "NI",
"NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF",
"PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW",
"PY", "QA", "RE", "RO", "RU", "RW", "SA", "SB", "SC", "SD",
"SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO",
"SR", "ST", "SV", "SY", "SZ", "TC", "TD", "TF", "TG", "TH",
"TJ", "TK", "TM", "TN", "TO", "TL", "TR", "TT", "TV", "TW",
"TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE",
"VG", "VI", "VN", "VU", "WF", "WS", "YE", "YT", "RS", "ZA",
"ZM", "ME", "ZW", "A1", "A2", "O1", "AX", "GG", "IM", "JE",
"BL", "MF"
};
const char GeoIPCountryCode3[][4] =
{
"AP", "EU", "AND", "ARE", "AFG", "ATG", "AIA", "ALB", "ARM", "ANT",
"AGO", "AQ", "ARG", "ASM", "AUT", "AUS", "ABW", "AZE", "BIH", "BRB",
"BGD", "BEL", "BFA", "BGR", "BHR", "BDI", "BEN", "BMU", "BRN", "BOL",
"BRA", "BHS", "BTN", "BV", "BWA", "BLR", "BLZ", "CAN", "CC", "COD",
"CAF", "COG", "CHE", "CIV", "COK", "CHL", "CMR", "CHN", "COL", "CRI",
"CUB", "CPV", "CX", "CYP", "CZE", "DEU", "DJI", "DNK", "DMA", "DOM",
"DZA", "ECU", "EST", "EGY", "ESH", "ERI", "ESP", "ETH", "FIN", "FJI",
"FLK", "FSM", "FRO", "FRA", "FX", "GAB", "GBR", "GRD", "GEO", "GUF",
"GHA", "GIB", "GRL", "GMB", "GIN", "GLP", "GNQ", "GRC", "GS", "GTM",
"GUM", "GNB", "GUY", "HKG", "HM", "HND", "HRV", "HTI", "HUN", "IDN",
"IRL", "ISR", "IND", "IO", "IRQ", "IRN", "ISL", "ITA", "JAM", "JOR",
"JPN", "KEN", "KGZ", "KHM", "KIR", "COM", "KNA", "PRK", "KOR", "KWT",
"CYM", "KAZ", "LAO", "LBN", "LCA", "LIE", "LKA", "LBR", "LSO", "LTU",
"LUX", "LVA", "LBY", "MAR", "MCO", "MDA", "MDG", "MHL", "MKD", "MLI",
"MMR", "MNG", "MAC", "MNP", "MTQ", "MRT", "MSR", "MLT", "MUS", "MDV",
"MWI", "MEX", "MYS", "MOZ", "NAM", "NCL", "NER", "NFK", "NGA", "NIC",
"NLD", "NOR", "NPL", "NRU", "NIU", "NZL", "OMN", "PAN", "PER", "PYF",
"PNG", "PHL", "PAK", "POL", "SPM", "PCN", "PRI", "PSE", "PRT", "PLW",
"PRY", "QAT", "REU", "ROU", "RUS", "RWA", "SAU", "SLB", "SYC", "SDN",
"SWE", "SGP", "SHN", "SVN", "SJM", "SVK", "SLE", "SMR", "SEN", "SOM",
"SUR", "STP", "SLV", "SYR", "SWZ", "TCA", "TCD", "TF", "TGO", "THA",
"TJK", "TKL", "TKM", "TUN", "TON", "TLS", "TUR", "TTO", "TUV", "TWN",
"TZA", "UKR", "UGA", "UM", "USA", "URY", "UZB", "VAT", "VCT", "VEN",
"VGB", "VIR", "VNM", "VUT", "WLF", "WSM", "YEM", "YT", "SRB", "ZAF",
"ZMB", "MNE", "ZWE", "A1", "A2", "O1", "ALA", "GGY", "IMN", "JEY",
"BLM", "MAF"
};
/* libmaxminddb package version from configure */
#define PACKAGE_VERSION "1.0.4"
#define MMDB_DATA_TYPE_EXTENDED (0)
#define MMDB_DATA_TYPE_POINTER (1)
@ -102,11 +56,11 @@ const char GeoIPCountryCode3[][4] =
#define MMDB_DATA_TYPE_BOOLEAN (14)
#define MMDB_DATA_TYPE_FLOAT (15)
/* GEOIPDB flags */
/* flags for open */
#define MMDB_MODE_MMAP (1)
#define MMDB_MODE_MASK (7)
/* GEOIPDB err codes */
/* error codes */
#define MMDB_SUCCESS (0)
#define MMDB_FILE_OPEN_ERROR (1)
#define MMDB_CORRUPT_SEARCH_TREE_ERROR (2)
@ -265,3 +219,7 @@ typedef struct MMDB_search_node_s {
/* *INDENT-ON* */
#endif /* MAXMINDDB_H */
#ifdef __cplusplus
}
#endif

View File

@ -1,8 +1,6 @@
#ifndef MAXMINDDB_CONFIG_H
#define MAXMINDDB_CONFIG_H
#define PACKAGE_VERSION "0.5.6" // Arkshine: added manually.
#ifndef MMDB_UINT128_USING_MODE
/* Define as 1 if we we use unsigned int __atribute__ ((__mode__(TI))) for uint128 values */
#define MMDB_UINT128_USING_MODE 0

View File

@ -14,6 +14,66 @@
#include "geoip_util.h"
#include "geoip_natives.h"
const char GeoIPCountryCode[252][3] =
{
"AP", "EU", "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AN",
"AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AZ", "BA", "BB",
"BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BM", "BN", "BO",
"BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC", "CD",
"CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR",
"CU", "CV", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO",
"DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ",
"FK", "FM", "FO", "FR", "FX", "GA", "GB", "GD", "GE", "GF",
"GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT",
"GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID",
"IE", "IL", "IN", "IO", "IQ", "IR", "IS", "IT", "JM", "JO",
"JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW",
"KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT",
"LU", "LV", "LY", "MA", "MC", "MD", "MG", "MH", "MK", "ML",
"MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV",
"MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NF", "NG", "NI",
"NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF",
"PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW",
"PY", "QA", "RE", "RO", "RU", "RW", "SA", "SB", "SC", "SD",
"SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO",
"SR", "ST", "SV", "SY", "SZ", "TC", "TD", "TF", "TG", "TH",
"TJ", "TK", "TM", "TN", "TO", "TL", "TR", "TT", "TV", "TW",
"TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE",
"VG", "VI", "VN", "VU", "WF", "WS", "YE", "YT", "RS", "ZA",
"ZM", "ME", "ZW", "A1", "A2", "O1", "AX", "GG", "IM", "JE",
"BL", "MF"
};
const char GeoIPCountryCode3[252][4] =
{
"AP", "EU", "AND", "ARE", "AFG", "ATG", "AIA", "ALB", "ARM", "ANT",
"AGO", "AQ", "ARG", "ASM", "AUT", "AUS", "ABW", "AZE", "BIH", "BRB",
"BGD", "BEL", "BFA", "BGR", "BHR", "BDI", "BEN", "BMU", "BRN", "BOL",
"BRA", "BHS", "BTN", "BV", "BWA", "BLR", "BLZ", "CAN", "CC", "COD",
"CAF", "COG", "CHE", "CIV", "COK", "CHL", "CMR", "CHN", "COL", "CRI",
"CUB", "CPV", "CX", "CYP", "CZE", "DEU", "DJI", "DNK", "DMA", "DOM",
"DZA", "ECU", "EST", "EGY", "ESH", "ERI", "ESP", "ETH", "FIN", "FJI",
"FLK", "FSM", "FRO", "FRA", "FX", "GAB", "GBR", "GRD", "GEO", "GUF",
"GHA", "GIB", "GRL", "GMB", "GIN", "GLP", "GNQ", "GRC", "GS", "GTM",
"GUM", "GNB", "GUY", "HKG", "HM", "HND", "HRV", "HTI", "HUN", "IDN",
"IRL", "ISR", "IND", "IO", "IRQ", "IRN", "ISL", "ITA", "JAM", "JOR",
"JPN", "KEN", "KGZ", "KHM", "KIR", "COM", "KNA", "PRK", "KOR", "KWT",
"CYM", "KAZ", "LAO", "LBN", "LCA", "LIE", "LKA", "LBR", "LSO", "LTU",
"LUX", "LVA", "LBY", "MAR", "MCO", "MDA", "MDG", "MHL", "MKD", "MLI",
"MMR", "MNG", "MAC", "MNP", "MTQ", "MRT", "MSR", "MLT", "MUS", "MDV",
"MWI", "MEX", "MYS", "MOZ", "NAM", "NCL", "NER", "NFK", "NGA", "NIC",
"NLD", "NOR", "NPL", "NRU", "NIU", "NZL", "OMN", "PAN", "PER", "PYF",
"PNG", "PHL", "PAK", "POL", "SPM", "PCN", "PRI", "PSE", "PRT", "PLW",
"PRY", "QAT", "REU", "ROU", "RUS", "RWA", "SAU", "SLB", "SYC", "SDN",
"SWE", "SGP", "SHN", "SVN", "SJM", "SVK", "SLE", "SMR", "SEN", "SOM",
"SUR", "STP", "SLV", "SYR", "SWZ", "TCA", "TCD", "TF", "TGO", "THA",
"TJK", "TKL", "TKM", "TUN", "TON", "TLS", "TUR", "TTO", "TUV", "TWN",
"TZA", "UKR", "UGA", "UM", "USA", "URY", "UZB", "VAT", "VCT", "VEN",
"VGB", "VIR", "VNM", "VUT", "WLF", "WSM", "YEM", "YT", "SRB", "ZAF",
"ZMB", "MNE", "ZWE", "A1", "A2", "O1", "ALA", "GGY", "IMN", "JEY",
"BLM", "MAF"
};
char *stripPort(char *ip)
{
char *tmp = strchr(ip, ':');

View File

@ -27,4 +27,7 @@ const char *getLang(int playerIndex);
const char* stristr(const char* str, const char* substr);
extern const char GeoIPCountryCode[252][3];
extern const char GeoIPCountryCode3[252][4];
#endif // _INCLUDE_GEOIPUTIL_H