tom

Merge remote-tracking branch 'origin/master'

......@@ -234,6 +234,30 @@ public final class IpAddress {
return mask() != 0;
}
/**
* Determines whether a given address is contained within this IpAddress'
* network.
*
* @param other another IP address that could be contained in this network
* @return true if the other IP address is contained in this address'
* network, otherwise false
*/
public boolean contains(IpAddress other) {
if (this.netmask <= other.netmask) {
// Special case where they're both /32 addresses
if (this.netmask == MAX_INET_MASK) {
return Arrays.equals(octets, other.octets);
}
// Mask the other address with our network mask
IpAddress otherMasked =
IpAddress.valueOf(other.octets, netmask).network();
return network().equals(otherMasked);
}
return false;
}
@Override
public int hashCode() {
final int prime = 31;
......
package org.onlab.packet;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
......@@ -73,4 +74,26 @@ public class IPAddressTest {
assertTrue("incorrect netmask",
Arrays.equals(IpAddress.ANY, ip2.netmask().toOctets()));
}
@Test
public void testContains() {
IpAddress slash31 = IpAddress.valueOf(BYTES1, 31);
IpAddress slash32 = IpAddress.valueOf(BYTES1, 32);
IpAddress differentSlash32 = IpAddress.valueOf(BYTES2, 32);
assertTrue(slash31.contains(differentSlash32));
assertFalse(differentSlash32.contains(slash31));
assertTrue(slash31.contains(slash32));
assertFalse(slash32.contains(differentSlash32));
assertFalse(differentSlash32.contains(slash32));
IpAddress zero = IpAddress.valueOf("0.0.0.0/0");
assertTrue(zero.contains(differentSlash32));
assertFalse(differentSlash32.contains(zero));
IpAddress slash8 = IpAddress.valueOf("10.0.0.0/8");
assertTrue(slash8.contains(slash31));
assertFalse(slash31.contains(slash8));
}
}
......