geoip-c 0.6.3 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.md +1 -0
  2. data/Rakefile +2 -2
  3. data/geoip.c +44 -58
  4. data/test.rb +1 -1
  5. metadata +2 -1
data/README.md CHANGED
@@ -140,6 +140,7 @@ Many thanks to our contributors:
140
140
 
141
141
  * Charles Brian Quinn
142
142
  * Michael Sheakoski
143
+ * Silvio Quadri
143
144
 
144
145
  License
145
146
  -------
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.6.3"
25
+ s.version = "0.7.0"
26
26
 
27
- s.authors = ['Ryah Dahl', 'Matt Todd', 'Charles Brian Quinn', 'Michael Sheakoski']
27
+ s.authors = ['Ryah Dahl', 'Matt Todd', 'Charles Brian Quinn', 'Michael Sheakoski', 'Silvio Quadri']
28
28
  s.email = 'mtodd@highgroove.com'
29
29
 
30
30
  s.summary = "A Binding to the GeoIP C library"
data/geoip.c CHANGED
@@ -11,6 +11,7 @@
11
11
 
12
12
  static VALUE mGeoIP;
13
13
  static VALUE mGeoIP_City;
14
+ static VALUE mGeoIP_Country;
14
15
  static VALUE mGeoIP_Organization;
15
16
  static VALUE mGeoIP_ISP;
16
17
  static VALUE mGeoIP_NetSpeed;
@@ -148,6 +149,41 @@ VALUE rb_geoip_city_look_up(VALUE self, VALUE addr) {
148
149
  return hash;
149
150
  }
150
151
 
152
+ /* GeoIP::Country ************************************************************/
153
+
154
+ /* GeoIP::Country.new('/path/to/GeoIPCountry.dat')
155
+ * load_option is not required for this database because it is ignored.
156
+ */
157
+ static VALUE rb_geoip_country_new(int argc, VALUE *argv, VALUE self)
158
+ {
159
+ return rb_geoip_database_new(mGeoIP_Country, argc, argv, self);
160
+ }
161
+
162
+ /* Pass this function an IP address as a string, it will return a hash
163
+ * containing all the information that the database knows about the IP
164
+ * db.look_up('24.24.24.24')
165
+ * => {:country_code=>"US",
166
+ * :country_code3=>"USA",
167
+ * :country_name=>"United States"}
168
+ */
169
+ VALUE rb_geoip_country_look_up(VALUE self, VALUE addr) {
170
+ GeoIP *gi;
171
+ VALUE hash = Qnil;
172
+ int country_id;
173
+
174
+ Check_Type(addr, T_STRING);
175
+ Data_Get_Struct(self, GeoIP, gi);
176
+ country_id = GeoIP_id_by_addr(gi, STR2CSTR(addr));
177
+ if(country_id < 1) return Qnil;
178
+
179
+ hash = rb_hash_new();
180
+ rb_hash_sset(hash, "country_code", rb_str_new2(GeoIP_country_code[country_id]));
181
+ rb_hash_sset(hash, "country_code3", rb_str_new2(GeoIP_country_code3[country_id]));
182
+ rb_hash_sset(hash, "country_name", rb_str_new2(GeoIP_country_name[country_id]));
183
+
184
+ return hash;
185
+ }
186
+
151
187
  /* GeoIP::Organization *******************************************************/
152
188
 
