Module: IPAddr::IPAddrExtensions::ClassMethods

Defined in:
lib/libmatty/ipaddr.rb

Instance Method Summary collapse

Instance Method Details

#inet_addr(str) ⇒ Object

construct an IPAddr from a dotted quad string or integer



29
30
31
32
33
34
35
36
37
# File 'lib/libmatty/ipaddr.rb', line 29

def inet_addr(str)
  if str.kind_of? String
    IPAddr.lite(str).to_i
  else
    i = IPAddr.new(0, Socket::AF_INET)
    i.set_int(str)
    return i
  end
end

#lite(str) ⇒ Object

construct an IPAddr from a dotted quad string without incurring a reverse lookup.



41
42
43
44
45
46
47
48
49
# File 'lib/libmatty/ipaddr.rb', line 41

def lite(str)
  ip = IPAddr.new(0, Socket::AF_INET)

  parts = str.split "/"

  ip.set_int(ip.inet_addr(parts[0]))
  ip = ip.mask parts[1] if parts[1]
  return ip
end

#mask2mlen(mask) ⇒ Object

Convert 255.255.255.0 to 24



52
53
54
55
56
57
58
59
60
# File 'lib/libmatty/ipaddr.rb', line 52

def mask2mlen(mask)
  len = 0
  while len < 32 && mask & 0x80000000 != 0
    mask <<= 1
    mask &= 0xFFFFFFFF
    len += 1
  end
  return len
end