geoip-c 0.5.6 → 0.6.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +29 -18
- data/Rakefile +2 -2
- data/geoip.c +133 -35
- metadata +2 -1
data/README.md
CHANGED
@@ -67,34 +67,35 @@ Install
|
|
67
67
|
Some variation of the following should work.
|
68
68
|
|
69
69
|
1. Install the GeoCity C library. You can get it from
|
70
|
-
[
|
70
|
+
[MaxMind](http://www.maxmind.com/app/c).
|
71
71
|
For example, I like to install mine in `/opt/GeoIP`, so I do this:
|
72
72
|
|
73
|
-
|
73
|
+
tar -zxvf GeoIP-1.4.3.tar.gz
|
74
|
+
cd GeoIP-1.4.3
|
75
|
+
./configure --prefix=/opt/GeoIP
|
76
|
+
make && sudo make install
|
74
77
|
|
75
|
-
|
78
|
+
On Mac OS X, you can install using MacPorts:
|
76
79
|
|
77
|
-
|
80
|
+
sudo port install libgeoip
|
78
81
|
|
79
|
-
|
80
|
-
|
81
|
-
NOTE: for Intel Mac OS X platforms, try the following:
|
82
|
-
|
83
|
-
env ARCHFLAGS="-arch i386" ./configure --prefix=/opt/GeoIP
|
84
|
-
|
85
|
-
env ARCHFLAGS="-arch i386" make
|
82
|
+
2. Now install the `geoip` gem
|
86
83
|
|
87
|
-
|
84
|
+
gem install mtodd-geoip -s http://gems.github.com/ -- --with-geoip-dir=/opt/GeoIP
|
88
85
|
|
89
|
-
|
86
|
+
Alternatively, if you installed libgeoip using MacPorts:
|
90
87
|
|
91
|
-
|
88
|
+
env ARCHFLAGS="-arch x86_64" gem install geoip-c -- --with-opt-dir=/opt/local
|
92
89
|
|
93
90
|
3. Download the GeoLite City database file in binary format at http://www.maxmind.com/app/geolitecity
|
94
91
|
Maybe this [direct link](http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz) will work.
|
95
92
|
I put this file in
|
96
93
|
|
97
|
-
|
94
|
+
/opt/GeoIP/share/GeoIP/GeoLiteCity.dat
|
95
|
+
|
96
|
+
If you installed libgeoip using MacPorts then put it in:
|
97
|
+
|
98
|
+
/opt/local/share/GeoIP/GeoLiteCity.dat
|
98
99
|
|
99
100
|
If you are a paying customer, you will download the files required below:
|
100
101
|
|
@@ -111,11 +112,11 @@ Hints
|
|
111
112
|
|
112
113
|
1. Might need to set
|
113
114
|
|
114
|
-
|
115
|
+
export ARCHFLAGS="-arch i386"
|
115
116
|
|
116
|
-
|
117
|
+
to be able to compile the gem.
|
117
118
|
|
118
|
-
|
119
|
+
Example:
|
119
120
|
|
120
121
|
env ARCHFLAGS="-arch i386" gem install mtodd-geoip -s http://gems.github.com/ -- --with-geoip-dir=/opt/GeoIP
|
121
122
|
|
@@ -130,6 +131,16 @@ This iteration of the library is based on the hard work of Ryah Dahl (ry@tinyclo
|
|
130
131
|
[rdocs](http://geoip-city.rubyforge.org/)
|
131
132
|
[git repo](https://github.com/ry/geoip-city/tree)
|
132
133
|
|
134
|
+
Thanks
|
135
|
+
------
|
136
|
+
|
137
|
+
Special appreciation and thanks belongs to Ry Dahl for his initial work on this library.
|
138
|
+
|
139
|
+
Many thanks to our contributors:
|
140
|
+
|
141
|
+
* Charles Brian Quinn
|
142
|
+
* Michael Sheakoski
|
143
|
+
|
133
144
|
License
|
134
145
|
-------
|
135
146
|
Copyright (C) 2007--2009 Ryah Dahl (ry@tinyclouds.org), Matt Todd (mtodd@highgroove.com)
|
data/Rakefile
CHANGED
@@ -22,9 +22,9 @@ end
|
|
22
22
|
|
23
23
|
spec = Gem::Specification.new do |s|
|
24
24
|
s.name = 'geoip-c'
|
25
|
-
s.version = "0.
|
25
|
+
s.version = "0.6.3"
|
26
26
|
|
27
|
-
s.authors = ['Ryah Dahl', 'Matt Todd', 'Charles Brian Quinn']
|
27
|
+
s.authors = ['Ryah Dahl', 'Matt Todd', 'Charles Brian Quinn', 'Michael Sheakoski']
|
28
28
|
s.email = 'mtodd@highgroove.com'
|
29
29
|
|
30
30
|
s.summary = "A Binding to the GeoIP C library"
|
data/geoip.c
CHANGED
@@ -12,6 +12,9 @@
|
|
12
12
|
static VALUE mGeoIP;
|
13
13
|
static VALUE mGeoIP_City;
|
14
14
|
static VALUE mGeoIP_Organization;
|
15
|
+
static VALUE mGeoIP_ISP;
|
16
|
+
static VALUE mGeoIP_NetSpeed;
|
17
|
+
static VALUE mGeoIP_Domain;
|
15
18
|
static VALUE rb_geoip_memory;
|
16
19
|
static VALUE rb_geoip_filesystem;
|
17
20
|
static VALUE rb_geoip_index;
|
@@ -56,15 +59,27 @@ static VALUE rb_geoip_database_new(VALUE mGeoIP_Database_Class, int argc, VALUE
|
|
56
59
|
if(gi = GeoIP_open(STR2CSTR(filename), flag)) {
|
57
60
|
database = Data_Wrap_Struct(mGeoIP_Database_Class, 0, GeoIP_delete, gi);
|
58
61
|
rb_obj_call_init(database, 0, 0);
|
59
|
-
} else {
|
62
|
+
} else {
|
60
63
|
rb_sys_fail("Problem opening database");
|
61
64
|
}
|
62
65
|
return database;
|
63
66
|
}
|
64
67
|
|
68
|
+
/* Generic, single-value look up method */
|
69
|
+
static VALUE generic_single_value_lookup_response(char *key, char *value)
|
70
|
+
{
|
71
|
+
VALUE result = rb_hash_new();
|
72
|
+
if(value) {
|
73
|
+
rb_hash_sset(result, key, rb_str_new2(value));
|
74
|
+
return result;
|
75
|
+
} else {
|
76
|
+
return Qnil;
|
77
|
+
}
|
78
|
+
}
|
79
|
+
|
65
80
|
/* GeoIP::City ***************************************************************/
|
66
81
|
|
67
|
-
VALUE rb_city_record_to_hash(GeoIPRecord *record)
|
82
|
+
VALUE rb_city_record_to_hash(GeoIPRecord *record)
|
68
83
|
{
|
69
84
|
VALUE hash = rb_hash_new();
|
70
85
|
|
@@ -88,20 +103,20 @@ VALUE rb_city_record_to_hash(GeoIPRecord *record)
|
|
88
103
|
rb_hash_sset(hash, "dma_code", INT2NUM(record->dma_code));
|
89
104
|
if(record->area_code)
|
90
105
|
rb_hash_sset(hash, "area_code", INT2NUM(record->area_code));
|
91
|
-
|
106
|
+
|
92
107
|
return hash;
|
93
108
|
}
|
94
109
|
|
95
|
-
/* The first argument is the filename of the GeoIPCity.dat file
|
110
|
+
/* The first argument is the filename of the GeoIPCity.dat file
|
96
111
|
* load_option = :standard, :index, or :memory. default :memory
|
97
112
|
* check_cache = true or false. default false
|
98
|
-
*
|
113
|
+
*
|
99
114
|
* filesystem: read database from filesystem, uses least memory.
|
100
|
-
*
|
115
|
+
*
|
101
116
|
* index: the most frequently accessed index portion of the database,
|
102
117
|
* resulting in faster lookups than :filesystem, but less memory usage than
|
103
118
|
* :memory.
|
104
|
-
*
|
119
|
+
*
|
105
120
|
* memory: load database into memory, faster performance but uses more
|
106
121
|
* memory.
|
107
122
|
*/
|
@@ -112,18 +127,18 @@ static VALUE rb_geoip_city_new(int argc, VALUE *argv, VALUE self)
|
|
112
127
|
|
113
128
|
/* Pass this function an IP address as a string, it will return a hash
|
114
129
|
* containing all the information that the database knows about the IP
|
115
|
-
* db.look_up('24.24.24.24')
|
116
|
-
* => {:city=>"Ithaca", :latitude=>42.4277992248535,
|
117
|
-
* :country_code=>"US", :longitude=>-76.4981994628906,
|
118
|
-
* :country_code3=>"USA", :dma_code=>555,
|
119
|
-
* :country_name=>"United States", :area_code=>607,
|
120
|
-
* :region=>"NY"}
|
121
|
-
*/
|
130
|
+
* db.look_up('24.24.24.24')
|
131
|
+
* => {:city=>"Ithaca", :latitude=>42.4277992248535,
|
132
|
+
* :country_code=>"US", :longitude=>-76.4981994628906,
|
133
|
+
* :country_code3=>"USA", :dma_code=>555,
|
134
|
+
* :country_name=>"United States", :area_code=>607,
|
135
|
+
* :region=>"NY"}
|
136
|
+
*/
|
122
137
|
VALUE rb_geoip_city_look_up(VALUE self, VALUE addr) {
|
123
138
|
GeoIP *gi;
|
124
139
|
GeoIPRecord *record = NULL;
|
125
|
-
VALUE hash = Qnil;
|
126
|
-
|
140
|
+
VALUE hash = Qnil;
|
141
|
+
|
127
142
|
Check_Type(addr, T_STRING);
|
128
143
|
Data_Get_Struct(self, GeoIP, gi);
|
129
144
|
if(record = GeoIP_record_by_addr(gi, STR2CSTR(addr))) {
|
@@ -140,7 +155,7 @@ VALUE rb_geoip_city_look_up(VALUE self, VALUE addr) {
|
|
140
155
|
* * :memory - load the data into memory, fastest (default)
|
141
156
|
* * :filesystem - look up from filesystem, least memory intensive
|
142
157
|
* * :index - stores in memory most recent queries
|
143
|
-
*
|
158
|
+
*
|
144
159
|
*/
|
145
160
|
static VALUE rb_geoip_org_new(int argc, VALUE *argv, VALUE self)
|
146
161
|
{
|
@@ -154,25 +169,96 @@ static VALUE rb_geoip_org_new(int argc, VALUE *argv, VALUE self)
|
|
154
169
|
*/
|
155
170
|
VALUE rb_geoip_org_look_up(VALUE self, VALUE addr) {
|
156
171
|
GeoIP *gi;
|
157
|
-
VALUE hash = rb_hash_new();
|
158
|
-
char * name = NULL;
|
159
|
-
|
160
172
|
Check_Type(addr, T_STRING);
|
161
173
|
Data_Get_Struct(self, GeoIP, gi);
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
174
|
+
return generic_single_value_lookup_response("name", GeoIP_name_by_addr(gi, STR2CSTR(addr)));
|
175
|
+
}
|
176
|
+
|
177
|
+
/* GeoIP::ISP *******************************************************/
|
178
|
+
|
179
|
+
/* GeoIP::ISP.new('/path/to/GeoIPISP.dat', load_option)
|
180
|
+
* load_option can be:
|
181
|
+
* * :memory - load the data into memory, fastest (default)
|
182
|
+
* * :filesystem - look up from filesystem, least memory intensive
|
183
|
+
* * :index - stores in memory most recent queries
|
184
|
+
*
|
185
|
+
*/
|
186
|
+
static VALUE rb_geoip_isp_new(int argc, VALUE *argv, VALUE self)
|
187
|
+
{
|
188
|
+
return rb_geoip_database_new(mGeoIP_ISP, argc, argv, self);
|
189
|
+
}
|
190
|
+
|
191
|
+
/* Pass this function an IP address as a string, it will return a hash
|
192
|
+
* containing all the information that the database knows about the IP:
|
193
|
+
* db.look_up('24.24.24.24')
|
194
|
+
* => {:isp => "Road Runner"}
|
195
|
+
*/
|
196
|
+
VALUE rb_geoip_isp_look_up(VALUE self, VALUE addr) {
|
197
|
+
GeoIP *gi;
|
198
|
+
Check_Type(addr, T_STRING);
|
199
|
+
Data_Get_Struct(self, GeoIP, gi);
|
200
|
+
return generic_single_value_lookup_response("isp", GeoIP_name_by_addr(gi, STR2CSTR(addr)));
|
201
|
+
}
|
202
|
+
|
203
|
+
/* GeoIP::NetSpeed *******************************************************/
|
204
|
+
|
205
|
+
/* GeoIP::NetSpeed.new('/path/to/GeoIPNetSpeed.dat', load_option)
|
206
|
+
* load_option can be:
|
207
|
+
* * :memory - load the data into memory, fastest (default)
|
208
|
+
* * :filesystem - look up from filesystem, least memory intensive
|
209
|
+
* * :index - stores in memory most recent queries
|
210
|
+
*
|
211
|
+
*/
|
212
|
+
static VALUE rb_geoip_netspeed_new(int argc, VALUE *argv, VALUE self)
|
213
|
+
{
|
214
|
+
return rb_geoip_database_new(mGeoIP_NetSpeed, argc, argv, self);
|
215
|
+
}
|
216
|
+
|
217
|
+
/* Pass this function an IP address as a string, it will return a hash
|
218
|
+
* containing all the information that the database knows about the IP:
|
219
|
+
* db.look_up('24.24.24.24')
|
220
|
+
* => {:netspeed => "Cable/DSL"}
|
221
|
+
*/
|
222
|
+
VALUE rb_geoip_netspeed_look_up(VALUE self, VALUE addr) {
|
223
|
+
GeoIP *gi;
|
224
|
+
Check_Type(addr, T_STRING);
|
225
|
+
Data_Get_Struct(self, GeoIP, gi);
|
226
|
+
return generic_single_value_lookup_response("netspeed", GeoIP_name_by_addr(gi, STR2CSTR(addr)));
|
227
|
+
}
|
228
|
+
|
229
|
+
/* GeoIP::Domain *******************************************************/
|
230
|
+
|
231
|
+
/* GeoIP::Domain.new('/path/to/GeoIPDomain.dat', load_option)
|
232
|
+
* load_option can be:
|
233
|
+
* * :memory - load the data into memory, fastest (default)
|
234
|
+
* * :filesystem - look up from filesystem, least memory intensive
|
235
|
+
* * :index - stores in memory most recent queries
|
236
|
+
*
|
237
|
+
*/
|
238
|
+
static VALUE rb_geoip_domain_new(int argc, VALUE *argv, VALUE self)
|
239
|
+
{
|
240
|
+
return rb_geoip_database_new(mGeoIP_Domain, argc, argv, self);
|
241
|
+
}
|
242
|
+
|
243
|
+
/* Pass this function an IP address as a string, it will return a hash
|
244
|
+
* containing all the information that the database knows about the IP:
|
245
|
+
* db.look_up('24.24.24.24')
|
246
|
+
* => {:domain => "rr.com"}
|
247
|
+
*/
|
248
|
+
VALUE rb_geoip_domain_look_up(VALUE self, VALUE addr) {
|
249
|
+
GeoIP *gi;
|
250
|
+
Check_Type(addr, T_STRING);
|
251
|
+
Data_Get_Struct(self, GeoIP, gi);
|
252
|
+
return generic_single_value_lookup_response("domain", GeoIP_name_by_addr(gi, STR2CSTR(addr)));
|
167
253
|
}
|
168
254
|
|
169
255
|
/* GeoIP *********************************************************************/
|
170
256
|
|
171
257
|
/* Returns the numeric form of an IP address.
|
172
|
-
*
|
258
|
+
*
|
173
259
|
* For example:
|
174
260
|
* 24.24.24.24 => 404232216
|
175
|
-
*
|
261
|
+
*
|
176
262
|
* This is used in order to be able to perform searches in CSV versions of the
|
177
263
|
* data files or in SQL records if the data has been put there.
|
178
264
|
*/
|
@@ -253,18 +339,30 @@ void Init_geoip()
|
|
253
339
|
{
|
254
340
|
mGeoIP = rb_define_module("GeoIP");
|
255
341
|
|
256
|
-
rb_geoip_memory = ID2SYM(rb_intern("memory"));
|
257
|
-
rb_geoip_filesystem = ID2SYM(rb_intern("filesystem"));
|
258
|
-
rb_geoip_index = ID2SYM(rb_intern("index"));
|
342
|
+
rb_geoip_memory = ID2SYM(rb_intern("memory"));
|
343
|
+
rb_geoip_filesystem = ID2SYM(rb_intern("filesystem"));
|
344
|
+
rb_geoip_index = ID2SYM(rb_intern("index"));
|
259
345
|
|
260
346
|
mGeoIP_City = rb_define_class_under(mGeoIP, "City", rb_cObject);
|
261
347
|
rb_define_singleton_method(mGeoIP_City, "new", rb_geoip_city_new, -1);
|
262
|
-
rb_define_method(mGeoIP_City,
|
263
|
-
|
348
|
+
rb_define_method( mGeoIP_City, "look_up", rb_geoip_city_look_up, 1);
|
349
|
+
|
264
350
|
mGeoIP_Organization = rb_define_class_under(mGeoIP, "Organization", rb_cObject);
|
265
|
-
rb_define_singleton_method(
|
266
|
-
rb_define_method(
|
267
|
-
|
351
|
+
rb_define_singleton_method(mGeoIP_Organization, "new", rb_geoip_org_new, -1);
|
352
|
+
rb_define_method( mGeoIP_Organization, "look_up", rb_geoip_org_look_up, 1);
|
353
|
+
|
354
|
+
mGeoIP_ISP = rb_define_class_under(mGeoIP, "ISP", rb_cObject);
|
355
|
+
rb_define_singleton_method(mGeoIP_ISP, "new", rb_geoip_isp_new, -1);
|
356
|
+
rb_define_method( mGeoIP_ISP, "look_up", rb_geoip_isp_look_up, 1);
|
357
|
+
|
358
|
+
mGeoIP_NetSpeed = rb_define_class_under(mGeoIP, "NetSpeed", rb_cObject);
|
359
|
+
rb_define_singleton_method(mGeoIP_NetSpeed, "new", rb_geoip_netspeed_new, -1);
|
360
|
+
rb_define_method( mGeoIP_NetSpeed, "look_up", rb_geoip_netspeed_look_up, 1);
|
361
|
+
|
362
|
+
mGeoIP_Domain = rb_define_class_under(mGeoIP, "Domain", rb_cObject);
|
363
|
+
rb_define_singleton_method(mGeoIP_Domain, "new", rb_geoip_domain_new, -1);
|
364
|
+
rb_define_method( mGeoIP_Domain, "look_up", rb_geoip_domain_look_up, 1);
|
365
|
+
|
268
366
|
rb_define_singleton_method(mGeoIP, "addr_to_num", rb_geoip_addr_to_num, 1);
|
269
367
|
rb_define_singleton_method(mGeoIP, "num_to_addr", rb_geoip_num_to_addr, 1);
|
270
368
|
}
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: geoip-c
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryah Dahl
|
8
8
|
- Matt Todd
|
9
9
|
- Charles Brian Quinn
|
10
|
+
- Michael Sheakoski
|
10
11
|
autorequire:
|
11
12
|
bindir: bin
|
12
13
|
cert_chain: []
|