2 * Copyright (c) 2013 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.Inet6Address;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
21 * Utility class containing the common utility functions needed for operating on
22 * networking data structures
24 public abstract class NetUtils {
25 protected static final Logger logger = LoggerFactory.getLogger(NetUtils.class);
27 * Constant holding the number of bits in a byte
29 public static final int NumBitsInAByte = 8;
32 * Converts a 4 bytes array into an integer number
35 * the 4 bytes long byte array
36 * @return the integer number
38 public static int byteArray4ToInt(byte[] ba) {
39 if (ba == null || ba.length != 4) {
42 return (0xff & ba[0]) << 24 | (0xff & ba[1]) << 16 | (0xff & ba[2]) << 8 | (0xff & ba[3]);
46 * Converts a long to 6 bytes array for mac addresses
51 public static byte[] longToByteArray6(long addr){
52 byte[] mac = new byte[6];
53 for(int i = 0; i < 6; i++){
54 mac[i] = (byte) (addr >> (i*8));
61 * Converts an integer number into a 4 bytes array
65 * @return the byte array
67 public static byte[] intToByteArray4(int i) {
68 return new byte[] { (byte) ((i >> 24) & 0xff), (byte) ((i >> 16) & 0xff), (byte) ((i >> 8) & 0xff),
73 * Converts an IP address passed as integer value into the respective
77 * the IP address in integer form
78 * @return the IP address in InetAddress form
80 public static InetAddress getInetAddress(int address) {
81 InetAddress ip = null;
83 ip = InetAddress.getByAddress(NetUtils.intToByteArray4(address));
84 } catch (UnknownHostException e) {
91 * Return the InetAddress Network Mask given the length of the prefix bit
92 * mask. The prefix bit mask indicates the contiguous leading bits that are
93 * NOT masked out. Example: A prefix bit mask length of 8 will give an
94 * InetAddress Network Mask of 255.0.0.0
96 * @param prefixMaskLength
97 * integer representing the length of the prefix network mask
99 * boolean representing the IP version of the returned address
102 public static InetAddress getInetNetworkMask(int prefixMaskLength, boolean isV6) {
103 if (prefixMaskLength < 0 || (!isV6 && prefixMaskLength > 32) || (isV6 && prefixMaskLength > 128)) {
106 byte v4Address[] = { 0, 0, 0, 0 };
107 byte v6Address[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
108 byte address[] = (isV6) ? v6Address : v4Address;
109 int numBytes = prefixMaskLength / 8;
110 int numBits = prefixMaskLength % 8;
112 for (; i < numBytes; i++) {
113 address[i] = (byte) 0xff;
117 for (int j = 0; j < numBits; j++) {
120 address[i] = (byte) rem;
124 return InetAddress.getByAddress(address);
125 } catch (UnknownHostException e) {
132 * Returns the number of contiguous bits belonging to the subnet, that have
133 * to be masked out Example: A prefix network byte mask of ff.ff.ff.00 will
134 * give a subnet mask length of 8, while ff.00.00.00 will return a subnet
135 * mask length of 24. If the passed prefixMask object is null, 0 is returned
138 * the prefix mask as byte array
139 * @return the length of the prefix network mask
141 public static int getSubnetMaskLength(byte[] prefixMask) {
143 if (prefixMask != null) {
146 int numBytes = prefixMask.length;
147 for (int i = 0; i < numBytes; i++) {
148 intMask |= (prefixMask[i] & 0xff) << (8 * (numBytes - 1 - i));
152 while (((intMask & bit) == 0) && (maskLength <= (numBytes * 8))) {
161 * Returns the number of contiguous bits belonging to the subnet, that have
162 * to be masked out Example: A prefix network byte mask of ff.ff.ff.00 will
163 * give a subnet mask length of 8, while ff.00.00.00 will return a subnet
164 * mask length of 24 If the passed prefixMask object is null, 0 is returned
167 * the prefix mask as InetAddress
168 * @return the length of the prefix network mask
170 public static int getSubnetMaskLength(InetAddress prefixMask) {
171 return (prefixMask == null) ? 0 : NetUtils.getSubnetMaskLength(prefixMask.getAddress());
175 * Given an IP address and a prefix network mask length, it returns the
176 * equivalent subnet prefix IP address Example: for ip = "172.28.30.254" and
177 * maskLen = 25 it will return "172.28.30.128"
180 * the IP address in InetAddress form
182 * the length of the prefix network mask
183 * @return the subnet prefix IP address in InetAddress form
185 public static InetAddress getSubnetPrefix(InetAddress ip, int maskLen) {
186 int bytes = maskLen / 8;
187 int bits = maskLen % 8;
189 byte[] sn = ip.getAddress();
191 modifiedByte = (byte) (sn[bytes] >> (8 - bits));
192 sn[bytes] = (byte) (modifiedByte << (8 - bits));
195 for (; bytes < sn.length; bytes++) {
196 sn[bytes] = (byte) (0);
199 return InetAddress.getByAddress(sn);
200 } catch (UnknownHostException e) {
206 * Checks if the test address and mask conflicts with the filter address and
209 * For example: testAddress: 172.28.2.23 testMask: 255.255.255.0
210 * filtAddress: 172.28.1.10 testMask: 255.255.255.0 conflict
212 * testAddress: 172.28.2.23 testMask: 255.255.255.0 filtAddress: 172.28.1.10
213 * testMask: 255.255.0.0 do not conflict
215 * Null parameters are permitted
218 * @param filterAddress
223 public static boolean inetAddressConflict(InetAddress testAddress, InetAddress filterAddress, InetAddress testMask,
224 InetAddress filterMask) {
226 if ((testAddress == null) || (filterAddress == null)) {
231 if (isAny(testAddress) || isAny(filterAddress)) {
235 int testMaskLen = (testMask != null) ? NetUtils.getSubnetMaskLength(testMask.getAddress()) : 0;
236 int filterMaskLen = (filterMask != null) ? NetUtils.getSubnetMaskLength(filterMask.getAddress()) : 0;
238 int testPrefixLen = (testAddress instanceof Inet6Address) ? (128 - testMaskLen) : (32 - testMaskLen);
239 int filterPrefixLen = (filterAddress instanceof Inet6Address) ? (128 - filterMaskLen) : (32 - filterMaskLen);
241 // Mask length check. Test mask has to be more specific than filter one
242 if (testPrefixLen < filterPrefixLen) {
246 // Subnet Prefix on filter mask length must be the same
247 InetAddress prefix1 = getSubnetPrefix(testAddress, filterPrefixLen);
248 InetAddress prefix2 = getSubnetPrefix(filterAddress, filterPrefixLen);
249 return (!prefix1.equals(prefix2));
253 * Returns true if the passed MAC address is all zero
256 * the byte array representing the MAC address
257 * @return true if all MAC bytes are zero
259 public static boolean isZeroMAC(byte[] mac) {
260 for (short i = 0; i < 6; i++) {
269 * Returns true if the passed InetAddress contains all zero
272 * the IP address to test
273 * @return true if the address is all zero
275 public static boolean isAny(InetAddress ip) {
276 for (byte b : ip.getAddress()) {
284 public static boolean fieldsConflict(int field1, int field2) {
285 if ((field1 == 0) || (field2 == 0) || (field1 == field2)) {
291 public static InetAddress parseInetAddress(String addressString) {
292 InetAddress address = null;
294 address = InetAddress.getByName(addressString);
295 } catch (UnknownHostException e) {
302 * Checks if the passed IP v4 address in string form is valid The address
303 * may specify a mask at the end as "/MM"
306 * the v4 address as A.B.C.D/MM
309 public static boolean isIPv4AddressValid(String cidr) {
314 String values[] = cidr.split("/");
315 Pattern ipv4Pattern = Pattern
316 .compile("(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])");
317 Matcher mm = ipv4Pattern.matcher(values[0]);
321 if (values.length >= 2) {
322 int prefix = Integer.valueOf(values[1]);
323 if ((prefix < 0) || (prefix > 32)) {
331 * Checks if the passed IP v6 address in string form is valid The address
332 * may specify a mask at the end as "/MMM"
335 * the v6 address as A::1/MMM
338 public static boolean isIPv6AddressValid(String cidr) {
343 String values[] = cidr.split("/");
345 // when given an IP address, InetAddress.getByName validates the ip
347 InetAddress addr = InetAddress.getByName(values[0]);
348 if (!(addr instanceof Inet6Address)) {
351 } catch (UnknownHostException ex) {
355 if (values.length >= 2) {
356 int prefix = Integer.valueOf(values[1]);
357 if ((prefix < 0) || (prefix > 128)) {
365 * Checks if the passed IP address in string form is a valid v4 or v6
366 * address. The address may specify a mask at the end as "/MMM"
369 * the v4 or v6 address as IP/MMM
372 public static boolean isIPAddressValid(String cidr) {
373 return NetUtils.isIPv4AddressValid(cidr) || NetUtils.isIPv6AddressValid(cidr);
377 * Following utilities are useful when you need to compare or bit shift java
378 * primitive type variable which are inerently signed
381 * Returns the unsigned value of the passed byte variable
385 * @return the int variable containing the unsigned byte value
387 public static int getUnsignedByte(byte b) {
388 return (b > 0) ? (int) b : (b & 0x7F | 0x80);
392 * Return the unsigned value of the passed short variable
396 * @return the int variable containing the unsigned short value
398 public static int getUnsignedShort(short s) {
399 return (s > 0) ? (int) s : (s & 0x7FFF | 0x8000);
403 * Returns the highest v4 or v6 InetAddress
406 * true for IPv6, false for Ipv4
407 * @return The highest IPv4 or IPv6 address
409 public static InetAddress gethighestIP(boolean v6) {
411 return (v6) ? InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") : InetAddress
412 .getByName("255.255.255.255");
413 } catch (UnknownHostException e) {