From 7dd1ea371975f01b3415ab5b46483986a21fb3b3 Mon Sep 17 00:00:00 2001 From: Arkshine Date: Wed, 30 Jul 2014 09:51:01 +0200 Subject: [PATCH] Geoip: Convert code to libmaxminddb API. --- dlls/geoip/AMBuilder | 8 +- dlls/geoip/GeoIP2/maxminddb.h | 60 ++++++ dlls/geoip/geoip_amxx.cpp | 332 +++++++++++++++++++++++++++----- dlls/geoip/geoip_amxx.h | 8 +- dlls/geoip/msvc10/geoip.vcxproj | 4 +- 5 files changed, 351 insertions(+), 61 deletions(-) diff --git a/dlls/geoip/AMBuilder b/dlls/geoip/AMBuilder index 66ddee96..715a32f2 100644 --- a/dlls/geoip/AMBuilder +++ b/dlls/geoip/AMBuilder @@ -1,10 +1,11 @@ # vim: set sts=2 ts=8 sw=2 tw=99 et ft=python: import os.path -binary = AMXX.Module(builder, 'geoip') +binary = AMXX.MetaModule(builder, 'geoip') -binary.compiler.defines += ['HAVE_STDINT_H'] -binary.compiler.includes += [os.path.join(builder.sourcePath, 'dlls', 'geoip', 'GeoIP2')] +binary.compiler.defines += [ + 'HAVE_STDINT_H' +] binary.sources = [ 'sdk/amxxmodule.cpp', @@ -14,5 +15,6 @@ binary.sources = [ if builder.target_platform == 'windows': binary.compiler.postlink += ['ws2_32.lib'] + binary.compiler.includes += [os.path.join(builder.sourcePath, 'dlls', 'geoip', 'GeoIP2')] AMXX.modules += [builder.Add(binary)] diff --git a/dlls/geoip/GeoIP2/maxminddb.h b/dlls/geoip/GeoIP2/maxminddb.h index 6d7c7f15..3c3f766a 100644 --- a/dlls/geoip/GeoIP2/maxminddb.h +++ b/dlls/geoip/GeoIP2/maxminddb.h @@ -25,6 +25,66 @@ typedef ADDRESS_FAMILY sa_family_t; #include #endif +const char GeoIPCountryCode[][3] = +{ + "AP", "EU", "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AN", + "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AZ", "BA", "BB", + "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BM", "BN", "BO", + "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC", "CD", + "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", + "CU", "CV", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", + "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ", + "FK", "FM", "FO", "FR", "FX", "GA", "GB", "GD", "GE", "GF", + "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", + "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", + "IE", "IL", "IN", "IO", "IQ", "IR", "IS", "IT", "JM", "JO", + "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", + "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", + "LU", "LV", "LY", "MA", "MC", "MD", "MG", "MH", "MK", "ML", + "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", + "MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NF", "NG", "NI", + "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF", + "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", + "PY", "QA", "RE", "RO", "RU", "RW", "SA", "SB", "SC", "SD", + "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", + "SR", "ST", "SV", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", + "TJ", "TK", "TM", "TN", "TO", "TL", "TR", "TT", "TV", "TW", + "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", + "VG", "VI", "VN", "VU", "WF", "WS", "YE", "YT", "RS", "ZA", + "ZM", "ME", "ZW", "A1", "A2", "O1", "AX", "GG", "IM", "JE", + "BL", "MF" +}; + +const char GeoIPCountryCode3[][4] = +{ + "AP", "EU", "AND", "ARE", "AFG", "ATG", "AIA", "ALB", "ARM", "ANT", + "AGO", "AQ", "ARG", "ASM", "AUT", "AUS", "ABW", "AZE", "BIH", "BRB", + "BGD", "BEL", "BFA", "BGR", "BHR", "BDI", "BEN", "BMU", "BRN", "BOL", + "BRA", "BHS", "BTN", "BV", "BWA", "BLR", "BLZ", "CAN", "CC", "COD", + "CAF", "COG", "CHE", "CIV", "COK", "CHL", "CMR", "CHN", "COL", "CRI", + "CUB", "CPV", "CX", "CYP", "CZE", "DEU", "DJI", "DNK", "DMA", "DOM", + "DZA", "ECU", "EST", "EGY", "ESH", "ERI", "ESP", "ETH", "FIN", "FJI", + "FLK", "FSM", "FRO", "FRA", "FX", "GAB", "GBR", "GRD", "GEO", "GUF", + "GHA", "GIB", "GRL", "GMB", "GIN", "GLP", "GNQ", "GRC", "GS", "GTM", + "GUM", "GNB", "GUY", "HKG", "HM", "HND", "HRV", "HTI", "HUN", "IDN", + "IRL", "ISR", "IND", "IO", "IRQ", "IRN", "ISL", "ITA", "JAM", "JOR", + "JPN", "KEN", "KGZ", "KHM", "KIR", "COM", "KNA", "PRK", "KOR", "KWT", + "CYM", "KAZ", "LAO", "LBN", "LCA", "LIE", "LKA", "LBR", "LSO", "LTU", + "LUX", "LVA", "LBY", "MAR", "MCO", "MDA", "MDG", "MHL", "MKD", "MLI", + "MMR", "MNG", "MAC", "MNP", "MTQ", "MRT", "MSR", "MLT", "MUS", "MDV", + "MWI", "MEX", "MYS", "MOZ", "NAM", "NCL", "NER", "NFK", "NGA", "NIC", + "NLD", "NOR", "NPL", "NRU", "NIU", "NZL", "OMN", "PAN", "PER", "PYF", + "PNG", "PHL", "PAK", "POL", "SPM", "PCN", "PRI", "PSE", "PRT", "PLW", + "PRY", "QAT", "REU", "ROU", "RUS", "RWA", "SAU", "SLB", "SYC", "SDN", + "SWE", "SGP", "SHN", "SVN", "SJM", "SVK", "SLE", "SMR", "SEN", "SOM", + "SUR", "STP", "SLV", "SYR", "SWZ", "TCA", "TCD", "TF", "TGO", "THA", + "TJK", "TKL", "TKM", "TUN", "TON", "TLS", "TUR", "TTO", "TUV", "TWN", + "TZA", "UKR", "UGA", "UM", "USA", "URY", "UZB", "VAT", "VCT", "VEN", + "VGB", "VIR", "VNM", "VUT", "WLF", "WSM", "YEM", "YT", "SRB", "ZAF", + "ZMB", "MNE", "ZWE", "A1", "A2", "O1", "ALA", "GGY", "IMN", "JEY", + "BLM", "MAF" +}; + #define MMDB_DATA_TYPE_EXTENDED (0) #define MMDB_DATA_TYPE_POINTER (1) #define MMDB_DATA_TYPE_UTF8_STRING (2) diff --git a/dlls/geoip/geoip_amxx.cpp b/dlls/geoip/geoip_amxx.cpp index 138f4586..16c7e56c 100755 --- a/dlls/geoip/geoip_amxx.cpp +++ b/dlls/geoip/geoip_amxx.cpp @@ -1,3 +1,4 @@ +<<<<<<< HEAD // vim: set ts=4 sw=4 tw=99 noet: // // AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO"). @@ -13,103 +14,328 @@ #include "geoip_amxx.h" -GeoIP *gi = NULL; +MMDB_s HandleDB; -inline void StripPort(char *ip) +char *stripPort(char *ip_port) { - char *tmp = strchr(ip, ':'); + char *tmp = strchr(ip_port, ':'); + if (!tmp) - return; + { + return ip_port; + } + *tmp = '\0'; + + return tmp; } +const char *lookupByIp(const char *ip, const char **path, int *length = NULL) +{ + int gai_error = 0, mmdb_error = 0; + MMDB_lookup_result_s lookup = MMDB_lookup_string(&HandleDB, ip, &gai_error, &mmdb_error); + + if (gai_error != 0 || MMDB_SUCCESS != mmdb_error || !lookup.found_entry) + { + return NULL; + } + + MMDB_entry_data_s entry_data; + MMDB_aget_value(&lookup.entry, &entry_data, path); + + if (!entry_data.has_data) + { + return NULL; + } + + if (length) + { + *length = entry_data.data_size; + } + + return entry_data.utf8_string; +} + +// native geoip_code2(const ip[], ccode[3]); +// Deprecated. static cell AMX_NATIVE_CALL amx_geoip_code2(AMX *amx, cell *params) { - int len = 0; - char *ip = MF_GetAmxString(amx, params[1], 0, &len); - StripPort(ip); - const char *ccode = GeoIP_country_code_by_addr(gi, ip); - return MF_SetAmxString(amx, params[2], ccode?ccode:"error", 3); + int length; + char *ip = MF_GetAmxString(amx, params[1], 0, &length); + + const char *path[] = { "country", "iso_code", NULL }; + const char *code = lookupByIp(stripPort(ip), path); + + return MF_SetAmxString(amx, params[2], code ? code : "error", 3); } +// native geoip_code3(const ip[], result[4]); +// Deprecated. static cell AMX_NATIVE_CALL amx_geoip_code3(AMX *amx, cell *params) { - int len = 0; - char *ip = MF_GetAmxString(amx, params[1], 0, &len); - StripPort(ip); - const char *ccode = GeoIP_country_code3_by_addr(gi, ip); - return MF_SetAmxString(amx, params[2], ccode?ccode:"error", 4); + int length; + char *ip = MF_GetAmxString(amx, params[1], 0, &length); + + const char *path[] = { "country", "iso_code", NULL }; + const char *code = lookupByIp(stripPort(ip), path); + + for (size_t i = 0; i < ARRAYSIZE(GeoIPCountryCode); ++i) + { + if (!strncmp(code, GeoIPCountryCode[i], 2)) + { + code = GeoIPCountryCode3[i]; + break; + } + } + + return MF_SetAmxString(amx, params[2], code ? code : "error", 4); } + +// native bool:geoip_code2_ex(const ip[], result[3]); static cell AMX_NATIVE_CALL amx_geoip_code2_ex(AMX *amx, cell *params) { - int len = 0; - char *ip = MF_GetAmxString(amx, params[1], 0, &len); - StripPort(ip); - const char *ccode = GeoIP_country_code_by_addr(gi, ip); - - if (ccode == NULL) + int length; + char *ip = MF_GetAmxString(amx, params[1], 0, &length); + + const char *path[] = { "country", "iso_code", NULL }; + const char *code = lookupByIp(stripPort(ip), path); + + if (!code) { return 0; } - - MF_SetAmxString(amx, params[2], ccode, 2); - + + MF_SetAmxString(amx, params[2], code, 2); + return 1; } +// native bool:geoip_code3_ex(const ip[], result[4]); static cell AMX_NATIVE_CALL amx_geoip_code3_ex(AMX *amx, cell *params) { - int len = 0; - char *ip = MF_GetAmxString(amx, params[1], 0, &len); - StripPort(ip); - const char *ccode = GeoIP_country_code3_by_addr(gi, ip); + int length; + char *ip = MF_GetAmxString(amx, params[1], 0, &length); - if (ccode == NULL) + const char *path[] = { "country", "iso_code", NULL }; + const char *code = lookupByIp(stripPort(ip), path, &length); + + if (!code) { return 0; } - - MF_SetAmxString(amx, params[2], ccode, 3); - + + for (size_t i = 0; i < ARRAYSIZE(GeoIPCountryCode); ++i) + { + if (!strncmp(code, GeoIPCountryCode[i], 2)) + { + code = GeoIPCountryCode3[i]; + break; + } + } + + MF_SetAmxString(amx, params[2], code, 3); + return 1; } +// native geoip_country(const ip[], result[], len); static cell AMX_NATIVE_CALL amx_geoip_country(AMX *amx, cell *params) { - int len = 0; - char *ip = MF_GetAmxString(amx, params[1], 0, &len); - StripPort(ip); - const char *ccode = GeoIP_country_name_by_addr(gi, ip); - return MF_SetAmxString(amx, params[2], ccode?ccode:"error", params[3]); + int length; + char *ip = MF_GetAmxString(amx, params[1], 0, &length); + + const char *path[] = { "country", "names", "en", NULL }; + const char *country = lookupByIp(stripPort(ip), path, &length); + + return MF_SetAmxString(amx, params[2], country, length >= params[3] ? params[3] : length); // TODO: make this utf8 safe. } + void OnAmxxAttach() { - char *path = MF_BuildPathname("%s/GeoIP.dat",MF_GetLocalInfo("amxx_datadir", "addons/amxmodx/data")); - gi = GeoIP_open(path, GEOIP_MEMORY_CACHE); - if (gi == NULL) { - MF_Log("Failed to instantiate GeoIP!"); + const char *databases[] = + { + "City", + "Country" // is the default shipped database with AMXX. + }; + + const char *modName = MF_GetModname(); + const char *dataDir = MF_GetLocalInfo("amxx_datadir", "addons/amxmodx/data"); + + char file[255]; + int status = -1; + + for (size_t i = 0; i < ARRAYSIZE(databases); ++i) + { + sprintf(file, "%s/%s/GeoLite2-%s.mmdb", modName, dataDir, databases[i]); // MF_BuildPathname not used because backslash + // makes CreateFileMapping failing under windows. + status = MMDB_open(file, MMDB_MODE_MMAP, &HandleDB); + + if (status == MMDB_SUCCESS) + { + break; + } + else if (status != MMDB_FILE_OPEN_ERROR) + { + MF_Log("Could not open %s - %s", file, MMDB_strerror(status)); + + if (status == MMDB_IO_ERROR) + { + MF_Log(" IO error: %s", strerror(errno)); + } + } + } + + if (status != MMDB_SUCCESS) + { + MF_Log("Could not find GeoIP2 databases. Disabled natives."); return; } - char *db_info = GeoIP_database_info(gi); - MF_Log("Database info: %s", db_info); + + MF_Log("Database info: %s %i.%i", + HandleDB.metadata.description.descriptions[0]->description, + HandleDB.metadata.binary_format_major_version, + HandleDB.metadata.binary_format_minor_version); + MF_AddNatives(geoip_natives); } void OnAmxxDetach() { - GeoIP_delete(gi); - gi = NULL; + MMDB_close(&HandleDB); } -AMX_NATIVE_INFO geoip_natives[] = { - {"geoip_code2", amx_geoip_code2}, - {"geoip_code3", amx_geoip_code3}, - - {"geoip_code2_ex", amx_geoip_code2_ex}, - {"geoip_code3_ex", amx_geoip_code3_ex}, - - {"geoip_country", amx_geoip_country}, - {NULL, NULL}, + +AMX_NATIVE_INFO geoip_natives[] = +{ + { "geoip_code2", amx_geoip_code2 }, + { "geoip_code3", amx_geoip_code3 }, + + { "geoip_code2_ex", amx_geoip_code2_ex }, + { "geoip_code3_ex", amx_geoip_code3_ex }, + + { "geoip_country", amx_geoip_country }, + { NULL, NULL }, }; + +/** +* GEOIP2 DATA EXAMPLE: +* +* { +* "city": { +* "confidence": 25, +* "geoname_id": 54321, +* "names": { +* "de": "Los Angeles", +* "en": "Los Angeles", +* "es": "Los Ángeles", +* "fr": "Los Angeles", +* "ja": "ロサンゼルス市", +* "pt-BR": "Los Angeles", +* "ru": "Лос-Анджелес", +* "zh-CN": "洛杉矶" +* } +* }, +* "continent": { +* "code": "NA", +* "geoname_id": 123456, +* "names": { +* "de": "Nordamerika", +* "en": "North America", +* "es": "América del Norte", +* "fr": "Amérique du Nord", +* "ja": "北アメリカ", +* "pt-BR": "América do Norte", +* "ru": "Северная Америка", +* "zh-CN": "北美洲" +* +* } +* }, +* "country": { +* "confidence": 75, +* "geoname_id": "6252001", +* "iso_code": "US", +* "names": { +* "de": "USA", +* "en": "United States", +* "es": "Estados Unidos", +* "fr": "États-Unis", +* "ja": "アメリカ合衆国", +* "pt-BR": "Estados Unidos", +* "ru": "США", +* "zh-CN": "美国" +* } +* }, +* "location": { +* "accuracy_radius": 20, +* "latitude": 37.6293, +* "longitude": -122.1163, +* "metro_code": 807, +* "time_zone": "America/Los_Angeles" +* }, +* "postal": { +* "code": "90001", +* "confidence": 10 +* }, +* "registered_country": { +* "geoname_id": "6252001", +* "iso_code": "US", +* "names": { +* "de": "USA", +* "en": "United States", +* "es": "Estados Unidos", +* "fr": "États-Unis", +* "ja": "アメリカ合衆国", +* "pt-BR": "Estados Unidos", +* "ru": "США", +* "zh-CN": "美国" +* } +* }, +* "represented_country": { +* "geoname_id": "6252001", +* "iso_code": "US", +* "names": { +* "de": "USA", +* "en": "United States", +* "es": "Estados Unidos", +* "fr": "États-Unis", +* "ja": "アメリカ合衆国", +* "pt-BR": "Estados Unidos", +* "ru": "США", +* "zh-CN": "美国" +* }, +* "type": "military" +* }, +* "subdivisions": [ +* { +* "confidence": 50, +* "geoname_id": 5332921, +* "iso_code": "CA", +* "names": { +* "de": "Kalifornien", +* "en": "California", +* "es": "California", +* "fr": "Californie", +* "ja": "カリフォルニア", +* "ru": "Калифорния", +* "zh-CN": "加州" +* } +* } +* ], +* "traits": { +* "autonomous_system_number": "1239", +* "autonomous_system_organization": "Linkem IR WiMax Network", +* "domain": "example.com", +* "is_anonymous_proxy": true, +* "is_transparent_proxy": true, +* "isp": "Linkem spa", +* "ip_address": "1.2.3.4", +* "organization": "Linkem IR WiMax Network", +* "user_type": "traveler", +* }, +* "maxmind": { +* "queries_remaining": "54321" +* } +* } +*/ diff --git a/dlls/geoip/geoip_amxx.h b/dlls/geoip/geoip_amxx.h index 1fe5223d..2e5ef298 100755 --- a/dlls/geoip/geoip_amxx.h +++ b/dlls/geoip/geoip_amxx.h @@ -14,10 +14,12 @@ #ifndef _INCLUDE_GEOIPAMXX_H #define _INCLUDE_GEOIPAMXX_H -#define GEOIPDATADIR "" - +#include "GeoIP2/maxminddb.h" #include "amxxmodule.h" -#include "GeoIP.h" + +#if defined(WIN32) + #define snprintf _snprintf +#endif extern AMX_NATIVE_INFO geoip_natives[]; diff --git a/dlls/geoip/msvc10/geoip.vcxproj b/dlls/geoip/msvc10/geoip.vcxproj index fbfe39ff..c8645c01 100644 --- a/dlls/geoip/msvc10/geoip.vcxproj +++ b/dlls/geoip/msvc10/geoip.vcxproj @@ -52,7 +52,7 @@ Disabled - ..\;..\sdk;..\GeoIP2;%(AdditionalIncludeDirectories) + ..\;..\sdk;..\GeoIP2;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories) WIN32;NDEBUG;_WINDOWS;_USRDLL;GEOIP_EXPORTS;HAVE_STDINT_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) true EnableFastChecks @@ -75,7 +75,7 @@ - ..\;..\sdk;..\GeoIP2;%(AdditionalIncludeDirectories) + ..\;..\sdk;..\GeoIP2;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories) WIN32;NDEBUG;_WINDOWS;_USRDLL;GEOIP_EXPORTS;HAVE_STDINT_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) MultiThreaded false