ip2location_ruby 7.0.0 → 8.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.document +5 -5
- data/.rspec +1 -1
- data/Gemfile +16 -16
- data/Gemfile.lock +74 -74
- data/LICENSE.txt +20 -20
- data/README.md +64 -0
- data/Rakefile +49 -49
- data/VERSION +1 -1
- data/example.rb +33 -0
- data/ip2location_ruby.gemspec +87 -79
- data/lib/ip2location_ruby.rb +539 -74
- data/lib/ip2location_ruby/database_config.rb +34 -34
- data/lib/ip2location_ruby/i2l_float_data.rb +6 -6
- data/lib/ip2location_ruby/i2l_ip_data.rb +9 -9
- data/lib/ip2location_ruby/i2l_string_data.rb +15 -15
- data/lib/ip2location_ruby/ip2location_config.rb +16 -14
- data/lib/ip2location_ruby/ip2location_record.rb +21 -21
- data/rb/data/IP2LOCATION-LITE-DB1.IPV6.BIN +0 -0
- data/spec/assets/DB24.DEMO4.BIN +0 -0
- data/spec/assets/DB24.DEMO6.BIN +0 -0
- data/spec/assets/IP-COUNTRY-SAMPLE.BIN +0 -0
- data/spec/ip2location_ruby_spec.rb +53 -53
- data/spec/spec_helper.rb +12 -12
- metadata +19 -10
- data/README.rdoc +0 -24
data/ip2location_ruby.gemspec
CHANGED
|
@@ -1,79 +1,87 @@
|
|
|
1
|
-
# Generated by jeweler
|
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
|
4
|
-
# -*- encoding: utf-8 -*-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
s.
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
s.
|
|
12
|
-
s.
|
|
13
|
-
s.
|
|
14
|
-
s.
|
|
15
|
-
s.
|
|
16
|
-
s.
|
|
17
|
-
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
".
|
|
23
|
-
"
|
|
24
|
-
"Gemfile",
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"ip2location_ruby.
|
|
31
|
-
"lib/ip2location_ruby.rb",
|
|
32
|
-
"lib/ip2location_ruby/
|
|
33
|
-
"lib/ip2location_ruby/
|
|
34
|
-
"lib/ip2location_ruby/
|
|
35
|
-
"lib/ip2location_ruby/
|
|
36
|
-
"lib/ip2location_ruby/
|
|
37
|
-
"
|
|
38
|
-
"spec/assets/DB24.
|
|
39
|
-
"spec/assets/
|
|
40
|
-
"spec/
|
|
41
|
-
"spec/
|
|
42
|
-
"
|
|
43
|
-
]
|
|
44
|
-
s.homepage = "
|
|
45
|
-
s.licenses = ["MIT"]
|
|
46
|
-
s.
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
s.
|
|
62
|
-
s.
|
|
63
|
-
s.
|
|
64
|
-
s.
|
|
65
|
-
s.
|
|
66
|
-
s.
|
|
67
|
-
s.
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
1
|
+
# Generated by jeweler
|
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
|
4
|
+
# -*- encoding: utf-8 -*-
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |s|
|
|
7
|
+
s.name = "ip2location_ruby"
|
|
8
|
+
s.version = "8.1.1"
|
|
9
|
+
|
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
|
+
s.require_paths = ["lib"]
|
|
12
|
+
s.authors = ["ip2location"]
|
|
13
|
+
s.email = ["support@ip2location.com"]
|
|
14
|
+
s.description = "The official IP2Location Ruby library to geolocate an IP address. You can lookup for country, region, city, latitude, longitude, zip code, time zone, ISP, domain, area code, usage type, mobile data, weather data and elevation from an IP address. Supported both IPv4 and IPv6 lookup."
|
|
15
|
+
s.email = "support@ip2location.com"
|
|
16
|
+
s.extra_rdoc_files = [
|
|
17
|
+
"LICENSE.txt",
|
|
18
|
+
"README.md"
|
|
19
|
+
]
|
|
20
|
+
s.files = [
|
|
21
|
+
".document",
|
|
22
|
+
".rspec",
|
|
23
|
+
"Gemfile",
|
|
24
|
+
"Gemfile.lock",
|
|
25
|
+
"LICENSE.txt",
|
|
26
|
+
"Rakefile",
|
|
27
|
+
"VERSION",
|
|
28
|
+
"ip2location_ruby.gemspec",
|
|
29
|
+
"example.rb",
|
|
30
|
+
"lib/ip2location_ruby.rb",
|
|
31
|
+
"lib/ip2location_ruby/database_config.rb",
|
|
32
|
+
"lib/ip2location_ruby/i2l_float_data.rb",
|
|
33
|
+
"lib/ip2location_ruby/i2l_ip_data.rb",
|
|
34
|
+
"lib/ip2location_ruby/i2l_string_data.rb",
|
|
35
|
+
"lib/ip2location_ruby/ip2location_config.rb",
|
|
36
|
+
"lib/ip2location_ruby/ip2location_record.rb",
|
|
37
|
+
"spec/assets/DB24.DEMO4.BIN",
|
|
38
|
+
"spec/assets/DB24.DEMO6.BIN",
|
|
39
|
+
"spec/assets/IP-COUNTRY-SAMPLE.BIN",
|
|
40
|
+
"spec/ip2location_ruby_spec.rb",
|
|
41
|
+
"spec/spec_helper.rb",
|
|
42
|
+
"rb/data/IP2LOCATION-LITE-DB1.IPV6.BIN"
|
|
43
|
+
]
|
|
44
|
+
s.homepage = "https://github.com/ip2location/ip2location-ruby"
|
|
45
|
+
s.licenses = ["MIT"]
|
|
46
|
+
s.summary = "the ip2location ruby library"
|
|
47
|
+
|
|
48
|
+
if s.respond_to?(:metadata=)
|
|
49
|
+
s.metadata = {
|
|
50
|
+
"bug_tracker_uri" => "https://github.com/ip2location/ip2location-ruby/issues",
|
|
51
|
+
"documentation_uri" => "https://www.rubydoc.info/gems/ip2location_ruby",
|
|
52
|
+
"homepage_uri" => "https://www.ip2location.com",
|
|
53
|
+
"source_code_uri" => "https://github.com/ip2location/ip2location-ruby",
|
|
54
|
+
}
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
if s.respond_to? :specification_version then
|
|
58
|
+
s.specification_version = 4
|
|
59
|
+
|
|
60
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
|
61
|
+
s.add_runtime_dependency(%q<bindata>, [">= 0"])
|
|
62
|
+
s.add_development_dependency(%q<awesome_print>, [">= 0"])
|
|
63
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
|
|
64
|
+
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
|
65
|
+
s.add_development_dependency(%q<bundler>, [">= 1.2.0"])
|
|
66
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
|
|
67
|
+
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
|
68
|
+
else
|
|
69
|
+
s.add_dependency(%q<bindata>, [">= 0"])
|
|
70
|
+
s.add_dependency(%q<awesome_print>, [">= 0"])
|
|
71
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
|
72
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
|
73
|
+
s.add_dependency(%q<bundler>, [">= 1.2.0"])
|
|
74
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
|
75
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
|
76
|
+
end
|
|
77
|
+
else
|
|
78
|
+
s.add_dependency(%q<bindata>, [">= 0"])
|
|
79
|
+
s.add_dependency(%q<awesome_print>, [">= 0"])
|
|
80
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
|
81
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
|
82
|
+
s.add_dependency(%q<bundler>, [">= 1.2.0"])
|
|
83
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
|
84
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
data/lib/ip2location_ruby.rb
CHANGED
|
@@ -1,74 +1,539 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
require 'bindata'
|
|
3
|
-
require 'ipaddr'
|
|
4
|
-
require 'ip2location_ruby/ip2location_config'
|
|
5
|
-
require 'ip2location_ruby/database_config'
|
|
6
|
-
require 'ip2location_ruby/i2l_float_data'
|
|
7
|
-
require 'ip2location_ruby/i2l_string_data'
|
|
8
|
-
require 'ip2location_ruby/i2l_ip_data'
|
|
9
|
-
require 'ip2location_ruby/ip2location_record'
|
|
10
|
-
|
|
11
|
-
class Ip2location
|
|
12
|
-
attr_accessor :
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
self.
|
|
19
|
-
|
|
20
|
-
self.
|
|
21
|
-
self.
|
|
22
|
-
self.
|
|
23
|
-
self.
|
|
24
|
-
self
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
self.
|
|
30
|
-
self.
|
|
31
|
-
self
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require 'bindata'
|
|
3
|
+
require 'ipaddr'
|
|
4
|
+
require 'ip2location_ruby/ip2location_config'
|
|
5
|
+
require 'ip2location_ruby/database_config'
|
|
6
|
+
require 'ip2location_ruby/i2l_float_data'
|
|
7
|
+
require 'ip2location_ruby/i2l_string_data'
|
|
8
|
+
require 'ip2location_ruby/i2l_ip_data'
|
|
9
|
+
require 'ip2location_ruby/ip2location_record'
|
|
10
|
+
|
|
11
|
+
class Ip2location
|
|
12
|
+
attr_accessor :record_class4, :record_class6, :v4, :file, :db_index, :count, :base_addr, :ipno, :count, :record, :database, :columns, :ip_version, :ipv4databasecount, :ipv4databaseaddr, :ipv4indexbaseaddr, :ipv6databasecount, :ipv6databaseaddr, :ipv6indexbaseaddr
|
|
13
|
+
|
|
14
|
+
FIELD_NOT_SUPPORTED = 'NOT SUPPORTED'
|
|
15
|
+
INVALID_IP_ADDRESS = 'INVALID IP ADDRESS'
|
|
16
|
+
|
|
17
|
+
def open(url)
|
|
18
|
+
self.file = File.open(File.expand_path url, 'rb')
|
|
19
|
+
i2l = Ip2locationConfig.read(file)
|
|
20
|
+
self.db_index = i2l.databasetype
|
|
21
|
+
self.columns = i2l.databasecolumn + 0
|
|
22
|
+
self.database = DbConfig.setup_database(self.db_index)
|
|
23
|
+
self.ipv4databasecount = i2l.ipv4databasecount
|
|
24
|
+
self.ipv4databaseaddr = i2l.ipv4databaseaddr
|
|
25
|
+
self.ipv6databasecount = i2l.ipv6databasecount
|
|
26
|
+
self.ipv6databaseaddr = i2l.ipv6databaseaddr
|
|
27
|
+
self.ipv4indexbaseaddr = i2l.ipv4indexbaseaddr
|
|
28
|
+
self.ipv6indexbaseaddr = i2l.ipv6indexbaseaddr
|
|
29
|
+
self.record_class4 = (Ip2LocationRecord.init database, 4)
|
|
30
|
+
self.record_class6 = (Ip2LocationRecord.init database, 6)
|
|
31
|
+
self
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def close()
|
|
35
|
+
self.file.close
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def get_record(ip)
|
|
39
|
+
ipno = IPAddr.new(ip, Socket::AF_UNSPEC)
|
|
40
|
+
self.ip_version, ipnum = validateip(ipno)
|
|
41
|
+
self.v4 = ip_version == 4 ? true : false
|
|
42
|
+
self.count = v4 ? self.ipv4databasecount + 0 : self.ipv6databasecount + 0
|
|
43
|
+
self.base_addr = (v4 ? self.ipv4databaseaddr - 1 : self.ipv6databaseaddr - 1)
|
|
44
|
+
col_length = columns * 4
|
|
45
|
+
if ipv4indexbaseaddr > 0 || ipv6indexbaseaddr > 0
|
|
46
|
+
indexpos = 0
|
|
47
|
+
case ip_version
|
|
48
|
+
when 4
|
|
49
|
+
indexpos = ipv4indexbaseaddr + ((ipnum >> 16) << 3)
|
|
50
|
+
realipno = ipnum
|
|
51
|
+
# if ipnum reach MAX_IPV4_RANGE
|
|
52
|
+
if realipno == 4294967295
|
|
53
|
+
ipnum = realipno - 1
|
|
54
|
+
end
|
|
55
|
+
when 6
|
|
56
|
+
indexpos = ipv6indexbaseaddr + ((ipnum >> 112) << 3)
|
|
57
|
+
realipno = ipnum
|
|
58
|
+
# if ipnum reach MAX_IPV6_RANGE
|
|
59
|
+
if realipno == 340282366920938463463374607431768211455
|
|
60
|
+
ipnum = realipno - 1
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
low = read32(indexpos)
|
|
64
|
+
high = read32(indexpos + 4)
|
|
65
|
+
return self.record = bsearch(low, high, ipnum, self.base_addr, col_length)
|
|
66
|
+
else
|
|
67
|
+
return self.record = bsearch(0, self.count, ipnum, self.base_addr, col_length)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def get_all(ip)
|
|
72
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
73
|
+
if valid
|
|
74
|
+
rec = get_record(ip)
|
|
75
|
+
if !(rec.nil?)
|
|
76
|
+
country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
|
|
77
|
+
country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
|
|
78
|
+
region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
|
|
79
|
+
city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
|
|
80
|
+
latitude = (defined?(rec.latitude) && rec.latitude != '') ? rec.latitude : FIELD_NOT_SUPPORTED
|
|
81
|
+
longitude = (defined?(rec.longitude) && rec.longitude != '') ? rec.longitude : FIELD_NOT_SUPPORTED
|
|
82
|
+
isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
|
|
83
|
+
domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
|
|
84
|
+
netspeed = (defined?(rec.netspeed) && rec.netspeed != '') ? rec.netspeed : FIELD_NOT_SUPPORTED
|
|
85
|
+
areacode = (defined?(rec.areacode) && rec.areacode != '') ? rec.areacode : FIELD_NOT_SUPPORTED
|
|
86
|
+
iddcode = (defined?(rec.iddcode) && rec.iddcode != '') ? rec.iddcode : FIELD_NOT_SUPPORTED
|
|
87
|
+
timezone = (defined?(rec.timezone) && rec.timezone != '') ? rec.timezone : FIELD_NOT_SUPPORTED
|
|
88
|
+
zipcode = (defined?(rec.zipcode) && rec.zipcode != '') ? rec.zipcode : FIELD_NOT_SUPPORTED
|
|
89
|
+
weatherstationname = (defined?(rec.weatherstationname) && rec.weatherstationname != '') ? rec.weatherstationname : FIELD_NOT_SUPPORTED
|
|
90
|
+
weatherstationcode = (defined?(rec.weatherstationcode) && rec.weatherstationcode != '') ? rec.weatherstationcode : FIELD_NOT_SUPPORTED
|
|
91
|
+
mcc = (defined?(rec.mcc) && rec.mcc != '') ? rec.mcc : FIELD_NOT_SUPPORTED
|
|
92
|
+
mnc = (defined?(rec.mnc) && rec.mnc != '') ? rec.mnc : FIELD_NOT_SUPPORTED
|
|
93
|
+
mobilebrand = (defined?(rec.mobilebrand) && rec.mobilebrand != '') ? rec.mobilebrand : FIELD_NOT_SUPPORTED
|
|
94
|
+
elevation = (defined?(rec.elevation) && rec.elevation != '') ? rec.elevation : FIELD_NOT_SUPPORTED
|
|
95
|
+
usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
|
|
96
|
+
else
|
|
97
|
+
country_short = INVALID_IP_ADDRESS
|
|
98
|
+
country_long = INVALID_IP_ADDRESS
|
|
99
|
+
region = INVALID_IP_ADDRESS
|
|
100
|
+
city = INVALID_IP_ADDRESS
|
|
101
|
+
latitude = INVALID_IP_ADDRESS
|
|
102
|
+
longitude = INVALID_IP_ADDRESS
|
|
103
|
+
isp = INVALID_IP_ADDRESS
|
|
104
|
+
domain = INVALID_IP_ADDRESS
|
|
105
|
+
netspeed = INVALID_IP_ADDRESS
|
|
106
|
+
areacode = INVALID_IP_ADDRESS
|
|
107
|
+
iddcode = INVALID_IP_ADDRESS
|
|
108
|
+
timezone = INVALID_IP_ADDRESS
|
|
109
|
+
zipcode = INVALID_IP_ADDRESS
|
|
110
|
+
weatherstationname = INVALID_IP_ADDRESS
|
|
111
|
+
weatherstationcode = INVALID_IP_ADDRESS
|
|
112
|
+
mcc = INVALID_IP_ADDRESS
|
|
113
|
+
mnc = INVALID_IP_ADDRESS
|
|
114
|
+
mobilebrand = INVALID_IP_ADDRESS
|
|
115
|
+
elevation = INVALID_IP_ADDRESS
|
|
116
|
+
usagetype = INVALID_IP_ADDRESS
|
|
117
|
+
end
|
|
118
|
+
else
|
|
119
|
+
country_short = INVALID_IP_ADDRESS
|
|
120
|
+
country_long = INVALID_IP_ADDRESS
|
|
121
|
+
region = INVALID_IP_ADDRESS
|
|
122
|
+
city = INVALID_IP_ADDRESS
|
|
123
|
+
latitude = INVALID_IP_ADDRESS
|
|
124
|
+
longitude = INVALID_IP_ADDRESS
|
|
125
|
+
isp = INVALID_IP_ADDRESS
|
|
126
|
+
domain = INVALID_IP_ADDRESS
|
|
127
|
+
netspeed = INVALID_IP_ADDRESS
|
|
128
|
+
areacode = INVALID_IP_ADDRESS
|
|
129
|
+
iddcode = INVALID_IP_ADDRESS
|
|
130
|
+
timezone = INVALID_IP_ADDRESS
|
|
131
|
+
zipcode = INVALID_IP_ADDRESS
|
|
132
|
+
weatherstationname = INVALID_IP_ADDRESS
|
|
133
|
+
weatherstationcode = INVALID_IP_ADDRESS
|
|
134
|
+
mcc = INVALID_IP_ADDRESS
|
|
135
|
+
mnc = INVALID_IP_ADDRESS
|
|
136
|
+
mobilebrand = INVALID_IP_ADDRESS
|
|
137
|
+
elevation = INVALID_IP_ADDRESS
|
|
138
|
+
usagetype = INVALID_IP_ADDRESS
|
|
139
|
+
end
|
|
140
|
+
results = {}
|
|
141
|
+
results['country_short'] = country_short
|
|
142
|
+
results['country_long'] = country_long
|
|
143
|
+
results['region'] = region
|
|
144
|
+
results['city'] = city
|
|
145
|
+
results['latitude'] = latitude
|
|
146
|
+
results['longitude'] = longitude
|
|
147
|
+
results['isp'] = isp
|
|
148
|
+
results['domain'] = domain
|
|
149
|
+
results['netspeed'] = netspeed
|
|
150
|
+
results['areacode'] = areacode
|
|
151
|
+
results['iddcode'] = iddcode
|
|
152
|
+
results['timezone'] = timezone
|
|
153
|
+
results['zipcode'] = zipcode
|
|
154
|
+
results['weatherstationname'] = weatherstationname
|
|
155
|
+
results['weatherstationcode'] = weatherstationcode
|
|
156
|
+
results['mcc'] = mcc
|
|
157
|
+
results['mnc'] = mnc
|
|
158
|
+
results['mobilebrand'] = mobilebrand
|
|
159
|
+
results['elevation'] = elevation
|
|
160
|
+
results['usagetype'] = usagetype
|
|
161
|
+
return results
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def get_country_short(ip)
|
|
165
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
166
|
+
if valid
|
|
167
|
+
rec = get_record(ip)
|
|
168
|
+
if !(rec.nil?)
|
|
169
|
+
country_short = (defined?(rec.country_short) && rec.country_short != '') ? rec.country_short : FIELD_NOT_SUPPORTED
|
|
170
|
+
else
|
|
171
|
+
country_short = INVALID_IP_ADDRESS
|
|
172
|
+
end
|
|
173
|
+
else
|
|
174
|
+
country_short = INVALID_IP_ADDRESS
|
|
175
|
+
end
|
|
176
|
+
return country_short
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def get_country_long(ip)
|
|
180
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
181
|
+
if valid
|
|
182
|
+
rec = get_record(ip)
|
|
183
|
+
if !(rec.nil?)
|
|
184
|
+
country_long = (defined?(rec.country_long) && rec.country_long != '') ? rec.country_long : FIELD_NOT_SUPPORTED
|
|
185
|
+
else
|
|
186
|
+
country_long = INVALID_IP_ADDRESS
|
|
187
|
+
end
|
|
188
|
+
else
|
|
189
|
+
country_long = INVALID_IP_ADDRESS
|
|
190
|
+
end
|
|
191
|
+
return country_long
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def get_region(ip)
|
|
195
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
196
|
+
if valid
|
|
197
|
+
rec = get_record(ip)
|
|
198
|
+
if !(rec.nil?)
|
|
199
|
+
region = (defined?(rec.region) && rec.region != '') ? rec.region : FIELD_NOT_SUPPORTED
|
|
200
|
+
else
|
|
201
|
+
region = INVALID_IP_ADDRESS
|
|
202
|
+
end
|
|
203
|
+
else
|
|
204
|
+
region = INVALID_IP_ADDRESS
|
|
205
|
+
end
|
|
206
|
+
return region
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def get_city(ip)
|
|
210
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
211
|
+
if valid
|
|
212
|
+
rec = get_record(ip)
|
|
213
|
+
if !(rec.nil?)
|
|
214
|
+
city = (defined?(rec.city) && rec.city != '') ? rec.city : FIELD_NOT_SUPPORTED
|
|
215
|
+
else
|
|
216
|
+
city = INVALID_IP_ADDRESS
|
|
217
|
+
end
|
|
218
|
+
else
|
|
219
|
+
city = INVALID_IP_ADDRESS
|
|
220
|
+
end
|
|
221
|
+
return city
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def get_latitude(ip)
|
|
225
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
226
|
+
if valid
|
|
227
|
+
rec = get_record(ip)
|
|
228
|
+
if !(rec.nil?)
|
|
229
|
+
latitude = (defined?(rec.latitude) && rec.latitude != '') ? rec.latitude : FIELD_NOT_SUPPORTED
|
|
230
|
+
else
|
|
231
|
+
latitude = INVALID_IP_ADDRESS
|
|
232
|
+
end
|
|
233
|
+
else
|
|
234
|
+
latitude = INVALID_IP_ADDRESS
|
|
235
|
+
end
|
|
236
|
+
return latitude
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def get_longitude(ip)
|
|
240
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
241
|
+
if valid
|
|
242
|
+
rec = get_record(ip)
|
|
243
|
+
if !(rec.nil?)
|
|
244
|
+
longitude = (defined?(rec.longitude) && rec.longitude != '') ? rec.longitude : FIELD_NOT_SUPPORTED
|
|
245
|
+
else
|
|
246
|
+
longitude = INVALID_IP_ADDRESS
|
|
247
|
+
end
|
|
248
|
+
else
|
|
249
|
+
longitude = INVALID_IP_ADDRESS
|
|
250
|
+
end
|
|
251
|
+
return longitude
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
def get_isp(ip)
|
|
255
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
256
|
+
if valid
|
|
257
|
+
rec = get_record(ip)
|
|
258
|
+
if !(rec.nil?)
|
|
259
|
+
isp = (defined?(rec.isp) && rec.isp != '') ? rec.isp : FIELD_NOT_SUPPORTED
|
|
260
|
+
else
|
|
261
|
+
isp = INVALID_IP_ADDRESS
|
|
262
|
+
end
|
|
263
|
+
else
|
|
264
|
+
isp = INVALID_IP_ADDRESS
|
|
265
|
+
end
|
|
266
|
+
return isp
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
def get_domain(ip)
|
|
270
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
271
|
+
if valid
|
|
272
|
+
rec = get_record(ip)
|
|
273
|
+
if !(rec.nil?)
|
|
274
|
+
domain = (defined?(rec.domain) && rec.domain != '') ? rec.domain : FIELD_NOT_SUPPORTED
|
|
275
|
+
else
|
|
276
|
+
domain = INVALID_IP_ADDRESS
|
|
277
|
+
end
|
|
278
|
+
else
|
|
279
|
+
domain = INVALID_IP_ADDRESS
|
|
280
|
+
end
|
|
281
|
+
return domain
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
def get_zipcode(ip)
|
|
285
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
286
|
+
if valid
|
|
287
|
+
rec = get_record(ip)
|
|
288
|
+
if !(rec.nil?)
|
|
289
|
+
zipcode = (defined?(rec.zipcode) && rec.zipcode != '') ? rec.zipcode : FIELD_NOT_SUPPORTED
|
|
290
|
+
else
|
|
291
|
+
zipcode = INVALID_IP_ADDRESS
|
|
292
|
+
end
|
|
293
|
+
else
|
|
294
|
+
zipcode = INVALID_IP_ADDRESS
|
|
295
|
+
end
|
|
296
|
+
return zipcode
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def get_timezone(ip)
|
|
300
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
301
|
+
if valid
|
|
302
|
+
rec = get_record(ip)
|
|
303
|
+
if !(rec.nil?)
|
|
304
|
+
timezone = (defined?(rec.timezone) && rec.timezone != '') ? rec.timezone : FIELD_NOT_SUPPORTED
|
|
305
|
+
else
|
|
306
|
+
timezone = INVALID_IP_ADDRESS
|
|
307
|
+
end
|
|
308
|
+
else
|
|
309
|
+
timezone = INVALID_IP_ADDRESS
|
|
310
|
+
end
|
|
311
|
+
return timezone
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
def get_netspeed(ip)
|
|
315
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
316
|
+
if valid
|
|
317
|
+
rec = get_record(ip)
|
|
318
|
+
if !(rec.nil?)
|
|
319
|
+
netspeed = (defined?(rec.netspeed) && rec.netspeed != '') ? rec.netspeed : FIELD_NOT_SUPPORTED
|
|
320
|
+
else
|
|
321
|
+
netspeed = INVALID_IP_ADDRESS
|
|
322
|
+
end
|
|
323
|
+
else
|
|
324
|
+
netspeed = INVALID_IP_ADDRESS
|
|
325
|
+
end
|
|
326
|
+
return netspeed
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
def get_iddcode(ip)
|
|
330
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
331
|
+
if valid
|
|
332
|
+
rec = get_record(ip)
|
|
333
|
+
if !(rec.nil?)
|
|
334
|
+
iddcode = (defined?(rec.iddcode) && rec.iddcode != '') ? rec.iddcode : FIELD_NOT_SUPPORTED
|
|
335
|
+
else
|
|
336
|
+
iddcode = INVALID_IP_ADDRESS
|
|
337
|
+
end
|
|
338
|
+
else
|
|
339
|
+
iddcode = INVALID_IP_ADDRESS
|
|
340
|
+
end
|
|
341
|
+
return iddcode
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
def get_areacode(ip)
|
|
345
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
346
|
+
if valid
|
|
347
|
+
rec = get_record(ip)
|
|
348
|
+
if !(rec.nil?)
|
|
349
|
+
areacode = (defined?(rec.areacode) && rec.areacode != '') ? rec.areacode : FIELD_NOT_SUPPORTED
|
|
350
|
+
else
|
|
351
|
+
areacode = INVALID_IP_ADDRESS
|
|
352
|
+
end
|
|
353
|
+
else
|
|
354
|
+
areacode = INVALID_IP_ADDRESS
|
|
355
|
+
end
|
|
356
|
+
return areacode
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
def get_weatherstationcode(ip)
|
|
360
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
361
|
+
if valid
|
|
362
|
+
rec = get_record(ip)
|
|
363
|
+
if !(rec.nil?)
|
|
364
|
+
weatherstationcode = (defined?(rec.weatherstationcode) && rec.weatherstationcode != '') ? rec.weatherstationcode : FIELD_NOT_SUPPORTED
|
|
365
|
+
else
|
|
366
|
+
weatherstationcode = INVALID_IP_ADDRESS
|
|
367
|
+
end
|
|
368
|
+
else
|
|
369
|
+
weatherstationcode = INVALID_IP_ADDRESS
|
|
370
|
+
end
|
|
371
|
+
return weatherstationcode
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
def get_weatherstationname(ip)
|
|
375
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
376
|
+
if valid
|
|
377
|
+
rec = get_record(ip)
|
|
378
|
+
if !(rec.nil?)
|
|
379
|
+
weatherstationname = (defined?(rec.weatherstationname) && rec.weatherstationname != '') ? rec.weatherstationname : FIELD_NOT_SUPPORTED
|
|
380
|
+
else
|
|
381
|
+
weatherstationname = INVALID_IP_ADDRESS
|
|
382
|
+
end
|
|
383
|
+
else
|
|
384
|
+
weatherstationname = INVALID_IP_ADDRESS
|
|
385
|
+
end
|
|
386
|
+
return weatherstationname
|
|
387
|
+
end
|
|
388
|
+
|
|
389
|
+
def get_mcc(ip)
|
|
390
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
391
|
+
if valid
|
|
392
|
+
rec = get_record(ip)
|
|
393
|
+
if !(rec.nil?)
|
|
394
|
+
mcc = (defined?(rec.mcc) && rec.mcc != '') ? rec.mcc : FIELD_NOT_SUPPORTED
|
|
395
|
+
else
|
|
396
|
+
mcc = INVALID_IP_ADDRESS
|
|
397
|
+
end
|
|
398
|
+
else
|
|
399
|
+
mcc = INVALID_IP_ADDRESS
|
|
400
|
+
end
|
|
401
|
+
return mcc
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
def get_mnc(ip)
|
|
405
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
406
|
+
if valid
|
|
407
|
+
rec = get_record(ip)
|
|
408
|
+
if !(rec.nil?)
|
|
409
|
+
mnc = (defined?(mnc) && rec.mnc != '') ? rec.mnc : FIELD_NOT_SUPPORTED
|
|
410
|
+
else
|
|
411
|
+
mnc = INVALID_IP_ADDRESS
|
|
412
|
+
end
|
|
413
|
+
else
|
|
414
|
+
mnc = INVALID_IP_ADDRESS
|
|
415
|
+
end
|
|
416
|
+
return mnc
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
def get_mobilebrand(ip)
|
|
420
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
421
|
+
if valid
|
|
422
|
+
rec = get_record(ip)
|
|
423
|
+
if !(rec.nil?)
|
|
424
|
+
mobilebrand = (defined?(rec.mobilebrand) && rec.mobilebrand != '') ? rec.mobilebrand : FIELD_NOT_SUPPORTED
|
|
425
|
+
else
|
|
426
|
+
mobilebrand = INVALID_IP_ADDRESS
|
|
427
|
+
end
|
|
428
|
+
else
|
|
429
|
+
mobilebrand = INVALID_IP_ADDRESS
|
|
430
|
+
end
|
|
431
|
+
return mobilebrand
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
def get_elevation(ip)
|
|
435
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
436
|
+
if valid
|
|
437
|
+
rec = get_record(ip)
|
|
438
|
+
if !(rec.nil?)
|
|
439
|
+
elevation = (defined?(rec.elevation) && rec.elevation != '') ? rec.elevation : FIELD_NOT_SUPPORTED
|
|
440
|
+
else
|
|
441
|
+
elevation = INVALID_IP_ADDRESS
|
|
442
|
+
end
|
|
443
|
+
else
|
|
444
|
+
elevation = INVALID_IP_ADDRESS
|
|
445
|
+
end
|
|
446
|
+
return elevation
|
|
447
|
+
end
|
|
448
|
+
|
|
449
|
+
def get_usagetype(ip)
|
|
450
|
+
valid = !(IPAddr.new(ip) rescue nil).nil?
|
|
451
|
+
if valid
|
|
452
|
+
rec = get_record(ip)
|
|
453
|
+
if !(rec.nil?)
|
|
454
|
+
usagetype = (defined?(rec.usagetype) && rec.usagetype != '') ? rec.usagetype : FIELD_NOT_SUPPORTED
|
|
455
|
+
else
|
|
456
|
+
usagetype = INVALID_IP_ADDRESS
|
|
457
|
+
end
|
|
458
|
+
else
|
|
459
|
+
usagetype = INVALID_IP_ADDRESS
|
|
460
|
+
end
|
|
461
|
+
return usagetype
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
def bsearch(low, high, ipnum, base_addr, col_length)
|
|
465
|
+
while low <= high do
|
|
466
|
+
mid = (low + high) >> 1
|
|
467
|
+
ip_from, ip_to = get_from_to(mid, base_addr, col_length)
|
|
468
|
+
if ipnum >= ip_from && ipnum < ip_to
|
|
469
|
+
from_base = ( base_addr + mid * (col_length + (self.v4 ? 0 : 12)))
|
|
470
|
+
file.seek(from_base)
|
|
471
|
+
if v4
|
|
472
|
+
return self.record_class4.read(file)
|
|
473
|
+
else
|
|
474
|
+
return self.record_class6.read(file)
|
|
475
|
+
end
|
|
476
|
+
else
|
|
477
|
+
if ipnum < ip_from
|
|
478
|
+
high = mid - 1
|
|
479
|
+
else
|
|
480
|
+
low = mid + 1
|
|
481
|
+
end
|
|
482
|
+
end
|
|
483
|
+
end
|
|
484
|
+
end
|
|
485
|
+
|
|
486
|
+
def get_from_to(mid, base_addr, col_length)
|
|
487
|
+
from_base = ( base_addr + mid * (col_length + (v4 ? 0 : 12)))
|
|
488
|
+
file.seek(from_base)
|
|
489
|
+
ip_from = v4 ? file.read(4).unpack('V').first : readipv6(file)
|
|
490
|
+
file.seek(from_base + col_length + (v4 ? 0 : 12))
|
|
491
|
+
ip_to = v4 ? file.read(4).unpack('V').first : readipv6(file)
|
|
492
|
+
[ip_from, ip_to]
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
def validateip(ip)
|
|
496
|
+
if ip.ipv4?
|
|
497
|
+
ipv = 4
|
|
498
|
+
ipnum = ip.to_i + 0
|
|
499
|
+
else
|
|
500
|
+
ipv = 6
|
|
501
|
+
ipnum = ip.to_i + 0
|
|
502
|
+
#reformat ipv4 address in ipv6
|
|
503
|
+
if ipnum >= 281470681743360 && ipnum <= 281474976710655
|
|
504
|
+
ipv = 4
|
|
505
|
+
ipnum = ipnum - 281470681743360
|
|
506
|
+
end
|
|
507
|
+
#reformat 6to4 address to ipv4 address 2002:: to 2002:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
|
|
508
|
+
if ipnum >= 42545680458834377588178886921629466624 && ipnum <= 42550872755692912415807417417958686719
|
|
509
|
+
ipv = 4
|
|
510
|
+
#bitshift right 80 bits
|
|
511
|
+
ipnum = ipnum >> 80
|
|
512
|
+
#bitwise modulus to get the last 32 bit
|
|
513
|
+
ipnum = ipnum % 4294967296
|
|
514
|
+
end
|
|
515
|
+
#reformat Teredo address to ipv4 address 2001:0000:: to 2001:0000:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:
|
|
516
|
+
if ipnum >= 42540488161975842760550356425300246528 && ipnum <= 42540488241204005274814694018844196863
|
|
517
|
+
ipv = 4
|
|
518
|
+
#bitwise not to invert binary
|
|
519
|
+
ipnum = ~ipnum
|
|
520
|
+
#bitwise modulus to get the last 32 bit
|
|
521
|
+
ipnum = ipnum % 4294967296
|
|
522
|
+
end
|
|
523
|
+
end
|
|
524
|
+
[ipv, ipnum]
|
|
525
|
+
end
|
|
526
|
+
|
|
527
|
+
def read32(indexp)
|
|
528
|
+
file.seek(indexp - 1)
|
|
529
|
+
return file.read(4).unpack('V').first
|
|
530
|
+
end
|
|
531
|
+
|
|
532
|
+
def readipv6(filer)
|
|
533
|
+
parts = filer.read(16).unpack('V*')
|
|
534
|
+
return parts[0] + parts[1] * 4294967296 + parts[2] * 4294967296**2 + parts[3] * 4294967296**3
|
|
535
|
+
end
|
|
536
|
+
|
|
537
|
+
private :get_record, :bsearch, :get_from_to, :read32, :readipv6
|
|
538
|
+
|
|
539
|
+
end
|