Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 3 | import java.util.Arrays; |
| 4 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 5 | import org.jboss.netty.buffer.ChannelBuffer; |
| 6 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 7 | import com.google.common.hash.PrimitiveSink; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 8 | import com.google.common.primitives.UnsignedInts; |
| 9 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 10 | |
| 11 | |
| 12 | /** |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 13 | * Wrapper around an IPv4Address address |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 14 | * |
| 15 | * @author Andreas Wundsam <andreas.wundsam@bigswitch.com> |
| 16 | */ |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 17 | public class IPv4Address extends IPAddress<IPv4Address> { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 18 | static final int LENGTH = 4; |
| 19 | private final int rawValue; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 20 | |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 21 | private final static int NONE_VAL = 0x0; |
| 22 | public final static IPv4Address NONE = new IPv4Address(NONE_VAL); |
| 23 | |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 24 | public static final IPv4Address NO_MASK = IPv4Address.of(0xFFFFFFFF); |
| 25 | public static final IPv4Address FULL_MASK = IPv4Address.of(0x00000000); |
| 26 | |
| 27 | private IPv4Address(final int rawValue) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 28 | this.rawValue = rawValue; |
| 29 | } |
| 30 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 31 | @Override |
Yotam Harchol | eb023dc | 2013-09-26 15:45:44 -0700 | [diff] [blame] | 32 | public IPVersion getIpVersion() { |
| 33 | return IPVersion.IPv4; |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 36 | |
| 37 | @Override |
| 38 | public int asCidrMaskLength() { |
| 39 | int maskint = getInt(); |
| 40 | if (maskint == 0) |
| 41 | return 0; |
| 42 | else if (Integer.bitCount((~maskint) + 1) == 1) { |
| 43 | // IP represents a true CIDR prefix length |
| 44 | return Integer.bitCount(maskint); |
| 45 | } else { |
| 46 | // IP is not a true prefix. |
| 47 | return -1; |
| 48 | } |
| 49 | } |
| 50 | |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 51 | public static IPv4Address of(final byte[] address) { |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame^] | 52 | if (address == null) { |
| 53 | throw new NullPointerException("Address must not be null"); |
| 54 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 55 | if (address.length != LENGTH) { |
| 56 | throw new IllegalArgumentException( |
Andreas Wundsam | c85b5c5 | 2013-09-24 13:01:43 -0700 | [diff] [blame] | 57 | "Invalid byte array length for IPv4Address address: " + address.length); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | int raw = |
| 61 | (address[0] & 0xFF) << 24 | (address[1] & 0xFF) << 16 |
| 62 | | (address[2] & 0xFF) << 8 | (address[3] & 0xFF) << 0; |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 63 | return IPv4Address.of(raw); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 66 | public static IPv4Address of(final int raw) { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 67 | if(raw == NONE_VAL) |
| 68 | return NONE; |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 69 | return new IPv4Address(raw); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 72 | public static IPv4Address of(final String string) { |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame^] | 73 | if (string == null) { |
| 74 | throw new NullPointerException("String must not be null"); |
| 75 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 76 | int start = 0; |
| 77 | int shift = 24; |
| 78 | |
| 79 | int raw = 0; |
| 80 | while (shift >= 0) { |
| 81 | int end = string.indexOf('.', start); |
| 82 | if (end == start || !((shift > 0) ^ (end < 0))) |
| 83 | throw new IllegalArgumentException("IP Address not well formed: " + string); |
| 84 | |
| 85 | String substr = |
| 86 | end > 0 ? string.substring(start, end) : string.substring(start); |
| 87 | int val = Integer.parseInt(substr); |
| 88 | if (val < 0 || val > 255) |
| 89 | throw new IllegalArgumentException("IP Address not well formed: " + string); |
| 90 | |
| 91 | raw |= val << shift; |
| 92 | |
| 93 | shift -= 8; |
| 94 | start = end + 1; |
| 95 | } |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 96 | return IPv4Address.of(raw); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | public int getInt() { |
| 100 | return rawValue; |
| 101 | } |
| 102 | |
| 103 | volatile byte[] bytesCache = null; |
| 104 | |
| 105 | public byte[] getBytes() { |
| 106 | if (bytesCache == null) { |
| 107 | synchronized (this) { |
| 108 | if (bytesCache == null) { |
| 109 | bytesCache = |
| 110 | new byte[] { (byte) ((rawValue >>> 24) & 0xFF), |
| 111 | (byte) ((rawValue >>> 16) & 0xFF), |
| 112 | (byte) ((rawValue >>> 8) & 0xFF), |
| 113 | (byte) ((rawValue >>> 0) & 0xFF) }; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | return bytesCache; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public int getLength() { |
| 122 | return LENGTH; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public String toString() { |
| 127 | StringBuilder res = new StringBuilder(); |
| 128 | res.append((rawValue >> 24) & 0xFF).append('.'); |
| 129 | res.append((rawValue >> 16) & 0xFF).append('.'); |
| 130 | res.append((rawValue >> 8) & 0xFF).append('.'); |
| 131 | res.append((rawValue >> 0) & 0xFF); |
| 132 | return res.toString(); |
| 133 | } |
| 134 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 135 | public void write4Bytes(ChannelBuffer c) { |
| 136 | c.writeInt(rawValue); |
| 137 | } |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 138 | |
| 139 | public static IPv4Address read4Bytes(ChannelBuffer c) { |
| 140 | return IPv4Address.of(c.readInt()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 144 | public IPv4Address applyMask(IPv4Address mask) { |
| 145 | return IPv4Address.of(this.rawValue & mask.rawValue); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 148 | @Override |
| 149 | public int hashCode() { |
| 150 | final int prime = 31; |
| 151 | int result = 1; |
| 152 | result = prime * result + Arrays.hashCode(bytesCache); |
| 153 | result = prime * result + rawValue; |
| 154 | return result; |
| 155 | } |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 156 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 157 | @Override |
| 158 | public boolean equals(Object obj) { |
| 159 | if (this == obj) |
| 160 | return true; |
| 161 | if (obj == null) |
| 162 | return false; |
| 163 | if (getClass() != obj.getClass()) |
| 164 | return false; |
| 165 | IPv4Address other = (IPv4Address) obj; |
| 166 | if (!Arrays.equals(bytesCache, other.bytesCache)) |
| 167 | return false; |
| 168 | if (rawValue != other.rawValue) |
| 169 | return false; |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public int compareTo(IPv4Address o) { |
| 175 | return UnsignedInts.compare(rawValue, o.rawValue); |
| 176 | } |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 177 | |
| 178 | @Override |
| 179 | public void putTo(PrimitiveSink sink) { |
| 180 | sink.putInt(rawValue); |
| 181 | } |
| 182 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 183 | } |