3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.sal.utils;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * Utility class containing the common utility functions needed
23 * for operating on networking data structures
28 public abstract class NetUtils {
29 protected static final Logger logger = LoggerFactory
30 .getLogger(NetUtils.class);
32 * Constant holding the number of bits in a byte
34 public static final int NumBitsInAByte = 8;
37 * Converts a 4 bytes array into an integer number
39 * @param ba the 4 bytes long byte array
40 * @return the integer number
42 public static int byteArray4ToInt(byte[] ba) {
43 if (ba == null || ba.length != 4)
45 return (int) ((0xff & ba[0]) << 24 | (0xff & ba[1]) << 16
46 | (0xff & ba[2]) << 8 | (0xff & ba[3]));
50 * Converts an integer number into a 4 bytes array
52 * @param i the integer number
53 * @return the byte array
55 public static byte[] intToByteArray4(int i) {
56 return new byte[] { (byte) ((i >> 24) & 0xff),
57 (byte) ((i >> 16) & 0xff), (byte) ((i >> 8) & 0xff),
62 * Converts an IP address passed as integer value into the
63 * respective InetAddress object
65 * @param address the IP address in integer form
66 * @return the IP address in InetAddress form
68 public static InetAddress getInetAddress(int address) {
69 InetAddress ip = null;
71 ip = InetAddress.getByAddress(NetUtils.intToByteArray4(address));
72 } catch (UnknownHostException e) {
79 * Return the InetAddress Network Mask given the length of the prefix bit mask.
80 * The prefix bit mask indicates the contiguous leading bits that are NOT masked out.
81 * Example: A prefix bit mask length of 8 will give an InetAddress Network Mask of 255.0.0.0
83 * @param prefixMaskLength integer representing the length of the prefix network mask
84 * @param isV6 boolean representing the IP version of the returned address
87 public static InetAddress getInetNetworkMask(int prefixMaskLength,
89 if (prefixMaskLength < 0 || (!isV6 && prefixMaskLength > 32)
90 || (isV6 && prefixMaskLength > 128)) {
93 byte v4Address[] = { 0, 0, 0, 0 };
94 byte v6Address[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
95 byte address[] = (isV6) ? v6Address : v4Address;
96 int numBytes = prefixMaskLength / 8;
97 int numBits = prefixMaskLength % 8;
99 for (; i < numBytes; i++) {
100 address[i] = (byte) 0xff;
104 for (int j = 0; j < numBits; j++) {
107 address[i] = (byte) rem;
111 return InetAddress.getByAddress(address);
112 } catch (UnknownHostException e) {
119 * Returns the number of contiguous bits belonging to the subnet, that have to be masked out
120 * Example: A prefix network byte mask of ff.ff.ff.00 will give a subnet mask length of 8,
121 * while ff.00.00.00 will return a subnet mask length of 24.
122 * If the passed prefixMask object is null, 0 is returned
124 * @param prefixMask the prefix mask as byte array
125 * @return the length of the prefix network mask
127 public static int getSubnetMaskLength(byte[] prefixMask) {
129 if (prefixMask != null) {
132 int numBytes = prefixMask.length;
133 for (int i = 0; i < numBytes; i++) {
134 intMask |= ((int) prefixMask[i] & 0xff) << (8 * (numBytes - 1 - i));
138 while (((intMask & bit) == 0) && (maskLength <= (numBytes * 8))) {
147 * Returns the number of contiguous bits belonging to the subnet, that have to be masked out
148 * Example: A prefix network byte mask of ff.ff.ff.00 will give a subnet mask length of 8,
149 * while ff.00.00.00 will return a subnet mask length of 24
150 * If the passed prefixMask object is null, 0 is returned
152 * @param prefixMask the prefix mask as InetAddress
153 * @return the length of the prefix network mask
155 public static int getSubnetMaskLength(InetAddress prefixMask) {
156 return (prefixMask == null) ? 0 : NetUtils
157 .getSubnetMaskLength(prefixMask.getAddress());
161 * Given an IP address and a prefix network mask length, it returns
162 * the equivalent subnet prefix IP address
163 * Example: for ip = "172.28.30.254" and maskLen = 25 it will return "172.28.30.128"
165 * @param ip the IP address in InetAddress form
166 * @param maskLen the length of the prefix network mask
167 * @return the subnet prefix IP address in InetAddress form
169 public static InetAddress getSubnetPrefix(InetAddress ip, int maskLen) {
170 int bytes = maskLen / 8;
171 int bits = maskLen % 8;
173 byte[] sn = ip.getAddress();
175 modifiedByte = (byte) (sn[bytes] >> (8 - bits));
176 sn[bytes] = (byte) (modifiedByte << (8 - bits));
179 for (; bytes < sn.length; bytes++) {
180 sn[bytes] = (byte) (0);
183 return InetAddress.getByAddress(sn);
184 } catch (UnknownHostException e) {
190 * Checks if the test address and mask conflicts with
191 * the filter address and mask
194 * testAddress: 172.28.2.23 testMask: 255.255.255.0
195 * filtAddress: 172.28.1.10 testMask: 255.255.255.0
198 * testAddress: 172.28.2.23 testMask: 255.255.255.0
199 * filtAddress: 172.28.1.10 testMask: 255.255.0.0
202 * Null parameters are permitted
205 * @param filterAddress
210 public static boolean inetAddressConflict(InetAddress testAddress,
211 InetAddress filterAddress, InetAddress testMask,
212 InetAddress filterMask) {
214 if ((testAddress == null) || (filterAddress == null)) {
219 if (isAny(testAddress) || isAny(filterAddress)) {
223 int testMaskLen = (testMask != null) ? NetUtils.getSubnetMaskLength(testMask.getAddress()) : 0;
224 int filterMaskLen = (filterMask != null) ? NetUtils.getSubnetMaskLength(filterMask.getAddress()) : 0;
226 int testPrefixLen = (testAddress instanceof Inet6Address) ? (128 - testMaskLen) : (32 - testMaskLen);
227 int filterPrefixLen = (filterAddress instanceof Inet6Address) ? (128 - filterMaskLen) : (32 - filterMaskLen);
229 // Mask length check. Test mask has to be more specific than filter one
230 if (testPrefixLen < filterPrefixLen) {
234 // Subnet Prefix on filter mask length must be the same
235 InetAddress prefix1 = getSubnetPrefix(testAddress, filterPrefixLen);
236 InetAddress prefix2 = getSubnetPrefix(filterAddress, filterPrefixLen);
237 return (!prefix1.equals(prefix2));
241 * Returns true if the passed MAC address is all zero
243 * @param mac the byte array representing the MAC address
244 * @return true if all MAC bytes are zero
246 public static boolean isZeroMAC(byte[] mac) {
247 for (short i = 0; i < 6; i++) {
256 * Returns true if the passed InetAddress contains all zero
258 * @param ip the IP address to test
259 * @return true if the address is all zero
261 public static boolean isAny(InetAddress ip) {
262 for (byte b : ip.getAddress()) {
270 public static boolean fieldsConflict(int field1, int field2) {
271 if ((field1 == 0) || (field2 == 0) || (field1 == field2)) {
277 public static InetAddress parseInetAddress(String addressString) {
278 InetAddress address = null;
280 address = InetAddress.getByName(addressString);
281 } catch (UnknownHostException e) {
288 * Checks if the passed IP v4 address in string form is valid
289 * The address may specify a mask at the end as "/MM"
291 * @param cidr the v4 address as A.B.C.D/MM
294 public static boolean isIPv4AddressValid(String cidr) {
298 String values[] = cidr.split("/");
299 Pattern ipv4Pattern = Pattern
300 .compile("(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])");
301 Matcher mm = ipv4Pattern.matcher(values[0]);
305 if (values.length >= 2) {
306 int prefix = Integer.valueOf(values[1]);
307 if ((prefix < 0) || (prefix > 32)) {
315 * Checks if the passed IP v6 address in string form is valid
316 * The address may specify a mask at the end as "/MMM"
318 * @param cidr the v6 address as A::1/MMM
321 public static boolean isIPv6AddressValid(String cidr) {
325 String values[] = cidr.split("/");
327 //when given an IP address, InetAddress.getByName validates the ip address
328 InetAddress addr = InetAddress.getByName(values[0]);
329 if (!(addr instanceof Inet6Address)) {
332 } catch (UnknownHostException ex) {
336 if (values.length >= 2) {
337 int prefix = Integer.valueOf(values[1]);
338 if ((prefix < 0) || (prefix > 128)) {
346 * Following utilities are useful when you need to
347 * compare or bit shift java primitive type variable
348 * which are inerently signed
351 * Returns the unsigned value of the passed byte variable
353 * @param b the byte value
354 * @return the int variable containing the unsigned byte value
356 public static int getUnsignedByte(byte b) {
357 return (b > 0)? (int)b : ((int)b & 0x7F | 0x80);
361 * Return the unsigned value of the passed short variable
363 * @param s the short value
364 * @return the int variable containing the unsigned short value
366 public static int getUnsignedShort(short s) {
367 return (s > 0)? (int)s : ((int)s & 0x7FFF | 0x8000);