diff options
author | Michael Rodrigues <mikebrodrigues@gmail.com> | 2014-07-01 19:27:58 -0700 |
---|---|---|
committer | Michael Rodrigues <mikebrodrigues@gmail.com> | 2014-07-01 19:27:58 -0700 |
commit | b085273a41659b5c919b78178066a1c7fcce19e6 (patch) | |
tree | ea93cc30baf692bec3a2d59346d9a2e937ed1f8d /lib/ipaddress/ipv4.rb | |
parent | c2704ae93df103bbd14a2daddd8a3fe5b2f9cf4b (diff) | |
download | ipaddress-b085273a41659b5c919b78178066a1c7fcce19e6.tar.gz |
#first, #last, #network, and #broadcast properly handle /31 (RFC3021) and /32
Diffstat (limited to 'lib/ipaddress/ipv4.rb')
-rw-r--r-- | lib/ipaddress/ipv4.rb | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb index a798a24..d68dfa9 100644 --- a/lib/ipaddress/ipv4.rb +++ b/lib/ipaddress/ipv4.rb @@ -294,9 +294,16 @@ module IPAddress; # #=> "172.16.10.255" # def broadcast - self.class.parse_u32(broadcast_u32, @prefix) + case + when prefix <= 30 + self.class.parse_u32(broadcast_u32, @prefix) + when prefix == 31 + self.class.parse_u32(-1, @prefix) + when prefix == 32 + return nil + end end - + # # Checks if the IP address is actually a network # @@ -348,7 +355,14 @@ module IPAddress; # #=> "192.168.100.1" # def first - self.class.parse_u32(network_u32+1, @prefix) + case + when prefix <= 30 + self.class.parse_u32(network_u32+1, @prefix) + when prefix == 31 + self.class.parse_u32(network_u32, @prefix) + when prefix == 32 + return self + end end # @@ -373,7 +387,14 @@ module IPAddress; # #=> "192.168.100.254" # def last - self.class.parse_u32(broadcast_u32-1, @prefix) + case + when prefix <= 30 + self.class.parse_u32(broadcast_u32-1, @prefix) + when prefix == 31 + self.class.parse_u32(broadcast_u32, @prefix) + when prefix == 32 + return self + end end # @@ -461,7 +482,7 @@ module IPAddress; return prefix <=> oth.prefix if to_u32 == oth.to_u32 to_u32 <=> oth.to_u32 end - + # # Returns the number of IP addresses included # in the network. It also counts the network |