ghazel-geoip 0.8.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,16 @@
1
+ == 0.8.1 2009-06-04
2
+
3
+ * Added GeoIP#close method to close the file descriptor
4
+
5
+ == 0.8.0 2008-08-29
6
+
7
+ * Added Mutex protection around file I/O for thread safety
8
+
9
+ == 0.7.0 2008-05-03
10
+
11
+ * Added Hez Ronningen's patch for using the ISP lookup data file
12
+
13
+ == 0.5.0 2007-10-24
14
+
15
+ * 1 major enhancement:
16
+ * Initial release
@@ -0,0 +1,7 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/geoip.rb
6
+ test/test_geoip.rb
7
+ test/test_helper.rb
@@ -0,0 +1,64 @@
1
+ = geoip
2
+
3
+ * http://github.com/cjheath/geoip
4
+
5
+ == DESCRIPTION:
6
+
7
+ GeoIP searches a GeoIP database for a given host or IP address, and
8
+ returns information about the country where the IP address is allocated,
9
+ and the city, ISP and other information, if you have that database version.
10
+
11
+ == FEATURES/PROBLEMS:
12
+
13
+ This release adds support for ASN data files, thanks to Roland Matiz
14
+
15
+ == SYNOPSIS:
16
+
17
+ require 'geoip'
18
+ GeoIP.new('GeoIP.dat').country("www.netscape.sk")
19
+ => ["www.netscape.sk", "217.67.16.35", 196, "SK", "SVK", "Slovakia", "EU"]
20
+
21
+ GeoIP.new('GeoIPASNum.dat').asn("www.fsb.ru")
22
+ => ["AS8342", "RTComm.RU Autonomous System"]
23
+
24
+ == REQUIREMENTS:
25
+
26
+ You need at least the free GeoIP.dat, for which the last known download
27
+ location is <http://www.maxmind.com/download/geoip/database/GeoIP.dat.gz>,
28
+ or the city database from <http://www.maxmind.com/app/geolitecity>.
29
+ The ASN database location is
30
+ <http://geolite.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz>.
31
+
32
+ This API requires the file to be decompressed for searching. Other versions
33
+ of this database are available for purchase which contain more detailed
34
+ information, but this information is not returned by this implementation.
35
+ See www.maxmind.com for more information.
36
+
37
+ == INSTALL:
38
+
39
+ sudo gem install geoip
40
+
41
+ == LICENSE:
42
+
43
+ (The MIT License)
44
+
45
+ Copyright (c) 2009 Clifford Heath
46
+
47
+ Permission is hereby granted, free of charge, to any person obtaining
48
+ a copy of this software and associated documentation files (the
49
+ 'Software'), to deal in the Software without restriction, including
50
+ without limitation the rights to use, copy, modify, merge, publish,
51
+ distribute, sublicense, and/or sell copies of the Software, and to
52
+ permit persons to whom the Software is furnished to do so, subject to
53
+ the following conditions:
54
+
55
+ The above copyright notice and this permission notice shall be
56
+ included in all copies or substantial portions of the Software.
57
+
58
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
59
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
60
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
61
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
62
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
63
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
64
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/geoip'
6
+
7
+ Hoe.plugin :newgem
8
+ Hoe.plugin :website
9
+
10
+ # Generate all the Rake tasks
11
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
12
+ $hoe = Hoe.spec 'geoip' do
13
+ self.developer 'Clifford Heath', 'clifford.heath@gmail.com'
14
+ self.developer 'Roland Moriz', 'rmoriz@gmail.com'
15
+ self.rubyforge_name = self.name # TODO this is default value
16
+ end
17
+
18
+ require 'newgem/tasks'
19
+ Dir['tasks/**/*.rake'].each { |t| load t }
20
+
21
+ # TODO - want other tests/tasks run by default? Add them to the list
22
+ # remove_task :default
23
+ # task :default => [:spec, :features]
@@ -0,0 +1,777 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ #
3
+ # Native Ruby reader for the GeoIP database
4
+ # Lookup the country where IP address is allocated
5
+ #
6
+ #= COPYRIGHT
7
+ # This version Copyright (C) 2005 Clifford Heath
8
+ # Derived from the C version, Copyright (C) 2003 MaxMind LLC
9
+ #
10
+ # This library is free software; you can redistribute it and/or
11
+ # modify it under the terms of the GNU General Public
12
+ # License as published by the Free Software Foundation; either
13
+ # version 2.1 of the License, or (at your option) any later version.
14
+ #
15
+ # This library is distributed in the hope that it will be useful,
16
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
+ # General Public License for more details.
19
+ #
20
+ # You should have received a copy of the GNU General Public
21
+ # License along with this library; if not, write to the Free Software
22
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
+ #
24
+ #= SYNOPSIS
25
+ #
26
+ # require 'geoip'
27
+ # p GeoIP.new('/usr/share/GeoIP/GeoIP.dat').country("www.netscape.sk")
28
+ #
29
+ #= DESCRIPTION
30
+ #
31
+ # GeoIP searches a GeoIP database for a given host or IP address, and
32
+ # returns information about the country where the IP address is allocated.
33
+ #
34
+ #= PREREQUISITES
35
+ #
36
+ # You need at least the free GeoIP.dat, for which the last known download
37
+ # location is <http://www.maxmind.com/download/geoip/database/GeoIP.dat.gz>
38
+ # This API requires the file to be decompressed for searching. Other versions
39
+ # of this database are available for purchase which contain more detailed
40
+ # information, but this information is not returned by this implementation.
41
+ # See www.maxmind.com for more information.
42
+ #
43
+ #=end
44
+ require 'thread' # Needed for Mutex
45
+ require 'socket'
46
+
47
+ class GeoIP
48
+ VERSION = "0.8.4"
49
+ private
50
+ CountryCode = [
51
+ "--","AP","EU","AD","AE","AF","AG","AI","AL","AM","AN",
52
+ "AO","AQ","AR","AS","AT","AU","AW","AZ","BA","BB",
53
+ "BD","BE","BF","BG","BH","BI","BJ","BM","BN","BO",
54
+ "BR","BS","BT","BV","BW","BY","BZ","CA","CC","CD",
55
+ "CF","CG","CH","CI","CK","CL","CM","CN","CO","CR",
56
+ "CU","CV","CX","CY","CZ","DE","DJ","DK","DM","DO",
57
+ "DZ","EC","EE","EG","EH","ER","ES","ET","FI","FJ",
58
+ "FK","FM","FO","FR","FX","GA","GB","GD","GE","GF",
59
+ "GH","GI","GL","GM","GN","GP","GQ","GR","GS","GT",
60
+ "GU","GW","GY","HK","HM","HN","HR","HT","HU","ID",
61
+ "IE","IL","IN","IO","IQ","IR","IS","IT","JM","JO",
62
+ "JP","KE","KG","KH","KI","KM","KN","KP","KR","KW",
63
+ "KY","KZ","LA","LB","LC","LI","LK","LR","LS","LT",
64
+ "LU","LV","LY","MA","MC","MD","MG","MH","MK","ML",
65
+ "MM","MN","MO","MP","MQ","MR","MS","MT","MU","MV",
66
+ "MW","MX","MY","MZ","NA","NC","NE","NF","NG","NI",
67
+ "NL","NO","NP","NR","NU","NZ","OM","PA","PE","PF",
68
+ "PG","PH","PK","PL","PM","PN","PR","PS","PT","PW",
69
+ "PY","QA","RE","RO","RU","RW","SA","SB","SC","SD",
70
+ "SE","SG","SH","SI","SJ","SK","SL","SM","SN","SO",
71
+ "SR","ST","SV","SY","SZ","TC","TD","TF","TG","TH",
72
+ "TJ","TK","TM","TN","TO","TL","TR","TT","TV","TW",
73
+ "TZ","UA","UG","UM","US","UY","UZ","VA","VC","VE",
74
+ "VG","VI","VN","VU","WF","WS","YE","YT","RS","ZA",
75
+ "ZM","ME","ZW","A1","A2","O1","AX","GG","IM","JE",
76
+ "BL","MF"
77
+ ]
78
+
79
+ CountryCode3 = [
80
+ "--","AP","EU","AND","ARE","AFG","ATG","AIA","ALB","ARM","ANT",
81
+ "AGO","AQ","ARG","ASM","AUT","AUS","ABW","AZE","BIH","BRB",
82
+ "BGD","BEL","BFA","BGR","BHR","BDI","BEN","BMU","BRN","BOL",
83
+ "BRA","BHS","BTN","BV","BWA","BLR","BLZ","CAN","CC","COD",
84
+ "CAF","COG","CHE","CIV","COK","CHL","CMR","CHN","COL","CRI",
85
+ "CUB","CPV","CX","CYP","CZE","DEU","DJI","DNK","DMA","DOM",
86
+ "DZA","ECU","EST","EGY","ESH","ERI","ESP","ETH","FIN","FJI",
87
+ "FLK","FSM","FRO","FRA","FX","GAB","GBR","GRD","GEO","GUF",
88
+ "GHA","GIB","GRL","GMB","GIN","GLP","GNQ","GRC","GS","GTM",
89
+ "GUM","GNB","GUY","HKG","HM","HND","HRV","HTI","HUN","IDN",
90
+ "IRL","ISR","IND","IO","IRQ","IRN","ISL","ITA","JAM","JOR",
91
+ "JPN","KEN","KGZ","KHM","KIR","COM","KNA","PRK","KOR","KWT",
92
+ "CYM","KAZ","LAO","LBN","LCA","LIE","LKA","LBR","LSO","LTU",
93
+ "LUX","LVA","LBY","MAR","MCO","MDA","MDG","MHL","MKD","MLI",
94
+ "MMR","MNG","MAC","MNP","MTQ","MRT","MSR","MLT","MUS","MDV",
95
+ "MWI","MEX","MYS","MOZ","NAM","NCL","NER","NFK","NGA","NIC",
96
+ "NLD","NOR","NPL","NRU","NIU","NZL","OMN","PAN","PER","PYF",
97
+ "PNG","PHL","PAK","POL","SPM","PCN","PRI","PSE","PRT","PLW",
98
+ "PRY","QAT","REU","ROU","RUS","RWA","SAU","SLB","SYC","SDN",
99
+ "SWE","SGP","SHN","SVN","SJM","SVK","SLE","SMR","SEN","SOM",
100
+ "SUR","STP","SLV","SYR","SWZ","TCA","TCD","TF","TGO","THA",
101
+ "TJK","TKL","TKM","TUN","TON","TLS","TUR","TTO","TUV","TWN",
102
+ "TZA","UKR","UGA","UM","USA","URY","UZB","VAT","VCT","VEN",
103
+ "VGB","VIR","VNM","VUT","WLF","WSM","YEM","YT","SRB","ZAF",
104
+ "ZMB","MNE","ZWE","A1","A2","O1","ALA","GGY","IMN","JEY",
105
+ "BLM","MAF"
106
+ ]
107
+
108
+ CountryName = [
109
+ "N/A",
110
+ "Asia/Pacific Region",
111
+ "Europe",
112
+ "Andorra",
113
+ "United Arab Emirates",
114
+ "Afghanistan",
115
+ "Antigua and Barbuda",
116
+ "Anguilla",
117
+ "Albania",
118
+ "Armenia",
119
+ "Netherlands Antilles",
120
+ "Angola",
121
+ "Antarctica",
122
+ "Argentina",
123
+ "American Samoa",
124
+ "Austria",
125
+ "Australia",
126
+ "Aruba",
127
+ "Azerbaijan",
128
+ "Bosnia and Herzegovina",
129
+ "Barbados",
130
+ "Bangladesh",
131
+ "Belgium",
132
+ "Burkina Faso",
133
+ "Bulgaria",
134
+ "Bahrain",
135
+ "Burundi",
136
+ "Benin",
137
+ "Bermuda",
138
+ "Brunei Darussalam",
139
+ "Bolivia",
140
+ "Brazil",
141
+ "Bahamas",
142
+ "Bhutan",
143
+ "Bouvet Island",
144
+ "Botswana",
145
+ "Belarus",
146
+ "Belize",
147
+ "Canada",
148
+ "Cocos (Keeling) Islands",
149
+ "Congo, the Democratic Republic of the",
150
+ "Central African Republic",
151
+ "Congo",
152
+ "Switzerland",
153
+ "Cote D'Ivoire",
154
+ "Cook Islands",
155
+ "Chile",
156
+ "Cameroon",
157
+ "China",
158
+ "Colombia",
159
+ "Costa Rica",
160
+ "Cuba",
161
+ "Cape Verde",
162
+ "Christmas Island",
163
+ "Cyprus",
164
+ "Czech Republic",
165
+ "Germany",
166
+ "Djibouti",
167
+ "Denmark",
168
+ "Dominica",
169
+ "Dominican Republic",
170
+ "Algeria",
171
+ "Ecuador",
172
+ "Estonia",
173
+ "Egypt",
174
+ "Western Sahara",
175
+ "Eritrea",
176
+ "Spain",
177
+ "Ethiopia",
178
+ "Finland",
179
+ "Fiji",
180
+ "Falkland Islands (Malvinas)",
181
+ "Micronesia, Federated States of",
182
+ "Faroe Islands",
183
+ "France",
184
+ "France, Metropolitan",
185
+ "Gabon",
186
+ "United Kingdom",
187
+ "Grenada",
188
+ "Georgia",
189
+ "French Guiana",
190
+ "Ghana",
191
+ "Gibraltar",
192
+ "Greenland",
193
+ "Gambia",
194
+ "Guinea",
195
+ "Guadeloupe",
196
+ "Equatorial Guinea",
197
+ "Greece",
198
+ "South Georgia and the South Sandwich Islands",
199
+ "Guatemala",
200
+ "Guam",
201
+ "Guinea-Bissau",
202
+ "Guyana",
203
+ "Hong Kong",
204
+ "Heard Island and McDonald Islands",
205
+ "Honduras",
206
+ "Croatia",
207
+ "Haiti",
208
+ "Hungary",
209
+ "Indonesia",
210
+ "Ireland",
211
+ "Israel",
212
+ "India",
213
+ "British Indian Ocean Territory",
214
+ "Iraq",
215
+ "Iran, Islamic Republic of",
216
+ "Iceland",
217
+ "Italy",
218
+ "Jamaica",
219
+ "Jordan",
220
+ "Japan",
221
+ "Kenya",
222
+ "Kyrgyzstan",
223
+ "Cambodia",
224
+ "Kiribati",
225
+ "Comoros",
226
+ "Saint Kitts and Nevis",
227
+ "Korea, Democratic People's Republic of",
228
+ "Korea, Republic of",
229
+ "Kuwait",
230
+ "Cayman Islands",
231
+ "Kazakhstan",
232
+ "Lao People's Democratic Republic",
233
+ "Lebanon",
234
+ "Saint Lucia",
235
+ "Liechtenstein",
236
+ "Sri Lanka",
237
+ "Liberia",
238
+ "Lesotho",
239
+ "Lithuania",
240
+ "Luxembourg",
241
+ "Latvia",
242
+ "Libyan Arab Jamahiriya",
243
+ "Morocco",
244
+ "Monaco",
245
+ "Moldova, Republic of",
246
+ "Madagascar",
247
+ "Marshall Islands",
248
+ "Macedonia, the Former Yugoslav Republic of",
249
+ "Mali",
250
+ "Myanmar",
251
+ "Mongolia",
252
+ "Macau",
253
+ "Northern Mariana Islands",
254
+ "Martinique",
255
+ "Mauritania",
256
+ "Montserrat",
257
+ "Malta",
258
+ "Mauritius",
259
+ "Maldives",
260
+ "Malawi",
261
+ "Mexico",
262
+ "Malaysia",
263
+ "Mozambique",
264
+ "Namibia",
265
+ "New Caledonia",
266
+ "Niger",
267
+ "Norfolk Island",
268
+ "Nigeria",
269
+ "Nicaragua",
270
+ "Netherlands",
271
+ "Norway",
272
+ "Nepal",
273
+ "Nauru",
274
+ "Niue",
275
+ "New Zealand",
276
+ "Oman",
277
+ "Panama",
278
+ "Peru",
279
+ "French Polynesia",
280
+ "Papua New Guinea",
281
+ "Philippines",
282
+ "Pakistan",
283
+ "Poland",
284
+ "Saint Pierre and Miquelon",
285
+ "Pitcairn",
286
+ "Puerto Rico",
287
+ "Palestinian Territory, Occupied",
288
+ "Portugal",
289
+ "Palau",
290
+ "Paraguay",
291
+ "Qatar",
292
+ "Reunion",
293
+ "Romania",
294
+ "Russian Federation",
295
+ "Rwanda",
296
+ "Saudi Arabia",
297
+ "Solomon Islands",
298
+ "Seychelles",
299
+ "Sudan",
300
+ "Sweden",
301
+ "Singapore",
302
+ "Saint Helena",
303
+ "Slovenia",
304
+ "Svalbard and Jan Mayen",
305
+ "Slovakia",
306
+ "Sierra Leone",
307
+ "San Marino",
308
+ "Senegal",
309
+ "Somalia",
310
+ "Suriname",
311
+ "Sao Tome and Principe",
312
+ "El Salvador",
313
+ "Syrian Arab Republic",
314
+ "Swaziland",
315
+ "Turks and Caicos Islands",
316
+ "Chad",
317
+ "French Southern Territories",
318
+ "Togo",
319
+ "Thailand",
320
+ "Tajikistan",
321
+ "Tokelau",
322
+ "Turkmenistan",
323
+ "Tunisia",
324
+ "Tonga",
325
+ "Timor-Leste",
326
+ "Turkey",
327
+ "Trinidad and Tobago",
328
+ "Tuvalu",
329
+ "Taiwan, Province of China",
330
+ "Tanzania, United Republic of",
331
+ "Ukraine",
332
+ "Uganda",
333
+ "United States Minor Outlying Islands",
334
+ "United States",
335
+ "Uruguay",
336
+ "Uzbekistan",
337
+ "Holy See (Vatican City State)",
338
+ "Saint Vincent and the Grenadines",
339
+ "Venezuela",
340
+ "Virgin Islands, British",
341
+ "Virgin Islands, U.S.",
342
+ "Viet Nam",
343
+ "Vanuatu",
344
+ "Wallis and Futuna",
345
+ "Samoa",
346
+ "Yemen",
347
+ "Mayotte",
348
+ "Serbia",
349
+ "South Africa",
350
+ "Zambia",
351
+ "Montenegro",
352
+ "Zimbabwe",
353
+ "Anonymous Proxy",
354
+ "Satellite Provider",
355
+ "Other",
356
+ "Aland Islands",
357
+ "Guernsey",
358
+ "Isle of Man",
359
+ "Jersey",
360
+ "Saint Barthelemy",
361
+ "Saint Martin"
362
+ ]
363
+
364
+ CountryContinent = [
365
+ "--","AS","EU","EU","AS","AS","SA","SA","EU","AS","SA",
366
+ "AF","AN","SA","OC","EU","OC","SA","AS","EU","SA",
367
+ "AS","EU","AF","EU","AS","AF","AF","SA","AS","SA",
368
+ "SA","SA","AS","AF","AF","EU","SA","NA","AS","AF",
369
+ "AF","AF","EU","AF","OC","SA","AF","AS","SA","SA",
370
+ "SA","AF","AS","AS","EU","EU","AF","EU","SA","SA",
371
+ "AF","SA","EU","AF","AF","AF","EU","AF","EU","OC",
372
+ "SA","OC","EU","EU","EU","AF","EU","SA","AS","SA",
373
+ "AF","EU","SA","AF","AF","SA","AF","EU","SA","SA",
374
+ "OC","AF","SA","AS","AF","SA","EU","SA","EU","AS",
375
+ "EU","AS","AS","AS","AS","AS","EU","EU","SA","AS",
376
+ "AS","AF","AS","AS","OC","AF","SA","AS","AS","AS",
377
+ "SA","AS","AS","AS","SA","EU","AS","AF","AF","EU",
378
+ "EU","EU","AF","AF","EU","EU","AF","OC","EU","AF",
379
+ "AS","AS","AS","OC","SA","AF","SA","EU","AF","AS",
380
+ "AF","NA","AS","AF","AF","OC","AF","OC","AF","SA",
381
+ "EU","EU","AS","OC","OC","OC","AS","SA","SA","OC",
382
+ "OC","AS","AS","EU","SA","OC","SA","AS","EU","OC",
383
+ "SA","AS","AF","EU","AS","AF","AS","OC","AF","AF",
384
+ "EU","AS","AF","EU","EU","EU","AF","EU","AF","AF",
385
+ "SA","AF","SA","AS","AF","SA","AF","AF","AF","AS",
386
+ "AS","OC","AS","AF","OC","AS","AS","SA","OC","AS",
387
+ "AF","EU","AF","OC","NA","SA","AS","EU","SA","SA",
388
+ "SA","SA","AS","OC","OC","OC","AS","AF","EU","AF",
389
+ "AF","EU","AF","--","--","--","EU","EU","EU","EU",
390
+ "SA","SA"
391
+ ]
392
+
393
+ public
394
+ # Edition enumeration:
395
+ (GEOIP_COUNTRY_EDITION,
396
+ GEOIP_CITY_EDITION_REV1,
397
+ GEOIP_REGION_EDITION_REV1,
398
+ GEOIP_ISP_EDITION,
399
+ GEOIP_ORG_EDITION,
400
+ GEOIP_CITY_EDITION_REV0,
401
+ GEOIP_REGION_EDITION_REV0,
402
+ GEOIP_PROXY_EDITION,
403
+ GEOIP_ASNUM_EDITION,
404
+ GEOIP_NETSPEED_EDITION,
405
+ ) = *1..10
406
+
407
+ private
408
+ COUNTRY_BEGIN = 16776960
409
+ STATE_BEGIN_REV0 = 16700000
410
+ STATE_BEGIN_REV1 = 16000000
411
+ STRUCTURE_INFO_MAX_SIZE = 20
412
+ DATABASE_INFO_MAX_SIZE = 100
413
+ MAX_ORG_RECORD_LENGTH = 300
414
+ MAX_ASN_RECORD_LENGTH = 300 # unverified
415
+ US_OFFSET = 1
416
+ CANADA_OFFSET = 677
417
+ WORLD_OFFSET = 1353
418
+ FIPS_RANGE = 360
419
+ FULL_RECORD_LENGTH = 50
420
+
421
+ STANDARD_RECORD_LENGTH = 3
422
+ SEGMENT_RECORD_LENGTH = 3
423
+
424
+ public
425
+ attr_reader :databaseType
426
+
427
+ # Open the GeoIP database and determine the file format version
428
+ #
429
+ # +filename+ is a String holding the path to the GeoIP.dat file
430
+ # +options+ is an integer holding caching flags (unimplemented)
431
+ def initialize(filename, flags = 0)
432
+ @mutex = Mutex.new
433
+ @flags = flags
434
+ @databaseType = GEOIP_COUNTRY_EDITION
435
+ @record_length = STANDARD_RECORD_LENGTH
436
+ @file = File.open(filename, 'rb')
437
+ @file.seek(-3, IO::SEEK_END)
438
+ 0.upto(STRUCTURE_INFO_MAX_SIZE-1) { |i|
439
+ if @file.read(3) == "\xFF\xFF\xFF"
440
+ @databaseType = @file.respond_to?(:getbyte) ? @file.getbyte : @file.getc
441
+ @databaseType -= 105 if @databaseType >= 106
442
+
443
+ if (@databaseType == GEOIP_REGION_EDITION_REV0)
444
+ # Region Edition, pre June 2003
445
+ @databaseSegments = [ STATE_BEGIN_REV0 ]
446
+ elsif (@databaseType == GEOIP_REGION_EDITION_REV1)
447
+ # Region Edition, post June 2003
448
+ @databaseSegments = [ STATE_BEGIN_REV1 ]
449
+ elsif (@databaseType == GEOIP_CITY_EDITION_REV0 ||
450
+ @databaseType == GEOIP_CITY_EDITION_REV1 ||
451
+ @databaseType == GEOIP_ORG_EDITION ||
452
+ @databaseType == GEOIP_ISP_EDITION ||
453
+ @databaseType == GEOIP_ASNUM_EDITION)
454
+ # City/Org Editions have two segments, read offset of second segment
455
+ @databaseSegments = [ 0 ]
456
+ sr = @file.read(3).unpack("C*")
457
+ @databaseSegments[0] += le_to_ui(sr)
458
+
459
+ if (@databaseType == GEOIP_ORG_EDITION ||
460
+ @databaseType == GEOIP_ISP_EDITION)
461
+ @record_length = 4
462
+ end
463
+ end
464
+ break
465
+
466
+ else
467
+ @file.seek(-4, IO::SEEK_CUR)
468
+ end
469
+ }
470
+ if (@databaseType == GEOIP_COUNTRY_EDITION ||
471
+ @databaseType == GEOIP_PROXY_EDITION ||
472
+ @databaseType == GEOIP_NETSPEED_EDITION)
473
+ @databaseSegments = [ COUNTRY_BEGIN ]
474
+ end
475
+ end
476
+
477
+ # Search the GeoIP database for the specified host, returning country info
478
+ #
479
+ # +hostname+ is a String holding the host's DNS name or numeric IP address.
480
+ # Return an array of seven elements:
481
+ # * The host or IP address string as requested
482
+ # * The IP address string after looking up the host
483
+ # * The GeoIP country-ID as an integer
484
+ # * The ISO3166-1 two-character country code
485
+ # * The ISO3166-2 three-character country code
486
+ # * The ISO3166 English-language name of the country
487
+ # * The two-character continent code
488
+ #
489
+ def country(hostname)
490
+ if (@databaseType == GEOIP_CITY_EDITION_REV0 ||
491
+ @databaseType == GEOIP_CITY_EDITION_REV1)
492
+ return city(hostname)
493
+ end
494
+
495
+ ip = hostname
496
+ if ip.kind_of?(String) && ip !~ /^[0-9.]*$/
497
+ # Lookup IP address, we were given a name
498
+ ip = IPSocket.getaddress(hostname)
499
+ ip = '0.0.0.0' if ip == '::1'
500
+ end
501
+
502
+ # Convert numeric IP address to an integer
503
+ ipnum = iptonum(ip)
504
+ if (@databaseType != GEOIP_COUNTRY_EDITION &&
505
+ @databaseType != GEOIP_PROXY_EDITION &&
506
+ @databaseType != GEOIP_NETSPEED_EDITION)
507
+ throw "Invalid GeoIP database type, can't look up Country by IP"
508
+ end
509
+ code = seek_record(ipnum) - COUNTRY_BEGIN;
510
+ [ hostname, # Requested hostname
511
+ ip, # Ip address as dotted quad
512
+ code, # GeoIP's country code
513
+ CountryCode[code], # ISO3166-1 code
514
+ CountryCode3[code], # ISO3166-2 code
515
+ CountryName[code], # Country name, per IS03166
516
+ CountryContinent[code] ] # Continent code.
517
+ end
518
+
519
+ # Search the GeoIP database for the specified host, returning city info
520
+ #
521
+ # +hostname+ is a String holding the host's DNS name or numeric IP address
522
+ # Return an array of twelve or fourteen elements:
523
+ # * All elements from the country query
524
+ # * The region (state or territory) name
525
+ # * The city name
526
+ # * The postal code (zipcode)
527
+ # * The latitude
528
+ # * The longitude
529
+ # * The dma_code and area_code, if available (REV1 City database)
530
+ private
531
+
532
+ def read_city(pos, hostname = '', ip = '')
533
+ record = ""
534
+ @mutex.synchronize {
535
+ @file.seek(pos + (2*@record_length-1) * @databaseSegments[0])
536
+ return nil unless record = @file.read(FULL_RECORD_LENGTH)
537
+ }
538
+
539
+ # The country code is the first byte:
540
+ code = record[0]
541
+ code = code.ord if code.respond_to?(:ord)
542
+ record = record[1..-1]
543
+ @iter_pos += 1 unless @iter_pos.nil?
544
+
545
+ spl = record.split("\x00", 4)
546
+ # Get the region:
547
+ region = spl[0]
548
+ @iter_pos += (region.size + 1) unless @iter_pos.nil?
549
+
550
+ # Get the city:
551
+ city = spl[1]
552
+ @iter_pos += (city.size + 1) unless @iter_pos.nil?
553
+
554
+ # Get the postal code:
555
+ postal_code = spl[2]
556
+ @iter_pos += (postal_code.size + 1) unless @iter_pos.nil?
557
+
558
+ record = spl[3]
559
+ # Get the latitude/longitude:
560
+ if(record && record[0,3]) then
561
+ latitude = le_to_ui(record[0,3].unpack('C*')) / 10000.0 - 180
562
+ record = record[3..-1]
563
+ @iter_pos += 3 unless @iter_pos.nil?
564
+ else
565
+ latitude = ''
566
+ end
567
+ if(record && record[0,3]) then
568
+ longitude = le_to_ui(record[0,3].unpack('C*')) / 10000.0 - 180
569
+ record = record[3..-1]
570
+ @iter_pos += 3 unless @iter_pos.nil?
571
+ else
572
+ longitude = ''
573
+ end
574
+
575
+ us_area_codes = []
576
+ if (record &&
577
+ record[0,3] &&
578
+ @databaseType == GEOIP_CITY_EDITION_REV1 &&
579
+ CountryCode[code] == "US") # UNTESTED
580
+ dmaarea_combo = le_to_ui(record[0,3].unpack('C*'))
581
+ dma_code = dmaarea_combo / 1000;
582
+ area_code = dmaarea_combo % 1000;
583
+ us_area_codes = [ dma_code, area_code ]
584
+ @iter_pos += 3 unless @iter_pos.nil?
585
+ end
586
+
587
+ [ hostname, # Requested hostname
588
+ ip, # Ip address as dotted quad
589
+ CountryCode[code], # ISO3166-1 code
590
+ CountryCode3[code], # ISO3166-2 code
591
+ CountryName[code], # Country name, per IS03166
592
+ CountryContinent[code], # Continent code.
593
+ region, # Region name
594
+ city, # City name
595
+ postal_code, # Postal code
596
+ latitude,
597
+ longitude,
598
+ ] + us_area_codes
599
+ end
600
+
601
+ public
602
+
603
+ # Search the GeoIP database for the specified host, returning city info.
604
+ #
605
+ # +hostname+ is a String holding the host's DNS name or numeric IP address.
606
+ # Return an array of twelve or fourteen elements:
607
+ # * The host or IP address string as requested
608
+ # * The IP address string after looking up the host
609
+ # * The GeoIP country-ID as an integer
610
+ # * The ISO3166-1 two-character country code
611
+ # * The ISO3166-2 three-character country code
612
+ # * The ISO3166 English-language name of the country
613
+ # * The two-character continent code
614
+ # * The region name
615
+ # * The city name
616
+ # * The postal code
617
+ # * The latitude
618
+ # * The longitude
619
+ # * The USA dma_code and area_code, if available (REV1 City database)
620
+ #
621
+ def city(hostname)
622
+ ip = hostname
623
+ if ip.kind_of?(String) && ip !~ /^[0-9.]*$/
624
+ # Lookup IP address, we were given a name
625
+ ip = IPSocket.getaddress(hostname)
626
+ ip = '0.0.0.0' if ip == '::1'
627
+ end
628
+
629
+ # Convert numeric IP address to an integer
630
+ ipnum = iptonum(ip)
631
+ if (@databaseType != GEOIP_CITY_EDITION_REV0 &&
632
+ @databaseType != GEOIP_CITY_EDITION_REV1)
633
+ throw "Invalid GeoIP database type, can't look up City by IP"
634
+ end
635
+ pos = seek_record(ipnum);
636
+ return nil if pos == @databaseSegments[0]
637
+ read_city(pos, hostname, ip)
638
+ end
639
+
640
+ # Search a ISP GeoIP database for the specified host, returning the ISP
641
+ #
642
+ # +hostname+ is a String holding the host's DNS name or numeric IP address.
643
+ # Return the ISP name
644
+ #
645
+ def isp(hostname)
646
+ ip = hostname
647
+ if ip.kind_of?(String) && ip !~ /^[0-9.]*$/
648
+ # Lookup IP address, we were given a name
649
+ ip = IPSocket.getaddress(hostname)
650
+ ip = '0.0.0.0' if ip == '::1'
651
+ end
652
+
653
+ # Convert numeric IP address to an integer
654
+ ipnum = iptonum(ip)
655
+ if @databaseType != GEOIP_ISP_EDITION
656
+ throw "Invalid GeoIP database type, can't look up Organization/ISP by IP"
657
+ end
658
+ pos = seek_record(ipnum);
659
+ record = ""
660
+ @mutex.synchronize {
661
+ @file.seek(pos + (2*@record_length-1) * @databaseSegments[0])
662
+ record = @file.read(MAX_ORG_RECORD_LENGTH)
663
+ }
664
+ record = record.sub(/\000.*/n, '')
665
+ record
666
+ end
667
+
668
+ # Search a ASN GeoIP database for the specified host, returning the AS number + description
669
+ #
670
+ # +hostname+ is a String holding the host's DNS name or numeric IP address.
671
+ # Return the AS number + description
672
+ #
673
+ # Source:
674
+ # http://geolite.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz
675
+ #
676
+ def asn(hostname)
677
+ ip = hostname
678
+ if ip.kind_of?(String) && ip !~ /^[0-9.]*$/
679
+ # Lookup IP address, we were given a name
680
+ ip = IPSocket.getaddress(hostname)
681
+ ip = '0.0.0.0' if ip == '::1'
682
+ end
683
+
684
+ # Convert numeric IP address to an integer
685
+ ipnum = iptonum(ip)
686
+ if (@databaseType != GEOIP_ASNUM_EDITION)
687
+ throw "Invalid GeoIP database type, can't look up ASN by IP"
688
+ end
689
+ pos = seek_record(ipnum);
690
+ record = ""
691
+ @mutex.synchronize {
692
+ @file.seek(pos + (2*@record_length-1) * @databaseSegments[0])
693
+ record = @file.read(MAX_ASN_RECORD_LENGTH)
694
+ }
695
+ record = record.sub(/\000.*/n, '')
696
+
697
+ if record =~ /^(AS\d+)\s(.*)$/
698
+ # AS####, Description
699
+ return [$1, $2]
700
+ end
701
+ end
702
+
703
+ # Search a ISP GeoIP database for the specified host, returning the organization
704
+ #
705
+ # +hostname+ is a String holding the host's DNS name or numeric IP address.
706
+ # Return the organization associated with it
707
+ #
708
+ alias_method(:organization, :isp) # Untested, according to Maxmind docs this should work
709
+
710
+ # Iterate through a GeoIP city database
711
+ def each
712
+ if (@databaseType != GEOIP_CITY_EDITION_REV0 &&
713
+ @databaseType != GEOIP_CITY_EDITION_REV1)
714
+ throw "Invalid GeoIP database type, can't iterate thru non-City database"
715
+ end
716
+
717
+ @iter_pos = @databaseSegments[0] + 1
718
+ num = 0
719
+ until((rec = read_city(@iter_pos)).nil?)
720
+ yield(rec)
721
+ print "#{num}: #{@iter_pos}\n" if((num += 1) % 1000 == 0)
722
+ end
723
+ @iter_pos = nil
724
+ self
725
+ end
726
+
727
+ private
728
+
729
+ def iptonum(ip) # Convert numeric IP address to integer
730
+ if ip.kind_of?(String) &&
731
+ ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/
732
+ ip = be_to_ui(Regexp.last_match().to_a.slice(1..4))
733
+ end
734
+ ip
735
+ end
736
+
737
+ def seek_record(ipnum)
738
+ # Binary search in the file.
739
+ # Records are pairs of little-endian integers, each of @record_length.
740
+ offset = 0
741
+ mask = 0x80000000
742
+ 31.downto(0) { |depth|
743
+ buf = @mutex.synchronize {
744
+ @file.seek(@record_length * 2 * offset);
745
+ @file.read(@record_length * 2);
746
+ }
747
+ buf.slice!(0...@record_length) if ((ipnum & mask) != 0)
748
+ offset = le_to_ui(buf[0...@record_length].unpack("C*"))
749
+ return offset if (offset >= @databaseSegments[0])
750
+ mask >>= 1
751
+ }
752
+ end
753
+
754
+ # Convert a big-endian array of numeric bytes to unsigned int
755
+ def be_to_ui(s)
756
+ s.inject(0) { |m, o|
757
+ (m << 8) + o.to_i
758
+ }
759
+ end
760
+
761
+ # Same for little-endian
762
+ def le_to_ui(s)
763
+ be_to_ui(s.reverse)
764
+ end
765
+ end
766
+
767
+ if $0 == __FILE__
768
+ data = '/usr/share/GeoIP/GeoIP.dat'
769
+ data = ARGV.shift if ARGV[0] =~ /\.dat\Z/
770
+ g = GeoIP.new data
771
+
772
+ req = ([GeoIP::GEOIP_CITY_EDITION_REV1, GeoIP::GEOIP_CITY_EDITION_REV0].include?(g.databaseType)) ? :city : :country
773
+ ARGV.each { |a|
774
+ p g.send(req, a)
775
+ }
776
+ end
777
+
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestGeoip < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/geoip'
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ghazel-geoip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.4.1
5
+ platform: ruby
6
+ authors:
7
+ - Clifford Heath
8
+ - Roland Moriz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-09-03 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: hoe
18
+ type: :development
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 2.3.2
25
+ version:
26
+ description: |-
27
+ GeoIP searches a GeoIP database for a given host or IP address, and
28
+ returns information about the country where the IP address is allocated,
29
+ and the city, ISP and other information, if you have that database version.
30
+ email:
31
+ - clifford.heath@gmail.com
32
+ - rmoriz@gmail.com
33
+ executables: []
34
+
35
+ extensions: []
36
+
37
+ extra_rdoc_files:
38
+ - History.txt
39
+ - Manifest.txt
40
+ files:
41
+ - History.txt
42
+ - Manifest.txt
43
+ - README.rdoc
44
+ - Rakefile
45
+ - lib/geoip.rb
46
+ - test/test_geoip.rb
47
+ - test/test_helper.rb
48
+ has_rdoc: true
49
+ homepage: http://github.com/cjheath/geoip
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --main
55
+ - README.rdoc
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project: geoip
73
+ rubygems_version: 1.3.5
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: GeoIP searches a GeoIP database for a given host or IP address, and returns information about the country where the IP address is allocated, and the city, ISP and other information, if you have that database version.
77
+ test_files:
78
+ - test/test_geoip.rb
79
+ - test/test_helper.rb