2 * Copyright (c) 2013-2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.sal.utils;
11 import java.net.Inet4Address;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
15 import java.util.Arrays;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Utility class containing the common utility functions needed for operating on
24 * networking data structures
26 public abstract class NetUtils {
27 protected static final Logger logger = LoggerFactory.getLogger(NetUtils.class);
29 * Constant holding the number of bits in a byte
31 public static final int NumBitsInAByte = 8;
34 * Constant holding the number of bytes in MAC Address
36 public static final int MACAddrLengthInBytes = 6;
39 * Constant holding the number of words in MAC Address
41 public static final int MACAddrLengthInWords = 3;
44 * Constant holding the broadcast MAC address
46 private static final byte[] BroadcastMACAddr = {-1, -1, -1, -1, -1, -1};
49 * Converts a 4 bytes array into an integer number
52 * the 4 bytes long byte array
53 * @return the integer number
55 public static int byteArray4ToInt(byte[] ba) {
56 if (ba == null || ba.length != 4) {
59 return (0xff & ba[0]) << 24 | (0xff & ba[1]) << 16 | (0xff & ba[2]) << 8 | (0xff & ba[3]);
63 * Converts a 6 bytes array into a long number MAC addresses.
66 * The 6 bytes long byte array.
67 * @return The long number.
68 * Zero is returned if {@code ba} is {@code null} or
69 * the length of it is not six.
71 public static long byteArray6ToLong(byte[] ba) {
72 if (ba == null || ba.length != MACAddrLengthInBytes) {
78 num <<= NumBitsInAByte;
81 } while (i < MACAddrLengthInBytes);
86 * Converts a long number to a 6 bytes array for MAC addresses.
90 * @return The byte array.
92 public static byte[] longToByteArray6(long addr){
93 byte[] mac = new byte[MACAddrLengthInBytes];
94 int i = MACAddrLengthInBytes - 1;
97 addr >>>= NumBitsInAByte;
104 * Converts an integer number into a 4 bytes array
108 * @return the byte array
110 public static byte[] intToByteArray4(int i) {
111 return new byte[] { (byte) ((i >> 24) & 0xff), (byte) ((i >> 16) & 0xff), (byte) ((i >> 8) & 0xff),
116 * Converts an IP address passed as integer value into the respective
120 * the IP address in integer form
121 * @return the IP address in InetAddress form
123 public static InetAddress getInetAddress(int address) {
124 InetAddress ip = null;
126 ip = InetAddress.getByAddress(NetUtils.intToByteArray4(address));
127 } catch (UnknownHostException e) {
134 * Return the InetAddress Network Mask given the length of the prefix bit
135 * mask. The prefix bit mask indicates the contiguous leading bits that are
136 * NOT masked out. Example: A prefix bit mask length of 8 will give an
137 * InetAddress Network Mask of 255.0.0.0
139 * @param prefixMaskLength
140 * integer representing the length of the prefix network mask
142 * boolean representing the IP version of the returned address
145 public static InetAddress getInetNetworkMask(int prefixMaskLength, boolean isV6) {
146 if (prefixMaskLength < 0 || (!isV6 && prefixMaskLength > 32) || (isV6 && prefixMaskLength > 128)) {
149 byte v4Address[] = { 0, 0, 0, 0 };
150 byte v6Address[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
151 byte address[] = (isV6) ? v6Address : v4Address;
152 int numBytes = prefixMaskLength / 8;
153 int numBits = prefixMaskLength % 8;
155 for (; i < numBytes; i++) {
156 address[i] = (byte) 0xff;
160 for (int j = 0; j < numBits; j++) {
163 address[i] = (byte) rem;
167 return InetAddress.getByAddress(address);
168 } catch (UnknownHostException e) {
175 * Returns the prefix size in bits of the specified subnet mask. Example:
176 * For the subnet mask ff.ff.ff.e0 it returns 25 while for ff.00.00.00 it
177 * returns 8. If the passed subnetMask array is null, 0 is returned.
180 * the subnet mask as byte array
181 * @return the prefix length as number of bits
183 public static int getSubnetMaskLength(byte[] subnetMask) {
185 if (subnetMask != null && (subnetMask.length == 4 || subnetMask.length == 16)) {
187 while (index < subnetMask.length && subnetMask[index] == (byte) 0xFF) {
188 maskLength += NetUtils.NumBitsInAByte;
191 if (index != subnetMask.length) {
192 int bits = NetUtils.NumBitsInAByte - 1;
193 while (bits >= 0 && (subnetMask[index] & 1 << bits) != 0) {
203 * Returns the prefix size in bits of the specified subnet mask. Example:
204 * For the subnet mask 255.255.255.128 it returns 25 while for 255.0.0.0 it
205 * returns 8. If the passed subnetMask object is null, 0 is returned
208 * the subnet mask as InetAddress
209 * @return the prefix length as number of bits
211 public static int getSubnetMaskLength(InetAddress subnetMask) {
212 return subnetMask == null ? 0 : NetUtils.getSubnetMaskLength(subnetMask.getAddress());
216 * Given an IP address and a prefix network mask length, it returns the
217 * equivalent subnet prefix IP address Example: for ip = "172.28.30.254" and
218 * maskLen = 25 it will return "172.28.30.128"
221 * the IP address in InetAddress form
223 * the length of the prefix network mask
224 * @return the subnet prefix IP address in InetAddress form
226 public static InetAddress getSubnetPrefix(InetAddress ip, int maskLen) {
227 int bytes = maskLen / 8;
228 int bits = maskLen % 8;
230 byte[] sn = ip.getAddress();
232 modifiedByte = (byte) (sn[bytes] >> (8 - bits));
233 sn[bytes] = (byte) (modifiedByte << (8 - bits));
236 for (; bytes < sn.length; bytes++) {
237 sn[bytes] = (byte) (0);
240 return InetAddress.getByAddress(sn);
241 } catch (UnknownHostException e) {
247 * Checks if the test address and mask conflicts with the filter address and
251 * testAddress: 172.28.2.23
252 * testMask: 255.255.255.0
253 * filterAddress: 172.28.1.10
254 * testMask: 255.255.255.0
257 * testAddress: 172.28.2.23
258 * testMask: 255.255.255.0
259 * filterAddress: 172.28.1.10
260 * testMask: 255.255.0.0
263 * Null parameters are permitted
266 * @param filterAddress
271 public static boolean inetAddressConflict(InetAddress testAddress, InetAddress filterAddress, InetAddress testMask,
272 InetAddress filterMask) {
274 if ((testAddress == null) || (filterAddress == null)) {
279 if (isAny(testAddress) || isAny(filterAddress)) {
283 int testMaskLen = (testMask == null) ? ((testAddress instanceof Inet4Address) ? 32 : 128) : NetUtils
284 .getSubnetMaskLength(testMask);
285 int filterMaskLen = (filterMask == null) ? ((testAddress instanceof Inet4Address) ? 32 : 128) : NetUtils
286 .getSubnetMaskLength(filterMask);
288 // Mask length check. Test mask has to be more specific than filter one
289 if (testMaskLen < filterMaskLen) {
293 // Subnet Prefix on filter mask length must be the same
294 InetAddress prefix1 = getSubnetPrefix(testAddress, filterMaskLen);
295 InetAddress prefix2 = getSubnetPrefix(filterAddress, filterMaskLen);
296 return (!prefix1.equals(prefix2));
300 * Returns true if the passed MAC address is all zero
303 * the byte array representing the MAC address
304 * @return true if all MAC bytes are zero
306 public static boolean isZeroMAC(byte[] mac) {
307 for (short i = 0; i < 6; i++) {
316 * Returns true if the MAC address is the broadcast MAC address and false
322 public static boolean isBroadcastMACAddr(byte[] MACAddress) {
323 if (MACAddress.length == MACAddrLengthInBytes) {
324 for (int i = 0; i < 6; i++) {
325 if (MACAddress[i] != BroadcastMACAddr[i]) {
335 * Returns true if the MAC address is a unicast MAC address and false
341 public static boolean isUnicastMACAddr(byte[] MACAddress) {
342 if (MACAddress.length == MACAddrLengthInBytes) {
343 return (MACAddress[0] & 1) == 0;
349 * Returns true if the MAC address is a multicast MAC address and false
350 * otherwise. Note that this explicitly returns false for the broadcast MAC
356 public static boolean isMulticastMACAddr(byte[] MACAddress) {
357 if (MACAddress.length == MACAddrLengthInBytes && !isBroadcastMACAddr(MACAddress)) {
358 return (MACAddress[0] & 1) != 0;
364 * Returns true if the passed InetAddress contains all zero
367 * the IP address to test
368 * @return true if the address is all zero
370 public static boolean isAny(InetAddress ip) {
371 for (byte b : ip.getAddress()) {
379 public static boolean fieldsConflict(int field1, int field2) {
380 if ((field1 == 0) || (field2 == 0) || (field1 == field2)) {
386 public static InetAddress parseInetAddress(String addressString) {
387 InetAddress address = null;
389 address = InetAddress.getByName(addressString);
390 } catch (UnknownHostException e) {
397 * Checks if the passed IP v4 address in string form is valid The address
398 * may specify a mask at the end as "/MM"
401 * the v4 address as A.B.C.D/MM
404 public static boolean isIPv4AddressValid(String cidr) {
409 String values[] = cidr.split("/");
410 Pattern ipv4Pattern = Pattern
411 .compile("(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])");
412 Matcher mm = ipv4Pattern.matcher(values[0]);
416 if (values.length >= 2) {
417 int prefix = Integer.valueOf(values[1]);
418 if ((prefix < 0) || (prefix > 32)) {
426 * Checks if the passed IP v6 address in string form is valid The address
427 * may specify a mask at the end as "/MMM"
430 * the v6 address as A::1/MMM
433 public static boolean isIPv6AddressValid(String cidr) {
438 String values[] = cidr.split("/");
440 // when given an IP address, InetAddress.getByName validates the ip
442 InetAddress addr = InetAddress.getByName(values[0]);
443 if (!(addr instanceof Inet6Address)) {
446 } catch (UnknownHostException ex) {
450 if (values.length >= 2) {
451 int prefix = Integer.valueOf(values[1]);
452 if ((prefix < 0) || (prefix > 128)) {
460 * Checks if the passed IP address in string form is a valid v4 or v6
461 * address. The address may specify a mask at the end as "/MMM"
464 * the v4 or v6 address as IP/MMM
467 public static boolean isIPAddressValid(String cidr) {
468 return NetUtils.isIPv4AddressValid(cidr) || NetUtils.isIPv6AddressValid(cidr);
472 * Following utilities are useful when you need to compare or bit shift java
473 * primitive type variable which are inherently signed
476 * Returns the unsigned value of the passed byte variable
480 * @return the int variable containing the unsigned byte value
482 public static int getUnsignedByte(byte b) {
487 * Return the unsigned value of the passed short variable
491 * @return the int variable containing the unsigned short value
493 public static int getUnsignedShort(short s) {
498 * Returns the highest v4 or v6 InetAddress
501 * true for IPv6, false for Ipv4
502 * @return The highest IPv4 or IPv6 address
504 public static InetAddress gethighestIP(boolean v6) {
506 return (v6) ? InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") : InetAddress
507 .getByName("255.255.255.255");
508 } catch (UnknownHostException e) {
514 * Returns Broadcast MAC Address
516 * @return the byte array containing broadcast mac address
518 public static byte[] getBroadcastMACAddr() {
519 return Arrays.copyOf(BroadcastMACAddr, BroadcastMACAddr.length);