Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 3 | import java.net.Inet4Address; |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 4 | import java.net.InetAddress; |
Andreas Wundsam | 5f71b41 | 2014-02-18 12:56:35 -0800 | [diff] [blame] | 5 | import java.util.Arrays; |
| 6 | |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 7 | import javax.annotation.Nonnull; |
| 8 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 9 | import org.jboss.netty.buffer.ChannelBuffer; |
| 10 | |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 11 | import com.google.common.base.Preconditions; |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 12 | import com.google.common.hash.PrimitiveSink; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 13 | import com.google.common.primitives.UnsignedInts; |
| 14 | |
Byungjoon Lee | ba92e29 | 2014-07-18 13:13:26 +0900 | [diff] [blame] | 15 | import org.projectfloodlight.openflow.protocol.Writeable; |
| 16 | import org.projectfloodlight.openflow.protocol.OFMessageReader; |
| 17 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 18 | |
| 19 | /** |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 20 | * Wrapper around an IPv4Address address |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 21 | * |
kjwon15 | 7bc8540 | 2015-02-12 15:07:42 +0900 | [diff] [blame] | 22 | * @author Andreas Wundsam {@literal <}andreas.wundsam@bigswitch.com{@literal >} |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 23 | */ |
Byungjoon Lee | ba92e29 | 2014-07-18 13:13:26 +0900 | [diff] [blame] | 24 | public class IPv4Address extends IPAddress<IPv4Address> implements Writeable { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 25 | static final int LENGTH = 4; |
| 26 | private final int rawValue; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 27 | |
Gregor Maier | 1acb450 | 2013-12-12 11:25:07 -0800 | [diff] [blame] | 28 | private static final int NOT_A_CIDR_MASK = -1; |
| 29 | private static final int CIDR_MASK_CACHE_UNSET = -2; |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 30 | // Must appear before the static IPv4Address constant assignments |
| 31 | private volatile int cidrMaskLengthCache = CIDR_MASK_CACHE_UNSET; |
| 32 | |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 33 | private final static int NONE_VAL = 0x0; |
| 34 | public final static IPv4Address NONE = new IPv4Address(NONE_VAL); |
| 35 | |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 36 | public static final IPv4Address NO_MASK = IPv4Address.of(0xFFFFFFFF); |
| 37 | public static final IPv4Address FULL_MASK = IPv4Address.of(0x00000000); |
| 38 | |
| 39 | private IPv4Address(final int rawValue) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 40 | this.rawValue = rawValue; |
| 41 | } |
| 42 | |
Byungjoon Lee | ba92e29 | 2014-07-18 13:13:26 +0900 | [diff] [blame] | 43 | public final static Reader READER = new Reader(); |
| 44 | |
| 45 | private static class Reader implements OFMessageReader<IPv4Address> { |
| 46 | @Override |
| 47 | public IPv4Address readFrom(ChannelBuffer bb) throws OFParseError { |
| 48 | return new IPv4Address(bb.readInt()); |
| 49 | } |
| 50 | } |
| 51 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 52 | @Override |
Yotam Harchol | eb023dc | 2013-09-26 15:45:44 -0700 | [diff] [blame] | 53 | public IPVersion getIpVersion() { |
| 54 | return IPVersion.IPv4; |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 57 | private int asCidrMaskLengthInternal() { |
| 58 | if (cidrMaskLengthCache == CIDR_MASK_CACHE_UNSET) { |
| 59 | // No lock required. We only write cidrMaskLengthCache once |
| 60 | int maskint = getInt(); |
| 61 | if (maskint == 0) { |
| 62 | cidrMaskLengthCache = 0; |
| 63 | } else if (Integer.bitCount((~maskint) + 1) == 1) { |
| 64 | // IP represents a true CIDR prefix length |
| 65 | cidrMaskLengthCache = Integer.bitCount(maskint); |
| 66 | } else { |
| 67 | cidrMaskLengthCache = NOT_A_CIDR_MASK; |
| 68 | } |
| 69 | } |
| 70 | return cidrMaskLengthCache; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public boolean isCidrMask() { |
| 75 | return asCidrMaskLengthInternal() != NOT_A_CIDR_MASK; |
| 76 | } |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 77 | |
| 78 | @Override |
| 79 | public int asCidrMaskLength() { |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 80 | if (!isCidrMask()) { |
| 81 | throw new IllegalStateException("IP is not a valid CIDR prefix " + |
| 82 | "mask " + toString()); |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 83 | } else { |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 84 | return asCidrMaskLengthInternal(); |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 88 | @Override |
| 89 | public boolean isBroadcast() { |
| 90 | return this.equals(NO_MASK); |
| 91 | } |
| 92 | |
nsong | d7701e1 | 2015-05-13 10:34:26 -0700 | [diff] [blame] | 93 | /** |
| 94 | * IPv4 multicast addresses are defined by the leading address bits of 1110 |
| 95 | */ |
| 96 | @Override |
| 97 | public boolean isMulticast() { |
nsong | 729900b | 2015-05-13 10:44:48 -0700 | [diff] [blame] | 98 | return ((rawValue >>> 24) & 0xF0) == 0xE0; |
nsong | d7701e1 | 2015-05-13 10:34:26 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 101 | @Override |
Aditya Vaja | 98c96e7 | 2014-03-11 15:19:01 -0700 | [diff] [blame] | 102 | public IPv4Address and(IPv4Address other) { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 103 | Preconditions.checkNotNull(other, "other must not be null"); |
| 104 | |
| 105 | IPv4Address otherIp = other; |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 106 | return IPv4Address.of(rawValue & otherIp.rawValue); |
| 107 | } |
| 108 | |
| 109 | @Override |
Aditya Vaja | 98c96e7 | 2014-03-11 15:19:01 -0700 | [diff] [blame] | 110 | public IPv4Address or(IPv4Address other) { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 111 | Preconditions.checkNotNull(other, "other must not be null"); |
| 112 | |
| 113 | IPv4Address otherIp = other; |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 114 | return IPv4Address.of(rawValue | otherIp.rawValue); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public IPv4Address not() { |
| 119 | return IPv4Address.of(~rawValue); |
| 120 | } |
| 121 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 122 | /** |
| 123 | * Returns an {@code IPv4Address} object that represents the given |
Ronald Li | 097cda5 | 2014-07-06 23:17:05 -0700 | [diff] [blame] | 124 | * IP address. The argument is in network byte order: the highest |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 125 | * order byte of the address is in {@code address[0]}. |
| 126 | * <p> |
| 127 | * The address byte array must be 4 bytes long (32 bits long). |
| 128 | * <p> |
| 129 | * Similar to {@link InetAddress#getByAddress(byte[])}. |
| 130 | * |
| 131 | * @param address the raw IP address in network byte order |
| 132 | * @return an {@code IPv4Address} object that represents the given |
| 133 | * raw IP address |
| 134 | * @throws NullPointerException if the given address was {@code null} |
| 135 | * @throws IllegalArgumentException if the given address was of an invalid |
| 136 | * byte array length |
| 137 | * @see InetAddress#getByAddress(byte[]) |
| 138 | */ |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 139 | @Nonnull |
| 140 | public static IPv4Address of(@Nonnull final byte[] address) { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 141 | Preconditions.checkNotNull(address, "address must not be null"); |
| 142 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 143 | if (address.length != LENGTH) { |
| 144 | throw new IllegalArgumentException( |
Ronald Li | 74b54bc | 2014-07-06 23:59:13 -0700 | [diff] [blame] | 145 | "Invalid byte array length for IPv4 address: " + address.length); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | int raw = |
| 149 | (address[0] & 0xFF) << 24 | (address[1] & 0xFF) << 16 |
| 150 | | (address[2] & 0xFF) << 8 | (address[3] & 0xFF) << 0; |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 151 | return IPv4Address.of(raw); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 154 | /** |
| 155 | * Returns an {@code IPv4Address} object that represents the given |
Ronald Li | 097cda5 | 2014-07-06 23:17:05 -0700 | [diff] [blame] | 156 | * IP address. |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 157 | * |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 158 | * @param raw the raw IP address represented as a 32-bit integer |
| 159 | * @return an {@code IPv4Address} object that represents the given |
| 160 | * raw IP address |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 161 | */ |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 162 | @Nonnull |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 163 | public static IPv4Address of(final int raw) { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 164 | if(raw == NONE_VAL) |
| 165 | return NONE; |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 166 | return new IPv4Address(raw); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 169 | /** |
| 170 | * Returns an {@code IPv4Address} object that represents the given |
Ronald Li | 5b41da9 | 2014-07-06 22:59:28 -0700 | [diff] [blame] | 171 | * IP address. The argument is in the canonical quad-dotted notation. |
| 172 | * For example, {@code 1.2.3.4}. |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 173 | * |
Ronald Li | 5b41da9 | 2014-07-06 22:59:28 -0700 | [diff] [blame] | 174 | * @param string the IP address in the canonical quad-dotted notation |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 175 | * @return an {@code IPv4Address} object that represents the given |
Ronald Li | 5b41da9 | 2014-07-06 22:59:28 -0700 | [diff] [blame] | 176 | * IP address |
Ronald Li | 097cda5 | 2014-07-06 23:17:05 -0700 | [diff] [blame] | 177 | * @throws NullPointerException if the given string was {@code null} |
| 178 | * @throws IllegalArgumentException if the given string was not a valid |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 179 | * IPv4 address |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 180 | */ |
| 181 | @Nonnull |
| 182 | public static IPv4Address of(@Nonnull final String string) throws IllegalArgumentException { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 183 | Preconditions.checkNotNull(string, "string must not be null"); |
| 184 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 185 | int start = 0; |
| 186 | int shift = 24; |
| 187 | |
| 188 | int raw = 0; |
| 189 | while (shift >= 0) { |
| 190 | int end = string.indexOf('.', start); |
| 191 | if (end == start || !((shift > 0) ^ (end < 0))) |
| 192 | throw new IllegalArgumentException("IP Address not well formed: " + string); |
| 193 | |
| 194 | String substr = |
| 195 | end > 0 ? string.substring(start, end) : string.substring(start); |
| 196 | int val = Integer.parseInt(substr); |
| 197 | if (val < 0 || val > 255) |
| 198 | throw new IllegalArgumentException("IP Address not well formed: " + string); |
| 199 | |
| 200 | raw |= val << shift; |
| 201 | |
| 202 | shift -= 8; |
| 203 | start = end + 1; |
| 204 | } |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 205 | return IPv4Address.of(raw); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 208 | /** |
Ronald Li | 5b41da9 | 2014-07-06 22:59:28 -0700 | [diff] [blame] | 209 | * Returns an {@code IPv4Address} object that represents the given |
| 210 | * IP address. The argument is given as an {@code Inet4Address} object. |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 211 | * |
Ronald Li | 382589f | 2014-07-06 23:58:08 -0700 | [diff] [blame] | 212 | * @param address the IP address as an {@code Inet4Address} object |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 213 | * @return an {@code IPv4Address} object that represents the |
| 214 | * given IP address |
Ronald Li | 5b41da9 | 2014-07-06 22:59:28 -0700 | [diff] [blame] | 215 | * @throws NullPointerException if the given {@code Inet4Address} was |
| 216 | * {@code null} |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 217 | */ |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 218 | @Nonnull |
| 219 | public static IPv4Address of(@Nonnull final Inet4Address address) { |
| 220 | Preconditions.checkNotNull(address, "address must not be null"); |
| 221 | return IPv4Address.of(address.getAddress()); |
| 222 | } |
| 223 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 224 | /** |
| 225 | * Returns an {@code IPv4Address} object that represents the |
Ronald Li | c3c4a95 | 2014-07-07 01:01:46 -0700 | [diff] [blame] | 226 | * CIDR subnet mask of the given prefix length. |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 227 | * |
Ronald Li | c3c4a95 | 2014-07-07 01:01:46 -0700 | [diff] [blame] | 228 | * @param cidrMaskLength the prefix length of the CIDR subnet mask |
| 229 | * (i.e. the number of leading one-bits), |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 230 | * where {@code 0 <= cidrMaskLength <= 32} |
| 231 | * @return an {@code IPv4Address} object that represents the |
| 232 | * CIDR subnet mask of the given prefix length |
| 233 | * @throws IllegalArgumentException if the given prefix length was invalid |
| 234 | */ |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 235 | @Nonnull |
Ronald Li | a748422 | 2014-07-03 17:14:12 -0700 | [diff] [blame] | 236 | public static IPv4Address ofCidrMaskLength(final int cidrMaskLength) { |
| 237 | Preconditions.checkArgument( |
| 238 | cidrMaskLength >= 0 && cidrMaskLength <= 32, |
| 239 | "Invalid IPv4 CIDR mask length: %s", cidrMaskLength); |
| 240 | |
| 241 | if (cidrMaskLength == 32) { |
| 242 | return IPv4Address.NO_MASK; |
| 243 | } else if (cidrMaskLength == 0) { |
| 244 | return IPv4Address.FULL_MASK; |
| 245 | } else { |
| 246 | int mask = (-1) << (32 - cidrMaskLength); |
| 247 | return IPv4Address.of(mask); |
| 248 | } |
| 249 | } |
| 250 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 251 | /** |
| 252 | * Returns an {@code IPv4AddressWithMask} object that represents this |
Ronald Li | ca08d5f | 2014-07-05 02:43:18 -0700 | [diff] [blame] | 253 | * IP address masked by the given IP address mask. |
| 254 | * |
| 255 | * @param mask the {@code IPv4Address} object that represents the mask |
| 256 | * @return an {@code IPv4AddressWithMask} object that represents this |
| 257 | * IP address masked by the given mask |
| 258 | * @throws NullPointerException if the given mask was {@code null} |
| 259 | */ |
| 260 | @Nonnull |
Ronald Li | bbf0194 | 2014-07-07 17:00:13 -0700 | [diff] [blame] | 261 | @Override |
Ronald Li | ca08d5f | 2014-07-05 02:43:18 -0700 | [diff] [blame] | 262 | public IPv4AddressWithMask withMask(@Nonnull final IPv4Address mask) { |
| 263 | return IPv4AddressWithMask.of(this, mask); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Returns an {@code IPv4AddressWithMask} object that represents this |
Ronald Li | 383d14e | 2014-07-07 01:09:18 -0700 | [diff] [blame] | 268 | * IP address masked by the CIDR subnet mask of the given prefix length. |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 269 | * |
Ronald Li | 383d14e | 2014-07-07 01:09:18 -0700 | [diff] [blame] | 270 | * @param cidrMaskLength the prefix length of the CIDR subnet mask |
| 271 | * (i.e. the number of leading one-bits), |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 272 | * where {@code 0 <= cidrMaskLength <= 32} |
| 273 | * @return an {@code IPv4AddressWithMask} object that |
| 274 | * represents this IP address masked by the CIDR |
| 275 | * subnet mask of the given prefix length |
| 276 | * @throws IllegalArgumentException if the given prefix length was invalid |
| 277 | * @see #ofCidrMaskLength(int) |
| 278 | */ |
| 279 | @Nonnull |
Ronald Li | bbf0194 | 2014-07-07 17:00:13 -0700 | [diff] [blame] | 280 | @Override |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 281 | public IPv4AddressWithMask withMaskOfLength(final int cidrMaskLength) { |
Ronald Li | bbf0194 | 2014-07-07 17:00:13 -0700 | [diff] [blame] | 282 | return this.withMask(IPv4Address.ofCidrMaskLength(cidrMaskLength)); |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 285 | public int getInt() { |
| 286 | return rawValue; |
| 287 | } |
| 288 | |
Andreas Wundsam | 4e2469e | 2014-02-17 15:32:43 -0800 | [diff] [blame] | 289 | private volatile byte[] bytesCache = null; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 290 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 291 | @Override |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 292 | public byte[] getBytes() { |
| 293 | if (bytesCache == null) { |
| 294 | synchronized (this) { |
| 295 | if (bytesCache == null) { |
| 296 | bytesCache = |
| 297 | new byte[] { (byte) ((rawValue >>> 24) & 0xFF), |
| 298 | (byte) ((rawValue >>> 16) & 0xFF), |
| 299 | (byte) ((rawValue >>> 8) & 0xFF), |
| 300 | (byte) ((rawValue >>> 0) & 0xFF) }; |
| 301 | } |
| 302 | } |
| 303 | } |
Andreas Wundsam | 5f71b41 | 2014-02-18 12:56:35 -0800 | [diff] [blame] | 304 | return Arrays.copyOf(bytesCache, bytesCache.length); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public int getLength() { |
| 309 | return LENGTH; |
| 310 | } |
| 311 | |
| 312 | @Override |
| 313 | public String toString() { |
| 314 | StringBuilder res = new StringBuilder(); |
| 315 | res.append((rawValue >> 24) & 0xFF).append('.'); |
| 316 | res.append((rawValue >> 16) & 0xFF).append('.'); |
| 317 | res.append((rawValue >> 8) & 0xFF).append('.'); |
| 318 | res.append((rawValue >> 0) & 0xFF); |
| 319 | return res.toString(); |
| 320 | } |
| 321 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 322 | public void write4Bytes(ChannelBuffer c) { |
| 323 | c.writeInt(rawValue); |
| 324 | } |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 325 | |
| 326 | public static IPv4Address read4Bytes(ChannelBuffer c) { |
| 327 | return IPv4Address.of(c.readInt()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @Override |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 331 | public IPv4Address applyMask(IPv4Address mask) { |
Aditya Vaja | 98c96e7 | 2014-03-11 15:19:01 -0700 | [diff] [blame] | 332 | return and(mask); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 335 | @Override |
| 336 | public int hashCode() { |
| 337 | final int prime = 31; |
| 338 | int result = 1; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 339 | result = prime * result + rawValue; |
| 340 | return result; |
| 341 | } |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 342 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 343 | @Override |
| 344 | public boolean equals(Object obj) { |
| 345 | if (this == obj) |
| 346 | return true; |
| 347 | if (obj == null) |
| 348 | return false; |
| 349 | if (getClass() != obj.getClass()) |
| 350 | return false; |
| 351 | IPv4Address other = (IPv4Address) obj; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 352 | if (rawValue != other.rawValue) |
| 353 | return false; |
| 354 | return true; |
| 355 | } |
| 356 | |
| 357 | @Override |
| 358 | public int compareTo(IPv4Address o) { |
| 359 | return UnsignedInts.compare(rawValue, o.rawValue); |
| 360 | } |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 361 | |
| 362 | @Override |
| 363 | public void putTo(PrimitiveSink sink) { |
| 364 | sink.putInt(rawValue); |
| 365 | } |
| 366 | |
Byungjoon Lee | ba92e29 | 2014-07-18 13:13:26 +0900 | [diff] [blame] | 367 | @Override |
| 368 | public void writeTo(ChannelBuffer bb) { |
| 369 | bb.writeInt(rawValue); |
| 370 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 371 | } |