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) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 52 | if (address.length != LENGTH) { |
| 53 | throw new IllegalArgumentException( |
Andreas Wundsam | c85b5c5 | 2013-09-24 13:01:43 -0700 | [diff] [blame] | 54 | "Invalid byte array length for IPv4Address address: " + address.length); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | int raw = |
| 58 | (address[0] & 0xFF) << 24 | (address[1] & 0xFF) << 16 |
| 59 | | (address[2] & 0xFF) << 8 | (address[3] & 0xFF) << 0; |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 60 | return IPv4Address.of(raw); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 63 | public static IPv4Address of(final int raw) { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 64 | if(raw == NONE_VAL) |
| 65 | return NONE; |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 66 | return new IPv4Address(raw); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 69 | public static IPv4Address of(final String string) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 70 | int start = 0; |
| 71 | int shift = 24; |
| 72 | |
| 73 | int raw = 0; |
| 74 | while (shift >= 0) { |
| 75 | int end = string.indexOf('.', start); |
| 76 | if (end == start || !((shift > 0) ^ (end < 0))) |
| 77 | throw new IllegalArgumentException("IP Address not well formed: " + string); |
| 78 | |
| 79 | String substr = |
| 80 | end > 0 ? string.substring(start, end) : string.substring(start); |
| 81 | int val = Integer.parseInt(substr); |
| 82 | if (val < 0 || val > 255) |
| 83 | throw new IllegalArgumentException("IP Address not well formed: " + string); |
| 84 | |
| 85 | raw |= val << shift; |
| 86 | |
| 87 | shift -= 8; |
| 88 | start = end + 1; |
| 89 | } |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 90 | return IPv4Address.of(raw); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | public int getInt() { |
| 94 | return rawValue; |
| 95 | } |
| 96 | |
| 97 | volatile byte[] bytesCache = null; |
| 98 | |
| 99 | public byte[] getBytes() { |
| 100 | if (bytesCache == null) { |
| 101 | synchronized (this) { |
| 102 | if (bytesCache == null) { |
| 103 | bytesCache = |
| 104 | new byte[] { (byte) ((rawValue >>> 24) & 0xFF), |
| 105 | (byte) ((rawValue >>> 16) & 0xFF), |
| 106 | (byte) ((rawValue >>> 8) & 0xFF), |
| 107 | (byte) ((rawValue >>> 0) & 0xFF) }; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | return bytesCache; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public int getLength() { |
| 116 | return LENGTH; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public String toString() { |
| 121 | StringBuilder res = new StringBuilder(); |
| 122 | res.append((rawValue >> 24) & 0xFF).append('.'); |
| 123 | res.append((rawValue >> 16) & 0xFF).append('.'); |
| 124 | res.append((rawValue >> 8) & 0xFF).append('.'); |
| 125 | res.append((rawValue >> 0) & 0xFF); |
| 126 | return res.toString(); |
| 127 | } |
| 128 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 129 | public void write4Bytes(ChannelBuffer c) { |
| 130 | c.writeInt(rawValue); |
| 131 | } |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 132 | |
| 133 | public static IPv4Address read4Bytes(ChannelBuffer c) { |
| 134 | return IPv4Address.of(c.readInt()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 138 | public IPv4Address applyMask(IPv4Address mask) { |
| 139 | return IPv4Address.of(this.rawValue & mask.rawValue); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 142 | @Override |
| 143 | public int hashCode() { |
| 144 | final int prime = 31; |
| 145 | int result = 1; |
| 146 | result = prime * result + Arrays.hashCode(bytesCache); |
| 147 | result = prime * result + rawValue; |
| 148 | return result; |
| 149 | } |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 150 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 151 | @Override |
| 152 | public boolean equals(Object obj) { |
| 153 | if (this == obj) |
| 154 | return true; |
| 155 | if (obj == null) |
| 156 | return false; |
| 157 | if (getClass() != obj.getClass()) |
| 158 | return false; |
| 159 | IPv4Address other = (IPv4Address) obj; |
| 160 | if (!Arrays.equals(bytesCache, other.bytesCache)) |
| 161 | return false; |
| 162 | if (rawValue != other.rawValue) |
| 163 | return false; |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public int compareTo(IPv4Address o) { |
| 169 | return UnsignedInts.compare(rawValue, o.rawValue); |
| 170 | } |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 171 | |
| 172 | @Override |
| 173 | public void putTo(PrimitiveSink sink) { |
| 174 | sink.putInt(rawValue); |
| 175 | } |
| 176 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 177 | } |