geoip 1.5.0 → 1.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/geoip.gemspec +3 -3
  3. data/lib/geoip.rb +27 -35
  4. data/test/test_file.rb +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8e46df24a662de22a8500c265c5eaaf2bfb0cb29
4
- data.tar.gz: d90310294059804b56d80ac41554fa68826393a2
3
+ metadata.gz: 1308c6df3d48989479c7b279f0be0672c3519000
4
+ data.tar.gz: 076311dc57ceadec5ef1969f95200f330c9a230c
5
5
  SHA512:
6
- metadata.gz: 5ad6a2221b51acf61c658412786484bd5e65ce2b56a4969fdb8954870be6a292360364867584844585c31fb8a04a597c7c30301d9a19d6f0ca125de9f65c00f3
7
- data.tar.gz: dba6732195af06951a47b1d36eaadad3d58f5a98fbbd35bae54858fe05a2966ecb1a009c63706873b287b8983d9d5ca38bedce586c0b72ebaeae994c76ea1554
6
+ metadata.gz: 2f36bf2d10008d34ded5a186932337deacd52de0d6ac0d3c9846ad3e5ba60267a5ef0447eed2bb4decdb3d4de10176cc4aa077fbcb7758afe94dfe8d7f0023b2
7
+ data.tar.gz: e711b0dca011d1e088498f4b27a74080d42154b4e5568335d5f309b0e42045c00a6caac487e0296ae09c638572dc93fb59849aff0371ad7ccc72f14b1c524ddd
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: geoip 1.5.0 ruby lib
5
+ # stub: geoip 1.6.1 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "geoip"
9
- s.version = "1.5.0"
9
+ s.version = "1.6.1"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Clifford Heath", "Roland Moriz"]
14
- s.date = "2015-03-10"
14
+ s.date = "2015-06-24"
15
15
  s.description = "GeoIP searches a GeoIP database for a given host or IP address, and\nreturns information about the country where the IP address is allocated,\nand the city, ISP and other information, if you have that database version."
16
16
  s.email = ["clifford.heath@gmail.com", "rmoriz@gmail.com"]
17
17
  s.executables = ["geoip"]
@@ -58,7 +58,7 @@ require 'yaml'
58
58
  class GeoIP
59
59
 
60
60
  # The GeoIP GEM version number
61
- VERSION = "1.5.0"
61
+ VERSION = "1.6.1"
62
62
 
63
63
  # The +data/+ directory for geoip
64
64
  DATA_DIR = File.expand_path(File.join(File.dirname(__FILE__),'..','data','geoip'))
@@ -297,8 +297,8 @@ class GeoIP
297
297
  # Convert numeric IP address to an integer
298
298
  ip = lookup_ip(hostname)
299
299
  ipnum = iptonum(ip)
300
- offset = (seek_record(ipnum) - @database_segments[0])
301
- read_netspeed(offset)
300
+ pos = seek_record(ipnum)
301
+ read_netspeed(pos-@database_segments[0])
302
302
  end
303
303
 
304
304
  # Search the GeoIP database for the specified host, returning region info.
@@ -332,7 +332,9 @@ class GeoIP
332
332
  throw "Invalid GeoIP database type, can't look up Region by IP"
333
333
  end
334
334
 
335
- unless pos == @database_segments[0]
335
+ if pos == @database_segments[0]
336
+ nil
337
+ else
336
338
  read_region(pos, hostname, ip)
337
339
  end
338
340
  end
@@ -373,18 +375,7 @@ class GeoIP
373
375
  throw "Invalid GeoIP database type, can't look up City by IP"
374
376
  end
375
377
 
376
- # This next statement was added to MaxMind's C version after it was
377
- # rewritten in Ruby. It prevents unassigned IP addresses from returning
378
- # bogus data. There was concern over whether the changes to an
379
- # application's behaviour were always correct, but this has been tested
380
- # using an exhaustive search of the top 16 bits of the IP address space.
381
- # The records where the change takes effect contained *no* valid data.
382
- # If you're concerned, email me, and I'll send you the test program so
383
- # you can test whatever IP range you think is causing problems,
384
- # as I don't care to undertake an exhaustive search of the 32-bit space.
385
- unless pos == @database_segments[0]
386
- read_city(pos-@database_segments[0], hostname, ip)
387
- end
378
+ read_city(pos-@database_segments[0], hostname, ip)
388
379
  end
389
380
 
390
381
  # Search a ISP GeoIP database for the specified host, returning the ISP
@@ -496,7 +487,7 @@ class GeoIP
496
487
  off1 = le_to_ui(record1.unpack('C*'))
497
488
  val = off1 - @database_segments[0]
498
489
  if val >= 0
499
- yield(ipnum, read_record(ipnum.to_s, ipnum, val)) if val > 0
490
+ yield(ipnum, val > 0 ? read_record(ipnum.to_s, ipnum, val) : nil)
500
491
  elsif mask != 0
501
492
  each_by_ip(off1, ipnum, mask >> 1, &callback)
502
493
  end
@@ -505,7 +496,7 @@ class GeoIP
505
496
  off2 = le_to_ui(record2.unpack('C*'))
506
497
  val = off2 - @database_segments[0]
507
498
  if val >= 0
508
- yield(ipnum|mask, read_record(ipnum.to_s, ipnum, val)) if val > 0
499
+ yield(ipnum|mask, val > 0 ? read_record(ipnum.to_s, ipnum, val) : nil)
509
500
  elsif mask != 0
