Merge pull request #99 from Arkshine/geoip-update-bis
Update Geoip module.
This commit is contained in:
commit
5fbc5c074d
|
@ -1,15 +1,22 @@
|
|||
# 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.sources = [
|
||||
'sdk/amxxmodule.cpp',
|
||||
'GeoIP.c',
|
||||
'geoip_amxx.cpp',
|
||||
'GeoIP2/maxminddb.cpp',
|
||||
'geoip_main.cpp',
|
||||
'geoip_natives.cpp',
|
||||
'geoip_util.cpp',
|
||||
]
|
||||
|
||||
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)]
|
||||
|
|
|
@ -1,991 +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 <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h> /* For ntohl */
|
||||
#include <arpa/inet.h>
|
||||
#else
|
||||
#include <windows.h>
|
||||
#define snprintf _snprintf
|
||||
#endif
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <sys/types.h> /* for fstat */
|
||||
#include <sys/stat.h> /* for fstat */
|
||||
|
||||
#ifdef HAVE_STDINT_H
|
||||
#include <stdint.h> /* 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"};
|
||||
|
||||
/* Possible continent codes are AF, AS, EU, NA, OC, SA for Africa, Asia, Europe, North America, Oceania
|
||||
and South America. */
|
||||
|
||||
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) {
|
||||
int len;
|
||||
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;
|
||||
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 {
|
||||
len = strlen(custom_directory);
|
||||
if (custom_directory[len-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;
|
||||
}
|
||||
_GeoIP_setup_dbfilename();
|
||||
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]) {
|
||||
gi->netmask = 32 - depth;
|
||||
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;
|
||||
gi->charset = GEOIP_CHARSET_ISO_8859_1;
|
||||
|
||||
_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, -6l, 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;
|
||||
}
|
||||
|
||||
int GeoIP_charset( GeoIP* gi){
|
||||
return gi->charset;
|
||||
}
|
||||
|
||||
int GeoIP_set_charset( GeoIP* gi, int charset ){
|
||||
int old_charset = gi->charset;
|
||||
gi->charset = charset;
|
||||
return old_charset;
|
||||
}
|
||||
|
||||
int GeoIP_last_netmask (GeoIP* gi) {
|
||||
return gi->netmask;
|
||||
}
|
||||
|
Binary file not shown.
|
@ -1,193 +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
|
||||
|
||||
#if defined _MSC_VER && _MSC_VER >= 1400
|
||||
/* Disable deprecation warnings concerning unsafe CRT functions */
|
||||
#define _CRT_SECURE_NO_DEPRECATE
|
||||
|
||||
/* Replace the POSIX function with ISO C++ conformant one as it is now deprecated */
|
||||
#define fileno _fileno
|
||||
|
||||
/* Disable deprecation warnings because MSVC8 seemingly thinks ISO C++ conformant functions are deprecated. */
|
||||
#pragma warning (disable : 4996)
|
||||
#endif
|
||||
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<string.h>
|
||||
#include <sys/types.h> /* for fstat */
|
||||
#include <sys/stat.h> /* 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 charset; /* 0 iso-8859-1 1 utf8 */
|
||||
int record_iter; /* used in GeoIP_next_record */
|
||||
int netmask; /* netmask of last lookup - set using depth in _GeoIP_seek_record */
|
||||
} GeoIP;
|
||||
|
||||
|
||||
typedef enum {
|
||||
GEOIP_CHARSET_ISO_8859_1 = 0,
|
||||
GEOIP_CHARSET_UTF8 = 1
|
||||
} GeoIPCharset;
|
||||
|
||||
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);
|
||||
|
||||
GEOIP_API int GeoIP_charset (GeoIP* gi);
|
||||
GEOIP_API int GeoIP_set_charset (GeoIP* gi, int charset);
|
||||
|
||||
GEOIP_API int GeoIP_last_netmask (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 */
|
305
dlls/geoip/GeoIP2/inttypes.h
Normal file
305
dlls/geoip/GeoIP2/inttypes.h
Normal file
|
@ -0,0 +1,305 @@
|
|||
// ISO C9x compliant inttypes.h for Microsoft Visual Studio
|
||||
// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124
|
||||
//
|
||||
// Copyright (c) 2006 Alexander Chemeris
|
||||
//
|
||||
// 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 be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _MSC_VER // [
|
||||
#error "Use this header only with Microsoft Visual C++ compilers!"
|
||||
#endif // _MSC_VER ]
|
||||
|
||||
#ifndef _MSC_INTTYPES_H_ // [
|
||||
#define _MSC_INTTYPES_H_
|
||||
|
||||
#if _MSC_VER > 1000
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// 7.8 Format conversion of integer types
|
||||
|
||||
typedef struct {
|
||||
intmax_t quot;
|
||||
intmax_t rem;
|
||||
} imaxdiv_t;
|
||||
|
||||
// 7.8.1 Macros for format specifiers
|
||||
|
||||
#if !defined(__cplusplus) || defined(__STDC_FORMAT_MACROS) // [ See footnote 185 at page 198
|
||||
|
||||
// The fprintf macros for signed integers are:
|
||||
#define PRId8 "d"
|
||||
#define PRIi8 "i"
|
||||
#define PRIdLEAST8 "d"
|
||||
#define PRIiLEAST8 "i"
|
||||
#define PRIdFAST8 "d"
|
||||
#define PRIiFAST8 "i"
|
||||
|
||||
#define PRId16 "hd"
|
||||
#define PRIi16 "hi"
|
||||
#define PRIdLEAST16 "hd"
|
||||
#define PRIiLEAST16 "hi"
|
||||
#define PRIdFAST16 "hd"
|
||||
#define PRIiFAST16 "hi"
|
||||
|
||||
#define PRId32 "I32d"
|
||||
#define PRIi32 "I32i"
|
||||
#define PRIdLEAST32 "I32d"
|
||||
#define PRIiLEAST32 "I32i"
|
||||
#define PRIdFAST32 "I32d"
|
||||
#define PRIiFAST32 "I32i"
|
||||
|
||||
#define PRId64 "I64d"
|
||||
#define PRIi64 "I64i"
|
||||
#define PRIdLEAST64 "I64d"
|
||||
#define PRIiLEAST64 "I64i"
|
||||
#define PRIdFAST64 "I64d"
|
||||
#define PRIiFAST64 "I64i"
|
||||
|
||||
#define PRIdMAX "I64d"
|
||||
#define PRIiMAX "I64i"
|
||||
|
||||
#define PRIdPTR "Id"
|
||||
#define PRIiPTR "Ii"
|
||||
|
||||
// The fprintf macros for unsigned integers are:
|
||||
#define PRIo8 "o"
|
||||
#define PRIu8 "u"
|
||||
#define PRIx8 "x"
|
||||
#define PRIX8 "X"
|
||||
#define PRIoLEAST8 "o"
|
||||
#define PRIuLEAST8 "u"
|
||||
#define PRIxLEAST8 "x"
|
||||
#define PRIXLEAST8 "X"
|
||||
#define PRIoFAST8 "o"
|
||||
#define PRIuFAST8 "u"
|
||||
#define PRIxFAST8 "x"
|
||||
#define PRIXFAST8 "X"
|
||||
|
||||
#define PRIo16 "ho"
|
||||
#define PRIu16 "hu"
|
||||
#define PRIx16 "hx"
|
||||
#define PRIX16 "hX"
|
||||
#define PRIoLEAST16 "ho"
|
||||
#define PRIuLEAST16 "hu"
|
||||
#define PRIxLEAST16 "hx"
|
||||
#define PRIXLEAST16 "hX"
|
||||
#define PRIoFAST16 "ho"
|
||||
#define PRIuFAST16 "hu"
|
||||
#define PRIxFAST16 "hx"
|
||||
#define PRIXFAST16 "hX"
|
||||
|
||||
#define PRIo32 "I32o"
|
||||
#define PRIu32 "I32u"
|
||||
#define PRIx32 "I32x"
|
||||
#define PRIX32 "I32X"
|
||||
#define PRIoLEAST32 "I32o"
|
||||
#define PRIuLEAST32 "I32u"
|
||||
#define PRIxLEAST32 "I32x"
|
||||
#define PRIXLEAST32 "I32X"
|
||||
#define PRIoFAST32 "I32o"
|
||||
#define PRIuFAST32 "I32u"
|
||||
#define PRIxFAST32 "I32x"
|
||||
#define PRIXFAST32 "I32X"
|
||||
|
||||
#define PRIo64 "I64o"
|
||||
#define PRIu64 "I64u"
|
||||
#define PRIx64 "I64x"
|
||||
#define PRIX64 "I64X"
|
||||
#define PRIoLEAST64 "I64o"
|
||||
#define PRIuLEAST64 "I64u"
|
||||
#define PRIxLEAST64 "I64x"
|
||||
#define PRIXLEAST64 "I64X"
|
||||
#define PRIoFAST64 "I64o"
|
||||
#define PRIuFAST64 "I64u"
|
||||
#define PRIxFAST64 "I64x"
|
||||
#define PRIXFAST64 "I64X"
|
||||
|
||||
#define PRIoMAX "I64o"
|
||||
#define PRIuMAX "I64u"
|
||||
#define PRIxMAX "I64x"
|
||||
#define PRIXMAX "I64X"
|
||||
|
||||
#define PRIoPTR "Io"
|
||||
#define PRIuPTR "Iu"
|
||||
#define PRIxPTR "Ix"
|
||||
#define PRIXPTR "IX"
|
||||
|
||||
// The fscanf macros for signed integers are:
|
||||
#define SCNd8 "d"
|
||||
#define SCNi8 "i"
|
||||
#define SCNdLEAST8 "d"
|
||||
#define SCNiLEAST8 "i"
|
||||
#define SCNdFAST8 "d"
|
||||
#define SCNiFAST8 "i"
|
||||
|
||||
#define SCNd16 "hd"
|
||||
#define SCNi16 "hi"
|
||||
#define SCNdLEAST16 "hd"
|
||||
#define SCNiLEAST16 "hi"
|
||||
#define SCNdFAST16 "hd"
|
||||
#define SCNiFAST16 "hi"
|
||||
|
||||
#define SCNd32 "ld"
|
||||
#define SCNi32 "li"
|
||||
#define SCNdLEAST32 "ld"
|
||||
#define SCNiLEAST32 "li"
|
||||
#define SCNdFAST32 "ld"
|
||||
#define SCNiFAST32 "li"
|
||||
|
||||
#define SCNd64 "I64d"
|
||||
#define SCNi64 "I64i"
|
||||
#define SCNdLEAST64 "I64d"
|
||||
#define SCNiLEAST64 "I64i"
|
||||
#define SCNdFAST64 "I64d"
|
||||
#define SCNiFAST64 "I64i"
|
||||
|
||||
#define SCNdMAX "I64d"
|
||||
#define SCNiMAX "I64i"
|
||||
|
||||
#ifdef _WIN64 // [
|
||||
# define SCNdPTR "I64d"
|
||||
# define SCNiPTR "I64i"
|
||||
#else // _WIN64 ][
|
||||
# define SCNdPTR "ld"
|
||||
# define SCNiPTR "li"
|
||||
#endif // _WIN64 ]
|
||||
|
||||
// The fscanf macros for unsigned integers are:
|
||||
#define SCNo8 "o"
|
||||
#define SCNu8 "u"
|
||||
#define SCNx8 "x"
|
||||
#define SCNX8 "X"
|
||||
#define SCNoLEAST8 "o"
|
||||
#define SCNuLEAST8 "u"
|
||||
#define SCNxLEAST8 "x"
|
||||
#define SCNXLEAST8 "X"
|
||||
#define SCNoFAST8 "o"
|
||||
#define SCNuFAST8 "u"
|
||||
#define SCNxFAST8 "x"
|
||||
#define SCNXFAST8 "X"
|
||||
|
||||
#define SCNo16 "ho"
|
||||
#define SCNu16 "hu"
|
||||
#define SCNx16 "hx"
|
||||
#define SCNX16 "hX"
|
||||
#define SCNoLEAST16 "ho"
|
||||
#define SCNuLEAST16 "hu"
|
||||
#define SCNxLEAST16 "hx"
|
||||
#define SCNXLEAST16 "hX"
|
||||
#define SCNoFAST16 "ho"
|
||||
#define SCNuFAST16 "hu"
|
||||
#define SCNxFAST16 "hx"
|
||||
#define SCNXFAST16 "hX"
|
||||
|
||||
#define SCNo32 "lo"
|
||||
#define SCNu32 "lu"
|
||||
#define SCNx32 "lx"
|
||||
#define SCNX32 "lX"
|
||||
#define SCNoLEAST32 "lo"
|
||||
#define SCNuLEAST32 "lu"
|
||||
#define SCNxLEAST32 "lx"
|
||||
#define SCNXLEAST32 "lX"
|
||||
#define SCNoFAST32 "lo"
|
||||
#define SCNuFAST32 "lu"
|
||||
#define SCNxFAST32 "lx"
|
||||
#define SCNXFAST32 "lX"
|
||||
|
||||
#define SCNo64 "I64o"
|
||||
#define SCNu64 "I64u"
|
||||
#define SCNx64 "I64x"
|
||||
#define SCNX64 "I64X"
|
||||
#define SCNoLEAST64 "I64o"
|
||||
#define SCNuLEAST64 "I64u"
|
||||
#define SCNxLEAST64 "I64x"
|
||||
#define SCNXLEAST64 "I64X"
|
||||
#define SCNoFAST64 "I64o"
|
||||
#define SCNuFAST64 "I64u"
|
||||
#define SCNxFAST64 "I64x"
|
||||
#define SCNXFAST64 "I64X"
|
||||
|
||||
#define SCNoMAX "I64o"
|
||||
#define SCNuMAX "I64u"
|
||||
#define SCNxMAX "I64x"
|
||||
#define SCNXMAX "I64X"
|
||||
|
||||
#ifdef _WIN64 // [
|
||||
# define SCNoPTR "I64o"
|
||||
# define SCNuPTR "I64u"
|
||||
# define SCNxPTR "I64x"
|
||||
# define SCNXPTR "I64X"
|
||||
#else // _WIN64 ][
|
||||
# define SCNoPTR "lo"
|
||||
# define SCNuPTR "lu"
|
||||
# define SCNxPTR "lx"
|
||||
# define SCNXPTR "lX"
|
||||
#endif // _WIN64 ]
|
||||
|
||||
#endif // __STDC_FORMAT_MACROS ]
|
||||
|
||||
// 7.8.2 Functions for greatest-width integer types
|
||||
|
||||
// 7.8.2.1 The imaxabs function
|
||||
#define imaxabs _abs64
|
||||
|
||||
// 7.8.2.2 The imaxdiv function
|
||||
|
||||
// This is modified version of div() function from Microsoft's div.c found
|
||||
// in %MSVC.NET%\crt\src\div.c
|
||||
#ifdef STATIC_IMAXDIV // [
|
||||
static
|
||||
#else // STATIC_IMAXDIV ][
|
||||
_inline
|
||||
#endif // STATIC_IMAXDIV ]
|
||||
imaxdiv_t __cdecl imaxdiv(intmax_t numer, intmax_t denom)
|
||||
{
|
||||
imaxdiv_t result;
|
||||
|
||||
result.quot = numer / denom;
|
||||
result.rem = numer % denom;
|
||||
|
||||
if (numer < 0 && result.rem > 0) {
|
||||
// did division wrong; must fix up
|
||||
++result.quot;
|
||||
result.rem -= denom;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// 7.8.2.3 The strtoimax and strtoumax functions
|
||||
#define strtoimax _strtoi64
|
||||
#define strtoumax _strtoui64
|
||||
|
||||
// 7.8.2.4 The wcstoimax and wcstoumax functions
|
||||
#define wcstoimax _wcstoi64
|
||||
#define wcstoumax _wcstoui64
|
||||
|
||||
|
||||
#endif // _MSC_INTTYPES_H_ ]
|
167
dlls/geoip/GeoIP2/maxminddb-compat-util.h
Normal file
167
dlls/geoip/GeoIP2/maxminddb-compat-util.h
Normal file
|
@ -0,0 +1,167 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* *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 <pascal.gloor@spale.com>
|
||||
*
|
||||
* 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 (void *)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 <das@FreeBSD.org>
|
||||
* 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 = (char *)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 = (char *)malloc(len + 1)) == NULL)
|
||||
return (NULL);
|
||||
memcpy(copy, str, len);
|
||||
copy[len] = '\0';
|
||||
return (copy);
|
||||
}
|
||||
/* *INDENT-ON* */
|
1782
dlls/geoip/GeoIP2/maxminddb.cpp
Normal file
1782
dlls/geoip/GeoIP2/maxminddb.cpp
Normal file
File diff suppressed because it is too large
Load Diff
267
dlls/geoip/GeoIP2/maxminddb.h
Normal file
267
dlls/geoip/GeoIP2/maxminddb.h
Normal file
|
@ -0,0 +1,267 @@
|
|||
#ifndef MAXMINDDB_H
|
||||
#define MAXMINDDB_H
|
||||
|
||||
#ifndef _POSIX_C_SOURCE
|
||||
#define _POSIX_C_SOURCE 200112L
|
||||
#endif
|
||||
|
||||
#include "maxminddb_config.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "stdbool.h" /* Arksnine: Not supported by MSVC */
|
||||
#include <BaseTsd.h> /* Arkshine: ssize_t replacement */
|
||||
typedef SSIZE_T ssize_t;
|
||||
#include <WinSock2.h>
|
||||
#include <WS2tcpip.h>
|
||||
typedef ADDRESS_FAMILY sa_family_t;
|
||||
#else
|
||||
#include <stdbool.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
const char GeoIPCountryCode[][3] =
|
||||
{
|
||||
"AP", "EU", "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AN",
|
||||
"AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AZ", "BA", "BB",
|
||||
"BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BM", "BN", "BO",
|
||||
"BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC", "CD",
|
||||
"CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR",
|
||||
"CU", "CV", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO",
|
||||
"DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ",
|
||||
"FK", "FM", "FO", "FR", "FX", "GA", "GB", "GD", "GE", "GF",
|
||||
"GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT",
|
||||
"GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID",
|
||||
"IE", "IL", "IN", "IO", "IQ", "IR", "IS", "IT", "JM", "JO",
|
||||
"JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW",
|
||||
"KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT",
|
||||
"LU", "LV", "LY", "MA", "MC", "MD", "MG", "MH", "MK", "ML",
|
||||
"MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV",
|
||||
"MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NF", "NG", "NI",
|
||||
"NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF",
|
||||
"PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW",
|
||||
"PY", "QA", "RE", "RO", "RU", "RW", "SA", "SB", "SC", "SD",
|
||||
"SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO",
|
||||
"SR", "ST", "SV", "SY", "SZ", "TC", "TD", "TF", "TG", "TH",
|
||||
"TJ", "TK", "TM", "TN", "TO", "TL", "TR", "TT", "TV", "TW",
|
||||
"TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE",
|
||||
"VG", "VI", "VN", "VU", "WF", "WS", "YE", "YT", "RS", "ZA",
|
||||
"ZM", "ME", "ZW", "A1", "A2", "O1", "AX", "GG", "IM", "JE",
|
||||
"BL", "MF"
|
||||
};
|
||||
|
||||
const char GeoIPCountryCode3[][4] =
|
||||
{
|
||||
"AP", "EU", "AND", "ARE", "AFG", "ATG", "AIA", "ALB", "ARM", "ANT",
|
||||
"AGO", "AQ", "ARG", "ASM", "AUT", "AUS", "ABW", "AZE", "BIH", "BRB",
|
||||
"BGD", "BEL", "BFA", "BGR", "BHR", "BDI", "BEN", "BMU", "BRN", "BOL",
|
||||
"BRA", "BHS", "BTN", "BV", "BWA", "BLR", "BLZ", "CAN", "CC", "COD",
|
||||
"CAF", "COG", "CHE", "CIV", "COK", "CHL", "CMR", "CHN", "COL", "CRI",
|
||||
"CUB", "CPV", "CX", "CYP", "CZE", "DEU", "DJI", "DNK", "DMA", "DOM",
|
||||
"DZA", "ECU", "EST", "EGY", "ESH", "ERI", "ESP", "ETH", "FIN", "FJI",
|
||||
"FLK", "FSM", "FRO", "FRA", "FX", "GAB", "GBR", "GRD", "GEO", "GUF",
|
||||
"GHA", "GIB", "GRL", "GMB", "GIN", "GLP", "GNQ", "GRC", "GS", "GTM",
|
||||
"GUM", "GNB", "GUY", "HKG", "HM", "HND", "HRV", "HTI", "HUN", "IDN",
|
||||
"IRL", "ISR", "IND", "IO", "IRQ", "IRN", "ISL", "ITA", "JAM", "JOR",
|
||||
"JPN", "KEN", "KGZ", "KHM", "KIR", "COM", "KNA", "PRK", "KOR", "KWT",
|
||||
"CYM", "KAZ", "LAO", "LBN", "LCA", "LIE", "LKA", "LBR", "LSO", "LTU",
|
||||
"LUX", "LVA", "LBY", "MAR", "MCO", "MDA", "MDG", "MHL", "MKD", "MLI",
|
||||
"MMR", "MNG", "MAC", "MNP", "MTQ", "MRT", "MSR", "MLT", "MUS", "MDV",
|
||||
"MWI", "MEX", "MYS", "MOZ", "NAM", "NCL", "NER", "NFK", "NGA", "NIC",
|
||||
"NLD", "NOR", "NPL", "NRU", "NIU", "NZL", "OMN", "PAN", "PER", "PYF",
|
||||
"PNG", "PHL", "PAK", "POL", "SPM", "PCN", "PRI", "PSE", "PRT", "PLW",
|
||||
"PRY", "QAT", "REU", "ROU", "RUS", "RWA", "SAU", "SLB", "SYC", "SDN",
|
||||
"SWE", "SGP", "SHN", "SVN", "SJM", "SVK", "SLE", "SMR", "SEN", "SOM",
|
||||
"SUR", "STP", "SLV", "SYR", "SWZ", "TCA", "TCD", "TF", "TGO", "THA",
|
||||
"TJK", "TKL", "TKM", "TUN", "TON", "TLS", "TUR", "TTO", "TUV", "TWN",
|
||||
"TZA", "UKR", "UGA", "UM", "USA", "URY", "UZB", "VAT", "VCT", "VEN",
|
||||
"VGB", "VIR", "VNM", "VUT", "WLF", "WSM", "YEM", "YT", "SRB", "ZAF",
|
||||
"ZMB", "MNE", "ZWE", "A1", "A2", "O1", "ALA", "GGY", "IMN", "JEY",
|
||||
"BLM", "MAF"
|
||||
};
|
||||
|
||||
#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)
|
||||
|
||||
/* GEOIPDB flags */
|
||||
#define MMDB_MODE_MMAP (1)
|
||||
#define MMDB_MODE_MASK (7)
|
||||
|
||||
/* GEOIPDB err 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 {
|
||||
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;
|
||||
} MMDB_entry_data_list_s;
|
||||
|
||||
typedef struct MMDB_description_s {
|
||||
const char *language;
|
||||
const char *description;
|
||||
} MMDB_description_s;
|
||||
|
||||
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;
|
||||
} MMDB_metadata_s;
|
||||
|
||||
typedef struct MMDB_ipv4_start_node_s {
|
||||
uint16_t netmask;
|
||||
uint32_t node_value;
|
||||
} MMDB_ipv4_start_node_s;
|
||||
|
||||
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;
|
||||
} MMDB_s;
|
||||
|
||||
typedef struct MMDB_search_node_s {
|
||||
uint64_t left_record;
|
||||
uint64_t right_record;
|
||||
} MMDB_search_node_s;
|
||||
|
||||
/* *INDENT-OFF* */
|
||||
/* --prototypes automatically generated by dev-bin/regen-prototypes.pl - don't remove this comment */
|
||||
extern int MMDB_open(const char *const filename, uint32_t flags, MMDB_s *const mmdb);
|
||||
extern MMDB_lookup_result_s MMDB_lookup_string(MMDB_s *const mmdb,
|
||||
const char *const ipstr,
|
||||
int *const gai_error,
|
||||
int *const mmdb_error);
|
||||
extern MMDB_lookup_result_s MMDB_lookup_sockaddr(
|
||||
MMDB_s *const mmdb,
|
||||
const struct sockaddr *const sockaddr,
|
||||
int *const mmdb_error);
|
||||
extern int MMDB_read_node(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(
|
||||
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);
|
||||
/* --prototypes end - don't remove this comment-- */
|
||||
/* *INDENT-ON* */
|
||||
|
||||
#endif /* MAXMINDDB_H */
|
16
dlls/geoip/GeoIP2/maxminddb_config.h
Normal file
16
dlls/geoip/GeoIP2/maxminddb_config.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef MAXMINDDB_CONFIG_H
|
||||
#define MAXMINDDB_CONFIG_H
|
||||
|
||||
#define PACKAGE_VERSION "0.5.6" // Arkshine: added manually.
|
||||
|
||||
#ifndef MMDB_UINT128_USING_MODE
|
||||
/* Define as 1 if we we use unsigned int __atribute__ ((__mode__(TI))) for uint128 values */
|
||||
#define MMDB_UINT128_USING_MODE 0
|
||||
#endif
|
||||
|
||||
#ifndef MMDB_UINT128_IS_BYTE_ARRAY
|
||||
/* Define as 1 if we don't have an unsigned __int128 type */
|
||||
#define MMDB_UINT128_IS_BYTE_ARRAY 1
|
||||
#endif
|
||||
|
||||
#endif /* MAXMINDDB_CONFIG_H */
|
46
dlls/geoip/GeoIP2/stdbool.h
Normal file
46
dlls/geoip/GeoIP2/stdbool.h
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Copyright (c) 2000 Jeroen Ruigrok van der Werven <asmodai@FreeBSD.org>
|
||||
* 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.
|
||||
*
|
||||
* $FreeBSD: src/include/stdbool.h,v 1.6 2002/08/16 07:33:14 alfred Exp $
|
||||
*/
|
||||
|
||||
#ifndef _STDBOOL_H_
|
||||
#define _STDBOOL_H_
|
||||
|
||||
#define __bool_true_false_are_defined 1
|
||||
|
||||
#ifndef __cplusplus
|
||||
|
||||
#define false 0
|
||||
#define true 1
|
||||
|
||||
#define bool _Bool
|
||||
#if __STDC_VERSION__ < 199901L && __GNUC__ < 3
|
||||
typedef int _Bool;
|
||||
#endif
|
||||
|
||||
#endif /* !__cplusplus */
|
||||
|
||||
#endif /* !_STDBOOL_H_ */
|
BIN
dlls/geoip/GeoLite2-Country.mmdb
Normal file
BIN
dlls/geoip/GeoLite2-Country.mmdb
Normal file
Binary file not shown.
|
@ -14,7 +14,7 @@ MM_ROOT = ../../../metamod/metamod
|
|||
|
||||
PROJECT = geoip
|
||||
|
||||
OBJECTS = sdk/amxxmodule.cpp GeoIP.c geoip_amxx.cpp
|
||||
OBJECTS = sdk/amxxmodule.cpp GeoIP2/maxminddb.cpp geoip_main.cpp geoip_natives.cpp geoip_util.cpp
|
||||
|
||||
##############################################
|
||||
### CONFIGURE ANY OTHER FLAGS/OPTIONS HERE ###
|
||||
|
|
|
@ -1,115 +0,0 @@
|
|||
// vim: set ts=4 sw=4 tw=99 noet:
|
||||
//
|
||||
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
|
||||
// Copyright (C) The AMX Mod X Development Team.
|
||||
//
|
||||
// This software is licensed under the GNU General Public License, version 3 or higher.
|
||||
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
|
||||
// https://alliedmods.net/amxmodx-license
|
||||
|
||||
//
|
||||
// GeoIP Module
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include "geoip_amxx.h"
|
||||
|
||||
GeoIP *gi = NULL;
|
||||
|
||||
inline void StripPort(char *ip)
|
||||
{
|
||||
char *tmp = strchr(ip, ':');
|
||||
if (!tmp)
|
||||
return;
|
||||
*tmp = '\0';
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
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)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
MF_SetAmxString(amx, params[2], ccode, 2);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (ccode == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
MF_SetAmxString(amx, params[2], ccode, 3);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
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!");
|
||||
return;
|
||||
}
|
||||
char *db_info = GeoIP_database_info(gi);
|
||||
MF_Log("Database info: %s", db_info);
|
||||
MF_AddNatives(geoip_natives);
|
||||
}
|
||||
|
||||
void OnAmxxDetach()
|
||||
{
|
||||
GeoIP_delete(gi);
|
||||
gi = 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},
|
||||
};
|
|
@ -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[];
|
||||
|
||||
|
|
232
dlls/geoip/geoip_main.cpp
Normal file
232
dlls/geoip/geoip_main.cpp
Normal file
|
@ -0,0 +1,232 @@
|
|||
// vim: set ts=4 sw=4 tw=99 noet:
|
||||
//
|
||||
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
|
||||
// Copyright (C) The AMX Mod X Development Team.
|
||||
//
|
||||
// This software is licensed under the GNU General Public License, version 3 or higher.
|
||||
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
|
||||
// https://alliedmods.net/amxmodx-license
|
||||
|
||||
//
|
||||
// GeoIP Module
|
||||
//
|
||||
|
||||
#include "geoip_main.h"
|
||||
#include "geoip_natives.h"
|
||||
#include "geoip_util.h"
|
||||
#include <time.h>
|
||||
|
||||
MMDB_s HandleDB;
|
||||
ke::Vector<ke::AString> LangList;
|
||||
|
||||
void OnAmxxAttach()
|
||||
{
|
||||
if (loadDatabase())
|
||||
{
|
||||
MF_AddNatives(GeoipNatives);
|
||||
}
|
||||
|
||||
REG_SVR_COMMAND("geoip", OnGeoipCommand);
|
||||
}
|
||||
|
||||
void OnAmxxDetach()
|
||||
{
|
||||
MMDB_close(&HandleDB);
|
||||
|
||||
LangList.clear();
|
||||
}
|
||||
|
||||
void OnGeoipCommand()
|
||||
{
|
||||
const char *cmd = CMD_ARGV(1);
|
||||
|
||||
if (!strcmp(cmd, "version"))
|
||||
{
|
||||
if (!HandleDB.filename)
|
||||
{
|
||||
printf("\n Database is not loaded.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
const char *meta_dump = "\n"
|
||||
" Database metadata\n"
|
||||
" Node count: %i\n"
|
||||
" Record size: %i bits\n"
|
||||
" IP version: IPv%i\n"
|
||||
" Binary format: %i.%i\n"
|
||||
" Build epoch: %llu (%s)\n"
|
||||
" Type: %s\n"
|
||||
" Languages: ";
|
||||
|
||||
char date[40];
|
||||
strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S UTC", gmtime((const time_t *)&HandleDB.metadata.build_epoch));
|
||||
|
||||
fprintf(stdout, meta_dump,
|
||||
HandleDB.metadata.node_count,
|
||||
HandleDB.metadata.record_size,
|
||||
HandleDB.metadata.ip_version,
|
||||
HandleDB.metadata.binary_format_major_version,
|
||||
HandleDB.metadata.binary_format_minor_version,
|
||||
HandleDB.metadata.build_epoch,
|
||||
date,
|
||||
HandleDB.metadata.database_type);
|
||||
|
||||
for (size_t i = 0; i < HandleDB.metadata.languages.count; ++i)
|
||||
{
|
||||
fprintf(stdout, "%s", HandleDB.metadata.languages.names[i]);
|
||||
|
||||
if (i <HandleDB.metadata.languages.count - 1)
|
||||
{
|
||||
fprintf(stdout, " ");
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stdout, "\n");
|
||||
fprintf(stdout, " Description:\n");
|
||||
|
||||
for (size_t i = 0; i < HandleDB.metadata.description.count; ++i)
|
||||
{
|
||||
fprintf(stdout, " %s: %s\n",
|
||||
HandleDB.metadata.description.descriptions[i]->language,
|
||||
HandleDB.metadata.description.descriptions[i]->description);
|
||||
}
|
||||
fprintf(stdout, "\n");
|
||||
}
|
||||
else if (!strcmp(cmd, "dump"))
|
||||
{
|
||||
if (!HandleDB.filename)
|
||||
{
|
||||
printf("\n Database is not loaded.\n\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int num_args = CMD_ARGC();
|
||||
|
||||
if (num_args < 3)
|
||||
{
|
||||
printf("\n An IP address must be provided.\n\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char *ip = stripPort((char *)CMD_ARGV(2));
|
||||
|
||||
int gai_error = 0;
|
||||
int mmdb_error = 0;
|
||||
|
||||
MMDB_lookup_result_s result = MMDB_lookup_string(&HandleDB, ip, &gai_error, &mmdb_error);
|
||||
|
||||
if (gai_error != 0 || mmdb_error != MMDB_SUCCESS || !result.found_entry)
|
||||
{
|
||||
printf("\n Either look up failed or no found result.\n\n");
|
||||
return;
|
||||
}
|
||||
|
||||
MMDB_entry_data_list_s *entry_data_list = NULL;
|
||||
int status = -1;
|
||||
|
||||
if ((status = MMDB_get_entry_data_list(&result.entry, &entry_data_list)) != MMDB_SUCCESS || entry_data_list == NULL)
|
||||
{
|
||||
printf("\n Could not retrieve data list - %s.\n\n", MMDB_strerror(status));
|
||||
return;
|
||||
}
|
||||
|
||||
const char *file = NULL;
|
||||
FILE *fp = NULL;
|
||||
|
||||
if (num_args > 3)
|
||||
{
|
||||
file = CMD_ARGV(3);
|
||||
fp = fopen(MF_BuildPathname("%s", file), "w");
|
||||
}
|
||||
|
||||
if (!fp)
|
||||
{
|
||||
file = NULL;
|
||||
fp = stdout;
|
||||
}
|
||||
|
||||
fprintf(fp, "\n");
|
||||
MMDB_dump_entry_data_list(fp, entry_data_list, 2);
|
||||
fprintf(fp, "\n");
|
||||
|
||||
if (file)
|
||||
{
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
MMDB_free_entry_data_list(entry_data_list);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("\n");
|
||||
printf(" Usage: geoip <command> [argument]\n");
|
||||
printf(" Commands:\n");
|
||||
printf(" version - display geoip database metadata\n");
|
||||
printf(" dump <ip> [output file] - dump all data from an IP address formatted in a JSON-ish fashion.\n");
|
||||
printf(" An output file is mod-based and if not provided, it will print in the console.\n");
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
bool loadDatabase()
|
||||
{
|
||||
if (HandleDB.filename) // Already loaded.
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
// MF_BuildPathname not used because backslash
|
||||
// makes CreateFileMapping failing under windows.
|
||||
|
||||
snprintf(file, sizeof(file)-1, "%s/%s/GeoLite2-%s.mmdb", modName, dataDir, databases[i]);
|
||||
|
||||
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 false;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// Retrieve supported languages.
|
||||
for (size_t i = 0; i < HandleDB.metadata.languages.count; i++)
|
||||
{
|
||||
LangList.append(ke::AString(HandleDB.metadata.languages.names[i]));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
23
dlls/geoip/geoip_main.h
Normal file
23
dlls/geoip/geoip_main.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
// vim: set ts=4 sw=4 tw=99 noet:
|
||||
//
|
||||
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
|
||||
// Copyright (C) The AMX Mod X Development Team.
|
||||
//
|
||||
// This software is licensed under the GNU General Public License, version 3 or higher.
|
||||
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
|
||||
// https://alliedmods.net/amxmodx-license
|
||||
|
||||
//
|
||||
// GeoIP Module
|
||||
//
|
||||
|
||||
#ifndef _INCLUDE_GEOIPMAIN_H
|
||||
#define _INCLUDE_GEOIPMAIN_H
|
||||
|
||||
#include "GeoIP2/maxminddb.h"
|
||||
#include "amxxmodule.h"
|
||||
|
||||
bool loadDatabase();
|
||||
void OnGeoipCommand();
|
||||
|
||||
#endif // _INCLUDE_GEOIPMAIN_H
|
399
dlls/geoip/geoip_natives.cpp
Executable file
399
dlls/geoip/geoip_natives.cpp
Executable file
|
@ -0,0 +1,399 @@
|
|||
// vim: set ts=4 sw=4 tw=99 noet:
|
||||
//
|
||||
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
|
||||
// Copyright (C) The AMX Mod X Development Team.
|
||||
//
|
||||
// This software is licensed under the GNU General Public License, version 3 or higher.
|
||||
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
|
||||
// https://alliedmods.net/amxmodx-license
|
||||
|
||||
//
|
||||
// GeoIP Module
|
||||
//
|
||||
|
||||
#include "geoip_main.h"
|
||||
#include "geoip_natives.h"
|
||||
#include "geoip_util.h"
|
||||
|
||||
#include <am-string.h>
|
||||
#include <am-vector.h>
|
||||
|
||||
// native geoip_code2(const ip[], ccode[3]);
|
||||
// Deprecated.
|
||||
static cell AMX_NATIVE_CALL amx_geoip_code2(AMX *amx, cell *params)
|
||||
{
|
||||
int length;
|
||||
char *ip = stripPort(MF_GetAmxString(amx, params[1], 0, &length));
|
||||
|
||||
const char *path[] = { "country", "iso_code", NULL };
|
||||
const char *code = lookupString(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 length;
|
||||
char *ip = stripPort(MF_GetAmxString(amx, params[1], 0, &length));
|
||||
|
||||
const char *path[] = { "country", "iso_code", NULL };
|
||||
const char *code = lookupString(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 length;
|
||||
char *ip = stripPort(MF_GetAmxString(amx, params[1], 0, &length));
|
||||
|
||||
const char *path[] = { "country", "iso_code", NULL };
|
||||
const char *code = lookupString(ip, path);
|
||||
|
||||
if (!code)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
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 length;
|
||||
char *ip = stripPort(MF_GetAmxString(amx, params[1], 0, &length));
|
||||
|
||||
const char *path[] = { "country", "iso_code", NULL };
|
||||
const char *code = lookupString(ip, path, &length);
|
||||
|
||||
if (!code)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
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, id = -1);
|
||||
static cell AMX_NATIVE_CALL amx_geoip_country(AMX *amx, cell *params)
|
||||
{
|
||||
int length;
|
||||
char *ip = stripPort(MF_GetAmxString(amx, params[1], 0, &length));
|
||||
|
||||
const char *path[] = { "country", "names", getLang(params[4]), NULL };
|
||||
const char *country = lookupString(ip, path, &length);
|
||||
|
||||
if (!country)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return MF_SetAmxStringUTF8Char(amx, params[2], country, length, params[3] + 1);
|
||||
}
|
||||
|
||||
// native geoip_city(const ip[], result[], len, id = -1);
|
||||
static cell AMX_NATIVE_CALL amx_geoip_city(AMX *amx, cell *params)
|
||||
{
|
||||
int length;
|
||||
char *ip = stripPort(MF_GetAmxString(amx, params[1], 0, &length));
|
||||
|
||||
const char *path[] = { "city", "names", getLang(params[4]), NULL };
|
||||
const char *city = lookupString(ip, path, &length);
|
||||
|
||||
return MF_SetAmxStringUTF8Char(amx, params[2], city ? city : "", length, params[3] + 1);
|
||||
}
|
||||
|
||||
// native geoip_region_code(const ip[], result[], len);
|
||||
static cell AMX_NATIVE_CALL amx_geoip_region_code(AMX *amx, cell *params)
|
||||
{
|
||||
int length;
|
||||
int finalLength = 0;
|
||||
char code[12]; // This should be largely enough to hold xx-yyyy and more if needed.
|
||||
|
||||
char *ip = stripPort(MF_GetAmxString(amx, params[1], 0, &length));
|
||||
|
||||
const char *pathCountry[] = { "country", "iso_code", NULL };
|
||||
const char *countryCode = lookupString(ip, pathCountry, &length);
|
||||
|
||||
if (countryCode)
|
||||
{
|
||||
finalLength = length + 1; // + 1 for dash.
|
||||
snprintf(code, finalLength + 1, "%s-", countryCode); // + EOS.
|
||||
|
||||
const char *pathRegion[] = { "subdivisions", "0", "iso_code", NULL }; // First result.
|
||||
const char *regionCode = lookupString(ip, pathRegion, &length);
|
||||
|
||||
if (regionCode)
|
||||
{
|
||||
finalLength += length;
|
||||
strncat(code, regionCode, length);
|
||||
}
|
||||
else
|
||||
{
|
||||
finalLength = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return MF_SetAmxString(amx, params[2], finalLength ? code : "", ke::Min(finalLength, params[3]));
|
||||
}
|
||||
|
||||
// native geoip_region_name(const ip[], result[], len, id = -1);
|
||||
static cell AMX_NATIVE_CALL amx_geoip_region_name(AMX *amx, cell *params)
|
||||
{
|
||||
int length;
|
||||
char *ip = stripPort(MF_GetAmxString(amx, params[1], 0, &length));
|
||||
|
||||
const char *path[] = { "subdivisions", "0", "names", getLang(params[4]), NULL }; // First result.
|
||||
const char *region = lookupString(ip, path, &length);
|
||||
|
||||
return MF_SetAmxStringUTF8Char(amx, params[2], region ? region : "", length, params[3] + 1);
|
||||
}
|
||||
|
||||
// native geoip_timezone(const ip[], result[], len);
|
||||
static cell AMX_NATIVE_CALL amx_geoip_timezone(AMX *amx, cell *params)
|
||||
{
|
||||
int length;
|
||||
char *ip = stripPort(MF_GetAmxString(amx, params[1], 0, &length));
|
||||
|
||||
const char *path[] = { "location", "time_zone", NULL };
|
||||
const char *timezone = lookupString(ip, path, &length);
|
||||
|
||||
return MF_SetAmxString(amx, params[2], timezone ? timezone : "", ke::Min(length, params[3]));
|
||||
}
|
||||
|
||||
// native geoip_latitude(const ip[]);
|
||||
static cell AMX_NATIVE_CALL amx_geoip_latitude(AMX *amx, cell *params)
|
||||
{
|
||||
int length;
|
||||
char *ip = stripPort(MF_GetAmxString(amx, params[1], 0, &length));
|
||||
|
||||
const char *path[] = { "location", "latitude", NULL };
|
||||
double latitude = lookupDouble(ip, path);
|
||||
|
||||
return amx_ftoc(latitude);
|
||||
}
|
||||
|
||||
// native geoip_longitude(const ip[]);
|
||||
static cell AMX_NATIVE_CALL amx_geoip_longitude(AMX *amx, cell *params)
|
||||
{
|
||||
int length;
|
||||
char *ip = stripPort(MF_GetAmxString(amx, params[1], 0, &length));
|
||||
|
||||
const char *path[] = { "location", "longitude", NULL };
|
||||
double longitude = lookupDouble(ip, path);
|
||||
|
||||
return amx_ftoc(longitude);
|
||||
}
|
||||
|
||||
// native Float:geoip_distance(Float:lat1, Float:lon1, Float:lat2, Float:lon2, system = SYSTEM_METRIC);
|
||||
static cell AMX_NATIVE_CALL amx_geoip_distance(AMX *amx, cell *params)
|
||||
{
|
||||
float earthRadius = params[5] ? 3958.0 : 6370.997; // miles / km
|
||||
|
||||
float lat1 = amx_ctof(params[1]) * (M_PI / 180);
|
||||
float lon1 = amx_ctof(params[2]) * (M_PI / 180);
|
||||
float lat2 = amx_ctof(params[3]) * (M_PI / 180);
|
||||
float lon2 = amx_ctof(params[4]) * (M_PI / 180);
|
||||
|
||||
return amx_ftoc(earthRadius * acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1)));
|
||||
}
|
||||
|
||||
// native Continent:geoip_continent_code(const ip[], result[3] = "");
|
||||
static cell AMX_NATIVE_CALL amx_geoip_continent_code(AMX *amx, cell *params)
|
||||
{
|
||||
int length;
|
||||
char *ip = stripPort(MF_GetAmxString(amx, params[1], 0, &length));
|
||||
|
||||
const char *path[] = { "continent", "code", NULL };
|
||||
const char *code = lookupString(ip, path, &length);
|
||||
|
||||
MF_SetAmxString(amx, params[2], code ? code : "", code ? 2 : 0);
|
||||
|
||||
return getContinentId(code);
|
||||
}
|
||||
|
||||
// native geoip_continent_name(const ip[], result[], len, id = -1);
|
||||
static cell AMX_NATIVE_CALL amx_geoip_continent_name(AMX *amx, cell *params)
|
||||
{
|
||||
int length;
|
||||
char *ip = stripPort(MF_GetAmxString(amx, params[1], 0, &length));
|
||||
|
||||
const char *path[] = { "continent", "names", getLang(params[4]), NULL };
|
||||
const char *continent = lookupString(ip, path, &length);
|
||||
|
||||
return MF_SetAmxStringUTF8Char(amx, params[2], continent ? continent : "", length, params[3] + 1);
|
||||
}
|
||||
|
||||
|
||||
AMX_NATIVE_INFO GeoipNatives[] =
|
||||
{
|
||||
{ "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 },
|
||||
{ "geoip_city" , amx_geoip_city },
|
||||
|
||||
{ "geoip_region_code", amx_geoip_region_code },
|
||||
{ "geoip_region_name", amx_geoip_region_name },
|
||||
|
||||
{ "geoip_timezone" , amx_geoip_timezone },
|
||||
{ "geoip_latitude" , amx_geoip_latitude },
|
||||
{ "geoip_longitude", amx_geoip_longitude },
|
||||
{ "geoip_distance" , amx_geoip_distance },
|
||||
|
||||
{ "geoip_continent_code", amx_geoip_continent_code },
|
||||
{ "geoip_continent_name", amx_geoip_continent_name },
|
||||
|
||||
{ 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"
|
||||
* }
|
||||
* }
|
||||
*/
|
24
dlls/geoip/geoip_natives.h
Normal file
24
dlls/geoip/geoip_natives.h
Normal file
|
@ -0,0 +1,24 @@
|
|||
// vim: set ts=4 sw=4 tw=99 noet:
|
||||
//
|
||||
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
|
||||
// Copyright (C) The AMX Mod X Development Team.
|
||||
//
|
||||
// This software is licensed under the GNU General Public License, version 3 or higher.
|
||||
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
|
||||
// https://alliedmods.net/amxmodx-license
|
||||
|
||||
//
|
||||
// GeoIP Module
|
||||
//
|
||||
|
||||
#ifndef _INCLUDE_GEOIPNATIVES_H
|
||||
#define _INCLUDE_GEOIPNATIVES_H
|
||||
|
||||
#include <am-string.h>
|
||||
#include <am-vector.h>
|
||||
|
||||
extern MMDB_s HandleDB;
|
||||
extern ke::Vector<ke::AString> LangList;
|
||||
extern AMX_NATIVE_INFO GeoipNatives[];
|
||||
|
||||
#endif // _INCLUDE_GEOIPNATIVES_H
|
215
dlls/geoip/geoip_util.cpp
Normal file
215
dlls/geoip/geoip_util.cpp
Normal file
|
@ -0,0 +1,215 @@
|
|||
// vim: set ts=4 sw=4 tw=99 noet:
|
||||
//
|
||||
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
|
||||
// Copyright (C) The AMX Mod X Development Team.
|
||||
//
|
||||
// This software is licensed under the GNU General Public License, version 3 or higher.
|
||||
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
|
||||
// https://alliedmods.net/amxmodx-license
|
||||
|
||||
//
|
||||
// GeoIP Module
|
||||
//
|
||||
|
||||
#include "geoip_util.h"
|
||||
#include "geoip_natives.h"
|
||||
|
||||
char *stripPort(char *ip)
|
||||
{
|
||||
char *tmp = strchr(ip, ':');
|
||||
|
||||
if (tmp)
|
||||
{
|
||||
*tmp = '\0';
|
||||
}
|
||||
|
||||
return ip;
|
||||
}
|
||||
|
||||
const char* stristr(const char* str, const char* substr)
|
||||
{
|
||||
register char *needle = (char *)substr;
|
||||
register char *prevloc = (char *)str;
|
||||
register char *haystack = (char *)str;
|
||||
|
||||
while (*haystack)
|
||||
{
|
||||
if (tolower(*haystack) == tolower(*needle))
|
||||
{
|
||||
haystack++;
|
||||
|
||||
if (!*++needle)
|
||||
{
|
||||
return prevloc;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
haystack = ++prevloc;
|
||||
needle = (char *)substr;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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(&HandleDB, 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_aget_value(&lookup.entry, &entry_data, path);
|
||||
|
||||
if (!entry_data.has_data)
|
||||
{
|
||||
size_t i = 0;
|
||||
|
||||
// Dirty fall back to default language ("en") in case provided user's language is not localized.
|
||||
|
||||
// Searh "names" position.
|
||||
while (strcmp(path[i++], "names"));
|
||||
|
||||
// No localized entry or we use already default language.
|
||||
if (!*path[i] || !strcmp(path[i], "en"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Overwrite user's language.
|
||||
path[i] = "en";
|
||||
|
||||
// Try again.
|
||||
gai_error = mmdb_error = 0;
|
||||
MMDB_aget_value(&lookup.entry, &entry_data, path);
|
||||
|
||||
if (!entry_data.has_data)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
*result = entry_data;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const char *lookupString(const char *ip, const char **path, int *length)
|
||||
{
|
||||
static char buffer[256]; // This should be large enough for long name in UTF-8.
|
||||
MMDB_entry_data_s result;
|
||||
|
||||
if (!lookupByIp(ip, path, &result))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Let's avoid a crash in case we go over the buffer size.
|
||||
size_t maxLength = ke::Min((size_t)result.data_size, sizeof(buffer)-1);
|
||||
|
||||
// Strings from database are not null terminated.
|
||||
memcpy(buffer, result.utf8_string, maxLength);
|
||||
buffer[result.data_size] = '\0';
|
||||
|
||||
if (length)
|
||||
{
|
||||
*length = maxLength;
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
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 playerIndex)
|
||||
{
|
||||
static cvar_t *amxmodx_language = NULL;
|
||||
static cvar_t *amxmodx_cl_langs = NULL;
|
||||
|
||||
if (!amxmodx_language)
|
||||
amxmodx_language = CVAR_GET_POINTER("amx_language");
|
||||
|
||||
if (!amxmodx_cl_langs)
|
||||
amxmodx_cl_langs = CVAR_GET_POINTER("amx_client_languages");
|
||||
|
||||
if (playerIndex >= 0 && amxmodx_cl_langs && amxmodx_language)
|
||||
{
|
||||
const char *value;
|
||||
const char *lang;
|
||||
|
||||
if (playerIndex == 0 || amxmodx_cl_langs->value <= 0 || !MF_IsPlayerIngame(playerIndex))
|
||||
{
|
||||
value = amxmodx_language->string;
|
||||
}
|
||||
else
|
||||
{
|
||||
value = ENTITY_KEYVALUE(MF_GetPlayerEdict(playerIndex), "lang");
|
||||
}
|
||||
|
||||
if (value && *value)
|
||||
{
|
||||
for (size_t i = 0; i < LangList.length(); ++i)
|
||||
{
|
||||
lang = LangList.at(i).chars();
|
||||
|
||||
if (stristr(lang, value) != NULL)
|
||||
{
|
||||
return lang;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "en";
|
||||
}
|
34
dlls/geoip/geoip_util.h
Normal file
34
dlls/geoip/geoip_util.h
Normal file
|
@ -0,0 +1,34 @@
|
|||
// vim: set ts=4 sw=4 tw=99 noet:
|
||||
//
|
||||
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
|
||||
// Copyright (C) The AMX Mod X Development Team.
|
||||
//
|
||||
// This software is licensed under the GNU General Public License, version 3 or higher.
|
||||
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
|
||||
// https://alliedmods.net/amxmodx-license
|
||||
|
||||
//
|
||||
// GeoIP Module
|
||||
//
|
||||
|
||||
#ifndef _INCLUDE_GEOIPUTIL_H
|
||||
#define _INCLUDE_GEOIPUTIL_H
|
||||
|
||||
#include "geoip_main.h"
|
||||
|
||||
#if defined(WIN32)
|
||||
#define snprintf _snprintf
|
||||
#endif
|
||||
|
||||
char *stripPort(char *ip);
|
||||
|
||||
bool lookupByIp(const char *ip, const char **path, MMDB_entry_data_s *result);
|
||||
double lookupDouble(const char *ip, const char **path);
|
||||
const char *lookupString(const char *ip, const char **path, int *length = NULL);
|
||||
|
||||
int getContinentId(const char *code);
|
||||
const char *getLang(int playerIndex);
|
||||
|
||||
const char* stristr(const char* str, const char* substr);
|
||||
|
||||
#endif // _INCLUDE_GEOIPUTIL_H
|
|
@ -52,8 +52,8 @@
|
|||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<AdditionalIncludeDirectories>..\;..\sdk;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;GEOIP_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>..\;..\sdk;..\..\..\public\amtl;..\GeoIP2;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;GEOIP_EXPORTS;HAVE_STDINT_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<MinimalRebuild>true</MinimalRebuild>
|
||||
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
|
||||
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
|
||||
|
@ -75,8 +75,8 @@
|
|||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<AdditionalIncludeDirectories>..\;..\sdk;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;GEOIP_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>..\;..\sdk;..\..\..\public\amtl;..\GeoIP2;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;GEOIP_EXPORTS;HAVE_STDINT_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
<RuntimeTypeInfo>false</RuntimeTypeInfo>
|
||||
<PrecompiledHeader>
|
||||
|
@ -95,14 +95,19 @@
|
|||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\geoip_amxx.cpp" />
|
||||
<ClCompile Include="..\GeoIP.c" />
|
||||
<ClCompile Include="..\GeoIP2\maxminddb.cpp" />
|
||||
<ClCompile Include="..\geoip_main.cpp" />
|
||||
<ClCompile Include="..\geoip_natives.cpp" />
|
||||
<ClCompile Include="..\geoip_util.cpp" />
|
||||
<ClCompile Include="..\sdk\amxxmodule.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\geoip_amxx.h" />
|
||||
<ClInclude Include="..\svn_version.h" />
|
||||
<ClInclude Include="..\GeoIP.h" />
|
||||
<ClInclude Include="..\GeoIP2\maxminddb-compat-util.h" />
|
||||
<ClInclude Include="..\GeoIP2\maxminddb.h" />
|
||||
<ClInclude Include="..\GeoIP2\maxminddb_config.h" />
|
||||
<ClInclude Include="..\geoip_main.h" />
|
||||
<ClInclude Include="..\geoip_natives.h" />
|
||||
<ClInclude Include="..\geoip_util.h" />
|
||||
<ClInclude Include="..\sdk\moduleconfig.h" />
|
||||
<ClInclude Include="..\sdk\amxxmodule.h" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -9,9 +9,6 @@
|
|||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="GeoIP">
|
||||
<UniqueIdentifier>{2df41d4d-d80c-4557-88e6-0a8e902154d5}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Module SDK">
|
||||
<UniqueIdentifier>{a6d73610-c960-4557-87c5-2d1b137ed3b3}</UniqueIdentifier>
|
||||
</Filter>
|
||||
|
@ -21,34 +18,52 @@
|
|||
<Filter Include="Pawn Includes">
|
||||
<UniqueIdentifier>{f31e7815-11bd-4a86-899e-43f85dfdc067}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="GeoIP2">
|
||||
<UniqueIdentifier>{0bd4b9fb-f847-4fe5-af3e-9000fc854c5c}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\geoip_amxx.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\GeoIP.c">
|
||||
<Filter>GeoIP</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\sdk\amxxmodule.cpp">
|
||||
<Filter>Module SDK\SDK Base</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\GeoIP2\maxminddb.cpp">
|
||||
<Filter>GeoIP2</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\geoip_util.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\geoip_main.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\geoip_natives.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\geoip_amxx.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\svn_version.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\GeoIP.h">
|
||||
<Filter>GeoIP</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\sdk\moduleconfig.h">
|
||||
<Filter>Module SDK</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\sdk\amxxmodule.h">
|
||||
<Filter>Module SDK\SDK Base</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\GeoIP2\maxminddb.h">
|
||||
<Filter>GeoIP2</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\GeoIP2\maxminddb_config.h">
|
||||
<Filter>GeoIP2</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\GeoIP2\maxminddb-compat-util.h">
|
||||
<Filter>GeoIP2</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\geoip_util.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\geoip_natives.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\geoip_main.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\..\..\plugins\include\geoip.inc">
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
#define MODULE_LIBRARY "geoip"
|
||||
#define MODULE_LIBCLASS ""
|
||||
// If you want the module not to be reloaded on mapchange, remove / comment out the next line
|
||||
//#define MODULE_RELOAD_ON_MAPCHANGE
|
||||
#define MODULE_RELOAD_ON_MAPCHANGE
|
||||
|
||||
#ifdef __DATE__
|
||||
#define MODULE_DATE __DATE__
|
||||
|
@ -38,7 +38,7 @@
|
|||
#endif // __DATE__
|
||||
|
||||
// metamod plugin?
|
||||
//#define USE_METAMOD
|
||||
#define USE_METAMOD
|
||||
|
||||
// use memory manager/tester?
|
||||
// note that if you use this, you cannot construct/allocate
|
||||
|
|
|
@ -12,71 +12,224 @@
|
|||
//
|
||||
|
||||
#if defined geoip_included
|
||||
#endinput
|
||||
#endinput
|
||||
#endif
|
||||
#define _geoip_included
|
||||
|
||||
#if AMXX_VERSION_NUM >= 175
|
||||
#pragma reqlib geoip
|
||||
#if !defined AMXMODX_NOAUTOLOAD
|
||||
#pragma loadlib geoip
|
||||
#endif
|
||||
#pragma reqlib geoip
|
||||
#if !defined AMXMODX_NOAUTOLOAD
|
||||
#pragma loadlib geoip
|
||||
#endif
|
||||
#else
|
||||
#pragma library geoip
|
||||
#pragma library geoip
|
||||
#endif
|
||||
|
||||
/// IP addresses passed to these natives can contain ports, the ports will be ignored.
|
||||
/**
|
||||
* @global IP addresses passed to these natives can contain ports, the ports will be ignored.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Lookup the two character country code for a given IP address.
|
||||
* Look up the two character country code for a given IP address.
|
||||
* e.g: "US", "CA", etc.
|
||||
*
|
||||
* @param ip The IP address to lookup.
|
||||
* @param result The result buffer. If the lookup does not succeed, the buffer is not modified.
|
||||
* @return true on a successful lookup, false on a failed lookup.
|
||||
* @param ip The IP address to lookup.
|
||||
* @param result The result buffer. If the lookup does not succeed, the buffer is not modified.
|
||||
*
|
||||
* @return true on a successful lookup, false on a failed lookup.
|
||||
*/
|
||||
native bool:geoip_code2_ex(const ip[], result[3]);
|
||||
|
||||
/**
|
||||
* Lookup the three character country code for a given IP address.
|
||||
* Look up the three character country code for a given IP address.
|
||||
* e.g: "USA", "cAN", etc.
|
||||
*
|
||||
* @param ip The IP address to lookup.
|
||||
* @param result The result buffer. If the lookup does not succeed, the buffer is not modified.
|
||||
* @return true on a successful lookup, false on a failed lookup.
|
||||
* @param ip The IP address to lookup.
|
||||
* @param result The result buffer. If the lookup does not succeed, the buffer is not modified.
|
||||
*
|
||||
* @return true on a successful lookup, false on a failed lookup.
|
||||
*/
|
||||
native bool:geoip_code3_ex(const ip[], result[4]);
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* Lookup the two character country code for a given IP address.
|
||||
*
|
||||
* @note This native will overflow the buffer by one cell on an unknown ip lookup!
|
||||
* @note Use geoip_code2_ex instead!
|
||||
* @deprecated This native will overflow the buffer by one cell on an unknown ip lookup!
|
||||
* Use geoip_code2_ex instead.
|
||||
*
|
||||
* @param ip The IP address to lookup.
|
||||
* @param result The result buffer.
|
||||
* @param ip The IP address to lookup.
|
||||
* @param result The result buffer.
|
||||
*
|
||||
* @return 1 on a successful lookup, 0 otherwise.
|
||||
*/
|
||||
//#pragma deprecated Use geoip_code2_ex() instead.
|
||||
native geoip_code2(const ip[], ccode[3]);
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* Lookup the three character country code for a given IP address.
|
||||
*
|
||||
* @note This native will overflow the buffer by one cell on an unknown ip lookup!
|
||||
* @note Use geoip_code3_ex instead!
|
||||
* @deprecated This native will overflow the buffer by one cell on an unknown ip lookup!
|
||||
* Use geoip_code3_ex instead.
|
||||
*
|
||||
* @param ip The IP address to lookup.
|
||||
* @param result The result buffer.
|
||||
* @param ip The IP address to lookup.
|
||||
* @param result The result buffer.
|
||||
*
|
||||
* @return 1 on a successful lookup, 0 otherwise.
|
||||
*/
|
||||
//#pragma deprecated Use geoip_code3() instead.
|
||||
native geoip_code3(const ip[], result[4]);
|
||||
|
||||
/**
|
||||
* Lookup the full country name for the given IP address. Sets the buffer to "error" on
|
||||
* an unsuccessful lookup.
|
||||
*
|
||||
* @param ip The IP address to lookup.
|
||||
* @param result The result of the geoip lookup.
|
||||
* @param len The maximum length of the result buffer.
|
||||
* @param ip The IP address to lookup.
|
||||
* @param result The result of the geoip lookup.
|
||||
* @param len The maximum length of the result buffer.
|
||||
* @param id An optional player's index in order to return the result
|
||||
* in the player's language, if supported.
|
||||
* -1: the default language, which is english.
|
||||
* 0: the server language. You can use LANG_SERVER define.
|
||||
* >=1: the player's language.
|
||||
*
|
||||
* @return The result length on successful lookup, 0 otherwise.
|
||||
*/
|
||||
native geoip_country(const ip[], result[], len=45);
|
||||
native geoip_country(const ip[], result[], len, id = -1);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* The following natives require GeoIP City database, which can be retrieved from:
|
||||
* http://dev.maxmind.com/geoip/geoip2/geolite2/ (MaxMind DB binary)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Look up the full city name for the given IP address.
|
||||
*
|
||||
* @param ip The IP address to look up.
|
||||
* @param result The result of the geoip look up.
|
||||
* @param len The maximum length of the result buffer.
|
||||
* @param id An optional player's index in order to return the result
|
||||
* in the player's language, if supported.
|
||||
* -1: the default language, which is english.
|
||||
* 0: the server language. You can use LANG_SERVER define.
|
||||
* >=1: the player's language.
|
||||
*
|
||||
* @return The result length on successful lookup, 0 otherwise.
|
||||
*/
|
||||
native geoip_city(const ip[], result[], len, id = -1);
|
||||
|
||||
/**
|
||||
* Look up the region/state code for the given IP address.
|
||||
* e.g. "US-OH", "DE-HH", IT-82, "FR-U", etc.
|
||||
*
|
||||
* @param ip The IP address to look up.
|
||||
* @param result The result of the geoip look up.
|
||||
* @param len The maximum length of the result buffer.
|
||||
*
|
||||
* @return The result length on successful lookup, 0 otherwise.
|
||||
*/
|
||||
native geoip_region_code(const ip[], result[], len);
|
||||
|
||||
/**
|
||||
* Look up the full region/state name for the given IP address.
|
||||
*
|
||||
* @param ip The IP address to look up.
|
||||
* @param result The result of the geoip look up.
|
||||
* @param len The maximum length of the result buffer.
|
||||
* @param id An optional player's index in order to return the result
|
||||
* in the player's language, if supported.
|
||||
* -1: the default language, which is english.
|
||||
* 0: the server language. You can use LANG_SERVER define.
|
||||
* >=1: the player's language.
|
||||
*
|
||||
* @return The result length on successful lookup, 0 otherwise.
|
||||
*/
|
||||
native geoip_region_name(const ip[], result[], len, id = -1);
|
||||
|
||||
/**
|
||||
* Look up the full time zone for the given IP address.
|
||||
* e.g. America/Los_Angeles, Europe/Paris.
|
||||
*
|
||||
* @param ip The IP address to look up.
|
||||
* @param result The result of the geoip look up.
|
||||
* @param len The maximum length of the result buffer.
|
||||
*
|
||||
* @return The result length on successful lookup, 0 otherwise.
|
||||
*/
|
||||
native geoip_timezone(const ip[], result[], len);
|
||||
|
||||
/**
|
||||
* Look up the city's latitude for the given IP address.
|
||||
*
|
||||
* @param ip The IP address to look up.
|
||||
*
|
||||
* @return The result of the geoip look up, 0 if latitude is not found.
|
||||
*/
|
||||
native Float:geoip_latitude(const ip[]);
|
||||
|
||||
/**
|
||||
* Look up the city's longitude for the given IP address.
|
||||
*
|
||||
* @param ip The IP address to look up.
|
||||
*
|
||||
* @return The result of the geoip look up, 0 if longitude is not found.
|
||||
*/
|
||||
native Float:geoip_longitude(const 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.
|
||||
*/
|
||||
#define SYSTEM_METRIC 0 // kilometers
|
||||
#define SYSTEM_IMPERIAL 1 // statute miles
|
||||
|
||||
native Float:geoip_distance(Float:lat1, Float:lon1, Float:lat2, Float:lon2, system = SYSTEM_METRIC);
|
||||
|
||||
/**
|
||||
* Look up the continent code for a given IP address.
|
||||
*
|
||||
* @note The code can be retrieved as integer (See CONTINENT_* constants.) or string (2 characters).
|
||||
* @note Possible continent codes are AF, AN, AS, EU, NA, OC, SA for
|
||||
* Africa(1), Antarctica(2), Asia(3), Europe(4), North America(5), Oceania(6), South America(7).
|
||||
*
|
||||
* @param ip The IP address to look up.
|
||||
* @param result The result of the geoip look up.
|
||||
*
|
||||
* @return The continent id on successful lookup, 0 otherwise.
|
||||
*/
|
||||
enum Continent
|
||||
{
|
||||
CONTINENT_UNKNOW = 0,
|
||||
CONTINENT_AFRICA,
|
||||
CONTINENT_ANTARCTICA,
|
||||
CONTINENT_ASIA,
|
||||
CONTINENT_EUROPE,
|
||||
CONTINENT_NORTH_AMERICA,
|
||||
CONTINENT_OCEANIA,
|
||||
CONTINENT_SOUTH_AMERICA,
|
||||
};
|
||||
native Continent:geoip_continent_code(const ip[], result[3]);
|
||||
|
||||
/**
|
||||
* Look up the full continent name for the given IP address.
|
||||
*
|
||||
* @param ip The IP address to look up.
|
||||
* @param result The result of the geoip look up.
|
||||
* @param len The maximum length of the result buffer.
|
||||
* @param id An optional player's index in order to return the result
|
||||
* in the player's language, if supported.
|
||||
* -1: the default language, which is english.
|
||||
* 0: the server language. You can use LANG_SERVER define.
|
||||
* >=1: the player's language.
|
||||
*
|
||||
* @return The result length on successful lookup, 0 otherwise.
|
||||
*/
|
||||
native geoip_continent_name(const ip[], result[], len, id = -1);
|
||||
|
|
|
@ -304,7 +304,7 @@ for filename in scripting_files:
|
|||
|
||||
# Copy weird things.
|
||||
weirdfiles = [
|
||||
('dlls/geoip/GeoIP.dat', 'base/addons/amxmodx/data'),
|
||||
('dlls/geoip/GeoLite2-Country.mmdb', 'base/addons/amxmodx/data'),
|
||||
('plugins/esf/ESF_mod_tutorial.txt', 'esf/addons/amxmodx/scripting'),
|
||||
]
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user