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