510
501
  each_by_ip(off2, ipnum|mask, mask >> 1, &callback)
511
502
  end
@@ -519,7 +510,7 @@ class GeoIP
519
510
  read_city(offset, hostname, ip)
520
511
 
521
512
  when Edition::REGION_REV0, Edition::REGION_REV1
522
- read_region(offset, hostname, ip)
513
+ read_region(offset+@database_segments[0], hostname, ip)
523
514
 
524
515
  when Edition::NETSPEED, Edition::NETSPEED_REV1
525
516
  read_netspeed(offset)
@@ -604,6 +595,7 @@ class GeoIP
604
595
  end
605
596
 
606
597
  case @database_type
598
+ when Edition::COUNTRY
607
599
  when Edition::NETSPEED_REV1
608
600
  when Edition::ASNUM
609
601
  when Edition::CITY_REV0
@@ -623,7 +615,6 @@ class GeoIP
623
615
  Edition::CITY_REV0_V6,
624
616
  Edition::CITY_REV1_V6,
625
617
  Edition::NETSPEED_REV1_V6,
626
- Edition::COUNTRY,
627
618
  Edition::COUNTRY_V6,
628
619
  Edition::PROXY
629
620
  @ip_bits = 128
@@ -712,8 +703,9 @@ class GeoIP
712
703
  )
713
704
  end
714
705
 
715
- def read_asn off
716
- record = atomic_read(MAX_ASN_RECORD_LENGTH, off+index_size)
706
+ def read_asn offset
707
+ return nil if offset == 0
708
+ record = atomic_read(MAX_ASN_RECORD_LENGTH, index_size+offset)
717
709
  record.slice!(record.index("\0")..-1)
718
710
 
719
711
  # AS####, Description
@@ -727,16 +719,15 @@ class GeoIP
727
719
 
728
720
  def read_netspeed(offset)
729
721
  return offset if @database_type == Edition::NETSPEED # Numeric value
722
+ return nil if offset == 0
730
723
 
731
724
  record = atomic_read(20, index_size+offset)
732
725
  record.slice!(record.index("\0")..-1)
733
726
  record
734
727
  end
735
728
 
736
- def read_isp pos
737
- off = pos + index_size
738
-
739
- record = atomic_read(MAX_ORG_RECORD_LENGTH, off)
729
+ def read_isp offset
730
+ record = atomic_read(MAX_ORG_RECORD_LENGTH, index_size+offset)
740
731
  record = record.sub(/\000.*/n, '')
741
732
  record.start_with?('*') ? nil : ISP.new(record)
742
733
  end
@@ -767,8 +758,9 @@ class GeoIP
767
758
  # * The dma_code and area_code, if available (REV1 City database)
768
759
  # * The timezone name, if known
769
760
  #
770
- def read_city(pos, hostname = '', ip = '') #:nodoc:
771
- record = atomic_read(FULL_RECORD_LENGTH, pos+index_size)
761
+ def read_city(offset, hostname = '', ip = '') #:nodoc:
762
+ return nil if offset == 0
763
+ record = atomic_read(FULL_RECORD_LENGTH, offset+index_size)
772
764
  return unless (record && record.size == FULL_RECORD_LENGTH)
773
765
 
774
766
  # The country code is the first byte:
@@ -910,27 +902,27 @@ class GeoIP
910
902
  be_to_ui(s.reverse)
911
903
  end
912
904
 
913
- # reads +length+ bytes from +offset+ as atomically as possible
905
+ # reads +length+ bytes from +pos+ as atomically as possible
914
906
  # if IO.pread is available, it'll use that (making it both multithread
915
907
  # and multiprocess-safe). Otherwise we'll use a mutex to synchronize
916
908
  # access (only providing protection against multiple threads, but not
917
909
  # file descriptors shared across multiple processes).
918
910
  # If the contents of the database have been preloaded it'll work with
919
911
  # the StringIO object directly.
920
- def atomic_read(length, offset) #:nodoc:
912
+ def atomic_read(length, pos) #:nodoc:
921
913
  if @mutex
922
- @mutex.synchronize { atomic_read_unguarded(length, offset) }
914
+ @mutex.synchronize { atomic_read_unguarded(length, pos) }
923
915
  else
924
- atomic_read_unguarded(length, offset)
916
+ atomic_read_unguarded(length, pos)
925
917
  end
926
918
  end
927
919
 
928
- def atomic_read_unguarded(length, offset)
920
+ def atomic_read_unguarded(length, pos)
929
921
  if @use_pread
930
- IO.pread(@file.fileno, length, offset)
922
+ IO.pread(@file.fileno, length, pos)
931
923
  else
932
924
  io = @contents || @file
933
- io.seek(offset)
925
+ io.seek(pos)
934
926
  io.read(length)
935
927
  end
936
928
  end
@@ -12,6 +12,6 @@ ARGV.each do |file|
12
12
  else
13
13
  '%d.%d.%d.%d' % [ip].pack('N').unpack('C4')
14
14
  end
15
- puts "#{ip_str}\t#{val.to_hash.to_a.sort.map{|n,v| "#{n}=#{v.inspect}"}*', '}"
15
+ puts "#{ip_str}\t#{val ? val.to_hash.to_a.sort.map{|n,v| "#{n}=#{v.inspect}"}*', ' : 'Unassigned'}"
16
16
  end
17
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geoip
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Clifford Heath
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-03-10 00:00:00.000000000 Z
12
+ date: 2015-06-24 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: |-
15
15
  GeoIP searches a GeoIP database for a given host or IP address, and