Charles Chan
Committed by Gerrit Code Review

Introduce an easy way to check whether an IP address is multicast or not

Change-Id: I192a1b3e04b843fa0446d011cc98393a1950a5a9
...@@ -419,11 +419,11 @@ public class IgmpSnoop { ...@@ -419,11 +419,11 @@ public class IgmpSnoop {
419 419
420 420
421 if (ip.getProtocol() != IPv4.PROTOCOL_IGMP || 421 if (ip.getProtocol() != IPv4.PROTOCOL_IGMP ||
422 - !IpPrefix.MULTICAST_RANGE.contains(gaddr)) { 422 + !IpPrefix.IPV4_MULTICAST_RANGE.contains(gaddr)) {
423 return; 423 return;
424 } 424 }
425 425
426 - if (IpPrefix.MULTICAST_RANGE.contains(saddr)) { 426 + if (IpPrefix.IPV4_MULTICAST_RANGE.contains(saddr)) {
427 log.debug("IGMP Picked up a packet with a multicast source address."); 427 log.debug("IGMP Picked up a packet with a multicast source address.");
428 return; 428 return;
429 } 429 }
......
...@@ -314,6 +314,17 @@ public class IpAddress implements Comparable<IpAddress> { ...@@ -314,6 +314,17 @@ public class IpAddress implements Comparable<IpAddress> {
314 return isIp4() && octets[0] == (byte) 169; 314 return isIp4() && octets[0] == (byte) 169;
315 } 315 }
316 316
317 + /**
318 + * Check if this IP address is a multicast address.
319 + *
320 + * @return true if this address a multicast address
321 + */
322 + public boolean isMulticast() {
323 + return isIp4() ?
324 + Ip4Prefix.IPV4_MULTICAST_RANGE.contains(this.getIp4Address()) :
325 + Ip6Prefix.IPV6_MULTICAST_RANGE.contains(this.getIp6Address());
326 + }
327 +
317 @Override 328 @Override
318 public int compareTo(IpAddress o) { 329 public int compareTo(IpAddress o) {
319 // Compare first the version 330 // Compare first the version
......
...@@ -27,11 +27,27 @@ import java.util.Objects; ...@@ -27,11 +27,27 @@ import java.util.Objects;
27 * </p> 27 * </p>
28 */ 28 */
29 public class IpPrefix { 29 public class IpPrefix {
30 - // Maximum network mask length 30 + /**
31 + * Longest IPv4 network prefix.
32 + */
31 public static final int MAX_INET_MASK_LENGTH = IpAddress.INET_BIT_LENGTH; 33 public static final int MAX_INET_MASK_LENGTH = IpAddress.INET_BIT_LENGTH;
34 + /**
35 + * Longest IPv6 network prefix.
36 + */
32 public static final int MAX_INET6_MASK_LENGTH = IpAddress.INET6_BIT_LENGTH; 37 public static final int MAX_INET6_MASK_LENGTH = IpAddress.INET6_BIT_LENGTH;
33 - 38 + /**
39 + * An IpPrefix that contains all IPv4 multicast addresses.
40 + */
41 + @Deprecated
34 public static final IpPrefix MULTICAST_RANGE = IpPrefix.valueOf("224.0.0.0/4"); 42 public static final IpPrefix MULTICAST_RANGE = IpPrefix.valueOf("224.0.0.0/4");
43 + /**
44 + * An IpPrefix that contains all IPv4 multicast addresses.
45 + */
46 + public static final IpPrefix IPV4_MULTICAST_RANGE = IpPrefix.valueOf("224.0.0.0/4");
47 + /**
48 + * An IpPrefix that contains all IPv6 multicast addresses.
49 + */
50 + public static final IpPrefix IPV6_MULTICAST_RANGE = IpPrefix.valueOf("ff00::/8");
35 51
36 private final IpAddress address; 52 private final IpAddress address;
37 private final short prefixLength; 53 private final short prefixLength;
......
...@@ -769,6 +769,21 @@ public class IpAddressTest { ...@@ -769,6 +769,21 @@ public class IpAddressTest {
769 } 769 }
770 770
771 /** 771 /**
772 + * Tests if the address is a multicast address.
773 + */
774 + @Test
775 + public void testIsMulticast() {
776 + IpAddress v4Unicast = IpAddress.valueOf("10.0.0.1");
777 + IpAddress v4Multicast = IpAddress.valueOf("224.0.0.1");
778 + IpAddress v6Unicast = IpAddress.valueOf("1000::1");
779 + IpAddress v6Multicast = IpAddress.valueOf("ff02::1");
780 + assertFalse(v4Unicast.isMulticast());
781 + assertTrue(v4Multicast.isMulticast());
782 + assertFalse(v6Unicast.isMulticast());
783 + assertTrue(v6Multicast.isMulticast());
784 + }
785 +
786 + /**
772 * Tests comparison of {@link IpAddress} for IPv4. 787 * Tests comparison of {@link IpAddress} for IPv4.
773 */ 788 */
774 @Test 789 @Test
......