Showing
2 changed files
with
47 additions
and
0 deletions
... | @@ -234,6 +234,30 @@ public final class IpAddress { | ... | @@ -234,6 +234,30 @@ public final class IpAddress { |
234 | return mask() != 0; | 234 | return mask() != 0; |
235 | } | 235 | } |
236 | 236 | ||
237 | + /** | ||
238 | + * Determines whether a given address is contained within this IpAddress' | ||
239 | + * network. | ||
240 | + * | ||
241 | + * @param other another IP address that could be contained in this network | ||
242 | + * @return true if the other IP address is contained in this address' | ||
243 | + * network, otherwise false | ||
244 | + */ | ||
245 | + public boolean contains(IpAddress other) { | ||
246 | + if (this.netmask <= other.netmask) { | ||
247 | + // Special case where they're both /32 addresses | ||
248 | + if (this.netmask == MAX_INET_MASK) { | ||
249 | + return Arrays.equals(octets, other.octets); | ||
250 | + } | ||
251 | + | ||
252 | + // Mask the other address with our network mask | ||
253 | + IpAddress otherMasked = | ||
254 | + IpAddress.valueOf(other.octets, netmask).network(); | ||
255 | + | ||
256 | + return network().equals(otherMasked); | ||
257 | + } | ||
258 | + return false; | ||
259 | + } | ||
260 | + | ||
237 | @Override | 261 | @Override |
238 | public int hashCode() { | 262 | public int hashCode() { |
239 | final int prime = 31; | 263 | final int prime = 31; | ... | ... |
1 | package org.onlab.packet; | 1 | package org.onlab.packet; |
2 | 2 | ||
3 | import static org.junit.Assert.assertEquals; | 3 | import static org.junit.Assert.assertEquals; |
4 | +import static org.junit.Assert.assertFalse; | ||
4 | import static org.junit.Assert.assertTrue; | 5 | import static org.junit.Assert.assertTrue; |
5 | 6 | ||
6 | import java.util.Arrays; | 7 | import java.util.Arrays; |
... | @@ -73,4 +74,26 @@ public class IPAddressTest { | ... | @@ -73,4 +74,26 @@ public class IPAddressTest { |
73 | assertTrue("incorrect netmask", | 74 | assertTrue("incorrect netmask", |
74 | Arrays.equals(IpAddress.ANY, ip2.netmask().toOctets())); | 75 | Arrays.equals(IpAddress.ANY, ip2.netmask().toOctets())); |
75 | } | 76 | } |
77 | + | ||
78 | + @Test | ||
79 | + public void testContains() { | ||
80 | + IpAddress slash31 = IpAddress.valueOf(BYTES1, 31); | ||
81 | + IpAddress slash32 = IpAddress.valueOf(BYTES1, 32); | ||
82 | + IpAddress differentSlash32 = IpAddress.valueOf(BYTES2, 32); | ||
83 | + | ||
84 | + assertTrue(slash31.contains(differentSlash32)); | ||
85 | + assertFalse(differentSlash32.contains(slash31)); | ||
86 | + | ||
87 | + assertTrue(slash31.contains(slash32)); | ||
88 | + assertFalse(slash32.contains(differentSlash32)); | ||
89 | + assertFalse(differentSlash32.contains(slash32)); | ||
90 | + | ||
91 | + IpAddress zero = IpAddress.valueOf("0.0.0.0/0"); | ||
92 | + assertTrue(zero.contains(differentSlash32)); | ||
93 | + assertFalse(differentSlash32.contains(zero)); | ||
94 | + | ||
95 | + IpAddress slash8 = IpAddress.valueOf("10.0.0.0/8"); | ||
96 | + assertTrue(slash8.contains(slash31)); | ||
97 | + assertFalse(slash31.contains(slash8)); | ||
98 | + } | ||
76 | } | 99 | } | ... | ... |
-
Please register or login to post a comment