blob: ad454eb1ba0701d9bc113f191118eddbb9a83b04 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4
5
6
7/**
Yotam Harchola289d552013-09-16 10:10:40 -07008 * Wrapper around an IPv4Address address
Yotam Harcholf3f11152013-09-05 16:47:16 -07009 *
10 * @author Andreas Wundsam <andreas.wundsam@bigswitch.com>
11 */
Yotam Harchola289d552013-09-16 10:10:40 -070012public class IPv4Address implements OFValueType<IPv4Address> {
Yotam Harcholf3f11152013-09-05 16:47:16 -070013 static final int LENGTH = 4;
14 private final int rawValue;
Yotam Harcholf3f11152013-09-05 16:47:16 -070015
Yotam Harchola289d552013-09-16 10:10:40 -070016 public static final IPv4Address NO_MASK = IPv4Address.of(0xFFFFFFFF);
17 public static final IPv4Address FULL_MASK = IPv4Address.of(0x00000000);
18
19 private IPv4Address(final int rawValue) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070020 this.rawValue = rawValue;
21 }
22
Yotam Harchola289d552013-09-16 10:10:40 -070023 public static IPv4Address of(final byte[] address) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070024 if (address.length != LENGTH) {
25 throw new IllegalArgumentException(
Yotam Harchola289d552013-09-16 10:10:40 -070026 "Invalid byte array length for IPv4Address address: " + address);
Yotam Harcholf3f11152013-09-05 16:47:16 -070027 }
28
29 int raw =
30 (address[0] & 0xFF) << 24 | (address[1] & 0xFF) << 16
31 | (address[2] & 0xFF) << 8 | (address[3] & 0xFF) << 0;
Yotam Harchola289d552013-09-16 10:10:40 -070032 return IPv4Address.of(raw);
Yotam Harcholf3f11152013-09-05 16:47:16 -070033 }
34
Yotam Harchola289d552013-09-16 10:10:40 -070035 public static IPv4Address of(final int raw) {
36 return new IPv4Address(raw);
Yotam Harcholf3f11152013-09-05 16:47:16 -070037 }
38
Yotam Harchola289d552013-09-16 10:10:40 -070039 public static IPv4Address of(final String string) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070040 int start = 0;
41 int shift = 24;
42
43 int raw = 0;
44 while (shift >= 0) {
45 int end = string.indexOf('.', start);
46 if (end == start || !((shift > 0) ^ (end < 0)))
47 throw new IllegalArgumentException("IP Address not well formed: " + string);
48
49 String substr =
50 end > 0 ? string.substring(start, end) : string.substring(start);
51 int val = Integer.parseInt(substr);
52 if (val < 0 || val > 255)
53 throw new IllegalArgumentException("IP Address not well formed: " + string);
54
55 raw |= val << shift;
56
57 shift -= 8;
58 start = end + 1;
59 }
Yotam Harchola289d552013-09-16 10:10:40 -070060 return IPv4Address.of(raw);
Yotam Harcholf3f11152013-09-05 16:47:16 -070061 }
62
63 public int getInt() {
64 return rawValue;
65 }
66
67 volatile byte[] bytesCache = null;
68
69 public byte[] getBytes() {
70 if (bytesCache == null) {
71 synchronized (this) {
72 if (bytesCache == null) {
73 bytesCache =
74 new byte[] { (byte) ((rawValue >>> 24) & 0xFF),
75 (byte) ((rawValue >>> 16) & 0xFF),
76 (byte) ((rawValue >>> 8) & 0xFF),
77 (byte) ((rawValue >>> 0) & 0xFF) };
78 }
79 }
80 }
81 return bytesCache;
82 }
83
84 @Override
85 public int getLength() {
86 return LENGTH;
87 }
88
89 @Override
90 public String toString() {
91 StringBuilder res = new StringBuilder();
92 res.append((rawValue >> 24) & 0xFF).append('.');
93 res.append((rawValue >> 16) & 0xFF).append('.');
94 res.append((rawValue >> 8) & 0xFF).append('.');
95 res.append((rawValue >> 0) & 0xFF);
96 return res.toString();
97 }
98
99 @Override
100 public int hashCode() {
101 final int prime = 31;
102 int result = 1;
103 result = prime * result + rawValue;
104 return result;
105 }
106
107 @Override
108 public boolean equals(final Object obj) {
109 if (this == obj)
110 return true;
111 if (obj == null)
112 return false;
113 if (getClass() != obj.getClass())
114 return false;
Yotam Harchola289d552013-09-16 10:10:40 -0700115 IPv4Address other = (IPv4Address) obj;
Yotam Harcholf3f11152013-09-05 16:47:16 -0700116 if (rawValue != other.rawValue)
117 return false;
118 return true;
119 }
Yotam Harchola289d552013-09-16 10:10:40 -0700120
Yotam Harcholf3f11152013-09-05 16:47:16 -0700121 public void write4Bytes(ChannelBuffer c) {
122 c.writeInt(rawValue);
123 }
Yotam Harchola289d552013-09-16 10:10:40 -0700124
125 public static IPv4Address read4Bytes(ChannelBuffer c) {
126 return IPv4Address.of(c.readInt());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700127 }
128
129 @Override
Yotam Harchola289d552013-09-16 10:10:40 -0700130 public IPv4Address applyMask(IPv4Address mask) {
131 return IPv4Address.of(this.rawValue & mask.rawValue);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700132 }
133
Yotam Harchola289d552013-09-16 10:10:40 -0700134
Yotam Harcholf3f11152013-09-05 16:47:16 -0700135}