module IPAddress

Constants

AUTHORS
GEM
NAME

Public Class Methods

demongoize(string) click to toggle source

Mongoid field deserialization

# File lib/ipaddress/mongoid.rb, line 55
def self.demongoize(string)
  parse(string)
rescue ArgumentError
  nil
end
evolve(ipaddress) click to toggle source

Delegates to IPAddress.mongoize

# File lib/ipaddress/mongoid.rb, line 64
def self.evolve(ipaddress)
  mongoize(ipaddress)
end
mongoize(ipaddress) click to toggle source

Mongoid field serialization

IPAddress objects are converted to String

IPAddress.mongoize IPAddress.parse("172.16.10.1")
  #=> "172.16.10.1"

Prefix will be removed from host adresses

IPAddress.mongoize "172.16.10.1/32"
  #=> "172.16.10.1"

Prefix will be kept for network addresses

IPAddress.mongoize "172.16.10.1/24"
  #=> "172.16.10.1/24"

IPv6 addresses will be stored uncompressed to ease DB search and sorting

IPAddress.mongoize "2001:db8::8:800:200c:417a"
  #=> "2001:0db8:0000:0000:0008:0800:200c:417a"
IPAddress.mongoize "2001:db8::8:800:200c:417a/64"
  #=> "2001:0db8:0000:0000:0008:0800:200c:417a/64"

Invalid addresses will be serialized as nil

IPAddress.mongoize "invalid"
  #=> nil
IPAddress.mongoize ""
  #=> nil
IPAddress.mongoize 1
  #=> nil
IPAddress.mongoize nil
  #=> nil
# File lib/ipaddress/mongoid.rb, line 39
def self.mongoize(ipaddress)
  ipaddress = self.parse(ipaddress) unless ipaddress.is_a?(IPAddress)
  if ipaddress.bits.length == ipaddress.prefix
    ipaddress.address
  elsif ipaddress.is_a?(IPAddress::IPv6)
    ipaddress.to_string_uncompressed
  else
    ipaddress.to_string
  end
rescue ArgumentError
  nil
end
ntoa(uint) click to toggle source

Converts a unit32 to IPv4

IPAddress::ntoa(167837953)
  #-> "10.1.1.1"
# File lib/ipaddress.rb, line 69
def self.ntoa(uint)
  unless(uint.is_a? Numeric and uint <= 0xffffffff and uint >= 0)
      raise(::ArgumentError, "not a long integer: #{uint.inspect}")
    end
    ret = []
    4.times do 
      ret.unshift(uint & 0xff)
      uint >>= 8
    end
    ret.join('.')
end
parse(str) click to toggle source

Parse the argument string to create a new IPv4, IPv6 or Mapped IP object

ip  = IPAddress.parse 167837953 # 10.1.1.1  
ip  = IPAddress.parse "172.16.10.1/24"
ip6 = IPAddress.parse "2001:db8::8:800:200c:417a/64"
ip_mapped = IPAddress.parse "::ffff:172.16.10.1/128"

All the object created will be instances of the correct class:

ip.class
  #=> IPAddress::IPv4
ip6.class
  #=> IPAddress::IPv6
ip_mapped.class
  #=> IPAddress::IPv6::Mapped
# File lib/ipaddress.rb, line 44
def IPAddress::parse(str)
  
  # Check if an int was passed
  if str.kind_of? Integer
    return IPAddress::IPv4.new(ntoa(str))  
  end

  case str
  when /:.+\./
    IPAddress::IPv6::Mapped.new(str)
  when /\./
    IPAddress::IPv4.new(str) 
  when /:/
    IPAddress::IPv6.new(str)
  else
    raise ArgumentError, "Unknown IP Address #{str}"
  end
end
valid?(addr) click to toggle source

Checks if the given string is a valid IP address, either IPv4 or IPv6

Example:

IPAddress::valid? "2002::1"
  #=> true

IPAddress::valid? "10.0.0.256"   
  #=> false
# File lib/ipaddress.rb, line 117
def self.valid?(addr)
  valid_ipv4?(addr) || valid_ipv6?(addr)
end
valid_ipv4?(addr) click to toggle source

Checks if the given string is a valid IPv4 address

Example:

IPAddress::valid_ipv4? "2002::1"
  #=> false

IPAddress::valid_ipv4? "172.16.10.1"
  #=> true
# File lib/ipaddress.rb, line 132
def self.valid_ipv4?(addr)
  if /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/ =~ addr
    return $~.captures.all? {|i| i.to_i < 256}
  end
  false
end
valid_ipv4_netmask?(addr) click to toggle source

Checks if the argument is a valid IPv4 netmask expressed in dotted decimal format.

IPAddress.valid_ipv4_netmask? "255.255.0.0"
  #=> true
# File lib/ipaddress.rb, line 146
def self.valid_ipv4_netmask?(addr)
  arr = addr.split(".").map{|i| i.to_i}.pack("CCCC").unpack("B*").first.scan(/01/)
  arr.empty? && valid_ipv4?(addr)
rescue
  return false
end
valid_ipv6?(addr) click to toggle source

Checks if the given string is a valid IPv6 address

Example:

IPAddress::valid_ipv6? "2002::1"
  #=> true

IPAddress::valid_ipv6? "2002::DEAD::BEEF"
  #=> false
# File lib/ipaddress.rb, line 164
def self.valid_ipv6?(addr) 
  # https://gist.github.com/cpetschnig/294476
  # http://forums.intermapper.com/viewtopic.php?t=452
  return true if /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/ =~ addr
  false
end

Public Instance Methods

ipv4?() click to toggle source

True if the object is an IPv4 address

ip = IPAddress("192.168.10.100/24")

ip.ipv4?
  #-> true
# File lib/ipaddress.rb, line 89
def ipv4?
  self.kind_of? IPAddress::IPv4
end
ipv6?() click to toggle source

True if the object is an IPv6 address

ip = IPAddress("192.168.10.100/24")

ip.ipv6?
  #-> false
# File lib/ipaddress.rb, line 101
def ipv6?
  self.kind_of? IPAddress::IPv6
end
mongoize() click to toggle source

Sends self object to IPAddress#mongoize

# File lib/ipaddress/mongoid.rb, line 71
def mongoize
  IPAddress.mongoize(self)
end