Skip to content

Commit bce2289

Browse files
author
alexsa
committed
Format code
1 parent 4698204 commit bce2289

File tree

6 files changed

+303
-296
lines changed

6 files changed

+303
-296
lines changed
Lines changed: 148 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,157 @@
11
package com.datastax.oss.driver.internal.core.addresstranslation;
22

33
import com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting;
4-
54
import java.net.InetAddress;
65
import java.net.UnknownHostException;
76
import java.util.Arrays;
87

98
class Subnet {
10-
private final byte[] subnet;
11-
private final byte[] networkMask;
12-
private final byte[] upper;
13-
private final byte[] lower;
14-
15-
private Subnet(byte[] subnet, byte[] networkMask) {
16-
this.subnet = subnet;
17-
this.networkMask = networkMask;
18-
19-
byte[] upper = new byte[subnet.length];
20-
byte[] lower = new byte[subnet.length];
21-
for (int i = 0; i < subnet.length; i++) {
22-
upper[i] = (byte) (subnet[i] | ~networkMask[i]);
23-
lower[i] = (byte) (subnet[i] & networkMask[i]);
24-
}
25-
this.upper = upper;
26-
this.lower = lower;
27-
}
28-
29-
static Subnet parse(String subnetCIDR) throws UnknownHostException {
30-
String[] parts = subnetCIDR.split("/");
31-
if (parts.length != 2) {
32-
throw new IllegalArgumentException("Invalid subnet: " + subnetCIDR);
33-
}
34-
35-
boolean isIPv6 = parts[0].contains(":");
36-
byte[] subnet = InetAddress.getByName(parts[0]).getAddress();
37-
if (isIPv4(subnet) && isIPv6) {
38-
subnet = toIPv6(subnet);
39-
}
40-
int prefixLength = Integer.parseInt(parts[1]);
41-
validatePrefixLength(subnet, prefixLength);
42-
43-
byte[] networkMask = toNetworkMask(subnet, prefixLength);
44-
validateSubnetIsPrefixBlock(subnet, networkMask, subnetCIDR);
45-
return new Subnet(subnet, networkMask);
46-
}
47-
48-
private static byte[] toNetworkMask(byte[] subnet, int prefixLength) {
49-
int fullBytes = prefixLength / 8;
50-
int remainingBits = prefixLength % 8;
51-
byte[] mask = new byte[subnet.length];
52-
Arrays.fill(mask, 0, fullBytes, (byte) 0xFF);
53-
if (remainingBits > 0) {
54-
mask[fullBytes] = (byte) (0xFF << (8 - remainingBits));
55-
}
56-
return mask;
57-
}
58-
59-
private static void validatePrefixLength(byte[] subnet, int prefixLength) {
60-
int max_prefix_length = subnet.length * 8;
61-
if (prefixLength < 0 || max_prefix_length < prefixLength) {
62-
throw new IllegalArgumentException(
63-
String.format("Prefix length %s must be within [0; %s]", prefixLength, max_prefix_length));
64-
}
65-
}
66-
67-
private static void validateSubnetIsPrefixBlock(byte[] subnet, byte[] networkMask, String subnetCIDR) {
68-
byte[] prefixBlock = toPrefixBlock(subnet, networkMask);
69-
if (!Arrays.equals(subnet, prefixBlock)) {
70-
throw new IllegalArgumentException(
71-
String.format("Subnet %s must be represented as a network prefix block", subnetCIDR));
72-
}
73-
}
74-
75-
private static byte[] toPrefixBlock(byte[] subnet, byte[] networkMask) {
76-
byte[] prefixBlock = new byte[subnet.length];
77-
for (int i = 0; i < subnet.length; i++) {
78-
prefixBlock[i] = (byte) (subnet[i] & networkMask[i]);
79-
}
80-
return prefixBlock;
81-
}
82-
83-
@VisibleForTesting
84-
byte[] getSubnet() {
85-
return Arrays.copyOf(subnet, subnet.length);
86-
}
87-
88-
@VisibleForTesting
89-
byte[] getNetworkMask() {
90-
return Arrays.copyOf(networkMask, networkMask.length);
91-
}
92-
93-
byte[] getUpper() {
94-
return Arrays.copyOf(upper, upper.length);
95-
}
96-
97-
byte[] getLower() {
98-
return Arrays.copyOf(lower, lower.length);
99-
}
100-
101-
boolean isIPv4() {
102-
return isIPv4(subnet);
103-
}
104-
105-
boolean isIPv6() {
106-
return isIPv6(subnet);
107-
}
108-
109-
boolean contains(byte[] ip) {
110-
if (isIPv4() && !isIPv4(ip)) {
111-
return false;
112-
}
113-
if (isIPv6() && isIPv4(ip)) {
114-
ip = toIPv6(ip);
115-
}
116-
if (subnet.length != ip.length) {
117-
throw new IllegalArgumentException("IP version is unknown: " + Arrays.toString(toZeroBasedByteArray(ip)));
118-
}
119-
for (int i = 0; i < subnet.length; i++) {
120-
if (subnet[i] != (byte) (ip[i] & networkMask[i])) {
121-
return false;
122-
}
123-
}
124-
return true;
125-
}
126-
127-
private static boolean isIPv4(byte[] ip) {
128-
return ip.length == 4;
129-
}
130-
131-
private static boolean isIPv6(byte[] ip) {
132-
return ip.length == 16;
133-
}
134-
135-
private static byte[] toIPv6(byte[] ipv4) {
136-
byte[] ipv6 = new byte[16];
137-
ipv6[10] = (byte) 0xFF;
138-
ipv6[11] = (byte) 0xFF;
139-
System.arraycopy(ipv4, 0, ipv6, 12, 4);
140-
return ipv6;
141-
}
142-
143-
@Override
144-
public String toString() {
145-
return Arrays.toString(toZeroBasedByteArray(subnet));
146-
}
147-
148-
private static int[] toZeroBasedByteArray(byte[] bytes) {
149-
int[] res = new int[bytes.length];
150-
for (int i = 0; i < bytes.length; i++) {
151-
res[i] = bytes[i] & 0xFF;
152-
}
153-
return res;
154-
}
9+
private final byte[] subnet;
10+
private final byte[] networkMask;
11+
private final byte[] upper;
12+
private final byte[] lower;
13+
14+
private Subnet(byte[] subnet, byte[] networkMask) {
15+
this.subnet = subnet;
16+
this.networkMask = networkMask;
17+
18+
byte[] upper = new byte[subnet.length];
19+
byte[] lower = new byte[subnet.length];
20+
for (int i = 0; i < subnet.length; i++) {
21+
upper[i] = (byte) (subnet[i] | ~networkMask[i]);
22+
lower[i] = (byte) (subnet[i] & networkMask[i]);
23+
}
24+
this.upper = upper;
25+
this.lower = lower;
26+
}
27+
28+
static Subnet parse(String subnetCIDR) throws UnknownHostException {
29+
String[] parts = subnetCIDR.split("/");
30+
if (parts.length != 2) {
31+
throw new IllegalArgumentException("Invalid subnet: " + subnetCIDR);
32+
}
33+
34+
boolean isIPv6 = parts[0].contains(":");
35+
byte[] subnet = InetAddress.getByName(parts[0]).getAddress();
36+
if (isIPv4(subnet) && isIPv6) {
37+
subnet = toIPv6(subnet);
38+
}
39+
int prefixLength = Integer.parseInt(parts[1]);
40+
validatePrefixLength(subnet, prefixLength);
41+
42+
byte[] networkMask = toNetworkMask(subnet, prefixLength);
43+
validateSubnetIsPrefixBlock(subnet, networkMask, subnetCIDR);
44+
return new Subnet(subnet, networkMask);
45+
}
46+
47+
private static byte[] toNetworkMask(byte[] subnet, int prefixLength) {
48+
int fullBytes = prefixLength / 8;
49+
int remainingBits = prefixLength % 8;
50+
byte[] mask = new byte[subnet.length];
51+
Arrays.fill(mask, 0, fullBytes, (byte) 0xFF);
52+
if (remainingBits > 0) {
53+
mask[fullBytes] = (byte) (0xFF << (8 - remainingBits));
54+
}
55+
return mask;
56+
}
57+
58+
private static void validatePrefixLength(byte[] subnet, int prefixLength) {
59+
int max_prefix_length = subnet.length * 8;
60+
if (prefixLength < 0 || max_prefix_length < prefixLength) {
61+
throw new IllegalArgumentException(
62+
String.format(
63+
"Prefix length %s must be within [0; %s]", prefixLength, max_prefix_length));
64+
}
65+
}
66+
67+
private static void validateSubnetIsPrefixBlock(
68+
byte[] subnet, byte[] networkMask, String subnetCIDR) {
69+
byte[] prefixBlock = toPrefixBlock(subnet, networkMask);
70+
if (!Arrays.equals(subnet, prefixBlock)) {
71+
throw new IllegalArgumentException(
72+
String.format("Subnet %s must be represented as a network prefix block", subnetCIDR));
73+
}
74+
}
75+
76+
private static byte[] toPrefixBlock(byte[] subnet, byte[] networkMask) {
77+
byte[] prefixBlock = new byte[subnet.length];
78+
for (int i = 0; i < subnet.length; i++) {
79+
prefixBlock[i] = (byte) (subnet[i] & networkMask[i]);
80+
}
81+
return prefixBlock;
82+
}
83+
84+
@VisibleForTesting
85+
byte[] getSubnet() {
86+
return Arrays.copyOf(subnet, subnet.length);
87+
}
88+
89+
@VisibleForTesting
90+
byte[] getNetworkMask() {
91+
return Arrays.copyOf(networkMask, networkMask.length);
92+
}
93+
94+
byte[] getUpper() {
95+
return Arrays.copyOf(upper, upper.length);
96+
}
97+
98+
byte[] getLower() {
99+
return Arrays.copyOf(lower, lower.length);
100+
}
101+
102+
boolean isIPv4() {
103+
return isIPv4(subnet);
104+
}
105+
106+
boolean isIPv6() {
107+
return isIPv6(subnet);
108+
}
109+
110+
boolean contains(byte[] ip) {
111+
if (isIPv4() && !isIPv4(ip)) {
112+
return false;
113+
}
114+
if (isIPv6() && isIPv4(ip)) {
115+
ip = toIPv6(ip);
116+
}
117+
if (subnet.length != ip.length) {
118+
throw new IllegalArgumentException(
119+
"IP version is unknown: " + Arrays.toString(toZeroBasedByteArray(ip)));
120+
}
121+
for (int i = 0; i < subnet.length; i++) {
122+
if (subnet[i] != (byte) (ip[i] & networkMask[i])) {
123+
return false;
124+
}
125+
}
126+
return true;
127+
}
128+
129+
private static boolean isIPv4(byte[] ip) {
130+
return ip.length == 4;
131+
}
132+
133+
private static boolean isIPv6(byte[] ip) {
134+
return ip.length == 16;
135+
}
136+
137+
private static byte[] toIPv6(byte[] ipv4) {
138+
byte[] ipv6 = new byte[16];
139+
ipv6[10] = (byte) 0xFF;
140+
ipv6[11] = (byte) 0xFF;
141+
System.arraycopy(ipv4, 0, ipv6, 12, 4);
142+
return ipv6;
143+
}
144+
145+
@Override
146+
public String toString() {
147+
return Arrays.toString(toZeroBasedByteArray(subnet));
148+
}
149+
150+
private static int[] toZeroBasedByteArray(byte[] bytes) {
151+
int[] res = new int[bytes.length];
152+
for (int i = 0; i < bytes.length; i++) {
153+
res[i] = bytes[i] & 0xFF;
154+
}
155+
return res;
156+
}
155157
}

