From 31045ff2b7b0106f7e7ef7198bed882131127f81 Mon Sep 17 00:00:00 2001 From: Colin Dixon Date: Fri, 5 Jul 2013 17:50:23 -0500 Subject: [PATCH] adding isBroadcast and isMulticast functions to Ethernet Change-Id: I8777ed281261d062b57cbeae00317c955c4b6385 Signed-off-by: Colin Dixon --- .../controller/sal/packet/Ethernet.java | 9 ++++ .../controller/sal/utils/NetUtils.java | 42 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/Ethernet.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/Ethernet.java index 7f151c9502..d0068564a9 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/Ethernet.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/packet/Ethernet.java @@ -16,6 +16,7 @@ import java.util.Map; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; import org.opendaylight.controller.sal.utils.EtherTypes; +import org.opendaylight.controller.sal.utils.NetUtils; /** * Class that represents the Ethernet frame objects @@ -102,6 +103,14 @@ public class Ethernet extends Packet { return BitBufferHelper.getShort(fieldValues.get(ETHT)); } + public boolean isBroadcast(){ + return NetUtils.isBroadcastMACAddr(getDestinationMACAddress()); + } + + public boolean isMulticast(){ + return NetUtils.isMulticastMACAddr(getDestinationMACAddress()); + } + /** * Sets the destination MAC address for the current Ethernet object instance * @param byte[] - the destinationMACAddress to set diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java index 8aee1cc37f..4b42cb7669 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java @@ -38,6 +38,11 @@ public abstract class NetUtils { */ public static final int MACAddrLengthInWords = 3; + /** + * Constant holding the broadcast MAC address + */ + public static byte[] BroadcastMACAddr = {-1, -1, -1, -1, -1, -1}; + /** * Converts a 4 bytes array into an integer number * @@ -275,6 +280,43 @@ public abstract class NetUtils { return true; } + /** + * Returns true if the MAC address is the broadcast MAC address and false + * otherwise. + * + * @param MACAddress + * @return + */ + public static boolean isBroadcastMACAddr(byte[] MACAddress) { + if (MACAddress.length == MACAddrLengthInBytes) { + for (int i = 0; i < 6; i++) { + if (MACAddress[i] != BroadcastMACAddr[i]) { + return false; + } + } + return true; + } + + return false; + } + + /** + * Returns true if the MAC address is a multicast MAC address and false + * otherwise. Note that this explicitly returns false for the broadcast MAC + * address. + * + * @param MACAddress + * @return + */ + public static boolean isMulticastMACAddr(byte[] MACAddress) { + if (MACAddress.length == MACAddrLengthInBytes && !isBroadcastMACAddr(MACAddress)) { + if (MACAddress[0] % 2 == 1) { + return true; + } + } + return false; + } + /** * Returns true if the passed InetAddress contains all zero * -- 2.36.6