blob: da6f12607915fa84ffab0d75578c97f96bede256 [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 Harchol5c9d6f42013-08-01 11:09:20 -070012public class IPv4 implements OFValueType<IPv4> {
Andreas Wundsam40e14f72013-05-06 14:49:08 -070013 static final int LENGTH = 4;
14 private final int rawValue;
Yotam Harcholc2813602013-08-20 12:14:22 -070015
Yotam Harcholff8f85e2013-08-21 10:02:23 -070016 public static final IPv4 NO_MASK = IPv4.of(0xFFFFFFFF);
17 public static final IPv4 FULL_MASK = IPv4.of(0x00000000);
Andreas Wundsam40e14f72013-05-06 14:49:08 -070018
19 private IPv4(final int rawValue) {
20 this.rawValue = rawValue;
21 }
22
23 public static IPv4 of(final byte[] address) {
24 if (address.length != LENGTH) {
25 throw new IllegalArgumentException(
26 "Invalid byte array length for IPv4 address: " + address);
27 }
28
29 int raw =
30 (address[0] & 0xFF) << 24 | (address[1] & 0xFF) << 16
31 | (address[2] & 0xFF) << 8 | (address[3] & 0xFF) << 0;
32 return IPv4.of(raw);
33 }
34
35 public static IPv4 of(final int raw) {
36 return new IPv4(raw);
37 }
38
39 public static IPv4 of(final String string) {
40 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 }
60 return IPv4.of(raw);
61 }
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
Andreas Wundsam40e14f72013-05-06 14:49:08 -070089 @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;
115 IPv4 other = (IPv4) obj;
116 if (rawValue != other.rawValue)
117 return false;
118 return true;
119 }
Yotam Harchol161a5d52013-07-25 17:17:48 -0700120
Yotam Harchold7b84202013-07-26 16:08:10 -0700121 public void write4Bytes(ChannelBuffer c) {
122 c.writeInt(rawValue);
Yotam Harchol161a5d52013-07-25 17:17:48 -0700123 }
Yotam Harchold7b84202013-07-26 16:08:10 -0700124
125 public static IPv4 read4Bytes(ChannelBuffer c) {
126 return IPv4.of(c.readInt());
127 }
Yotam Harchol5c9d6f42013-08-01 11:09:20 -0700128
129 @Override
130 public IPv4 applyMask(IPv4 mask) {
131 return IPv4.of(this.rawValue & mask.rawValue);
132 }
133
Yotam Harchold7b84202013-07-26 16:08:10 -0700134
Andreas Wundsam40e14f72013-05-06 14:49:08 -0700135}