ffi-geoip 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ccd128b11adeb854b516279654e8d309c9fc1c32
4
+ data.tar.gz: 05a0b119ed18f0e236b53605861914adc67e0365
5
+ SHA512:
6
+ metadata.gz: 325303b0a104fc0a63296051324a90deb16e95c317624c68ef240f3ebf9f36f25528af7b2942940ed589c8caf3f5683bbb2a3560e21f877e0b7cabda43254ad1
7
+ data.tar.gz: 12a91c22e4902b90ac83bb0b9b83247d01766efa1099d6784be12b5db5a27bdc98cc5e43ceb66a5a7c3edc61766f9fa22ccc79b5d169fc71a742f61afcd6ed77
@@ -0,0 +1,23 @@
1
+ *.orig
2
+ .*.swp
3
+ .*.swo
4
+ *.tmp
5
+ *.kpf
6
+ *.komodoproject
7
+ *~
8
+ .DS_Store
9
+ *tmp*
10
+ pkg/
11
+ doc/
12
+ coverage/
13
+ *.orig
14
+ *.patch
15
+ .vimrc_local
16
+ *.rbc
17
+ test-all.sh
18
+ .rbx
19
+ Gemfile.lock
20
+ Gemfile.local
21
+ Guardfile.local
22
+ *.dat
23
+ *.csv
@@ -0,0 +1,14 @@
1
+ cache: bundler
2
+ sudo: false
3
+ language: ruby
4
+ rvm:
5
+ - 2.2.3
6
+ - 2.1.7
7
+ - 2.0.0
8
+ - 1.9.3
9
+ - rbx-2
10
+ - jruby-19mode
11
+ addons:
12
+ apt:
13
+ packages:
14
+ - libgeoip-dev
data/Gemfile ADDED
@@ -0,0 +1,25 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem "rdoc", "~> 3.12"
6
+ gem "rake", "~> 10.0"
7
+ gem "minitest"
8
+ gem "minitest-reporters"
9
+
10
+ platforms :rbx do
11
+ gem 'rubysl', '~> 2.0'
12
+ gem 'rubinius-developer_tools'
13
+ end
14
+
15
+
16
+ if RUBY_VERSION >= '1.9'
17
+ gem "simplecov"
18
+ gem "guard"
19
+ gem 'guard-minitest'
20
+ end
21
+
22
+ if File.exists?('Gemfile.local')
23
+ instance_eval File.read('Gemfile.local')
24
+ end
25
+
@@ -0,0 +1,17 @@
1
+
2
+ guard 'minitest', :test_folders => 'test', :test_file_patterns => '*_tests.rb' do
3
+ watch(%r|^test/(.+)_tests\.rb|)
4
+
5
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) do |m|
6
+ "test/#{m[2]}_tests.rb"
7
+ end
8
+
9
+ watch(%r|^test/test_helper\.rb|) do
10
+ "test"
11
+ end
12
+ end
13
+
14
+ if File.exists?('Guardfile.local')
15
+ instance_eval File.read('Guardfile.local')
16
+ end
17
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014-2015 J Smith <dark.panda@gmail.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,22 @@
1
+
2
+ = geoip-c Ruby Bindings via FFI
3
+
4
+ == Requirements
5
+
6
+ * the ffi extension for Ruby.
7
+ * the GeoIP library version 1.5 or higher. Lower versions may work to some
8
+ extent but this is untested.
9
+
10
+ == Features
11
+
12
+ ffi-geoip is an FFI-based implementation of the native GeoIP Ruby bindings.
13
+ This gem mimics the native GeoIP bindings as closely as possible and should be
14
+ directly compatible. To test this, you can run the tests using the native
15
+ bindings via the `USE_BINARY` environment variable and they should also
16
+ succeed. A simple use-case for this extension is for, say, JRuby, where the
17
+ native GeoIP gem is not available.
18
+
19
+ == License
20
+
21
+ This gem is licensed under an MIT-style license. See the +MIT-LICENSE+ file for
22
+ details.
@@ -0,0 +1,29 @@
1
+
2
+ # -*- ruby -*-
3
+
4
+ require 'rubygems'
5
+ require 'rubygems/package_task'
6
+ require 'rake/testtask'
7
+ require 'rdoc/task'
8
+ require 'bundler/gem_tasks'
9
+
10
+ $:.push File.expand_path(File.dirname(__FILE__), 'lib')
11
+
12
+ version = GeoIP::VERSION
13
+
14
+ desc 'Test GeoIP interface'
15
+ Rake::TestTask.new(:test) do |t|
16
+ t.test_files = FileList['test/**/*_tests.rb']
17
+ t.verbose = !!ENV['VERBOSE_TESTS']
18
+ t.warning = !!ENV['WARNINGS']
19
+ end
20
+
21
+ task :default => :test
22
+
23
+ desc 'Build docs'
24
+ Rake::RDocTask.new do |t|
25
+ t.title = "ffi-geoip #{version}"
26
+ t.main = 'README.rdoc'
27
+ t.rdoc_dir = 'doc'
28
+ t.rdoc_files.include('README.rdoc', 'MIT-LICENSE', 'lib/**/*.rb')
29
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../lib/ffi-geoip/version', __FILE__)
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ffi-geoip"
7
+ s.version = GeoIP::VERSION
8
+
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.authors = ["J Smith"]
11
+ s.description = "An ffi wrapper for GeoIP"
12
+ s.summary = s.description
13
+ s.email = "dark.panda@gmail.com"
14
+ s.license = "MIT"
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = `git ls-files`.split($\)
19
+ s.executables = s.files.grep(%r{^bin/}).map { |f| File.basename(f) }
20
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
21
+ s.homepage = "http://github.com/dark-panda/ffi-geoip"
22
+ s.require_paths = ["lib"]
23
+
24
+ s.add_dependency("ffi", [">= 1.0.0"])
25
+ end
26
+
@@ -0,0 +1,470 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'ffi'
4
+ require 'rbconfig'
5
+ require 'ffi-geoip/version'
6
+
7
+ module GeoIP
8
+ GEOIP_BASE = File.join(File.dirname(__FILE__), 'ffi-geoip')
9
+
10
+ autoload :Base,
11
+ File.join(GEOIP_BASE, 'base')
12
+
13
+ autoload :City,
14
+ File.join(GEOIP_BASE, 'city')
15
+
16
+ autoload :Country,
17
+ File.join(GEOIP_BASE, 'country')
18
+
19
+ autoload :Organization,
20
+ File.join(GEOIP_BASE, 'organization')
21
+
22
+ autoload :ISP,
23
+ File.join(GEOIP_BASE, 'isp')
24
+
25
+ autoload :NetSpeed,
26
+ File.join(GEOIP_BASE, 'netspeed')
27
+
28
+ autoload :Domain,
29
+ File.join(GEOIP_BASE, 'domain')
30
+
31
+ autoload :Tools,
32
+ File.join(GEOIP_BASE, 'tools')
33
+
34
+ autoload :Record,
35
+ File.join(GEOIP_BASE, 'record')
36
+
37
+ module FFIGeoIP
38
+ def self.search_paths
39
+ @search_paths ||= begin
40
+ if ENV['GEOIP_LIBRARY_PATH']
41
+ [ ENV['GEOIP_LIBRARY_PATH'] ]
42
+ elsif FFI::Platform::IS_WINDOWS
43
+ ENV['PATH'].split(File::PATH_SEPARATOR)
44
+ else
45
+ [ '/usr/local/{lib64,lib}', '/opt/local/{lib64,lib}', '/usr/{lib64,lib}' ]
46
+ [ '/usr/local/{lib64,lib}', '/opt/local/{lib64,lib}', '/usr/{lib64,lib}', '/usr/lib/{x86_64,i386}-linux-gnu' ]
47
+ end
48
+ end
49
+ end
50
+
51
+ def self.find_lib(lib)
52
+ if ENV['GEOIP_LIBRARY_PATH'] && File.file?(ENV['GEOIP_LIBRARY_PATH'])
53
+ ENV['GEOIP_LIBRARY_PATH']
54
+ else
55
+ Dir.glob(search_paths.map { |path|
56
+ File.expand_path(File.join(path, "#{lib}.#{FFI::Platform::LIBSUFFIX}"))
57
+ }).first
58
+ end
59
+ end
60
+
61
+ def self.geoip_library_path
62
+ @geoip_library_path ||= begin
63
+ # On MingW the libraries have version numbers
64
+ find_lib('{lib,}GeoIP{,-?}')
65
+ end
66
+ end
67
+
68
+ extend ::FFI::Library
69
+
70
+ GeoIP::GeoIPOptions = enum(:geoip_option, [
71
+ :standard, 0,
72
+ :memory_cache, 1,
73
+ :check_cache, 2,
74
+ :index_cache, 4,
75
+ :mmap_cache, 8
76
+ ])
77
+
78
+ FFI_LAYOUT = {
79
+ :GeoIP_open => [
80
+ # GeoIP*, filename, flags
81
+ :pointer, :string, :int
82
+ ],
83
+
84
+ :GeoIP_delete => [
85
+ # void, GeoIP*
86
+ :void, :pointer
87
+ ],
88
+
89
+ #### countries ####
90
+ # string, GeoIP*, addr
91
+ :GeoIP_country_code_by_addr => [
92
+ :string, :pointer, :string
93
+ ],
94
+
95
+ # string, GeoIP*, host
96
+ :GeoIP_country_code_by_name => [
97
+ :string, :pointer, :string
98
+ ],
99
+
100
+ # string, GeoIP*, addr
101
+ :GeoIP_country_code3_by_addr => [
102
+ :string, :pointer, :string
103
+ ],
104
+
105
+ # string, GeoIP*, host
106
+ :GeoIP_country_code3_by_name => [
107
+ :string, :pointer, :string
108
+ ],
109
+
110
+ # string, GeoIP*, addr
111
+ :GeoIP_country_name_by_addr => [
112
+ :string, :pointer, :string
113
+ ],
114
+
115
+ # string, GeoIP*, host
116
+ :GeoIP_country_name_by_name => [
117
+ :string, :pointer, :string
118
+ ],
119
+
120
+ # string, GeoIP*, ipnum
121
+ :GeoIP_country_name_by_ipnum => [
122
+ :string, :pointer, :ulong
123
+ ],
124
+
125
+ # string, GeoIP*, ipnum
126
+ :GeoIP_country_code_by_ipnum => [
127
+ :string, :pointer, :ulong
128
+ ],
129
+
130
+ # string, GeoIP*, ipnum
131
+ :GeoIP_country_code3_by_ipnum => [
132
+ :string, :pointer, :ulong
133
+ ],
134
+
135
+ # # string, GeoIP*, geoipv6_t ipnum
136
+ # :GeoIP_country_name_by_ipnum_v6 => [
137
+ # :string, :pointer, :geoipv6_t
138
+ # ],
139
+ # # string, GeoIP*, geoipv6_t ipnum
140
+ # :GeoIP_country_code_by_ipnum_v6 => [
141
+ # :string, :pointer, :geoipv6_t
142
+ # ],
143
+ # # string, GeoIP*, geoipv6_t ipnum
144
+ # :GeoIP_country_code3_by_ipnum_v6 => [
145
+ # :string, :pointer, :geoipv6_t
146
+ # ],
147
+
148
+ # string, GeoIP*, addr
149
+ :GeoIP_country_code_by_addr_v6 => [
150
+ :string, :pointer, :string
151
+ ],
152
+
153
+ # string, GeoIP*, host
154
+ :GeoIP_country_code_by_name_v6 => [
155
+ :string, :pointer, :string
156
+ ],
157
+
158
+ # string, GeoIP*, addr
159
+ :GeoIP_country_code3_by_addr_v6 => [
160
+ :string, :pointer, :string
161
+ ],
162
+
163
+ # string, GeoIP*, host
164
+ :GeoIP_country_code3_by_name_v6 => [
165
+ :string, :pointer, :string
166
+ ],
167
+
168
+ # string, GeoIP*, addr
169
+ :GeoIP_country_name_by_addr_v6 => [
170
+ :string, :pointer, :string
171
+ ],
172
+
173
+ # string, GeoIP*, host
174
+ :GeoIP_country_name_by_name_v6 => [
175
+ :string, :pointer, :string
176
+ ],
177
+ #### /countries ####
178
+
179
+ #### IDs ####
180
+ # id, GeoIP*, addr
181
+ :GeoIP_id_by_addr => [
182
+ :int, :pointer, :string
183
+ ],
184
+
185
+ # id, GeoIP*, host
186
+ :GeoIP_id_by_name => [
187
+ :int, :pointer, :string
188
+ ],
189
+
190
+ # id, GeoIP*, ipnum
191
+ :GeoIP_id_by_ipnum => [
192
+ :int, :pointer, :ulong
193
+ ],
194
+
195
+ # id, GeoIP*, addr
196
+ :GeoIP_id_by_addr_v6 => [
197
+ :int, :pointer, :string
198
+ ],
199
+
200
+ # id, GeoIP*, host
201
+ :GeoIP_id_by_name_v6 => [
202
+ :int, :pointer, :string
203
+ ],
204
+
205
+ # # id, GeoIP*, ipnum
206
+ # :GeoIP_id_by_ipnum_v6 => [
207
+ # :int, :pointer, :geoipv6_t
208
+ # ],
209
+ #### /IDs ####
210
+
211
+ #### regions ####
212
+ # GeoIPRegion*, GeoIP*, addr
213
+ :GeoIP_region_by_addr => [
214
+ :pointer, :pointer, :string
215
+ ],
216
+
217
+ # GeoIPRegion*, GeoIP*, host
218
+ :GeoIP_region_by_name => [
219
+ :pointer, :pointer, :string
220
+ ],
221
+
222
+ # GeoIPRegion*, GeoIP*, ipnum
223
+ :GeoIP_region_by_ipnum => [
224
+ :pointer, :pointer, :ulong
225
+ ],
226
+
227
+ # GeoIPRegion*, GeoIP*, addr
228
+ :GeoIP_region_by_addr_v6 => [
229
+ :pointer, :pointer, :string
230
+ ],
231
+
232
+ # GeoIPRegion*, GeoIP*, host
233
+ :GeoIP_region_by_name_v6 => [
234
+ :pointer, :pointer, :string
235
+ ],
236
+
237
+ # # GeoIPRegion*, GeoIP*, ipnum
238
+ # :GeoIP_region_by_ipnum_v6 => [
239
+ # :pointer, :pointer, :geoipv6_t
240
+ # ],
241
+
242
+ # void, GeoIPRegion*
243
+ :GeoIPRegion_delete => [
244
+ :void, :pointer
245
+ ],
246
+ #### /region
247
+
248
+ #### organization names ####
249
+ # name, GeoIP*, ipnum
250
+ :GeoIP_name_by_ipnum => [
251
+ :string, :pointer, :ulong
252
+ ],
253
+ # name, GeoIP*, addr
254
+ :GeoIP_name_by_addr => [
255
+ :string, :pointer, :string
256
+ ],
257
+ # name, GeoIP*, host
258
+ :GeoIP_name_by_name => [
259
+ :string, :pointer, :string
260
+ ],
261
+
262
+ # # name, GeoIP*, ipnum
263
+ # :GeoIP_name_by_ipnum_v6 => [
264
+ # :string, :pointer, :geoipv6_t
265
+ # ],
266
+
267
+ # name, GeoIP*, addr
268
+ :GeoIP_name_by_addr_v6 => [
269
+ :string, :pointer, :string
270
+ ],
271
+ # name, GeoIP*, host
272
+ :GeoIP_name_by_name_v6 => [
273
+ :string, :pointer, :string
274
+ ],
275
+ #### /organization names ####
276
+
277
+ #### fetch by IDs ####
278
+ # string, id
279
+ :GeoIP_code_by_id => [
280
+ :string, :int
281
+ ],
282
+
283
+ # string, id
284
+ :GeoIP_code3_by_id => [
285
+ :string, :int
286
+ ],
287
+
288
+ # string, GeoIP*, id
289
+ :GeoIP_country_name_by_id => [
290
+ :string, :pointer, :int
291
+ ],
292
+
293
+ # string, id
294
+ :GeoIP_name_by_id => [
295
+ :string, :int
296
+ ],
297
+
298
+ # string, id
299
+ :GeoIP_continent_by_id => [
300
+ :string, :int
301
+ ],
302
+
303
+ # id, country
304
+ :GeoIP_id_by_code => [
305
+ :int, :string
306
+ ],
307
+ #### /fetch by IDs ####
308
+
309
+ #### full city records ####
310
+ # GeoIPRecord*, GeoIP*, ipnum
311
+ :GeoIP_record_by_ipnum => [
312
+ GeoIP::Record, :pointer, :ulong
313
+ ],
314
+
315
+ # GeoIPRecord*, GeoIP*, addr
316
+ :GeoIP_record_by_addr => [
317
+ GeoIP::Record, :pointer, :string
318
+ ],
319
+
320
+ # GeoIPRecord*, GeoIP*, host
321
+ :GeoIP_record_by_name => [
322
+ GeoIP::Record, :pointer, :string
323
+ ],
324
+
325
+ # # GeoIPRecord*, GeoIP*, geoipv6_t ipnum
326
+ # :GeoIP_record_by_ipnum_v6 => [
327
+ # :pointer, :pointer, :geoipv6_t
328
+ # ],
329
+
330
+ # GeoIPRecord*, GeoIP*, addr
331
+ :GeoIP_record_by_addr_v6 => [
332
+ GeoIP::Record, :pointer, :string
333
+ ],
334
+
335
+ # GeoIPRecord*, GeoIP*, host
336
+ :GeoIP_record_by_name_v6 => [
337
+ GeoIP::Record, :pointer, :string
338
+ ],
339
+
340
+ # record ID, GeoIP*, addr
341
+ :GeoIP_record_id_by_addr => [
342
+ :int, :pointer, :string
343
+ ],
344
+
345
+ # record ID, GeoIP*, string
346
+ :GeoIP_record_id_by_addr_v6 => [
347
+ :int, :pointer, :string
348
+ ],
349
+
350
+ # void, GeoIPRecord*
351
+ :GeoIPRecord_delete => [
352
+ :void, GeoIP::Record
353
+ ],
354
+ #### /full record
355
+
356
+ #### utility functions ####
357
+ # num, void
358
+ :GeoIP_num_countries => [
359
+ :uint
360
+ ],
361
+
362
+ # info, GeoIP*
363
+ :GeoIP_database_info => [
364
+ :string, :pointer
365
+ ],
366
+
367
+ # edition, GeoIP*
368
+ :GeoIP_database_edition => [
369
+ :uchar, :pointer
370
+ ],
371
+
372
+ # int, GeoIP*
373
+ :GeoIP_charset => [
374
+ :int, :pointer
375
+ ],
376
+
377
+ # int, GeoIP*, charset
378
+ :GeoIP_set_charset => [
379
+ :int, :pointer, :int
380
+ ],
381
+
382
+ # int, GeoIP*, true/false
383
+ :GeoIP_enable_teredo => [
384
+ :int, :pointer, :int
385
+ ],
386
+
387
+ # int, GeoIP*
388
+ :GeoIP_teredo => [
389
+ :int, :pointer
390
+ ],
391
+
392
+ # int, GeoIP*
393
+ :GeoIP_last_netmask => [
394
+ :int, :pointer
395
+ ],
396
+
397
+ # char**, GeoIP*, addr
398
+ :GeoIP_range_by_ip => [
399
+ :pointer, :pointer, :string
400
+ ],
401
+
402
+ # ipnum, addr
403
+ :GeoIP_addr_to_num => [
404
+ :ulong, :string
405
+ ],
406
+
407
+ # addr, ipnum
408
+ :GeoIP_num_to_addr => [
409
+ :string, :ulong
410
+ ],
411
+ #### /utility functions ####
412
+
413
+ #### region code ####
414
+ # string, country_code, region_code
415
+ :GeoIP_region_name_by_code => [
416
+ :string, :string, :string
417
+ ],
418
+ #### /region code ####
419
+
420
+ #### time zone ####
421
+ # string, country_code, region_code
422
+ :GeoIP_time_zone_by_country_and_region => [
423
+ :string, :string, :string
424
+ ],
425
+ #### /time zone ####
426
+
427
+ # string, void
428
+ :GeoIP_lib_version => [
429
+ :string
430
+ ]
431
+ }
432
+
433
+ begin
434
+ ffi_lib(geoip_library_path)
435
+
436
+ FFI_LAYOUT.each do |fun, ary|
437
+ ret = ary.shift
438
+ begin
439
+ self.class_eval do
440
+ attach_function(fun, ary, ret)
441
+ end
442
+ rescue FFI::NotFoundError
443
+ # that's okay
444
+ end
445
+ end
446
+
447
+ rescue LoadError, NoMethodError
448
+ raise LoadError.new("Couldn't load the GeoIP library.")
449
+ end
450
+ end
451
+
452
+ class << self
453
+ include GeoIP::Tools
454
+
455
+ def version
456
+ @version ||= FFIGeoIP.GeoIP_lib_version
457
+ end
458
+
459
+ def addr_to_num(addr)
460
+ check_type(addr, String)
461
+ FFIGeoIP.GeoIP_addr_to_num(addr)
462
+ end
463
+
464
+ def num_to_addr(num)
465
+ check_type(num, Fixnum, Bignum)
466
+ FFIGeoIP.GeoIP_num_to_addr(num)
467
+ end
468
+ end
469
+ end
470
+