summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarco Ceresa <ceresa@gmail.com>2015-02-02 14:44:12 +0100
committerMarco Ceresa <ceresa@gmail.com>2015-02-02 14:44:12 +0100
commit52234fe47f8682c94119930771eebb00acfe1f90 (patch)
tree8a13b779af6b110d10df9b3bd42de990325dd2cf
parentf2080750a409c90360db24653436f1d1d026fcd3 (diff)
parentf90b36f2da80070411131af985c2af9f236a1685 (diff)
downloadipaddress-52234fe47f8682c94119930771eebb00acfe1f90.tar.gz
Merge pull request #57 from mikemackintosh/uint32-parse
added #ntoa unint32 -> ip and tests
-rw-r--r--README.rdoc11
-rw-r--r--lib/ipaddress.rb25
-rw-r--r--test/ipaddress_test.rb28
-rw-r--r--test/test_helper.rb1
4 files changed, 63 insertions, 2 deletions
diff --git a/README.rdoc b/README.rdoc
index 9f28191..b74c9b0 100644
--- a/README.rdoc
+++ b/README.rdoc
@@ -89,7 +89,7 @@ or, in a easier way, using the IPAddress parse method
ip = IPAddress.parse "172.16.10.1/24"
-which accepts and parses any kind of IP (IPv4, IPV6 and
+which accepts and parses any kind of IP (uint32, IPv4, IPV6 and
IPv4 IPv6 Mapped addresses).
If you like syntactic sugar, you can use the wrapper method
@@ -120,7 +120,14 @@ The new created object has prefix /32, which is the same
as we created the following:
host = IPAddress::IPv4.new "10.1.1.1/32"
-
+
+You can also pass a uint32 to obtain an IPAddress::IPv4 object:
+
+ # Create host object
+ ip = IPAddress 167837953
+ puts ip.to_string
+ #=> "10.1.1.1/32"
+
=== Handling the IPv4 address
Once created, you can obtain the attributes for an IPv4 object:
diff --git a/lib/ipaddress.rb b/lib/ipaddress.rb
index e2161e1..4033c3a 100644
--- a/lib/ipaddress.rb
+++ b/lib/ipaddress.rb
@@ -26,6 +26,7 @@ module IPAddress
# 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"
@@ -41,6 +42,12 @@ module IPAddress
# #=> 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)
@@ -54,6 +61,24 @@ module IPAddress
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)
+ 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")
diff --git a/test/ipaddress_test.rb b/test/ipaddress_test.rb
index 8e2bb66..c8bf2e7 100644
--- a/test/ipaddress_test.rb
+++ b/test/ipaddress_test.rb
@@ -11,9 +11,28 @@ class IPAddressTest < Test::Unit::TestCase
@invalid_ipv6 = ":1:2:3:4:5:6:7"
@invalid_mapped = "::1:2.3.4"
+ @valid_ipv4_uint32 = [4294967295, # 255.255.255.255
+ 167772160, # 10.0.0.0
+ 3232235520, # 192.168.0.0
+ 0]
+
+ @invalid_ipv4_uint32 = [4294967296, # 256.0.0.0
+ "A294967295", # Invalid uINT
+ -1] # Invalid
+
+
@ipv4class = IPAddress::IPv4
@ipv6class = IPAddress::IPv6
@mappedclass = IPAddress::IPv6::Mapped
+
+ @invalid_ipv4 = ["10.0.0.256",
+ "10.0.0.0.0",
+ "10.0.0",
+ "10.0"]
+
+ @valid_ipv4_range = ["10.0.0.1-254",
+ "10.0.1-254.0",
+ "10.1-254.0.0"]
@method = Module.method("IPAddress")
end
@@ -31,6 +50,15 @@ class IPAddressTest < Test::Unit::TestCase
assert_raise(ArgumentError) {@method.call(@invalid_ipv6)}
assert_raise(ArgumentError) {@method.call(@invalid_mapped)}
+ assert_instance_of @ipv4class, @method.call(@valid_ipv4_uint32[0])
+ assert_instance_of @ipv4class, @method.call(@valid_ipv4_uint32[1])
+ assert_instance_of @ipv4class, @method.call(@valid_ipv4_uint32[2])
+ assert_instance_of @ipv4class, @method.call(@valid_ipv4_uint32[3])
+
+ assert_raise(ArgumentError) {@method.call(@invalid_ipv4_uint32[0])}
+ assert_raise(ArgumentError) {@method.call(@invalid_ipv4_uint32[1])}
+ assert_raise(ArgumentError) {@method.call(@invalid_ipv4_uint32[2])}
+
end
def test_module_method_valid?
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 61e381e..99fd5e9 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -1,4 +1,5 @@
require 'rubygems'
+require 'minitest/autorun'
require 'test/unit'
$LOAD_PATH.unshift(File.dirname(__FILE__))