153
189
  /* GeoIP::Organization.new('/path/to/GeoIPOrg.dat', load_option)
@@ -267,62 +303,9 @@ VALUE rb_geoip_addr_to_num(VALUE self, VALUE addr) {
267
303
  return UINT2NUM((unsigned int)_GeoIP_addr_to_num(STR2CSTR(addr)));
268
304
  }
269
305
 
270
- /* This returns a pointer to a character array that represents the IP string of
271
- * the IP Number given it.
272
- *
273
- * This was originally a slightly patched version of the function found in the
274
- * GeoIP library called _GeoIP_num_to_addr with the same declaration but
275
- * instead a slight variation has been used here.
276
- *
277
- * The function definition this replaces follows:
278
- * char *ret_str;
279
- * char *cur_str;
280
- * int octet[4];
281
- * int num_chars_written, i;
282
- *
283
- * ret_str = malloc(sizeof(char) * 16);
284
- * memset(ret_str, '\0', sizeof(char) * 16); // ensure we null-terminate the string
285
- * cur_str = ret_str;
286
- *
287
- * for (i = 0; i<4; i++) {
288
- * octet[3 - i] = ipnum % 256;
289
- * ipnum >>= 8;
290
- * }
291
- *
292
- * for (i = 0; i<4; i++) {
293
- * num_chars_written = sprintf(cur_str, "%d", octet[i]);
294
- * cur_str += num_chars_written;
295
- *
296
- * if (i < 3) {
297
- * cur_str[0] = '.';
298
- * cur_str++;
299
- * }
300
- * }
301
- *
302
- * return ret_str;
303
- *
304
- * I make no assertions about speed or efficiency here. There is no error
305
- * handling (in case of malloc failing), also.
306
- *
307
- * However, this works on 64bit platforms, where the previous implementation
308
- * did not.
309
- */
310
- char *_patched_GeoIP_num_to_addr(GeoIP* gi, unsigned long ipnum) {
311
- char *ip;
312
- int i, octet[4];
313
-
314
- ip = malloc(sizeof(char) * 16);
315
- memset(ip, '\0', sizeof(char) * 16);
316
-
317
- for(i = 0; i < 4; i++)
318
- {
319
- octet[i] = (ipnum >> (i*8)) & 0xFF;
320
- }
321
-
322
- sprintf(ip, "%i.%i.%i.%i", octet[3], octet[2], octet[1], octet[0]);
323
-
324
- return ip;
325
- }
306
+ // Fixes a bug with 64bit architectures where this delcaration doesn't exist
307
+ // in the GeoIP library causing segmentation faults.
308
+ char *_GeoIP_num_to_addr(GeoIP* gi, unsigned long ipnum);
326
309
 
327
310
  VALUE rb_geoip_num_to_addr(VALUE self, VALUE num) {
328
311
  VALUE num_type = TYPE(num);
@@ -331,8 +314,7 @@ VALUE rb_geoip_num_to_addr(VALUE self, VALUE num) {
331
314
  case T_BIGNUM: break;
332
315
  default: rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or Bignum)", rb_obj_classname(num));
333
316
  }
334
- // return rb_str_new2((char*)_GeoIP_num_to_addr(NULL, (unsigned long)NUM2ULONG(num)));
335
- return rb_str_new2((char*)_patched_GeoIP_num_to_addr(NULL, (unsigned long)NUM2ULONG(num)));
317
+ return rb_str_new2((char*)_GeoIP_num_to_addr(NULL, (unsigned long)NUM2ULONG(num)));
336
318
  }
337
319
 
338
320
  void Init_geoip()
@@ -347,6 +329,10 @@ void Init_geoip()
347
329
  rb_define_singleton_method(mGeoIP_City, "new", rb_geoip_city_new, -1);
348
330
  rb_define_method( mGeoIP_City, "look_up", rb_geoip_city_look_up, 1);
349
331
 
332
+ mGeoIP_Country = rb_define_class_under(mGeoIP, "Country", rb_cObject);
333
+ rb_define_singleton_method(mGeoIP_Country, "new", rb_geoip_country_new, -1);
334
+ rb_define_method( mGeoIP_Country, "look_up", rb_geoip_country_look_up, 1);
335
+
350
336
  mGeoIP_Organization = rb_define_class_under(mGeoIP, "Organization", rb_cObject);
351
337
  rb_define_singleton_method(mGeoIP_Organization, "new", rb_geoip_org_new, -1);
352
338
  rb_define_method( mGeoIP_Organization, "look_up", rb_geoip_org_look_up, 1);
data/test.rb CHANGED
@@ -51,7 +51,7 @@ class GeoIPTest < Test::Unit::TestCase
51
51
  end
52
52
 
53
53
  def test_num_to_addr_converts_large_ipnums_to_an_ip_correctly
54
- # assert_equal @large_ip, GeoIP.num_to_addr(@large_ipnum)
54
+ assert_equal @large_ip, GeoIP.num_to_addr(@large_ipnum)
55
55
  end
56
56
 
57
57
  def test_num_to_addr_expects_a_numeric_ip
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geoip-c
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryah Dahl
8
8
  - Matt Todd
9
9
  - Charles Brian Quinn
10
10
  - Michael Sheakoski
11
+ - Silvio Quadri
11
12
  autorequire:
12
13
  bindir: bin
13
14
  cert_chain: []