From b0563a493ce0661156c6080cdd7e37a7bbae9bfb Mon Sep 17 00:00:00 2001 From: Accelerator Date: Wed, 30 Jun 2021 16:01:12 +0500 Subject: [PATCH] Update GeoIP2 extension to new GeoLite2 .mmdb database format (#1245) * Add support for Maxmind GeoIP2 database files (#913). * Copy/paste error. * Mark GeoipCode3 as deprecated. * Fix build when compiling with AMBuild. * Replace loose libmaxminddb files with submodule. * Fix Linux build. * One more hack for submodule. * Actually fix Linux build. * GeoIP2 extension to sourcemod * Update basevotes When a player leaves during a voteban, he will be banned anyway. Also added a cvar with a ban time setting. * Update basevotes.sp * Update AMBuilder * ke::AString to std::string * Update extension.cpp * Update AMBuilder * Added coordination natives Added GeoipLatitude, GeoipLongitude, GeoipDistance natives. * Create osdefs.h * Update maxminddb_config.h * Update extension.cpp * Update extension.cpp * Added automatic search for database file * Fix automatic search for database file * Update extension.cpp * Update geoip.inc * .gitmodules revert * Update geoip.inc * Update libmaxminddb to version 1.5.2 * Update extension.cpp * Check language in the DB * Removed langCount variable * Determination of the client's language * Update geoip.inc * Update geoip.inc * Update extension.cpp * Update geoip.inc * Update extension.cpp * space instead of tab in .inc * Update extension.cpp * Update geoip.inc * Optimizing length measurement region code * Update package script to fetch the new GeoLite2 database This package is the last CC-BY-SA licensed GeoLite2-City database extracted from https://src.fedoraproject.org/rpms/geolite2 from december 2019. This doubles the download size for SM packages, but it's what we have to deal with atm :( * Fix potentially returning uninitialized memory in GeoipRegionCode If the lookup failed, we'd copy back whatever is on the stack in the ccode buffer. Co-authored-by: Nick Hastings Co-authored-by: Headline Co-authored-by: Accelerator74 Co-authored-by: Peace-Maker --- extensions/geoip/AMBuilder | 9 +- extensions/geoip/GeoIP.c | 969 ---------- extensions/geoip/GeoIP.h | 169 -- extensions/geoip/data-pool.c | 169 ++ extensions/geoip/data-pool.h | 52 + extensions/geoip/extension.cpp | 331 +++- extensions/geoip/extension.h | 3 + extensions/geoip/geoip_util.cpp | 209 +++ extensions/geoip/geoip_util.h | 46 + extensions/geoip/maxminddb-compat-util.h | 160 ++ extensions/geoip/maxminddb.c | 2092 ++++++++++++++++++++++ extensions/geoip/maxminddb.h | 258 +++ extensions/geoip/maxminddb_config.h | 16 + extensions/geoip/osdefs.h | 64 + extensions/geoip/smsdk_config.h | 4 +- plugins/include/geoip.inc | 147 +- tools/buildbot/package.pl | 44 +- 17 files changed, 3530 insertions(+), 1212 deletions(-) delete mode 100644 extensions/geoip/GeoIP.c delete mode 100644 extensions/geoip/GeoIP.h create mode 100644 extensions/geoip/data-pool.c create mode 100644 extensions/geoip/data-pool.h create mode 100644 extensions/geoip/geoip_util.cpp create mode 100644 extensions/geoip/geoip_util.h create mode 100644 extensions/geoip/maxminddb-compat-util.h create mode 100644 extensions/geoip/maxminddb.c create mode 100644 extensions/geoip/maxminddb.h create mode 100644 extensions/geoip/maxminddb_config.h create mode 100644 extensions/geoip/osdefs.h diff --git a/extensions/geoip/AMBuilder b/extensions/geoip/AMBuilder index 62f4c230..964cc273 100644 --- a/extensions/geoip/AMBuilder +++ b/extensions/geoip/AMBuilder @@ -8,13 +8,14 @@ for cxx in builder.targets: elif binary.compiler.family == 'msvc': binary.compiler.cxxflags += ['/GR-'] if binary.compiler.target.platform == 'windows': - binary.compiler.postlink += ['wsock32.lib'] + binary.compiler.postlink += ['wsock32.lib', 'ws2_32.lib'] binary.sources += [ 'extension.cpp', - 'GeoIP.c', - '../../public/smsdk_ext.cpp' + 'geoip_util.cpp', + '../../public/smsdk_ext.cpp', + 'data-pool.c', + 'maxminddb.c', ] SM.extensions += [builder.Add(binary)] - diff --git a/extensions/geoip/GeoIP.c b/extensions/geoip/GeoIP.c deleted file mode 100644 index 1090b893..00000000 --- a/extensions/geoip/GeoIP.c +++ /dev/null @@ -1,969 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ -/* GeoIP.c - * - * Copyright (C) 2006 MaxMind LLC - * - * This library 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 2.1 of the License, or (at your option) any later version. - * - * This library 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 this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#define GEOIPDATADIR "" - -#include "GeoIP.h" - -#ifndef _WIN32 -#include -#include -#include /* For ntohl */ -#include -#else -#include -#define snprintf _snprintf -#endif -#include -#include -#include -#include -#include -#include /* for fstat */ -#include /* for fstat */ - -#ifdef HAVE_STDINT_H -#include /* For uint32_t */ -#endif - -#ifndef INADDR_NONE -#define INADDR_NONE -1 -#endif - -#define COUNTRY_BEGIN 16776960 -#define STATE_BEGIN_REV0 16700000 -#define STATE_BEGIN_REV1 16000000 -#define STRUCTURE_INFO_MAX_SIZE 20 -#define DATABASE_INFO_MAX_SIZE 100 -#define MAX_ORG_RECORD_LENGTH 300 -#define US_OFFSET 1 -#define CANADA_OFFSET 677 -#define WORLD_OFFSET 1353 -#define FIPS_RANGE 360 - -#define CHECK_ERR(err, msg) { \ - if (err != Z_OK) { \ - fprintf(stderr, "%s error: %d\n", msg, err); \ - exit(1); \ - } \ -} - -const char GeoIP_country_code[251][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"}; - -const char GeoIP_country_code3[251][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"}; - -const char * GeoIP_country_name[251] = {"N/A","Asia/Pacific Region","Europe","Andorra","United Arab Emirates","Afghanistan","Antigua and Barbuda","Anguilla","Albania","Armenia","Netherlands Antilles", - "Angola","Antarctica","Argentina","American Samoa","Austria","Australia","Aruba","Azerbaijan","Bosnia and Herzegovina","Barbados", - "Bangladesh","Belgium","Burkina Faso","Bulgaria","Bahrain","Burundi","Benin","Bermuda","Brunei Darussalam","Bolivia", - "Brazil","Bahamas","Bhutan","Bouvet Island","Botswana","Belarus","Belize","Canada","Cocos (Keeling) Islands","Congo, The Democratic Republic of the", - "Central African Republic","Congo","Switzerland","Cote D'Ivoire","Cook Islands","Chile","Cameroon","China","Colombia","Costa Rica", - "Cuba","Cape Verde","Christmas Island","Cyprus","Czech Republic","Germany","Djibouti","Denmark","Dominica","Dominican Republic", - "Algeria","Ecuador","Estonia","Egypt","Western Sahara","Eritrea","Spain","Ethiopia","Finland","Fiji", - "Falkland Islands (Malvinas)","Micronesia, Federated States of","Faroe Islands","France","France, Metropolitan","Gabon","United Kingdom","Grenada","Georgia","French Guiana", - "Ghana","Gibraltar","Greenland","Gambia","Guinea","Guadeloupe","Equatorial Guinea","Greece","South Georgia and the South Sandwich Islands","Guatemala", - "Guam","Guinea-Bissau","Guyana","Hong Kong","Heard Island and McDonald Islands","Honduras","Croatia","Haiti","Hungary","Indonesia", - "Ireland","Israel","India","British Indian Ocean Territory","Iraq","Iran, Islamic Republic of","Iceland","Italy","Jamaica","Jordan", - "Japan","Kenya","Kyrgyzstan","Cambodia","Kiribati","Comoros","Saint Kitts and Nevis","Korea, Democratic People's Republic of","Korea, Republic of","Kuwait", - "Cayman Islands","Kazakhstan","Lao People's Democratic Republic","Lebanon","Saint Lucia","Liechtenstein","Sri Lanka","Liberia","Lesotho","Lithuania", - "Luxembourg","Latvia","Libyan Arab Jamahiriya","Morocco","Monaco","Moldova, Republic of","Madagascar","Marshall Islands","Macedonia","Mali", - "Myanmar","Mongolia","Macau","Northern Mariana Islands","Martinique","Mauritania","Montserrat","Malta","Mauritius","Maldives", - "Malawi","Mexico","Malaysia","Mozambique","Namibia","New Caledonia","Niger","Norfolk Island","Nigeria","Nicaragua", - "Netherlands","Norway","Nepal","Nauru","Niue","New Zealand","Oman","Panama","Peru","French Polynesia", - "Papua New Guinea","Philippines","Pakistan","Poland","Saint Pierre and Miquelon","Pitcairn Islands","Puerto Rico","Palestinian Territory","Portugal","Palau", - "Paraguay","Qatar","Reunion","Romania","Russian Federation","Rwanda","Saudi Arabia","Solomon Islands","Seychelles","Sudan", - "Sweden","Singapore","Saint Helena","Slovenia","Svalbard and Jan Mayen","Slovakia","Sierra Leone","San Marino","Senegal","Somalia","Suriname", - "Sao Tome and Principe","El Salvador","Syrian Arab Republic","Swaziland","Turks and Caicos Islands","Chad","French Southern Territories","Togo","Thailand", - "Tajikistan","Tokelau","Turkmenistan","Tunisia","Tonga","Timor-Leste","Turkey","Trinidad and Tobago","Tuvalu","Taiwan", - "Tanzania, United Republic of","Ukraine","Uganda","United States Minor Outlying Islands","United States","Uruguay","Uzbekistan","Holy See (Vatican City State)","Saint Vincent and the Grenadines","Venezuela", - "Virgin Islands, British","Virgin Islands, U.S.","Vietnam","Vanuatu","Wallis and Futuna","Samoa","Yemen","Mayotte","Serbia","South Africa", - "Zambia","Montenegro","Zimbabwe","Anonymous Proxy","Satellite Provider","Other","Aland Islands","Guernsey","Isle of Man","Jersey"}; - -const char GeoIP_country_continent[251][3] = {"--","AS","EU","EU","AS","AS","SA","SA","EU","AS","SA", - "AF","AN","SA","OC","EU","OC","SA","AS","EU","SA", - "AS","EU","AF","EU","AS","AF","AF","SA","AS","SA", - "SA","SA","AS","AF","AF","EU","SA","NA","AS","AF", - "AF","AF","EU","AF","OC","SA","AF","AS","SA","SA", - "SA","AF","AS","AS","EU","EU","AF","EU","SA","SA", - "AF","SA","EU","AF","AF","AF","EU","AF","EU","OC", - "SA","OC","EU","EU","EU","AF","EU","SA","AS","SA", - "AF","EU","SA","AF","AF","SA","AF","EU","SA","SA", - "OC","AF","SA","AS","AF","SA","EU","SA","EU","AS", - "EU","AS","AS","AS","AS","AS","EU","EU","SA","AS", - "AS","AF","AS","AS","OC","AF","SA","AS","AS","AS", - "SA","AS","AS","AS","SA","EU","AS","AF","AF","EU", - "EU","EU","AF","AF","EU","EU","AF","OC","EU","AF", - "AS","AS","AS","OC","SA","AF","SA","EU","AF","AS", - "AF","NA","AS","AF","AF","OC","AF","OC","AF","SA", - "EU","EU","AS","OC","OC","OC","AS","SA","SA","OC", - "OC","AS","AS","EU","SA","OC","SA","AS","EU","OC", - "SA","AS","AF","EU","AS","AF","AS","OC","AF","AF", - "EU","AS","AF","EU","EU","EU","AF","EU","AF","AF", - "SA","AF","SA","AS","AF","SA","AF","AF","AF","AS", - "AS","OC","AS","AF","OC","AS","AS","SA","OC","AS", - "AF","EU","AF","OC","NA","SA","AS","EU","SA","SA", - "SA","SA","AS","OC","OC","OC","AS","AF","EU","AF", - "AF","EU","AF","--","--","--","EU","EU","EU","EU"}; - -const char * GeoIPDBDescription[NUM_DB_TYPES] = {NULL, "GeoIP Country Edition", "GeoIP City Edition, Rev 1", "GeoIP Region Edition, Rev 1", "GeoIP ISP Edition", "GeoIP Organization Edition", "GeoIP City Edition, Rev 0", "GeoIP Region Edition, Rev 0","GeoIP Proxy Edition","GeoIP ASNum Edition","GeoIP Netspeed Edition","GeoIP Domain Name Edition"}; - -char * custom_directory = NULL; - -void GeoIP_setup_custom_directory (char * dir) { - custom_directory = dir; -} - -char *_GeoIP_full_path_to(const char *file_name) { - char *path = malloc(sizeof(char) * 1024); - - if (custom_directory == NULL){ -#ifndef _WIN32 - memset(path, 0, sizeof(char) * 1024); - snprintf(path, sizeof(char) * 1024 - 1, "%s/%s", GEOIPDATADIR, file_name); -#else - char buf[MAX_PATH], *p, *q = NULL; - int len; - memset(buf, 0, sizeof(buf)); - len = GetModuleFileName(GetModuleHandle(NULL), buf, sizeof(buf) - 1); - for (p = buf + len; p > buf; p--) - if (*p == '\\') - { - if (!q) - q = p; - else - *p = '/'; - } - *q = 0; - memset(path, 0, sizeof(char) * 1024); - snprintf(path, sizeof(char) * 1024 - 1, "%s/%s", buf, file_name); -#endif - } else { - size_t l = strlen(custom_directory); - if (custom_directory[l-1] != '/') { - snprintf(path, sizeof(char) * 1024 - 1, "%s/%s",custom_directory, file_name); - } else { - snprintf(path, sizeof(char) * 1024 - 1, "%s%s", custom_directory, file_name); - } - } - return path; -} - -char ** GeoIPDBFileName = NULL; - -void _GeoIP_setup_dbfilename() { - if (NULL == GeoIPDBFileName) { - GeoIPDBFileName = malloc(sizeof(char *) * NUM_DB_TYPES); - memset(GeoIPDBFileName, 0, sizeof(char *) * NUM_DB_TYPES); - - GeoIPDBFileName[GEOIP_COUNTRY_EDITION] = _GeoIP_full_path_to("GeoIP.dat"); - GeoIPDBFileName[GEOIP_REGION_EDITION_REV0] = _GeoIP_full_path_to("GeoIPRegion.dat"); - GeoIPDBFileName[GEOIP_REGION_EDITION_REV1] = _GeoIP_full_path_to("GeoIPRegion.dat"); - GeoIPDBFileName[GEOIP_CITY_EDITION_REV0] = _GeoIP_full_path_to("GeoIPCity.dat"); - GeoIPDBFileName[GEOIP_CITY_EDITION_REV1] = _GeoIP_full_path_to("GeoIPCity.dat"); - GeoIPDBFileName[GEOIP_ISP_EDITION] = _GeoIP_full_path_to("GeoIPISP.dat"); - GeoIPDBFileName[GEOIP_ORG_EDITION] = _GeoIP_full_path_to("GeoIPOrg.dat"); - GeoIPDBFileName[GEOIP_PROXY_EDITION] = _GeoIP_full_path_to("GeoIPProxy.dat"); - GeoIPDBFileName[GEOIP_ASNUM_EDITION] = _GeoIP_full_path_to("GeoIPASNum.dat"); - GeoIPDBFileName[GEOIP_NETSPEED_EDITION] = _GeoIP_full_path_to("GeoIPNetSpeed.dat"); - GeoIPDBFileName[GEOIP_DOMAIN_EDITION] = _GeoIP_full_path_to("GeoIPDomain.dat"); - } -} - -static -int _file_exists(const char *file_name) { - struct stat file_stat; - return( (stat(file_name, &file_stat) == 0) ? 1:0); -} - -int GeoIP_db_avail(int type) { - const char * filePath; - if (type < 0 || type >= NUM_DB_TYPES) { - return 0; - } - filePath = GeoIPDBFileName[type]; - if (NULL == filePath) { - return 0; - } - return _file_exists(filePath); -} - -static -void _setup_segments(GeoIP * gi) { - int i, j; - unsigned char delim[3]; - unsigned char buf[SEGMENT_RECORD_LENGTH]; - - gi->databaseSegments = NULL; - - /* default to GeoIP Country Edition */ - gi->databaseType = GEOIP_COUNTRY_EDITION; - gi->record_length = STANDARD_RECORD_LENGTH; - fseek(gi->GeoIPDatabase, -3l, SEEK_END); - for (i = 0; i < STRUCTURE_INFO_MAX_SIZE; i++) { - fread(delim, 1, 3, gi->GeoIPDatabase); - if (delim[0] == 255 && delim[1] == 255 && delim[2] == 255) { - fread(&gi->databaseType, 1, 1, gi->GeoIPDatabase); - if (gi->databaseType >= 106) { - /* backwards compatibility with databases from April 2003 and earlier */ - gi->databaseType -= 105; - } - - if (gi->databaseType == GEOIP_REGION_EDITION_REV0) { - /* Region Edition, pre June 2003 */ - gi->databaseSegments = malloc(sizeof(int)); - gi->databaseSegments[0] = STATE_BEGIN_REV0; - } else if (gi->databaseType == GEOIP_REGION_EDITION_REV1) { - /* Region Edition, post June 2003 */ - gi->databaseSegments = malloc(sizeof(int)); - gi->databaseSegments[0] = STATE_BEGIN_REV1; - } else if (gi->databaseType == GEOIP_CITY_EDITION_REV0 || - gi->databaseType == GEOIP_CITY_EDITION_REV1 || - gi->databaseType == GEOIP_ORG_EDITION || - gi->databaseType == GEOIP_ISP_EDITION || - gi->databaseType == GEOIP_ASNUM_EDITION) { - /* City/Org Editions have two segments, read offset of second segment */ - gi->databaseSegments = malloc(sizeof(int)); - gi->databaseSegments[0] = 0; - fread(buf, SEGMENT_RECORD_LENGTH, 1, gi->GeoIPDatabase); - for (j = 0; j < SEGMENT_RECORD_LENGTH; j++) { - gi->databaseSegments[0] += (buf[j] << (j * 8)); - } - if (gi->databaseType == GEOIP_ORG_EDITION || - gi->databaseType == GEOIP_ISP_EDITION) - gi->record_length = ORG_RECORD_LENGTH; - } - break; - } else { - fseek(gi->GeoIPDatabase, -4l, SEEK_CUR); - } - } - if (gi->databaseType == GEOIP_COUNTRY_EDITION || - gi->databaseType == GEOIP_PROXY_EDITION || - gi->databaseType == GEOIP_NETSPEED_EDITION) { - gi->databaseSegments = malloc(sizeof(int)); - gi->databaseSegments[0] = COUNTRY_BEGIN; - } -} - -static -int _check_mtime(GeoIP *gi) { - struct stat buf; - if (gi->flags & GEOIP_CHECK_CACHE) { - if (stat(gi->file_path, &buf) != -1) { - if (buf.st_mtime != gi->mtime) { - /* GeoIP Database file updated */ - if (gi->flags & GEOIP_MEMORY_CACHE) { - /* reload database into memory cache */ - if ((gi->cache = (unsigned char*) realloc(gi->cache, buf.st_size)) == NULL) { - fprintf(stderr,"Out of memory when reloading %s\n",gi->file_path); - return -1; - } - } - /* refresh filehandle */ - fclose(gi->GeoIPDatabase); - gi->GeoIPDatabase = fopen(gi->file_path,"rb"); - if (gi->GeoIPDatabase == NULL) { - fprintf(stderr,"Error Opening file %s when reloading\n",gi->file_path); - return -1; - } - gi->mtime = buf.st_mtime; - if (gi->flags & GEOIP_MEMORY_CACHE) { - if (fread(gi->cache, sizeof(unsigned char), buf.st_size, gi->GeoIPDatabase) != (size_t) buf.st_size) { - fprintf(stderr,"Error reading file %s when reloading\n",gi->file_path); - return -1; - } - } - if (gi->databaseSegments != NULL) { - free(gi->databaseSegments); - gi->databaseSegments = NULL; - } - _setup_segments(gi); - if (gi->databaseSegments == NULL) { - fprintf(stderr, "Error reading file %s -- corrupt\n", gi->file_path); - return -1; - } - if (gi->flags & GEOIP_INDEX_CACHE) { - gi->index_cache = (unsigned char *) realloc(gi->index_cache, sizeof(unsigned char) * ((gi->databaseSegments[0] * (long)gi->record_length * 2))); - if (gi->index_cache != NULL) { - fseek(gi->GeoIPDatabase, 0, SEEK_SET); - if (fread(gi->index_cache, sizeof(unsigned char), gi->databaseSegments[0] * (long)gi->record_length * 2, gi->GeoIPDatabase) != (size_t) (gi->databaseSegments[0]*(long)gi->record_length * 2)) { - fprintf(stderr,"Error reading file %s where reloading\n",gi->file_path); - return -1; - } - } - } - } - } - } - return 0; -} - -unsigned int _GeoIP_seek_record (GeoIP *gi, unsigned long ipnum) { - int depth; - unsigned int x; - unsigned char stack_buffer[2 * MAX_RECORD_LENGTH]; - const unsigned char *buf = (gi->cache == NULL) ? stack_buffer : NULL; - unsigned int offset = 0; - - const unsigned char * p; - int j; - - _check_mtime(gi); - for (depth = 31; depth >= 0; depth--) { - if (gi->cache == NULL && gi->index_cache == NULL) { - /* read from disk */ - fseek(gi->GeoIPDatabase, (long)gi->record_length * 2 * offset, SEEK_SET); - fread(stack_buffer,gi->record_length,2,gi->GeoIPDatabase); - } else if (gi->index_cache == NULL) { - /* simply point to record in memory */ - buf = gi->cache + (long)gi->record_length * 2 *offset; - } else { - buf = gi->index_cache + (long)gi->record_length * 2 * offset; - } - - if (ipnum & (1 << depth)) { - /* Take the right-hand branch */ - if ( gi->record_length == 3 ) { - /* Most common case is completely unrolled and uses constants. */ - x = (buf[3*1 + 0] << (0*8)) - + (buf[3*1 + 1] << (1*8)) - + (buf[3*1 + 2] << (2*8)); - - } else { - /* General case */ - j = gi->record_length; - p = &buf[2*j]; - x = 0; - do { - x <<= 8; - x += *(--p); - } while ( --j ); - } - - } else { - /* Take the left-hand branch */ - if ( gi->record_length == 3 ) { - /* Most common case is completely unrolled and uses constants. */ - x = (buf[3*0 + 0] << (0*8)) - + (buf[3*0 + 1] << (1*8)) - + (buf[3*0 + 2] << (2*8)); - } else { - /* General case */ - j = gi->record_length; - p = &buf[1*j]; - x = 0; - do { - x <<= 8; - x += *(--p); - } while ( --j ); - } - } - - if (x >= gi->databaseSegments[0]) { - return x; - } - offset = x; - } - - /* shouldn't reach here */ - fprintf(stderr,"Error Traversing Database for ipnum = %lu - Perhaps database is corrupt?\n",ipnum); - return 0; -} - -unsigned long _GeoIP_addr_to_num (const char *addr) { - int i; - char tok[4]; - int octet; - int j = 0, k = 0; - unsigned long ipnum = 0; - char c = 0; - - for (i=0; i<4; i++) { - for (;;) { - c = addr[k++]; - if (c == '.' || c == '\0') { - tok[j] = '\0'; - octet = atoi(tok); - if (octet > 255) - return 0; - ipnum += (octet << ((3-i)*8)); - j = 0; - break; - } else if (c >= '0' && c<= '9') { - if (j > 2) { - return 0; - } - tok[j++] = c; - } else { - return 0; - } - } - if(c == '\0' && i<3) { - return 0; - } - } - return ipnum; -} - -GeoIP* GeoIP_open_type (int type, int flags) { - GeoIP * gi; - const char * filePath; - if (type < 0 || type >= NUM_DB_TYPES) { - printf("Invalid database type %d\n", type); - return NULL; - } - _GeoIP_setup_dbfilename(); - filePath = GeoIPDBFileName[type]; - if (filePath == NULL) { - printf("Invalid database type %d\n", type); - return NULL; - } - gi = GeoIP_open (filePath, flags); - return gi; -} - -GeoIP* GeoIP_new (int flags) { - GeoIP * gi; - _GeoIP_setup_dbfilename(); - gi = GeoIP_open (GeoIPDBFileName[GEOIP_COUNTRY_EDITION], flags); - return gi; -} - -GeoIP* GeoIP_open (const char * filename, int flags) { - struct stat buf; - GeoIP * gi; - size_t len; - -#ifdef _WIN32 - WSADATA wsa; - if (WSAStartup(MAKEWORD(1, 1), &wsa) != 0) - return NULL; -#endif - - gi = (GeoIP *)malloc(sizeof(GeoIP)); - if (gi == NULL) - return NULL; - len = sizeof(char) * (strlen(filename)+1); - gi->file_path = malloc(len); - if (gi->file_path == NULL) { - free(gi); - return NULL; - } - strncpy(gi->file_path, filename, len); - gi->GeoIPDatabase = fopen(filename,"rb"); - if (gi->GeoIPDatabase == NULL) { - fprintf(stderr,"Error Opening file %s\n",filename); - free(gi->file_path); - free(gi); - return NULL; - } else { - if (flags & GEOIP_MEMORY_CACHE) { - if (fstat(fileno(gi->GeoIPDatabase), &buf) == -1) { - fprintf(stderr,"Error stating file %s\n",filename); - free(gi->file_path); - free(gi); - return NULL; - } - gi->mtime = buf.st_mtime; - gi->cache = (unsigned char *) malloc(sizeof(unsigned char) * buf.st_size); - if (gi->cache != NULL) { - if (fread(gi->cache, sizeof(unsigned char), buf.st_size, gi->GeoIPDatabase) != (size_t) buf.st_size) { - fprintf(stderr,"Error reading file %s\n",filename); - free(gi->cache); - free(gi->file_path); - free(gi); - return NULL; - } - } - } else { - if (flags & GEOIP_CHECK_CACHE) { - if (fstat(fileno(gi->GeoIPDatabase), &buf) == -1) { - fprintf(stderr,"Error stating file %s\n",filename); - free(gi->file_path); - free(gi); - return NULL; - } - gi->mtime = buf.st_mtime; - } - gi->cache = NULL; - } - gi->flags = flags; - _setup_segments(gi); - if (flags & GEOIP_INDEX_CACHE) { - gi->index_cache = (unsigned char *) malloc(sizeof(unsigned char) * ((gi->databaseSegments[0] * (long)gi->record_length * 2))); - if (gi->index_cache != NULL) { - fseek(gi->GeoIPDatabase, 0, SEEK_SET); - if (fread(gi->index_cache, sizeof(unsigned char), gi->databaseSegments[0] * (long)gi->record_length * 2, gi->GeoIPDatabase) != (size_t) (gi->databaseSegments[0]*(long)gi->record_length * 2)) { - fprintf(stderr,"Error reading file %s\n",filename); - free(gi->databaseSegments); - free(gi->index_cache); - free(gi); - return NULL; - } - } - } else { - gi->index_cache = NULL; - } - return gi; - } -} - -void GeoIP_delete (GeoIP *gi) { - if (gi == NULL ) - return; - if (gi->GeoIPDatabase != NULL) - fclose(gi->GeoIPDatabase); - if (gi->cache != NULL) - free(gi->cache); - if (gi->index_cache != NULL) - free(gi->index_cache); - if (gi->file_path != NULL) - free(gi->file_path); - if (gi->databaseSegments != NULL) - free(gi->databaseSegments); - free(gi); -} - -const char *GeoIP_country_code_by_name (GeoIP* gi, const char *name) { - int country_id; - country_id = GeoIP_id_by_name(gi, name); - return (country_id > 0) ? GeoIP_country_code[country_id] : NULL; -} - -const char *GeoIP_country_code3_by_name (GeoIP* gi, const char *name) { - int country_id; - country_id = GeoIP_id_by_name(gi, name); - return (country_id > 0) ? GeoIP_country_code3[country_id] : NULL; -} - -const char *GeoIP_country_name_by_name (GeoIP* gi, const char *name) { - int country_id; - country_id = GeoIP_id_by_name(gi, name); - return (country_id > 0) ? GeoIP_country_name[country_id] : NULL; -} - -unsigned long _GeoIP_lookupaddress (const char *host) { - unsigned long addr = inet_addr(host); - struct hostent phe2; - struct hostent * phe = &phe2; - char *buf = NULL; - int result = 0; -#ifdef HAVE_GETHOSTBYNAME_R - buf = malloc(buflength); -#endif - if (addr == INADDR_NONE) { -#ifdef HAVE_GETHOSTBYNAME_R - while (1) { - /* we use gethostbyname_r here because it is thread-safe and gethostbyname is not */ -#ifdef GETHOSTBYNAME_R_RETURNS_INT - result = gethostbyname_r(host,&phe2,buf,buflength,&phe,&herr); -#else - phe = gethostbyname_r(host,&phe2,buf,buflength,&herr); -#endif - if (herr != ERANGE) - break; - if (result == 0) - break; - /* double the buffer if the buffer is too small */ - buflength = buflength * 2; - buf = realloc(buf,buflength); - } -#endif -#ifndef HAVE_GETHOSTBYNAME_R - /* Some systems do not support gethostbyname_r, such as Mac OS X */ - phe = gethostbyname(host); -#endif - if (!phe || result != 0) { - free(buf); - return 0; - } - addr = *((unsigned long *) phe->h_addr_list[0]); - } -#ifdef HAVE_GETHOSTBYNAME_R - free(buf); -#endif - return ntohl(addr); -} - -int GeoIP_id_by_name (GeoIP* gi, const char *name) { - unsigned long ipnum; - int ret; - if (name == NULL) { - return 0; - } - if (gi->databaseType != GEOIP_COUNTRY_EDITION && gi->databaseType != GEOIP_PROXY_EDITION && gi->databaseType != GEOIP_NETSPEED_EDITION) { - printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_COUNTRY_EDITION]); - return 0; - } - if (!(ipnum = _GeoIP_lookupaddress(name))) - return 0; - ret = _GeoIP_seek_record(gi, ipnum) - COUNTRY_BEGIN; - return ret; - -} - -const char *GeoIP_country_code_by_addr (GeoIP* gi, const char *addr) { - int country_id; - country_id = GeoIP_id_by_addr(gi, addr); - return (country_id > 0) ? GeoIP_country_code[country_id] : NULL; -} - -const char *GeoIP_country_code3_by_addr (GeoIP* gi, const char *addr) { - int country_id; - country_id = GeoIP_id_by_addr(gi, addr); - return (country_id > 0) ? GeoIP_country_code3[country_id] : NULL; - return GeoIP_country_code3[country_id]; -} - -const char *GeoIP_country_name_by_addr (GeoIP* gi, const char *addr) { - int country_id; - country_id = GeoIP_id_by_addr(gi, addr); - return (country_id > 0) ? GeoIP_country_name[country_id] : NULL; - return GeoIP_country_name[country_id]; -} - -const char *GeoIP_country_name_by_ipnum (GeoIP* gi, unsigned long ipnum) { - int country_id; - country_id = GeoIP_id_by_ipnum(gi, ipnum); - return (country_id > 0) ? GeoIP_country_name[country_id] : NULL; -} - -const char *GeoIP_country_code_by_ipnum (GeoIP* gi, unsigned long ipnum) { - int country_id; - country_id = GeoIP_id_by_ipnum(gi, ipnum); - return (country_id > 0) ? GeoIP_country_code[country_id] : NULL; -} - -const char *GeoIP_country_code3_by_ipnum (GeoIP* gi, unsigned long ipnum) { - int country_id; - country_id = GeoIP_id_by_ipnum(gi, ipnum); - return (country_id > 0) ? GeoIP_country_code3[country_id] : NULL; -} - -int GeoIP_country_id_by_addr (GeoIP* gi, const char *addr) { - return GeoIP_id_by_addr(gi, addr); -} - -int GeoIP_country_id_by_name (GeoIP* gi, const char *host) { - return GeoIP_id_by_name(gi, host); -} - -int GeoIP_id_by_addr (GeoIP* gi, const char *addr) { - unsigned long ipnum; - int ret; - if (addr == NULL) { - return 0; - } - if (gi->databaseType != GEOIP_COUNTRY_EDITION && - gi->databaseType != GEOIP_PROXY_EDITION && - gi->databaseType != GEOIP_NETSPEED_EDITION) { - printf("Invalid database type %s, expected %s\n", - GeoIPDBDescription[(int)gi->databaseType], - GeoIPDBDescription[GEOIP_COUNTRY_EDITION]); - return 0; - } - ipnum = _GeoIP_addr_to_num(addr); - ret = _GeoIP_seek_record(gi, ipnum) - COUNTRY_BEGIN; - return ret; -} - -int GeoIP_id_by_ipnum (GeoIP* gi, unsigned long ipnum) { - int ret; - if (ipnum == 0) { - return 0; - } - if (gi->databaseType != GEOIP_COUNTRY_EDITION && - gi->databaseType != GEOIP_PROXY_EDITION && - gi->databaseType != GEOIP_NETSPEED_EDITION) { - printf("Invalid database type %s, expected %s\n", - GeoIPDBDescription[(int)gi->databaseType], - GeoIPDBDescription[GEOIP_COUNTRY_EDITION]); - return 0; - } - ret = _GeoIP_seek_record(gi, ipnum) - COUNTRY_BEGIN; - return ret; -} - -char *GeoIP_database_info (GeoIP* gi) { - int i; - unsigned char buf[3]; - char *retval; - int hasStructureInfo = 0; - - if(gi == NULL) - return NULL; - - _check_mtime(gi); - fseek(gi->GeoIPDatabase, -3l, SEEK_END); - - /* first get past the database structure information */ - for (i = 0; i < STRUCTURE_INFO_MAX_SIZE; i++) { - fread(buf, 1, 3, gi->GeoIPDatabase); - if (buf[0] == 255 && buf[1] == 255 && buf[2] == 255) { - hasStructureInfo = 1; - break; - } - fseek(gi->GeoIPDatabase, -4l, SEEK_CUR); - } - if (hasStructureInfo == 1) { - fseek(gi->GeoIPDatabase, -3l, SEEK_CUR); - } else { - /* no structure info, must be pre Sep 2002 database, go back to end */ - fseek(gi->GeoIPDatabase, -3l, SEEK_END); - } - - for (i = 0; i < DATABASE_INFO_MAX_SIZE; i++) { - fread(buf, 1, 3, gi->GeoIPDatabase); - if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0) { - retval = malloc(sizeof(char) * (i+1)); - if (retval == NULL) { - return NULL; - } - fread(retval, 1, i, gi->GeoIPDatabase); - retval[i] = '\0'; - return retval; - } - fseek(gi->GeoIPDatabase, -4l, SEEK_CUR); - } - return NULL; -} - -/* GeoIP Region Edition functions */ - -void GeoIP_assign_region_by_inetaddr(GeoIP* gi, unsigned long inetaddr, GeoIPRegion *region) { - unsigned int seek_region; - - /* This also writes in the terminating NULs (if you decide to - * keep them) and clear any fields that are not set. */ - memset(region, 0, sizeof(GeoIPRegion)); - - seek_region = _GeoIP_seek_record(gi, ntohl(inetaddr)); - - if (gi->databaseType == GEOIP_REGION_EDITION_REV0) { - /* Region Edition, pre June 2003 */ - seek_region -= STATE_BEGIN_REV0; - if (seek_region >= 1000) { - region->country_code[0] = 'U'; - region->country_code[1] = 'S'; - region->region[0] = (char) ((seek_region - 1000)/26 + 65); - region->region[1] = (char) ((seek_region - 1000)%26 + 65); - } else { - memcpy(region->country_code, GeoIP_country_code[seek_region], 2); - } - } else if (gi->databaseType == GEOIP_REGION_EDITION_REV1) { - /* Region Edition, post June 2003 */ - seek_region -= STATE_BEGIN_REV1; - if (seek_region < US_OFFSET) { - /* Unknown */ - /* we don't need to do anything here b/c we memset region to 0 */ - } else if (seek_region < CANADA_OFFSET) { - /* USA State */ - region->country_code[0] = 'U'; - region->country_code[1] = 'S'; - region->region[0] = (char) ((seek_region - US_OFFSET)/26 + 65); - region->region[1] = (char) ((seek_region - US_OFFSET)%26 + 65); - } else if (seek_region < WORLD_OFFSET) { - /* Canada Province */ - region->country_code[0] = 'C'; - region->country_code[1] = 'A'; - region->region[0] = (char) ((seek_region - CANADA_OFFSET)/26 + 65); - region->region[1] = (char) ((seek_region - CANADA_OFFSET)%26 + 65); - } else { - /* Not US or Canada */ - memcpy(region->country_code, GeoIP_country_code[(seek_region - WORLD_OFFSET) / FIPS_RANGE], 2); - } - } -} - -static -GeoIPRegion * _get_region(GeoIP* gi, unsigned long ipnum) { - GeoIPRegion * region; - - region = malloc(sizeof(GeoIPRegion)); - if (region) { - GeoIP_assign_region_by_inetaddr(gi, htonl(ipnum), region); - } - return region; -} - -GeoIPRegion * GeoIP_region_by_addr (GeoIP* gi, const char *addr) { - unsigned long ipnum; - if (addr == NULL) { - return 0; - } - if (gi->databaseType != GEOIP_REGION_EDITION_REV0 && - gi->databaseType != GEOIP_REGION_EDITION_REV1) { - printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_REGION_EDITION_REV1]); - return 0; - } - ipnum = _GeoIP_addr_to_num(addr); - return _get_region(gi, ipnum); -} - -GeoIPRegion * GeoIP_region_by_name (GeoIP* gi, const char *name) { - unsigned long ipnum; - if (name == NULL) { - return 0; - } - if (gi->databaseType != GEOIP_REGION_EDITION_REV0 && - gi->databaseType != GEOIP_REGION_EDITION_REV1) { - printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_REGION_EDITION_REV1]); - return 0; - } - if (!(ipnum = _GeoIP_lookupaddress(name))) - return 0; - return _get_region(gi, ipnum); -} - -GeoIPRegion * GeoIP_region_by_ipnum (GeoIP* gi, unsigned long ipnum) { - if (gi->databaseType != GEOIP_REGION_EDITION_REV0 && - gi->databaseType != GEOIP_REGION_EDITION_REV1) { - printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_REGION_EDITION_REV1]); - return 0; - } - return _get_region(gi, ipnum); -} - -void GeoIPRegion_delete (GeoIPRegion *gir) { - free(gir); -} - -/* GeoIP Organization, ISP and AS Number Edition private method */ -static -char *_get_name (GeoIP* gi, unsigned long ipnum) { - int seek_org; - char buf[MAX_ORG_RECORD_LENGTH]; - char * org_buf, * buf_pointer; - int record_pointer; - size_t len; - - if (gi->databaseType != GEOIP_ORG_EDITION && - gi->databaseType != GEOIP_ISP_EDITION && - gi->databaseType != GEOIP_ASNUM_EDITION) { - printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_ORG_EDITION]); - return 0; - } - - seek_org = _GeoIP_seek_record(gi, ipnum); - if (seek_org == gi->databaseSegments[0]) - return NULL; - - record_pointer = seek_org + (2 * gi->record_length - 1) * gi->databaseSegments[0]; - - if (gi->cache == NULL) { - fseek(gi->GeoIPDatabase, record_pointer, SEEK_SET); - fread(buf, sizeof(char), MAX_ORG_RECORD_LENGTH, gi->GeoIPDatabase); - len = sizeof(char) * (strlen(buf)+1); - org_buf = malloc(len); - strncpy(org_buf, buf, len); - } else { - buf_pointer = (char *)(gi->cache + (long)record_pointer); - len = sizeof(char) * (strlen(buf_pointer)+1); - org_buf = malloc(len); - strncpy(org_buf, buf_pointer, len); - } - return org_buf; -} - -char *GeoIP_name_by_ipnum (GeoIP* gi, unsigned long ipnum) { - return _get_name(gi,ipnum); -} - -char *GeoIP_name_by_addr (GeoIP* gi, const char *addr) { - unsigned long ipnum; - if (addr == NULL) { - return 0; - } - ipnum = _GeoIP_addr_to_num(addr); - return _get_name(gi, ipnum); -} - -char *GeoIP_name_by_name (GeoIP* gi, const char *name) { - unsigned long ipnum; - if (name == NULL) { - return 0; - } - if (!(ipnum = _GeoIP_lookupaddress(name))) - return 0; - return _get_name(gi, ipnum); -} - -char *GeoIP_org_by_ipnum (GeoIP* gi, unsigned long ipnum) { - return GeoIP_name_by_ipnum(gi, ipnum); -} - -char *GeoIP_org_by_addr (GeoIP* gi, const char *addr) { - return GeoIP_name_by_addr(gi, addr); -} - -char *GeoIP_org_by_name (GeoIP* gi, const char *name) { - return GeoIP_name_by_name(gi, name); -} - -unsigned char GeoIP_database_edition (GeoIP* gi) { - return gi->databaseType; -} diff --git a/extensions/geoip/GeoIP.h b/extensions/geoip/GeoIP.h deleted file mode 100644 index da759e6e..00000000 --- a/extensions/geoip/GeoIP.h +++ /dev/null @@ -1,169 +0,0 @@ -/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ -/* GeoIP.h - * - * Copyright (C) 2006 MaxMind LLC - * - * This library 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 2.1 of the License, or (at your option) any later version. - * - * This library 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 this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef GEOIP_H -#define GEOIP_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include -#include -#include /* for fstat */ -#include /* for fstat */ - -#define SEGMENT_RECORD_LENGTH 3 -#define STANDARD_RECORD_LENGTH 3 -#define ORG_RECORD_LENGTH 4 -#define MAX_RECORD_LENGTH 4 -#define NUM_DB_TYPES 20 - -typedef struct GeoIPTag { - FILE *GeoIPDatabase; - char *file_path; - unsigned char *cache; - unsigned char *index_cache; - unsigned int *databaseSegments; - char databaseType; - time_t mtime; - int flags; - char record_length; - int record_iter; /* used in GeoIP_next_record */ -} GeoIP; - -typedef struct GeoIPRegionTag { - char country_code[3]; - char region[3]; -} GeoIPRegion; - -typedef enum { - GEOIP_STANDARD = 0, - GEOIP_MEMORY_CACHE = 1, - GEOIP_CHECK_CACHE = 2, - GEOIP_INDEX_CACHE = 4, -} GeoIPOptions; - -typedef enum { - GEOIP_COUNTRY_EDITION = 1, - GEOIP_REGION_EDITION_REV0 = 7, - GEOIP_CITY_EDITION_REV0 = 6, - GEOIP_ORG_EDITION = 5, - GEOIP_ISP_EDITION = 4, - GEOIP_CITY_EDITION_REV1 = 2, - GEOIP_REGION_EDITION_REV1 = 3, - GEOIP_PROXY_EDITION = 8, - GEOIP_ASNUM_EDITION = 9, - GEOIP_NETSPEED_EDITION = 10, - GEOIP_DOMAIN_EDITION = 11 -} GeoIPDBTypes; - -typedef enum { - GEOIP_ANON_PROXY = 1, - GEOIP_HTTP_X_FORWARDED_FOR_PROXY = 2, - GEOIP_HTTP_CLIENT_IP_PROXY = 3, -} GeoIPProxyTypes; - -typedef enum { - GEOIP_UNKNOWN_SPEED = 0, - GEOIP_DIALUP_SPEED = 1, - GEOIP_CABLEDSL_SPEED = 2, - GEOIP_CORPORATE_SPEED = 3, -} GeoIPNetspeedValues; - -extern char **GeoIPDBFileName; -extern const char * GeoIPDBDescription[NUM_DB_TYPES]; -extern const char *GeoIPCountryDBFileName; -extern const char *GeoIPRegionDBFileName; -extern const char *GeoIPCityDBFileName; -extern const char *GeoIPOrgDBFileName; -extern const char *GeoIPISPDBFileName; - -extern const char GeoIP_country_code[251][3]; -extern const char GeoIP_country_code3[251][4]; -extern const char * GeoIP_country_name[251]; -extern const char GeoIP_country_continent[251][3]; - -#ifdef DLL -#define GEOIP_API __declspec(dllexport) -#else -#define GEOIP_API -#endif /* DLL */ - -GEOIP_API void GeoIP_setup_custom_directory(char *dir); -GEOIP_API GeoIP* GeoIP_open_type (int type, int flags); -GEOIP_API GeoIP* GeoIP_new(int flags); -GEOIP_API GeoIP* GeoIP_open(const char * filename, int flags); -GEOIP_API int GeoIP_db_avail(int type); -GEOIP_API void GeoIP_delete(GeoIP* gi); -GEOIP_API const char *GeoIP_country_code_by_addr (GeoIP* gi, const char *addr); -GEOIP_API const char *GeoIP_country_code_by_name (GeoIP* gi, const char *host); -GEOIP_API const char *GeoIP_country_code3_by_addr (GeoIP* gi, const char *addr); -GEOIP_API const char *GeoIP_country_code3_by_name (GeoIP* gi, const char *host); -GEOIP_API const char *GeoIP_country_name_by_addr (GeoIP* gi, const char *addr); -GEOIP_API const char *GeoIP_country_name_by_name (GeoIP* gi, const char *host); -GEOIP_API const char *GeoIP_country_name_by_ipnum (GeoIP* gi, unsigned long ipnum); -GEOIP_API const char *GeoIP_country_code_by_ipnum (GeoIP* gi, unsigned long ipnum); -GEOIP_API const char *GeoIP_country_code3_by_ipnum (GeoIP* gi, unsigned long ipnum); - -/* Deprecated - for backwards compatibility only */ -GEOIP_API int GeoIP_country_id_by_addr (GeoIP* gi, const char *addr); -GEOIP_API int GeoIP_country_id_by_name (GeoIP* gi, const char *host); -GEOIP_API char *GeoIP_org_by_addr (GeoIP* gi, const char *addr); -GEOIP_API char *GeoIP_org_by_name (GeoIP* gi, const char *host); -/* End deprecated */ - -GEOIP_API int GeoIP_id_by_addr (GeoIP* gi, const char *addr); -GEOIP_API int GeoIP_id_by_name (GeoIP* gi, const char *host); -GEOIP_API int GeoIP_id_by_ipnum (GeoIP* gi, unsigned long ipnum); - -GEOIP_API GeoIPRegion * GeoIP_region_by_addr (GeoIP* gi, const char *addr); -GEOIP_API GeoIPRegion * GeoIP_region_by_name (GeoIP* gi, const char *host); -GEOIP_API GeoIPRegion * GeoIP_region_by_ipnum (GeoIP *gi, unsigned long ipnum); - -/* Warning - don't call this after GeoIP_assign_region_by_inetaddr calls */ -GEOIP_API void GeoIPRegion_delete (GeoIPRegion *gir); - -GEOIP_API void GeoIP_assign_region_by_inetaddr(GeoIP* gi, unsigned long inetaddr, GeoIPRegion *gir); - -/* Used to query GeoIP Organization, ISP and AS Number databases */ -GEOIP_API char *GeoIP_name_by_ipnum (GeoIP* gi, unsigned long ipnum); -GEOIP_API char *GeoIP_name_by_addr (GeoIP* gi, const char *addr); -GEOIP_API char *GeoIP_name_by_name (GeoIP* gi, const char *host); - -GEOIP_API char *GeoIP_database_info (GeoIP* gi); -GEOIP_API unsigned char GeoIP_database_edition (GeoIP* gi); - -/* Convert region code to region name */ -GEOIP_API const char * GeoIP_region_name_by_code(const char *country_code, const char *region_code); - -/* Get timezone from country and region code */ -GEOIP_API const char * GeoIP_time_zone_by_country_and_region(const char *country_code, const char *region_code); - -#ifdef BSD -#define memcpy(dest, src, n) bcopy(src, dest, n) -#endif - -#ifdef __cplusplus -} -#endif - -#endif /* GEOIP_H */ diff --git a/extensions/geoip/data-pool.c b/extensions/geoip/data-pool.c new file mode 100644 index 00000000..7b3c2a59 --- /dev/null +++ b/extensions/geoip/data-pool.c @@ -0,0 +1,169 @@ +#include "data-pool.h" +#include "maxminddb.h" + +#include +#include +#include + +static bool can_multiply(size_t const, size_t const, size_t const); + +// Allocate an MMDB_data_pool_s. It initially has space for size +// MMDB_entry_data_list_s structs. +MMDB_data_pool_s *data_pool_new(size_t const size) { + MMDB_data_pool_s *const pool = calloc(1, sizeof(MMDB_data_pool_s)); + if (!pool) { + return NULL; + } + + if (size == 0 || + !can_multiply(SIZE_MAX, size, sizeof(MMDB_entry_data_list_s))) { + data_pool_destroy(pool); + return NULL; + } + pool->size = size; + pool->blocks[0] = calloc(pool->size, sizeof(MMDB_entry_data_list_s)); + if (!pool->blocks[0]) { + data_pool_destroy(pool); + return NULL; + } + pool->blocks[0]->pool = pool; + + pool->sizes[0] = size; + + pool->block = pool->blocks[0]; + + return pool; +} + +// Determine if we can multiply m*n. We can do this if the result will be below +// the given max. max will typically be SIZE_MAX. +// +// We want to know if we'll wrap around. +static bool can_multiply(size_t const max, size_t const m, size_t const n) { + if (m == 0) { + return false; + } + + return n <= max / m; +} + +// Clean up the data pool. +void data_pool_destroy(MMDB_data_pool_s *const pool) { + if (!pool) { + return; + } + + for (size_t i = 0; i <= pool->index; i++) { + free(pool->blocks[i]); + } + + free(pool); +} + +// Claim a new struct from the pool. Doing this may cause the pool's size to +// grow. +MMDB_entry_data_list_s *data_pool_alloc(MMDB_data_pool_s *const pool) { + if (!pool) { + return NULL; + } + + if (pool->used < pool->size) { + MMDB_entry_data_list_s *const element = pool->block + pool->used; + pool->used++; + return element; + } + + // Take it from a new block of memory. + + size_t const new_index = pool->index + 1; + if (new_index == DATA_POOL_NUM_BLOCKS) { + // See the comment about not growing this on DATA_POOL_NUM_BLOCKS. + return NULL; + } + + if (!can_multiply(SIZE_MAX, pool->size, 2)) { + return NULL; + } + size_t const new_size = pool->size * 2; + + if (!can_multiply(SIZE_MAX, new_size, sizeof(MMDB_entry_data_list_s))) { + return NULL; + } + pool->blocks[new_index] = calloc(new_size, sizeof(MMDB_entry_data_list_s)); + if (!pool->blocks[new_index]) { + return NULL; + } + + // We don't need to set this, but it's useful for introspection in tests. + pool->blocks[new_index]->pool = pool; + + pool->index = new_index; + pool->block = pool->blocks[pool->index]; + + pool->size = new_size; + pool->sizes[pool->index] = pool->size; + + MMDB_entry_data_list_s *const element = pool->block; + pool->used = 1; + return element; +} + +// Turn the structs in the array-like pool into a linked list. +// +// Before calling this function, the list isn't linked up. +MMDB_entry_data_list_s *data_pool_to_list(MMDB_data_pool_s *const pool) { + if (!pool) { + return NULL; + } + + if (pool->index == 0 && pool->used == 0) { + return NULL; + } + + for (size_t i = 0; i <= pool->index; i++) { + MMDB_entry_data_list_s *const block = pool->blocks[i]; + + size_t size = pool->sizes[i]; + if (i == pool->index) { + size = pool->used; + } + + for (size_t j = 0; j < size - 1; j++) { + MMDB_entry_data_list_s *const cur = block + j; + cur->next = block + j + 1; + } + + if (i < pool->index) { + MMDB_entry_data_list_s *const last = block + size - 1; + last->next = pool->blocks[i + 1]; + } + } + + return pool->blocks[0]; +} + +#ifdef TEST_DATA_POOL + +#include +#include + +static void test_can_multiply(void); + +int main(void) { + plan(NO_PLAN); + test_can_multiply(); + done_testing(); +} + +static void test_can_multiply(void) { + { ok(can_multiply(SIZE_MAX, 1, SIZE_MAX), "1*SIZE_MAX is ok"); } + + { ok(!can_multiply(SIZE_MAX, 2, SIZE_MAX), "2*SIZE_MAX is not ok"); } + + { + ok(can_multiply(SIZE_MAX, 10240, sizeof(MMDB_entry_data_list_s)), + "1024 entry_data_list_s's are okay"); + } +} + +#endif diff --git a/extensions/geoip/data-pool.h b/extensions/geoip/data-pool.h new file mode 100644 index 00000000..25d09923 --- /dev/null +++ b/extensions/geoip/data-pool.h @@ -0,0 +1,52 @@ +#ifndef DATA_POOL_H +#define DATA_POOL_H + +#include "maxminddb.h" + +#include +#include + +// This should be large enough that we never need to grow the array of pointers +// to blocks. 32 is enough. Even starting out of with size 1 (1 struct), the +// 32nd element alone will provide 2**32 structs as we exponentially increase +// the number in each block. Being confident that we do not have to grow the +// array lets us avoid writing code to do that. That code would be risky as it +// would rarely be hit and likely not be well tested. +#define DATA_POOL_NUM_BLOCKS 32 + +// A pool of memory for MMDB_entry_data_list_s structs. This is so we can +// allocate multiple up front rather than one at a time for performance +// reasons. +// +// The order you add elements to it (by calling data_pool_alloc()) ends up as +// the order of the list. +// +// The memory only grows. There is no support for releasing an element you take +// back to the pool. +typedef struct MMDB_data_pool_s { + // Index of the current block we're allocating out of. + size_t index; + + // The size of the current block, counting by structs. + size_t size; + + // How many used in the current block, counting by structs. + size_t used; + + // The current block we're allocating out of. + MMDB_entry_data_list_s *block; + + // The size of each block. + size_t sizes[DATA_POOL_NUM_BLOCKS]; + + // An array of pointers to blocks of memory holding space for list + // elements. + MMDB_entry_data_list_s *blocks[DATA_POOL_NUM_BLOCKS]; +} MMDB_data_pool_s; + +MMDB_data_pool_s *data_pool_new(size_t const); +void data_pool_destroy(MMDB_data_pool_s *const); +MMDB_entry_data_list_s *data_pool_alloc(MMDB_data_pool_s *const); +MMDB_entry_data_list_s *data_pool_to_list(MMDB_data_pool_s *const); + +#endif diff --git a/extensions/geoip/extension.cpp b/extensions/geoip/extension.cpp index d96c81a2..72da9bf8 100644 --- a/extensions/geoip/extension.cpp +++ b/extensions/geoip/extension.cpp @@ -29,44 +29,106 @@ * Version: $Id$ */ +#define _USE_MATH_DEFINES + #include +#include #include "extension.h" -#include "GeoIP.h" -#include "am-string.h" +#include "geoip_util.h" /** * @file extension.cpp * @brief Implement extension code here. */ GeoIP_Extension g_GeoIP; -GeoIP *gi = NULL; +MMDB_s mmdb; SMEXT_LINK(&g_GeoIP); bool GeoIP_Extension::SDK_OnLoad(char *error, size_t maxlength, bool late) { - char path[PLATFORM_MAX_PATH]; - - g_pSM->BuildPath(Path_SM, path, sizeof(path), "configs/geoip/GeoIP.dat"); - gi = GeoIP_open(path, GEOIP_MEMORY_CACHE); - - if (!gi) + if (mmdb.filename) // Already loaded. { - ke::SafeStrcpy(error, maxlength, "Could not load configs/geoip/GeoIP.dat"); + return true; + } + + char m_GeoipDir[PLATFORM_MAX_PATH]; + g_pSM->BuildPath(Path_SM, m_GeoipDir, sizeof(m_GeoipDir), "configs/geoip"); + + bool hasEntry = false; + + IDirectory *dir = libsys->OpenDirectory(m_GeoipDir); + if (dir) + { + while (dir->MoreFiles()) + { + if (dir->IsEntryFile()) + { + const char *name = dir->GetEntryName(); + size_t len = strlen(name); + if (len >= 5 && strcmp(&name[len-5], ".mmdb") == 0) + { + char database[PLATFORM_MAX_PATH]; + libsys->PathFormat(database, sizeof(database), "%s/%s", m_GeoipDir, name); + + int status = MMDB_open(database, MMDB_MODE_MMAP, &mmdb); + + if (status != MMDB_SUCCESS) + { + ke::SafeStrcpy(error, maxlength, "Failed to open GeoIP2 database."); + libsys->CloseDirectory(dir); + return false; + } + + hasEntry = true; + break; + } + } + dir->NextEntry(); + } + libsys->CloseDirectory(dir); + } + + if (!hasEntry) + { + ke::SafeStrcpy(error, maxlength, "Could not find GeoIP2 database."); return false; } g_pShareSys->AddNatives(myself, geoip_natives); g_pShareSys->RegisterLibrary(myself, "GeoIP"); - g_pSM->LogMessage(myself, "GeoIP database info: %s", GeoIP_database_info(gi)); + + char date[40]; + const time_t epoch = (const time_t)mmdb.metadata.build_epoch; + strftime(date, 40, "%F %T UTC", gmtime(&epoch)); + + g_pSM->LogMessage(myself, "GeoIP2 database loaded: %s (%s)", mmdb.metadata.database_type, date); + + if (mmdb.metadata.languages.count > 0) + { + char buf[64]; + for (size_t i = 0; i < mmdb.metadata.languages.count; i++) + { + if (i == 0) + { + strcpy(buf, mmdb.metadata.languages.names[i]); + } + else + { + strcat(buf, " "); + strcat(buf, mmdb.metadata.languages.names[i]); + } + } + + g_pSM->LogMessage(myself, "GeoIP2 supported languages: %s", buf); + } return true; } void GeoIP_Extension::SDK_OnUnload() { - GeoIP_delete(gi); - gi = NULL; + MMDB_close(&mmdb); } const char *GeoIP_Extension::GetExtensionVerString() @@ -96,51 +158,264 @@ inline void StripPort(char *ip) static cell_t sm_Geoip_Code2(IPluginContext *pCtx, const cell_t *params) { char *ip; - const char *ccode; - pCtx->LocalToString(params[1], &ip); StripPort(ip); - ccode = GeoIP_country_code_by_addr(gi, ip); + const char *path[] = {"country", "iso_code", NULL}; + std::string str = lookupString(ip, path); + const char *ccode = str.c_str(); - pCtx->StringToLocal(params[2], 3, ccode ? ccode : ""); + pCtx->StringToLocalUTF8(params[2], 3, ccode, NULL); - return ccode ? 1 : 0; + return (str.length() != 0) ? 1 : 0; } static cell_t sm_Geoip_Code3(IPluginContext *pCtx, const cell_t *params) { char *ip; - const char *ccode; - pCtx->LocalToString(params[1], &ip); StripPort(ip); - ccode = GeoIP_country_code3_by_addr(gi, ip); - pCtx->StringToLocal(params[2], 4, ccode ? ccode : ""); + const char *path[] = {"country", "iso_code", NULL}; + std::string str = lookupString(ip, path); + const char *ccode = str.c_str(); - return ccode ? 1 : 0; + for (size_t i = 0; i < SM_ARRAYSIZE(GeoIPCountryCode); i++) + { + if (!strncmp(ccode, GeoIPCountryCode[i], 2)) + { + ccode = GeoIPCountryCode3[i]; + break; + } + } + + pCtx->StringToLocalUTF8(params[2], 4, ccode, NULL); + + return (str.length() != 0) ? 1 : 0; +} + +static cell_t sm_Geoip_RegionCode(IPluginContext *pCtx, const cell_t *params) +{ + char *ip; + pCtx->LocalToString(params[1], &ip); + StripPort(ip); + + size_t length = 0; + char ccode[12] = { 0 }; + + const char *pathCountry[] = {"country", "iso_code", NULL}; + std::string countryCode = lookupString(ip, pathCountry); + + if (countryCode.length() != 0) + { + const char *pathRegion[] = {"subdivisions", "0", "iso_code", NULL}; + std::string regionCode = lookupString(ip, pathRegion); + + length = regionCode.length(); + + if (length != 0) + { + ke::SafeSprintf(ccode, sizeof(ccode), "%s-%s", countryCode.c_str(), regionCode.c_str()); + } + } + + pCtx->StringToLocalUTF8(params[2], sizeof(ccode), ccode, NULL); + + return (length != 0) ? 1 : 0; +} + +static cell_t sm_Geoip_ContinentCode(IPluginContext *pCtx, const cell_t *params) +{ + char *ip; + pCtx->LocalToString(params[1], &ip); + StripPort(ip); + + const char *path[] = {"continent", "code", NULL}; + std::string str = lookupString(ip, path); + const char *ccode = str.c_str(); + + pCtx->StringToLocalUTF8(params[2], 3, ccode, NULL); + + return getContinentId(ccode); } static cell_t sm_Geoip_Country(IPluginContext *pCtx, const cell_t *params) { char *ip; - const char *ccode; - pCtx->LocalToString(params[1], &ip); StripPort(ip); - ccode = GeoIP_country_name_by_addr(gi, ip); - pCtx->StringToLocal(params[2], params[3], (ccode) ? ccode : ""); + const char *path[] = {"country", "names", "en", NULL}; + std::string str = lookupString(ip, path); + const char *ccode = str.c_str(); - return ccode ? 1 : 0; + pCtx->StringToLocalUTF8(params[2], params[3], ccode, NULL); + + return (str.length() != 0) ? 1 : 0; +} + +static cell_t sm_Geoip_CountryEx(IPluginContext *pCtx, const cell_t *params) +{ + char *ip; + pCtx->LocalToString(params[1], &ip); + StripPort(ip); + + if (params[4] > 0) + { + IGamePlayer *player = playerhelpers->GetGamePlayer(params[4]); + if (!player || !player->IsConnected()) + { + return pCtx->ThrowNativeError("Invalid client index %d", params[4]); + } + } + + const char *path[] = {"country", "names", getLang(params[4]), NULL}; + std::string str = lookupString(ip, path); + const char *ccode = str.c_str(); + + pCtx->StringToLocalUTF8(params[2], params[3], ccode, NULL); + + return (str.length() != 0) ? 1 : 0; +} + +static cell_t sm_Geoip_Continent(IPluginContext *pCtx, const cell_t *params) +{ + char *ip; + pCtx->LocalToString(params[1], &ip); + StripPort(ip); + + if (params[4] > 0) + { + IGamePlayer *player = playerhelpers->GetGamePlayer(params[4]); + if (!player || !player->IsConnected()) + { + return pCtx->ThrowNativeError("Invalid client index %d", params[4]); + } + } + + const char *path[] = {"continent", "names", getLang(params[4]), NULL}; + std::string str = lookupString(ip, path); + const char *ccode = str.c_str(); + + pCtx->StringToLocalUTF8(params[2], params[3], ccode, NULL); + + return (str.length() != 0) ? 1 : 0; +} + +static cell_t sm_Geoip_Region(IPluginContext *pCtx, const cell_t *params) +{ + char *ip; + pCtx->LocalToString(params[1], &ip); + StripPort(ip); + + if (params[4] > 0) + { + IGamePlayer *player = playerhelpers->GetGamePlayer(params[4]); + if (!player || !player->IsConnected()) + { + return pCtx->ThrowNativeError("Invalid client index %d", params[4]); + } + } + + const char *path[] = {"subdivisions", "0", "names", getLang(params[4]), NULL}; + std::string str = lookupString(ip, path); + const char *ccode = str.c_str(); + + pCtx->StringToLocalUTF8(params[2], params[3], ccode, NULL); + + return (str.length() != 0) ? 1 : 0; +} + +static cell_t sm_Geoip_City(IPluginContext *pCtx, const cell_t *params) +{ + char *ip; + pCtx->LocalToString(params[1], &ip); + StripPort(ip); + + if (params[4] > 0) + { + IGamePlayer *player = playerhelpers->GetGamePlayer(params[4]); + if (!player || !player->IsConnected()) + { + return pCtx->ThrowNativeError("Invalid client index %d", params[4]); + } + } + + const char *path[] = {"city", "names", getLang(params[4]), NULL}; + std::string str = lookupString(ip, path); + const char *ccode = str.c_str(); + + pCtx->StringToLocalUTF8(params[2], params[3], ccode, NULL); + + return (str.length() != 0) ? 1 : 0; +} + +static cell_t sm_Geoip_Timezone(IPluginContext *pCtx, const cell_t *params) +{ + char *ip; + pCtx->LocalToString(params[1], &ip); + StripPort(ip); + + const char *path[] = {"location", "time_zone", NULL}; + std::string str = lookupString(ip, path); + const char *ccode = str.c_str(); + + pCtx->StringToLocalUTF8(params[2], params[3], ccode, NULL); + + return (str.length() != 0) ? 1 : 0; +} + +static cell_t sm_Geoip_Latitude(IPluginContext *pCtx, const cell_t *params) +{ + char *ip; + pCtx->LocalToString(params[1], &ip); + StripPort(ip); + + const char *path[] = {"location", "latitude", NULL}; + double latitude = lookupDouble(ip, path); + + return sp_ftoc(latitude); +} + +static cell_t sm_Geoip_Longitude(IPluginContext *pCtx, const cell_t *params) +{ + char *ip; + pCtx->LocalToString(params[1], &ip); + StripPort(ip); + + const char *path[] = {"location", "longitude", NULL}; + double longitude = lookupDouble(ip, path); + + return sp_ftoc(longitude); +} + +static cell_t sm_Geoip_Distance(IPluginContext *pCtx, const cell_t *params) +{ + float earthRadius = params[5] ? 3958.0 : 6370.997; // miles / km + + float lat1 = sp_ctof(params[1]) * (M_PI / 180); + float lon1 = sp_ctof(params[2]) * (M_PI / 180); + float lat2 = sp_ctof(params[3]) * (M_PI / 180); + float lon2 = sp_ctof(params[4]) * (M_PI / 180); + + return sp_ftoc(earthRadius * acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1))); } const sp_nativeinfo_t geoip_natives[] = { {"GeoipCode2", sm_Geoip_Code2}, {"GeoipCode3", sm_Geoip_Code3}, + {"GeoipRegionCode", sm_Geoip_RegionCode}, + {"GeoipContinentCode", sm_Geoip_ContinentCode}, {"GeoipCountry", sm_Geoip_Country}, + {"GeoipCountryEx", sm_Geoip_CountryEx}, + {"GeoipContinent", sm_Geoip_Continent}, + {"GeoipRegion", sm_Geoip_Region}, + {"GeoipCity", sm_Geoip_City}, + {"GeoipTimezone", sm_Geoip_Timezone}, + {"GeoipLatitude", sm_Geoip_Latitude}, + {"GeoipLongitude", sm_Geoip_Longitude}, + {"GeoipDistance", sm_Geoip_Distance}, {NULL, NULL}, }; diff --git a/extensions/geoip/extension.h b/extensions/geoip/extension.h index c7c3f44c..a2b3babe 100644 --- a/extensions/geoip/extension.h +++ b/extensions/geoip/extension.h @@ -38,6 +38,8 @@ */ #include "smsdk_ext.h" +#include "maxminddb.h" +#include "am-string.h" /** @@ -119,6 +121,7 @@ public: #endif }; +extern MMDB_s mmdb; extern const sp_nativeinfo_t geoip_natives[]; #endif // _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_ diff --git a/extensions/geoip/geoip_util.cpp b/extensions/geoip/geoip_util.cpp new file mode 100644 index 00000000..01905957 --- /dev/null +++ b/extensions/geoip/geoip_util.cpp @@ -0,0 +1,209 @@ +/** + * vim: set ts=4 : + * ============================================================================= + * SourceMod GeoIP Extension + * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. + * ============================================================================= + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, version 3.0, as published by the + * Free Software Foundation. + * + * 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, see . + * + * As a special exception, AlliedModders LLC gives you permission to link the + * code of this program (as well as its derivative works) to "Half-Life 2," the + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software + * by the Valve Corporation. You must obey the GNU General Public License in + * all respects for all other code used. Additionally, AlliedModders LLC grants + * this exception to all derivative works. AlliedModders LLC defines further + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), + * or . + * + * Version: $Id$ + */ + +#include "geoip_util.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" +}; + +bool lookupByIp(const char *ip, const char **path, MMDB_entry_data_s *result) +{ + int gai_error = 0, mmdb_error = 0; + MMDB_lookup_result_s lookup = MMDB_lookup_string(&mmdb, ip, &gai_error, &mmdb_error); + + if (gai_error != 0 || mmdb_error != MMDB_SUCCESS || !lookup.found_entry) + { + return false; + } + + MMDB_entry_data_s entry_data; + mmdb_error = MMDB_aget_value(&lookup.entry, &entry_data, path); + + if (mmdb_error != MMDB_SUCCESS) + { + return false; + } + + *result = entry_data; + + return true; +} + +double lookupDouble(const char *ip, const char **path) +{ + MMDB_entry_data_s result; + + if (!lookupByIp(ip, path, &result)) + { + return 0; + } + + return result.double_value; +} + +int getContinentId(const char *code) +{ + #define CONTINENT_UNKNOWN 0 + #define CONTINENT_AFRICA 1 + #define CONTINENT_ANTARCTICA 2 + #define CONTINENT_ASIA 3 + #define CONTINENT_EUROPE 4 + #define CONTINENT_NORTH_AMERICA 5 + #define CONTINENT_OCEANIA 6 + #define CONTINENT_SOUTH_AMERICA 7 + + int index = CONTINENT_UNKNOWN; + + if (code) + { + switch (code[0]) + { + case 'A': + { + switch (code[1]) + { + case 'F': index = CONTINENT_AFRICA; break; + case 'N': index = CONTINENT_ANTARCTICA; break; + case 'S': index = CONTINENT_ASIA; break; + } + + break; + } + + case 'E': index = CONTINENT_EUROPE; break; + case 'O': index = CONTINENT_OCEANIA; break; + case 'N': index = CONTINENT_NORTH_AMERICA; break; + case 'S': index = CONTINENT_SOUTH_AMERICA; break; + } + } + + return index; +} + +const char *getLang(int target) +{ + if (target != -1 && mmdb.metadata.languages.count > 0) + { + unsigned int langid; + const char *code; + + if (target != 0) + { + langid = translator->GetClientLanguage(target); + } + else + { + langid = translator->GetServerLanguage(); + } + + if (translator->GetLanguageInfo(langid, &code, NULL)) + { + for (size_t i = 0; i < mmdb.metadata.languages.count; i++) + { + if (strcmp(code, mmdb.metadata.languages.names[i]) == 0) + { + return code; + } + } + } + } + + return "en"; +} + +std::string lookupString(const char *ip, const char **path) +{ + MMDB_entry_data_s result; + + if (!lookupByIp(ip, path, &result)) + { + return std::string(""); + } + + return std::string(result.utf8_string, result.data_size); +} \ No newline at end of file diff --git a/extensions/geoip/geoip_util.h b/extensions/geoip/geoip_util.h new file mode 100644 index 00000000..e97a3b15 --- /dev/null +++ b/extensions/geoip/geoip_util.h @@ -0,0 +1,46 @@ +/** + * vim: set ts=4 : + * ============================================================================= + * SourceMod GeoIP Extension + * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. + * ============================================================================= + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, version 3.0, as published by the + * Free Software Foundation. + * + * 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, see . + * + * As a special exception, AlliedModders LLC gives you permission to link the + * code of this program (as well as its derivative works) to "Half-Life 2," the + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software + * by the Valve Corporation. You must obey the GNU General Public License in + * all respects for all other code used. Additionally, AlliedModders LLC grants + * this exception to all derivative works. AlliedModders LLC defines further + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), + * or . + * + * Version: $Id$ + */ + +#ifndef _INCLUDE_SOURCEMOD_GEOIPUTIL_H_ +#define _INCLUDE_SOURCEMOD_GEOIPUTIL_H_ + +#include "extension.h" + +bool lookupByIp(const char *ip, const char **path, MMDB_entry_data_s *result); +double lookupDouble(const char *ip, const char **path); +int getContinentId(const char *code); +const char *getLang(int target); +std::string lookupString(const char *ip, const char **path); + +extern const char GeoIPCountryCode[252][3]; +extern const char GeoIPCountryCode3[252][4]; + +#endif // _INCLUDE_SOURCEMOD_GEOIPUTIL_H_ diff --git a/extensions/geoip/maxminddb-compat-util.h b/extensions/geoip/maxminddb-compat-util.h new file mode 100644 index 00000000..5d719943 --- /dev/null +++ b/extensions/geoip/maxminddb-compat-util.h @@ -0,0 +1,160 @@ +#include +#include + +/* *INDENT-OFF* */ + +/* The memmem, strdup, and strndup functions were all copied from the + * FreeBSD source, along with the relevant copyright notice. + * + * It'd be nicer to simply use the functions available on the system if they + * exist, but there doesn't seem to be a good way to detect them without also + * defining things like _GNU_SOURCE, which we want to avoid, because then we + * end up _accidentally_ using GNU features without noticing, which then + * breaks on systems like OSX. + * + * C is fun! */ + +/* Applies to memmem implementation */ +/*- + * Copyright (c) 2005 Pascal Gloor + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +static void * +mmdb_memmem(const void *l, size_t l_len, const void *s, size_t s_len) { + register char *cur, *last; + const char *cl = (const char *)l; + const char *cs = (const char *)s; + + /* we need something to compare */ + if (l_len == 0 || s_len == 0) + return NULL; + + /* "s" must be smaller or equal to "l" */ + if (l_len < s_len) + return NULL; + + /* special case where s_len == 1 */ + if (s_len == 1) + return memchr(l, (int)*cs, l_len); + + /* the last position where its possible to find "s" in "l" */ + last = (char *)cl + l_len - s_len; + + for (cur = (char *)cl; cur <= last; cur++) + if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0) + return cur; + + return NULL; +} + +/* Applies to strnlen implementation */ +/*- + * Copyright (c) 2009 David Schultz + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +static size_t mmdb_strnlen(const char *s, size_t maxlen) { + size_t len; + + for (len = 0; len < maxlen; len++, s++) { + if (!*s) + break; + } + return (len); +} + +/* Applies to strdup and strndup implementation */ +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +static char *mmdb_strdup(const char *str) { + size_t len; + char *copy; + + len = strlen(str) + 1; + if ((copy = malloc(len)) == NULL) + return (NULL); + memcpy(copy, str, len); + return (copy); +} + +static char *mmdb_strndup(const char *str, size_t n) { + size_t len; + char *copy; + + len = mmdb_strnlen(str, n); + if ((copy = malloc(len + 1)) == NULL) + return (NULL); + memcpy(copy, str, len); + copy[len] = '\0'; + return (copy); +} +/* *INDENT-ON* */ diff --git a/extensions/geoip/maxminddb.c b/extensions/geoip/maxminddb.c new file mode 100644 index 00000000..5e97426c --- /dev/null +++ b/extensions/geoip/maxminddb.c @@ -0,0 +1,2092 @@ +#if HAVE_CONFIG_H +#include +#endif +#include "data-pool.h" +#include "maxminddb-compat-util.h" +#include "maxminddb.h" +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#ifndef UNICODE +#define UNICODE +#endif +#include +#include +#else +#include +#include +#include +#endif + +#define MMDB_DATA_SECTION_SEPARATOR (16) +#define MAXIMUM_DATA_STRUCTURE_DEPTH (512) + +#ifdef MMDB_DEBUG +#define DEBUG_MSG(msg) fprintf(stderr, msg "\n") +#define DEBUG_MSGF(fmt, ...) fprintf(stderr, fmt "\n", __VA_ARGS__) +#define DEBUG_BINARY(fmt, byte) \ + do { \ + char *binary = byte_to_binary(byte); \ + if (NULL == binary) { \ + fprintf(stderr, "Calloc failed in DEBUG_BINARY\n"); \ + abort(); \ + } \ + fprintf(stderr, fmt "\n", binary); \ + free(binary); \ + } while (0) +#define DEBUG_NL fprintf(stderr, "\n") +#else +#define DEBUG_MSG(...) +#define DEBUG_MSGF(...) +#define DEBUG_BINARY(...) +#define DEBUG_NL +#endif + +#ifdef MMDB_DEBUG +char *byte_to_binary(uint8_t byte) { + char *bits = calloc(9, sizeof(char)); + if (NULL == bits) { + return bits; + } + + for (uint8_t i = 0; i < 8; i++) { + bits[i] = byte & (128 >> i) ? '1' : '0'; + } + bits[8] = '\0'; + + return bits; +} + +char *type_num_to_name(uint8_t num) { + switch (num) { + case 0: + return "extended"; + case 1: + return "pointer"; + case 2: + return "utf8_string"; + case 3: + return "double"; + case 4: + return "bytes"; + case 5: + return "uint16"; + case 6: + return "uint32"; + case 7: + return "map"; + case 8: + return "int32"; + case 9: + return "uint64"; + case 10: + return "uint128"; + case 11: + return "array"; + case 12: + return "container"; + case 13: + return "end_marker"; + case 14: + return "boolean"; + case 15: + return "float"; + default: + return "unknown type"; + } +} +#endif + +/* None of the values we check on the lhs are bigger than uint32_t, so on + * platforms where SIZE_MAX is a 64-bit integer, this would be a no-op, and it + * makes the compiler complain if we do the check anyway. */ +#if SIZE_MAX == UINT32_MAX +#define MAYBE_CHECK_SIZE_OVERFLOW(lhs, rhs, error) \ + if ((lhs) > (rhs)) { \ + return error; \ + } +#else +#define MAYBE_CHECK_SIZE_OVERFLOW(...) +#endif + +typedef struct record_info_s { + uint16_t record_length; + uint32_t (*left_record_getter)(const uint8_t *); + uint32_t (*right_record_getter)(const uint8_t *); + uint8_t right_record_offset; +} record_info_s; + +#define METADATA_MARKER "\xab\xcd\xefMaxMind.com" +/* This is 128kb */ +#define METADATA_BLOCK_MAX_SIZE 131072 + +// 64 leads us to allocating 4 KiB on a 64bit system. +#define MMDB_POOL_INIT_SIZE 64 + +static int map_file(MMDB_s *const mmdb); +static const uint8_t *find_metadata(const uint8_t *file_content, + ssize_t file_size, + uint32_t *metadata_size); +static int read_metadata(MMDB_s *mmdb); +static MMDB_s make_fake_metadata_db(const MMDB_s *const mmdb); +static int +value_for_key_as_uint16(MMDB_entry_s *start, char *key, uint16_t *value); +static int +value_for_key_as_uint32(MMDB_entry_s *start, char *key, uint32_t *value); +static int +value_for_key_as_uint64(MMDB_entry_s *start, char *key, uint64_t *value); +static int +value_for_key_as_string(MMDB_entry_s *start, char *key, char const **value); +static int populate_languages_metadata(MMDB_s *mmdb, + MMDB_s *metadata_db, + MMDB_entry_s *metadata_start); +static int populate_description_metadata(MMDB_s *mmdb, + MMDB_s *metadata_db, + MMDB_entry_s *metadata_start); +static int resolve_any_address(const char *ipstr, struct addrinfo **addresses); +static int find_address_in_search_tree(const MMDB_s *const mmdb, + uint8_t *address, + sa_family_t address_family, + MMDB_lookup_result_s *result); +static record_info_s record_info_for_database(const MMDB_s *const mmdb); +static int find_ipv4_start_node(MMDB_s *const mmdb); +static uint8_t record_type(const MMDB_s *const mmdb, uint64_t record); +static uint32_t get_left_28_bit_record(const uint8_t *record); +static uint32_t get_right_28_bit_record(const uint8_t *record); +static uint32_t data_section_offset_for_record(const MMDB_s *const mmdb, + uint64_t record); +static int path_length(va_list va_path); +static int lookup_path_in_array(const char *path_elem, + const MMDB_s *const mmdb, + MMDB_entry_data_s *entry_data); +static int lookup_path_in_map(const char *path_elem, + const MMDB_s *const mmdb, + MMDB_entry_data_s *entry_data); +static int skip_map_or_array(const MMDB_s *const mmdb, + MMDB_entry_data_s *entry_data); +static int decode_one_follow(const MMDB_s *const mmdb, + uint32_t offset, + MMDB_entry_data_s *entry_data); +static int decode_one(const MMDB_s *const mmdb, + uint32_t offset, + MMDB_entry_data_s *entry_data); +static int get_ext_type(int raw_ext_type); +static uint32_t +get_ptr_from(uint8_t ctrl, uint8_t const *const ptr, int ptr_size); +static int get_entry_data_list(const MMDB_s *const mmdb, + uint32_t offset, + MMDB_entry_data_list_s *const entry_data_list, + MMDB_data_pool_s *const pool, + int depth); +static float get_ieee754_float(const uint8_t *restrict p); +static double get_ieee754_double(const uint8_t *restrict p); +static uint32_t get_uint32(const uint8_t *p); +static uint32_t get_uint24(const uint8_t *p); +static uint32_t get_uint16(const uint8_t *p); +static uint64_t get_uintX(const uint8_t *p, int length); +static int32_t get_sintX(const uint8_t *p, int length); +static void free_mmdb_struct(MMDB_s *const mmdb); +static void free_languages_metadata(MMDB_s *mmdb); +static void free_descriptions_metadata(MMDB_s *mmdb); +static MMDB_entry_data_list_s * +dump_entry_data_list(FILE *stream, + MMDB_entry_data_list_s *entry_data_list, + int indent, + int *status); +static void print_indentation(FILE *stream, int i); +static char *bytes_to_hex(uint8_t *bytes, uint32_t size); + +#define CHECKED_DECODE_ONE(mmdb, offset, entry_data) \ + do { \ + int status = decode_one(mmdb, offset, entry_data); \ + if (MMDB_SUCCESS != status) { \ + DEBUG_MSGF("CHECKED_DECODE_ONE failed." \ + " status = %d (%s)", \ + status, \ + MMDB_strerror(status)); \ + return status; \ + } \ + } while (0) + +#define CHECKED_DECODE_ONE_FOLLOW(mmdb, offset, entry_data) \ + do { \ + int status = decode_one_follow(mmdb, offset, entry_data); \ + if (MMDB_SUCCESS != status) { \ + DEBUG_MSGF("CHECKED_DECODE_ONE_FOLLOW failed." \ + " status = %d (%s)", \ + status, \ + MMDB_strerror(status)); \ + return status; \ + } \ + } 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) { + int status = MMDB_SUCCESS; + + mmdb->file_content = NULL; + mmdb->data_section = NULL; + mmdb->metadata.database_type = NULL; + mmdb->metadata.languages.count = 0; + mmdb->metadata.languages.names = NULL; + mmdb->metadata.description.count = 0; + + mmdb->filename = mmdb_strdup(filename); + if (NULL == mmdb->filename) { + status = MMDB_OUT_OF_MEMORY_ERROR; + goto cleanup; + } + + if ((flags & MMDB_MODE_MASK) == 0) { + flags |= MMDB_MODE_MMAP; + } + mmdb->flags = flags; + + if (MMDB_SUCCESS != (status = map_file(mmdb))) { + goto cleanup; + } + +#ifdef _WIN32 + WSADATA wsa; + WSAStartup(MAKEWORD(2, 2), &wsa); +#endif + + uint32_t metadata_size = 0; + const uint8_t *metadata = + find_metadata(mmdb->file_content, mmdb->file_size, &metadata_size); + if (NULL == metadata) { + status = MMDB_INVALID_METADATA_ERROR; + goto cleanup; + } + + mmdb->metadata_section = metadata; + mmdb->metadata_section_size = metadata_size; + + status = read_metadata(mmdb); + if (MMDB_SUCCESS != status) { + goto cleanup; + } + + if (mmdb->metadata.binary_format_major_version != 2) { + status = MMDB_UNKNOWN_DATABASE_FORMAT_ERROR; + goto cleanup; + } + + uint32_t search_tree_size = + mmdb->metadata.node_count * mmdb->full_record_byte_size; + + mmdb->data_section = + mmdb->file_content + search_tree_size + MMDB_DATA_SECTION_SEPARATOR; + if (search_tree_size + MMDB_DATA_SECTION_SEPARATOR > + (uint32_t)mmdb->file_size) { + status = MMDB_INVALID_METADATA_ERROR; + goto cleanup; + } + mmdb->data_section_size = (uint32_t)mmdb->file_size - search_tree_size - + MMDB_DATA_SECTION_SEPARATOR; + + // Although it is likely not possible to construct a database with valid + // valid metadata, as parsed above, and a data_section_size less than 3, + // we do this check as later we assume it is at least three when doing + // bound checks. + if (mmdb->data_section_size < 3) { + status = MMDB_INVALID_DATA_ERROR; + goto cleanup; + } + + mmdb->metadata_section = metadata; + mmdb->ipv4_start_node.node_value = 0; + mmdb->ipv4_start_node.netmask = 0; + + // We do this immediately as otherwise there is a race to set + // ipv4_start_node.node_value and ipv4_start_node.netmask. + if (mmdb->metadata.ip_version == 6) { + status = find_ipv4_start_node(mmdb); + if (status != MMDB_SUCCESS) { + goto cleanup; + } + } + +cleanup: + if (MMDB_SUCCESS != status) { + int saved_errno = errno; + free_mmdb_struct(mmdb); + errno = saved_errno; + } + return status; +} + +#ifdef _WIN32 + +static LPWSTR utf8_to_utf16(const char *utf8_str) { + int wide_chars = MultiByteToWideChar(CP_UTF8, 0, utf8_str, -1, NULL, 0); + wchar_t *utf16_str = (wchar_t *)calloc(wide_chars, sizeof(wchar_t)); + if (!utf16_str) { + return NULL; + } + + if (MultiByteToWideChar(CP_UTF8, 0, utf8_str, -1, utf16_str, wide_chars) < + 1) { + free(utf16_str); + return NULL; + } + + return utf16_str; +} + +static int map_file(MMDB_s *const mmdb) { + DWORD size; + int status = MMDB_SUCCESS; + HANDLE mmh = NULL; + HANDLE fd = INVALID_HANDLE_VALUE; + LPWSTR utf16_filename = utf8_to_utf16(mmdb->filename); + if (!utf16_filename) { + status = MMDB_FILE_OPEN_ERROR; + goto cleanup; + } + fd = CreateFileW(utf16_filename, + GENERIC_READ, + FILE_SHARE_READ, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL); + if (fd == INVALID_HANDLE_VALUE) { + status = MMDB_FILE_OPEN_ERROR; + goto cleanup; + } + size = GetFileSize(fd, NULL); + if (size == INVALID_FILE_SIZE) { + status = MMDB_FILE_OPEN_ERROR; + goto cleanup; + } + mmh = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, size, NULL); + /* Microsoft documentation for CreateFileMapping indicates this returns + NULL not INVALID_HANDLE_VALUE on error */ + if (NULL == mmh) { + status = MMDB_IO_ERROR; + goto cleanup; + } + uint8_t *file_content = + (uint8_t *)MapViewOfFile(mmh, FILE_MAP_READ, 0, 0, 0); + if (file_content == NULL) { + status = MMDB_IO_ERROR; + goto cleanup; + } + + mmdb->file_size = size; + mmdb->file_content = file_content; + +cleanup:; + int saved_errno = errno; + if (INVALID_HANDLE_VALUE != fd) { + CloseHandle(fd); + } + if (NULL != mmh) { + CloseHandle(mmh); + } + errno = saved_errno; + free(utf16_filename); + + return status; +} + +#else // _WIN32 + +static int map_file(MMDB_s *const mmdb) { + ssize_t size; + int status = MMDB_SUCCESS; + + int flags = O_RDONLY; +#ifdef O_CLOEXEC + flags |= O_CLOEXEC; +#endif + int fd = open(mmdb->filename, flags); + struct stat s; + if (fd < 0 || fstat(fd, &s)) { + status = MMDB_FILE_OPEN_ERROR; + goto cleanup; + } + + size = s.st_size; + if (size < 0 || size != s.st_size) { + status = MMDB_OUT_OF_MEMORY_ERROR; + goto cleanup; + } + + uint8_t *file_content = + (uint8_t *)mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); + if (MAP_FAILED == file_content) { + if (ENOMEM == errno) { + status = MMDB_OUT_OF_MEMORY_ERROR; + } else { + status = MMDB_IO_ERROR; + } + goto cleanup; + } + + mmdb->file_size = size; + mmdb->file_content = file_content; + +cleanup:; + int saved_errno = errno; + if (fd >= 0) { + close(fd); + } + errno = saved_errno; + + return status; +} + +#endif // _WIN32 + +static const uint8_t *find_metadata(const uint8_t *file_content, + ssize_t file_size, + uint32_t *metadata_size) { + const ssize_t marker_len = sizeof(METADATA_MARKER) - 1; + ssize_t max_size = file_size > METADATA_BLOCK_MAX_SIZE + ? METADATA_BLOCK_MAX_SIZE + : file_size; + + uint8_t *search_area = (uint8_t *)(file_content + (file_size - max_size)); + uint8_t *start = search_area; + uint8_t *tmp; + do { + tmp = mmdb_memmem(search_area, max_size, METADATA_MARKER, marker_len); + + if (NULL != tmp) { + max_size -= tmp - search_area; + search_area = tmp; + + /* Continue searching just after the marker we just read, in case + * there are multiple markers in the same file. This would be odd + * but is certainly not impossible. */ + max_size -= marker_len; + search_area += marker_len; + } + } while (NULL != tmp); + + if (search_area == start) { + return NULL; + } + + *metadata_size = (uint32_t)max_size; + + return search_area; +} + +static int read_metadata(MMDB_s *mmdb) { + /* We need to create a fake MMDB_s struct in order to decode values from + the metadata. The metadata is basically just like the data section, so we + want to use the same functions we use for the data section to get + metadata values. */ + MMDB_s metadata_db = make_fake_metadata_db(mmdb); + + MMDB_entry_s metadata_start = {.mmdb = &metadata_db, .offset = 0}; + + int status = value_for_key_as_uint32( + &metadata_start, "node_count", &mmdb->metadata.node_count); + if (MMDB_SUCCESS != status) { + return status; + } + if (!mmdb->metadata.node_count) { + DEBUG_MSG("could not find node_count value in metadata"); + return MMDB_INVALID_METADATA_ERROR; + } + + status = value_for_key_as_uint16( + &metadata_start, "record_size", &mmdb->metadata.record_size); + if (MMDB_SUCCESS != status) { + return status; + } + if (!mmdb->metadata.record_size) { + DEBUG_MSG("could not find record_size value in metadata"); + return MMDB_INVALID_METADATA_ERROR; + } + + if (mmdb->metadata.record_size != 24 && mmdb->metadata.record_size != 28 && + mmdb->metadata.record_size != 32) { + DEBUG_MSGF("bad record size in metadata: %i", + mmdb->metadata.record_size); + return MMDB_UNKNOWN_DATABASE_FORMAT_ERROR; + } + + status = value_for_key_as_uint16( + &metadata_start, "ip_version", &mmdb->metadata.ip_version); + if (MMDB_SUCCESS != status) { + return status; + } + if (!mmdb->metadata.ip_version) { + DEBUG_MSG("could not find ip_version value in metadata"); + return MMDB_INVALID_METADATA_ERROR; + } + if (!(mmdb->metadata.ip_version == 4 || mmdb->metadata.ip_version == 6)) { + DEBUG_MSGF("ip_version value in metadata is not 4 or 6 - it was %i", + mmdb->metadata.ip_version); + return MMDB_INVALID_METADATA_ERROR; + } + + status = value_for_key_as_string( + &metadata_start, "database_type", &mmdb->metadata.database_type); + if (MMDB_SUCCESS != status) { + DEBUG_MSG("error finding database_type value in metadata"); + return status; + } + + status = populate_languages_metadata(mmdb, &metadata_db, &metadata_start); + if (MMDB_SUCCESS != status) { + DEBUG_MSG("could not populate languages from metadata"); + return status; + } + + status = + value_for_key_as_uint16(&metadata_start, + "binary_format_major_version", + &mmdb->metadata.binary_format_major_version); + if (MMDB_SUCCESS != status) { + return status; + } + if (!mmdb->metadata.binary_format_major_version) { + DEBUG_MSG( + "could not find binary_format_major_version value in metadata"); + return MMDB_INVALID_METADATA_ERROR; + } + + status = + value_for_key_as_uint16(&metadata_start, + "binary_format_minor_version", + &mmdb->metadata.binary_format_minor_version); + if (MMDB_SUCCESS != status) { + return status; + } + + status = value_for_key_as_uint64( + &metadata_start, "build_epoch", &mmdb->metadata.build_epoch); + if (MMDB_SUCCESS != status) { + return status; + } + if (!mmdb->metadata.build_epoch) { + DEBUG_MSG("could not find build_epoch value in metadata"); + return MMDB_INVALID_METADATA_ERROR; + } + + status = populate_description_metadata(mmdb, &metadata_db, &metadata_start); + if (MMDB_SUCCESS != status) { + DEBUG_MSG("could not populate description from metadata"); + return status; + } + + mmdb->full_record_byte_size = mmdb->metadata.record_size * 2 / 8U; + + mmdb->depth = mmdb->metadata.ip_version == 4 ? 32 : 128; + + return MMDB_SUCCESS; +} + +static MMDB_s make_fake_metadata_db(const MMDB_s *const mmdb) { + MMDB_s fake_metadata_db = {.data_section = mmdb->metadata_section, + .data_section_size = + mmdb->metadata_section_size}; + + return fake_metadata_db; +} + +static int +value_for_key_as_uint16(MMDB_entry_s *start, char *key, uint16_t *value) { + MMDB_entry_data_s entry_data; + const char *path[] = {key, NULL}; + int status = MMDB_aget_value(start, &entry_data, path); + if (MMDB_SUCCESS != status) { + return status; + } + if (MMDB_DATA_TYPE_UINT16 != entry_data.type) { + DEBUG_MSGF("expect uint16 for %s but received %s", + key, + type_num_to_name(entry_data.type)); + return MMDB_INVALID_METADATA_ERROR; + } + *value = entry_data.uint16; + return MMDB_SUCCESS; +} + +static int +value_for_key_as_uint32(MMDB_entry_s *start, char *key, uint32_t *value) { + MMDB_entry_data_s entry_data; + const char *path[] = {key, NULL}; + int status = MMDB_aget_value(start, &entry_data, path); + if (MMDB_SUCCESS != status) { + return status; + } + if (MMDB_DATA_TYPE_UINT32 != entry_data.type) { + DEBUG_MSGF("expect uint32 for %s but received %s", + key, + type_num_to_name(entry_data.type)); + return MMDB_INVALID_METADATA_ERROR; + } + *value = entry_data.uint32; + return MMDB_SUCCESS; +} + +static int +value_for_key_as_uint64(MMDB_entry_s *start, char *key, uint64_t *value) { + MMDB_entry_data_s entry_data; + const char *path[] = {key, NULL}; + int status = MMDB_aget_value(start, &entry_data, path); + if (MMDB_SUCCESS != status) { + return status; + } + if (MMDB_DATA_TYPE_UINT64 != entry_data.type) { + DEBUG_MSGF("expect uint64 for %s but received %s", + key, + type_num_to_name(entry_data.type)); + return MMDB_INVALID_METADATA_ERROR; + } + *value = entry_data.uint64; + return MMDB_SUCCESS; +} + +static int +value_for_key_as_string(MMDB_entry_s *start, char *key, char const **value) { + MMDB_entry_data_s entry_data; + const char *path[] = {key, NULL}; + int status = MMDB_aget_value(start, &entry_data, path); + if (MMDB_SUCCESS != status) { + return status; + } + if (MMDB_DATA_TYPE_UTF8_STRING != entry_data.type) { + DEBUG_MSGF("expect string for %s but received %s", + key, + type_num_to_name(entry_data.type)); + return MMDB_INVALID_METADATA_ERROR; + } + *value = mmdb_strndup((char *)entry_data.utf8_string, entry_data.data_size); + if (NULL == *value) { + return MMDB_OUT_OF_MEMORY_ERROR; + } + return MMDB_SUCCESS; +} + +static int populate_languages_metadata(MMDB_s *mmdb, + MMDB_s *metadata_db, + MMDB_entry_s *metadata_start) { + MMDB_entry_data_s entry_data; + + const char *path[] = {"languages", NULL}; + int status = MMDB_aget_value(metadata_start, &entry_data, path); + if (MMDB_SUCCESS != status) { + return status; + } + if (MMDB_DATA_TYPE_ARRAY != entry_data.type) { + return MMDB_INVALID_METADATA_ERROR; + } + + MMDB_entry_s array_start = {.mmdb = metadata_db, + .offset = entry_data.offset}; + + MMDB_entry_data_list_s *member; + status = MMDB_get_entry_data_list(&array_start, &member); + if (MMDB_SUCCESS != status) { + return status; + } + + MMDB_entry_data_list_s *first_member = member; + + uint32_t array_size = member->entry_data.data_size; + MAYBE_CHECK_SIZE_OVERFLOW( + array_size, SIZE_MAX / sizeof(char *), MMDB_INVALID_METADATA_ERROR); + + mmdb->metadata.languages.count = 0; + mmdb->metadata.languages.names = calloc(array_size, sizeof(char *)); + if (NULL == mmdb->metadata.languages.names) { + return MMDB_OUT_OF_MEMORY_ERROR; + } + + for (uint32_t i = 0; i < array_size; i++) { + member = member->next; + if (MMDB_DATA_TYPE_UTF8_STRING != member->entry_data.type) { + return MMDB_INVALID_METADATA_ERROR; + } + + mmdb->metadata.languages.names[i] = + mmdb_strndup((char *)member->entry_data.utf8_string, + member->entry_data.data_size); + + if (NULL == mmdb->metadata.languages.names[i]) { + return MMDB_OUT_OF_MEMORY_ERROR; + } + // We assign this as we go so that if we fail a calloc and need to + // free it, the count is right. + mmdb->metadata.languages.count = i + 1; + } + + MMDB_free_entry_data_list(first_member); + + return MMDB_SUCCESS; +} + +static int populate_description_metadata(MMDB_s *mmdb, + MMDB_s *metadata_db, + MMDB_entry_s *metadata_start) { + MMDB_entry_data_s entry_data; + + const char *path[] = {"description", NULL}; + int status = MMDB_aget_value(metadata_start, &entry_data, path); + if (MMDB_SUCCESS != status) { + return status; + } + + if (MMDB_DATA_TYPE_MAP != entry_data.type) { + DEBUG_MSGF("Unexpected entry_data type: %d", entry_data.type); + return MMDB_INVALID_METADATA_ERROR; + } + + MMDB_entry_s map_start = {.mmdb = metadata_db, .offset = entry_data.offset}; + + MMDB_entry_data_list_s *member; + status = MMDB_get_entry_data_list(&map_start, &member); + if (MMDB_SUCCESS != status) { + DEBUG_MSGF( + "MMDB_get_entry_data_list failed while populating description." + " status = %d (%s)", + status, + MMDB_strerror(status)); + return status; + } + + MMDB_entry_data_list_s *first_member = member; + + uint32_t map_size = member->entry_data.data_size; + mmdb->metadata.description.count = 0; + if (0 == map_size) { + mmdb->metadata.description.descriptions = NULL; + goto cleanup; + } + MAYBE_CHECK_SIZE_OVERFLOW(map_size, + SIZE_MAX / sizeof(MMDB_description_s *), + MMDB_INVALID_METADATA_ERROR); + + mmdb->metadata.description.descriptions = + calloc(map_size, sizeof(MMDB_description_s *)); + if (NULL == mmdb->metadata.description.descriptions) { + status = MMDB_OUT_OF_MEMORY_ERROR; + goto cleanup; + } + + for (uint32_t i = 0; i < map_size; i++) { + mmdb->metadata.description.descriptions[i] = + calloc(1, sizeof(MMDB_description_s)); + if (NULL == mmdb->metadata.description.descriptions[i]) { + status = MMDB_OUT_OF_MEMORY_ERROR; + goto cleanup; + } + + mmdb->metadata.description.count = i + 1; + mmdb->metadata.description.descriptions[i]->language = NULL; + mmdb->metadata.description.descriptions[i]->description = NULL; + + member = member->next; + + if (MMDB_DATA_TYPE_UTF8_STRING != member->entry_data.type) { + status = MMDB_INVALID_METADATA_ERROR; + goto cleanup; + } + + mmdb->metadata.description.descriptions[i]->language = + mmdb_strndup((char *)member->entry_data.utf8_string, + member->entry_data.data_size); + + if (NULL == mmdb->metadata.description.descriptions[i]->language) { + status = MMDB_OUT_OF_MEMORY_ERROR; + goto cleanup; + } + + member = member->next; + + if (MMDB_DATA_TYPE_UTF8_STRING != member->entry_data.type) { + status = MMDB_INVALID_METADATA_ERROR; + goto cleanup; + } + + mmdb->metadata.description.descriptions[i]->description = + mmdb_strndup((char *)member->entry_data.utf8_string, + member->entry_data.data_size); + + if (NULL == mmdb->metadata.description.descriptions[i]->description) { + status = MMDB_OUT_OF_MEMORY_ERROR; + goto cleanup; + } + } + +cleanup: + MMDB_free_entry_data_list(first_member); + + return status; +} + +MMDB_lookup_result_s MMDB_lookup_string(const MMDB_s *const mmdb, + const char *const ipstr, + int *const gai_error, + int *const mmdb_error) { + MMDB_lookup_result_s result = {.found_entry = false, + .netmask = 0, + .entry = {.mmdb = mmdb, .offset = 0}}; + + struct addrinfo *addresses = NULL; + *gai_error = resolve_any_address(ipstr, &addresses); + + if (!*gai_error) { + result = MMDB_lookup_sockaddr(mmdb, addresses->ai_addr, mmdb_error); + } + + if (NULL != addresses) { + freeaddrinfo(addresses); + } + + return result; +} + +static int resolve_any_address(const char *ipstr, struct addrinfo **addresses) { + struct addrinfo hints = { + .ai_family = AF_UNSPEC, + .ai_flags = AI_NUMERICHOST, + // We set ai_socktype so that we only get one result back + .ai_socktype = SOCK_STREAM}; + + int gai_status = getaddrinfo(ipstr, NULL, &hints, addresses); + if (gai_status) { + return gai_status; + } + + return 0; +} + +MMDB_lookup_result_s MMDB_lookup_sockaddr(const MMDB_s *const mmdb, + const struct sockaddr *const sockaddr, + int *const mmdb_error) { + MMDB_lookup_result_s result = {.found_entry = false, + .netmask = 0, + .entry = {.mmdb = mmdb, .offset = 0}}; + + uint8_t mapped_address[16], *address; + if (mmdb->metadata.ip_version == 4) { + if (sockaddr->sa_family == AF_INET6) { + *mmdb_error = MMDB_IPV6_LOOKUP_IN_IPV4_DATABASE_ERROR; + return result; + } + address = (uint8_t *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr; + } else { + if (sockaddr->sa_family == AF_INET6) { + address = (uint8_t *)&((struct sockaddr_in6 *)sockaddr) + ->sin6_addr.s6_addr; + } else { + address = mapped_address; + memset(address, 0, 12); + memcpy(address + 12, + &((struct sockaddr_in *)sockaddr)->sin_addr.s_addr, + 4); + } + } + + *mmdb_error = find_address_in_search_tree( + mmdb, address, sockaddr->sa_family, &result); + + return result; +} + +static int find_address_in_search_tree(const MMDB_s *const mmdb, + uint8_t *address, + sa_family_t address_family, + MMDB_lookup_result_s *result) { + record_info_s record_info = record_info_for_database(mmdb); + if (0 == record_info.right_record_offset) { + return MMDB_UNKNOWN_DATABASE_FORMAT_ERROR; + } + + uint32_t value = 0; + uint16_t current_bit = 0; + if (mmdb->metadata.ip_version == 6 && address_family == AF_INET) { + value = mmdb->ipv4_start_node.node_value; + current_bit = mmdb->ipv4_start_node.netmask; + } + + uint32_t node_count = mmdb->metadata.node_count; + const uint8_t *search_tree = mmdb->file_content; + const uint8_t *record_pointer; + for (; current_bit < mmdb->depth && value < node_count; current_bit++) { + uint8_t bit = + 1U & (address[current_bit >> 3] >> (7 - (current_bit % 8))); + + record_pointer = &search_tree[value * record_info.record_length]; + if (record_pointer + record_info.record_length > mmdb->data_section) { + return MMDB_CORRUPT_SEARCH_TREE_ERROR; + } + if (bit) { + record_pointer += record_info.right_record_offset; + value = record_info.right_record_getter(record_pointer); + } else { + value = record_info.left_record_getter(record_pointer); + } + } + + result->netmask = current_bit; + + if (value >= node_count + mmdb->data_section_size) { + // The pointer points off the end of the database. + return MMDB_CORRUPT_SEARCH_TREE_ERROR; + } + + if (value == node_count) { + // record is empty + result->found_entry = false; + return MMDB_SUCCESS; + } + result->found_entry = true; + result->entry.offset = data_section_offset_for_record(mmdb, value); + + return MMDB_SUCCESS; +} + +static record_info_s record_info_for_database(const MMDB_s *const mmdb) { + record_info_s record_info = {.record_length = mmdb->full_record_byte_size, + .right_record_offset = 0}; + + if (record_info.record_length == 6) { + record_info.left_record_getter = &get_uint24; + record_info.right_record_getter = &get_uint24; + record_info.right_record_offset = 3; + } else if (record_info.record_length == 7) { + record_info.left_record_getter = &get_left_28_bit_record; + record_info.right_record_getter = &get_right_28_bit_record; + record_info.right_record_offset = 3; + } else if (record_info.record_length == 8) { + record_info.left_record_getter = &get_uint32; + record_info.right_record_getter = &get_uint32; + record_info.right_record_offset = 4; + } else { + assert(false); + } + + return record_info; +} + +static int find_ipv4_start_node(MMDB_s *const mmdb) { + /* In a pathological case of a database with a single node search tree, + * this check will be true even after we've found the IPv4 start node, but + * that doesn't seem worth trying to fix. */ + if (mmdb->ipv4_start_node.node_value != 0) { + return MMDB_SUCCESS; + } + + record_info_s record_info = record_info_for_database(mmdb); + + const uint8_t *search_tree = mmdb->file_content; + uint32_t node_value = 0; + const uint8_t *record_pointer; + uint16_t netmask; + uint32_t node_count = mmdb->metadata.node_count; + + for (netmask = 0; netmask < 96 && node_value < node_count; netmask++) { + record_pointer = &search_tree[node_value * record_info.record_length]; + if (record_pointer + record_info.record_length > mmdb->data_section) { + return MMDB_CORRUPT_SEARCH_TREE_ERROR; + } + node_value = record_info.left_record_getter(record_pointer); + } + + mmdb->ipv4_start_node.node_value = node_value; + mmdb->ipv4_start_node.netmask = netmask; + + return MMDB_SUCCESS; +} + +static uint8_t record_type(const MMDB_s *const mmdb, uint64_t record) { + uint32_t node_count = mmdb->metadata.node_count; + + /* Ideally we'd check to make sure that a record never points to a + * previously seen value, but that's more complicated. For now, we can + * at least check that we don't end up at the top of the tree again. */ + if (record == 0) { + DEBUG_MSG("record has a value of 0"); + return MMDB_RECORD_TYPE_INVALID; + } + + if (record < node_count) { + return MMDB_RECORD_TYPE_SEARCH_NODE; + } + + if (record == node_count) { + return MMDB_RECORD_TYPE_EMPTY; + } + + if (record - node_count < mmdb->data_section_size) { + return MMDB_RECORD_TYPE_DATA; + } + + DEBUG_MSG("record has a value that points outside of the database"); + return MMDB_RECORD_TYPE_INVALID; +} + +static uint32_t get_left_28_bit_record(const uint8_t *record) { + return record[0] * 65536 + record[1] * 256 + record[2] + + ((record[3] & 0xf0) << 20); +} + +static uint32_t get_right_28_bit_record(const uint8_t *record) { + uint32_t value = get_uint32(record); + return value & 0xfffffff; +} + +int MMDB_read_node(const MMDB_s *const mmdb, + uint32_t node_number, + MMDB_search_node_s *const node) { + record_info_s record_info = record_info_for_database(mmdb); + if (0 == record_info.right_record_offset) { + return MMDB_UNKNOWN_DATABASE_FORMAT_ERROR; + } + + if (node_number > mmdb->metadata.node_count) { + return MMDB_INVALID_NODE_NUMBER_ERROR; + } + + const uint8_t *search_tree = mmdb->file_content; + const uint8_t *record_pointer = + &search_tree[node_number * record_info.record_length]; + node->left_record = record_info.left_record_getter(record_pointer); + record_pointer += record_info.right_record_offset; + node->right_record = record_info.right_record_getter(record_pointer); + + node->left_record_type = record_type(mmdb, node->left_record); + node->right_record_type = record_type(mmdb, node->right_record); + + // Note that offset will be invalid if the record type is not + // MMDB_RECORD_TYPE_DATA, but that's ok. Any use of the record entry + // for other data types is a programming error. + node->left_record_entry = (struct MMDB_entry_s){ + .mmdb = mmdb, + .offset = data_section_offset_for_record(mmdb, node->left_record), + }; + node->right_record_entry = (struct MMDB_entry_s){ + .mmdb = mmdb, + .offset = data_section_offset_for_record(mmdb, node->right_record), + }; + + return MMDB_SUCCESS; +} + +static uint32_t data_section_offset_for_record(const MMDB_s *const mmdb, + uint64_t record) { + return (uint32_t)record - mmdb->metadata.node_count - + MMDB_DATA_SECTION_SEPARATOR; +} + +int MMDB_get_value(MMDB_entry_s *const start, + MMDB_entry_data_s *const entry_data, + ...) { + va_list path; + va_start(path, entry_data); + int status = MMDB_vget_value(start, entry_data, path); + va_end(path); + return status; +} + +int MMDB_vget_value(MMDB_entry_s *const start, + MMDB_entry_data_s *const entry_data, + va_list va_path) { + int length = path_length(va_path); + const char *path_elem; + int i = 0; + + MAYBE_CHECK_SIZE_OVERFLOW(length, + SIZE_MAX / sizeof(const char *) - 1, + MMDB_INVALID_METADATA_ERROR); + + const char **path = calloc(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); + + free((char **)path); + + return status; +} + +static 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) { + const MMDB_s *const mmdb = start->mmdb; + uint32_t offset = start->offset; + + memset(entry_data, 0, sizeof(MMDB_entry_data_s)); + DEBUG_NL; + DEBUG_MSG("looking up value by path"); + + CHECKED_DECODE_ONE_FOLLOW(mmdb, offset, entry_data); + + DEBUG_NL; + DEBUG_MSGF("top level element is a %s", type_num_to_name(entry_data->type)); + + /* Can this happen? It'd probably represent a pathological case under + * normal use, but there's nothing preventing someone from passing an + * invalid MMDB_entry_s struct to this function */ + if (!entry_data->has_data) { + return MMDB_INVALID_LOOKUP_PATH_ERROR; + } + + const char *path_elem; + int i = 0; + while (NULL != (path_elem = path[i++])) { + DEBUG_NL; + DEBUG_MSGF("path elem = %s", path_elem); + + /* XXX - it'd be good to find a quicker way to skip through these + entries that doesn't involve decoding them + completely. Basically we need to just use the size from the + control byte to advance our pointer rather than calling + decode_one(). */ + if (entry_data->type == MMDB_DATA_TYPE_ARRAY) { + int status = lookup_path_in_array(path_elem, mmdb, entry_data); + if (MMDB_SUCCESS != status) { + memset(entry_data, 0, sizeof(MMDB_entry_data_s)); + return status; + } + } else if (entry_data->type == MMDB_DATA_TYPE_MAP) { + int status = lookup_path_in_map(path_elem, mmdb, entry_data); + if (MMDB_SUCCESS != status) { + memset(entry_data, 0, sizeof(MMDB_entry_data_s)); + return status; + } + } else { + /* Once we make the code traverse maps & arrays without calling + * decode_one() we can get rid of this. */ + memset(entry_data, 0, sizeof(MMDB_entry_data_s)); + return MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR; + } + } + + return MMDB_SUCCESS; +} + +static int lookup_path_in_array(const char *path_elem, + const MMDB_s *const mmdb, + MMDB_entry_data_s *entry_data) { + uint32_t size = entry_data->data_size; + char *first_invalid; + + int saved_errno = errno; + errno = 0; + int array_index = strtol(path_elem, &first_invalid, 10); + if (ERANGE == errno) { + errno = saved_errno; + return MMDB_INVALID_LOOKUP_PATH_ERROR; + } + errno = saved_errno; + + if (array_index < 0) { + array_index += size; + + if (array_index < 0) { + return MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR; + } + } + + if (*first_invalid || (uint32_t)array_index >= size) { + return MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR; + } + + for (int i = 0; i < array_index; i++) { + /* We don't want to follow a pointer here. If the next element is a + * pointer we simply skip it and keep going */ + CHECKED_DECODE_ONE(mmdb, entry_data->offset_to_next, entry_data); + int status = skip_map_or_array(mmdb, entry_data); + if (MMDB_SUCCESS != status) { + return status; + } + } + + MMDB_entry_data_s value; + CHECKED_DECODE_ONE_FOLLOW(mmdb, entry_data->offset_to_next, &value); + memcpy(entry_data, &value, sizeof(MMDB_entry_data_s)); + + return MMDB_SUCCESS; +} + +static int lookup_path_in_map(const char *path_elem, + const MMDB_s *const mmdb, + MMDB_entry_data_s *entry_data) { + uint32_t size = entry_data->data_size; + uint32_t offset = entry_data->offset_to_next; + size_t path_elem_len = strlen(path_elem); + + while (size-- > 0) { + MMDB_entry_data_s key, value; + CHECKED_DECODE_ONE_FOLLOW(mmdb, offset, &key); + + uint32_t offset_to_value = key.offset_to_next; + + if (MMDB_DATA_TYPE_UTF8_STRING != key.type) { + return MMDB_INVALID_DATA_ERROR; + } + + if (key.data_size == path_elem_len && + !memcmp(path_elem, key.utf8_string, path_elem_len)) { + + DEBUG_MSG("found key matching path elem"); + + CHECKED_DECODE_ONE_FOLLOW(mmdb, offset_to_value, &value); + memcpy(entry_data, &value, sizeof(MMDB_entry_data_s)); + return MMDB_SUCCESS; + } else { + /* We don't want to follow a pointer here. If the next element is + * a pointer we simply skip it and keep going */ + CHECKED_DECODE_ONE(mmdb, offset_to_value, &value); + int status = skip_map_or_array(mmdb, &value); + if (MMDB_SUCCESS != status) { + return status; + } + offset = value.offset_to_next; + } + } + + memset(entry_data, 0, sizeof(MMDB_entry_data_s)); + return MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR; +} + +static int skip_map_or_array(const MMDB_s *const mmdb, + MMDB_entry_data_s *entry_data) { + if (entry_data->type == MMDB_DATA_TYPE_MAP) { + uint32_t size = entry_data->data_size; + while (size-- > 0) { + CHECKED_DECODE_ONE( + mmdb, entry_data->offset_to_next, entry_data); // key + CHECKED_DECODE_ONE( + mmdb, entry_data->offset_to_next, entry_data); // value + int status = skip_map_or_array(mmdb, entry_data); + if (MMDB_SUCCESS != status) { + return status; + } + } + } else if (entry_data->type == MMDB_DATA_TYPE_ARRAY) { + uint32_t size = entry_data->data_size; + while (size-- > 0) { + CHECKED_DECODE_ONE( + mmdb, entry_data->offset_to_next, entry_data); // value + int status = skip_map_or_array(mmdb, entry_data); + if (MMDB_SUCCESS != status) { + return status; + } + } + } + + return MMDB_SUCCESS; +} + +static int decode_one_follow(const MMDB_s *const mmdb, + uint32_t offset, + MMDB_entry_data_s *entry_data) { + CHECKED_DECODE_ONE(mmdb, offset, entry_data); + if (entry_data->type == MMDB_DATA_TYPE_POINTER) { + uint32_t next = entry_data->offset_to_next; + CHECKED_DECODE_ONE(mmdb, entry_data->pointer, entry_data); + /* Pointers to pointers are illegal under the spec */ + if (entry_data->type == MMDB_DATA_TYPE_POINTER) { + DEBUG_MSG("pointer points to another pointer"); + return MMDB_INVALID_DATA_ERROR; + } + + /* The pointer could point to any part of the data section but the + * next entry for this particular offset may be the one after the + * pointer, not the one after whatever the pointer points to. This + * depends on whether the pointer points to something that is a simple + * value or a compound value. For a compound value, the next one is + * the one after the pointer result, not the one after the pointer. */ + if (entry_data->type != MMDB_DATA_TYPE_MAP && + entry_data->type != MMDB_DATA_TYPE_ARRAY) { + + entry_data->offset_to_next = next; + } + } + + return MMDB_SUCCESS; +} + +#if !MMDB_UINT128_IS_BYTE_ARRAY +static mmdb_uint128_t get_uint128(const uint8_t *p, int length) { + mmdb_uint128_t value = 0; + while (length-- > 0) { + value <<= 8; + value += *p++; + } + return value; +} +#endif + +static int decode_one(const MMDB_s *const mmdb, + uint32_t offset, + MMDB_entry_data_s *entry_data) { + const uint8_t *mem = mmdb->data_section; + + // We subtract rather than add as it possible that offset + 1 + // could overflow for a corrupt database while an underflow + // from data_section_size - 1 should not be possible. + if (offset > mmdb->data_section_size - 1) { + DEBUG_MSGF("Offset (%d) past data section (%d)", + offset, + mmdb->data_section_size); + return MMDB_INVALID_DATA_ERROR; + } + + entry_data->offset = offset; + entry_data->has_data = true; + + DEBUG_NL; + DEBUG_MSGF("Offset: %i", offset); + + uint8_t ctrl = mem[offset++]; + DEBUG_BINARY("Control byte: %s", ctrl); + + int type = (ctrl >> 5) & 7; + DEBUG_MSGF("Type: %i (%s)", type, type_num_to_name(type)); + + if (type == MMDB_DATA_TYPE_EXTENDED) { + // Subtracting 1 to avoid possible overflow on offset + 1 + if (offset > mmdb->data_section_size - 1) { + DEBUG_MSGF("Extended type offset (%d) past data section (%d)", + offset, + mmdb->data_section_size); + return MMDB_INVALID_DATA_ERROR; + } + type = get_ext_type(mem[offset++]); + DEBUG_MSGF("Extended type: %i (%s)", type, type_num_to_name(type)); + } + + entry_data->type = type; + + if (type == MMDB_DATA_TYPE_POINTER) { + uint8_t psize = ((ctrl >> 3) & 3) + 1; + DEBUG_MSGF("Pointer size: %i", psize); + + // We check that the offset does not extend past the end of the + // database and that the subtraction of psize did not underflow. + if (offset > mmdb->data_section_size - psize || + mmdb->data_section_size < psize) { + DEBUG_MSGF("Pointer offset (%d) past data section (%d)", + offset + psize, + mmdb->data_section_size); + return MMDB_INVALID_DATA_ERROR; + } + entry_data->pointer = get_ptr_from(ctrl, &mem[offset], psize); + DEBUG_MSGF("Pointer to: %i", entry_data->pointer); + + entry_data->data_size = psize; + entry_data->offset_to_next = offset + psize; + return MMDB_SUCCESS; + } + + uint32_t size = ctrl & 31; + switch (size) { + case 29: + // We subtract when checking offset to avoid possible overflow + if (offset > mmdb->data_section_size - 1) { + DEBUG_MSGF("String end (%d, case 29) past data section (%d)", + offset, + mmdb->data_section_size); + return MMDB_INVALID_DATA_ERROR; + } + size = 29 + mem[offset++]; + break; + case 30: + // We subtract when checking offset to avoid possible overflow + if (offset > mmdb->data_section_size - 2) { + DEBUG_MSGF("String end (%d, case 30) past data section (%d)", + offset, + mmdb->data_section_size); + return MMDB_INVALID_DATA_ERROR; + } + size = 285 + get_uint16(&mem[offset]); + offset += 2; + break; + case 31: + // We subtract when checking offset to avoid possible overflow + if (offset > mmdb->data_section_size - 3) { + DEBUG_MSGF("String end (%d, case 31) past data section (%d)", + offset, + mmdb->data_section_size); + return MMDB_INVALID_DATA_ERROR; + } + size = 65821 + get_uint24(&mem[offset]); + offset += 3; + default: + break; + } + + DEBUG_MSGF("Size: %i", size); + + if (type == MMDB_DATA_TYPE_MAP || type == MMDB_DATA_TYPE_ARRAY) { + entry_data->data_size = size; + entry_data->offset_to_next = offset; + return MMDB_SUCCESS; + } + + if (type == MMDB_DATA_TYPE_BOOLEAN) { + entry_data->boolean = size ? true : false; + entry_data->data_size = 0; + entry_data->offset_to_next = offset; + DEBUG_MSGF("boolean value: %s", entry_data->boolean ? "true" : "false"); + return MMDB_SUCCESS; + } + + // Check that the data doesn't extend past the end of the memory + // buffer and that the calculation in doing this did not underflow. + if (offset > mmdb->data_section_size - size || + mmdb->data_section_size < size) { + DEBUG_MSGF("Data end (%d) past data section (%d)", + offset + size, + mmdb->data_section_size); + return MMDB_INVALID_DATA_ERROR; + } + + if (type == MMDB_DATA_TYPE_UINT16) { + if (size > 2) { + DEBUG_MSGF("uint16 of size %d", size); + return MMDB_INVALID_DATA_ERROR; + } + entry_data->uint16 = (uint16_t)get_uintX(&mem[offset], size); + DEBUG_MSGF("uint16 value: %u", entry_data->uint16); + } else if (type == MMDB_DATA_TYPE_UINT32) { + if (size > 4) { + DEBUG_MSGF("uint32 of size %d", size); + return MMDB_INVALID_DATA_ERROR; + } + entry_data->uint32 = (uint32_t)get_uintX(&mem[offset], size); + DEBUG_MSGF("uint32 value: %u", entry_data->uint32); + } else if (type == MMDB_DATA_TYPE_INT32) { + if (size > 4) { + DEBUG_MSGF("int32 of size %d", size); + return MMDB_INVALID_DATA_ERROR; + } + entry_data->int32 = get_sintX(&mem[offset], size); + DEBUG_MSGF("int32 value: %i", entry_data->int32); + } else if (type == MMDB_DATA_TYPE_UINT64) { + if (size > 8) { + DEBUG_MSGF("uint64 of size %d", size); + return MMDB_INVALID_DATA_ERROR; + } + entry_data->uint64 = get_uintX(&mem[offset], size); + DEBUG_MSGF("uint64 value: %" PRIu64, entry_data->uint64); + } else if (type == MMDB_DATA_TYPE_UINT128) { + if (size > 16) { + DEBUG_MSGF("uint128 of size %d", size); + return MMDB_INVALID_DATA_ERROR; + } +#if MMDB_UINT128_IS_BYTE_ARRAY + memset(entry_data->uint128, 0, 16); + if (size > 0) { + memcpy(entry_data->uint128 + 16 - size, &mem[offset], size); + } +#else + entry_data->uint128 = get_uint128(&mem[offset], size); +#endif + } else if (type == MMDB_DATA_TYPE_FLOAT) { + if (size != 4) { + DEBUG_MSGF("float of size %d", size); + return MMDB_INVALID_DATA_ERROR; + } + size = 4; + entry_data->float_value = get_ieee754_float(&mem[offset]); + DEBUG_MSGF("float value: %f", entry_data->float_value); + } else if (type == MMDB_DATA_TYPE_DOUBLE) { + if (size != 8) { + DEBUG_MSGF("double of size %d", size); + return MMDB_INVALID_DATA_ERROR; + } + size = 8; + entry_data->double_value = get_ieee754_double(&mem[offset]); + DEBUG_MSGF("double value: %f", entry_data->double_value); + } else if (type == MMDB_DATA_TYPE_UTF8_STRING) { + entry_data->utf8_string = size == 0 ? "" : (char *)&mem[offset]; + entry_data->data_size = size; +#ifdef MMDB_DEBUG + char *string = + mmdb_strndup(entry_data->utf8_string, size > 50 ? 50 : size); + if (NULL == string) { + abort(); + } + DEBUG_MSGF("string value: %s", string); + free(string); +#endif + } else if (type == MMDB_DATA_TYPE_BYTES) { + entry_data->bytes = &mem[offset]; + entry_data->data_size = size; + } + + entry_data->offset_to_next = offset + size; + + return MMDB_SUCCESS; +} + +static int get_ext_type(int raw_ext_type) { return 7 + raw_ext_type; } + +static uint32_t +get_ptr_from(uint8_t ctrl, uint8_t const *const ptr, int ptr_size) { + uint32_t new_offset; + switch (ptr_size) { + case 1: + new_offset = ((ctrl & 7) << 8) + ptr[0]; + break; + case 2: + new_offset = 2048 + ((ctrl & 7) << 16) + (ptr[0] << 8) + ptr[1]; + break; + case 3: + new_offset = 2048 + 524288 + ((ctrl & 7) << 24) + get_uint24(ptr); + break; + case 4: + default: + new_offset = get_uint32(ptr); + break; + } + return new_offset; +} + +int MMDB_get_metadata_as_entry_data_list( + const MMDB_s *const mmdb, MMDB_entry_data_list_s **const entry_data_list) { + MMDB_s metadata_db = make_fake_metadata_db(mmdb); + + MMDB_entry_s metadata_start = {.mmdb = &metadata_db, .offset = 0}; + + return MMDB_get_entry_data_list(&metadata_start, entry_data_list); +} + +int MMDB_get_entry_data_list(MMDB_entry_s *start, + MMDB_entry_data_list_s **const entry_data_list) { + MMDB_data_pool_s *const pool = data_pool_new(MMDB_POOL_INIT_SIZE); + if (!pool) { + return MMDB_OUT_OF_MEMORY_ERROR; + } + + MMDB_entry_data_list_s *const list = data_pool_alloc(pool); + if (!list) { + data_pool_destroy(pool); + return MMDB_OUT_OF_MEMORY_ERROR; + } + + int const status = + get_entry_data_list(start->mmdb, start->offset, list, pool, 0); + + *entry_data_list = data_pool_to_list(pool); + if (!*entry_data_list) { + data_pool_destroy(pool); + return MMDB_OUT_OF_MEMORY_ERROR; + } + + return status; +} + +static int get_entry_data_list(const MMDB_s *const mmdb, + uint32_t offset, + MMDB_entry_data_list_s *const entry_data_list, + MMDB_data_pool_s *const pool, + int depth) { + if (depth >= MAXIMUM_DATA_STRUCTURE_DEPTH) { + DEBUG_MSG("reached the maximum data structure depth"); + return MMDB_INVALID_DATA_ERROR; + } + depth++; + CHECKED_DECODE_ONE(mmdb, offset, &entry_data_list->entry_data); + + switch (entry_data_list->entry_data.type) { + case MMDB_DATA_TYPE_POINTER: { + uint32_t next_offset = entry_data_list->entry_data.offset_to_next; + uint32_t last_offset; + CHECKED_DECODE_ONE(mmdb, + last_offset = + entry_data_list->entry_data.pointer, + &entry_data_list->entry_data); + + /* Pointers to pointers are illegal under the spec */ + if (entry_data_list->entry_data.type == MMDB_DATA_TYPE_POINTER) { + DEBUG_MSG("pointer points to another pointer"); + return MMDB_INVALID_DATA_ERROR; + } + + if (entry_data_list->entry_data.type == MMDB_DATA_TYPE_ARRAY || + entry_data_list->entry_data.type == MMDB_DATA_TYPE_MAP) { + + int status = get_entry_data_list( + mmdb, last_offset, entry_data_list, pool, depth); + if (MMDB_SUCCESS != status) { + DEBUG_MSG("get_entry_data_list on pointer failed."); + return status; + } + } + entry_data_list->entry_data.offset_to_next = next_offset; + } break; + case MMDB_DATA_TYPE_ARRAY: { + uint32_t array_size = entry_data_list->entry_data.data_size; + uint32_t array_offset = entry_data_list->entry_data.offset_to_next; + while (array_size-- > 0) { + MMDB_entry_data_list_s *entry_data_list_to = + data_pool_alloc(pool); + if (!entry_data_list_to) { + return MMDB_OUT_OF_MEMORY_ERROR; + } + + int status = get_entry_data_list( + mmdb, array_offset, entry_data_list_to, pool, depth); + if (MMDB_SUCCESS != status) { + DEBUG_MSG("get_entry_data_list on array element failed."); + return status; + } + + array_offset = entry_data_list_to->entry_data.offset_to_next; + } + entry_data_list->entry_data.offset_to_next = array_offset; + + } break; + case MMDB_DATA_TYPE_MAP: { + uint32_t size = entry_data_list->entry_data.data_size; + + offset = entry_data_list->entry_data.offset_to_next; + while (size-- > 0) { + MMDB_entry_data_list_s *list_key = data_pool_alloc(pool); + if (!list_key) { + return MMDB_OUT_OF_MEMORY_ERROR; + } + + int status = + get_entry_data_list(mmdb, offset, list_key, pool, depth); + if (MMDB_SUCCESS != status) { + DEBUG_MSG("get_entry_data_list on map key failed."); + return status; + } + + offset = list_key->entry_data.offset_to_next; + + MMDB_entry_data_list_s *list_value = data_pool_alloc(pool); + if (!list_value) { + return MMDB_OUT_OF_MEMORY_ERROR; + } + + status = + get_entry_data_list(mmdb, offset, list_value, pool, depth); + if (MMDB_SUCCESS != status) { + DEBUG_MSG("get_entry_data_list on map element failed."); + return status; + } + offset = list_value->entry_data.offset_to_next; + } + entry_data_list->entry_data.offset_to_next = offset; + } break; + default: + break; + } + + return MMDB_SUCCESS; +} + +static float get_ieee754_float(const uint8_t *restrict p) { + volatile float f; + uint8_t *q = (void *)&f; +/* Windows builds don't use autoconf but we can assume they're all + * little-endian. */ +#if MMDB_LITTLE_ENDIAN || _WIN32 + q[3] = p[0]; + q[2] = p[1]; + q[1] = p[2]; + q[0] = p[3]; +#else + memcpy(q, p, 4); +#endif + return f; +} + +static double get_ieee754_double(const uint8_t *restrict p) { + volatile double d; + uint8_t *q = (void *)&d; +#if MMDB_LITTLE_ENDIAN || _WIN32 + q[7] = p[0]; + q[6] = p[1]; + q[5] = p[2]; + q[4] = p[3]; + q[3] = p[4]; + q[2] = p[5]; + q[1] = p[6]; + q[0] = p[7]; +#else + memcpy(q, p, 8); +#endif + + return d; +} + +static uint32_t get_uint32(const uint8_t *p) { + return p[0] * 16777216U + p[1] * 65536 + p[2] * 256 + p[3]; +} + +static uint32_t get_uint24(const uint8_t *p) { + return p[0] * 65536U + p[1] * 256 + p[2]; +} + +static uint32_t get_uint16(const uint8_t *p) { return p[0] * 256U + p[1]; } + +static uint64_t get_uintX(const uint8_t *p, int length) { + uint64_t value = 0; + while (length-- > 0) { + value <<= 8; + value += *p++; + } + return value; +} + +static int32_t get_sintX(const uint8_t *p, int length) { + return (int32_t)get_uintX(p, length); +} + +void MMDB_free_entry_data_list(MMDB_entry_data_list_s *const entry_data_list) { + if (entry_data_list == NULL) { + return; + } + data_pool_destroy(entry_data_list->pool); +} + +void MMDB_close(MMDB_s *const mmdb) { free_mmdb_struct(mmdb); } + +static void free_mmdb_struct(MMDB_s *const mmdb) { + if (!mmdb) { + return; + } + + if (NULL != mmdb->filename) { + FREE_AND_SET_NULL(mmdb->filename); + } + if (NULL != mmdb->file_content) { +#ifdef _WIN32 + UnmapViewOfFile(mmdb->file_content); + /* Winsock is only initialized if open was successful so we only have + * to cleanup then. */ + WSACleanup(); +#else + munmap((void *)mmdb->file_content, mmdb->file_size); +#endif + } + + if (NULL != mmdb->metadata.database_type) { + FREE_AND_SET_NULL(mmdb->metadata.database_type); + } + + free_languages_metadata(mmdb); + free_descriptions_metadata(mmdb); +} + +static void free_languages_metadata(MMDB_s *mmdb) { + if (!mmdb->metadata.languages.names) { + return; + } + + for (size_t i = 0; i < mmdb->metadata.languages.count; i++) { + FREE_AND_SET_NULL(mmdb->metadata.languages.names[i]); + } + FREE_AND_SET_NULL(mmdb->metadata.languages.names); +} + +static void free_descriptions_metadata(MMDB_s *mmdb) { + if (!mmdb->metadata.description.count) { + return; + } + + for (size_t i = 0; i < mmdb->metadata.description.count; i++) { + if (NULL != mmdb->metadata.description.descriptions[i]) { + if (NULL != 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_AND_SET_NULL( + mmdb->metadata.description.descriptions[i]->description); + } + FREE_AND_SET_NULL(mmdb->metadata.description.descriptions[i]); + } + } + + FREE_AND_SET_NULL(mmdb->metadata.description.descriptions); +} + +const char *MMDB_lib_version(void) { return PACKAGE_VERSION; } + +int MMDB_dump_entry_data_list(FILE *const stream, + MMDB_entry_data_list_s *const entry_data_list, + int indent) { + int status; + dump_entry_data_list(stream, entry_data_list, indent, &status); + return status; +} + +static MMDB_entry_data_list_s * +dump_entry_data_list(FILE *stream, + MMDB_entry_data_list_s *entry_data_list, + int indent, + int *status) { + switch (entry_data_list->entry_data.type) { + case MMDB_DATA_TYPE_MAP: { + uint32_t size = entry_data_list->entry_data.data_size; + + print_indentation(stream, indent); + fprintf(stream, "{\n"); + indent += 2; + + for (entry_data_list = entry_data_list->next; + size && entry_data_list; + size--) { + + if (MMDB_DATA_TYPE_UTF8_STRING != + entry_data_list->entry_data.type) { + *status = MMDB_INVALID_DATA_ERROR; + return NULL; + } + char *key = mmdb_strndup( + (char *)entry_data_list->entry_data.utf8_string, + entry_data_list->entry_data.data_size); + if (NULL == key) { + *status = MMDB_OUT_OF_MEMORY_ERROR; + return NULL; + } + + print_indentation(stream, indent); + fprintf(stream, "\"%s\": \n", key); + free(key); + + entry_data_list = entry_data_list->next; + entry_data_list = dump_entry_data_list( + stream, entry_data_list, indent + 2, status); + + if (MMDB_SUCCESS != *status) { + return NULL; + } + } + + indent -= 2; + print_indentation(stream, indent); + fprintf(stream, "}\n"); + } break; + case MMDB_DATA_TYPE_ARRAY: { + uint32_t size = entry_data_list->entry_data.data_size; + + print_indentation(stream, indent); + fprintf(stream, "[\n"); + indent += 2; + + for (entry_data_list = entry_data_list->next; + size && entry_data_list; + size--) { + entry_data_list = dump_entry_data_list( + stream, entry_data_list, indent, status); + if (MMDB_SUCCESS != *status) { + return NULL; + } + } + + indent -= 2; + print_indentation(stream, indent); + fprintf(stream, "]\n"); + } break; + case MMDB_DATA_TYPE_UTF8_STRING: { + char *string = + mmdb_strndup((char *)entry_data_list->entry_data.utf8_string, + entry_data_list->entry_data.data_size); + if (NULL == string) { + *status = MMDB_OUT_OF_MEMORY_ERROR; + return NULL; + } + print_indentation(stream, indent); + fprintf(stream, "\"%s\" \n", string); + free(string); + entry_data_list = entry_data_list->next; + } break; + case MMDB_DATA_TYPE_BYTES: { + char *hex_string = + bytes_to_hex((uint8_t *)entry_data_list->entry_data.bytes, + entry_data_list->entry_data.data_size); + + if (NULL == hex_string) { + *status = MMDB_OUT_OF_MEMORY_ERROR; + return NULL; + } + + print_indentation(stream, indent); + fprintf(stream, "%s \n", hex_string); + free(hex_string); + + entry_data_list = entry_data_list->next; + } break; + case MMDB_DATA_TYPE_DOUBLE: + print_indentation(stream, indent); + fprintf(stream, + "%f \n", + entry_data_list->entry_data.double_value); + entry_data_list = entry_data_list->next; + break; + case MMDB_DATA_TYPE_FLOAT: + print_indentation(stream, indent); + fprintf(stream, + "%f \n", + entry_data_list->entry_data.float_value); + entry_data_list = entry_data_list->next; + break; + case MMDB_DATA_TYPE_UINT16: + print_indentation(stream, indent); + fprintf( + stream, "%u \n", entry_data_list->entry_data.uint16); + entry_data_list = entry_data_list->next; + break; + case MMDB_DATA_TYPE_UINT32: + print_indentation(stream, indent); + fprintf( + stream, "%u \n", entry_data_list->entry_data.uint32); + entry_data_list = entry_data_list->next; + break; + case MMDB_DATA_TYPE_BOOLEAN: + print_indentation(stream, indent); + fprintf(stream, + "%s \n", + entry_data_list->entry_data.boolean ? "true" : "false"); + entry_data_list = entry_data_list->next; + break; + case MMDB_DATA_TYPE_UINT64: + print_indentation(stream, indent); + fprintf(stream, + "%" PRIu64 " \n", + entry_data_list->entry_data.uint64); + entry_data_list = entry_data_list->next; + break; + case MMDB_DATA_TYPE_UINT128: + print_indentation(stream, indent); +#if MMDB_UINT128_IS_BYTE_ARRAY + char *hex_string = bytes_to_hex( + (uint8_t *)entry_data_list->entry_data.uint128, 16); + if (NULL == hex_string) { + *status = MMDB_OUT_OF_MEMORY_ERROR; + return NULL; + } + fprintf(stream, "0x%s \n", hex_string); + free(hex_string); +#else + uint64_t high = entry_data_list->entry_data.uint128 >> 64; + uint64_t low = (uint64_t)entry_data_list->entry_data.uint128; + fprintf(stream, + "0x%016" PRIX64 "%016" PRIX64 " \n", + high, + low); +#endif + entry_data_list = entry_data_list->next; + break; + case MMDB_DATA_TYPE_INT32: + print_indentation(stream, indent); + fprintf(stream, "%d \n", entry_data_list->entry_data.int32); + entry_data_list = entry_data_list->next; + break; + default: + *status = MMDB_INVALID_DATA_ERROR; + return NULL; + } + + *status = MMDB_SUCCESS; + return entry_data_list; +} + +static void print_indentation(FILE *stream, int i) { + char buffer[1024]; + int size = i >= 1024 ? 1023 : i; + memset(buffer, 32, size); + buffer[size] = '\0'; + fputs(buffer, stream); +} + +static char *bytes_to_hex(uint8_t *bytes, uint32_t size) { + char *hex_string; + MAYBE_CHECK_SIZE_OVERFLOW(size, SIZE_MAX / 2 - 1, NULL); + + hex_string = calloc((size * 2) + 1, sizeof(char)); + if (NULL == hex_string) { + return NULL; + } + + for (uint32_t i = 0; i < size; i++) { + sprintf(hex_string + (2 * i), "%02X", bytes[i]); + } + + return hex_string; +} + +const char *MMDB_strerror(int error_code) { + switch (error_code) { + case MMDB_SUCCESS: + return "Success (not an error)"; + case MMDB_FILE_OPEN_ERROR: + return "Error opening the specified MaxMind DB file"; + case MMDB_CORRUPT_SEARCH_TREE_ERROR: + return "The MaxMind DB file's search tree is corrupt"; + case MMDB_INVALID_METADATA_ERROR: + return "The MaxMind DB file contains invalid metadata"; + case MMDB_IO_ERROR: + return "An attempt to read data from the MaxMind DB file failed"; + case MMDB_OUT_OF_MEMORY_ERROR: + return "A memory allocation call failed"; + case MMDB_UNKNOWN_DATABASE_FORMAT_ERROR: + return "The MaxMind DB file is in a format this library can't " + "handle (unknown record size or binary format version)"; + case MMDB_INVALID_DATA_ERROR: + return "The MaxMind DB file's data section contains bad data " + "(unknown data type or corrupt data)"; + case MMDB_INVALID_LOOKUP_PATH_ERROR: + return "The lookup path contained an invalid value (like a " + "negative integer for an array index)"; + case MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR: + return "The lookup path does not match the data (key that doesn't " + "exist, array index bigger than the array, expected array " + "or map where none exists)"; + case MMDB_INVALID_NODE_NUMBER_ERROR: + return "The MMDB_read_node function was called with a node number " + "that does not exist in the search tree"; + case MMDB_IPV6_LOOKUP_IN_IPV4_DATABASE_ERROR: + return "You attempted to look up an IPv6 address in an IPv4-only " + "database"; + default: + return "Unknown error code"; + } +} diff --git a/extensions/geoip/maxminddb.h b/extensions/geoip/maxminddb.h new file mode 100644 index 00000000..0e4c096d --- /dev/null +++ b/extensions/geoip/maxminddb.h @@ -0,0 +1,258 @@ +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef MAXMINDDB_H +#define MAXMINDDB_H + +/* Request POSIX.1-2008. However, we want to remain compatible with + * POSIX.1-2001 (since we have been historically and see no reason to drop + * compatibility). By requesting POSIX.1-2008, we can conditionally use + * features provided by that standard if the implementation provides it. We can + * check for what the implementation provides by checking the _POSIX_VERSION + * macro after including unistd.h. If a feature is in POSIX.1-2008 but not + * POSIX.1-2001, check that macro before using the feature (or check for the + * feature directly if possible). */ +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L +#endif + +#include "maxminddb_config.h" +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#include +#include +/* libmaxminddb package version from configure */ +#define PACKAGE_VERSION "1.5.2" + +typedef ADDRESS_FAMILY sa_family_t; + +#if defined(_MSC_VER) +/* MSVC doesn't define signed size_t, copy it from configure */ +#define ssize_t SSIZE_T + +/* MSVC doesn't support restricted pointers */ +#define restrict +#endif +#else +#include +#include +#include +#endif + +#define MMDB_DATA_TYPE_EXTENDED (0) +#define MMDB_DATA_TYPE_POINTER (1) +#define MMDB_DATA_TYPE_UTF8_STRING (2) +#define MMDB_DATA_TYPE_DOUBLE (3) +#define MMDB_DATA_TYPE_BYTES (4) +#define MMDB_DATA_TYPE_UINT16 (5) +#define MMDB_DATA_TYPE_UINT32 (6) +#define MMDB_DATA_TYPE_MAP (7) +#define MMDB_DATA_TYPE_INT32 (8) +#define MMDB_DATA_TYPE_UINT64 (9) +#define MMDB_DATA_TYPE_UINT128 (10) +#define MMDB_DATA_TYPE_ARRAY (11) +#define MMDB_DATA_TYPE_CONTAINER (12) +#define MMDB_DATA_TYPE_END_MARKER (13) +#define MMDB_DATA_TYPE_BOOLEAN (14) +#define MMDB_DATA_TYPE_FLOAT (15) + +#define MMDB_RECORD_TYPE_SEARCH_NODE (0) +#define MMDB_RECORD_TYPE_EMPTY (1) +#define MMDB_RECORD_TYPE_DATA (2) +#define MMDB_RECORD_TYPE_INVALID (3) + +/* flags for open */ +#define MMDB_MODE_MMAP (1) +#define MMDB_MODE_MASK (7) + +/* error codes */ +#define MMDB_SUCCESS (0) +#define MMDB_FILE_OPEN_ERROR (1) +#define MMDB_CORRUPT_SEARCH_TREE_ERROR (2) +#define MMDB_INVALID_METADATA_ERROR (3) +#define MMDB_IO_ERROR (4) +#define MMDB_OUT_OF_MEMORY_ERROR (5) +#define MMDB_UNKNOWN_DATABASE_FORMAT_ERROR (6) +#define MMDB_INVALID_DATA_ERROR (7) +#define MMDB_INVALID_LOOKUP_PATH_ERROR (8) +#define MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR (9) +#define MMDB_INVALID_NODE_NUMBER_ERROR (10) +#define MMDB_IPV6_LOOKUP_IN_IPV4_DATABASE_ERROR (11) + +#if !(MMDB_UINT128_IS_BYTE_ARRAY) +#if MMDB_UINT128_USING_MODE +typedef unsigned int mmdb_uint128_t __attribute__((__mode__(TI))); +#else +typedef unsigned __int128 mmdb_uint128_t; +#endif +#endif + +/* This is a pointer into the data section for a given IP address lookup */ +typedef struct MMDB_entry_s { + const struct MMDB_s *mmdb; + uint32_t offset; +} MMDB_entry_s; + +typedef struct MMDB_lookup_result_s { + bool found_entry; + MMDB_entry_s entry; + uint16_t netmask; +} MMDB_lookup_result_s; + +typedef struct MMDB_entry_data_s { + bool has_data; + union { + uint32_t pointer; + const char *utf8_string; + double double_value; + const uint8_t *bytes; + uint16_t uint16; + uint32_t uint32; + int32_t int32; + uint64_t uint64; +#if MMDB_UINT128_IS_BYTE_ARRAY + uint8_t uint128[16]; +#else + mmdb_uint128_t uint128; +#endif + bool boolean; + float float_value; + }; + /* This is a 0 if a given entry cannot be found. This can only happen + * when a call to MMDB_(v)get_value() asks for hash keys or array + * indices that don't exist. */ + uint32_t offset; + /* This is the next entry in the data section, but it's really only + * relevant for entries that part of a larger map or array + * struct. There's no good reason for an end user to look at this + * directly. */ + uint32_t offset_to_next; + /* This is only valid for strings, utf8_strings or binary data */ + uint32_t data_size; + /* This is an MMDB_DATA_TYPE_* constant */ + uint32_t type; +} MMDB_entry_data_s; + +/* This is the return type when someone asks for all the entry data in a map or + * array */ +typedef struct MMDB_entry_data_list_s { + MMDB_entry_data_s entry_data; + struct MMDB_entry_data_list_s *next; + void *pool; +} MMDB_entry_data_list_s; + +typedef struct MMDB_description_s { + const char *language; + const char *description; +} MMDB_description_s; + +/* WARNING: do not add new fields to this struct without bumping the SONAME. + * The struct is allocated by the users of this library and increasing the + * size will cause existing users to allocate too little space when the shared + * library is upgraded */ +typedef struct MMDB_metadata_s { + uint32_t node_count; + uint16_t record_size; + uint16_t ip_version; + const char *database_type; + struct { + size_t count; + const char **names; + } languages; + uint16_t binary_format_major_version; + uint16_t binary_format_minor_version; + uint64_t build_epoch; + struct { + size_t count; + MMDB_description_s **descriptions; + } description; + /* See above warning before adding fields */ +} MMDB_metadata_s; + +/* WARNING: do not add new fields to this struct without bumping the SONAME. + * The struct is allocated by the users of this library and increasing the + * size will cause existing users to allocate too little space when the shared + * library is upgraded */ +typedef struct MMDB_ipv4_start_node_s { + uint16_t netmask; + uint32_t node_value; + /* See above warning before adding fields */ +} MMDB_ipv4_start_node_s; + +/* WARNING: do not add new fields to this struct without bumping the SONAME. + * The struct is allocated by the users of this library and increasing the + * size will cause existing users to allocate too little space when the shared + * library is upgraded */ +typedef struct MMDB_s { + uint32_t flags; + const char *filename; + ssize_t file_size; + const uint8_t *file_content; + const uint8_t *data_section; + uint32_t data_section_size; + const uint8_t *metadata_section; + uint32_t metadata_section_size; + uint16_t full_record_byte_size; + uint16_t depth; + MMDB_ipv4_start_node_s ipv4_start_node; + MMDB_metadata_s metadata; + /* See above warning before adding fields */ +} MMDB_s; + +typedef struct MMDB_search_node_s { + uint64_t left_record; + uint64_t right_record; + uint8_t left_record_type; + uint8_t right_record_type; + MMDB_entry_s left_record_entry; + MMDB_entry_s right_record_entry; +} MMDB_search_node_s; + +extern int +MMDB_open(const char *const filename, uint32_t flags, MMDB_s *const mmdb); +extern MMDB_lookup_result_s MMDB_lookup_string(const MMDB_s *const mmdb, + const char *const ipstr, + int *const gai_error, + int *const mmdb_error); +extern MMDB_lookup_result_s +MMDB_lookup_sockaddr(const MMDB_s *const mmdb, + const struct sockaddr *const sockaddr, + int *const mmdb_error); +extern int MMDB_read_node(const MMDB_s *const mmdb, + uint32_t node_number, + MMDB_search_node_s *const node); +extern int MMDB_get_value(MMDB_entry_s *const start, + MMDB_entry_data_s *const entry_data, + ...); +extern int MMDB_vget_value(MMDB_entry_s *const start, + MMDB_entry_data_s *const entry_data, + va_list va_path); +extern int MMDB_aget_value(MMDB_entry_s *const start, + MMDB_entry_data_s *const entry_data, + const char *const *const path); +extern int MMDB_get_metadata_as_entry_data_list( + const MMDB_s *const mmdb, MMDB_entry_data_list_s **const entry_data_list); +extern int +MMDB_get_entry_data_list(MMDB_entry_s *start, + MMDB_entry_data_list_s **const entry_data_list); +extern void +MMDB_free_entry_data_list(MMDB_entry_data_list_s *const entry_data_list); +extern void MMDB_close(MMDB_s *const mmdb); +extern const char *MMDB_lib_version(void); +extern int +MMDB_dump_entry_data_list(FILE *const stream, + MMDB_entry_data_list_s *const entry_data_list, + int indent); +extern const char *MMDB_strerror(int error_code); + +#endif /* MAXMINDDB_H */ + +#ifdef __cplusplus +} +#endif diff --git a/extensions/geoip/maxminddb_config.h b/extensions/geoip/maxminddb_config.h new file mode 100644 index 00000000..9ce1b309 --- /dev/null +++ b/extensions/geoip/maxminddb_config.h @@ -0,0 +1,16 @@ +#ifndef MAXMINDDB_CONFIG_H +#define MAXMINDDB_CONFIG_H + +#include "osdefs.h" // BYTE_ORDER, LITTLE_ENDIAN + +/* This fixes a behavior change in after https://github.com/maxmind/libmaxminddb/pull/123. */ +#if defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN + #define MMDB_LITTLE_ENDIAN 1 +#endif + +#define MMDB_UINT128_USING_MODE 0 +#define MMDB_UINT128_IS_BYTE_ARRAY 1 + +#define PACKAGE_VERSION "1.5.2" + +#endif /* MAXMINDDB_CONFIG_H */ diff --git a/extensions/geoip/osdefs.h b/extensions/geoip/osdefs.h new file mode 100644 index 00000000..6a85f600 --- /dev/null +++ b/extensions/geoip/osdefs.h @@ -0,0 +1,64 @@ +/* __MSDOS__ set when compiling for DOS (not Windows) + * _Windows set when compiling for any version of Microsoft Windows + * __WIN32__ set when compiling for Windows95 or WindowsNT (32 bit mode) + * __32BIT__ set when compiling in 32-bit "flat" mode (DOS or Windows) + * + * Copyright 1998-2002, ITB CompuPhase, The Netherlands. + * info@compuphase.com. + */ + +#ifndef _OSDEFS_H +#define _OSDEFS_H + +/* Every compiler uses different "default" macros to indicate the mode + * it is in. Throughout the source, we use the Borland C++ macros, so + * the macros of Watcom C/C++ and Microsoft Visual C/C++ are mapped to + * those of Borland C++. + */ +#if defined(__WATCOMC__) +# if defined(__WINDOWS__) || defined(__NT__) +# define _Windows 1 +# endif +# ifdef __386__ +# define __32BIT__ 1 +# endif +# if defined(_Windows) && defined(__32BIT__) +# define __WIN32__ 1 +# endif +#elif defined(_MSC_VER) +# if defined(_WINDOWS) || defined(_WIN32) +# define _Windows 1 +# endif +# ifdef _WIN32 +# define __WIN32__ 1 +# define __32BIT__ 1 +# endif +#endif + +#if defined __linux__ + #include +#endif + +#if defined __APPLE__ + #include +#endif + +/* Linux NOW has these */ +#if !defined BIG_ENDIAN + #define BIG_ENDIAN 4321 +#endif +#if !defined LITTLE_ENDIAN + #define LITTLE_ENDIAN 1234 +#endif + +/* educated guess, BYTE_ORDER is undefined, i386 is common => little endian */ +#if !defined BYTE_ORDER + #if defined UCLINUX + #define BYTE_ORDER BIG_ENDIAN + #else + #define BYTE_ORDER LITTLE_ENDIAN + #endif +#endif + +#endif /* _OSDEFS_H */ + diff --git a/extensions/geoip/smsdk_config.h b/extensions/geoip/smsdk_config.h index fa147344..0c890b59 100644 --- a/extensions/geoip/smsdk_config.h +++ b/extensions/geoip/smsdk_config.h @@ -61,8 +61,10 @@ /** Enable interfaces you want to use here by uncommenting lines */ //#define SMEXT_ENABLE_FORWARDSYS //#define SMEXT_ENABLE_HANDLESYS -//#define SMEXT_ENABLE_PLAYERHELPERS +#define SMEXT_ENABLE_PLAYERHELPERS //#define SMEXT_ENABLE_DBMANAGER //#define SMEXT_ENABLE_GAMECONF +#define SMEXT_ENABLE_LIBSYS +#define SMEXT_ENABLE_TRANSLATOR #endif // _INCLUDE_SOURCEMOD_EXTENSION_CONFIG_H_ diff --git a/plugins/include/geoip.inc b/plugins/include/geoip.inc index 13045899..76d17774 100644 --- a/plugins/include/geoip.inc +++ b/plugins/include/geoip.inc @@ -35,6 +35,22 @@ #endif #define _geoip_included +enum Continent +{ + CONTINENT_UNKNOWN = 0, + CONTINENT_AFRICA, + CONTINENT_ANTARCTICA, + CONTINENT_ASIA, + CONTINENT_EUROPE, + CONTINENT_NORTH_AMERICA, + CONTINENT_OCEANIA, + CONTINENT_SOUTH_AMERICA, +}; + +// The system of measurement for calculate the distance between geographical coordinates +#define SYSTEM_METRIC 0 // kilometers +#define SYSTEM_IMPERIAL 1 // statute miles + #include /** @@ -46,7 +62,7 @@ * * @param ip Ip to determine the country code. * @param ccode Destination string buffer to store the code. - * @return True on success, false if no country found. + * @return True on success, false otherwise. */ native bool GeoipCode2(const char[] ip, char ccode[3]); @@ -55,20 +71,133 @@ native bool GeoipCode2(const char[] ip, char ccode[3]); * * @param ip Ip to determine the country code. * @param ccode Destination string buffer to store the code. - * @return True on success, false if no country found. + * @return True on success, false otherwise. */ native bool GeoipCode3(const char[] ip, char ccode[4]); /** - * Gets the full country name. (max length of output string is 45) + * Gets the region code with country code from an IP address. (US-IL, CH-CHE, etc) + * + * @param ip Ip to determine the region code. + * @param ccode Destination string buffer to store the code. + * @return True on success, false otherwise. + */ +native bool GeoipRegionCode(const char[] ip, char ccode[12]); + +/** + * Gets the two character continent code from an IP address. (EU, AS, etc) + * + * @param ip Ip to determine the continent code. + * @param ccode Destination string buffer to store the code. + * @return The continent id on success, 0 otherwise. + */ +native Continent GeoipContinentCode(const char[] ip, char ccode[3]); + +/** + * Gets the full country name. * * @param ip Ip to determine the country code. * @param name Destination string buffer to store the country name. * @param maxlength Maximum length of output string buffer. - * @return True on success, false if no country found. + * @return True on success, false otherwise. */ native bool GeoipCountry(const char[] ip, char[] name, int maxlength); +/** + * Gets the full country name. + * + * @param ip Ip to determine the country code. + * @param name Destination string buffer to store the country name. + * @param maxlength Maximum length of output string buffer. + * @param client Client index in order to return the result in the player's language + * -1: the default language, which is english. + * 0: the server language. You can use LANG_SERVER define. + * >=1: the player's language. + * @return True on success, false otherwise. + */ +native bool GeoipCountryEx(const char[] ip, char[] name, int maxlength, int client = -1); + +/** + * Gets the full continent name. + * + * @param ip Ip to determine the continent code. + * @param name Destination string buffer to store the continent name. + * @param maxlength Maximum length of output string buffer. + * @param client Client index in order to return the result in the player's language + * -1: the default language, which is english. + * 0: the server language. You can use LANG_SERVER define. + * >=1: the player's language. + * @return True on success, false otherwise. + */ +native bool GeoipContinent(const char[] ip, char[] name, int maxlength, int client = -1); + +/** + * Gets the full region name. + * + * @param ip Ip to determine the region code. + * @param name Destination string buffer to store the region name. + * @param maxlength Maximum length of output string buffer. + * @param client Client index in order to return the result in the player's language + * -1: the default language, which is english. + * 0: the server language. You can use LANG_SERVER define. + * >=1: the player's language. + * @return True on success, false otherwise. + */ +native bool GeoipRegion(const char[] ip, char[] name, int maxlength, int client = -1); + +/** + * Gets the city name. + * + * @param ip Ip to determine the city code. + * @param name Destination string buffer to store the city name. + * @param maxlength Maximum length of output string buffer. + * @param client Client index in order to return the result in the player's language + * -1: the default language, which is english. + * 0: the server language. You can use LANG_SERVER define. + * >=1: the player's language. + * @return True on success, false otherwise. + */ +native bool GeoipCity(const char[] ip, char[] name, int maxlength, int client = -1); + +/** + * Gets the timezone. + * + * @param ip Ip to determine the timezone. + * @param name Destination string buffer to store the timezone. + * @param maxlength Maximum length of output string buffer. + * @return True on success, false otherwise. + */ +native bool GeoipTimezone(const char[] ip, char[] name, int maxlength); + +/** + * Gets the city's latitude + * + * @param ip Ip to determine the city latitude. + * @return The result of the latitude, 0 if latitude is not found + */ +native float GeoipLatitude(const char[] ip); + +/** + * Gets the city's longitude + * + * @param ip Ip to determine the city longitude. + * @return The result of the longitude, 0 if longitude is not found + */ +native float GeoipLongitude(const char[] ip); + +/* + * Calculate the distance between geographical coordinates, latitude and longitude. + * + * @param lat1 The first IP latitude. + * @param lon1 The first IP longitude. + * @param lat2 The second IP latitude. + * @param lon2 The second IP longitude. + * @param system The system of measurement, 0 = Metric(kilometers) or 1 = English(miles). + * + * @return The distance as result in specified system of measurement. + */ +native float GeoipDistance(float lat1, float lon1, float lat2, float lon2, int system = SYSTEM_METRIC); + /** * @endsection */ @@ -97,6 +226,16 @@ public void __ext_geoip_SetNTVOptional() { MarkNativeAsOptional("GeoipCode2"); MarkNativeAsOptional("GeoipCode3"); + MarkNativeAsOptional("GeoipRegionCode"); + MarkNativeAsOptional("GeoipContinentCode"); MarkNativeAsOptional("GeoipCountry"); + MarkNativeAsOptional("GeoipCountryEx"); + MarkNativeAsOptional("GeoipContinent"); + MarkNativeAsOptional("GeoipRegion"); + MarkNativeAsOptional("GeoipCity"); + MarkNativeAsOptional("GeoipTimezone"); + MarkNativeAsOptional("GeoipLatitude"); + MarkNativeAsOptional("GeoipLongitude"); + MarkNativeAsOptional("GeoipDistance"); } #endif diff --git a/tools/buildbot/package.pl b/tools/buildbot/package.pl index ab2feba1..f1f99208 100755 --- a/tools/buildbot/package.pl +++ b/tools/buildbot/package.pl @@ -3,11 +3,10 @@ use strict; use Cwd; use File::Basename; +use File::Copy; use File::stat; use File::Temp qw/ tempfile :seekable/; use Net::FTP; -use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; -use Time::localtime; my ($ftp_file, $ftp_host, $ftp_user, $ftp_pass, $ftp_path, $tag); @@ -55,48 +54,19 @@ while (my $ln = <$fh>) { } close($fh); -my $needNewGeoIP = 1; -if (-e '../GeoIP.dat.gz') +unless (-e '../GeoLite2-City_20191217.tar.gz') { - my $stats = stat('../GeoIP.dat.gz'); - if ($stats->size != 0) - { - my $fileModifiedTime = $stats->mtime; - my $fileModifiedMonth = localtime($fileModifiedTime)->mon; - my $currentMonth = localtime->mon; - my $thirtyOneDays = 60 * 60 * 24 * 31; - - # GeoIP file only updates once per month - if ($currentMonth == $fileModifiedMonth || (time() - $fileModifiedTime) < $thirtyOneDays) - { - $needNewGeoIP = 0; - } - } -} - -if ($needNewGeoIP) -{ - print "Downloading GeoIP.dat...\n"; + print "Downloading GeoLite2-City.mmdb...\n"; # Don't check certificate. It will fail on the slaves and we're resolving to internal addressing anyway - system('wget --no-check-certificate -q -O ../GeoIP.dat.gz https://sm.alliedmods.net/GeoIP.dat.gz'); + system('wget --no-check-certificate -q -O ../GeoLite2-City_20191217.tar.gz https://sm.alliedmods.net/GeoLite2-City_20191217.tar.gz'); + system('tar -C ../ -xzf GeoLite2-City_20191217.tar.gz'); + copy('../GeoLite2-City_20191217/GeoLite2-City.mmdb', 'addons/sourcemod/configs/geoip/GeoLite2-City.mmdb'); } else { - print "Reusing existing GeoIP.dat\n"; + print "Reusing existing GeoLite2-City.mmdb\n"; } -my $geoIPfile = 'addons/sourcemod/configs/geoip/GeoIP.dat'; -if (-e $geoIPfile) { - unlink($geoIPfile); -} - -open(my $fh, ">", $geoIPfile) - or die "cannot open $geoIPfile for writing: $!"; -binmode($fh); -gunzip '../GeoIP.dat.gz' => $fh - or die "gunzip failed: $GunzipError\n"; -close($fh); - my ($version); $version = Build::ProductVersion(Build::PathFormat('../../build/product.version'));