blob: ac5a741a37ed8aeef27260beae7ed45f4e6853d5 [file] [log] [blame]
Andrea Campanella01e886e2017-12-15 15:27:31 +01001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.t3.impl;
18
19import java.math.BigInteger;
20import java.net.InetAddress;
21import java.net.UnknownHostException;
22
23/**
24 * Utility class to test if an Ip is in a given subnet.
25 */
26public class Subnet {
27 private final int bytesSubnetCount;
28 private final BigInteger bigMask;
29 private final BigInteger bigSubnetMasked;
30
31 /**
32 * Constructor for use via format "192.168.0.0/24" or "2001:db8:85a3:880:0:0:0:0/57".
33 * @param subnetAddress the address
34 * @param bits the mask
35 */
36 public Subnet(InetAddress subnetAddress, int bits) {
37 bytesSubnetCount = subnetAddress.getAddress().length; // 4 or 16
38 bigMask = BigInteger.valueOf(-1).shiftLeft(bytesSubnetCount * 8 - bits); // mask = -1 << 32 - bits
39 bigSubnetMasked = new BigInteger(subnetAddress.getAddress()).and(bigMask);
40 }
41
42 /**
43 * Constructor for use via format "192.168.0.0/255.255.255.0" or single address.
44 * @param subnetAddress the address
45 * @param mask the mask
46 */
47 public Subnet(InetAddress subnetAddress, InetAddress mask) {
48 bytesSubnetCount = subnetAddress.getAddress().length;
49 // no mask given case is handled here.
50 bigMask = null == mask ? BigInteger.valueOf(-1) : new BigInteger(mask.getAddress());
51 bigSubnetMasked = new BigInteger(subnetAddress.getAddress()).and(bigMask);
52 }
53
54 /**
55 * Subnet factory method.
56 *
57 * @param subnetMask format: "192.168.0.0/24" or "192.168.0.0/255.255.255.0"
58 * or single address or "2001:db8:85a3:880:0:0:0:0/57"
59 * @return a new instance
60 * @throws UnknownHostException thrown if unsupported subnet mask.
61 */
62 public static Subnet createInstance(String subnetMask)
63 throws UnknownHostException {
64 final String[] stringArr = subnetMask.split("/");
65 if (2 > stringArr.length) {
66 return new Subnet(InetAddress.getByName(stringArr[0]), (InetAddress) null);
67 } else if (stringArr[1].contains(".") || stringArr[1].contains(":")) {
68 return new Subnet(InetAddress.getByName(stringArr[0]), InetAddress.getByName(stringArr[1]));
69 } else {
70 return new Subnet(InetAddress.getByName(stringArr[0]), Integer.parseInt(stringArr[1]));
71 }
72 }
73
74 /**
75 * Tests if the address is in the given subnet.
76 * @param address the address to test.
77 * @return true if inside the subnet
78 */
79 public boolean isInSubnet(InetAddress address) {
80 byte[] bytesAddress = address.getAddress();
81 if (this.bytesSubnetCount != bytesAddress.length) {
82 return false;
83 }
84 BigInteger bigAddress = new BigInteger(bytesAddress);
85 return bigAddress.and(this.bigMask).equals(this.bigSubnetMasked);
86 }
87
88 @Override
89 public final boolean equals(Object obj) {
90 if (!(obj instanceof Subnet)) {
91 return false;
92 }
93 final Subnet other = (Subnet) obj;
94 return bigSubnetMasked.equals(other.bigSubnetMasked) &&
95 bigMask.equals(other.bigMask) &&
96 bytesSubnetCount == other.bytesSubnetCount;
97 }
98
99 @Override
100 public final int hashCode() {
101 return bytesSubnetCount;
102 }
103
104 @Override
105 public String toString() {
106 final StringBuilder buf = new StringBuilder();
107 bigInteger2IpString(buf, bigSubnetMasked, bytesSubnetCount);
108 buf.append('/');
109 bigInteger2IpString(buf, bigMask, bytesSubnetCount);
110 return buf.toString();
111 }
112
113 private void bigInteger2IpString(StringBuilder buf, BigInteger bigInteger, int displayBytes) {
114 boolean isIPv4 = 4 == displayBytes;
115 byte[] bytes = bigInteger.toByteArray();
116 int diffLen = displayBytes - bytes.length;
117 byte fillByte = 0 > (int) bytes[0] ? (byte) 0xFF : (byte) 0x00;
118
119 int integer;
120 for (int i = 0; i < displayBytes; i++) {
121 if (0 < i && !isIPv4 && i % 2 == 0) {
122 buf.append(':');
123 } else if (0 < i && isIPv4) {
124 buf.append('.');
125 }
126 integer = 0xFF & (i < diffLen ? fillByte : bytes[i - diffLen]);
127 if (!isIPv4 && 0x10 > integer) {
128 buf.append('0');
129 }
130 buf.append(isIPv4 ? integer : Integer.toHexString(integer));
131 }
132 }
133}