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