blob: 865fb79d616492bc266e62439d04736863486044 [file] [log] [blame]
alshabib1f44e8e2014-08-14 15:19:57 -07001package org.projectfloodlight.openflow.types;
2
3import java.util.Arrays;
4
5import javax.annotation.Nonnull;
6
7import org.jboss.netty.buffer.ChannelBuffer;
8
9import com.google.common.hash.PrimitiveSink;
10import com.google.common.primitives.UnsignedInts;
11
12
13
14/**
15 * Wrapper around an IPv4Address address
16 *
17 * @author Andreas Wundsam <andreas.wundsam@bigswitch.com>
18 */
19public class IPv4Address extends IPAddress<IPv4Address> {
20 static final int LENGTH = 4;
21 private final int rawValue;
22
23 private static final int NOT_A_CIDR_MASK = -1;
24 private static final int CIDR_MASK_CACHE_UNSET = -2;
25 // Must appear before the static IPv4Address constant assignments
26 private volatile int cidrMaskLengthCache = CIDR_MASK_CACHE_UNSET;
27
28 private final static int NONE_VAL = 0x0;
29 public final static IPv4Address NONE = new IPv4Address(NONE_VAL);
30
31 public static final IPv4Address NO_MASK = IPv4Address.of(0xFFFFFFFF);
32 public static final IPv4Address FULL_MASK = IPv4Address.of(0x00000000);
33
34 private IPv4Address(final int rawValue) {
35 this.rawValue = rawValue;
36 }
37
38 @Override
39 public IPVersion getIpVersion() {
40 return IPVersion.IPv4;
41 }
42
43 private int asCidrMaskLengthInternal() {
44 if (cidrMaskLengthCache == CIDR_MASK_CACHE_UNSET) {
45 // No lock required. We only write cidrMaskLengthCache once
46 int maskint = getInt();
47 if (maskint == 0) {
48 cidrMaskLengthCache = 0;
49 } else if (Integer.bitCount((~maskint) + 1) == 1) {
50 // IP represents a true CIDR prefix length
51 cidrMaskLengthCache = Integer.bitCount(maskint);
52 } else {
53 cidrMaskLengthCache = NOT_A_CIDR_MASK;
54 }
55 }
56 return cidrMaskLengthCache;
57 }
58
59 @Override
60 public boolean isCidrMask() {
61 return asCidrMaskLengthInternal() != NOT_A_CIDR_MASK;
62 }
63
64 @Override
65 public int asCidrMaskLength() {
66 if (!isCidrMask()) {
67 throw new IllegalStateException("IP is not a valid CIDR prefix " +
68 "mask " + toString());
69 } else {
70 return asCidrMaskLengthInternal();
71 }
72 }
73
74 @Override
75 public boolean isBroadcast() {
76 return this.equals(NO_MASK);
77 }
78
79 @Override
80 public IPv4Address and(IPv4Address other) {
81 if (other == null) {
82 throw new NullPointerException("Other IP Address must not be null");
83 }
84 IPv4Address otherIp = (IPv4Address) other;
85 return IPv4Address.of(rawValue & otherIp.rawValue);
86 }
87
88 @Override
89 public IPv4Address or(IPv4Address other) {
90 if (other == null) {
91 throw new NullPointerException("Other IP Address must not be null");
92 }
93 IPv4Address otherIp = (IPv4Address) other;
94 return IPv4Address.of(rawValue | otherIp.rawValue);
95 }
96
97 @Override
98 public IPv4Address not() {
99 return IPv4Address.of(~rawValue);
100 }
101
102 public static IPv4Address of(final byte[] address) {
103 if (address == null) {
104 throw new NullPointerException("Address must not be null");
105 }
106 if (address.length != LENGTH) {
107 throw new IllegalArgumentException(
108 "Invalid byte array length for IPv4Address address: " + address.length);
109 }
110
111 int raw =
112 (address[0] & 0xFF) << 24 | (address[1] & 0xFF) << 16
113 | (address[2] & 0xFF) << 8 | (address[3] & 0xFF) << 0;
114 return IPv4Address.of(raw);
115 }
116
117 /** construct an IPv4Address from a 32-bit integer value.
118 *
119 * @param raw the IPAdress represented as a 32-bit integer
120 * @return the constructed IPv4Address
121 */
122 public static IPv4Address of(final int raw) {
123 if(raw == NONE_VAL)
124 return NONE;
125 return new IPv4Address(raw);
126 }
127
128 /** parse an IPv4Address from the canonical dotted-quad representation
129 * (1.2.3.4).
130 *
131 * @param string an IPv4 address in dotted-quad representation
132 * @return the parsed IPv4 address
133 * @throws NullPointerException if string is null
134 * @throws IllegalArgumentException if string is not a valid IPv4Address
135 */
136 @Nonnull
137 public static IPv4Address of(@Nonnull final String string) throws IllegalArgumentException {
138 if (string == null) {
139 throw new NullPointerException("String must not be null");
140 }
141 int start = 0;
142 int shift = 24;
143
144 int raw = 0;
145 while (shift >= 0) {
146 int end = string.indexOf('.', start);
147 if (end == start || !((shift > 0) ^ (end < 0)))
148 throw new IllegalArgumentException("IP Address not well formed: " + string);
149
150 String substr =
151 end > 0 ? string.substring(start, end) : string.substring(start);
152 int val = Integer.parseInt(substr);
153 if (val < 0 || val > 255)
154 throw new IllegalArgumentException("IP Address not well formed: " + string);
155
156 raw |= val << shift;
157
158 shift -= 8;
159 start = end + 1;
160 }
161 return IPv4Address.of(raw);
162 }
163
164 public int getInt() {
165 return rawValue;
166 }
167
168 private volatile byte[] bytesCache = null;
169
170 public byte[] getBytes() {
171 if (bytesCache == null) {
172 synchronized (this) {
173 if (bytesCache == null) {
174 bytesCache =
175 new byte[] { (byte) ((rawValue >>> 24) & 0xFF),
176 (byte) ((rawValue >>> 16) & 0xFF),
177 (byte) ((rawValue >>> 8) & 0xFF),
178 (byte) ((rawValue >>> 0) & 0xFF) };
179 }
180 }
181 }
182 return Arrays.copyOf(bytesCache, bytesCache.length);
183 }
184
185 @Override
186 public int getLength() {
187 return LENGTH;
188 }
189
190 @Override
191 public String toString() {
192 StringBuilder res = new StringBuilder();
193 res.append((rawValue >> 24) & 0xFF).append('.');
194 res.append((rawValue >> 16) & 0xFF).append('.');
195 res.append((rawValue >> 8) & 0xFF).append('.');
196 res.append((rawValue >> 0) & 0xFF);
197 return res.toString();
198 }
199
200 public void write4Bytes(ChannelBuffer c) {
201 c.writeInt(rawValue);
202 }
203
204 public static IPv4Address read4Bytes(ChannelBuffer c) {
205 return IPv4Address.of(c.readInt());
206 }
207
208 @Override
209 public IPv4Address applyMask(IPv4Address mask) {
210 return and(mask);
211 }
212
213 @Override
214 public int hashCode() {
215 final int prime = 31;
216 int result = 1;
217 result = prime * result + rawValue;
218 return result;
219 }
220
221 @Override
222 public boolean equals(Object obj) {
223 if (this == obj)
224 return true;
225 if (obj == null)
226 return false;
227 if (getClass() != obj.getClass())
228 return false;
229 IPv4Address other = (IPv4Address) obj;
230 if (rawValue != other.rawValue)
231 return false;
232 return true;
233 }
234
235 @Override
236 public int compareTo(IPv4Address o) {
237 return UnsignedInts.compare(rawValue, o.rawValue);
238 }
239
240 @Override
241 public void putTo(PrimitiveSink sink) {
242 sink.putInt(rawValue);
243 }
244
245}