diff options
author | eahlstrom <erik@negahok.se> | 2010-12-16 00:00:41 +0100 |
---|---|---|
committer | eahlstrom <erik@negahok.se> | 2011-02-24 21:13:15 +0100 |
commit | a8ab168210bc294a442ba90e881175fb1308a093 (patch) | |
tree | 862ab27650a052b990ec918892e3d6a34ab9eed4 /lib | |
parent | 120a5093a115c730bb6941285f32b5b20eeb6b93 (diff) | |
download | ipaddress-a8ab168210bc294a442ba90e881175fb1308a093.tar.gz |
bugfix: summarizing a small net with a large outside it's supernet raised error
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ipaddress/ipv4.rb | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb index dba33ac..bdfba97 100644 --- a/lib/ipaddress/ipv4.rb +++ b/lib/ipaddress/ipv4.rb @@ -538,6 +538,13 @@ module IPAddress; end # + # Checks that all other IPAddress::IPv4 is included in this object + # + def include_all?(*others) + others.find_all{|oth| include?(oth)}.length == others.length + end + + # # Checks if an IPv4 address objects belongs # to a private network RFC1918 # @@ -943,20 +950,15 @@ module IPAddress; end def aggregate(ip1,ip2) - if ip1.include? ip2 - return [ip1] + return [ip1] if ip1.include? ip2 + + snet = ip1.supernet(ip1.prefix-1) + if snet.include_all?(ip1, ip2) && ((ip1.size + ip2.size) == snet.size) + return [snet] else - snet = ip1.supernet(ip1.prefix-1) - arr1 = ip1.subnet(2**(ip2.prefix-ip1.prefix)).map{|i| i.to_string} - arr2 = snet.subnet(2**(ip2.prefix-snet.prefix)).map{|i| i.to_string} - if (arr2 - [ip2.to_string] - arr1).empty? - return [snet] - else - return [ip1, ip2] - end + return [ip1, ip2] end end - end # class IPv4 end # module IPAddress |