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