updated the GEOIP API
This commit is contained in:
parent
5286672268
commit
b5498818a3
|
@ -1,37 +1,40 @@
|
||||||
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
|
||||||
/* GeoIP.c
|
/* GeoIP.c
|
||||||
*
|
*
|
||||||
* Copyright (C) 2003 MaxMind LLC All Rights Reserved.
|
* Copyright (C) 2006 MaxMind LLC
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU General Public
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
* License as published by the Free Software Foundation; either
|
* License as published by the Free Software Foundation; either
|
||||||
* version 2 of the License, or (at your option) any later version.
|
* 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,
|
* This library is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
* General Public License for more details.
|
* Lesser General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
* License along with this library; if not, write to the Free Software
|
* License along with this library; if not, write to the Free Software
|
||||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define GEOIPDATADIR ""
|
#define GEOIPDATADIR ""
|
||||||
|
|
||||||
#include "GeoIP.h"
|
#include "GeoIP.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h> /* For ntohl */
|
#include <netinet/in.h> /* For ntohl */
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#else
|
#else
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
#define snprintf _snprintf
|
||||||
#endif
|
#endif
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <sys/types.h> /* for fstat */
|
#include <sys/types.h> /* for fstat */
|
||||||
#include <sys/stat.h> /* for fstat */
|
#include <sys/stat.h> /* for fstat */
|
||||||
|
@ -40,6 +43,10 @@
|
||||||
#include <stdint.h> /* For uint32_t */
|
#include <stdint.h> /* For uint32_t */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef INADDR_NONE
|
||||||
|
#define INADDR_NONE -1
|
||||||
|
#endif
|
||||||
|
|
||||||
#define COUNTRY_BEGIN 16776960
|
#define COUNTRY_BEGIN 16776960
|
||||||
#define STATE_BEGIN_REV0 16700000
|
#define STATE_BEGIN_REV0 16700000
|
||||||
#define STATE_BEGIN_REV1 16000000
|
#define STATE_BEGIN_REV1 16000000
|
||||||
|
@ -58,19 +65,19 @@
|
||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
||||||
const char GeoIP_country_code[247][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","TP","TR","TT","TV","TW","TZ","UA","UG","UM","US","UY","UZ","VA","VC","VE","VG","VI","VN","VU","WF","WS","YE","YT","YU","ZA","ZM","ZR","ZW","A1","A2","O1"};
|
const char GeoIP_country_code[247][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","CS","ZA","ZM","ZR","ZW","A1","A2","O1"};
|
||||||
|
|
||||||
const char GeoIP_country_code3[247][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","TLS","TKM","TUN","TON","TUR","TTO","TUV","TWN","TZA","UKR","UGA","UM","USA","URY","UZB","VAT","VCT","VEN","VGB","VIR","VNM","VUT","WLF","WSM","YEM","YT","YUG","ZAF","ZMB","ZR","ZWE","A1","A2","O1"};
|
const char GeoIP_country_code3[247][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","SCG","ZAF","ZMB","ZR","ZWE","A1","A2","O1"};
|
||||||
|
|
||||||
const char * GeoIP_country_name[247] = {"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",
|
const char * GeoIP_country_name[247] = {"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, Occupied","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","East Timor","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","Yugoslavia","South Africa","Zambia","Zaire","Zimbabwe",
|
"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 and Montenegro","South Africa","Zambia","Zaire","Zimbabwe",
|
||||||
"Anonymous Proxy","Satellite Provider","Other"};
|
"Anonymous Proxy","Satellite Provider","Other"};
|
||||||
|
|
||||||
const char GeoIP_country_continent[247][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","AF","AF"};
|
const char GeoIP_country_continent[247][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","AF","AF"};
|
||||||
|
|
||||||
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 Netspeed Edition"};
|
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 *_full_path_to(const char *file_name) {
|
char *_GeoIP_full_path_to(const char *file_name) {
|
||||||
char *path = malloc(sizeof(char) * 1024);
|
char *path = malloc(sizeof(char) * 1024);
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
|
@ -91,7 +98,7 @@ char *_full_path_to(const char *file_name) {
|
||||||
}
|
}
|
||||||
*q = 0;
|
*q = 0;
|
||||||
memset(path, 0, sizeof(char) * 1024);
|
memset(path, 0, sizeof(char) * 1024);
|
||||||
sprintf(path, "%s/%s", buf, file_name);
|
snprintf(path, sizeof(char) * 1024 - 1, "%s/%s", buf, file_name);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return path;
|
return path;
|
||||||
|
@ -99,44 +106,26 @@ char *_full_path_to(const char *file_name) {
|
||||||
|
|
||||||
char ** GeoIPDBFileName = NULL;
|
char ** GeoIPDBFileName = NULL;
|
||||||
|
|
||||||
void _setup_dbfilename() {
|
void _GeoIP_setup_dbfilename() {
|
||||||
if (NULL == GeoIPDBFileName) {
|
if (NULL == GeoIPDBFileName) {
|
||||||
GeoIPDBFileName = malloc(sizeof(char *) * NUM_DB_TYPES);
|
GeoIPDBFileName = malloc(sizeof(char *) * NUM_DB_TYPES);
|
||||||
memset(GeoIPDBFileName, 0, sizeof(char *) * NUM_DB_TYPES);
|
memset(GeoIPDBFileName, 0, sizeof(char *) * NUM_DB_TYPES);
|
||||||
|
|
||||||
GeoIPDBFileName[GEOIP_COUNTRY_EDITION] = _full_path_to("GeoIP.dat");
|
GeoIPDBFileName[GEOIP_COUNTRY_EDITION] = _GeoIP_full_path_to("GeoIP.dat");
|
||||||
GeoIPDBFileName[GEOIP_REGION_EDITION_REV0] = _full_path_to("GeoIPRegion.dat");
|
GeoIPDBFileName[GEOIP_REGION_EDITION_REV0] = _GeoIP_full_path_to("GeoIPRegion.dat");
|
||||||
GeoIPDBFileName[GEOIP_REGION_EDITION_REV1] = _full_path_to("GeoIPRegion.dat");
|
GeoIPDBFileName[GEOIP_REGION_EDITION_REV1] = _GeoIP_full_path_to("GeoIPRegion.dat");
|
||||||
GeoIPDBFileName[GEOIP_CITY_EDITION_REV0] = _full_path_to("GeoIPCity.dat");
|
GeoIPDBFileName[GEOIP_CITY_EDITION_REV0] = _GeoIP_full_path_to("GeoIPCity.dat");
|
||||||
GeoIPDBFileName[GEOIP_CITY_EDITION_REV1] = _full_path_to("GeoIPCity.dat");
|
GeoIPDBFileName[GEOIP_CITY_EDITION_REV1] = _GeoIP_full_path_to("GeoIPCity.dat");
|
||||||
GeoIPDBFileName[GEOIP_ISP_EDITION] = _full_path_to("GeoIPISP.dat");
|
GeoIPDBFileName[GEOIP_ISP_EDITION] = _GeoIP_full_path_to("GeoIPISP.dat");
|
||||||
GeoIPDBFileName[GEOIP_ORG_EDITION] = _full_path_to("GeoIPOrg.dat");
|
GeoIPDBFileName[GEOIP_ORG_EDITION] = _GeoIP_full_path_to("GeoIPOrg.dat");
|
||||||
GeoIPDBFileName[GEOIP_PROXY_EDITION] = _full_path_to("GeoIPProxy.dat");
|
GeoIPDBFileName[GEOIP_PROXY_EDITION] = _GeoIP_full_path_to("GeoIPProxy.dat");
|
||||||
GeoIPDBFileName[GEOIP_ASNUM_EDITION] = _full_path_to("GeoIPASNum.dat");
|
GeoIPDBFileName[GEOIP_ASNUM_EDITION] = _GeoIP_full_path_to("GeoIPASNum.dat");
|
||||||
GeoIPDBFileName[GEOIP_NETSPEED_EDITION] = _full_path_to("GeoIPNetSpeed.dat");
|
GeoIPDBFileName[GEOIP_NETSPEED_EDITION] = _GeoIP_full_path_to("GeoIPNetSpeed.dat");
|
||||||
|
GeoIPDBFileName[GEOIP_DOMAIN_EDITION] = _GeoIP_full_path_to("GeoIPDomain.dat");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int _check_mtime(GeoIP *gi) {
|
static
|
||||||
struct stat buf;
|
|
||||||
|
|
||||||
if (gi->flags & GEOIP_CHECK_CACHE) {
|
|
||||||
if (fstat(fileno(gi->GeoIPDatabase), &buf) != -1) {
|
|
||||||
if (buf.st_mtime > gi->mtime) {
|
|
||||||
/* GeoIP Database file updated, reload database into memory cache */
|
|
||||||
if (realloc(gi->cache, buf.st_size) != 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",gi->file_path);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
gi->mtime = buf.st_mtime;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int _file_exists(const char *file_name) {
|
int _file_exists(const char *file_name) {
|
||||||
struct stat file_stat;
|
struct stat file_stat;
|
||||||
return( (stat(file_name, &file_stat) == 0) ? 1:0);
|
return( (stat(file_name, &file_stat) == 0) ? 1:0);
|
||||||
|
@ -154,6 +143,7 @@ int GeoIP_db_avail(int type) {
|
||||||
return _file_exists(filePath);
|
return _file_exists(filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static
|
||||||
void _setup_segments(GeoIP * gi) {
|
void _setup_segments(GeoIP * gi) {
|
||||||
int i, j;
|
int i, j;
|
||||||
unsigned char delim[3];
|
unsigned char delim[3];
|
||||||
|
@ -209,7 +199,54 @@ void _setup_segments(GeoIP * gi) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int _seek_record (GeoIP *gi, unsigned long ipnum) {
|
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);
|
||||||
|
_setup_segments(gi);
|
||||||
|
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;
|
int depth;
|
||||||
unsigned int x;
|
unsigned int x;
|
||||||
unsigned char stack_buffer[2 * MAX_RECORD_LENGTH];
|
unsigned char stack_buffer[2 * MAX_RECORD_LENGTH];
|
||||||
|
@ -221,13 +258,15 @@ unsigned int _seek_record (GeoIP *gi, unsigned long ipnum) {
|
||||||
|
|
||||||
_check_mtime(gi);
|
_check_mtime(gi);
|
||||||
for (depth = 31; depth >= 0; depth--) {
|
for (depth = 31; depth >= 0; depth--) {
|
||||||
if (gi->cache == NULL) {
|
if (gi->cache == NULL && gi->index_cache == NULL) {
|
||||||
/* read from disk */
|
/* read from disk */
|
||||||
fseek(gi->GeoIPDatabase, (long)gi->record_length * 2 * offset, SEEK_SET);
|
fseek(gi->GeoIPDatabase, (long)gi->record_length * 2 * offset, SEEK_SET);
|
||||||
fread(stack_buffer,gi->record_length,2,gi->GeoIPDatabase);
|
fread(stack_buffer,gi->record_length,2,gi->GeoIPDatabase);
|
||||||
} else {
|
} else if (gi->index_cache == NULL) {
|
||||||
/* simply point to record in memory */
|
/* simply point to record in memory */
|
||||||
buf = gi->cache + (long)gi->record_length * 2 *offset;
|
buf = gi->cache + (long)gi->record_length * 2 *offset;
|
||||||
|
} else {
|
||||||
|
buf = gi->index_cache + (long)gi->record_length * 2 * offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ipnum & (1 << depth)) {
|
if (ipnum & (1 << depth)) {
|
||||||
|
@ -279,7 +318,7 @@ unsigned int _seek_record (GeoIP *gi, unsigned long ipnum) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long _addr_to_num (const char *addr) {
|
unsigned long _GeoIP_addr_to_num (const char *addr) {
|
||||||
int i;
|
int i;
|
||||||
char tok[4];
|
char tok[4];
|
||||||
int octet;
|
int octet;
|
||||||
|
@ -321,7 +360,7 @@ GeoIP* GeoIP_open_type (int type, int flags) {
|
||||||
printf("Invalid database type %d\n", type);
|
printf("Invalid database type %d\n", type);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
_setup_dbfilename();
|
_GeoIP_setup_dbfilename();
|
||||||
filePath = GeoIPDBFileName[type];
|
filePath = GeoIPDBFileName[type];
|
||||||
if (filePath == NULL) {
|
if (filePath == NULL) {
|
||||||
printf("Invalid database type %d\n", type);
|
printf("Invalid database type %d\n", type);
|
||||||
|
@ -333,21 +372,21 @@ GeoIP* GeoIP_open_type (int type, int flags) {
|
||||||
|
|
||||||
GeoIP* GeoIP_new (int flags) {
|
GeoIP* GeoIP_new (int flags) {
|
||||||
GeoIP * gi;
|
GeoIP * gi;
|
||||||
_setup_dbfilename();
|
_GeoIP_setup_dbfilename();
|
||||||
gi = GeoIP_open (GeoIPDBFileName[GEOIP_COUNTRY_EDITION], flags);
|
gi = GeoIP_open (GeoIPDBFileName[GEOIP_COUNTRY_EDITION], flags);
|
||||||
return gi;
|
return gi;
|
||||||
}
|
}
|
||||||
|
|
||||||
GeoIP* GeoIP_open (const char * filename, int flags) {
|
GeoIP* GeoIP_open (const char * filename, int flags) {
|
||||||
struct stat buf;
|
struct stat buf;
|
||||||
GeoIP *gi = (GeoIP *)malloc(sizeof(GeoIP));
|
GeoIP * gi;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
WSADATA wsa;
|
WSADATA wsa;
|
||||||
if (WSAStartup(MAKEWORD(1, 1), &wsa) != 0)
|
if (WSAStartup(MAKEWORD(1, 1), &wsa) != 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
gi = (GeoIP *)malloc(sizeof(GeoIP));
|
||||||
if (gi == NULL)
|
if (gi == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
gi->file_path = malloc(sizeof(char) * (strlen(filename)+1));
|
gi->file_path = malloc(sizeof(char) * (strlen(filename)+1));
|
||||||
|
@ -378,19 +417,45 @@ GeoIP* GeoIP_open (const char * filename, int flags) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if (flags & GEOIP_CHECK_CACHE) {
|
||||||
|
if (fstat(fileno(gi->GeoIPDatabase), &buf) == -1) {
|
||||||
|
fprintf(stderr,"Error stating file %s\n",filename);
|
||||||
|
free(gi);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
gi->mtime = buf.st_mtime;
|
||||||
|
}
|
||||||
gi->cache = NULL;
|
gi->cache = NULL;
|
||||||
}
|
}
|
||||||
gi->flags = flags;
|
gi->flags = flags;
|
||||||
_setup_segments(gi);
|
_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->index_cache);
|
||||||
|
free(gi);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
gi->index_cache = NULL;
|
||||||
|
}
|
||||||
return gi;
|
return gi;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GeoIP_delete (GeoIP *gi) {
|
void GeoIP_delete (GeoIP *gi) {
|
||||||
|
if (gi == NULL )
|
||||||
|
return;
|
||||||
if (gi->GeoIPDatabase != NULL)
|
if (gi->GeoIPDatabase != NULL)
|
||||||
fclose(gi->GeoIPDatabase);
|
fclose(gi->GeoIPDatabase);
|
||||||
if (gi->cache != NULL)
|
if (gi->cache != NULL)
|
||||||
free(gi->cache);
|
free(gi->cache);
|
||||||
|
if (gi->index_cache != NULL)
|
||||||
|
free(gi->index_cache);
|
||||||
if (gi->file_path != NULL)
|
if (gi->file_path != NULL)
|
||||||
free(gi->file_path);
|
free(gi->file_path);
|
||||||
if (gi->databaseSegments != NULL)
|
if (gi->databaseSegments != NULL)
|
||||||
|
@ -416,16 +481,48 @@ const char *GeoIP_country_name_by_name (GeoIP* gi, const char *name) {
|
||||||
return (country_id > 0) ? GeoIP_country_name[country_id] : NULL;
|
return (country_id > 0) ? GeoIP_country_name[country_id] : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long lookupaddress (const char *host) {
|
unsigned long _GeoIP_lookupaddress (const char *host) {
|
||||||
unsigned long addr = inet_addr(host);
|
unsigned long addr = inet_addr(host);
|
||||||
struct hostent * phe;
|
struct hostent phe2;
|
||||||
if (addr == INADDR_NONE)
|
struct hostent * phe = &phe2;
|
||||||
{
|
char *buf = NULL;
|
||||||
|
int buflength = 16384;
|
||||||
|
int herr = 0;
|
||||||
|
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);
|
phe = gethostbyname(host);
|
||||||
if (!phe)
|
#endif
|
||||||
|
if (!phe || result != 0) {
|
||||||
|
free(buf);
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
addr = *((unsigned long *) phe->h_addr_list[0]);
|
addr = *((unsigned long *) phe->h_addr_list[0]);
|
||||||
}
|
}
|
||||||
|
#ifdef HAVE_GETHOSTBYNAME_R
|
||||||
|
free(buf);
|
||||||
|
#endif
|
||||||
return ntohl(addr);
|
return ntohl(addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -439,9 +536,9 @@ int GeoIP_id_by_name (GeoIP* gi, const char *name) {
|
||||||
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_COUNTRY_EDITION]);
|
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_COUNTRY_EDITION]);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (!(ipnum = lookupaddress(name)))
|
if (!(ipnum = _GeoIP_lookupaddress(name)))
|
||||||
return 0;
|
return 0;
|
||||||
ret = _seek_record(gi, ipnum) - COUNTRY_BEGIN;
|
ret = _GeoIP_seek_record(gi, ipnum) - COUNTRY_BEGIN;
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -466,6 +563,24 @@ const char *GeoIP_country_name_by_addr (GeoIP* gi, const char *addr) {
|
||||||
return GeoIP_country_name[country_id];
|
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) {
|
int GeoIP_country_id_by_addr (GeoIP* gi, const char *addr) {
|
||||||
return GeoIP_id_by_addr(gi, addr);
|
return GeoIP_id_by_addr(gi, addr);
|
||||||
}
|
}
|
||||||
|
@ -477,16 +592,36 @@ int GeoIP_country_id_by_name (GeoIP* gi, const char *host) {
|
||||||
int GeoIP_id_by_addr (GeoIP* gi, const char *addr) {
|
int GeoIP_id_by_addr (GeoIP* gi, const char *addr) {
|
||||||
unsigned long ipnum;
|
unsigned long ipnum;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if (addr == NULL) {
|
if (addr == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (gi->databaseType != GEOIP_COUNTRY_EDITION && gi->databaseType != GEOIP_PROXY_EDITION && gi->databaseType != GEOIP_NETSPEED_EDITION) {
|
if (gi->databaseType != GEOIP_COUNTRY_EDITION &&
|
||||||
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[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;
|
return 0;
|
||||||
}
|
}
|
||||||
ipnum = _addr_to_num(addr);
|
ipnum = _GeoIP_addr_to_num(addr);
|
||||||
ret = _seek_record(gi, ipnum) - COUNTRY_BEGIN;
|
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;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -543,7 +678,7 @@ void GeoIP_assign_region_by_inetaddr(GeoIP* gi, unsigned long inetaddr, GeoIPReg
|
||||||
* keep them) and clear any fields that are not set. */
|
* keep them) and clear any fields that are not set. */
|
||||||
memset(region, 0, sizeof(GeoIPRegion));
|
memset(region, 0, sizeof(GeoIPRegion));
|
||||||
|
|
||||||
seek_region = _seek_record(gi, ntohl(inetaddr));
|
seek_region = _GeoIP_seek_record(gi, ntohl(inetaddr));
|
||||||
|
|
||||||
if (gi->databaseType == GEOIP_REGION_EDITION_REV0) {
|
if (gi->databaseType == GEOIP_REGION_EDITION_REV0) {
|
||||||
/* Region Edition, pre June 2003 */
|
/* Region Edition, pre June 2003 */
|
||||||
|
@ -581,6 +716,7 @@ void GeoIP_assign_region_by_inetaddr(GeoIP* gi, unsigned long inetaddr, GeoIPReg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static
|
||||||
GeoIPRegion * _get_region(GeoIP* gi, unsigned long ipnum) {
|
GeoIPRegion * _get_region(GeoIP* gi, unsigned long ipnum) {
|
||||||
GeoIPRegion * region;
|
GeoIPRegion * region;
|
||||||
|
|
||||||
|
@ -601,7 +737,7 @@ GeoIPRegion * GeoIP_region_by_addr (GeoIP* gi, const char *addr) {
|
||||||
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_REGION_EDITION_REV1]);
|
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_REGION_EDITION_REV1]);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
ipnum = _addr_to_num(addr);
|
ipnum = _GeoIP_addr_to_num(addr);
|
||||||
return _get_region(gi, ipnum);
|
return _get_region(gi, ipnum);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -615,16 +751,26 @@ GeoIPRegion * GeoIP_region_by_name (GeoIP* gi, const char *name) {
|
||||||
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_REGION_EDITION_REV1]);
|
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_REGION_EDITION_REV1]);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (!(ipnum = lookupaddress(name)))
|
if (!(ipnum = _GeoIP_lookupaddress(name)))
|
||||||
return 0;
|
return 0;
|
||||||
return _get_region(gi, ipnum);
|
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) {
|
void GeoIPRegion_delete (GeoIPRegion *gir) {
|
||||||
free(gir);
|
free(gir);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* GeoIP Organization, ISP and AS Number Edition private method */
|
/* GeoIP Organization, ISP and AS Number Edition private method */
|
||||||
|
static
|
||||||
char *_get_name (GeoIP* gi, unsigned long ipnum) {
|
char *_get_name (GeoIP* gi, unsigned long ipnum) {
|
||||||
int seek_org;
|
int seek_org;
|
||||||
char buf[MAX_ORG_RECORD_LENGTH];
|
char buf[MAX_ORG_RECORD_LENGTH];
|
||||||
|
@ -638,7 +784,7 @@ char *_get_name (GeoIP* gi, unsigned long ipnum) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
seek_org = _seek_record(gi, ipnum);
|
seek_org = _GeoIP_seek_record(gi, ipnum);
|
||||||
if (seek_org == gi->databaseSegments[0])
|
if (seek_org == gi->databaseSegments[0])
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -657,12 +803,16 @@ char *_get_name (GeoIP* gi, unsigned long ipnum) {
|
||||||
return org_buf;
|
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) {
|
char *GeoIP_name_by_addr (GeoIP* gi, const char *addr) {
|
||||||
unsigned long ipnum;
|
unsigned long ipnum;
|
||||||
if (addr == NULL) {
|
if (addr == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
ipnum = _addr_to_num(addr);
|
ipnum = _GeoIP_addr_to_num(addr);
|
||||||
return _get_name(gi, ipnum);
|
return _get_name(gi, ipnum);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -671,11 +821,15 @@ char *GeoIP_name_by_name (GeoIP* gi, const char *name) {
|
||||||
if (name == NULL) {
|
if (name == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (!(ipnum = lookupaddress(name)))
|
if (!(ipnum = _GeoIP_lookupaddress(name)))
|
||||||
return 0;
|
return 0;
|
||||||
return _get_name(gi, ipnum);
|
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) {
|
char *GeoIP_org_by_addr (GeoIP* gi, const char *addr) {
|
||||||
return GeoIP_name_by_addr(gi, addr);
|
return GeoIP_name_by_addr(gi, addr);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,21 @@
|
||||||
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
|
||||||
/* GeoIP.h
|
/* GeoIP.h
|
||||||
*
|
*
|
||||||
* Copyright (C) 2003 MaxMind LLC
|
* Copyright (C) 2006 MaxMind LLC
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU General Public
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
* License as published by the Free Software Foundation; either
|
* License as published by the Free Software Foundation; either
|
||||||
* version 2.1 of the License, or (at your option) any later version.
|
* 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,
|
* This library is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
* General Public License for more details.
|
* Lesser General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
* License along with this library; if not, write to the Free Software
|
* License along with this library; if not, write to the Free Software
|
||||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef GEOIP_H
|
#ifndef GEOIP_H
|
||||||
|
@ -44,18 +44,17 @@ extern "C" {
|
||||||
#include <sys/types.h> /* for fstat */
|
#include <sys/types.h> /* for fstat */
|
||||||
#include <sys/stat.h> /* for fstat */
|
#include <sys/stat.h> /* for fstat */
|
||||||
|
|
||||||
//typedef int uint32_t;
|
|
||||||
|
|
||||||
#define SEGMENT_RECORD_LENGTH 3
|
#define SEGMENT_RECORD_LENGTH 3
|
||||||
#define STANDARD_RECORD_LENGTH 3
|
#define STANDARD_RECORD_LENGTH 3
|
||||||
#define ORG_RECORD_LENGTH 4
|
#define ORG_RECORD_LENGTH 4
|
||||||
#define MAX_RECORD_LENGTH 4
|
#define MAX_RECORD_LENGTH 4
|
||||||
#define NUM_DB_TYPES 16
|
#define NUM_DB_TYPES 20
|
||||||
|
|
||||||
typedef struct GeoIPTag {
|
typedef struct GeoIPTag {
|
||||||
FILE *GeoIPDatabase;
|
FILE *GeoIPDatabase;
|
||||||
char *file_path;
|
char *file_path;
|
||||||
unsigned char *cache;
|
unsigned char *cache;
|
||||||
|
unsigned char *index_cache;
|
||||||
unsigned int *databaseSegments;
|
unsigned int *databaseSegments;
|
||||||
char databaseType;
|
char databaseType;
|
||||||
time_t mtime;
|
time_t mtime;
|
||||||
|
@ -73,6 +72,7 @@ typedef enum {
|
||||||
GEOIP_STANDARD = 0,
|
GEOIP_STANDARD = 0,
|
||||||
GEOIP_MEMORY_CACHE = 1,
|
GEOIP_MEMORY_CACHE = 1,
|
||||||
GEOIP_CHECK_CACHE = 2,
|
GEOIP_CHECK_CACHE = 2,
|
||||||
|
GEOIP_INDEX_CACHE = 4,
|
||||||
} GeoIPOptions;
|
} GeoIPOptions;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
@ -86,6 +86,7 @@ typedef enum {
|
||||||
GEOIP_PROXY_EDITION = 8,
|
GEOIP_PROXY_EDITION = 8,
|
||||||
GEOIP_ASNUM_EDITION = 9,
|
GEOIP_ASNUM_EDITION = 9,
|
||||||
GEOIP_NETSPEED_EDITION = 10,
|
GEOIP_NETSPEED_EDITION = 10,
|
||||||
|
GEOIP_DOMAIN_EDITION = 11
|
||||||
} GeoIPDBTypes;
|
} GeoIPDBTypes;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
@ -131,6 +132,9 @@ 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_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_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_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 */
|
/* 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_addr (GeoIP* gi, const char *addr);
|
||||||
|
@ -141,9 +145,11 @@ GEOIP_API char *GeoIP_org_by_name (GeoIP* gi, const char *host);
|
||||||
|
|
||||||
GEOIP_API int GeoIP_id_by_addr (GeoIP* gi, const char *addr);
|
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_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_addr (GeoIP* gi, const char *addr);
|
||||||
GEOIP_API GeoIPRegion * GeoIP_region_by_name (GeoIP* gi, const char *host);
|
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 */
|
/* Warning - don't call this after GeoIP_assign_region_by_inetaddr calls */
|
||||||
GEOIP_API void GeoIPRegion_delete (GeoIPRegion *gir);
|
GEOIP_API void GeoIPRegion_delete (GeoIPRegion *gir);
|
||||||
|
@ -151,15 +157,13 @@ GEOIP_API void GeoIPRegion_delete (GeoIPRegion *gir);
|
||||||
GEOIP_API void GeoIP_assign_region_by_inetaddr(GeoIP* gi, unsigned long inetaddr, 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 */
|
/* 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_addr (GeoIP* gi, const char *addr);
|
||||||
GEOIP_API char *GeoIP_name_by_name (GeoIP* gi, const char *host);
|
GEOIP_API char *GeoIP_name_by_name (GeoIP* gi, const char *host);
|
||||||
|
|
||||||
GEOIP_API char *GeoIP_database_info (GeoIP* gi);
|
GEOIP_API char *GeoIP_database_info (GeoIP* gi);
|
||||||
GEOIP_API unsigned char GeoIP_database_edition (GeoIP* gi);
|
GEOIP_API unsigned char GeoIP_database_edition (GeoIP* gi);
|
||||||
|
|
||||||
GEOIP_API unsigned int _seek_record (GeoIP *gi, unsigned long ipnum);
|
|
||||||
GEOIP_API unsigned long _addr_to_num (const char *addr);
|
|
||||||
|
|
||||||
#ifdef BSD
|
#ifdef BSD
|
||||||
#define memcpy(dest, src, n) bcopy(src, dest, n)
|
#define memcpy(dest, src, n) bcopy(src, dest, n)
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -2773,7 +2773,7 @@ void ValidateMacros_DontCallThis_Smiley()
|
||||||
MF_FindLibrary(NULL, LibType_Class);
|
MF_FindLibrary(NULL, LibType_Class);
|
||||||
MF_AddLibraries(NULL, LibType_Class, NULL);
|
MF_AddLibraries(NULL, LibType_Class, NULL);
|
||||||
MF_RemoveLibraries(NULL);
|
MF_RemoveLibraries(NULL);
|
||||||
MF_OverrideNatives(NULL);
|
MF_OverrideNatives(NULL, NULL);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ static cell AMX_NATIVE_CALL amx_geoip_country(AMX *amx, cell *params)
|
||||||
void OnAmxxAttach()
|
void OnAmxxAttach()
|
||||||
{
|
{
|
||||||
char *path = MF_BuildPathname("%s/GeoIP.dat",MF_GetLocalInfo("amxx_datadir", "addons/amxmodx/data"));
|
char *path = MF_BuildPathname("%s/GeoIP.dat",MF_GetLocalInfo("amxx_datadir", "addons/amxmodx/data"));
|
||||||
gi = GeoIP_open(path, GEOIP_STANDARD);
|
gi = GeoIP_open(path, GEOIP_MEMORY_CACHE);
|
||||||
if (gi == NULL) {
|
if (gi == NULL) {
|
||||||
MF_Log("Failed to instantiate GeoIP!");
|
MF_Log("Failed to instantiate GeoIP!");
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user