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 {
if (ip.getProtocol() != IPv4.PROTOCOL_IGMP ||
!IpPrefix.MULTICAST_RANGE.contains(gaddr)) {
!IpPrefix.IPV4_MULTICAST_RANGE.contains(gaddr)) {
return;
}
if (IpPrefix.MULTICAST_RANGE.contains(saddr)) {
if (IpPrefix.IPV4_MULTICAST_RANGE.contains(saddr)) {
log.debug("IGMP Picked up a packet with a multicast source address.");
return;
}
......
......@@ -314,6 +314,17 @@ public class IpAddress implements Comparable<IpAddress> {
return isIp4() && octets[0] == (byte) 169;
}
/**
* Check if this IP address is a multicast address.
*
* @return true if this address a multicast address
*/
public boolean isMulticast() {
return isIp4() ?
Ip4Prefix.IPV4_MULTICAST_RANGE.contains(this.getIp4Address()) :
Ip6Prefix.IPV6_MULTICAST_RANGE.contains(this.getIp6Address());
}
@Override
public int compareTo(IpAddress o) {
// Compare first the version
......
......@@ -27,11 +27,27 @@ import java.util.Objects;
* </p>
*/
public class IpPrefix {
// Maximum network mask length
/**
* Longest IPv4 network prefix.
*/
public static final int MAX_INET_MASK_LENGTH = IpAddress.INET_BIT_LENGTH;
/**
* Longest IPv6 network prefix.
*/
public static final int MAX_INET6_MASK_LENGTH = IpAddress.INET6_BIT_LENGTH;
/**
* An IpPrefix that contains all IPv4 multicast addresses.
*/
@Deprecated
public static final IpPrefix MULTICAST_RANGE = IpPrefix.valueOf("224.0.0.0/4");
/**
* An IpPrefix that contains all IPv4 multicast addresses.
*/
public static final IpPrefix IPV4_MULTICAST_RANGE = IpPrefix.valueOf("224.0.0.0/4");
/**
* An IpPrefix that contains all IPv6 multicast addresses.
*/
public static final IpPrefix IPV6_MULTICAST_RANGE = IpPrefix.valueOf("ff00::/8");
private final IpAddress address;
private final short prefixLength;
......
......@@ -769,6 +769,21 @@ public class IpAddressTest {
}
/**
* Tests if the address is a multicast address.
*/
@Test
public void testIsMulticast() {
IpAddress v4Unicast = IpAddress.valueOf("10.0.0.1");
IpAddress v4Multicast = IpAddress.valueOf("224.0.0.1");
IpAddress v6Unicast = IpAddress.valueOf("1000::1");
IpAddress v6Multicast = IpAddress.valueOf("ff02::1");
assertFalse(v4Unicast.isMulticast());
assertTrue(v4Multicast.isMulticast());
assertFalse(v6Unicast.isMulticast());
assertTrue(v6Multicast.isMulticast());
}
/**
* Tests comparison of {@link IpAddress} for IPv4.
*/
@Test
......