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 * Constant holding the number of bytes in MAC Address
34 public static final int MACAddrLengthInBytes = 6;
37 * Constant holding the number of words in MAC Address
39 public static final int MACAddrLengthInWords = 3;
42 * Constant holding the broadcast MAC address
44 public static byte[] BroadcastMACAddr = {-1, -1, -1, -1, -1, -1};
47 * Converts a 4 bytes array into an integer number
50 * the 4 bytes long byte array
51 * @return the integer number
53 public static int byteArray4ToInt(byte[] ba) {
54 if (ba == null || ba.length != 4) {
57 return (0xff & ba[0]) << 24 | (0xff & ba[1]) << 16 | (0xff & ba[2]) << 8 | (0xff & ba[3]);
61 * Converts a long to 6 bytes array for mac addresses
66 public static byte[] longToByteArray6(long addr){
67 byte[] mac = new byte[6];
68 for(int i = 0; i < 6; i++){
69 mac[i] = (byte) (addr >> (i*8));
76 * Converts an integer number into a 4 bytes array
80 * @return the byte array
82 public static byte[] intToByteArray4(int i) {
83 return new byte[] { (byte) ((i >> 24) & 0xff), (byte) ((i >> 16) & 0xff), (byte) ((i >> 8) & 0xff),
88 * Converts an IP address passed as integer value into the respective
92 * the IP address in integer form
93 * @return the IP address in InetAddress form
95 public static InetAddress getInetAddress(int address) {
96 InetAddress ip = null;
98 ip = InetAddress.getByAddress(NetUtils.intToByteArray4(address));
99 } catch (UnknownHostException e) {
106 * Return the InetAddress Network Mask given the length of the prefix bit
107 * mask. The prefix bit mask indicates the contiguous leading bits that are
108 * NOT masked out. Example: A prefix bit mask length of 8 will give an
109 * InetAddress Network Mask of 255.0.0.0
111 * @param prefixMaskLength
112 * integer representing the length of the prefix network mask
114 * boolean representing the IP version of the returned address
117 public static InetAddress getInetNetworkMask(int prefixMaskLength, boolean isV6) {
118 if (prefixMaskLength < 0 || (!isV6 && prefixMaskLength > 32) || (isV6 && prefixMaskLength > 128)) {
121 byte v4Address[] = { 0, 0, 0, 0 };
122 byte v6Address[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
123 byte address[] = (isV6) ? v6Address : v4Address;
124 int numBytes = prefixMaskLength / 8;
125 int numBits = prefixMaskLength % 8;
127 for (; i < numBytes; i++) {
128 address[i] = (byte) 0xff;
132 for (int j = 0; j < numBits; j++) {
135 address[i] = (byte) rem;
139 return InetAddress.getByAddress(address);
140 } catch (UnknownHostException e) {
147 * Returns the number of contiguous bits belonging to the subnet, that have
148 * to be masked out Example: A prefix network byte mask of ff.ff.ff.00 will
149 * give a subnet mask length of 8, while ff.00.00.00 will return a subnet
150 * mask length of 24. If the passed prefixMask object is null, 0 is returned
153 * the prefix mask as byte array
154 * @return the length of the prefix network mask
156 public static int getSubnetMaskLength(byte[] prefixMask) {
158 if (prefixMask != null) {
161 int numBytes = prefixMask.length;
162 for (int i = 0; i < numBytes; i++) {
163 intMask |= (prefixMask[i] & 0xff) << (8 * (numBytes - 1 - i));
167 while (((intMask & bit) == 0) && (maskLength <= (numBytes * 8))) {
176 * Returns the number of contiguous bits belonging to the subnet, that have
177 * to be masked out Example: A prefix network byte mask of ff.ff.ff.00 will
178 * give a subnet mask length of 8, while ff.00.00.00 will return a subnet
179 * mask length of 24 If the passed prefixMask object is null, 0 is returned
182 * the prefix mask as InetAddress
183 * @return the length of the prefix network mask
185 public static int getSubnetMaskLength(InetAddress prefixMask) {
186 return (prefixMask == null) ? 0 : NetUtils.getSubnetMaskLength(prefixMask.getAddress());
190 * Given an IP address and a prefix network mask length, it returns the
191 * equivalent subnet prefix IP address Example: for ip = "172.28.30.254" and
192 * maskLen = 25 it will return "172.28.30.128"
195 * the IP address in InetAddress form
197 * the length of the prefix network mask
198 * @return the subnet prefix IP address in InetAddress form
200 public static InetAddress getSubnetPrefix(InetAddress ip, int maskLen) {
201 int bytes = maskLen / 8;
202 int bits = maskLen % 8;
204 byte[] sn = ip.getAddress();
206 modifiedByte = (byte) (sn[bytes] >> (8 - bits));
207 sn[bytes] = (byte) (modifiedByte << (8 - bits));
210 for (; bytes < sn.length; bytes++) {
211 sn[bytes] = (byte) (0);
214 return InetAddress.getByAddress(sn);
215 } catch (UnknownHostException e) {
221 * Checks if the test address and mask conflicts with the filter address and
224 * For example: testAddress: 172.28.2.23 testMask: 255.255.255.0
225 * filtAddress: 172.28.1.10 testMask: 255.255.255.0 conflict
227 * testAddress: 172.28.2.23 testMask: 255.255.255.0 filtAddress: 172.28.1.10
228 * testMask: 255.255.0.0 do not conflict
230 * Null parameters are permitted
233 * @param filterAddress
238 public static boolean inetAddressConflict(InetAddress testAddress, InetAddress filterAddress, InetAddress testMask,
239 InetAddress filterMask) {
241 if ((testAddress == null) || (filterAddress == null)) {
246 if (isAny(testAddress) || isAny(filterAddress)) {
250 int testMaskLen = (testMask != null) ? NetUtils.getSubnetMaskLength(testMask.getAddress()) : 0;
251 int filterMaskLen = (filterMask != null) ? NetUtils.getSubnetMaskLength(filterMask.getAddress()) : 0;
253 int testPrefixLen = (testAddress instanceof Inet6Address) ? (128 - testMaskLen) : (32 - testMaskLen);
254 int filterPrefixLen = (filterAddress instanceof Inet6Address) ? (128 - filterMaskLen) : (32 - filterMaskLen);
256 // Mask length check. Test mask has to be more specific than filter one
257 if (testPrefixLen < filterPrefixLen) {
261 // Subnet Prefix on filter mask length must be the same
262 InetAddress prefix1 = getSubnetPrefix(testAddress, filterPrefixLen);
263 InetAddress prefix2 = getSubnetPrefix(filterAddress, filterPrefixLen);
264 return (!prefix1.equals(prefix2));
268 * Returns true if the passed MAC address is all zero
271 * the byte array representing the MAC address
272 * @return true if all MAC bytes are zero
274 public static boolean isZeroMAC(byte[] mac) {
275 for (short i = 0; i < 6; i++) {
284 * Returns true if the MAC address is the broadcast MAC address and false
290 public static boolean isBroadcastMACAddr(byte[] MACAddress) {
291 if (MACAddress.length == MACAddrLengthInBytes) {
292 for (int i = 0; i < 6; i++) {
293 if (MACAddress[i] != BroadcastMACAddr[i]) {
304 * Returns true if the MAC address is a multicast MAC address and false
305 * otherwise. Note that this explicitly returns false for the broadcast MAC
311 public static boolean isMulticastMACAddr(byte[] MACAddress) {
312 if (MACAddress.length == MACAddrLengthInBytes && !isBroadcastMACAddr(MACAddress)) {
313 if (MACAddress[0] % 2 == 1) {
321 * Returns true if the passed InetAddress contains all zero
324 * the IP address to test
325 * @return true if the address is all zero
327 public static boolean isAny(InetAddress ip) {
328 for (byte b : ip.getAddress()) {
336 public static boolean fieldsConflict(int field1, int field2) {
337 if ((field1 == 0) || (field2 == 0) || (field1 == field2)) {
343 public static InetAddress parseInetAddress(String addressString) {
344 InetAddress address = null;
346 address = InetAddress.getByName(addressString);
347 } catch (UnknownHostException e) {
354 * Checks if the passed IP v4 address in string form is valid The address
355 * may specify a mask at the end as "/MM"
358 * the v4 address as A.B.C.D/MM
361 public static boolean isIPv4AddressValid(String cidr) {
366 String values[] = cidr.split("/");
367 Pattern ipv4Pattern = Pattern
368 .compile("(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])");
369 Matcher mm = ipv4Pattern.matcher(values[0]);
373 if (values.length >= 2) {
374 int prefix = Integer.valueOf(values[1]);
375 if ((prefix < 0) || (prefix > 32)) {
383 * Checks if the passed IP v6 address in string form is valid The address
384 * may specify a mask at the end as "/MMM"
387 * the v6 address as A::1/MMM
390 public static boolean isIPv6AddressValid(String cidr) {
395 String values[] = cidr.split("/");
397 // when given an IP address, InetAddress.getByName validates the ip
399 InetAddress addr = InetAddress.getByName(values[0]);
400 if (!(addr instanceof Inet6Address)) {
403 } catch (UnknownHostException ex) {
407 if (values.length >= 2) {
408 int prefix = Integer.valueOf(values[1]);
409 if ((prefix < 0) || (prefix > 128)) {
417 * Checks if the passed IP address in string form is a valid v4 or v6
418 * address. The address may specify a mask at the end as "/MMM"
421 * the v4 or v6 address as IP/MMM
424 public static boolean isIPAddressValid(String cidr) {
425 return NetUtils.isIPv4AddressValid(cidr) || NetUtils.isIPv6AddressValid(cidr);
429 * Following utilities are useful when you need to compare or bit shift java
430 * primitive type variable which are inerently signed
433 * Returns the unsigned value of the passed byte variable
437 * @return the int variable containing the unsigned byte value
439 public static int getUnsignedByte(byte b) {
440 return (b > 0) ? (int) b : (b & 0x7F | 0x80);
444 * Return the unsigned value of the passed short variable
448 * @return the int variable containing the unsigned short value
450 public static int getUnsignedShort(short s) {
451 return (s > 0) ? (int) s : (s & 0x7FFF | 0x8000);
455 * Returns the highest v4 or v6 InetAddress
458 * true for IPv6, false for Ipv4
459 * @return The highest IPv4 or IPv6 address
461 public static InetAddress gethighestIP(boolean v6) {
463 return (v6) ? InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") : InetAddress
464 .getByName("255.255.255.255");
465 } catch (UnknownHostException e) {