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