Pavlin Radoslavov

IpAddress and IpPrefix related cleanup:

 * Removed IpAddress.MAX_INET_MASK and use IpPrefix.MAX_INET_MASK_LENGTH
   instead
 * Renamed IpAddress.INET_LEN to INET_BYTE_LENGTH
 * Added IpAddress.INET_BIT_LENGTH, INET6_BYTE_LENGTH, and INET6_BIT_LENGTH
 * Removed methods that are semantically incorrect, or are not needed/used
   - IpAddress.prefixLength()
   - IpAddress.toPrefix()
   - IpAddress.mask()
   - IpAddress.netmask()
   - IpAddress.network()
   - IpAddress.host()
   - IpAddress.isMasked()
   - IpAddress.contains()
   - IpPrefix constructor for version and bytes (but no netmask)
   - IpPrefix.valueOf(int)

 * Misc. other cleanup.
...@@ -148,9 +148,9 @@ public class PeerConnectivityManager { ...@@ -148,9 +148,9 @@ public class PeerConnectivityManager {
148 .matchEthType(Ethernet.TYPE_IPV4) 148 .matchEthType(Ethernet.TYPE_IPV4)
149 .matchIPProtocol(IPv4.PROTOCOL_TCP) 149 .matchIPProtocol(IPv4.PROTOCOL_TCP)
150 .matchIPSrc(IpPrefix.valueOf(bgpdAddress.toInt(), 150 .matchIPSrc(IpPrefix.valueOf(bgpdAddress.toInt(),
151 - IpAddress.MAX_INET_MASK)) 151 + IpPrefix.MAX_INET_MASK_LENGTH))
152 .matchIPDst(IpPrefix.valueOf(bgpdPeerAddress.toInt(), 152 .matchIPDst(IpPrefix.valueOf(bgpdPeerAddress.toInt(),
153 - IpAddress.MAX_INET_MASK)) 153 + IpPrefix.MAX_INET_MASK_LENGTH))
154 .matchTcpDst((short) BgpConstants.BGP_PORT) 154 .matchTcpDst((short) BgpConstants.BGP_PORT)
155 .build(); 155 .build();
156 156
...@@ -171,9 +171,9 @@ public class PeerConnectivityManager { ...@@ -171,9 +171,9 @@ public class PeerConnectivityManager {
171 .matchEthType(Ethernet.TYPE_IPV4) 171 .matchEthType(Ethernet.TYPE_IPV4)
172 .matchIPProtocol(IPv4.PROTOCOL_TCP) 172 .matchIPProtocol(IPv4.PROTOCOL_TCP)
173 .matchIPSrc(IpPrefix.valueOf(bgpdAddress.toInt(), 173 .matchIPSrc(IpPrefix.valueOf(bgpdAddress.toInt(),
174 - IpAddress.MAX_INET_MASK)) 174 + IpPrefix.MAX_INET_MASK_LENGTH))
175 .matchIPDst(IpPrefix.valueOf(bgpdPeerAddress.toInt(), 175 .matchIPDst(IpPrefix.valueOf(bgpdPeerAddress.toInt(),
176 - IpAddress.MAX_INET_MASK)) 176 + IpPrefix.MAX_INET_MASK_LENGTH))
177 .matchTcpSrc((short) BgpConstants.BGP_PORT) 177 .matchTcpSrc((short) BgpConstants.BGP_PORT)
178 .build(); 178 .build();
179 179
...@@ -191,9 +191,9 @@ public class PeerConnectivityManager { ...@@ -191,9 +191,9 @@ public class PeerConnectivityManager {
191 .matchEthType(Ethernet.TYPE_IPV4) 191 .matchEthType(Ethernet.TYPE_IPV4)
192 .matchIPProtocol(IPv4.PROTOCOL_TCP) 192 .matchIPProtocol(IPv4.PROTOCOL_TCP)
193 .matchIPSrc(IpPrefix.valueOf(bgpdPeerAddress.toInt(), 193 .matchIPSrc(IpPrefix.valueOf(bgpdPeerAddress.toInt(),
194 - IpAddress.MAX_INET_MASK)) 194 + IpPrefix.MAX_INET_MASK_LENGTH))
195 .matchIPDst(IpPrefix.valueOf(bgpdAddress.toInt(), 195 .matchIPDst(IpPrefix.valueOf(bgpdAddress.toInt(),
196 - IpAddress.MAX_INET_MASK)) 196 + IpPrefix.MAX_INET_MASK_LENGTH))
197 .matchTcpDst((short) BgpConstants.BGP_PORT) 197 .matchTcpDst((short) BgpConstants.BGP_PORT)
198 .build(); 198 .build();
199 199
...@@ -211,9 +211,9 @@ public class PeerConnectivityManager { ...@@ -211,9 +211,9 @@ public class PeerConnectivityManager {
211 .matchEthType(Ethernet.TYPE_IPV4) 211 .matchEthType(Ethernet.TYPE_IPV4)
212 .matchIPProtocol(IPv4.PROTOCOL_TCP) 212 .matchIPProtocol(IPv4.PROTOCOL_TCP)
213 .matchIPSrc(IpPrefix.valueOf(bgpdPeerAddress.toInt(), 213 .matchIPSrc(IpPrefix.valueOf(bgpdPeerAddress.toInt(),
214 - IpAddress.MAX_INET_MASK)) 214 + IpPrefix.MAX_INET_MASK_LENGTH))
215 .matchIPDst(IpPrefix.valueOf(bgpdAddress.toInt(), 215 .matchIPDst(IpPrefix.valueOf(bgpdAddress.toInt(),
216 - IpAddress.MAX_INET_MASK)) 216 + IpPrefix.MAX_INET_MASK_LENGTH))
217 .matchTcpSrc((short) BgpConstants.BGP_PORT) 217 .matchTcpSrc((short) BgpConstants.BGP_PORT)
218 .build(); 218 .build();
219 219
...@@ -281,9 +281,9 @@ public class PeerConnectivityManager { ...@@ -281,9 +281,9 @@ public class PeerConnectivityManager {
281 .matchEthType(Ethernet.TYPE_IPV4) 281 .matchEthType(Ethernet.TYPE_IPV4)
282 .matchIPProtocol(IPv4.PROTOCOL_ICMP) 282 .matchIPProtocol(IPv4.PROTOCOL_ICMP)
283 .matchIPSrc(IpPrefix.valueOf(bgpdAddress.toInt(), 283 .matchIPSrc(IpPrefix.valueOf(bgpdAddress.toInt(),
284 - IpAddress.MAX_INET_MASK)) 284 + IpPrefix.MAX_INET_MASK_LENGTH))
285 .matchIPDst(IpPrefix.valueOf(bgpdPeerAddress.toInt(), 285 .matchIPDst(IpPrefix.valueOf(bgpdPeerAddress.toInt(),
286 - IpAddress.MAX_INET_MASK)) 286 + IpPrefix.MAX_INET_MASK_LENGTH))
287 .build(); 287 .build();
288 288
289 TrafficTreatment treatment = DefaultTrafficTreatment.builder() 289 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
...@@ -301,9 +301,9 @@ public class PeerConnectivityManager { ...@@ -301,9 +301,9 @@ public class PeerConnectivityManager {
301 .matchEthType(Ethernet.TYPE_IPV4) 301 .matchEthType(Ethernet.TYPE_IPV4)
302 .matchIPProtocol(IPv4.PROTOCOL_ICMP) 302 .matchIPProtocol(IPv4.PROTOCOL_ICMP)
303 .matchIPSrc(IpPrefix.valueOf(bgpdPeerAddress.toInt(), 303 .matchIPSrc(IpPrefix.valueOf(bgpdPeerAddress.toInt(),
304 - IpAddress.MAX_INET_MASK)) 304 + IpPrefix.MAX_INET_MASK_LENGTH))
305 .matchIPDst(IpPrefix.valueOf(bgpdAddress.toInt(), 305 .matchIPDst(IpPrefix.valueOf(bgpdAddress.toInt(),
306 - IpAddress.MAX_INET_MASK)) 306 + IpPrefix.MAX_INET_MASK_LENGTH))
307 .build(); 307 .build();
308 308
309 PointToPointIntent reversedIntent = 309 PointToPointIntent reversedIntent =
......
...@@ -75,7 +75,7 @@ public class RouteEntry { ...@@ -75,7 +75,7 @@ public class RouteEntry {
75 StringBuilder result = new StringBuilder(ip4Prefix.prefixLength()); 75 StringBuilder result = new StringBuilder(ip4Prefix.prefixLength());
76 long value = ip4Prefix.toInt(); 76 long value = ip4Prefix.toInt();
77 for (int i = 0; i < ip4Prefix.prefixLength(); i++) { 77 for (int i = 0; i < ip4Prefix.prefixLength(); i++) {
78 - long mask = 1 << (IpAddress.MAX_INET_MASK - 1 - i); 78 + long mask = 1 << (IpPrefix.MAX_INET_MASK_LENGTH - 1 - i);
79 result.append(((value & mask) == 0) ? "0" : "1"); 79 result.append(((value & mask) == 0) ? "0" : "1");
80 } 80 }
81 return result.toString(); 81 return result.toString();
......
...@@ -190,12 +190,12 @@ class TestBgpPeerChannelHandler extends SimpleChannelHandler { ...@@ -190,12 +190,12 @@ class TestBgpPeerChannelHandler extends SimpleChannelHandler {
190 190
191 IpAddress address = prefix.toIpAddress(); 191 IpAddress address = prefix.toIpAddress();
192 long value = address.toInt() & 0xffffffffL; 192 long value = address.toInt() & 0xffffffffL;
193 - for (int i = 0; i < IpAddress.INET_LEN; i++) { 193 + for (int i = 0; i < IpAddress.INET_BYTE_LENGTH; i++) {
194 if (prefixBytelen-- == 0) { 194 if (prefixBytelen-- == 0) {
195 break; 195 break;
196 } 196 }
197 long nextByte = 197 long nextByte =
198 - (value >> ((IpAddress.INET_LEN - i - 1) * 8)) & 0xff; 198 + (value >> ((IpAddress.INET_BYTE_LENGTH - i - 1) * 8)) & 0xff;
199 message.writeByte((int) nextByte); 199 message.writeByte((int) nextByte);
200 } 200 }
201 } 201 }
......
...@@ -46,7 +46,6 @@ import org.onlab.onos.net.provider.ProviderId; ...@@ -46,7 +46,6 @@ import org.onlab.onos.net.provider.ProviderId;
46 import org.onlab.packet.ARP; 46 import org.onlab.packet.ARP;
47 import org.onlab.packet.Ethernet; 47 import org.onlab.packet.Ethernet;
48 import org.onlab.packet.IpAddress; 48 import org.onlab.packet.IpAddress;
49 -import org.onlab.packet.IpPrefix;
50 import org.onlab.packet.MacAddress; 49 import org.onlab.packet.MacAddress;
51 import org.onlab.util.Timer; 50 import org.onlab.util.Timer;
52 51
...@@ -212,7 +211,7 @@ public class HostMonitor implements TimerTask { ...@@ -212,7 +211,7 @@ public class HostMonitor implements TimerTask {
212 arp.setHardwareType(ARP.HW_TYPE_ETHERNET) 211 arp.setHardwareType(ARP.HW_TYPE_ETHERNET)
213 .setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH) 212 .setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH)
214 .setProtocolType(ARP.PROTO_TYPE_IP) 213 .setProtocolType(ARP.PROTO_TYPE_IP)
215 - .setProtocolAddressLength((byte) IpPrefix.INET_LEN) 214 + .setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH)
216 .setOpCode(ARP.OP_REQUEST); 215 .setOpCode(ARP.OP_REQUEST);
217 216
218 arp.setSenderHardwareAddress(sourceMac.getAddress()) 217 arp.setSenderHardwareAddress(sourceMac.getAddress())
......
...@@ -56,7 +56,6 @@ import org.onlab.onos.net.proxyarp.ProxyArpService; ...@@ -56,7 +56,6 @@ import org.onlab.onos.net.proxyarp.ProxyArpService;
56 import org.onlab.packet.ARP; 56 import org.onlab.packet.ARP;
57 import org.onlab.packet.Ethernet; 57 import org.onlab.packet.Ethernet;
58 import org.onlab.packet.IpAddress; 58 import org.onlab.packet.IpAddress;
59 -import org.onlab.packet.IpPrefix;
60 import org.onlab.packet.MacAddress; 59 import org.onlab.packet.MacAddress;
61 import org.onlab.packet.VlanId; 60 import org.onlab.packet.VlanId;
62 import org.slf4j.Logger; 61 import org.slf4j.Logger;
...@@ -368,7 +367,7 @@ public class ProxyArpManager implements ProxyArpService { ...@@ -368,7 +367,7 @@ public class ProxyArpManager implements ProxyArpService {
368 arp.setProtocolType(ARP.PROTO_TYPE_IP); 367 arp.setProtocolType(ARP.PROTO_TYPE_IP);
369 arp.setHardwareType(ARP.HW_TYPE_ETHERNET); 368 arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
370 369
371 - arp.setProtocolAddressLength((byte) IpPrefix.INET_LEN); 370 + arp.setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH);
372 arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH); 371 arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH);
373 arp.setSenderHardwareAddress(srcMac.getAddress()); 372 arp.setSenderHardwareAddress(srcMac.getAddress());
374 arp.setTargetHardwareAddress(request.getSourceMACAddress()); 373 arp.setTargetHardwareAddress(request.getSourceMACAddress());
......
...@@ -534,7 +534,7 @@ public class ProxyArpManagerTest { ...@@ -534,7 +534,7 @@ public class ProxyArpManagerTest {
534 arp.setProtocolType(ARP.PROTO_TYPE_IP); 534 arp.setProtocolType(ARP.PROTO_TYPE_IP);
535 arp.setHardwareType(ARP.HW_TYPE_ETHERNET); 535 arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
536 536
537 - arp.setProtocolAddressLength((byte) IpPrefix.INET_LEN); 537 + arp.setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH);
538 arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH); 538 arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH);
539 arp.setSenderHardwareAddress(srcMac.getAddress()); 539 arp.setSenderHardwareAddress(srcMac.getAddress());
540 540
......
...@@ -171,7 +171,8 @@ public class FlowEntryBuilder { ...@@ -171,7 +171,8 @@ public class FlowEntryBuilder {
171 builder.setIpDst(IpPrefix.valueOf(di.getInt(), 171 builder.setIpDst(IpPrefix.valueOf(di.getInt(),
172 di.asCidrMaskLength())); 172 di.asCidrMaskLength()));
173 } else { 173 } else {
174 - builder.setIpDst(IpPrefix.valueOf(di.getInt())); 174 + builder.setIpDst(IpPrefix.valueOf(di.getInt(),
175 + IpPrefix.MAX_INET_MASK_LENGTH));
175 } 176 }
176 break; 177 break;
177 case SET_NW_SRC: 178 case SET_NW_SRC:
...@@ -181,7 +182,8 @@ public class FlowEntryBuilder { ...@@ -181,7 +182,8 @@ public class FlowEntryBuilder {
181 builder.setIpSrc(IpPrefix.valueOf(si.getInt(), 182 builder.setIpSrc(IpPrefix.valueOf(si.getInt(),
182 si.asCidrMaskLength())); 183 si.asCidrMaskLength()));
183 } else { 184 } else {
184 - builder.setIpSrc(IpPrefix.valueOf(si.getInt())); 185 + builder.setIpSrc(IpPrefix.valueOf(si.getInt(),
186 + IpPrefix.MAX_INET_MASK_LENGTH));
185 } 187 }
186 break; 188 break;
187 case EXPERIMENTER: 189 case EXPERIMENTER:
...@@ -256,7 +258,7 @@ public class FlowEntryBuilder { ...@@ -256,7 +258,7 @@ public class FlowEntryBuilder {
256 } else { 258 } else {
257 dip = IpPrefix.valueOf( 259 dip = IpPrefix.valueOf(
258 match.get(MatchField.IPV4_DST).getInt(), 260 match.get(MatchField.IPV4_DST).getInt(),
259 - IpPrefix.MAX_INET_MASK); 261 + IpPrefix.MAX_INET_MASK_LENGTH);
260 } 262 }
261 263
262 builder.matchIPDst(dip); 264 builder.matchIPDst(dip);
...@@ -272,7 +274,7 @@ public class FlowEntryBuilder { ...@@ -272,7 +274,7 @@ public class FlowEntryBuilder {
272 } else { 274 } else {
273 sip = IpPrefix.valueOf( 275 sip = IpPrefix.valueOf(
274 match.get(MatchField.IPV4_SRC).getInt(), 276 match.get(MatchField.IPV4_SRC).getInt(),
275 - IpPrefix.MAX_INET_MASK); 277 + IpPrefix.MAX_INET_MASK_LENGTH);
276 } 278 }
277 279
278 builder.matchIPSrc(sip); 280 builder.matchIPSrc(sip);
......
...@@ -26,48 +26,37 @@ public final class IpPrefix { ...@@ -26,48 +26,37 @@ public final class IpPrefix {
26 26
27 // TODO a comparator for netmasks? E.g. for sorting by prefix match order. 27 // TODO a comparator for netmasks? E.g. for sorting by prefix match order.
28 28
29 - //IP Versions 29 + // IP Versions: IPv4 and IPv6
30 public enum Version { INET, INET6 }; 30 public enum Version { INET, INET6 };
31 31
32 - //lengths of address, in bytes 32 + // Maximum network mask length
33 - public static final int INET_LEN = 4; 33 + public static final int MAX_INET_MASK_LENGTH = IpAddress.INET_BIT_LENGTH;
34 - public static final int INET6_LEN = 16; 34 + public static final int MAX_INET6_MASK_LENGTH = IpAddress.INET6_BIT_LENGTH;
35 35
36 - //maximum CIDR value
37 - public static final int MAX_INET_MASK = 32;
38 //no mask (no network), e.g. a simple address 36 //no mask (no network), e.g. a simple address
39 - public static final int DEFAULT_MASK = 0; 37 + private static final int DEFAULT_MASK = 0;
40 38
41 /** 39 /**
42 * Default value indicating an unspecified address. 40 * Default value indicating an unspecified address.
43 */ 41 */
44 - static final byte[] ANY = new byte [] {0, 0, 0, 0}; 42 + private static final byte[] ANY = new byte[] {0, 0, 0, 0};
45 43
46 - protected Version version; 44 + private final Version version;
47 - 45 + private final byte[] octets;
48 - protected byte[] octets; 46 + private final int netmask;
49 - protected int netmask;
50 -
51 - private IpPrefix(Version ver, byte[] octets, int netmask) {
52 - this.version = ver;
53 - this.octets = Arrays.copyOf(octets, INET_LEN);
54 - this.netmask = netmask;
55 - }
56 -
57 - private IpPrefix(Version ver, byte[] octets) {
58 - this.version = ver;
59 - this.octets = Arrays.copyOf(octets, INET_LEN);
60 - this.netmask = DEFAULT_MASK;
61 - }
62 47
63 /** 48 /**
64 - * Converts a byte array into an IP address. 49 + * Constructor for given IP address version, prefix address octets,
50 + * and network mask length.
65 * 51 *
66 - * @param address a byte array 52 + * @param ver the IP address version
67 - * @return an IP address 53 + * @param octets the IP prefix address octets
54 + * @param netmask the network mask length
68 */ 55 */
69 - public static IpPrefix valueOf(byte [] address) { 56 + private IpPrefix(Version ver, byte[] octets, int netmask) {
70 - return new IpPrefix(Version.INET, address); 57 + this.version = ver;
58 + this.octets = Arrays.copyOf(octets, IpAddress.INET_BYTE_LENGTH);
59 + this.netmask = netmask;
71 } 60 }
72 61
73 /** 62 /**
...@@ -77,7 +66,7 @@ public final class IpPrefix { ...@@ -77,7 +66,7 @@ public final class IpPrefix {
77 * @param netmask the CIDR value subnet mask 66 * @param netmask the CIDR value subnet mask
78 * @return an IP address 67 * @return an IP address
79 */ 68 */
80 - public static IpPrefix valueOf(byte [] address, int netmask) { 69 + public static IpPrefix valueOf(byte[] address, int netmask) {
81 return new IpPrefix(Version.INET, address, netmask); 70 return new IpPrefix(Version.INET, address, netmask);
82 } 71 }
83 72
...@@ -87,10 +76,11 @@ public final class IpPrefix { ...@@ -87,10 +76,11 @@ public final class IpPrefix {
87 * @param address the integer to convert 76 * @param address the integer to convert
88 * @return a byte array 77 * @return a byte array
89 */ 78 */
90 - private static byte [] bytes(int address) { 79 + private static byte[] bytes(int address) {
91 - byte [] bytes = new byte [INET_LEN]; 80 + byte[] bytes = new byte [IpAddress.INET_BYTE_LENGTH];
92 - for (int i = 0; i < INET_LEN; i++) { 81 + for (int i = 0; i < IpAddress.INET_BYTE_LENGTH; i++) {
93 - bytes[i] = (byte) ((address >> (INET_LEN - (i + 1)) * 8) & 0xff); 82 + bytes[i] = (byte) ((address >> (IpAddress.INET_BYTE_LENGTH
83 + - (i + 1)) * 8) & 0xff);
94 } 84 }
95 85
96 return bytes; 86 return bytes;
...@@ -100,16 +90,6 @@ public final class IpPrefix { ...@@ -100,16 +90,6 @@ public final class IpPrefix {
100 * Converts an integer into an IPv4 address. 90 * Converts an integer into an IPv4 address.
101 * 91 *
102 * @param address an integer representing an IP value 92 * @param address an integer representing an IP value
103 - * @return an IP address
104 - */
105 - public static IpPrefix valueOf(int address) {
106 - return new IpPrefix(Version.INET, bytes(address));
107 - }
108 -
109 - /**
110 - * Converts an integer into an IPv4 address.
111 - *
112 - * @param address an integer representing an IP value
113 * @param netmask the CIDR value subnet mask 93 * @param netmask the CIDR value subnet mask
114 * @return an IP address 94 * @return an IP address
115 */ 95 */
...@@ -127,7 +107,7 @@ public final class IpPrefix { ...@@ -127,7 +107,7 @@ public final class IpPrefix {
127 */ 107 */
128 public static IpPrefix valueOf(String address) { 108 public static IpPrefix valueOf(String address) {
129 109
130 - final String [] parts = address.split("\\/"); 110 + final String[] parts = address.split("\\/");
131 if (parts.length > 2) { 111 if (parts.length > 2) {
132 throw new IllegalArgumentException("Malformed IP address string; " 112 throw new IllegalArgumentException("Malformed IP address string; "
133 + "Address must take form \"x.x.x.x\" or \"x.x.x.x/y\""); 113 + "Address must take form \"x.x.x.x\" or \"x.x.x.x/y\"");
...@@ -136,20 +116,20 @@ public final class IpPrefix { ...@@ -136,20 +116,20 @@ public final class IpPrefix {
136 int mask = DEFAULT_MASK; 116 int mask = DEFAULT_MASK;
137 if (parts.length == 2) { 117 if (parts.length == 2) {
138 mask = Integer.parseInt(parts[1]); 118 mask = Integer.parseInt(parts[1]);
139 - if (mask > MAX_INET_MASK) { 119 + if (mask > MAX_INET_MASK_LENGTH) {
140 throw new IllegalArgumentException( 120 throw new IllegalArgumentException(
141 "Value of subnet mask cannot exceed " 121 "Value of subnet mask cannot exceed "
142 - + MAX_INET_MASK); 122 + + MAX_INET_MASK_LENGTH);
143 } 123 }
144 } 124 }
145 125
146 - final String [] net = parts[0].split("\\."); 126 + final String[] net = parts[0].split("\\.");
147 - if (net.length != INET_LEN) { 127 + if (net.length != IpAddress.INET_BYTE_LENGTH) {
148 throw new IllegalArgumentException("Malformed IP address string; " 128 throw new IllegalArgumentException("Malformed IP address string; "
149 + "Address must have four decimal values separated by dots (.)"); 129 + "Address must have four decimal values separated by dots (.)");
150 } 130 }
151 - final byte [] bytes = new byte[INET_LEN]; 131 + final byte[] bytes = new byte[IpAddress.INET_BYTE_LENGTH];
152 - for (int i = 0; i < INET_LEN; i++) { 132 + for (int i = 0; i < IpAddress.INET_BYTE_LENGTH; i++) {
153 bytes[i] = (byte) Short.parseShort(net[i], 10); 133 bytes[i] = (byte) Short.parseShort(net[i], 10);
154 } 134 }
155 return new IpPrefix(Version.INET, bytes, mask); 135 return new IpPrefix(Version.INET, bytes, mask);
...@@ -170,7 +150,7 @@ public final class IpPrefix { ...@@ -170,7 +150,7 @@ public final class IpPrefix {
170 * @return a byte array 150 * @return a byte array
171 */ 151 */
172 public byte[] toOctets() { 152 public byte[] toOctets() {
173 - return Arrays.copyOf(this.octets, INET_LEN); 153 + return Arrays.copyOf(this.octets, IpAddress.INET_BYTE_LENGTH);
174 } 154 }
175 155
176 /** 156 /**
...@@ -202,18 +182,17 @@ public final class IpPrefix { ...@@ -202,18 +182,17 @@ public final class IpPrefix {
202 * @return an integer bitmask 182 * @return an integer bitmask
203 */ 183 */
204 private int mask() { 184 private int mask() {
205 - int shift = MAX_INET_MASK - this.netmask; 185 + int shift = MAX_INET_MASK_LENGTH - this.netmask;
206 return ((Integer.MAX_VALUE >>> (shift - 1)) << shift); 186 return ((Integer.MAX_VALUE >>> (shift - 1)) << shift);
207 } 187 }
208 188
209 /** 189 /**
210 - * Returns the subnet mask in IpAddress form. The netmask value for 190 + * Returns the subnet mask in IpAddress form.
211 - * the returned IpAddress is 0, as the address itself is a mask.
212 * 191 *
213 - * @return the subnet mask 192 + * @return the subnet mask as an IpAddress
214 */ 193 */
215 - public IpPrefix netmask() { 194 + public IpAddress netmask() {
216 - return new IpPrefix(Version.INET, bytes(mask())); 195 + return IpAddress.valueOf(mask());
217 } 196 }
218 197
219 /** 198 /**
...@@ -228,9 +207,9 @@ public final class IpPrefix { ...@@ -228,9 +207,9 @@ public final class IpPrefix {
228 return new IpPrefix(version, ANY, DEFAULT_MASK); 207 return new IpPrefix(version, ANY, DEFAULT_MASK);
229 } 208 }
230 209
231 - byte [] net = new byte [4]; 210 + byte[] net = new byte [4];
232 - byte [] mask = bytes(mask()); 211 + byte[] mask = bytes(mask());
233 - for (int i = 0; i < INET_LEN; i++) { 212 + for (int i = 0; i < IpAddress.INET_BYTE_LENGTH; i++) {
234 net[i] = (byte) (octets[i] & mask[i]); 213 net[i] = (byte) (octets[i] & mask[i]);
235 } 214 }
236 return new IpPrefix(version, net, netmask); 215 return new IpPrefix(version, net, netmask);
...@@ -249,9 +228,9 @@ public final class IpPrefix { ...@@ -249,9 +228,9 @@ public final class IpPrefix {
249 new IpPrefix(version, octets, netmask); 228 new IpPrefix(version, octets, netmask);
250 } 229 }
251 230
252 - byte [] host = new byte [INET_LEN]; 231 + byte[] host = new byte [IpAddress.INET_BYTE_LENGTH];
253 - byte [] mask = bytes(mask()); 232 + byte[] mask = bytes(mask());
254 - for (int i = 0; i < INET_LEN; i++) { 233 + for (int i = 0; i < IpAddress.INET_BYTE_LENGTH; i++) {
255 host[i] = (byte) (octets[i] & ~mask[i]); 234 host[i] = (byte) (octets[i] & ~mask[i]);
256 } 235 }
257 return new IpPrefix(version, host, netmask); 236 return new IpPrefix(version, host, netmask);
...@@ -283,7 +262,7 @@ public final class IpPrefix { ...@@ -283,7 +262,7 @@ public final class IpPrefix {
283 public boolean contains(IpPrefix other) { 262 public boolean contains(IpPrefix other) {
284 if (this.netmask <= other.netmask) { 263 if (this.netmask <= other.netmask) {
285 // Special case where they're both /32 addresses 264 // Special case where they're both /32 addresses
286 - if (this.netmask == MAX_INET_MASK) { 265 + if (this.netmask == MAX_INET_MASK_LENGTH) {
287 return Arrays.equals(octets, other.octets); 266 return Arrays.equals(octets, other.octets);
288 } 267 }
289 268
...@@ -302,7 +281,7 @@ public final class IpPrefix { ...@@ -302,7 +281,7 @@ public final class IpPrefix {
302 IpPrefix meMasked = network(); 281 IpPrefix meMasked = network();
303 282
304 IpPrefix otherMasked = 283 IpPrefix otherMasked =
305 - IpPrefix.valueOf(address.octets, netmask).network(); 284 + IpPrefix.valueOf(address.toOctets(), netmask).network();
306 285
307 return Arrays.equals(meMasked.octets, otherMasked.octets); 286 return Arrays.equals(meMasked.octets, otherMasked.octets);
308 } 287 }
...@@ -364,5 +343,4 @@ public final class IpPrefix { ...@@ -364,5 +343,4 @@ public final class IpPrefix {
364 } 343 }
365 return builder.toString(); 344 return builder.toString();
366 } 345 }
367 -
368 } 346 }
......
...@@ -32,15 +32,15 @@ public class IpPrefixTest { ...@@ -32,15 +32,15 @@ public class IpPrefixTest {
32 private static final byte [] BYTES2 = new byte [] {0xa, 0x0, 0x0, 0xb}; 32 private static final byte [] BYTES2 = new byte [] {0xa, 0x0, 0x0, 0xb};
33 private static final int INTVAL1 = 167772170; 33 private static final int INTVAL1 = 167772170;
34 private static final int INTVAL2 = 167772171; 34 private static final int INTVAL2 = 167772171;
35 - private static final String STRVAL = "10.0.0.12"; 35 + private static final String STRVAL = "10.0.0.12/16";
36 - private static final int MASK = 16; 36 + private static final int MASK_LENGTH = 16;
37 37
38 @Test 38 @Test
39 public void testEquality() { 39 public void testEquality() {
40 - IpPrefix ip1 = IpPrefix.valueOf(BYTES1); 40 + IpPrefix ip1 = IpPrefix.valueOf(BYTES1, IpPrefix.MAX_INET_MASK_LENGTH);
41 - IpPrefix ip2 = IpPrefix.valueOf(INTVAL1); 41 + IpPrefix ip2 = IpPrefix.valueOf(INTVAL1, IpPrefix.MAX_INET_MASK_LENGTH);
42 - IpPrefix ip3 = IpPrefix.valueOf(BYTES2); 42 + IpPrefix ip3 = IpPrefix.valueOf(BYTES2, IpPrefix.MAX_INET_MASK_LENGTH);
43 - IpPrefix ip4 = IpPrefix.valueOf(INTVAL2); 43 + IpPrefix ip4 = IpPrefix.valueOf(INTVAL2, IpPrefix.MAX_INET_MASK_LENGTH);
44 IpPrefix ip5 = IpPrefix.valueOf(STRVAL); 44 IpPrefix ip5 = IpPrefix.valueOf(STRVAL);
45 45
46 new EqualsTester().addEqualityGroup(ip1, ip2) 46 new EqualsTester().addEqualityGroup(ip1, ip2)
...@@ -49,21 +49,21 @@ public class IpPrefixTest { ...@@ -49,21 +49,21 @@ public class IpPrefixTest {
49 .testEquals(); 49 .testEquals();
50 50
51 // string conversions 51 // string conversions
52 - IpPrefix ip6 = IpPrefix.valueOf(BYTES1, MASK); 52 + IpPrefix ip6 = IpPrefix.valueOf(BYTES1, MASK_LENGTH);
53 IpPrefix ip7 = IpPrefix.valueOf("10.0.0.10/16"); 53 IpPrefix ip7 = IpPrefix.valueOf("10.0.0.10/16");
54 - IpPrefix ip8 = IpPrefix.valueOf(new byte [] {0xa, 0x0, 0x0, 0xc}); 54 + IpPrefix ip8 = IpPrefix.valueOf(new byte [] {0xa, 0x0, 0x0, 0xc}, 16);
55 assertEquals("incorrect address conversion", ip6, ip7); 55 assertEquals("incorrect address conversion", ip6, ip7);
56 assertEquals("incorrect address conversion", ip5, ip8); 56 assertEquals("incorrect address conversion", ip5, ip8);
57 } 57 }
58 58
59 @Test 59 @Test
60 public void basics() { 60 public void basics() {
61 - IpPrefix ip1 = IpPrefix.valueOf(BYTES1, MASK); 61 + IpPrefix ip1 = IpPrefix.valueOf(BYTES1, MASK_LENGTH);
62 final byte [] bytes = new byte [] {0xa, 0x0, 0x0, 0xa}; 62 final byte [] bytes = new byte [] {0xa, 0x0, 0x0, 0xa};
63 63
64 //check fields 64 //check fields
65 assertEquals("incorrect IP Version", Version.INET, ip1.version()); 65 assertEquals("incorrect IP Version", Version.INET, ip1.version());
66 - assertEquals("incorrect netmask", 16, ip1.netmask); 66 + assertEquals("incorrect netmask", 16, ip1.prefixLength());
67 assertTrue("faulty toOctets()", Arrays.equals(bytes, ip1.toOctets())); 67 assertTrue("faulty toOctets()", Arrays.equals(bytes, ip1.toOctets()));
68 assertEquals("faulty toInt()", INTVAL1, ip1.toInt()); 68 assertEquals("faulty toInt()", INTVAL1, ip1.toInt());
69 assertEquals("faulty toString()", "10.0.0.10/16", ip1.toString()); 69 assertEquals("faulty toString()", "10.0.0.10/16", ip1.toString());
...@@ -72,7 +72,7 @@ public class IpPrefixTest { ...@@ -72,7 +72,7 @@ public class IpPrefixTest {
72 @Test 72 @Test
73 public void netmasks() { 73 public void netmasks() {
74 // masked 74 // masked
75 - IpPrefix ip1 = IpPrefix.valueOf(BYTES1, MASK); 75 + IpPrefix ip1 = IpPrefix.valueOf(BYTES1, MASK_LENGTH);
76 76
77 IpPrefix host = IpPrefix.valueOf("0.0.0.10/16"); 77 IpPrefix host = IpPrefix.valueOf("0.0.0.10/16");
78 IpPrefix network = IpPrefix.valueOf("10.0.0.0/16"); 78 IpPrefix network = IpPrefix.valueOf("10.0.0.0/16");
...@@ -80,14 +80,6 @@ public class IpPrefixTest { ...@@ -80,14 +80,6 @@ public class IpPrefixTest {
80 assertEquals("incorrect network address", network, ip1.network()); 80 assertEquals("incorrect network address", network, ip1.network());
81 assertEquals("incorrect netmask", "255.255.0.0", ip1.netmask().toString()); 81 assertEquals("incorrect netmask", "255.255.0.0", ip1.netmask().toString());
82 82
83 - //unmasked
84 - IpPrefix ip2 = IpPrefix.valueOf(BYTES1);
85 - IpPrefix umhost = IpPrefix.valueOf("10.0.0.10/0");
86 - IpPrefix umnet = IpPrefix.valueOf("0.0.0.0/0");
87 - assertEquals("incorrect host address", umhost, ip2.host());
88 - assertEquals("incorrect host address", umnet, ip2.network());
89 - assertTrue("incorrect netmask",
90 - Arrays.equals(IpPrefix.ANY, ip2.netmask().toOctets()));
91 } 83 }
92 84
93 @Test 85 @Test
......