Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
| 17 | |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 18 | import java.nio.ByteBuffer; |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 19 | import java.util.Arrays; |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 20 | import java.util.Objects; |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * A class representing an IPv4 address. |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 25 | */ |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 26 | public final class IpAddress implements Comparable<IpAddress> { |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 27 | // IP Versions |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 28 | public enum Version { INET, INET6 }; |
| 29 | |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 30 | // 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 Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 35 | |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 36 | private final Version version; |
| 37 | private final byte[] octets; |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 40 | * Constructor for given IP address version and address octets. |
| 41 | * |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 42 | * @param value the IP address value stored in network byte order |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 43 | * (i.e., the most significant byte first) |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 44 | * @param value the IP address value |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 45 | */ |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 46 | private IpAddress(Version version, byte[] value) { |
| 47 | checkNotNull(value); |
| 48 | |
| 49 | this.version = version; |
| 50 | this.octets = Arrays.copyOf(value, INET_BYTE_LENGTH); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 54 | * Converts an integer into an IPv4 address. |
| 55 | * |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 56 | * @param value an integer representing an IPv4 value |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 57 | * @return an IP address |
| 58 | */ |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 59 | public static IpAddress valueOf(int value) { |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 60 | byte[] bytes = |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 61 | ByteBuffer.allocate(INET_BYTE_LENGTH).putInt(value).array(); |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 62 | return new IpAddress(Version.INET, bytes); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 66 | * 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 Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 107 | * Converts a dotted-decimal string (x.x.x.x) into an IPv4 address. |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 108 | * |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 109 | * @param address a IP address in string form, e.g. "10.0.0.1". |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 110 | * @return an IP address |
| 111 | */ |
| 112 | public static IpAddress valueOf(String address) { |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 113 | final String[] net = address.split("\\."); |
| 114 | if (net.length != INET_BYTE_LENGTH) { |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 115 | String msg = "Malformed IPv4 address string; " + |
| 116 | "Address must have four decimal values separated by dots (.)"; |
| 117 | throw new IllegalArgumentException(msg); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 118 | } |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 119 | final byte[] bytes = new byte[INET_BYTE_LENGTH]; |
| 120 | for (int i = 0; i < INET_BYTE_LENGTH; i++) { |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 121 | bytes[i] = (byte) Short.parseShort(net[i], 10); |
| 122 | } |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 123 | return new IpAddress(Version.INET, bytes); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 124 | } |
| 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 Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 141 | return Arrays.copyOf(this.octets, INET_BYTE_LENGTH); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 142 | } |
| 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 Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 150 | ByteBuffer bb = ByteBuffer.wrap(octets); |
| 151 | return bb.getInt(); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 154 | /** |
| 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 Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 196 | @Override |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 197 | public int compareTo(IpAddress o) { |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 198 | Long lv = ((long) this.toInt()) & 0xffffffffL; |
| 199 | Long rv = ((long) o.toInt()) & 0xffffffffL; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 200 | return lv.compareTo(rv); |
| 201 | } |
| 202 | |
| 203 | @Override |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 204 | public int hashCode() { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame^] | 205 | return Objects.hash(version, Arrays.hashCode(octets)); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public boolean equals(Object obj) { |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 210 | if (obj == this) { |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 211 | return true; |
| 212 | } |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 213 | if ((obj == null) || (getClass() != obj.getClass())) { |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 214 | return false; |
| 215 | } |
| 216 | IpAddress other = (IpAddress) obj; |
Pavlin Radoslavov | 49e159a | 2014-10-29 16:22:13 -0700 | [diff] [blame] | 217 | return (version == other.version) && |
| 218 | Arrays.equals(octets, other.octets); |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | @Override |
| 222 | /* |
| 223 | * (non-Javadoc) |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 224 | * format is "x.x.x.x" for IPv4 addresses. |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 225 | * |
| 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 Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 236 | return builder.toString(); |
| 237 | } |
Jonathan Hart | dec62d4 | 2014-09-22 15:59:04 -0700 | [diff] [blame] | 238 | } |