Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 1 | package org.openflow.types; |
| 2 | |
| 3 | import org.jboss.netty.buffer.ChannelBuffer; |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 4 | |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 5 | |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 6 | |
| 7 | /** |
| 8 | * Wrapper around an IPv4 address |
| 9 | * |
| 10 | * @author Andreas Wundsam <andreas.wundsam@bigswitch.com> |
| 11 | */ |
Yotam Harchol | 5c9d6f4 | 2013-08-01 11:09:20 -0700 | [diff] [blame] | 12 | public class IPv4 implements OFValueType<IPv4> { |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 13 | static final int LENGTH = 4; |
| 14 | private final int rawValue; |
Yotam Harchol | c281360 | 2013-08-20 12:14:22 -0700 | [diff] [blame] | 15 | |
Yotam Harchol | ff8f85e | 2013-08-21 10:02:23 -0700 | [diff] [blame] | 16 | public static final IPv4 NO_MASK = IPv4.of(0xFFFFFFFF); |
| 17 | public static final IPv4 FULL_MASK = IPv4.of(0x00000000); |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 18 | |
| 19 | private IPv4(final int rawValue) { |
| 20 | this.rawValue = rawValue; |
| 21 | } |
| 22 | |
| 23 | public static IPv4 of(final byte[] address) { |
| 24 | if (address.length != LENGTH) { |
| 25 | throw new IllegalArgumentException( |
| 26 | "Invalid byte array length for IPv4 address: " + address); |
| 27 | } |
| 28 | |
| 29 | int raw = |
| 30 | (address[0] & 0xFF) << 24 | (address[1] & 0xFF) << 16 |
| 31 | | (address[2] & 0xFF) << 8 | (address[3] & 0xFF) << 0; |
| 32 | return IPv4.of(raw); |
| 33 | } |
| 34 | |
| 35 | public static IPv4 of(final int raw) { |
| 36 | return new IPv4(raw); |
| 37 | } |
| 38 | |
| 39 | public static IPv4 of(final String string) { |
| 40 | int start = 0; |
| 41 | int shift = 24; |
| 42 | |
| 43 | int raw = 0; |
| 44 | while (shift >= 0) { |
| 45 | int end = string.indexOf('.', start); |
| 46 | if (end == start || !((shift > 0) ^ (end < 0))) |
| 47 | throw new IllegalArgumentException("IP Address not well formed: " + string); |
| 48 | |
| 49 | String substr = |
| 50 | end > 0 ? string.substring(start, end) : string.substring(start); |
| 51 | int val = Integer.parseInt(substr); |
| 52 | if (val < 0 || val > 255) |
| 53 | throw new IllegalArgumentException("IP Address not well formed: " + string); |
| 54 | |
| 55 | raw |= val << shift; |
| 56 | |
| 57 | shift -= 8; |
| 58 | start = end + 1; |
| 59 | } |
| 60 | return IPv4.of(raw); |
| 61 | } |
| 62 | |
| 63 | public int getInt() { |
| 64 | return rawValue; |
| 65 | } |
| 66 | |
| 67 | volatile byte[] bytesCache = null; |
| 68 | |
| 69 | public byte[] getBytes() { |
| 70 | if (bytesCache == null) { |
| 71 | synchronized (this) { |
| 72 | if (bytesCache == null) { |
| 73 | bytesCache = |
| 74 | new byte[] { (byte) ((rawValue >>> 24) & 0xFF), |
| 75 | (byte) ((rawValue >>> 16) & 0xFF), |
| 76 | (byte) ((rawValue >>> 8) & 0xFF), |
| 77 | (byte) ((rawValue >>> 0) & 0xFF) }; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | return bytesCache; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public int getLength() { |
| 86 | return LENGTH; |
| 87 | } |
| 88 | |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 89 | @Override |
| 90 | public String toString() { |
| 91 | StringBuilder res = new StringBuilder(); |
| 92 | res.append((rawValue >> 24) & 0xFF).append('.'); |
| 93 | res.append((rawValue >> 16) & 0xFF).append('.'); |
| 94 | res.append((rawValue >> 8) & 0xFF).append('.'); |
| 95 | res.append((rawValue >> 0) & 0xFF); |
| 96 | return res.toString(); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public int hashCode() { |
| 101 | final int prime = 31; |
| 102 | int result = 1; |
| 103 | result = prime * result + rawValue; |
| 104 | return result; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public boolean equals(final Object obj) { |
| 109 | if (this == obj) |
| 110 | return true; |
| 111 | if (obj == null) |
| 112 | return false; |
| 113 | if (getClass() != obj.getClass()) |
| 114 | return false; |
| 115 | IPv4 other = (IPv4) obj; |
| 116 | if (rawValue != other.rawValue) |
| 117 | return false; |
| 118 | return true; |
| 119 | } |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 120 | |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 121 | public void write4Bytes(ChannelBuffer c) { |
| 122 | c.writeInt(rawValue); |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame] | 123 | } |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 124 | |
| 125 | public static IPv4 read4Bytes(ChannelBuffer c) { |
| 126 | return IPv4.of(c.readInt()); |
| 127 | } |
Yotam Harchol | 5c9d6f4 | 2013-08-01 11:09:20 -0700 | [diff] [blame] | 128 | |
| 129 | @Override |
| 130 | public IPv4 applyMask(IPv4 mask) { |
| 131 | return IPv4.of(this.rawValue & mask.rawValue); |
| 132 | } |
| 133 | |
Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame] | 134 | |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 135 | } |