fastgeoip 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +25 -0
- data/LICENSE +20 -0
- data/README.rdoc +11 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/ext/GeoIP.c +1640 -0
- data/ext/GeoIP.h +252 -0
- data/ext/GeoIPCity.c +297 -0
- data/ext/GeoIPCity.h +72 -0
- data/ext/GeoIP_internal.h +19 -0
- data/ext/extconf.h +5 -0
- data/ext/extconf.rb +58 -0
- data/ext/fastgeoip.c +59 -0
- data/ext/global.h +32 -0
- data/ext/md5.c +326 -0
- data/ext/md5.h +40 -0
- data/ext/mkmf.log +24 -0
- data/ext/regionName.c +13518 -0
- data/ext/ruby_geoip.h +15 -0
- data/ext/timeZone.c +941 -0
- data/ext/types.h +140 -0
- data/fastgeoip.gemspec +66 -0
- data/lib/fastgeoip.rb +0 -0
- data/test/helper.rb +10 -0
- metadata +92 -0
data/.document
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
## MAC OS
|
2
|
+
.DS_Store
|
3
|
+
|
4
|
+
## TEXTMATE
|
5
|
+
*.tmproj
|
6
|
+
tmtags
|
7
|
+
|
8
|
+
## EMACS
|
9
|
+
*~
|
10
|
+
\#*
|
11
|
+
.\#*
|
12
|
+
|
13
|
+
## VIM
|
14
|
+
*.swp
|
15
|
+
|
16
|
+
## PROJECT::GENERAL
|
17
|
+
coverage
|
18
|
+
rdoc
|
19
|
+
pkg
|
20
|
+
|
21
|
+
## PROJECT::SPECIFIC
|
22
|
+
*.o
|
23
|
+
*.bundle
|
24
|
+
Makefile
|
25
|
+
*.dSYM
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Martin Karlsch
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "fastgeoip"
|
8
|
+
gem.summary = "Wrapper for MaxMind GeoIP based on net/geoip"
|
9
|
+
gem.description = "A much longer description will follow ..."
|
10
|
+
gem.email = "martin@karlsch.com"
|
11
|
+
gem.homepage = "http://github.com/dynamix/fastgeoip"
|
12
|
+
gem.authors = ["Martin Karlsch"]
|
13
|
+
# gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
gem.extensions = ['ext/extconf.rb']
|
15
|
+
gem.require_path = "ext"
|
16
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'rake/testtask'
|
24
|
+
Rake::TestTask.new(:test) do |test|
|
25
|
+
test.libs << 'lib' << 'test'
|
26
|
+
test.pattern = 'test/**/test_*.rb'
|
27
|
+
test.verbose = true
|
28
|
+
end
|
29
|
+
|
30
|
+
begin
|
31
|
+
require 'rcov/rcovtask'
|
32
|
+
Rcov::RcovTask.new do |test|
|
33
|
+
test.libs << 'test'
|
34
|
+
test.pattern = 'test/**/test_*.rb'
|
35
|
+
test.verbose = true
|
36
|
+
end
|
37
|
+
rescue LoadError
|
38
|
+
task :rcov do
|
39
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
task :test => :check_dependencies
|
44
|
+
|
45
|
+
task :default => :test
|
46
|
+
|
47
|
+
require 'rake/rdoctask'
|
48
|
+
Rake::RDocTask.new do |rdoc|
|
49
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
50
|
+
|
51
|
+
rdoc.rdoc_dir = 'rdoc'
|
52
|
+
rdoc.title = "fastgeoip #{version}"
|
53
|
+
rdoc.rdoc_files.include('README*')
|
54
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
55
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/ext/GeoIP.c
ADDED
@@ -0,0 +1,1640 @@
|
|
1
|
+
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
|
2
|
+
/* GeoIP.c
|
3
|
+
*
|
4
|
+
* Copyright (C) 2006 MaxMind LLC
|
5
|
+
*
|
6
|
+
* This library is free software; you can redistribute it and/or
|
7
|
+
* modify it under the terms of the GNU Lesser General Public
|
8
|
+
* License as published by the Free Software Foundation; either
|
9
|
+
* version 2.1 of the License, or (at your option) any later version.
|
10
|
+
*
|
11
|
+
* This library is distributed in the hope that it will be useful,
|
12
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14
|
+
* Lesser General Public License for more details.
|
15
|
+
*
|
16
|
+
* You should have received a copy of the GNU Lesser General Public
|
17
|
+
* License along with this library; if not, write to the Free Software
|
18
|
+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
19
|
+
*/
|
20
|
+
|
21
|
+
#define GEOIPDATADIR "."
|
22
|
+
|
23
|
+
#include "GeoIP.h"
|
24
|
+
|
25
|
+
static geoipv6_t IPV6_NULL;
|
26
|
+
|
27
|
+
#if !defined(_WIN32)
|
28
|
+
#include <netdb.h>
|
29
|
+
#include <sys/mman.h>
|
30
|
+
#endif /* !defined(_WIN32) */
|
31
|
+
|
32
|
+
#include <errno.h>
|
33
|
+
#include <stdio.h>
|
34
|
+
#include <stdlib.h>
|
35
|
+
#include <string.h>
|
36
|
+
#include <assert.h>
|
37
|
+
#include <sys/types.h> /* for fstat */
|
38
|
+
#include <sys/stat.h> /* for fstat */
|
39
|
+
|
40
|
+
#ifdef HAVE_GETTIMEOFDAY
|
41
|
+
#include <sys/time.h> /* for gettimeofday */
|
42
|
+
#endif
|
43
|
+
|
44
|
+
#ifdef HAVE_STDINT_H
|
45
|
+
#include <stdint.h> /* For uint32_t */
|
46
|
+
#endif
|
47
|
+
|
48
|
+
#ifndef INADDR_NONE
|
49
|
+
#define INADDR_NONE -1
|
50
|
+
#endif
|
51
|
+
|
52
|
+
#define COUNTRY_BEGIN 16776960
|
53
|
+
#define STATE_BEGIN_REV0 16700000
|
54
|
+
#define STATE_BEGIN_REV1 16000000
|
55
|
+
#define STRUCTURE_INFO_MAX_SIZE 20
|
56
|
+
#define DATABASE_INFO_MAX_SIZE 100
|
57
|
+
#define MAX_ORG_RECORD_LENGTH 300
|
58
|
+
#define US_OFFSET 1
|
59
|
+
#define CANADA_OFFSET 677
|
60
|
+
#define WORLD_OFFSET 1353
|
61
|
+
#define FIPS_RANGE 360
|
62
|
+
|
63
|
+
#define CHECK_ERR(err, msg) { \
|
64
|
+
if (err != Z_OK) { \
|
65
|
+
fprintf(stderr, "%s error: %d\n", msg, err); \
|
66
|
+
exit(1); \
|
67
|
+
} \
|
68
|
+
}
|
69
|
+
|
70
|
+
const char GeoIP_country_code[253][3] = { "--","AP","EU","AD","AE","AF","AG","AI","AL","AM","AN",
|
71
|
+
"AO","AQ","AR","AS","AT","AU","AW","AZ","BA","BB",
|
72
|
+
"BD","BE","BF","BG","BH","BI","BJ","BM","BN","BO",
|
73
|
+
"BR","BS","BT","BV","BW","BY","BZ","CA","CC","CD",
|
74
|
+
"CF","CG","CH","CI","CK","CL","CM","CN","CO","CR",
|
75
|
+
"CU","CV","CX","CY","CZ","DE","DJ","DK","DM","DO",
|
76
|
+
"DZ","EC","EE","EG","EH","ER","ES","ET","FI","FJ",
|
77
|
+
"FK","FM","FO","FR","FX","GA","GB","GD","GE","GF",
|
78
|
+
"GH","GI","GL","GM","GN","GP","GQ","GR","GS","GT",
|
79
|
+
"GU","GW","GY","HK","HM","HN","HR","HT","HU","ID",
|
80
|
+
"IE","IL","IN","IO","IQ","IR","IS","IT","JM","JO",
|
81
|
+
"JP","KE","KG","KH","KI","KM","KN","KP","KR","KW",
|
82
|
+
"KY","KZ","LA","LB","LC","LI","LK","LR","LS","LT",
|
83
|
+
"LU","LV","LY","MA","MC","MD","MG","MH","MK","ML",
|
84
|
+
"MM","MN","MO","MP","MQ","MR","MS","MT","MU","MV",
|
85
|
+
"MW","MX","MY","MZ","NA","NC","NE","NF","NG","NI",
|
86
|
+
"NL","NO","NP","NR","NU","NZ","OM","PA","PE","PF",
|
87
|
+
"PG","PH","PK","PL","PM","PN","PR","PS","PT","PW",
|
88
|
+
"PY","QA","RE","RO","RU","RW","SA","SB","SC","SD",
|
89
|
+
"SE","SG","SH","SI","SJ","SK","SL","SM","SN","SO",
|
90
|
+
"SR","ST","SV","SY","SZ","TC","TD","TF","TG","TH",
|
91
|
+
"TJ","TK","TM","TN","TO","TL","TR","TT","TV","TW",
|
92
|
+
"TZ","UA","UG","UM","US","UY","UZ","VA","VC","VE",
|
93
|
+
"VG","VI","VN","VU","WF","WS","YE","YT","RS","ZA",
|
94
|
+
"ZM","ME","ZW","A1","A2","O1","AX","GG","IM","JE",
|
95
|
+
"BL","MF"};
|
96
|
+
|
97
|
+
static const unsigned num_GeoIP_countries = (unsigned)(sizeof(GeoIP_country_code)/sizeof(GeoIP_country_code[0]));
|
98
|
+
|
99
|
+
const char GeoIP_country_code3[253][4] = { "--","AP","EU","AND","ARE","AFG","ATG","AIA","ALB","ARM","ANT",
|
100
|
+
"AGO","AQ","ARG","ASM","AUT","AUS","ABW","AZE","BIH","BRB",
|
101
|
+
"BGD","BEL","BFA","BGR","BHR","BDI","BEN","BMU","BRN","BOL",
|
102
|
+
"BRA","BHS","BTN","BV","BWA","BLR","BLZ","CAN","CC","COD",
|
103
|
+
"CAF","COG","CHE","CIV","COK","CHL","CMR","CHN","COL","CRI",
|
104
|
+
"CUB","CPV","CX","CYP","CZE","DEU","DJI","DNK","DMA","DOM",
|
105
|
+
"DZA","ECU","EST","EGY","ESH","ERI","ESP","ETH","FIN","FJI",
|
106
|
+
"FLK","FSM","FRO","FRA","FX","GAB","GBR","GRD","GEO","GUF",
|
107
|
+
"GHA","GIB","GRL","GMB","GIN","GLP","GNQ","GRC","GS","GTM",
|
108
|
+
"GUM","GNB","GUY","HKG","HM","HND","HRV","HTI","HUN","IDN",
|
109
|
+
"IRL","ISR","IND","IO","IRQ","IRN","ISL","ITA","JAM","JOR",
|
110
|
+
"JPN","KEN","KGZ","KHM","KIR","COM","KNA","PRK","KOR","KWT",
|
111
|
+
"CYM","KAZ","LAO","LBN","LCA","LIE","LKA","LBR","LSO","LTU",
|
112
|
+
"LUX","LVA","LBY","MAR","MCO","MDA","MDG","MHL","MKD","MLI",
|
113
|
+
"MMR","MNG","MAC","MNP","MTQ","MRT","MSR","MLT","MUS","MDV",
|
114
|
+
"MWI","MEX","MYS","MOZ","NAM","NCL","NER","NFK","NGA","NIC",
|
115
|
+
"NLD","NOR","NPL","NRU","NIU","NZL","OMN","PAN","PER","PYF",
|
116
|
+
"PNG","PHL","PAK","POL","SPM","PCN","PRI","PSE","PRT","PLW",
|
117
|
+
"PRY","QAT","REU","ROU","RUS","RWA","SAU","SLB","SYC","SDN",
|
118
|
+
"SWE","SGP","SHN","SVN","SJM","SVK","SLE","SMR","SEN","SOM",
|
119
|
+
"SUR","STP","SLV","SYR","SWZ","TCA","TCD","TF","TGO","THA",
|
120
|
+
"TJK","TKL","TKM","TUN","TON","TLS","TUR","TTO","TUV","TWN",
|
121
|
+
"TZA","UKR","UGA","UM","USA","URY","UZB","VAT","VCT","VEN",
|
122
|
+
"VGB","VIR","VNM","VUT","WLF","WSM","YEM","YT","SRB","ZAF",
|
123
|
+
"ZMB","MNE","ZWE","A1","A2","O1","ALA","GGY","IMN","JEY",
|
124
|
+
"BLM","MAF"};
|
125
|
+
|
126
|
+
const char * GeoIP_country_name[253] = {"N/A","Asia/Pacific Region","Europe","Andorra","United Arab Emirates","Afghanistan","Antigua and Barbuda","Anguilla","Albania","Armenia","Netherlands Antilles",
|
127
|
+
"Angola","Antarctica","Argentina","American Samoa","Austria","Australia","Aruba","Azerbaijan","Bosnia and Herzegovina","Barbados",
|
128
|
+
"Bangladesh","Belgium","Burkina Faso","Bulgaria","Bahrain","Burundi","Benin","Bermuda","Brunei Darussalam","Bolivia",
|
129
|
+
"Brazil","Bahamas","Bhutan","Bouvet Island","Botswana","Belarus","Belize","Canada","Cocos (Keeling) Islands","Congo, The Democratic Republic of the",
|
130
|
+
"Central African Republic","Congo","Switzerland","Cote D'Ivoire","Cook Islands","Chile","Cameroon","China","Colombia","Costa Rica",
|
131
|
+
"Cuba","Cape Verde","Christmas Island","Cyprus","Czech Republic","Germany","Djibouti","Denmark","Dominica","Dominican Republic",
|
132
|
+
"Algeria","Ecuador","Estonia","Egypt","Western Sahara","Eritrea","Spain","Ethiopia","Finland","Fiji",
|
133
|
+
"Falkland Islands (Malvinas)","Micronesia, Federated States of","Faroe Islands","France","France, Metropolitan","Gabon","United Kingdom","Grenada","Georgia","French Guiana",
|
134
|
+
"Ghana","Gibraltar","Greenland","Gambia","Guinea","Guadeloupe","Equatorial Guinea","Greece","South Georgia and the South Sandwich Islands","Guatemala",
|
135
|
+
"Guam","Guinea-Bissau","Guyana","Hong Kong","Heard Island and McDonald Islands","Honduras","Croatia","Haiti","Hungary","Indonesia",
|
136
|
+
"Ireland","Israel","India","British Indian Ocean Territory","Iraq","Iran, Islamic Republic of","Iceland","Italy","Jamaica","Jordan",
|
137
|
+
"Japan","Kenya","Kyrgyzstan","Cambodia","Kiribati","Comoros","Saint Kitts and Nevis","Korea, Democratic People's Republic of","Korea, Republic of","Kuwait",
|
138
|
+
"Cayman Islands","Kazakhstan","Lao People's Democratic Republic","Lebanon","Saint Lucia","Liechtenstein","Sri Lanka","Liberia","Lesotho","Lithuania",
|
139
|
+
"Luxembourg","Latvia","Libyan Arab Jamahiriya","Morocco","Monaco","Moldova, Republic of","Madagascar","Marshall Islands","Macedonia","Mali",
|
140
|
+
"Myanmar","Mongolia","Macau","Northern Mariana Islands","Martinique","Mauritania","Montserrat","Malta","Mauritius","Maldives",
|
141
|
+
"Malawi","Mexico","Malaysia","Mozambique","Namibia","New Caledonia","Niger","Norfolk Island","Nigeria","Nicaragua",
|
142
|
+
"Netherlands","Norway","Nepal","Nauru","Niue","New Zealand","Oman","Panama","Peru","French Polynesia",
|
143
|
+
"Papua New Guinea","Philippines","Pakistan","Poland","Saint Pierre and Miquelon","Pitcairn Islands","Puerto Rico","Palestinian Territory","Portugal","Palau",
|
144
|
+
"Paraguay","Qatar","Reunion","Romania","Russian Federation","Rwanda","Saudi Arabia","Solomon Islands","Seychelles","Sudan",
|
145
|
+
"Sweden","Singapore","Saint Helena","Slovenia","Svalbard and Jan Mayen","Slovakia","Sierra Leone","San Marino","Senegal","Somalia","Suriname",
|
146
|
+
"Sao Tome and Principe","El Salvador","Syrian Arab Republic","Swaziland","Turks and Caicos Islands","Chad","French Southern Territories","Togo","Thailand",
|
147
|
+
"Tajikistan","Tokelau","Turkmenistan","Tunisia","Tonga","Timor-Leste","Turkey","Trinidad and Tobago","Tuvalu","Taiwan",
|
148
|
+
"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",
|
149
|
+
"Virgin Islands, British","Virgin Islands, U.S.","Vietnam","Vanuatu","Wallis and Futuna","Samoa","Yemen","Mayotte","Serbia","South Africa",
|
150
|
+
"Zambia","Montenegro","Zimbabwe","Anonymous Proxy","Satellite Provider","Other","Aland Islands","Guernsey","Isle of Man","Jersey",
|
151
|
+
"Saint Barthelemy","Saint Martin"};
|
152
|
+
|
153
|
+
/* Possible continent codes are AF, AS, EU, NA, OC, SA for Africa, Asia, Europe, North America, Oceania
|
154
|
+
and South America. */
|
155
|
+
|
156
|
+
const char GeoIP_country_continent[253][3] = {"--","AS","EU","EU","AS","AS","SA","SA","EU","AS","SA",
|
157
|
+
"AF","AN","SA","OC","EU","OC","SA","AS","EU","SA",
|
158
|
+
"AS","EU","AF","EU","AS","AF","AF","SA","AS","SA",
|
159
|
+
"SA","SA","AS","AF","AF","EU","SA","NA","AS","AF",
|
160
|
+
"AF","AF","EU","AF","OC","SA","AF","AS","SA","SA",
|
161
|
+
"SA","AF","AS","AS","EU","EU","AF","EU","SA","SA",
|
162
|
+
"AF","SA","EU","AF","AF","AF","EU","AF","EU","OC",
|
163
|
+
"SA","OC","EU","EU","EU","AF","EU","SA","AS","SA",
|
164
|
+
"AF","EU","SA","AF","AF","SA","AF","EU","SA","SA",
|
165
|
+
"OC","AF","SA","AS","AF","SA","EU","SA","EU","AS",
|
166
|
+
"EU","AS","AS","AS","AS","AS","EU","EU","SA","AS",
|
167
|
+
"AS","AF","AS","AS","OC","AF","SA","AS","AS","AS",
|
168
|
+
"SA","AS","AS","AS","SA","EU","AS","AF","AF","EU",
|
169
|
+
"EU","EU","AF","AF","EU","EU","AF","OC","EU","AF",
|
170
|
+
"AS","AS","AS","OC","SA","AF","SA","EU","AF","AS",
|
171
|
+
"AF","NA","AS","AF","AF","OC","AF","OC","AF","SA",
|
172
|
+
"EU","EU","AS","OC","OC","OC","AS","SA","SA","OC",
|
173
|
+
"OC","AS","AS","EU","SA","OC","SA","AS","EU","OC",
|
174
|
+
"SA","AS","AF","EU","AS","AF","AS","OC","AF","AF",
|
175
|
+
"EU","AS","AF","EU","EU","EU","AF","EU","AF","AF",
|
176
|
+
"SA","AF","SA","AS","AF","SA","AF","AF","AF","AS",
|
177
|
+
"AS","OC","AS","AF","OC","AS","AS","SA","OC","AS",
|
178
|
+
"AF","EU","AF","OC","NA","SA","AS","EU","SA","SA",
|
179
|
+
"SA","SA","AS","OC","OC","OC","AS","AF","EU","AF",
|
180
|
+
"AF","EU","AF","--","--","--","EU","EU","EU","EU",
|
181
|
+
"SA","SA"};
|
182
|
+
|
183
|
+
geoipv6_t _GeoIP_lookupaddress_v6 (const char *host);
|
184
|
+
|
185
|
+
#if defined(_WIN32)
|
186
|
+
/* http://www.mail-archive.com/users@ipv6.org/msg02107.html */
|
187
|
+
static const char * _GeoIP_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
|
188
|
+
{
|
189
|
+
if (af == AF_INET)
|
190
|
+
{
|
191
|
+
struct sockaddr_in in;
|
192
|
+
memset(&in, 0, sizeof(in));
|
193
|
+
in.sin_family = AF_INET;
|
194
|
+
memcpy(&in.sin_addr, src, sizeof(struct in_addr));
|
195
|
+
getnameinfo((struct sockaddr *)&in, sizeof(struct
|
196
|
+
sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
|
197
|
+
return dst;
|
198
|
+
}
|
199
|
+
else if (af == AF_INET6)
|
200
|
+
{
|
201
|
+
struct sockaddr_in6 in;
|
202
|
+
memset(&in, 0, sizeof(in));
|
203
|
+
in.sin6_family = AF_INET6;
|
204
|
+
memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
|
205
|
+
getnameinfo((struct sockaddr *)&in, sizeof(struct
|
206
|
+
sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
|
207
|
+
return dst;
|
208
|
+
}
|
209
|
+
return NULL;
|
210
|
+
}
|
211
|
+
|
212
|
+
static int _GeoIP_inet_pton(int af, const char *src, void *dst)
|
213
|
+
{
|
214
|
+
struct addrinfo hints, *res, *ressave;
|
215
|
+
|
216
|
+
memset(&hints, 0, sizeof(struct addrinfo));
|
217
|
+
hints.ai_family = af;
|
218
|
+
|
219
|
+
if (getaddrinfo(src, NULL, &hints, &res) != 0)
|
220
|
+
{
|
221
|
+
fprintf(stderr, "Couldn't resolve host %s\n", src);
|
222
|
+
return -1;
|
223
|
+
}
|
224
|
+
|
225
|
+
ressave = res;
|
226
|
+
|
227
|
+
while (res)
|
228
|
+
{
|
229
|
+
memcpy(dst, res->ai_addr, res->ai_addrlen);
|
230
|
+
res = res->ai_next;
|
231
|
+
}
|
232
|
+
|
233
|
+
freeaddrinfo(ressave);
|
234
|
+
return 0;
|
235
|
+
}
|
236
|
+
#else
|
237
|
+
static int _GeoIP_inet_pton(int af, const char *src, void *dst) {
|
238
|
+
return inet_pton(af, src, dst);
|
239
|
+
}
|
240
|
+
static const char * _GeoIP_inet_ntop(int af, const void *src, char *dst, socklen_t cnt) {
|
241
|
+
return inet_ntop(af, src, dst, cnt);
|
242
|
+
}
|
243
|
+
|
244
|
+
#endif /* defined(_WIN32) */
|
245
|
+
|
246
|
+
|
247
|
+
int __GEOIP_V6_IS_NULL(geoipv6_t v6) {
|
248
|
+
int i;
|
249
|
+
for (i=0;i<16;i++) {
|
250
|
+
if (v6.s6_addr[i])
|
251
|
+
return 0;
|
252
|
+
}
|
253
|
+
return 1;
|
254
|
+
}
|
255
|
+
|
256
|
+
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", "GeoIP Country V6 Edition"};
|
257
|
+
|
258
|
+
char * custom_directory = NULL;
|
259
|
+
|
260
|
+
void GeoIP_setup_custom_directory (char * dir) {
|
261
|
+
custom_directory = dir;
|
262
|
+
}
|
263
|
+
|
264
|
+
char *_GeoIP_full_path_to(const char *file_name) {
|
265
|
+
int len;
|
266
|
+
char *path = malloc(sizeof(char) * 1024);
|
267
|
+
|
268
|
+
if (custom_directory == NULL){
|
269
|
+
#if !defined(_WIN32)
|
270
|
+
memset(path, 0, sizeof(char) * 1024);
|
271
|
+
snprintf(path, sizeof(char) * 1024 - 1, "%s/%s", GEOIPDATADIR, file_name);
|
272
|
+
#else
|
273
|
+
char buf[MAX_PATH], *p, *q = NULL;
|
274
|
+
memset(buf, 0, sizeof(buf));
|
275
|
+
len = GetModuleFileNameA(GetModuleHandle(NULL), buf, sizeof(buf) - 1);
|
276
|
+
for (p = buf + len; p > buf; p--)
|
277
|
+
if (*p == '\\')
|
278
|
+
{
|
279
|
+
if (!q)
|
280
|
+
q = p;
|
281
|
+
else
|
282
|
+
*p = '/';
|
283
|
+
}
|
284
|
+
*q = 0;
|
285
|
+
memset(path, 0, sizeof(char) * 1024);
|
286
|
+
snprintf(path, sizeof(char) * 1024 - 1, "%s/%s", buf, file_name);
|
287
|
+
#endif
|
288
|
+
} else {
|
289
|
+
len = strlen(custom_directory);
|
290
|
+
if (custom_directory[len-1] != '/') {
|
291
|
+
snprintf(path, sizeof(char) * 1024 - 1, "%s/%s",custom_directory, file_name);
|
292
|
+
} else {
|
293
|
+
snprintf(path, sizeof(char) * 1024 - 1, "%s%s", custom_directory, file_name);
|
294
|
+
}
|
295
|
+
}
|
296
|
+
return path;
|
297
|
+
}
|
298
|
+
|
299
|
+
char ** GeoIPDBFileName = NULL;
|
300
|
+
|
301
|
+
void _GeoIP_setup_dbfilename() {
|
302
|
+
if (NULL == GeoIPDBFileName) {
|
303
|
+
GeoIPDBFileName = malloc(sizeof(char *) * NUM_DB_TYPES);
|
304
|
+
memset(GeoIPDBFileName, 0, sizeof(char *) * NUM_DB_TYPES);
|
305
|
+
|
306
|
+
GeoIPDBFileName[GEOIP_COUNTRY_EDITION] = _GeoIP_full_path_to("GeoIP.dat");
|
307
|
+
GeoIPDBFileName[GEOIP_REGION_EDITION_REV0] = _GeoIP_full_path_to("GeoIPRegion.dat");
|
308
|
+
GeoIPDBFileName[GEOIP_REGION_EDITION_REV1] = _GeoIP_full_path_to("GeoIPRegion.dat");
|
309
|
+
GeoIPDBFileName[GEOIP_CITY_EDITION_REV0] = _GeoIP_full_path_to("GeoIPCity.dat");
|
310
|
+
GeoIPDBFileName[GEOIP_CITY_EDITION_REV1] = _GeoIP_full_path_to("GeoIPCity.dat");
|
311
|
+
GeoIPDBFileName[GEOIP_ISP_EDITION] = _GeoIP_full_path_to("GeoIPISP.dat");
|
312
|
+
GeoIPDBFileName[GEOIP_ORG_EDITION] = _GeoIP_full_path_to("GeoIPOrg.dat");
|
313
|
+
GeoIPDBFileName[GEOIP_PROXY_EDITION] = _GeoIP_full_path_to("GeoIPProxy.dat");
|
314
|
+
GeoIPDBFileName[GEOIP_ASNUM_EDITION] = _GeoIP_full_path_to("GeoIPASNum.dat");
|
315
|
+
GeoIPDBFileName[GEOIP_NETSPEED_EDITION] = _GeoIP_full_path_to("GeoIPNetSpeed.dat");
|
316
|
+
GeoIPDBFileName[GEOIP_DOMAIN_EDITION] = _GeoIP_full_path_to("GeoIPDomain.dat");
|
317
|
+
GeoIPDBFileName[GEOIP_COUNTRY_EDITION_V6] = _GeoIP_full_path_to("GeoIPv6.dat");
|
318
|
+
}
|
319
|
+
}
|
320
|
+
|
321
|
+
static
|
322
|
+
int _file_exists(const char *file_name) {
|
323
|
+
struct stat file_stat;
|
324
|
+
return( (stat(file_name, &file_stat) == 0) ? 1:0);
|
325
|
+
}
|
326
|
+
|
327
|
+
int GeoIP_db_avail(int type) {
|
328
|
+
const char * filePath;
|
329
|
+
if (type < 0 || type >= NUM_DB_TYPES) {
|
330
|
+
return 0;
|
331
|
+
}
|
332
|
+
_GeoIP_setup_dbfilename();
|
333
|
+
filePath = GeoIPDBFileName[type];
|
334
|
+
if (NULL == filePath) {
|
335
|
+
return 0;
|
336
|
+
}
|
337
|
+
return _file_exists(filePath);
|
338
|
+
}
|
339
|
+
|
340
|
+
static
|
341
|
+
void _setup_segments(GeoIP * gi) {
|
342
|
+
int i, j;
|
343
|
+
unsigned char delim[3];
|
344
|
+
unsigned char buf[SEGMENT_RECORD_LENGTH];
|
345
|
+
size_t silence;
|
346
|
+
|
347
|
+
gi->databaseSegments = NULL;
|
348
|
+
|
349
|
+
/* default to GeoIP Country Edition */
|
350
|
+
gi->databaseType = GEOIP_COUNTRY_EDITION;
|
351
|
+
gi->record_length = STANDARD_RECORD_LENGTH;
|
352
|
+
fseek(gi->GeoIPDatabase, -3l, SEEK_END);
|
353
|
+
for (i = 0; i < STRUCTURE_INFO_MAX_SIZE; i++) {
|
354
|
+
silence = fread(delim, 1, 3, gi->GeoIPDatabase);
|
355
|
+
if (delim[0] == 255 && delim[1] == 255 && delim[2] == 255) {
|
356
|
+
silence = fread(&gi->databaseType, 1, 1, gi->GeoIPDatabase);
|
357
|
+
if (gi->databaseType >= 106) {
|
358
|
+
/* backwards compatibility with databases from April 2003 and earlier */
|
359
|
+
gi->databaseType -= 105;
|
360
|
+
}
|
361
|
+
|
362
|
+
if (gi->databaseType == GEOIP_REGION_EDITION_REV0) {
|
363
|
+
/* Region Edition, pre June 2003 */
|
364
|
+
gi->databaseSegments = malloc(sizeof(int));
|
365
|
+
gi->databaseSegments[0] = STATE_BEGIN_REV0;
|
366
|
+
} else if (gi->databaseType == GEOIP_REGION_EDITION_REV1) {
|
367
|
+
/* Region Edition, post June 2003 */
|
368
|
+
gi->databaseSegments = malloc(sizeof(int));
|
369
|
+
gi->databaseSegments[0] = STATE_BEGIN_REV1;
|
370
|
+
} else if (gi->databaseType == GEOIP_CITY_EDITION_REV0 ||
|
371
|
+
gi->databaseType == GEOIP_CITY_EDITION_REV1 ||
|
372
|
+
gi->databaseType == GEOIP_ORG_EDITION ||
|
373
|
+
gi->databaseType == GEOIP_ISP_EDITION ||
|
374
|
+
gi->databaseType == GEOIP_ASNUM_EDITION) {
|
375
|
+
/* City/Org Editions have two segments, read offset of second segment */
|
376
|
+
gi->databaseSegments = malloc(sizeof(int));
|
377
|
+
gi->databaseSegments[0] = 0;
|
378
|
+
silence = fread(buf, SEGMENT_RECORD_LENGTH, 1, gi->GeoIPDatabase);
|
379
|
+
for (j = 0; j < SEGMENT_RECORD_LENGTH; j++) {
|
380
|
+
gi->databaseSegments[0] += (buf[j] << (j * 8));
|
381
|
+
}
|
382
|
+
if (gi->databaseType == GEOIP_ORG_EDITION ||
|
383
|
+
gi->databaseType == GEOIP_ISP_EDITION)
|
384
|
+
gi->record_length = ORG_RECORD_LENGTH;
|
385
|
+
}
|
386
|
+
break;
|
387
|
+
} else {
|
388
|
+
fseek(gi->GeoIPDatabase, -4l, SEEK_CUR);
|
389
|
+
}
|
390
|
+
}
|
391
|
+
if (gi->databaseType == GEOIP_COUNTRY_EDITION ||
|
392
|
+
gi->databaseType == GEOIP_PROXY_EDITION ||
|
393
|
+
gi->databaseType == GEOIP_NETSPEED_EDITION ||
|
394
|
+
gi->databaseType == GEOIP_COUNTRY_EDITION_V6 ) {
|
395
|
+
gi->databaseSegments = malloc(sizeof(int));
|
396
|
+
gi->databaseSegments[0] = COUNTRY_BEGIN;
|
397
|
+
}
|
398
|
+
}
|
399
|
+
|
400
|
+
static
|
401
|
+
int _check_mtime(GeoIP *gi) {
|
402
|
+
struct stat buf;
|
403
|
+
#if !defined(_WIN32)
|
404
|
+
struct timeval t;
|
405
|
+
|
406
|
+
/* stat only has second granularity, so don't
|
407
|
+
call it more than once a second */
|
408
|
+
gettimeofday(&t, NULL);
|
409
|
+
if (t.tv_sec == gi->last_mtime_check){
|
410
|
+
return 0;
|
411
|
+
}
|
412
|
+
gi->last_mtime_check = t.tv_sec;
|
413
|
+
#else /* !defined(_WIN32) */
|
414
|
+
FILETIME ft;
|
415
|
+
ULONGLONG t;
|
416
|
+
|
417
|
+
/* stat only has second granularity, so don't
|
418
|
+
call it more than once a second */
|
419
|
+
GetSystemTimeAsFileTime(&ft);
|
420
|
+
t = FILETIME_TO_USEC(ft) / 1000 / 1000;
|
421
|
+
if (t == gi->last_mtime_check){
|
422
|
+
return 0;
|
423
|
+
}
|
424
|
+
gi->last_mtime_check = t;
|
425
|
+
#endif /* !defined(_WIN32) */
|
426
|
+
|
427
|
+
|
428
|
+
if (gi->flags & GEOIP_CHECK_CACHE) {
|
429
|
+
if (stat(gi->file_path, &buf) != -1) {
|
430
|
+
if (buf.st_mtime != gi->mtime) {
|
431
|
+
/* GeoIP Database file updated */
|
432
|
+
if (gi->flags & (GEOIP_MEMORY_CACHE | GEOIP_MMAP_CACHE)) {
|
433
|
+
if ( gi->flags & GEOIP_MMAP_CACHE) {
|
434
|
+
#if !defined(_WIN32)
|
435
|
+
/* MMAP is only avail on UNIX */
|
436
|
+
munmap(gi->cache, gi->size);
|
437
|
+
gi->cache = NULL;
|
438
|
+
#endif
|
439
|
+
} else {
|
440
|
+
/* reload database into memory cache */
|
441
|
+
if ((gi->cache = (unsigned char*) realloc(gi->cache, buf.st_size)) == NULL) {
|
442
|
+
fprintf(stderr,"Out of memory when reloading %s\n",gi->file_path);
|
443
|
+
return -1;
|
444
|
+
}
|
445
|
+
}
|
446
|
+
}
|
447
|
+
/* refresh filehandle */
|
448
|
+
fclose(gi->GeoIPDatabase);
|
449
|
+
gi->GeoIPDatabase = fopen(gi->file_path,"rb");
|
450
|
+
if (gi->GeoIPDatabase == NULL) {
|
451
|
+
fprintf(stderr,"Error Opening file %s when reloading\n",gi->file_path);
|
452
|
+
return -1;
|
453
|
+
}
|
454
|
+
gi->mtime = buf.st_mtime;
|
455
|
+
gi->size = buf.st_size;
|
456
|
+
|
457
|
+
if ( gi->flags & GEOIP_MMAP_CACHE) {
|
458
|
+
#if defined(_WIN32)
|
459
|
+
fprintf(stderr, "GEOIP_MMAP_CACHE is not supported on WIN32\n");
|
460
|
+
gi->cache = 0;
|
461
|
+
return -1;
|
462
|
+
#else
|
463
|
+
gi->cache = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fileno(gi->GeoIPDatabase), 0);
|
464
|
+
if ( gi->cache == MAP_FAILED ) {
|
465
|
+
|
466
|
+
fprintf(stderr,"Error remapping file %s when reloading\n",gi->file_path);
|
467
|
+
|
468
|
+
gi->cache = 0;
|
469
|
+
return -1;
|
470
|
+
}
|
471
|
+
#endif
|
472
|
+
} else if ( gi->flags & GEOIP_MEMORY_CACHE ) {
|
473
|
+
if (fread(gi->cache, sizeof(unsigned char), buf.st_size, gi->GeoIPDatabase) != (size_t) buf.st_size) {
|
474
|
+
fprintf(stderr,"Error reading file %s when reloading\n",gi->file_path);
|
475
|
+
return -1;
|
476
|
+
}
|
477
|
+
}
|
478
|
+
|
479
|
+
if (gi->databaseSegments != NULL) {
|
480
|
+
free(gi->databaseSegments);
|
481
|
+
gi->databaseSegments = NULL;
|
482
|
+
}
|
483
|
+
_setup_segments(gi);
|
484
|
+
if (gi->databaseSegments == NULL) {
|
485
|
+
fprintf(stderr, "Error reading file %s -- corrupt\n", gi->file_path);
|
486
|
+
return -1;
|
487
|
+
}
|
488
|
+
if (gi->flags & GEOIP_INDEX_CACHE) {
|
489
|
+
gi->index_cache = (unsigned char *) realloc(gi->index_cache, sizeof(unsigned char) * ((gi->databaseSegments[0] * (long)gi->record_length * 2)));
|
490
|
+
if (gi->index_cache != NULL) {
|
491
|
+
fseek(gi->GeoIPDatabase, 0, SEEK_SET);
|
492
|
+
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)) {
|
493
|
+
fprintf(stderr,"Error reading file %s where reloading\n",gi->file_path);
|
494
|
+
return -1;
|
495
|
+
}
|
496
|
+
}
|
497
|
+
}
|
498
|
+
}
|
499
|
+
}
|
500
|
+
}
|
501
|
+
return 0;
|
502
|
+
}
|
503
|
+
|
504
|
+
#define ADDR_STR_LEN (8 * 4 + 7 + 1)
|
505
|
+
unsigned int _GeoIP_seek_record_v6 (GeoIP *gi, geoipv6_t ipnum) {
|
506
|
+
int depth;
|
507
|
+
char paddr[ADDR_STR_LEN];
|
508
|
+
unsigned int x;
|
509
|
+
unsigned char stack_buffer[2 * MAX_RECORD_LENGTH];
|
510
|
+
const unsigned char *buf = (gi->cache == NULL) ? stack_buffer : NULL;
|
511
|
+
unsigned int offset = 0;
|
512
|
+
|
513
|
+
const unsigned char * p;
|
514
|
+
int j;
|
515
|
+
size_t silence;
|
516
|
+
|
517
|
+
_check_mtime(gi);
|
518
|
+
for (depth = 127; depth >= 0; depth--) {
|
519
|
+
if (gi->cache == NULL && gi->index_cache == NULL) {
|
520
|
+
/* read from disk */
|
521
|
+
fseek(gi->GeoIPDatabase, (long)gi->record_length * 2 * offset, SEEK_SET);
|
522
|
+
silence = fread(stack_buffer,gi->record_length,2,gi->GeoIPDatabase);
|
523
|
+
} else if (gi->index_cache == NULL) {
|
524
|
+
/* simply point to record in memory */
|
525
|
+
buf = gi->cache + (long)gi->record_length * 2 *offset;
|
526
|
+
} else {
|
527
|
+
buf = gi->index_cache + (long)gi->record_length * 2 * offset;
|
528
|
+
}
|
529
|
+
|
530
|
+
if (GEOIP_CHKBIT_V6(depth, ipnum.s6_addr )) {
|
531
|
+
/* Take the right-hand branch */
|
532
|
+
if ( gi->record_length == 3 ) {
|
533
|
+
/* Most common case is completely unrolled and uses constants. */
|
534
|
+
x = (buf[3*1 + 0] << (0*8))
|
535
|
+
+ (buf[3*1 + 1] << (1*8))
|
536
|
+
+ (buf[3*1 + 2] << (2*8));
|
537
|
+
|
538
|
+
} else {
|
539
|
+
/* General case */
|
540
|
+
j = gi->record_length;
|
541
|
+
p = &buf[2*j];
|
542
|
+
x = 0;
|
543
|
+
do {
|
544
|
+
x <<= 8;
|
545
|
+
x += *(--p);
|
546
|
+
} while ( --j );
|
547
|
+
}
|
548
|
+
|
549
|
+
} else {
|
550
|
+
/* Take the left-hand branch */
|
551
|
+
if ( gi->record_length == 3 ) {
|
552
|
+
/* Most common case is completely unrolled and uses constants. */
|
553
|
+
x = (buf[3*0 + 0] << (0*8))
|
554
|
+
+ (buf[3*0 + 1] << (1*8))
|
555
|
+
+ (buf[3*0 + 2] << (2*8));
|
556
|
+
} else {
|
557
|
+
/* General case */
|
558
|
+
j = gi->record_length;
|
559
|
+
p = &buf[1*j];
|
560
|
+
x = 0;
|
561
|
+
do {
|
562
|
+
x <<= 8;
|
563
|
+
x += *(--p);
|
564
|
+
} while ( --j );
|
565
|
+
}
|
566
|
+
}
|
567
|
+
|
568
|
+
if (x >= gi->databaseSegments[0]) {
|
569
|
+
gi->netmask = 128 - depth;
|
570
|
+
return x;
|
571
|
+
}
|
572
|
+
offset = x;
|
573
|
+
}
|
574
|
+
|
575
|
+
/* shouldn't reach here */
|
576
|
+
_GeoIP_inet_ntop(AF_INET6, &ipnum.s6_addr[0], paddr, ADDR_STR_LEN);
|
577
|
+
fprintf(stderr,"Error Traversing Database for ipnum = %s - Perhaps database is corrupt?\n", paddr);
|
578
|
+
return 0;
|
579
|
+
}
|
580
|
+
|
581
|
+
geoipv6_t
|
582
|
+
_GeoIP_addr_to_num_v6(const char *addr)
|
583
|
+
{
|
584
|
+
geoipv6_t ipnum;
|
585
|
+
if ( 1 == _GeoIP_inet_pton(AF_INET6, addr, &ipnum.s6_addr[0] ) )
|
586
|
+
return ipnum;
|
587
|
+
return IPV6_NULL;
|
588
|
+
}
|
589
|
+
|
590
|
+
unsigned int _GeoIP_seek_record (GeoIP *gi, unsigned long ipnum) {
|
591
|
+
int depth;
|
592
|
+
unsigned int x;
|
593
|
+
unsigned char stack_buffer[2 * MAX_RECORD_LENGTH];
|
594
|
+
const unsigned char *buf = (gi->cache == NULL) ? stack_buffer : NULL;
|
595
|
+
unsigned int offset = 0;
|
596
|
+
|
597
|
+
const unsigned char * p;
|
598
|
+
int j;
|
599
|
+
size_t silence;
|
600
|
+
|
601
|
+
_check_mtime(gi);
|
602
|
+
for (depth = 31; depth >= 0; depth--) {
|
603
|
+
if (gi->cache == NULL && gi->index_cache == NULL) {
|
604
|
+
/* read from disk */
|
605
|
+
fseek(gi->GeoIPDatabase, (long)gi->record_length * 2 * offset, SEEK_SET);
|
606
|
+
silence = fread(stack_buffer,gi->record_length,2,gi->GeoIPDatabase);
|
607
|
+
} else if (gi->index_cache == NULL) {
|
608
|
+
/* simply point to record in memory */
|
609
|
+
buf = gi->cache + (long)gi->record_length * 2 *offset;
|
610
|
+
} else {
|
611
|
+
buf = gi->index_cache + (long)gi->record_length * 2 * offset;
|
612
|
+
}
|
613
|
+
|
614
|
+
if (ipnum & (1 << depth)) {
|
615
|
+
/* Take the right-hand branch */
|
616
|
+
if ( gi->record_length == 3 ) {
|
617
|
+
/* Most common case is completely unrolled and uses constants. */
|
618
|
+
x = (buf[3*1 + 0] << (0*8))
|
619
|
+
+ (buf[3*1 + 1] << (1*8))
|
620
|
+
+ (buf[3*1 + 2] << (2*8));
|
621
|
+
|
622
|
+
} else {
|
623
|
+
/* General case */
|
624
|
+
j = gi->record_length;
|
625
|
+
p = &buf[2*j];
|
626
|
+
x = 0;
|
627
|
+
do {
|
628
|
+
x <<= 8;
|
629
|
+
x += *(--p);
|
630
|
+
} while ( --j );
|
631
|
+
}
|
632
|
+
|
633
|
+
} else {
|
634
|
+
/* Take the left-hand branch */
|
635
|
+
if ( gi->record_length == 3 ) {
|
636
|
+
/* Most common case is completely unrolled and uses constants. */
|
637
|
+
x = (buf[3*0 + 0] << (0*8))
|
638
|
+
+ (buf[3*0 + 1] << (1*8))
|
639
|
+
+ (buf[3*0 + 2] << (2*8));
|
640
|
+
} else {
|
641
|
+
/* General case */
|
642
|
+
j = gi->record_length;
|
643
|
+
p = &buf[1*j];
|
644
|
+
x = 0;
|
645
|
+
do {
|
646
|
+
x <<= 8;
|
647
|
+
x += *(--p);
|
648
|
+
} while ( --j );
|
649
|
+
}
|
650
|
+
}
|
651
|
+
|
652
|
+
if (x >= gi->databaseSegments[0]) {
|
653
|
+
gi->netmask = 32 - depth;
|
654
|
+
return x;
|
655
|
+
}
|
656
|
+
offset = x;
|
657
|
+
}
|
658
|
+
|
659
|
+
/* shouldn't reach here */
|
660
|
+
fprintf(stderr,"Error Traversing Database for ipnum = %lu - Perhaps database is corrupt?\n",ipnum);
|
661
|
+
return 0;
|
662
|
+
}
|
663
|
+
|
664
|
+
unsigned long
|
665
|
+
_GeoIP_addr_to_num(const char *addr)
|
666
|
+
{
|
667
|
+
unsigned int c, octet, t;
|
668
|
+
unsigned long ipnum;
|
669
|
+
int i = 3;
|
670
|
+
|
671
|
+
octet = ipnum = 0;
|
672
|
+
while ((c = *addr++)) {
|
673
|
+
if (c == '.') {
|
674
|
+
if (octet > 255)
|
675
|
+
return 0;
|
676
|
+
ipnum <<= 8;
|
677
|
+
ipnum += octet;
|
678
|
+
i--;
|
679
|
+
octet = 0;
|
680
|
+
} else {
|
681
|
+
t = octet;
|
682
|
+
octet <<= 3;
|
683
|
+
octet += t;
|
684
|
+
octet += t;
|
685
|
+
c -= '0';
|
686
|
+
if (c > 9)
|
687
|
+
return 0;
|
688
|
+
octet += c;
|
689
|
+
}
|
690
|
+
}
|
691
|
+
if ((octet > 255) || (i != 0))
|
692
|
+
return 0;
|
693
|
+
ipnum <<= 8;
|
694
|
+
return ipnum + octet;
|
695
|
+
}
|
696
|
+
|
697
|
+
GeoIP* GeoIP_open_type (int type, int flags) {
|
698
|
+
GeoIP * gi;
|
699
|
+
const char * filePath;
|
700
|
+
if (type < 0 || type >= NUM_DB_TYPES) {
|
701
|
+
printf("Invalid database type %d\n", type);
|
702
|
+
return NULL;
|
703
|
+
}
|
704
|
+
_GeoIP_setup_dbfilename();
|
705
|
+
filePath = GeoIPDBFileName[type];
|
706
|
+
if (filePath == NULL) {
|
707
|
+
printf("Invalid database type %d\n", type);
|
708
|
+
return NULL;
|
709
|
+
}
|
710
|
+
gi = GeoIP_open (filePath, flags);
|
711
|
+
return gi;
|
712
|
+
}
|
713
|
+
|
714
|
+
GeoIP* GeoIP_new (int flags) {
|
715
|
+
GeoIP * gi;
|
716
|
+
_GeoIP_setup_dbfilename();
|
717
|
+
gi = GeoIP_open (GeoIPDBFileName[GEOIP_COUNTRY_EDITION], flags);
|
718
|
+
return gi;
|
719
|
+
}
|
720
|
+
|
721
|
+
GeoIP* GeoIP_open (const char * filename, int flags) {
|
722
|
+
struct stat buf;
|
723
|
+
GeoIP * gi;
|
724
|
+
size_t len;
|
725
|
+
|
726
|
+
gi = (GeoIP *)malloc(sizeof(GeoIP));
|
727
|
+
if (gi == NULL)
|
728
|
+
return NULL;
|
729
|
+
len = sizeof(char) * (strlen(filename)+1);
|
730
|
+
gi->file_path = malloc(len);
|
731
|
+
if (gi->file_path == NULL) {
|
732
|
+
free(gi);
|
733
|
+
return NULL;
|
734
|
+
}
|
735
|
+
strncpy(gi->file_path, filename, len);
|
736
|
+
gi->GeoIPDatabase = fopen(filename,"rb");
|
737
|
+
if (gi->GeoIPDatabase == NULL) {
|
738
|
+
fprintf(stderr,"Error Opening file %s\n",filename);
|
739
|
+
free(gi->file_path);
|
740
|
+
free(gi);
|
741
|
+
return NULL;
|
742
|
+
} else {
|
743
|
+
if (flags & (GEOIP_MEMORY_CACHE | GEOIP_MMAP_CACHE) ) {
|
744
|
+
if (fstat(fileno(gi->GeoIPDatabase), &buf) == -1) {
|
745
|
+
fprintf(stderr,"Error stating file %s\n",filename);
|
746
|
+
free(gi->file_path);
|
747
|
+
free(gi);
|
748
|
+
return NULL;
|
749
|
+
}
|
750
|
+
gi->mtime = buf.st_mtime;
|
751
|
+
gi->size = buf.st_size;
|
752
|
+
|
753
|
+
/* MMAP added my Peter Shipley */
|
754
|
+
if ( flags & GEOIP_MMAP_CACHE ) {
|
755
|
+
#if !defined(_WIN32)
|
756
|
+
gi->cache = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fileno(gi->GeoIPDatabase), 0);
|
757
|
+
if ( gi->cache == MAP_FAILED ) {
|
758
|
+
fprintf(stderr,"Error mmaping file %s\n",filename);
|
759
|
+
free(gi->file_path);
|
760
|
+
free(gi);
|
761
|
+
return NULL;
|
762
|
+
}
|
763
|
+
#endif
|
764
|
+
} else {
|
765
|
+
gi->cache = (unsigned char *) malloc(sizeof(unsigned char) * buf.st_size);
|
766
|
+
|
767
|
+
if (gi->cache != NULL) {
|
768
|
+
if (fread(gi->cache, sizeof(unsigned char), buf.st_size, gi->GeoIPDatabase) != (size_t) buf.st_size) {
|
769
|
+
fprintf(stderr,"Error reading file %s\n",filename);
|
770
|
+
free(gi->cache);
|
771
|
+
free(gi->file_path);
|
772
|
+
free(gi);
|
773
|
+
return NULL;
|
774
|
+
}
|
775
|
+
}
|
776
|
+
}
|
777
|
+
} else {
|
778
|
+
if (flags & GEOIP_CHECK_CACHE) {
|
779
|
+
if (fstat(fileno(gi->GeoIPDatabase), &buf) == -1) {
|
780
|
+
fprintf(stderr,"Error stating file %s\n",filename);
|
781
|
+
free(gi->file_path);
|
782
|
+
free(gi);
|
783
|
+
return NULL;
|
784
|
+
}
|
785
|
+
gi->mtime = buf.st_mtime;
|
786
|
+
}
|
787
|
+
gi->cache = NULL;
|
788
|
+
}
|
789
|
+
gi->flags = flags;
|
790
|
+
gi->charset = GEOIP_CHARSET_ISO_8859_1;
|
791
|
+
|
792
|
+
_setup_segments(gi);
|
793
|
+
if (flags & GEOIP_INDEX_CACHE) {
|
794
|
+
gi->index_cache = (unsigned char *) malloc(sizeof(unsigned char) * ((gi->databaseSegments[0] * (long)gi->record_length * 2)));
|
795
|
+
if (gi->index_cache != NULL) {
|
796
|
+
fseek(gi->GeoIPDatabase, 0, SEEK_SET);
|
797
|
+
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)) {
|
798
|
+
fprintf(stderr,"Error reading file %s\n",filename);
|
799
|
+
free(gi->databaseSegments);
|
800
|
+
free(gi->index_cache);
|
801
|
+
free(gi);
|
802
|
+
return NULL;
|
803
|
+
}
|
804
|
+
}
|
805
|
+
} else {
|
806
|
+
gi->index_cache = NULL;
|
807
|
+
}
|
808
|
+
return gi;
|
809
|
+
}
|
810
|
+
}
|
811
|
+
|
812
|
+
void GeoIP_delete (GeoIP *gi) {
|
813
|
+
if (gi == NULL )
|
814
|
+
return;
|
815
|
+
if (gi->GeoIPDatabase != NULL)
|
816
|
+
fclose(gi->GeoIPDatabase);
|
817
|
+
if (gi->cache != NULL) {
|
818
|
+
if ( gi->flags & GEOIP_MMAP_CACHE ) {
|
819
|
+
#if !defined(_WIN32)
|
820
|
+
munmap(gi->cache, gi->size);
|
821
|
+
#endif
|
822
|
+
} else {
|
823
|
+
free(gi->cache);
|
824
|
+
}
|
825
|
+
gi->cache = NULL;
|
826
|
+
}
|
827
|
+
if (gi->index_cache != NULL)
|
828
|
+
free(gi->index_cache);
|
829
|
+
if (gi->file_path != NULL)
|
830
|
+
free(gi->file_path);
|
831
|
+
if (gi->databaseSegments != NULL)
|
832
|
+
free(gi->databaseSegments);
|
833
|
+
free(gi);
|
834
|
+
}
|
835
|
+
|
836
|
+
const char *GeoIP_country_code_by_name (GeoIP* gi, const char *name) {
|
837
|
+
int country_id;
|
838
|
+
country_id = GeoIP_id_by_name(gi, name);
|
839
|
+
return (country_id > 0) ? GeoIP_country_code[country_id] : NULL;
|
840
|
+
}
|
841
|
+
|
842
|
+
const char *GeoIP_country_code3_by_name (GeoIP* gi, const char *name) {
|
843
|
+
int country_id;
|
844
|
+
country_id = GeoIP_id_by_name(gi, name);
|
845
|
+
return (country_id > 0) ? GeoIP_country_code3[country_id] : NULL;
|
846
|
+
}
|
847
|
+
|
848
|
+
const char *GeoIP_country_name_by_name (GeoIP* gi, const char *name) {
|
849
|
+
int country_id;
|
850
|
+
country_id = GeoIP_id_by_name(gi, name);
|
851
|
+
return (country_id > 0) ? GeoIP_country_name[country_id] : NULL;
|
852
|
+
}
|
853
|
+
|
854
|
+
unsigned long _GeoIP_lookupaddress (const char *host) {
|
855
|
+
unsigned long addr = inet_addr(host);
|
856
|
+
struct hostent phe2;
|
857
|
+
struct hostent * phe = &phe2;
|
858
|
+
char *buf = NULL;
|
859
|
+
#ifdef HAVE_GETHOSTBYNAME_R
|
860
|
+
int buflength = 16384;
|
861
|
+
int herr = 0;
|
862
|
+
#endif
|
863
|
+
int result = 0;
|
864
|
+
#ifdef HAVE_GETHOSTBYNAME_R
|
865
|
+
buf = malloc(buflength);
|
866
|
+
#endif
|
867
|
+
if (addr == INADDR_NONE) {
|
868
|
+
#ifdef HAVE_GETHOSTBYNAME_R
|
869
|
+
while (1) {
|
870
|
+
/* we use gethostbyname_r here because it is thread-safe and gethostbyname is not */
|
871
|
+
#ifdef GETHOSTBYNAME_R_RETURNS_INT
|
872
|
+
result = gethostbyname_r(host,&phe2,buf,buflength,&phe,&herr);
|
873
|
+
#else
|
874
|
+
phe = gethostbyname_r(host,&phe2,buf,buflength,&herr);
|
875
|
+
#endif
|
876
|
+
if (herr != ERANGE)
|
877
|
+
break;
|
878
|
+
if (result == 0)
|
879
|
+
break;
|
880
|
+
/* double the buffer if the buffer is too small */
|
881
|
+
buflength = buflength * 2;
|
882
|
+
buf = realloc(buf,buflength);
|
883
|
+
}
|
884
|
+
#else
|
885
|
+
/* Some systems do not support gethostbyname_r, such as Mac OS X */
|
886
|
+
phe = gethostbyname(host);
|
887
|
+
#endif
|
888
|
+
if (!phe || result != 0) {
|
889
|
+
free(buf);
|
890
|
+
return 0;
|
891
|
+
}
|
892
|
+
#if !defined(_WIN32)
|
893
|
+
addr = *((in_addr_t *) phe->h_addr_list[0]);
|
894
|
+
#else
|
895
|
+
addr = *(phe->h_addr_list[0]);
|
896
|
+
#endif
|
897
|
+
}
|
898
|
+
#ifdef HAVE_GETHOSTBYNAME_R
|
899
|
+
free(buf);
|
900
|
+
#endif
|
901
|
+
return ntohl(addr);
|
902
|
+
}
|
903
|
+
|
904
|
+
geoipv6_t
|
905
|
+
_GeoIP_lookupaddress_v6(const char *host)
|
906
|
+
{
|
907
|
+
geoipv6_t ipnum;
|
908
|
+
int gaierr;
|
909
|
+
struct addrinfo hints, *aifirst;
|
910
|
+
|
911
|
+
memset(&hints, 0, sizeof(hints));
|
912
|
+
hints.ai_family = AF_INET6;
|
913
|
+
/* hints.ai_flags = AI_V4MAPPED; */
|
914
|
+
hints.ai_socktype = SOCK_STREAM;
|
915
|
+
|
916
|
+
if ((gaierr = getaddrinfo(host, NULL, &hints, &aifirst)) != 0) {
|
917
|
+
/* fprintf(stderr, "Err: %s (%d %s)\n", host, gaierr, gai_strerror(gaierr)); */
|
918
|
+
return IPV6_NULL;
|
919
|
+
}
|
920
|
+
memcpy(ipnum.s6_addr, ((struct sockaddr_in6 *) aifirst->ai_addr)->sin6_addr.s6_addr, sizeof(geoipv6_t));
|
921
|
+
freeaddrinfo(aifirst);
|
922
|
+
/* inet_pton(AF_INET6, host, ipnum.s6_addr); */
|
923
|
+
|
924
|
+
return ipnum;
|
925
|
+
}
|
926
|
+
|
927
|
+
int GeoIP_id_by_name (GeoIP* gi, const char *name) {
|
928
|
+
unsigned long ipnum;
|
929
|
+
int ret;
|
930
|
+
if (name == NULL) {
|
931
|
+
return 0;
|
932
|
+
}
|
933
|
+
if (gi->databaseType != GEOIP_COUNTRY_EDITION && gi->databaseType != GEOIP_PROXY_EDITION && gi->databaseType != GEOIP_NETSPEED_EDITION) {
|
934
|
+
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_COUNTRY_EDITION]);
|
935
|
+
return 0;
|
936
|
+
}
|
937
|
+
if (!(ipnum = _GeoIP_lookupaddress(name)))
|
938
|
+
return 0;
|
939
|
+
ret = _GeoIP_seek_record(gi, ipnum) - COUNTRY_BEGIN;
|
940
|
+
return ret;
|
941
|
+
|
942
|
+
}
|
943
|
+
|
944
|
+
int GeoIP_id_by_name_v6 (GeoIP* gi, const char *name) {
|
945
|
+
geoipv6_t ipnum;
|
946
|
+
int ret;
|
947
|
+
if (name == NULL) {
|
948
|
+
return 0;
|
949
|
+
}
|
950
|
+
if (gi->databaseType != GEOIP_COUNTRY_EDITION_V6 && gi->databaseType != GEOIP_PROXY_EDITION && gi->databaseType != GEOIP_NETSPEED_EDITION) {
|
951
|
+
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_COUNTRY_EDITION_V6]);
|
952
|
+
return 0;
|
953
|
+
}
|
954
|
+
ipnum = _GeoIP_lookupaddress_v6(name);
|
955
|
+
if (__GEOIP_V6_IS_NULL(ipnum))
|
956
|
+
return 0;
|
957
|
+
ret = _GeoIP_seek_record_v6(gi, ipnum) - COUNTRY_BEGIN;
|
958
|
+
return ret;
|
959
|
+
}
|
960
|
+
|
961
|
+
const char *GeoIP_country_code_by_addr (GeoIP* gi, const char *addr) {
|
962
|
+
int country_id;
|
963
|
+
country_id = GeoIP_id_by_addr(gi, addr);
|
964
|
+
return (country_id > 0) ? GeoIP_country_code[country_id] : NULL;
|
965
|
+
}
|
966
|
+
|
967
|
+
const char *GeoIP_country_code3_by_addr (GeoIP* gi, const char *addr) {
|
968
|
+
int country_id;
|
969
|
+
country_id = GeoIP_id_by_addr(gi, addr);
|
970
|
+
return (country_id > 0) ? GeoIP_country_code3[country_id] : NULL;
|
971
|
+
}
|
972
|
+
|
973
|
+
const char *GeoIP_country_name_by_addr (GeoIP* gi, const char *addr) {
|
974
|
+
int country_id;
|
975
|
+
country_id = GeoIP_id_by_addr(gi, addr);
|
976
|
+
return (country_id > 0) ? GeoIP_country_name[country_id] : NULL;
|
977
|
+
}
|
978
|
+
|
979
|
+
const char *GeoIP_country_name_by_ipnum (GeoIP* gi, unsigned long ipnum) {
|
980
|
+
int country_id;
|
981
|
+
country_id = GeoIP_id_by_ipnum(gi, ipnum);
|
982
|
+
return (country_id > 0) ? GeoIP_country_name[country_id] : NULL;
|
983
|
+
}
|
984
|
+
|
985
|
+
const char *GeoIP_country_name_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum) {
|
986
|
+
int country_id;
|
987
|
+
country_id = GeoIP_id_by_ipnum_v6(gi, ipnum);
|
988
|
+
return (country_id > 0) ? GeoIP_country_name[country_id] : NULL;
|
989
|
+
}
|
990
|
+
|
991
|
+
const char *GeoIP_country_code_by_ipnum (GeoIP* gi, unsigned long ipnum) {
|
992
|
+
int country_id;
|
993
|
+
country_id = GeoIP_id_by_ipnum(gi, ipnum);
|
994
|
+
return (country_id > 0) ? GeoIP_country_code[country_id] : NULL;
|
995
|
+
}
|
996
|
+
|
997
|
+
const char *GeoIP_country_code_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum) {
|
998
|
+
int country_id;
|
999
|
+
country_id = GeoIP_id_by_ipnum_v6(gi, ipnum);
|
1000
|
+
return (country_id > 0) ? GeoIP_country_code[country_id] : NULL;
|
1001
|
+
}
|
1002
|
+
|
1003
|
+
const char *GeoIP_country_code3_by_ipnum (GeoIP* gi, unsigned long ipnum) {
|
1004
|
+
int country_id;
|
1005
|
+
country_id = GeoIP_id_by_ipnum(gi, ipnum);
|
1006
|
+
return (country_id > 0) ? GeoIP_country_code3[country_id] : NULL;
|
1007
|
+
}
|
1008
|
+
|
1009
|
+
const char *GeoIP_country_code3_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum) {
|
1010
|
+
int country_id;
|
1011
|
+
country_id = GeoIP_id_by_ipnum_v6(gi, ipnum);
|
1012
|
+
return (country_id > 0) ? GeoIP_country_code3[country_id] : NULL;
|
1013
|
+
}
|
1014
|
+
|
1015
|
+
int GeoIP_country_id_by_addr (GeoIP* gi, const char *addr) {
|
1016
|
+
return GeoIP_id_by_addr(gi, addr);
|
1017
|
+
}
|
1018
|
+
|
1019
|
+
int GeoIP_country_id_by_name (GeoIP* gi, const char *host) {
|
1020
|
+
return GeoIP_id_by_name(gi, host);
|
1021
|
+
}
|
1022
|
+
|
1023
|
+
int GeoIP_id_by_addr_v6 (GeoIP* gi, const char *addr) {
|
1024
|
+
geoipv6_t ipnum;
|
1025
|
+
int ret;
|
1026
|
+
if (addr == NULL) {
|
1027
|
+
return 0;
|
1028
|
+
}
|
1029
|
+
if (gi->databaseType != GEOIP_COUNTRY_EDITION_V6 &&
|
1030
|
+
gi->databaseType != GEOIP_PROXY_EDITION &&
|
1031
|
+
gi->databaseType != GEOIP_NETSPEED_EDITION) {
|
1032
|
+
printf("Invalid database type %s, expected %s\n",
|
1033
|
+
GeoIPDBDescription[(int)gi->databaseType],
|
1034
|
+
GeoIPDBDescription[GEOIP_COUNTRY_EDITION_V6]);
|
1035
|
+
return 0;
|
1036
|
+
}
|
1037
|
+
ipnum = _GeoIP_addr_to_num_v6(addr);
|
1038
|
+
ret = _GeoIP_seek_record_v6(gi, ipnum) - COUNTRY_BEGIN;
|
1039
|
+
return ret;
|
1040
|
+
}
|
1041
|
+
|
1042
|
+
int GeoIP_id_by_addr (GeoIP* gi, const char *addr) {
|
1043
|
+
unsigned long ipnum;
|
1044
|
+
int ret;
|
1045
|
+
if (addr == NULL) {
|
1046
|
+
return 0;
|
1047
|
+
}
|
1048
|
+
if (gi->databaseType != GEOIP_COUNTRY_EDITION &&
|
1049
|
+
gi->databaseType != GEOIP_PROXY_EDITION &&
|
1050
|
+
gi->databaseType != GEOIP_NETSPEED_EDITION) {
|
1051
|
+
printf("Invalid database type %s, expected %s\n",
|
1052
|
+
GeoIPDBDescription[(int)gi->databaseType],
|
1053
|
+
GeoIPDBDescription[GEOIP_COUNTRY_EDITION]);
|
1054
|
+
return 0;
|
1055
|
+
}
|
1056
|
+
ipnum = _GeoIP_addr_to_num(addr);
|
1057
|
+
ret = _GeoIP_seek_record(gi, ipnum) - COUNTRY_BEGIN;
|
1058
|
+
return ret;
|
1059
|
+
}
|
1060
|
+
|
1061
|
+
int GeoIP_id_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum) {
|
1062
|
+
int ret;
|
1063
|
+
/* if (ipnum == 0) {
|
1064
|
+
return 0;
|
1065
|
+
}
|
1066
|
+
*/
|
1067
|
+
if (gi->databaseType != GEOIP_COUNTRY_EDITION_V6 &&
|
1068
|
+
gi->databaseType != GEOIP_PROXY_EDITION &&
|
1069
|
+
gi->databaseType != GEOIP_NETSPEED_EDITION) {
|
1070
|
+
printf("Invalid database type %s, expected %s\n",
|
1071
|
+
GeoIPDBDescription[(int)gi->databaseType],
|
1072
|
+
GeoIPDBDescription[GEOIP_COUNTRY_EDITION_V6]);
|
1073
|
+
return 0;
|
1074
|
+
}
|
1075
|
+
ret = _GeoIP_seek_record_v6(gi, ipnum) - COUNTRY_BEGIN;
|
1076
|
+
return ret;
|
1077
|
+
}
|
1078
|
+
|
1079
|
+
|
1080
|
+
|
1081
|
+
int GeoIP_id_by_ipnum (GeoIP* gi, unsigned long ipnum) {
|
1082
|
+
int ret;
|
1083
|
+
if (ipnum == 0) {
|
1084
|
+
return 0;
|
1085
|
+
}
|
1086
|
+
if (gi->databaseType != GEOIP_COUNTRY_EDITION &&
|
1087
|
+
gi->databaseType != GEOIP_PROXY_EDITION &&
|
1088
|
+
gi->databaseType != GEOIP_NETSPEED_EDITION) {
|
1089
|
+
printf("Invalid database type %s, expected %s\n",
|
1090
|
+
GeoIPDBDescription[(int)gi->databaseType],
|
1091
|
+
GeoIPDBDescription[GEOIP_COUNTRY_EDITION]);
|
1092
|
+
return 0;
|
1093
|
+
}
|
1094
|
+
ret = _GeoIP_seek_record(gi, ipnum) - COUNTRY_BEGIN;
|
1095
|
+
return ret;
|
1096
|
+
}
|
1097
|
+
|
1098
|
+
char *GeoIP_database_info (GeoIP* gi) {
|
1099
|
+
int i;
|
1100
|
+
unsigned char buf[3];
|
1101
|
+
char *retval;
|
1102
|
+
int hasStructureInfo = 0;
|
1103
|
+
size_t silence;
|
1104
|
+
|
1105
|
+
if(gi == NULL)
|
1106
|
+
return NULL;
|
1107
|
+
|
1108
|
+
_check_mtime(gi);
|
1109
|
+
fseek(gi->GeoIPDatabase, -3l, SEEK_END);
|
1110
|
+
|
1111
|
+
/* first get past the database structure information */
|
1112
|
+
for (i = 0; i < STRUCTURE_INFO_MAX_SIZE; i++) {
|
1113
|
+
silence = fread(buf, 1, 3, gi->GeoIPDatabase);
|
1114
|
+
if (buf[0] == 255 && buf[1] == 255 && buf[2] == 255) {
|
1115
|
+
hasStructureInfo = 1;
|
1116
|
+
break;
|
1117
|
+
}
|
1118
|
+
fseek(gi->GeoIPDatabase, -4l, SEEK_CUR);
|
1119
|
+
}
|
1120
|
+
if (hasStructureInfo == 1) {
|
1121
|
+
fseek(gi->GeoIPDatabase, -6l, SEEK_CUR);
|
1122
|
+
} else {
|
1123
|
+
/* no structure info, must be pre Sep 2002 database, go back to end */
|
1124
|
+
fseek(gi->GeoIPDatabase, -3l, SEEK_END);
|
1125
|
+
}
|
1126
|
+
|
1127
|
+
for (i = 0; i < DATABASE_INFO_MAX_SIZE; i++) {
|
1128
|
+
silence = fread(buf, 1, 3, gi->GeoIPDatabase);
|
1129
|
+
if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0) {
|
1130
|
+
retval = malloc(sizeof(char) * (i+1));
|
1131
|
+
if (retval == NULL) {
|
1132
|
+
return NULL;
|
1133
|
+
}
|
1134
|
+
silence = fread(retval, 1, i, gi->GeoIPDatabase);
|
1135
|
+
retval[i] = '\0';
|
1136
|
+
return retval;
|
1137
|
+
}
|
1138
|
+
fseek(gi->GeoIPDatabase, -4l, SEEK_CUR);
|
1139
|
+
}
|
1140
|
+
return NULL;
|
1141
|
+
}
|
1142
|
+
|
1143
|
+
/* GeoIP Region Edition functions */
|
1144
|
+
|
1145
|
+
void GeoIP_assign_region_by_inetaddr(GeoIP* gi, unsigned long inetaddr, GeoIPRegion *region) {
|
1146
|
+
unsigned int seek_region;
|
1147
|
+
|
1148
|
+
/* This also writes in the terminating NULs (if you decide to
|
1149
|
+
* keep them) and clear any fields that are not set. */
|
1150
|
+
memset(region, 0, sizeof(GeoIPRegion));
|
1151
|
+
|
1152
|
+
seek_region = _GeoIP_seek_record(gi, ntohl(inetaddr));
|
1153
|
+
|
1154
|
+
if (gi->databaseType == GEOIP_REGION_EDITION_REV0) {
|
1155
|
+
/* Region Edition, pre June 2003 */
|
1156
|
+
seek_region -= STATE_BEGIN_REV0;
|
1157
|
+
if (seek_region >= 1000) {
|
1158
|
+
region->country_code[0] = 'U';
|
1159
|
+
region->country_code[1] = 'S';
|
1160
|
+
region->region[0] = (char) ((seek_region - 1000)/26 + 65);
|
1161
|
+
region->region[1] = (char) ((seek_region - 1000)%26 + 65);
|
1162
|
+
} else {
|
1163
|
+
memcpy(region->country_code, GeoIP_country_code[seek_region], 2);
|
1164
|
+
}
|
1165
|
+
} else if (gi->databaseType == GEOIP_REGION_EDITION_REV1) {
|
1166
|
+
/* Region Edition, post June 2003 */
|
1167
|
+
seek_region -= STATE_BEGIN_REV1;
|
1168
|
+
if (seek_region < US_OFFSET) {
|
1169
|
+
/* Unknown */
|
1170
|
+
/* we don't need to do anything here b/c we memset region to 0 */
|
1171
|
+
} else if (seek_region < CANADA_OFFSET) {
|
1172
|
+
/* USA State */
|
1173
|
+
region->country_code[0] = 'U';
|
1174
|
+
region->country_code[1] = 'S';
|
1175
|
+
region->region[0] = (char) ((seek_region - US_OFFSET)/26 + 65);
|
1176
|
+
region->region[1] = (char) ((seek_region - US_OFFSET)%26 + 65);
|
1177
|
+
} else if (seek_region < WORLD_OFFSET) {
|
1178
|
+
/* Canada Province */
|
1179
|
+
region->country_code[0] = 'C';
|
1180
|
+
region->country_code[1] = 'A';
|
1181
|
+
region->region[0] = (char) ((seek_region - CANADA_OFFSET)/26 + 65);
|
1182
|
+
region->region[1] = (char) ((seek_region - CANADA_OFFSET)%26 + 65);
|
1183
|
+
} else {
|
1184
|
+
/* Not US or Canada */
|
1185
|
+
memcpy(region->country_code, GeoIP_country_code[(seek_region - WORLD_OFFSET) / FIPS_RANGE], 2);
|
1186
|
+
}
|
1187
|
+
}
|
1188
|
+
}
|
1189
|
+
|
1190
|
+
void GeoIP_assign_region_by_inetaddr_v6(GeoIP* gi, geoipv6_t inetaddr, GeoIPRegion *region) {
|
1191
|
+
unsigned int seek_region;
|
1192
|
+
|
1193
|
+
/* This also writes in the terminating NULs (if you decide to
|
1194
|
+
* keep them) and clear any fields that are not set. */
|
1195
|
+
memset(region, 0, sizeof(GeoIPRegion));
|
1196
|
+
|
1197
|
+
seek_region = _GeoIP_seek_record_v6(gi, inetaddr);
|
1198
|
+
|
1199
|
+
if (gi->databaseType == GEOIP_REGION_EDITION_REV0) {
|
1200
|
+
/* Region Edition, pre June 2003 */
|
1201
|
+
seek_region -= STATE_BEGIN_REV0;
|
1202
|
+
if (seek_region >= 1000) {
|
1203
|
+
region->country_code[0] = 'U';
|
1204
|
+
region->country_code[1] = 'S';
|
1205
|
+
region->region[0] = (char) ((seek_region - 1000)/26 + 65);
|
1206
|
+
region->region[1] = (char) ((seek_region - 1000)%26 + 65);
|
1207
|
+
} else {
|
1208
|
+
memcpy(region->country_code, GeoIP_country_code[seek_region], 2);
|
1209
|
+
}
|
1210
|
+
} else if (gi->databaseType == GEOIP_REGION_EDITION_REV1) {
|
1211
|
+
/* Region Edition, post June 2003 */
|
1212
|
+
seek_region -= STATE_BEGIN_REV1;
|
1213
|
+
if (seek_region < US_OFFSET) {
|
1214
|
+
/* Unknown */
|
1215
|
+
/* we don't need to do anything here b/c we memset region to 0 */
|
1216
|
+
} else if (seek_region < CANADA_OFFSET) {
|
1217
|
+
/* USA State */
|
1218
|
+
region->country_code[0] = 'U';
|
1219
|
+
region->country_code[1] = 'S';
|
1220
|
+
region->region[0] = (char) ((seek_region - US_OFFSET)/26 + 65);
|
1221
|
+
region->region[1] = (char) ((seek_region - US_OFFSET)%26 + 65);
|
1222
|
+
} else if (seek_region < WORLD_OFFSET) {
|
1223
|
+
/* Canada Province */
|
1224
|
+
region->country_code[0] = 'C';
|
1225
|
+
region->country_code[1] = 'A';
|
1226
|
+
region->region[0] = (char) ((seek_region - CANADA_OFFSET)/26 + 65);
|
1227
|
+
region->region[1] = (char) ((seek_region - CANADA_OFFSET)%26 + 65);
|
1228
|
+
} else {
|
1229
|
+
/* Not US or Canada */
|
1230
|
+
memcpy(region->country_code, GeoIP_country_code[(seek_region - WORLD_OFFSET) / FIPS_RANGE], 2);
|
1231
|
+
}
|
1232
|
+
}
|
1233
|
+
}
|
1234
|
+
|
1235
|
+
static
|
1236
|
+
GeoIPRegion * _get_region(GeoIP* gi, unsigned long ipnum) {
|
1237
|
+
GeoIPRegion * region;
|
1238
|
+
|
1239
|
+
region = malloc(sizeof(GeoIPRegion));
|
1240
|
+
if (region) {
|
1241
|
+
GeoIP_assign_region_by_inetaddr(gi, htonl(ipnum), region);
|
1242
|
+
}
|
1243
|
+
return region;
|
1244
|
+
}
|
1245
|
+
|
1246
|
+
static
|
1247
|
+
GeoIPRegion * _get_region_v6(GeoIP* gi, geoipv6_t ipnum) {
|
1248
|
+
GeoIPRegion * region;
|
1249
|
+
|
1250
|
+
region = malloc(sizeof(GeoIPRegion));
|
1251
|
+
if (region) {
|
1252
|
+
GeoIP_assign_region_by_inetaddr_v6(gi, ipnum, region);
|
1253
|
+
}
|
1254
|
+
return region;
|
1255
|
+
}
|
1256
|
+
|
1257
|
+
GeoIPRegion * GeoIP_region_by_addr (GeoIP* gi, const char *addr) {
|
1258
|
+
unsigned long ipnum;
|
1259
|
+
if (addr == NULL) {
|
1260
|
+
return 0;
|
1261
|
+
}
|
1262
|
+
if (gi->databaseType != GEOIP_REGION_EDITION_REV0 &&
|
1263
|
+
gi->databaseType != GEOIP_REGION_EDITION_REV1) {
|
1264
|
+
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_REGION_EDITION_REV1]);
|
1265
|
+
return 0;
|
1266
|
+
}
|
1267
|
+
ipnum = _GeoIP_addr_to_num(addr);
|
1268
|
+
return _get_region(gi, ipnum);
|
1269
|
+
}
|
1270
|
+
|
1271
|
+
GeoIPRegion * GeoIP_region_by_addr_v6 (GeoIP* gi, const char *addr) {
|
1272
|
+
geoipv6_t ipnum;
|
1273
|
+
if (addr == NULL) {
|
1274
|
+
return 0;
|
1275
|
+
}
|
1276
|
+
if (gi->databaseType != GEOIP_REGION_EDITION_REV0 &&
|
1277
|
+
gi->databaseType != GEOIP_REGION_EDITION_REV1) {
|
1278
|
+
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_REGION_EDITION_REV1]);
|
1279
|
+
return 0;
|
1280
|
+
}
|
1281
|
+
ipnum = _GeoIP_addr_to_num_v6(addr);
|
1282
|
+
return _get_region_v6(gi, ipnum);
|
1283
|
+
}
|
1284
|
+
|
1285
|
+
GeoIPRegion * GeoIP_region_by_name (GeoIP* gi, const char *name) {
|
1286
|
+
unsigned long ipnum;
|
1287
|
+
if (name == NULL) {
|
1288
|
+
return 0;
|
1289
|
+
}
|
1290
|
+
if (gi->databaseType != GEOIP_REGION_EDITION_REV0 &&
|
1291
|
+
gi->databaseType != GEOIP_REGION_EDITION_REV1) {
|
1292
|
+
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_REGION_EDITION_REV1]);
|
1293
|
+
return 0;
|
1294
|
+
}
|
1295
|
+
if (!(ipnum = _GeoIP_lookupaddress(name)))
|
1296
|
+
return 0;
|
1297
|
+
return _get_region(gi, ipnum);
|
1298
|
+
}
|
1299
|
+
|
1300
|
+
GeoIPRegion * GeoIP_region_by_name_v6 (GeoIP* gi, const char *name) {
|
1301
|
+
geoipv6_t ipnum;
|
1302
|
+
if (name == NULL) {
|
1303
|
+
return 0;
|
1304
|
+
}
|
1305
|
+
if (gi->databaseType != GEOIP_REGION_EDITION_REV0 &&
|
1306
|
+
gi->databaseType != GEOIP_REGION_EDITION_REV1) {
|
1307
|
+
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_REGION_EDITION_REV1]);
|
1308
|
+
return 0;
|
1309
|
+
}
|
1310
|
+
|
1311
|
+
ipnum = _GeoIP_lookupaddress_v6(name);
|
1312
|
+
if (__GEOIP_V6_IS_NULL(ipnum))
|
1313
|
+
return 0;
|
1314
|
+
return _get_region_v6(gi, ipnum);
|
1315
|
+
}
|
1316
|
+
|
1317
|
+
GeoIPRegion * GeoIP_region_by_ipnum (GeoIP* gi, unsigned long ipnum) {
|
1318
|
+
if (gi->databaseType != GEOIP_REGION_EDITION_REV0 &&
|
1319
|
+
gi->databaseType != GEOIP_REGION_EDITION_REV1) {
|
1320
|
+
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_REGION_EDITION_REV1]);
|
1321
|
+
return 0;
|
1322
|
+
}
|
1323
|
+
return _get_region(gi, ipnum);
|
1324
|
+
}
|
1325
|
+
|
1326
|
+
GeoIPRegion * GeoIP_region_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum) {
|
1327
|
+
if (gi->databaseType != GEOIP_REGION_EDITION_REV0 &&
|
1328
|
+
gi->databaseType != GEOIP_REGION_EDITION_REV1) {
|
1329
|
+
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_REGION_EDITION_REV1]);
|
1330
|
+
return 0;
|
1331
|
+
}
|
1332
|
+
return _get_region_v6(gi, ipnum);
|
1333
|
+
}
|
1334
|
+
|
1335
|
+
void GeoIPRegion_delete (GeoIPRegion *gir) {
|
1336
|
+
free(gir);
|
1337
|
+
}
|
1338
|
+
|
1339
|
+
/* GeoIP Organization, ISP and AS Number Edition private method */
|
1340
|
+
static
|
1341
|
+
char *_get_name (GeoIP* gi, unsigned long ipnum) {
|
1342
|
+
int seek_org;
|
1343
|
+
char buf[MAX_ORG_RECORD_LENGTH];
|
1344
|
+
char * org_buf, * buf_pointer;
|
1345
|
+
int record_pointer;
|
1346
|
+
size_t len;
|
1347
|
+
size_t silence;
|
1348
|
+
|
1349
|
+
if (gi->databaseType != GEOIP_ORG_EDITION &&
|
1350
|
+
gi->databaseType != GEOIP_ISP_EDITION &&
|
1351
|
+
gi->databaseType != GEOIP_ASNUM_EDITION) {
|
1352
|
+
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_ORG_EDITION]);
|
1353
|
+
return NULL;
|
1354
|
+
}
|
1355
|
+
|
1356
|
+
seek_org = _GeoIP_seek_record(gi, ipnum);
|
1357
|
+
if (seek_org == gi->databaseSegments[0])
|
1358
|
+
return NULL;
|
1359
|
+
|
1360
|
+
record_pointer = seek_org + (2 * gi->record_length - 1) * gi->databaseSegments[0];
|
1361
|
+
|
1362
|
+
if (gi->cache == NULL) {
|
1363
|
+
fseek(gi->GeoIPDatabase, record_pointer, SEEK_SET);
|
1364
|
+
silence = fread(buf, sizeof(char), MAX_ORG_RECORD_LENGTH, gi->GeoIPDatabase);
|
1365
|
+
len = sizeof(char) * (strlen(buf)+1);
|
1366
|
+
org_buf = malloc(len);
|
1367
|
+
strncpy(org_buf, buf, len);
|
1368
|
+
} else {
|
1369
|
+
buf_pointer = (char *)(gi->cache + (long)record_pointer);
|
1370
|
+
len = sizeof(char) * (strlen(buf_pointer)+1);
|
1371
|
+
org_buf = malloc(len);
|
1372
|
+
strncpy(org_buf, buf_pointer, len);
|
1373
|
+
}
|
1374
|
+
return org_buf;
|
1375
|
+
}
|
1376
|
+
|
1377
|
+
char *_get_name_v6 (GeoIP* gi, geoipv6_t ipnum) {
|
1378
|
+
int seek_org;
|
1379
|
+
char buf[MAX_ORG_RECORD_LENGTH];
|
1380
|
+
char * org_buf, * buf_pointer;
|
1381
|
+
int record_pointer;
|
1382
|
+
size_t len;
|
1383
|
+
size_t silence;
|
1384
|
+
|
1385
|
+
if (gi->databaseType != GEOIP_ORG_EDITION &&
|
1386
|
+
gi->databaseType != GEOIP_ISP_EDITION &&
|
1387
|
+
gi->databaseType != GEOIP_ASNUM_EDITION) {
|
1388
|
+
printf("Invalid database type %s, expected %s\n", GeoIPDBDescription[(int)gi->databaseType], GeoIPDBDescription[GEOIP_ORG_EDITION]);
|
1389
|
+
return NULL;
|
1390
|
+
}
|
1391
|
+
|
1392
|
+
seek_org = _GeoIP_seek_record_v6(gi, ipnum);
|
1393
|
+
if (seek_org == gi->databaseSegments[0])
|
1394
|
+
return NULL;
|
1395
|
+
|
1396
|
+
record_pointer = seek_org + (2 * gi->record_length - 1) * gi->databaseSegments[0];
|
1397
|
+
|
1398
|
+
if (gi->cache == NULL) {
|
1399
|
+
fseek(gi->GeoIPDatabase, record_pointer, SEEK_SET);
|
1400
|
+
silence = fread(buf, sizeof(char), MAX_ORG_RECORD_LENGTH, gi->GeoIPDatabase);
|
1401
|
+
len = sizeof(char) * (strlen(buf)+1);
|
1402
|
+
org_buf = malloc(len);
|
1403
|
+
strncpy(org_buf, buf, len);
|
1404
|
+
} else {
|
1405
|
+
buf_pointer = (char *)(gi->cache + (long)record_pointer);
|
1406
|
+
len = sizeof(char) * (strlen(buf_pointer)+1);
|
1407
|
+
org_buf = malloc(len);
|
1408
|
+
strncpy(org_buf, buf_pointer, len);
|
1409
|
+
}
|
1410
|
+
return org_buf;
|
1411
|
+
}
|
1412
|
+
|
1413
|
+
char *_GeoIP_num_to_addr (GeoIP* gi, unsigned long ipnum) {
|
1414
|
+
char *ret_str;
|
1415
|
+
char *cur_str;
|
1416
|
+
int octet[4];
|
1417
|
+
int num_chars_written, i;
|
1418
|
+
|
1419
|
+
ret_str = malloc(sizeof(char) * 16);
|
1420
|
+
cur_str = ret_str;
|
1421
|
+
|
1422
|
+
for (i = 0; i<4; i++) {
|
1423
|
+
octet[3 - i] = ipnum % 256;
|
1424
|
+
ipnum >>= 8;
|
1425
|
+
}
|
1426
|
+
|
1427
|
+
for (i = 0; i<4; i++) {
|
1428
|
+
num_chars_written = sprintf(cur_str, "%d", octet[i]);
|
1429
|
+
cur_str += num_chars_written;
|
1430
|
+
|
1431
|
+
if (i < 3) {
|
1432
|
+
cur_str[0] = '.';
|
1433
|
+
cur_str++;
|
1434
|
+
}
|
1435
|
+
}
|
1436
|
+
|
1437
|
+
return ret_str;
|
1438
|
+
}
|
1439
|
+
|
1440
|
+
char **GeoIP_range_by_ip (GeoIP* gi, const char *addr) {
|
1441
|
+
unsigned long ipnum;
|
1442
|
+
unsigned long left_seek;
|
1443
|
+
unsigned long right_seek;
|
1444
|
+
unsigned long mask;
|
1445
|
+
int orig_netmask;
|
1446
|
+
int target_value;
|
1447
|
+
char **ret;
|
1448
|
+
|
1449
|
+
if (addr == NULL) {
|
1450
|
+
return 0;
|
1451
|
+
}
|
1452
|
+
|
1453
|
+
ret = malloc(sizeof(char *) * 2);
|
1454
|
+
|
1455
|
+
ipnum = _GeoIP_addr_to_num(addr);
|
1456
|
+
target_value = _GeoIP_seek_record(gi, ipnum);
|
1457
|
+
orig_netmask = GeoIP_last_netmask(gi);
|
1458
|
+
mask = 0xffffffff << ( 32 - orig_netmask );
|
1459
|
+
left_seek = ipnum & mask;
|
1460
|
+
right_seek = left_seek + ( 0xffffffff & ~mask );
|
1461
|
+
|
1462
|
+
while (left_seek != 0
|
1463
|
+
&& target_value == _GeoIP_seek_record(gi, left_seek - 1) ) {
|
1464
|
+
|
1465
|
+
/* Go to beginning of netblock defined by netmask */
|
1466
|
+
mask = 0xffffffff << ( 32 - GeoIP_last_netmask(gi) );
|
1467
|
+
left_seek = ( left_seek - 1 ) & mask;
|
1468
|
+
}
|
1469
|
+
ret[0] = _GeoIP_num_to_addr(gi, left_seek);
|
1470
|
+
|
1471
|
+
while (right_seek != 0xffffffff
|
1472
|
+
&& target_value == _GeoIP_seek_record(gi, right_seek + 1) ) {
|
1473
|
+
|
1474
|
+
/* Go to end of netblock defined by netmask */
|
1475
|
+
mask = 0xffffffff << ( 32 - GeoIP_last_netmask(gi) );
|
1476
|
+
right_seek = ( right_seek + 1 ) & mask;
|
1477
|
+
right_seek += 0xffffffff & ~mask;
|
1478
|
+
}
|
1479
|
+
ret[1] = _GeoIP_num_to_addr(gi, right_seek);
|
1480
|
+
|
1481
|
+
gi->netmask = orig_netmask;
|
1482
|
+
|
1483
|
+
return ret;
|
1484
|
+
}
|
1485
|
+
|
1486
|
+
void GeoIP_range_by_ip_delete( char ** ptr ){
|
1487
|
+
if ( ptr ){
|
1488
|
+
if ( ptr[0] )
|
1489
|
+
free(ptr[0]);
|
1490
|
+
if ( ptr[1] )
|
1491
|
+
free(ptr[1]);
|
1492
|
+
free(ptr);
|
1493
|
+
}
|
1494
|
+
}
|
1495
|
+
|
1496
|
+
char *GeoIP_name_by_ipnum (GeoIP* gi, unsigned long ipnum) {
|
1497
|
+
return _get_name(gi,ipnum);
|
1498
|
+
}
|
1499
|
+
|
1500
|
+
char *GeoIP_name_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum) {
|
1501
|
+
return _get_name_v6(gi,ipnum);
|
1502
|
+
}
|
1503
|
+
|
1504
|
+
char *GeoIP_name_by_addr (GeoIP* gi, const char *addr) {
|
1505
|
+
unsigned long ipnum;
|
1506
|
+
if (addr == NULL) {
|
1507
|
+
return 0;
|
1508
|
+
}
|
1509
|
+
ipnum = _GeoIP_addr_to_num(addr);
|
1510
|
+
return _get_name(gi, ipnum);
|
1511
|
+
}
|
1512
|
+
|
1513
|
+
char *GeoIP_name_by_addr_v6 (GeoIP* gi, const char *addr) {
|
1514
|
+
geoipv6_t ipnum;
|
1515
|
+
if (addr == NULL) {
|
1516
|
+
return 0;
|
1517
|
+
}
|
1518
|
+
ipnum = _GeoIP_addr_to_num_v6(addr);
|
1519
|
+
return _get_name_v6(gi, ipnum);
|
1520
|
+
}
|
1521
|
+
|
1522
|
+
char *GeoIP_name_by_name (GeoIP* gi, const char *name) {
|
1523
|
+
unsigned long ipnum;
|
1524
|
+
if (name == NULL) {
|
1525
|
+
return 0;
|
1526
|
+
}
|
1527
|
+
if (!(ipnum = _GeoIP_lookupaddress(name)))
|
1528
|
+
return 0;
|
1529
|
+
return _get_name(gi, ipnum);
|
1530
|
+
}
|
1531
|
+
|
1532
|
+
char *GeoIP_name_by_name_v6 (GeoIP* gi, const char *name) {
|
1533
|
+
geoipv6_t ipnum;
|
1534
|
+
if (name == NULL) {
|
1535
|
+
return 0;
|
1536
|
+
}
|
1537
|
+
ipnum = _GeoIP_lookupaddress_v6(name);
|
1538
|
+
if (__GEOIP_V6_IS_NULL(ipnum))
|
1539
|
+
return 0;
|
1540
|
+
return _get_name_v6(gi, ipnum);
|
1541
|
+
}
|
1542
|
+
|
1543
|
+
char *GeoIP_org_by_ipnum (GeoIP* gi, unsigned long ipnum) {
|
1544
|
+
return GeoIP_name_by_ipnum(gi, ipnum);
|
1545
|
+
}
|
1546
|
+
|
1547
|
+
char *GeoIP_org_by_ipnum_v6 (GeoIP* gi, geoipv6_t ipnum) {
|
1548
|
+
return GeoIP_name_by_ipnum_v6(gi, ipnum);
|
1549
|
+
}
|
1550
|
+
|
1551
|
+
char *GeoIP_org_by_addr (GeoIP* gi, const char *addr) {
|
1552
|
+
return GeoIP_name_by_addr(gi, addr);
|
1553
|
+
}
|
1554
|
+
|
1555
|
+
char *GeoIP_org_by_addr_v6 (GeoIP* gi, const char *addr) {
|
1556
|
+
return GeoIP_name_by_addr_v6(gi, addr);
|
1557
|
+
}
|
1558
|
+
|
1559
|
+
char *GeoIP_org_by_name (GeoIP* gi, const char *name) {
|
1560
|
+
return GeoIP_name_by_name(gi, name);
|
1561
|
+
}
|
1562
|
+
|
1563
|
+
char *GeoIP_org_by_name_v6 (GeoIP* gi, const char *name) {
|
1564
|
+
return GeoIP_name_by_name_v6(gi, name);
|
1565
|
+
}
|
1566
|
+
|
1567
|
+
unsigned char GeoIP_database_edition (GeoIP* gi) {
|
1568
|
+
return gi->databaseType;
|
1569
|
+
}
|
1570
|
+
|
1571
|
+
int GeoIP_charset( GeoIP* gi){
|
1572
|
+
return gi->charset;
|
1573
|
+
}
|
1574
|
+
|
1575
|
+
int GeoIP_set_charset( GeoIP* gi, int charset ){
|
1576
|
+
int old_charset = gi->charset;
|
1577
|
+
gi->charset = charset;
|
1578
|
+
return old_charset;
|
1579
|
+
}
|
1580
|
+
|
1581
|
+
int GeoIP_last_netmask (GeoIP* gi) {
|
1582
|
+
return gi->netmask;
|
1583
|
+
}
|
1584
|
+
|
1585
|
+
|
1586
|
+
/** return two letter country code */
|
1587
|
+
const char* GeoIP_code_by_id(int id)
|
1588
|
+
{
|
1589
|
+
if (id < 0 || id >= (int) num_GeoIP_countries)
|
1590
|
+
return NULL;
|
1591
|
+
|
1592
|
+
return GeoIP_country_code[id];
|
1593
|
+
}
|
1594
|
+
|
1595
|
+
/** return three letter country code */
|
1596
|
+
const char* GeoIP_code3_by_id(int id)
|
1597
|
+
{
|
1598
|
+
if (id < 0 || id >= (int) num_GeoIP_countries)
|
1599
|
+
return NULL;
|
1600
|
+
|
1601
|
+
return GeoIP_country_code3[id];
|
1602
|
+
}
|
1603
|
+
|
1604
|
+
|
1605
|
+
/** return full name of country */
|
1606
|
+
const char* GeoIP_name_by_id(int id)
|
1607
|
+
{
|
1608
|
+
if (id < 0 || id >= (int) num_GeoIP_countries)
|
1609
|
+
return NULL;
|
1610
|
+
|
1611
|
+
return GeoIP_country_name[id];
|
1612
|
+
}
|
1613
|
+
|
1614
|
+
/** return continent of country */
|
1615
|
+
const char* GeoIP_continent_by_id(int id)
|
1616
|
+
{
|
1617
|
+
if (id < 0 || id >= (int) num_GeoIP_countries)
|
1618
|
+
return NULL;
|
1619
|
+
|
1620
|
+
return GeoIP_country_continent[id];
|
1621
|
+
}
|
1622
|
+
|
1623
|
+
/** return id by country code **/
|
1624
|
+
int GeoIP_id_by_code(const char *country)
|
1625
|
+
{
|
1626
|
+
unsigned i;
|
1627
|
+
|
1628
|
+
for ( i = 0; i < num_GeoIP_countries; ++i)
|
1629
|
+
{
|
1630
|
+
if (strcmp(country, GeoIP_country_code[i]) == 0)
|
1631
|
+
return i;
|
1632
|
+
}
|
1633
|
+
|
1634
|
+
return 0;
|
1635
|
+
}
|
1636
|
+
|
1637
|
+
unsigned GeoIP_num_countries(void)
|
1638
|
+
{
|
1639
|
+
return num_GeoIP_countries;
|
1640
|
+
}
|