core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddress.java

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,45 @@
44
import java.net.UnknownHostException;
55

66
class SubnetAddress {
7-
private final Subnet subnet;
8-
private final InetSocketAddress address;
9-
10-
SubnetAddress(String subnetCIDR, InetSocketAddress address) {
11-
try {
12-
this.subnet = Subnet.parse(subnetCIDR);
13-
} catch (UnknownHostException e) {
14-
throw new RuntimeException(e);
15-
}
16-
this.address = address;
17-
}
18-
19-
InetSocketAddress getAddress() {
20-
return this.address;
21-
}
7+
private final Subnet subnet;
8+
private final InetSocketAddress address;
229

23-
boolean isOverlapping(SubnetAddress other) {
24-
Subnet thisSubnet = this.subnet;
25-
Subnet otherSubnet = other.subnet;
26-
return thisSubnet.contains(otherSubnet.getLower())
27-
|| thisSubnet.contains(otherSubnet.getUpper())
28-
|| otherSubnet.contains(thisSubnet.getLower())
29-
|| otherSubnet.contains(thisSubnet.getUpper());
10+
SubnetAddress(String subnetCIDR, InetSocketAddress address) {
11+
try {
12+
this.subnet = Subnet.parse(subnetCIDR);
13+
} catch (UnknownHostException e) {
14+
throw new RuntimeException(e);
3015
}
16+
this.address = address;
17+
}
3118

32-
boolean contains(InetSocketAddress address) {
33-
return subnet.contains(address.getAddress().getAddress());
34-
}
35-
36-
boolean isIPv4() {
37-
return subnet.isIPv4();
38-
}
39-
40-
boolean isIPv6() {
41-
return subnet.isIPv6();
42-
}
19+
InetSocketAddress getAddress() {
20+
return this.address;
21+
}
4322

44-
@Override
45-
public String toString() {
46-
return "SubnetAddress[subnet=" + subnet +
47-
", address=" + address +
48-
"]";
49-
}
23+
boolean isOverlapping(SubnetAddress other) {
24+
Subnet thisSubnet = this.subnet;
25+
Subnet otherSubnet = other.subnet;
26+
return thisSubnet.contains(otherSubnet.getLower())
27+
|| thisSubnet.contains(otherSubnet.getUpper())
28+
|| otherSubnet.contains(thisSubnet.getLower())
29+
|| otherSubnet.contains(thisSubnet.getUpper());
30+
}
31+
32+
boolean contains(InetSocketAddress address) {
33+
return subnet.contains(address.getAddress().getAddress());
34+
}
35+
36+
boolean isIPv4() {
37+
return subnet.isIPv4();
38+
}
39+
40+
boolean isIPv6() {
41+
return subnet.isIPv6();
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return "SubnetAddress[subnet=" + subnet + ", address=" + address + "]";
47+
}
5048
}

0 commit comments

Comments
 (0)