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.
Showing
10 changed files
with
112 additions
and
334 deletions
... | @@ -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); | ... | ... |
... | @@ -15,147 +15,76 @@ | ... | @@ -15,147 +15,76 @@ |
15 | */ | 15 | */ |
16 | package org.onlab.packet; | 16 | package org.onlab.packet; |
17 | 17 | ||
18 | +import java.nio.ByteBuffer; | ||
18 | import java.util.Arrays; | 19 | import java.util.Arrays; |
19 | 20 | ||
20 | - | ||
21 | - | ||
22 | /** | 21 | /** |
23 | * A class representing an IPv4 address. | 22 | * A class representing an IPv4 address. |
24 | - * <p/> | ||
25 | - * TODO this class is a clone of IpPrefix and still needs to be modified to | ||
26 | - * look more like an IpAddress. | ||
27 | */ | 23 | */ |
28 | public final class IpAddress implements Comparable<IpAddress> { | 24 | public final class IpAddress implements Comparable<IpAddress> { |
29 | - | 25 | + // IP Versions |
30 | - // TODO a comparator for netmasks? E.g. for sorting by prefix match order. | ||
31 | - | ||
32 | - //IP Versions | ||
33 | public enum Version { INET, INET6 }; | 26 | public enum Version { INET, INET6 }; |
34 | 27 | ||
35 | - //lengths of address, in bytes | 28 | + // lengths of address, in bytes |
36 | - public static final int INET_LEN = 4; | 29 | + public static final int INET_BYTE_LENGTH = 4; |
37 | - public static final int INET6_LEN = 16; | 30 | + public static final int INET_BIT_LENGTH = INET_BYTE_LENGTH * Byte.SIZE; |
31 | + public static final int INET6_BYTE_LENGTH = 16; | ||
32 | + public static final int INET6_BIT_LENGTH = INET6_BYTE_LENGTH * Byte.SIZE; | ||
38 | 33 | ||
39 | - //maximum CIDR value | 34 | + private final Version version; |
40 | - public static final int MAX_INET_MASK = 32; | 35 | + private final byte[] octets; |
41 | - //no mask (no network), e.g. a simple address | ||
42 | - public static final int DEFAULT_MASK = 0; | ||
43 | 36 | ||
44 | /** | 37 | /** |
45 | - * Default value indicating an unspecified address. | 38 | + * Constructor for given IP address version and address octets. |
39 | + * | ||
40 | + * @param ver the IP address version | ||
41 | + * @param octets the IP address octets | ||
46 | */ | 42 | */ |
47 | - static final byte[] ANY = new byte [] {0, 0, 0, 0}; | ||
48 | - | ||
49 | - protected Version version; | ||
50 | - | ||
51 | - protected byte[] octets; | ||
52 | - protected int netmask; | ||
53 | - | ||
54 | - private IpAddress(Version ver, byte[] octets, int netmask) { | ||
55 | - this.version = ver; | ||
56 | - this.octets = Arrays.copyOf(octets, INET_LEN); | ||
57 | - this.netmask = netmask; | ||
58 | - } | ||
59 | - | ||
60 | private IpAddress(Version ver, byte[] octets) { | 43 | private IpAddress(Version ver, byte[] octets) { |
61 | this.version = ver; | 44 | this.version = ver; |
62 | - this.octets = Arrays.copyOf(octets, INET_LEN); | 45 | + this.octets = Arrays.copyOf(octets, INET_BYTE_LENGTH); |
63 | - this.netmask = DEFAULT_MASK; | ||
64 | } | 46 | } |
65 | 47 | ||
66 | /** | 48 | /** |
67 | * Converts a byte array into an IP address. | 49 | * Converts a byte array into an IP address. |
68 | * | 50 | * |
69 | - * @param address a byte array | 51 | + * @param address the IP address value stored in network byte order |
52 | + * (i.e., the most significant byte first) | ||
70 | * @return an IP address | 53 | * @return an IP address |
71 | */ | 54 | */ |
72 | - public static IpAddress valueOf(byte [] address) { | 55 | + public static IpAddress valueOf(byte[] address) { |
73 | return new IpAddress(Version.INET, address); | 56 | return new IpAddress(Version.INET, address); |
74 | } | 57 | } |
75 | 58 | ||
76 | /** | 59 | /** |
77 | - * Converts a byte array into an IP address. | ||
78 | - * | ||
79 | - * @param address a byte array | ||
80 | - * @param netmask the CIDR value subnet mask | ||
81 | - * @return an IP address | ||
82 | - */ | ||
83 | - public static IpAddress valueOf(byte [] address, int netmask) { | ||
84 | - return new IpAddress(Version.INET, address, netmask); | ||
85 | - } | ||
86 | - | ||
87 | - /** | ||
88 | - * Helper to convert an integer into a byte array. | ||
89 | - * | ||
90 | - * @param address the integer to convert | ||
91 | - * @return a byte array | ||
92 | - */ | ||
93 | - private static byte [] bytes(int address) { | ||
94 | - byte [] bytes = new byte [INET_LEN]; | ||
95 | - for (int i = 0; i < INET_LEN; i++) { | ||
96 | - bytes[i] = (byte) ((address >> (INET_LEN - (i + 1)) * 8) & 0xff); | ||
97 | - } | ||
98 | - | ||
99 | - return bytes; | ||
100 | - } | ||
101 | - | ||
102 | - /** | ||
103 | * Converts an integer into an IPv4 address. | 60 | * Converts an integer into an IPv4 address. |
104 | * | 61 | * |
105 | - * @param address an integer representing an IP value | 62 | + * @param address an integer representing an IPv4 value |
106 | * @return an IP address | 63 | * @return an IP address |
107 | */ | 64 | */ |
108 | public static IpAddress valueOf(int address) { | 65 | public static IpAddress valueOf(int address) { |
109 | - return new IpAddress(Version.INET, bytes(address)); | 66 | + byte[] bytes = |
110 | - } | 67 | + ByteBuffer.allocate(INET_BYTE_LENGTH).putInt(address).array(); |
111 | - | 68 | + return new IpAddress(Version.INET, bytes); |
112 | - /** | ||
113 | - * Converts an integer into an IPv4 address. | ||
114 | - * | ||
115 | - * @param address an integer representing an IP value | ||
116 | - * @param netmask the CIDR value subnet mask | ||
117 | - * @return an IP address | ||
118 | - */ | ||
119 | - public static IpAddress valueOf(int address, int netmask) { | ||
120 | - return new IpAddress(Version.INET, bytes(address), netmask); | ||
121 | } | 69 | } |
122 | 70 | ||
123 | /** | 71 | /** |
124 | - * Converts a dotted-decimal string (x.x.x.x) into an IPv4 address. The | 72 | + * Converts a dotted-decimal string (x.x.x.x) into an IPv4 address. |
125 | - * string can also be in CIDR (slash) notation. If the netmask is omitted, | ||
126 | - * it will be set to DEFAULT_MASK (0). | ||
127 | * | 73 | * |
128 | - * @param address a IP address in string form, e.g. "10.0.0.1", "10.0.0.1/24" | 74 | + * @param address a IP address in string form, e.g. "10.0.0.1". |
129 | * @return an IP address | 75 | * @return an IP address |
130 | */ | 76 | */ |
131 | public static IpAddress valueOf(String address) { | 77 | public static IpAddress valueOf(String address) { |
132 | - | 78 | + final String[] net = address.split("\\."); |
133 | - final String [] parts = address.split("\\/"); | 79 | + if (net.length != INET_BYTE_LENGTH) { |
134 | - if (parts.length > 2) { | ||
135 | - throw new IllegalArgumentException("Malformed IP address string; " | ||
136 | - + "Address must take form \"x.x.x.x\" or \"x.x.x.x/y\""); | ||
137 | - } | ||
138 | - | ||
139 | - int mask = DEFAULT_MASK; | ||
140 | - if (parts.length == 2) { | ||
141 | - mask = Integer.parseInt(parts[1]); | ||
142 | - if (mask > MAX_INET_MASK) { | ||
143 | - throw new IllegalArgumentException( | ||
144 | - "Value of subnet mask cannot exceed " | ||
145 | - + MAX_INET_MASK); | ||
146 | - } | ||
147 | - } | ||
148 | - | ||
149 | - final String [] net = parts[0].split("\\."); | ||
150 | - if (net.length != INET_LEN) { | ||
151 | throw new IllegalArgumentException("Malformed IP address string; " | 80 | throw new IllegalArgumentException("Malformed IP address string; " |
152 | + "Address must have four decimal values separated by dots (.)"); | 81 | + "Address must have four decimal values separated by dots (.)"); |
153 | } | 82 | } |
154 | - final byte [] bytes = new byte[INET_LEN]; | 83 | + final byte[] bytes = new byte[INET_BYTE_LENGTH]; |
155 | - for (int i = 0; i < INET_LEN; i++) { | 84 | + for (int i = 0; i < INET_BYTE_LENGTH; i++) { |
156 | bytes[i] = (byte) Short.parseShort(net[i], 10); | 85 | bytes[i] = (byte) Short.parseShort(net[i], 10); |
157 | } | 86 | } |
158 | - return new IpAddress(Version.INET, bytes, mask); | 87 | + return new IpAddress(Version.INET, bytes); |
159 | } | 88 | } |
160 | 89 | ||
161 | /** | 90 | /** |
... | @@ -173,16 +102,7 @@ public final class IpAddress implements Comparable<IpAddress> { | ... | @@ -173,16 +102,7 @@ public final class IpAddress implements Comparable<IpAddress> { |
173 | * @return a byte array | 102 | * @return a byte array |
174 | */ | 103 | */ |
175 | public byte[] toOctets() { | 104 | public byte[] toOctets() { |
176 | - return Arrays.copyOf(this.octets, INET_LEN); | 105 | + return Arrays.copyOf(this.octets, INET_BYTE_LENGTH); |
177 | - } | ||
178 | - | ||
179 | - /** | ||
180 | - * Returns the IP address prefix length. | ||
181 | - * | ||
182 | - * @return prefix length | ||
183 | - */ | ||
184 | - public int prefixLength() { | ||
185 | - return netmask; | ||
186 | } | 106 | } |
187 | 107 | ||
188 | /** | 108 | /** |
... | @@ -191,110 +111,8 @@ public final class IpAddress implements Comparable<IpAddress> { | ... | @@ -191,110 +111,8 @@ public final class IpAddress implements Comparable<IpAddress> { |
191 | * @return the IP address's value as an integer | 111 | * @return the IP address's value as an integer |
192 | */ | 112 | */ |
193 | public int toInt() { | 113 | public int toInt() { |
194 | - int val = 0; | 114 | + ByteBuffer bb = ByteBuffer.wrap(octets); |
195 | - for (int i = 0; i < octets.length; i++) { | 115 | + return bb.getInt(); |
196 | - val <<= 8; | ||
197 | - val |= octets[i] & 0xff; | ||
198 | - } | ||
199 | - return val; | ||
200 | - } | ||
201 | - | ||
202 | - /** | ||
203 | - * Converts the IP address to a /32 IP prefix. | ||
204 | - * | ||
205 | - * @return the new IP prefix | ||
206 | - */ | ||
207 | - public IpPrefix toPrefix() { | ||
208 | - return IpPrefix.valueOf(octets, MAX_INET_MASK); | ||
209 | - } | ||
210 | - | ||
211 | - /** | ||
212 | - * Helper for computing the mask value from CIDR. | ||
213 | - * | ||
214 | - * @return an integer bitmask | ||
215 | - */ | ||
216 | - private int mask() { | ||
217 | - int shift = MAX_INET_MASK - this.netmask; | ||
218 | - return ((Integer.MAX_VALUE >>> (shift - 1)) << shift); | ||
219 | - } | ||
220 | - | ||
221 | - /** | ||
222 | - * Returns the subnet mask in IpAddress form. The netmask value for | ||
223 | - * the returned IpAddress is 0, as the address itself is a mask. | ||
224 | - * | ||
225 | - * @return the subnet mask | ||
226 | - */ | ||
227 | - public IpAddress netmask() { | ||
228 | - return new IpAddress(Version.INET, bytes(mask())); | ||
229 | - } | ||
230 | - | ||
231 | - /** | ||
232 | - * Returns the network portion of this address as an IpAddress. | ||
233 | - * The netmask of the returned IpAddress is the current mask. If this | ||
234 | - * address doesn't have a mask, this returns an all-0 IpAddress. | ||
235 | - * | ||
236 | - * @return the network address or null | ||
237 | - */ | ||
238 | - public IpAddress network() { | ||
239 | - if (netmask == DEFAULT_MASK) { | ||
240 | - return new IpAddress(version, ANY, DEFAULT_MASK); | ||
241 | - } | ||
242 | - | ||
243 | - byte [] net = new byte [4]; | ||
244 | - byte [] mask = bytes(mask()); | ||
245 | - for (int i = 0; i < INET_LEN; i++) { | ||
246 | - net[i] = (byte) (octets[i] & mask[i]); | ||
247 | - } | ||
248 | - return new IpAddress(version, net, netmask); | ||
249 | - } | ||
250 | - | ||
251 | - /** | ||
252 | - * Returns the host portion of the IPAddress, as an IPAddress. | ||
253 | - * The netmask of the returned IpAddress is the current mask. If this | ||
254 | - * address doesn't have a mask, this returns a copy of the current | ||
255 | - * address. | ||
256 | - * | ||
257 | - * @return the host address | ||
258 | - */ | ||
259 | - public IpAddress host() { | ||
260 | - if (netmask == DEFAULT_MASK) { | ||
261 | - new IpAddress(version, octets, netmask); | ||
262 | - } | ||
263 | - | ||
264 | - byte [] host = new byte [INET_LEN]; | ||
265 | - byte [] mask = bytes(mask()); | ||
266 | - for (int i = 0; i < INET_LEN; i++) { | ||
267 | - host[i] = (byte) (octets[i] & ~mask[i]); | ||
268 | - } | ||
269 | - return new IpAddress(version, host, netmask); | ||
270 | - } | ||
271 | - | ||
272 | - public boolean isMasked() { | ||
273 | - return mask() != 0; | ||
274 | - } | ||
275 | - | ||
276 | - /** | ||
277 | - * Determines whether a given address is contained within this IpAddress' | ||
278 | - * network. | ||
279 | - * | ||
280 | - * @param other another IP address that could be contained in this network | ||
281 | - * @return true if the other IP address is contained in this address' | ||
282 | - * network, otherwise false | ||
283 | - */ | ||
284 | - public boolean contains(IpAddress other) { | ||
285 | - if (this.netmask <= other.netmask) { | ||
286 | - // Special case where they're both /32 addresses | ||
287 | - if (this.netmask == MAX_INET_MASK) { | ||
288 | - return Arrays.equals(octets, other.octets); | ||
289 | - } | ||
290 | - | ||
291 | - // Mask the other address with our network mask | ||
292 | - IpAddress otherMasked = | ||
293 | - IpAddress.valueOf(other.octets, netmask).network(); | ||
294 | - | ||
295 | - return network().equals(otherMasked); | ||
296 | - } | ||
297 | - return false; | ||
298 | } | 116 | } |
299 | 117 | ||
300 | @Override | 118 | @Override |
... | @@ -308,7 +126,6 @@ public final class IpAddress implements Comparable<IpAddress> { | ... | @@ -308,7 +126,6 @@ public final class IpAddress implements Comparable<IpAddress> { |
308 | public int hashCode() { | 126 | public int hashCode() { |
309 | final int prime = 31; | 127 | final int prime = 31; |
310 | int result = 1; | 128 | int result = 1; |
311 | - result = prime * result + netmask; | ||
312 | result = prime * result + Arrays.hashCode(octets); | 129 | result = prime * result + Arrays.hashCode(octets); |
313 | result = prime * result + ((version == null) ? 0 : version.hashCode()); | 130 | result = prime * result + ((version == null) ? 0 : version.hashCode()); |
314 | return result; | 131 | return result; |
... | @@ -326,9 +143,6 @@ public final class IpAddress implements Comparable<IpAddress> { | ... | @@ -326,9 +143,6 @@ public final class IpAddress implements Comparable<IpAddress> { |
326 | return false; | 143 | return false; |
327 | } | 144 | } |
328 | IpAddress other = (IpAddress) obj; | 145 | IpAddress other = (IpAddress) obj; |
329 | - if (netmask != other.netmask) { | ||
330 | - return false; | ||
331 | - } | ||
332 | if (!Arrays.equals(octets, other.octets)) { | 146 | if (!Arrays.equals(octets, other.octets)) { |
333 | return false; | 147 | return false; |
334 | } | 148 | } |
... | @@ -341,8 +155,7 @@ public final class IpAddress implements Comparable<IpAddress> { | ... | @@ -341,8 +155,7 @@ public final class IpAddress implements Comparable<IpAddress> { |
341 | @Override | 155 | @Override |
342 | /* | 156 | /* |
343 | * (non-Javadoc) | 157 | * (non-Javadoc) |
344 | - * format is "x.x.x.x" for non-masked (netmask 0) addresses, | 158 | + * format is "x.x.x.x" for IPv4 addresses. |
345 | - * and "x.x.x.x/y" for masked addresses. | ||
346 | * | 159 | * |
347 | * @see java.lang.Object#toString() | 160 | * @see java.lang.Object#toString() |
348 | */ | 161 | */ |
... | @@ -354,11 +167,6 @@ public final class IpAddress implements Comparable<IpAddress> { | ... | @@ -354,11 +167,6 @@ public final class IpAddress implements Comparable<IpAddress> { |
354 | } | 167 | } |
355 | builder.append(String.format("%d", b & 0xff)); | 168 | builder.append(String.format("%d", b & 0xff)); |
356 | } | 169 | } |
357 | - if (netmask != DEFAULT_MASK) { | ||
358 | - builder.append("/"); | ||
359 | - builder.append(String.format("%d", netmask)); | ||
360 | - } | ||
361 | return builder.toString(); | 170 | return builder.toString(); |
362 | } | 171 | } |
363 | - | ||
364 | } | 172 | } | ... | ... |
... | @@ -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 | ... | ... |
-
Please register or login to post a comment