Class: GeoIP
- Inherits:
-
Object
- Object
- GeoIP
- Defined in:
- lib/geoip.rb
Defined Under Namespace
Modules: Edition Classes: ASN, City, Country, ISP, Region
Constant Summary collapse
- VERSION =
The GeoIP GEM version number
"1.6.4"
- DATA_DIR =
The
data/
directory for geoip File.(File.join(File.dirname(__FILE__),'..','data','geoip'))
- CountryCode =
Ordered list of the ISO3166 2-character country codes, ordered by GeoIP ID
YAML.load_file(File.join(DATA_DIR,'country_code.yml'))
- CountryCode3 =
Ordered list of the ISO3166 3-character country codes, ordered by GeoIP ID
YAML.load_file(File.join(DATA_DIR,'country_code3.yml'))
- CountryName =
Ordered list of the English names of the countries, ordered by GeoIP ID
YAML.load_file(File.join(DATA_DIR,'country_name.yml'))
- CountryContinent =
Ordered list of the ISO3166 2-character continent code of the countries, ordered by GeoIP ID
YAML.load_file(File.join(DATA_DIR,'country_continent.yml'))
- RegionName =
Load a hash of region names by region code
YAML.load_file(File.join(DATA_DIR,'region.yml'))
- TimeZone =
Hash of the timezone codes mapped to timezone name, per zoneinfo
YAML.load_file(File.join(DATA_DIR,'time_zone.yml'))
- GEOIP_COUNTRY_EDITION =
1
- GEOIP_CITY_EDITION_REV1 =
2
- GEOIP_REGION_EDITION_REV1 =
3
- GEOIP_ISP_EDITION =
4
- GEOIP_ORG_EDITION =
5
- GEOIP_CITY_EDITION_REV0 =
6
- GEOIP_REGION_EDITION_REV0 =
7
- GEOIP_PROXY_EDITION =
8
- GEOIP_ASNUM_EDITION =
9
- GEOIP_NETSPEED_EDITION =
10
- GEOIP_COUNTRY_EDITION_V6 =
12
- GEOIP_CITY_EDITION_REV1_V6 =
30
- GEOIP_NETSPEED_EDITION_REV1 =
32
- GEOIP_UNKNOWN_SPEED =
Numeric codes for NETSPEED (NETSPEED_REV1* is string-based):
0
- GEOIP_DIALUP_SPEED =
1
- GEOIP_CABLEDSL_SPEED =
2
- GEOIP_CORPORATE_SPEED =
3
- COUNTRY_BEGIN =
:nodoc:
16776960
- STATE_BEGIN_REV0 =
:nodoc:
16700000
- STATE_BEGIN_REV1 =
:nodoc:
16000000
- STRUCTURE_INFO_MAX_SIZE =
:nodoc:
20
- DATABASE_INFO_MAX_SIZE =
:nodoc:
100
- MAX_ORG_RECORD_LENGTH =
:nodoc:
300
- MAX_ASN_RECORD_LENGTH =
:nodoc: unverified
300
- US_OFFSET =
:nodoc:
1
- CANADA_OFFSET =
:nodoc:
677
- WORLD_OFFSET =
:nodoc:
1353
- FIPS_RANGE =
:nodoc:
360
- FULL_RECORD_LENGTH =
:nodoc:
50
- STANDARD_RECORD_LENGTH =
:nodoc:
3
- SEGMENT_RECORD_LENGTH =
:nodoc:
3
Instance Attribute Summary collapse
-
#database_type ⇒ Object
(also: #databaseType)
readonly
The Edition number that identifies which kind of database you’ve opened.
-
#local_ip_alias ⇒ Object
An IP that is used instead of local IPs.
Instance Method Summary collapse
-
#asn(hostname) ⇒ Object
Search a ASN GeoIP database for the specified host, returning the AS number and description.
-
#city(hostname) ⇒ Object
Search the GeoIP database for the specified host, returning city info.
-
#country(hostname) ⇒ Object
Search the GeoIP database for the specified host, returning country info.
-
#each ⇒ Object
Iterate through a GeoIP city database by.
-
#each_by_ip(offset = 0, ipnum = 0, mask = nil, &callback) ⇒ Object
Call like this, for example: GeoIP.new(‘GeoIPNetSpeedCell.dat’).each{|*a| puts(“0x%08Xt%d” % a)} or: GeoIP.new(‘GeoIPv6.dat’).each{|*a| puts(“0x%032Xt%d” % a)}.
-
#initialize(filename, options = {}) ⇒ GeoIP
constructor
Open the GeoIP database and determine the file format version.
-
#isp(hostname) ⇒ Object
(also: #organization)
Search a ISP GeoIP database for the specified host, returning the ISP Not all GeoIP databases contain ISP information.
-
#netspeed(hostname) ⇒ Object
Search a GeoIP Connection Type (Netspeed) database for the specified host, returning the speed code.
-
#region(hostname) ⇒ Object
Search the GeoIP database for the specified host, returning region info.
Constructor Details
#initialize(filename, options = {}) ⇒ GeoIP
Open the GeoIP database and determine the file format version.
filename
is a String holding the path to the GeoIP.dat file options
is a Hash allowing you to specify the caching options
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/geoip.rb', line 225 def initialize(filename, = {}) if [:preload] || !IO.respond_to?(:pread) @mutex = Mutex.new end @use_pread = IO.respond_to?(:pread) && ![:preload] @contents = nil @iter_pos = nil @options = @database_type = Edition::COUNTRY @record_length = STANDARD_RECORD_LENGTH @file = File.open(filename, 'rb') detect_database_type! preload_data if [:preload] end |
Instance Attribute Details
#database_type ⇒ Object (readonly) Also known as: databaseType
The Edition number that identifies which kind of database you’ve opened
213 214 215 |
# File 'lib/geoip.rb', line 213 def database_type @database_type end |
#local_ip_alias ⇒ Object
An IP that is used instead of local IPs
216 217 218 |
# File 'lib/geoip.rb', line 216 def local_ip_alias @local_ip_alias end |
Instance Method Details
#asn(hostname) ⇒ Object
Search a ASN GeoIP database for the specified host, returning the AS number and description.
Many other types of GeoIP database (e.g. userType) mis-identify as ASN type, and this can read those too.
hostname
is a String holding the host’s DNS name or numeric IP address.
Returns the AS number and description.
Source: geolite.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/geoip.rb', line 431 def asn(hostname) ip = lookup_ip(hostname) if (@database_type == Edition::ASNUM) # Convert numeric IP address to an integer ipnum = iptonum(ip) pos = seek_record(ipnum) elsif (@database_type == Edition::ASNUM_V6) ipaddr = IPAddr.new ip pos = seek_record(ipaddr.to_i) else throw "Invalid GeoIP database type #{@database_type}, can't look up ASN by IP" end read_asn(pos-@database_segments[0]) end |
#city(hostname) ⇒ Object
Search the GeoIP database for the specified host, returning city info.
hostname
is a String holding the host’s DNS name or numeric IP address.
Returns a City object with the fourteen elements:
-
The host or IP address string as requested
-
The IP address string after looking up the host
-
The two-character country code (ISO 3166-1 alpha-2)
-
The three-character country code (ISO 3166-2 alpha-3)
-
The ISO 3166 English-language name of the country
-
The two-character continent code
-
The region name (state or territory)
-
The city name
-
The postal code (zipcode)
-
The latitude
-
The longitude
-
The USA dma_code if known (only REV1 City database)
-
The USA area_code if known (only REV1 City database)
-
The timezone name, if known
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/geoip.rb', line 364 def city(hostname) ip = lookup_ip(hostname) if (@database_type == Edition::CITY_REV0 || @database_type == Edition::CITY_REV1) # Convert numeric IP address to an integer ipnum = iptonum(ip) pos = seek_record(ipnum) elsif (@database_type == Edition::CITY_REV1_V6) ipaddr = IPAddr.new ip pos = seek_record(ipaddr.to_i) else throw "Invalid GeoIP database type, can't look up City by IP" end read_city(pos-@database_segments[0], hostname, ip) end |
#country(hostname) ⇒ Object
Search the GeoIP database for the specified host, returning country info.
hostname
is a String holding the host’s DNS name or numeric IP address.
If the database is a City database (normal), return the result that city
would return.
Otherwise, return a Country object with the seven elements:
-
The host or IP address string as requested
-
The IP address string after looking up the host
-
The GeoIP country-ID as an integer (N.B. this is excluded from the city results!)
-
The two-character country code (ISO 3166-1 alpha-2)
-
The three-character country code (ISO 3166-2 alpha-3)
-
The ISO 3166 English-language name of the country
-
The two-character continent code
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/geoip.rb', line 262 def country(hostname) case @database_type when Edition::CITY_REV0, Edition::CITY_REV1, Edition::CITY_REV1_V6 city(hostname) when Edition::REGION_REV0, Edition::REGION_REV1 region(hostname) when Edition::NETSPEED, Edition::NETSPEED_REV1 netspeed(hostname) when Edition::COUNTRY, Edition::PROXY, Edition::COUNTRY_V6 ip = lookup_ip(hostname) if @ip_bits > 32 ipaddr = IPAddr.new ip code = (seek_record(ipaddr.to_i) - COUNTRY_BEGIN) else # Convert numeric IP address to an integer ipnum = iptonum(ip) code = (seek_record(ipnum) - @database_segments[0]) end read_country(code, hostname, ip) else throw "Invalid GeoIP database type #{@database_type}, can't look up Country by IP" end end |
#each ⇒ Object
Iterate through a GeoIP city database by
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
# File 'lib/geoip.rb', line 459 def each return enum_for unless block_given? if (@database_type != Edition::CITY_REV0 && @database_type != Edition::CITY_REV1) throw "Invalid GeoIP database type, can't iterate thru non-City database" end @iter_pos = @database_segments[0] + 1 num = 0 until ((rec = read_city(@iter_pos)).nil?) yield rec print "#{num}: #{@iter_pos}\n" if((num += 1) % 1000 == 0) end @iter_pos = nil return self end |
#each_by_ip(offset = 0, ipnum = 0, mask = nil, &callback) ⇒ Object
Call like this, for example: GeoIP.new(‘GeoIPNetSpeedCell.dat’).each{|*a| puts(“0x%08Xt%d” % a)} or: GeoIP.new(‘GeoIPv6.dat’).each{|*a| puts(“0x%032Xt%d” % a)}
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
# File 'lib/geoip.rb', line 483 def each_by_ip offset = 0, ipnum = 0, mask = nil, &callback mask ||= 1 << (@ip_bits-1) # Read the two pointers and split them: record2 = atomic_read(@record_length*2, @record_length*2*offset) record1 = record2.slice!(0, @record_length) # Traverse the left tree off1 = le_to_ui(record1.unpack('C*')) val = off1 - @database_segments[0] if val >= 0 yield(ipnum, val > 0 ? read_record(ipnum.to_s, ipnum, val) : nil) elsif mask != 0 each_by_ip(off1, ipnum, mask >> 1, &callback) end # Traverse the right tree off2 = le_to_ui(record2.unpack('C*')) val = off2 - @database_segments[0] if val >= 0 yield(ipnum|mask, val > 0 ? read_record(ipnum.to_s, ipnum, val) : nil) elsif mask != 0 each_by_ip(off2, ipnum|mask, mask >> 1, &callback) end end |
#isp(hostname) ⇒ Object Also known as: organization
Search a ISP GeoIP database for the specified host, returning the ISP Not all GeoIP databases contain ISP information. Check maxmind.com
hostname
is a String holding the host’s DNS name or numeric IP address.
Returns the ISP name.
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'lib/geoip.rb', line 391 def isp(hostname) ip = lookup_ip(hostname) # Convert numeric IP address to an integer ipnum = iptonum(ip) case @database_type when Edition::ORG, Edition::ISP, Edition::DOMAIN, Edition::ASNUM, Edition::ACCURACYRADIUS, Edition::NETSPEED, Edition::USERTYPE, Edition::REGISTRAR, Edition::LOCATIONA, Edition::CITYCONF, Edition::COUNTRYCONF, Edition::REGIONCONF, Edition::POSTALCONF pos = seek_record(ipnum) read_isp(pos-@database_segments[0]) else throw "Invalid GeoIP database type, can't look up Organization/ISP by IP" end end |
#netspeed(hostname) ⇒ Object
Search a GeoIP Connection Type (Netspeed) database for the specified host, returning the speed code.
hostname
is a String holding the host’s DNS name or numeric IP address.
293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/geoip.rb', line 293 def netspeed(hostname) unless (@database_type == Edition::NETSPEED || @database_type == Edition::NETSPEED_REV1) throw "Invalid GeoIP database type #{@database_type}, can't look up Netspeed by IP" end # Convert numeric IP address to an integer ip = lookup_ip(hostname) ipnum = iptonum(ip) pos = seek_record(ipnum) read_netspeed(pos-@database_segments[0]) end |
#region(hostname) ⇒ Object
Search the GeoIP database for the specified host, returning region info.
hostname
is a String holding the hosts’s DNS name or numeric IP address.
Returns a Region object with the nine elements:
-
The host or IP address string as requested
-
The IP address string after looking up the host
-
The two-character country code (ISO 3166-1 alpha-2)
-
The three-character country code (ISO 3166-2 alpha-3)
-
The ISO 3166 English-language name of the country
-
The two-character continent code
-
The region name (state or territory)
-
The timezone name, if known
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/geoip.rb', line 320 def region(hostname) if (@database_type == Edition::CITY_REV0 || @database_type == Edition::CITY_REV1 || @database_type == Edition::CITY_REV1_V6) return city(hostname) end if (@database_type == Edition::REGION_REV0 || @database_type == Edition::REGION_REV1) ip = lookup_ip(hostname) ipnum = iptonum(ip) pos = seek_record(ipnum) else throw "Invalid GeoIP database type, can't look up Region by IP" end if pos == @database_segments[0] nil else read_region(pos, hostname, ip) end end |