/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2013-2014 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
+import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
}
/**
- * Converts a long to 6 bytes array for mac addresses
- * @param addr
- * @return
+ * Converts a 6 bytes array into a long number MAC addresses.
+ *
+ * @param ba
+ * The 6 bytes long byte array.
+ * @return The long number.
+ * Zero is returned if {@code ba} is {@code null} or
+ * the length of it is not six.
*/
+ public static long byteArray6ToLong(byte[] ba) {
+ if (ba == null || ba.length != MACAddrLengthInBytes) {
+ return 0L;
+ }
+ long num = 0L;
+ int i = 0;
+ do {
+ num <<= NumBitsInAByte;
+ num |= 0xff & ba[i];
+ i++;
+ } while (i < MACAddrLengthInBytes);
+ return num;
+ }
+ /**
+ * Converts a long number to a 6 bytes array for MAC addresses.
+ *
+ * @param addr
+ * The long number.
+ * @return The byte array.
+ */
public static byte[] longToByteArray6(long addr){
- byte[] mac = new byte[6];
- for(int i = 0; i < 6; i++){
- mac[i] = (byte) (addr >> (i*8));
- }
+ byte[] mac = new byte[MACAddrLengthInBytes];
+ int i = MACAddrLengthInBytes - 1;
+ do {
+ mac[i] = (byte) addr;
+ addr >>>= NumBitsInAByte;
+ i--;
+ } while (i >= 0);
return mac;
}
-
/**
* Converts an integer number into a 4 bytes array
*
* Checks if the test address and mask conflicts with the filter address and
* mask
*
- * For example: testAddress: 172.28.2.23 testMask: 255.255.255.0
- * filtAddress: 172.28.1.10 testMask: 255.255.255.0 conflict
+ * For example:
+ * testAddress: 172.28.2.23
+ * testMask: 255.255.255.0
+ * filterAddress: 172.28.1.10
+ * testMask: 255.255.255.0
+ * do conflict
*
- * testAddress: 172.28.2.23 testMask: 255.255.255.0 filtAddress: 172.28.1.10
- * testMask: 255.255.0.0 do not conflict
+ * testAddress: 172.28.2.23
+ * testMask: 255.255.255.0
+ * filterAddress: 172.28.1.10
+ * testMask: 255.255.0.0
+ * do not conflict
*
* Null parameters are permitted
*
int testMaskLen = (testMask == null) ? ((testAddress instanceof Inet4Address) ? 32 : 128) : NetUtils
.getSubnetMaskLength(testMask);
- int filterMaskLen = NetUtils.getSubnetMaskLength(filterMask);
+ int filterMaskLen = (filterMask == null) ? ((testAddress instanceof Inet4Address) ? 32 : 128) : NetUtils
+ .getSubnetMaskLength(filterMask);
// Mask length check. Test mask has to be more specific than filter one
if (testMaskLen < filterMaskLen) {
return false;
}
+ /**
+ * Returns true if the MAC address is a unicast MAC address and false
+ * otherwise.
+ *
+ * @param MACAddress
+ * @return
+ */
+ public static boolean isUnicastMACAddr(byte[] MACAddress) {
+ if (MACAddress.length == MACAddrLengthInBytes) {
+ return (MACAddress[0] & 1) == 0;
+ }
+ return false;
+ }
/**
* Returns true if the MAC address is a multicast MAC address and false
*/
public static boolean isMulticastMACAddr(byte[] MACAddress) {
if (MACAddress.length == MACAddrLengthInBytes && !isBroadcastMACAddr(MACAddress)) {
- if (MACAddress[0] % 2 == 1) {
- return true;
- }
+ return (MACAddress[0] & 1) != 0;
}
return false;
}
/*
* Following utilities are useful when you need to compare or bit shift java
- * primitive type variable which are inerently signed
+ * primitive type variable which are inherently signed
*/
/**
* Returns the unsigned value of the passed byte variable
* @return the int variable containing the unsigned byte value
*/
public static int getUnsignedByte(byte b) {
- return (b > 0) ? (int) b : (b & 0x7F | 0x80);
+ return b & 0xFF;
}
/**
* @return the int variable containing the unsigned short value
*/
public static int getUnsignedShort(short s) {
- return (s > 0) ? (int) s : (s & 0x7FFF | 0x8000);
+ return s & 0xFFFF;
}
/**
return null;
}
}
+
+ /**
+ * Returns Broadcast MAC Address
+ *
+ * @return the byte array containing broadcast mac address
+ */
+ public static byte[] getBroadcastMACAddr() {
+ return Arrays.copyOf(BroadcastMACAddr, BroadcastMACAddr.length);
+ }
}