blob: 78b8dd2e9ba8ec05461496d458371f331c33680e [file] [log] [blame]
Andreas Wundsam40e14f72013-05-06 14:49:08 -07001package org.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
Yotam Harchold7b84202013-07-26 16:08:10 -07004
Yotam Harchol161a5d52013-07-25 17:17:48 -07005
Andreas Wundsam40e14f72013-05-06 14:49:08 -07006
7/**
8 * Wrapper around an IPv4 address
9 *
10 * @author Andreas Wundsam <andreas.wundsam@bigswitch.com>
11 */
Yotam Harchol161a5d52013-07-25 17:17:48 -070012public class IPv4 implements OFValueType {
Andreas Wundsam40e14f72013-05-06 14:49:08 -070013 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 Wundsam40e14f72013-05-06 14:49:08 -070086 @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 Harchol161a5d52013-07-25 17:17:48 -0700117
Yotam Harchold7b84202013-07-26 16:08:10 -0700118 public void write4Bytes(ChannelBuffer c) {
119 c.writeInt(rawValue);
Yotam Harchol161a5d52013-07-25 17:17:48 -0700120 }
Yotam Harchold7b84202013-07-26 16:08:10 -0700121
122 public static IPv4 read4Bytes(ChannelBuffer c) {
123 return IPv4.of(c.readInt());
124 }
125
Andreas Wundsam40e14f72013-05-06 14:49:08 -0700126}