lib/ipaddress.rb in ipaddress-0.8.0 vs lib/ipaddress.rb in ipaddress-0.8.2
- old
+ new
@@ -12,10 +12,11 @@
#
#++
require 'ipaddress/ipv4'
require 'ipaddress/ipv6'
+require 'ipaddress/mongoid' if defined?(Mongoid)
module IPAddress
NAME = "IPAddress"
GEM = "ipaddress"
@@ -23,10 +24,11 @@
#
# 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
@@ -38,19 +40,47 @@
# #=> IPAddress::IPv6
# ip_mapped.class
# #=> IPAddress::IPv6::Mapped
#
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
- IPAddress::IPv4.new(str) rescue IPAddress::IPv6.new(str)
+ raise ArgumentError, "Unknown IP Address #{str}"
end
end
#
+ # Converts a unit32 to IPv4
+ #
+ # IPAddress::ntoa(167837953)
+ # #-> "10.1.1.1"
+ #
+ 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
+
+ #
# True if the object is an IPv4 address
#
# ip = IPAddress("192.168.10.100/24")
#
# ip.ipv4?
@@ -129,18 +159,13 @@
# #=> true
#
# IPAddress::valid_ipv6? "2002::DEAD::BEEF"
# #=> false
#
- def self.valid_ipv6?(addr)
- # IPv6 (normal)
- return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*\Z/ =~ addr
- return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr
- return true if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr
- # IPv6 (IPv4 compat)
- return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:/ =~ addr && valid_ipv4?($')
- return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_ipv4?($')
- return true if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_ipv4?($')
+ 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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