/*
- * Copyright (c) 2013-2014 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2013, 2015 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,
* networking data structures
*/
public abstract class NetUtils {
- protected static final Logger logger = LoggerFactory.getLogger(NetUtils.class);
+ protected static final Logger LOG = LoggerFactory.getLogger(NetUtils.class);
/**
* Constant holding the number of bits in a byte
*/
* the 4 bytes long byte array
* @return the integer number
*/
- public static int byteArray4ToInt(byte[] ba) {
+ public static int byteArray4ToInt(final byte[] ba) {
if (ba == null || ba.length != 4) {
return 0;
}
- return (0xff & ba[0]) << 24 | (0xff & ba[1]) << 16 | (0xff & ba[2]) << 8 | (0xff & ba[3]);
+ return (0xff & ba[0]) << 24 | (0xff & ba[1]) << 16 | (0xff & ba[2]) << 8 | 0xff & ba[3];
}
/**
* Zero is returned if {@code ba} is {@code null} or
* the length of it is not six.
*/
- public static long byteArray6ToLong(byte[] ba) {
+ public static long byteArray6ToLong(final byte[] ba) {
if (ba == null || ba.length != MACAddrLengthInBytes) {
return 0L;
}
* the integer number
* @return the byte array
*/
- public static byte[] intToByteArray4(int i) {
- return new byte[] { (byte) ((i >> 24) & 0xff), (byte) ((i >> 16) & 0xff), (byte) ((i >> 8) & 0xff),
+ public static byte[] intToByteArray4(final int i) {
+ return new byte[] { (byte) (i >> 24 & 0xff), (byte) (i >> 16 & 0xff), (byte) (i >> 8 & 0xff),
(byte) (i & 0xff) };
}
* the IP address in integer form
* @return the IP address in InetAddress form
*/
- public static InetAddress getInetAddress(int address) {
+ public static InetAddress getInetAddress(final int address) {
InetAddress ip = null;
try {
ip = InetAddress.getByAddress(NetUtils.intToByteArray4(address));
- } catch (UnknownHostException e) {
- logger.error("", e);
+ } catch (final UnknownHostException e) {
+ LOG.error("", e);
}
return ip;
}
* boolean representing the IP version of the returned address
* @return
*/
- public static InetAddress getInetNetworkMask(int prefixMaskLength, boolean isV6) {
- if (prefixMaskLength < 0 || (!isV6 && prefixMaskLength > 32) || (isV6 && prefixMaskLength > 128)) {
+ public static InetAddress getInetNetworkMask(final int prefixMaskLength, final boolean isV6) {
+ if (prefixMaskLength < 0 || !isV6 && prefixMaskLength > 32 || isV6 && prefixMaskLength > 128) {
return null;
}
byte v4Address[] = { 0, 0, 0, 0 };
byte v6Address[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- byte address[] = (isV6) ? v6Address : v4Address;
+ byte address[] = isV6 ? v6Address : v4Address;
int numBytes = prefixMaskLength / 8;
int numBits = prefixMaskLength % 8;
int i = 0;
if (numBits > 0) {
int rem = 0;
for (int j = 0; j < numBits; j++) {
- rem |= 1 << (7 - j);
+ rem |= 1 << 7 - j;
}
address[i] = (byte) rem;
}
try {
return InetAddress.getByAddress(address);
- } catch (UnknownHostException e) {
- logger.error("", e);
+ } catch (final UnknownHostException e) {
+ LOG.error("", e);
}
return null;
}
* the subnet mask as byte array
* @return the prefix length as number of bits
*/
- public static int getSubnetMaskLength(byte[] subnetMask) {
+ public static int getSubnetMaskLength(final byte[] subnetMask) {
int maskLength = 0;
if (subnetMask != null && (subnetMask.length == 4 || subnetMask.length == 16)) {
int index = 0;
* the subnet mask as InetAddress
* @return the prefix length as number of bits
*/
- public static int getSubnetMaskLength(InetAddress subnetMask) {
+ public static int getSubnetMaskLength(final InetAddress subnetMask) {
return subnetMask == null ? 0 : NetUtils.getSubnetMaskLength(subnetMask.getAddress());
}
* the length of the prefix network mask
* @return the subnet prefix IP address in InetAddress form
*/
- public static InetAddress getSubnetPrefix(InetAddress ip, int maskLen) {
+ public static InetAddress getSubnetPrefix(final InetAddress ip, final int maskLen) {
int bytes = maskLen / 8;
int bits = maskLen % 8;
byte modifiedByte;
byte[] sn = ip.getAddress();
if (bits > 0) {
- modifiedByte = (byte) (sn[bytes] >> (8 - bits));
- sn[bytes] = (byte) (modifiedByte << (8 - bits));
+ modifiedByte = (byte) (sn[bytes] >> 8 - bits);
+ sn[bytes] = (byte) (modifiedByte << 8 - bits);
bytes++;
}
for (; bytes < sn.length; bytes++) {
- sn[bytes] = (byte) (0);
+ sn[bytes] = (byte) 0;
}
try {
return InetAddress.getByAddress(sn);
- } catch (UnknownHostException e) {
+ } catch (final UnknownHostException e) {
return null;
}
}
* @param filterMask
* @return
*/
- public static boolean inetAddressConflict(InetAddress testAddress, InetAddress filterAddress, InetAddress testMask,
- InetAddress filterMask) {
+ public static boolean inetAddressConflict(final InetAddress testAddress, final InetAddress filterAddress, final InetAddress testMask,
+ final InetAddress filterMask) {
// Sanity check
- if ((testAddress == null) || (filterAddress == null)) {
+ if (testAddress == null || filterAddress == null) {
return false;
}
return false;
}
- int testMaskLen = (testMask == null) ? ((testAddress instanceof Inet4Address) ? 32 : 128) : NetUtils
+ int testMaskLen = testMask == null ? testAddress instanceof Inet4Address ? 32 : 128 : NetUtils
.getSubnetMaskLength(testMask);
- int filterMaskLen = (filterMask == null) ? ((testAddress instanceof Inet4Address) ? 32 : 128) : NetUtils
+ 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
// Subnet Prefix on filter mask length must be the same
InetAddress prefix1 = getSubnetPrefix(testAddress, filterMaskLen);
InetAddress prefix2 = getSubnetPrefix(filterAddress, filterMaskLen);
- return (!prefix1.equals(prefix2));
+ return !prefix1.equals(prefix2);
}
/**
* the byte array representing the MAC address
* @return true if all MAC bytes are zero
*/
- public static boolean isZeroMAC(byte[] mac) {
+ public static boolean isZeroMAC(final byte[] mac) {
for (short i = 0; i < 6; i++) {
if (mac[i] != 0) {
return false;
* @param MACAddress
* @return
*/
- public static boolean isBroadcastMACAddr(byte[] MACAddress) {
+ public static boolean isBroadcastMACAddr(final byte[] MACAddress) {
if (MACAddress.length == MACAddrLengthInBytes) {
for (int i = 0; i < 6; i++) {
if (MACAddress[i] != BroadcastMACAddr[i]) {
* @param MACAddress
* @return
*/
- public static boolean isUnicastMACAddr(byte[] MACAddress) {
+ public static boolean isUnicastMACAddr(final byte[] MACAddress) {
if (MACAddress.length == MACAddrLengthInBytes) {
return (MACAddress[0] & 1) == 0;
}
* @param MACAddress
* @return
*/
- public static boolean isMulticastMACAddr(byte[] MACAddress) {
+ public static boolean isMulticastMACAddr(final byte[] MACAddress) {
if (MACAddress.length == MACAddrLengthInBytes && !isBroadcastMACAddr(MACAddress)) {
return (MACAddress[0] & 1) != 0;
}
* the IP address to test
* @return true if the address is all zero
*/
- public static boolean isAny(InetAddress ip) {
+ public static boolean isAny(final InetAddress ip) {
for (byte b : ip.getAddress()) {
if (b != 0) {
return false;
return true;
}
- public static boolean fieldsConflict(int field1, int field2) {
- if ((field1 == 0) || (field2 == 0) || (field1 == field2)) {
+ public static boolean fieldsConflict(final int field1, final int field2) {
+ if (field1 == 0 || field2 == 0 || field1 == field2) {
return false;
}
return true;
}
- public static InetAddress parseInetAddress(String addressString) {
+ public static InetAddress parseInetAddress(final String addressString) {
InetAddress address = null;
try {
address = InetAddress.getByName(addressString);
- } catch (UnknownHostException e) {
- logger.error("", e);
+ } catch (final UnknownHostException e) {
+ LOG.error("", e);
}
return address;
}
* the v4 address as A.B.C.D/MM
* @return
*/
- public static boolean isIPv4AddressValid(String cidr) {
+ public static boolean isIPv4AddressValid(final String cidr) {
if (cidr == null) {
return false;
}
}
if (values.length >= 2) {
int prefix = Integer.valueOf(values[1]);
- if ((prefix < 0) || (prefix > 32)) {
+ if (prefix < 0 || prefix > 32) {
return false;
}
}
* the v6 address as A::1/MMM
* @return
*/
- public static boolean isIPv6AddressValid(String cidr) {
+ public static boolean isIPv6AddressValid(final String cidr) {
if (cidr == null) {
return false;
}
if (!(addr instanceof Inet6Address)) {
return false;
}
- } catch (UnknownHostException ex) {
+ } catch (final UnknownHostException ex) {
return false;
}
if (values.length >= 2) {
int prefix = Integer.valueOf(values[1]);
- if ((prefix < 0) || (prefix > 128)) {
+ if (prefix < 0 || prefix > 128) {
return false;
}
}
* the v4 or v6 address as IP/MMM
* @return
*/
- public static boolean isIPAddressValid(String cidr) {
+ public static boolean isIPAddressValid(final String cidr) {
return NetUtils.isIPv4AddressValid(cidr) || NetUtils.isIPv6AddressValid(cidr);
}
* the byte value
* @return the int variable containing the unsigned byte value
*/
- public static int getUnsignedByte(byte b) {
+ public static int getUnsignedByte(final byte b) {
return b & 0xFF;
}
* the short value
* @return the int variable containing the unsigned short value
*/
- public static int getUnsignedShort(short s) {
+ public static int getUnsignedShort(final short s) {
return s & 0xFFFF;
}
* true for IPv6, false for Ipv4
* @return The highest IPv4 or IPv6 address
*/
- public static InetAddress gethighestIP(boolean v6) {
+ public static InetAddress gethighestIP(final boolean v6) {
try {
- return (v6) ? InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") : InetAddress
+ return v6 ? InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") : InetAddress
.getByName("255.255.255.255");
- } catch (UnknownHostException e) {
+ } catch (final UnknownHostException e) {
return null;
}
}