forked from mtodd/geoip
-
Notifications
You must be signed in to change notification settings - Fork 2
/
geoip.c
454 lines (392 loc) · 16 KB
/
geoip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/* ry dahl <[email protected]> May 21, 2007 */
/* Matt Todd <[email protected]> June 4, 2009 */
/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details. */
#include <ruby.h>
#include <GeoIP.h>
#include <GeoIPCity.h>
static VALUE mGeoIP;
static VALUE mGeoIP_City;
static VALUE mGeoIP_Region;
static VALUE mGeoIP_Country;
static VALUE mGeoIP_CountryV6;
static VALUE mGeoIP_Organization;
static VALUE mGeoIP_ISP;
static VALUE mGeoIP_NetSpeed;
static VALUE mGeoIP_Domain;
static VALUE rb_geoip_memory;
static VALUE rb_geoip_filesystem;
static VALUE rb_geoip_index;
/* helpers */
void rb_hash_sset(VALUE hash, const char *str, VALUE v) {
rb_hash_aset(hash, ID2SYM(rb_intern(str)), v);
}
int check_load_option(VALUE load_option) {
if(load_option == rb_geoip_memory) {
return GEOIP_MEMORY_CACHE;
} else if(load_option == rb_geoip_filesystem) {
return GEOIP_STANDARD;
} else if(load_option == rb_geoip_index) {
return GEOIP_INDEX_CACHE;
} else {
rb_raise(rb_eTypeError, "the second option must be :memory, :filesystem, or :index");
return Qnil;
}
}
/* Generic initializer */
static VALUE rb_geoip_database_new(VALUE mGeoIP_Database_Class, int argc, VALUE *argv, VALUE self)
{
GeoIP *gi;
VALUE database = Qnil;
VALUE filename, load_option = Qnil, check_cache = Qnil;
int flag;
rb_scan_args(argc, argv, "12", &filename, &load_option, &check_cache);
if(NIL_P(load_option))
load_option = rb_geoip_memory;
if(NIL_P(check_cache))
check_cache = Qfalse;
Check_Type(load_option, T_SYMBOL);
if(flag = check_load_option(load_option)) flag;
if(RTEST(check_cache)) flag |= GEOIP_CHECK_CACHE;
if(gi = GeoIP_open(StringValuePtr(filename), flag)) {
database = Data_Wrap_Struct(mGeoIP_Database_Class, 0, GeoIP_delete, gi);
rb_obj_call_init(database, 0, 0);
} else {
rb_sys_fail("Problem opening database");
}
return database;
}
/* Generic, single-value look up method */
static VALUE generic_single_value_lookup_response(char *key, char *value)
{
VALUE result = rb_hash_new();
if(value) {
rb_hash_sset(result, key, rb_str_new2(value));
return result;
} else {
return Qnil;
}
}
/* GeoIP::City ***************************************************************/
VALUE rb_city_record_to_hash(GeoIPRecord *record)
{
VALUE hash = rb_hash_new();
if(record->country_code)
rb_hash_sset(hash, "country_code", rb_str_new2(record->country_code));
if(record->country_code3)
rb_hash_sset(hash, "country_code3", rb_str_new2(record->country_code3));
if(record->country_name)
rb_hash_sset(hash, "country_name", rb_str_new2(record->country_name));
if(record->region)
rb_hash_sset(hash, "region", rb_str_new2(record->region));
if(record->city)
rb_hash_sset(hash, "city", rb_str_new2(record->city));
if(record->postal_code)
rb_hash_sset(hash, "postal_code", rb_str_new2(record->postal_code));
if(record->latitude)
rb_hash_sset(hash, "latitude", rb_float_new((double)record->latitude));
if(record->longitude)
rb_hash_sset(hash, "longitude", rb_float_new((double)record->longitude));
if(record->dma_code)
rb_hash_sset(hash, "dma_code", INT2NUM(record->dma_code));
if(record->area_code)
rb_hash_sset(hash, "area_code", INT2NUM(record->area_code));
return hash;
}
/* The first argument is the filename of the GeoIPCity.dat file
* load_option = :standard, :index, or :memory. default :memory
* check_cache = true or false. default false
*
* filesystem: read database from filesystem, uses least memory.
*
* index: the most frequently accessed index portion of the database,
* resulting in faster lookups than :filesystem, but less memory usage than
* :memory.
*
* memory: load database into memory, faster performance but uses more
* memory.
*/
static VALUE rb_geoip_city_new(int argc, VALUE *argv, VALUE self)
{
return rb_geoip_database_new(mGeoIP_City, argc, argv, self);
}
/* Pass this function an IP address as a string, it will return a hash
* containing all the information that the database knows about the IP
* db.look_up('24.24.24.24')
* => {:city=>"Ithaca", :latitude=>42.4277992248535,
* :country_code=>"US", :longitude=>-76.4981994628906,
* :country_code3=>"USA", :dma_code=>555,
* :country_name=>"United States", :area_code=>607,
* :region=>"NY"}
*/
VALUE rb_geoip_city_look_up(VALUE self, VALUE addr) {
GeoIP *gi;
GeoIPRecord *record = NULL;
VALUE hash = Qnil;
Check_Type(addr, T_STRING);
Data_Get_Struct(self, GeoIP, gi);
if(record = GeoIP_record_by_addr(gi, StringValuePtr(addr))) {
hash = rb_city_record_to_hash(record);
GeoIPRecord_delete(record);
}
return hash;
}
/* GeoIP::Region ************************************************************/
VALUE rb_region_record_to_hash(GeoIPRegion *record)
{
VALUE hash = rb_hash_new();
int country_id;
const char * rn;
if(strlen(record->country_code)==0)
return Qnil;
rb_hash_sset(hash, "country_code", rb_str_new2(record->country_code));
country_id = GeoIP_id_by_code(record->country_code);
rb_hash_sset(hash, "country_code3", rb_str_new2(GeoIP_country_code3[country_id]));
rb_hash_sset(hash, "country_name", rb_str_new2(GeoIP_country_name[country_id]));
rb_hash_sset(hash, "continent", rb_str_new2(GeoIP_country_continent[country_id]));
if(record->region && strlen(record->region)>0) {
rb_hash_sset(hash, "region", rb_str_new2(record->region));
if(rn = GeoIP_region_name_by_code(record->country_code, record->region)) {
rb_hash_sset(hash, "region_name", rb_str_new2(rn));
}
}
return hash;
}
/* GeoIP::Region.new('/path/to/GeoIPRegion.dat')
* load_option is not required for this database because it is ignored.
*/
static VALUE rb_geoip_region_new(int argc, VALUE *argv, VALUE self)
{
return rb_geoip_database_new(mGeoIP_Region, argc, argv, self);
}
/* Pass this function an IP address as a string, it will return a hash
* containing all the information that the database knows about the IP
* db.look_up('24.24.24.24')
* => {:country_code=>"US",
* :region=>"NY",
* :region_name=>"New York"}
*/
VALUE rb_geoip_region_look_up(VALUE self, VALUE addr) {
GeoIP *gi;
GeoIPRegion *record = NULL;
VALUE hash = Qnil;
Check_Type(addr, T_STRING);
Data_Get_Struct(self, GeoIP, gi);
if(record = GeoIP_region_by_addr(gi, StringValuePtr(addr))) {
hash = rb_region_record_to_hash(record);
GeoIPRegion_delete(record);
}
return hash;
}
/* GeoIP::Country ************************************************************/
/* GeoIP::Country.new('/path/to/GeoIPCountry.dat')
* load_option is not required for this database because it is ignored.
*/
static VALUE rb_geoip_country_new(int argc, VALUE *argv, VALUE self)
{
return rb_geoip_database_new(mGeoIP_Country, argc, argv, self);
}
/* Pass this function an IP address as a string, it will return a hash
* containing all the information that the database knows about the IP
* db.look_up('24.24.24.24')
* => {:country_code=>"US",
* :country_code3=>"USA",
* :country_name=>"United States"}
*/
VALUE rb_geoip_country_look_up(VALUE self, VALUE addr) {
GeoIP *gi;
VALUE hash = Qnil;
int country_id;
Check_Type(addr, T_STRING);
Data_Get_Struct(self, GeoIP, gi);
country_id = GeoIP_id_by_addr(gi, StringValuePtr(addr));
if(country_id < 1) return Qnil;
hash = rb_hash_new();
rb_hash_sset(hash, "country_code", rb_str_new2(GeoIP_country_code[country_id]));
rb_hash_sset(hash, "country_code3", rb_str_new2(GeoIP_country_code3[country_id]));
rb_hash_sset(hash, "country_name", rb_str_new2(GeoIP_country_name[country_id]));
rb_hash_sset(hash, "continent", rb_str_new2(GeoIP_country_continent[country_id]));
return hash;
}
/* GeoIP::Country for IPv6 ****************************************************/
/* GeoIP::CountryV6.new('/path/to/GeoIPv6.dat')
*/
static VALUE rb_geoip_country_v6_new(int argc, VALUE *argv, VALUE self)
{
return rb_geoip_database_new(mGeoIP_CountryV6, argc, argv, self);
}
/* Pass this function an IPv6 address as a string, it will return a hash
* containing all the information that the database knows about the IP
* db.look_up('2001:470:0:76::2')
* => {:country_code=>"US",
* :country_code3=>"USA",
* :country_name=>"United States"}
*/
VALUE rb_geoip_country_v6_look_up(VALUE self, VALUE addr) {
GeoIP *gi;
VALUE hash = Qnil;
int country_id;
Check_Type(addr, T_STRING);
Data_Get_Struct(self, GeoIP, gi);
country_id = GeoIP_id_by_addr_v6(gi, StringValuePtr(addr));
if(country_id < 1) return Qnil;
hash = rb_hash_new();
rb_hash_sset(hash, "country_code", rb_str_new2(GeoIP_country_code[country_id]));
rb_hash_sset(hash, "country_code3", rb_str_new2(GeoIP_country_code3[country_id]));
rb_hash_sset(hash, "country_name", rb_str_new2(GeoIP_country_name[country_id]));
rb_hash_sset(hash, "continent", rb_str_new2(GeoIP_country_continent[country_id]));
return hash;
}
/* GeoIP::Organization *******************************************************/
/* GeoIP::Organization.new('/path/to/GeoIPOrg.dat', load_option)
* load_option can be:
* * :memory - load the data into memory, fastest (default)
* * :filesystem - look up from filesystem, least memory intensive
* * :index - stores in memory most recent queries
*
*/
static VALUE rb_geoip_org_new(int argc, VALUE *argv, VALUE self)
{
return rb_geoip_database_new(mGeoIP_Organization, argc, argv, self);
}
/* Pass this function an IP address as a string, it will return a hash
* containing all the information that the database knows about the IP:
* db.look_up('24.24.24.24')
* => {:name => "Road Runner"}
*/
VALUE rb_geoip_org_look_up(VALUE self, VALUE addr) {
GeoIP *gi;
Check_Type(addr, T_STRING);
Data_Get_Struct(self, GeoIP, gi);
return generic_single_value_lookup_response("name", GeoIP_name_by_addr(gi, StringValuePtr(addr)));
}
/* GeoIP::ISP *******************************************************/
/* GeoIP::ISP.new('/path/to/GeoIPISP.dat', load_option)
* load_option can be:
* * :memory - load the data into memory, fastest (default)
* * :filesystem - look up from filesystem, least memory intensive
* * :index - stores in memory most recent queries
*
*/
static VALUE rb_geoip_isp_new(int argc, VALUE *argv, VALUE self)
{
return rb_geoip_database_new(mGeoIP_ISP, argc, argv, self);
}
/* Pass this function an IP address as a string, it will return a hash
* containing all the information that the database knows about the IP:
* db.look_up('24.24.24.24')
* => {:isp => "Road Runner"}
*/
VALUE rb_geoip_isp_look_up(VALUE self, VALUE addr) {
GeoIP *gi;
Check_Type(addr, T_STRING);
Data_Get_Struct(self, GeoIP, gi);
return generic_single_value_lookup_response("isp", GeoIP_name_by_addr(gi, StringValuePtr(addr)));
}
/* GeoIP::NetSpeed *******************************************************/
/* GeoIP::NetSpeed.new('/path/to/GeoIPNetSpeed.dat', load_option)
* load_option can be:
* * :memory - load the data into memory, fastest (default)
* * :filesystem - look up from filesystem, least memory intensive
* * :index - stores in memory most recent queries
*
*/
static VALUE rb_geoip_netspeed_new(int argc, VALUE *argv, VALUE self)
{
return rb_geoip_database_new(mGeoIP_NetSpeed, argc, argv, self);
}
/* Pass this function an IP address as a string, it will return a hash
* containing all the information that the database knows about the IP:
* db.look_up('24.24.24.24')
* => {:netspeed => "Cable/DSL"}
*/
VALUE rb_geoip_netspeed_look_up(VALUE self, VALUE addr) {
GeoIP *gi;
Check_Type(addr, T_STRING);
Data_Get_Struct(self, GeoIP, gi);
return generic_single_value_lookup_response("netspeed", GeoIP_name_by_addr(gi, StringValuePtr(addr)));
}
/* GeoIP::Domain *******************************************************/
/* GeoIP::Domain.new('/path/to/GeoIPDomain.dat', load_option)
* load_option can be:
* * :memory - load the data into memory, fastest (default)
* * :filesystem - look up from filesystem, least memory intensive
* * :index - stores in memory most recent queries
*
*/
static VALUE rb_geoip_domain_new(int argc, VALUE *argv, VALUE self)
{
return rb_geoip_database_new(mGeoIP_Domain, argc, argv, self);
}
/* Pass this function an IP address as a string, it will return a hash
* containing all the information that the database knows about the IP:
* db.look_up('24.24.24.24')
* => {:domain => "rr.com"}
*/
VALUE rb_geoip_domain_look_up(VALUE self, VALUE addr) {
GeoIP *gi;
Check_Type(addr, T_STRING);
Data_Get_Struct(self, GeoIP, gi);
return generic_single_value_lookup_response("domain", GeoIP_name_by_addr(gi, StringValuePtr(addr)));
}
/* GeoIP *********************************************************************/
/* Returns the numeric form of an IP address.
*
* For example:
* 24.24.24.24 => 404232216
*
* This is used in order to be able to perform searches in CSV versions of the
* data files or in SQL records if the data has been put there.
*/
VALUE rb_geoip_addr_to_num(VALUE self, VALUE addr) {
Check_Type(addr, T_STRING);
return UINT2NUM((unsigned int)_GeoIP_addr_to_num(StringValuePtr(addr)));
}
// Fixes a bug with 64bit architectures where this delcaration doesn't exist
// in the GeoIP library causing segmentation faults.
char *_GeoIP_num_to_addr(GeoIP* gi, unsigned long ipnum);
VALUE rb_geoip_num_to_addr(VALUE self, VALUE num) {
VALUE num_type = TYPE(num);
switch(num_type) {
case T_FIXNUM: break;
case T_BIGNUM: break;
default: rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or Bignum)", rb_obj_classname(num));
}
return rb_str_new2((char*)_GeoIP_num_to_addr(NULL, (unsigned long)NUM2ULONG(num)));
}
void Init_geoip()
{
mGeoIP = rb_define_module("GeoIP");
rb_geoip_memory = ID2SYM(rb_intern("memory"));
rb_geoip_filesystem = ID2SYM(rb_intern("filesystem"));
rb_geoip_index = ID2SYM(rb_intern("index"));
mGeoIP_City = rb_define_class_under(mGeoIP, "City", rb_cObject);
rb_define_singleton_method(mGeoIP_City, "new", rb_geoip_city_new, -1);
rb_define_method( mGeoIP_City, "look_up", rb_geoip_city_look_up, 1);
mGeoIP_Region = rb_define_class_under(mGeoIP, "Region", rb_cObject);
rb_define_singleton_method(mGeoIP_Region, "new", rb_geoip_region_new, -1);
rb_define_method( mGeoIP_Region, "look_up", rb_geoip_region_look_up, 1);
mGeoIP_Country = rb_define_class_under(mGeoIP, "Country", rb_cObject);
rb_define_singleton_method(mGeoIP_Country, "new", rb_geoip_country_new, -1);
rb_define_method( mGeoIP_Country, "look_up", rb_geoip_country_look_up, 1);
mGeoIP_CountryV6 = rb_define_class_under(mGeoIP, "CountryV6", rb_cObject);
rb_define_singleton_method(mGeoIP_CountryV6, "new", rb_geoip_country_v6_new, -1);
rb_define_method( mGeoIP_CountryV6, "look_up", rb_geoip_country_v6_look_up, 1);
mGeoIP_Organization = rb_define_class_under(mGeoIP, "Organization", rb_cObject);
rb_define_singleton_method(mGeoIP_Organization, "new", rb_geoip_org_new, -1);
rb_define_method( mGeoIP_Organization, "look_up", rb_geoip_org_look_up, 1);
mGeoIP_ISP = rb_define_class_under(mGeoIP, "ISP", rb_cObject);
rb_define_singleton_method(mGeoIP_ISP, "new", rb_geoip_isp_new, -1);
rb_define_method( mGeoIP_ISP, "look_up", rb_geoip_isp_look_up, 1);
mGeoIP_NetSpeed = rb_define_class_under(mGeoIP, "NetSpeed", rb_cObject);
rb_define_singleton_method(mGeoIP_NetSpeed, "new", rb_geoip_netspeed_new, -1);
rb_define_method( mGeoIP_NetSpeed, "look_up", rb_geoip_netspeed_look_up, 1);
mGeoIP_Domain = rb_define_class_under(mGeoIP, "Domain", rb_cObject);
rb_define_singleton_method(mGeoIP_Domain, "new", rb_geoip_domain_new, -1);
rb_define_method( mGeoIP_Domain, "look_up", rb_geoip_domain_look_up, 1);
rb_define_singleton_method(mGeoIP, "addr_to_num", rb_geoip_addr_to_num, 1);
rb_define_singleton_method(mGeoIP, "num_to_addr", rb_geoip_num_to_addr, 1);
}