diff options
author | bluemonk <ceresa@gmail.com> | 2011-05-10 22:04:13 +0200 |
---|---|---|
committer | bluemonk <ceresa@gmail.com> | 2011-05-10 22:04:13 +0200 |
commit | 05d1cc71bfa12bfdfe1bf61afed1b0fc78449531 (patch) | |
tree | 0cc6f1a423ffb6001a5228afcd9c338a9199f510 /lib | |
parent | dba60ba1b34db3d817ef20158e2cb2d7e2e3f34d (diff) | |
download | ipaddress-05d1cc71bfa12bfdfe1bf61afed1b0fc78449531.tar.gz |
Added IPv6#each, IPv6#broadcast_u128 and Prefix128#host_prefix
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ipaddress/ipv4.rb | 24 | ||||
-rw-r--r-- | lib/ipaddress/ipv6.rb | 62 | ||||
-rw-r--r-- | lib/ipaddress/prefix.rb | 13 |
3 files changed, 74 insertions, 25 deletions
diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb index 2a0ca65..d895cff 100644 --- a/lib/ipaddress/ipv4.rb +++ b/lib/ipaddress/ipv4.rb @@ -553,30 +553,6 @@ module IPAddress; end # - # True if the object is an IPv4 address - # - # ip = IPAddress("192.168.10.100/24") - # - # ip.ipv4? - # #-> true - # -# def ipv4? -# true -# end - - # - # True if the object is an IPv6 address - # - # ip = IPAddress("192.168.10.100/24") - # - # ip.ipv6? - # #-> false - # -# def ipv6? -# false -# end - - # # Checks if an IPv4 address objects belongs # to a private network RFC1918 # diff --git a/lib/ipaddress/ipv6.rb b/lib/ipaddress/ipv6.rb index c0823ea..eb4b13c 100644 --- a/lib/ipaddress/ipv6.rb +++ b/lib/ipaddress/ipv6.rb @@ -329,6 +329,36 @@ module IPAddress; end # + # Returns the broadcast address in Unsigned 128bits format + # + # ip6 = IPAddress "2001:db8::8:800:200c:417a/64" + # + # ip6.broadcast_u128 + # #=> 42540766411282592875350729025363378175 + # + # Please note that there is no Broadcast concept in IPv6 + # addresses as in IPv4 addresses, and this method is just + # an helper to other functions. + # + def broadcast_u128 + network_u128 + size - 1 + end + + # + # Returns the number of IP addresses included + # in the network. It also counts the network + # address and the broadcast address. + # + # ip6 = IPAddress("2001:db8::8:800:200c:417a/64") + # + # ip6.size + # #=> 18446744073709551616 + # + def size + 2 ** @prefix.host_prefix + end + + # # Checks whether a subnet includes the given IP address. # # Example: @@ -384,7 +414,37 @@ module IPAddress; def mapped? to_u128 >> 32 == 0xffff end - + + # + # Iterates over all the IP addresses for the given + # network (or IP address). + # + # The object yielded is a new IPv6 object created + # from the iteration. + # + # ip6 = IPAddress("2001:db8::4/125") + # + # ip6.each do |i| + # p i.compressed + # end + # #=> "2001:db8::" + # #=> "2001:db8::1" + # #=> "2001:db8::2" + # #=> "2001:db8::3" + # #=> "2001:db8::4" + # #=> "2001:db8::5" + # #=> "2001:db8::6" + # #=> "2001:db8::7" + # + # WARNING: if the host portion is very large, this method + # can be very slow and possibly hang your system! + # + def each + (network_u128..broadcast_u128).each do |i| + yield self.class.parse_u128(i, @prefix) + end + end + # # Returns the address portion of an IP in binary format, # as a string containing a sequence of 0 and 1 diff --git a/lib/ipaddress/prefix.rb b/lib/ipaddress/prefix.rb index aac2090..688b962 100644 --- a/lib/ipaddress/prefix.rb +++ b/lib/ipaddress/prefix.rb @@ -247,6 +247,19 @@ module IPAddress bits.to_i(2) end + # + # Returns the length of the host portion + # of a netmask. + # + # prefix = Prefix128.new 96 + # + # prefix.host_prefix + # #=> 32 + # + def host_prefix + 128 - @prefix + end + end # class Prefix123 < Prefix end # module IPAddress |