blob: 6af87a733608e3c1b825e21880035fa4d584dbac [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
Jonathan Hartdec62d42014-09-22 15:59:04 -070016package org.onlab.packet;
17
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070018import java.nio.ByteBuffer;
Jonathan Hartdec62d42014-09-22 15:59:04 -070019import java.util.Arrays;
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -070020import java.util.Objects;
21import static com.google.common.base.Preconditions.checkNotNull;
Jonathan Hartdec62d42014-09-22 15:59:04 -070022
23/**
24 * A class representing an IPv4 address.
Jonathan Hartdec62d42014-09-22 15:59:04 -070025 */
Jonathan Hartab63aac2014-10-16 08:52:55 -070026public final class IpAddress implements Comparable<IpAddress> {
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070027 // IP Versions
Jonathan Hartdec62d42014-09-22 15:59:04 -070028 public enum Version { INET, INET6 };
29
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070030 // lengths of address, in bytes
31 public static final int INET_BYTE_LENGTH = 4;
32 public static final int INET_BIT_LENGTH = INET_BYTE_LENGTH * Byte.SIZE;
33 public static final int INET6_BYTE_LENGTH = 16;
34 public static final int INET6_BIT_LENGTH = INET6_BYTE_LENGTH * Byte.SIZE;
Jonathan Hartdec62d42014-09-22 15:59:04 -070035
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070036 private final Version version;
37 private final byte[] octets;
Jonathan Hartdec62d42014-09-22 15:59:04 -070038
39 /**
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070040 * Constructor for given IP address version and address octets.
41 *
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -070042 * @param value the IP address value stored in network byte order
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070043 * (i.e., the most significant byte first)
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -070044 * @param value the IP address value
Jonathan Hartdec62d42014-09-22 15:59:04 -070045 */
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -070046 private IpAddress(Version version, byte[] value) {
47 checkNotNull(value);
48
49 this.version = version;
50 this.octets = Arrays.copyOf(value, INET_BYTE_LENGTH);
Jonathan Hartdec62d42014-09-22 15:59:04 -070051 }
52
53 /**
Jonathan Hartdec62d42014-09-22 15:59:04 -070054 * Converts an integer into an IPv4 address.
55 *
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -070056 * @param value an integer representing an IPv4 value
Jonathan Hartdec62d42014-09-22 15:59:04 -070057 * @return an IP address
58 */
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -070059 public static IpAddress valueOf(int value) {
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070060 byte[] bytes =
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -070061 ByteBuffer.allocate(INET_BYTE_LENGTH).putInt(value).array();
Pavlin Radoslavov52307e62014-10-29 15:07:37 -070062 return new IpAddress(Version.INET, bytes);
Jonathan Hartdec62d42014-09-22 15:59:04 -070063 }
64
65 /**
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -070066 * Converts a byte array into an IP address.
67 *
68 * @param value the IP address value stored in network byte order
69 * (i.e., the most significant byte first)
70 * @return an IP address
71 */
72 public static IpAddress valueOf(byte[] value) {
73 return new IpAddress(Version.INET, value);
74 }
75
76 /**
77 * Converts a byte array and a given offset from the beginning of the
78 * array into an IP address.
79 * <p/>
80 * The IP address is stored in network byte order (i.e., the most
81 * significant byte first).
82 *
83 * @param value the value to use
84 * @param offset the offset in bytes from the beginning of the byte array
85 * @return an IP address
86 */
87 public static IpAddress valueOf(byte[] value, int offset) {
88 // Verify the arguments
89 if ((offset < 0) || (offset + INET_BYTE_LENGTH > value.length)) {
90 String msg;
91 if (value.length < INET_BYTE_LENGTH) {
92 msg = "Invalid IPv4 address array: array length: " +
93 value.length + ". Must be at least " + INET_BYTE_LENGTH;
94 } else {
95 msg = "Invalid IPv4 address array: array offset: " +
96 offset + ". Must be in the interval [0, " +
97 (value.length - INET_BYTE_LENGTH) + "]";
98 }
99 throw new IllegalArgumentException(msg);
100 }
101
102 byte[] bc = Arrays.copyOfRange(value, offset, value.length);
103 return IpAddress.valueOf(bc);
104 }
105
106 /**
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700107 * Converts a dotted-decimal string (x.x.x.x) into an IPv4 address.
Jonathan Hartdec62d42014-09-22 15:59:04 -0700108 *
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700109 * @param address a IP address in string form, e.g. "10.0.0.1".
Jonathan Hartdec62d42014-09-22 15:59:04 -0700110 * @return an IP address
111 */
112 public static IpAddress valueOf(String address) {
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700113 final String[] net = address.split("\\.");
114 if (net.length != INET_BYTE_LENGTH) {
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700115 String msg = "Malformed IPv4 address string; " +
116 "Address must have four decimal values separated by dots (.)";
117 throw new IllegalArgumentException(msg);
Jonathan Hartdec62d42014-09-22 15:59:04 -0700118 }
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700119 final byte[] bytes = new byte[INET_BYTE_LENGTH];
120 for (int i = 0; i < INET_BYTE_LENGTH; i++) {
Jonathan Hartdec62d42014-09-22 15:59:04 -0700121 bytes[i] = (byte) Short.parseShort(net[i], 10);
122 }
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700123 return new IpAddress(Version.INET, bytes);
Jonathan Hartdec62d42014-09-22 15:59:04 -0700124 }
125
126 /**
127 * Returns the IP version of this address.
128 *
129 * @return the version
130 */
131 public Version version() {
132 return this.version;
133 }
134
135 /**
136 * Returns the IP address as a byte array.
137 *
138 * @return a byte array
139 */
140 public byte[] toOctets() {
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700141 return Arrays.copyOf(this.octets, INET_BYTE_LENGTH);
Jonathan Hartdec62d42014-09-22 15:59:04 -0700142 }
143
144 /**
145 * Returns the integral value of this IP address.
146 *
147 * @return the IP address's value as an integer
148 */
149 public int toInt() {
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700150 ByteBuffer bb = ByteBuffer.wrap(octets);
151 return bb.getInt();
Jonathan Hartdec62d42014-09-22 15:59:04 -0700152 }
153
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700154 /**
155 * Creates an IP network mask prefix.
156 *
157 * @param prefixLen the length of the mask prefix. Must be in the interval
158 * [0, 32] for IPv4
159 * @return a new IP address that contains a mask prefix of the
160 * specified length
161 */
162 public static IpAddress makeMaskPrefix(int prefixLen) {
163 // Verify the prefix length
164 if ((prefixLen < 0) || (prefixLen > INET_BIT_LENGTH)) {
165 final String msg = "Invalid IPv4 prefix length: " + prefixLen +
166 ". Must be in the interval [0, 32].";
167 throw new IllegalArgumentException(msg);
168 }
169
170 long v = (0xffffffffL << (INET_BIT_LENGTH - prefixLen)) & 0xffffffffL;
171 return IpAddress.valueOf((int) v);
172 }
173
174 /**
175 * Creates an IP address by masking it with a network mask of given
176 * mask length.
177 *
178 * @param addr the address to mask
179 * @param prefixLen the length of the mask prefix. Must be in the interval
180 * [0, 32] for IPv4
181 * @return a new IP address that is masked with a mask prefix of the
182 * specified length
183 */
184 public static IpAddress makeMaskedAddress(final IpAddress addr,
185 int prefixLen) {
186 IpAddress mask = IpAddress.makeMaskPrefix(prefixLen);
187 byte[] net = new byte[INET_BYTE_LENGTH];
188
189 // Mask each byte
190 for (int i = 0; i < INET_BYTE_LENGTH; i++) {
191 net[i] = (byte) (addr.octets[i] & mask.octets[i]);
192 }
193 return IpAddress.valueOf(net);
194 }
195
Jonathan Hartdec62d42014-09-22 15:59:04 -0700196 @Override
Jonathan Hartab63aac2014-10-16 08:52:55 -0700197 public int compareTo(IpAddress o) {
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700198 Long lv = ((long) this.toInt()) & 0xffffffffL;
199 Long rv = ((long) o.toInt()) & 0xffffffffL;
Jonathan Hartab63aac2014-10-16 08:52:55 -0700200 return lv.compareTo(rv);
201 }
202
203 @Override
Jonathan Hartdec62d42014-09-22 15:59:04 -0700204 public int hashCode() {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700205 return Objects.hash(version, Arrays.hashCode(octets));
Jonathan Hartdec62d42014-09-22 15:59:04 -0700206 }
207
208 @Override
209 public boolean equals(Object obj) {
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700210 if (obj == this) {
Jonathan Hartdec62d42014-09-22 15:59:04 -0700211 return true;
212 }
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700213 if ((obj == null) || (getClass() != obj.getClass())) {
Jonathan Hartdec62d42014-09-22 15:59:04 -0700214 return false;
215 }
216 IpAddress other = (IpAddress) obj;
Pavlin Radoslavov49e159a2014-10-29 16:22:13 -0700217 return (version == other.version) &&
218 Arrays.equals(octets, other.octets);
Jonathan Hartdec62d42014-09-22 15:59:04 -0700219 }
220
221 @Override
222 /*
223 * (non-Javadoc)
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700224 * format is "x.x.x.x" for IPv4 addresses.
Jonathan Hartdec62d42014-09-22 15:59:04 -0700225 *
226 * @see java.lang.Object#toString()
227 */
228 public String toString() {
229 final StringBuilder builder = new StringBuilder();
230 for (final byte b : this.octets) {
231 if (builder.length() > 0) {
232 builder.append(".");
233 }
234 builder.append(String.format("%d", b & 0xff));
235 }
Jonathan Hartdec62d42014-09-22 15:59:04 -0700236 return builder.toString();
237 }
Jonathan Hartdec62d42014-09-22 15:59:04 -0700238}