Method: GeoIP#each_by_ip

Defined in:
lib/geoip.rb

#each_by_ip(offset = 0, ipnum = 0, mask = nil, &callback) ⇒ Object

Call like this, for example: GeoIP.new(‘GeoIPNetSpeedCell.dat’).each{|*a| puts(“0x%08Xt%d” % a)} or: GeoIP.new(‘GeoIPv6.dat’).each{|*a| puts(“0x%032Xt%d” % a)}



479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/geoip.rb', line 479

def each_by_ip offset = 0, ipnum = 0, mask = nil, &callback
  mask ||= 1 << (@ip_bits-1)

  # Read the two pointers and split them:
  record2 = atomic_read(@record_length*2, @record_length*2*offset)
  record1 = record2.slice!(0, @record_length)

  # Traverse the left tree
  off1 = le_to_ui(record1.unpack('C*'))
  val = off1 - @database_segments[0]
  if val >= 0
    yield(ipnum, val > 0 ? read_record(ipnum.to_s, ipnum, val) : nil) 
  elsif mask != 0
    each_by_ip(off1, ipnum, mask >> 1, &callback)
  end

  # Traverse the right tree
  off2 = le_to_ui(record2.unpack('C*'))
  val = off2 - @database_segments[0]
  if val >= 0
    yield(ipnum|mask, val > 0 ? read_record(ipnum.to_s, ipnum, val) : nil)
  elsif mask != 0
    each_by_ip(off2, ipnum|mask, mask >> 1, &callback)
  end
end