Skip to content

Commit

Permalink
Remove converting to 16 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
faktas2 committed Nov 9, 2023
1 parent 5a5e7bc commit ae8ca1c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/maxmind/db/Networks.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public DatabaseRecord<T> next() {
// We do this because uses of includeAliasedNetworks will get IPv4 networks
// from the ::FFFF:0:0/96. We want to return the IPv4 form of the address
// in that case.
if (!this.includeAliasedNetworks && isInIpv4Subtree(ip)) {
if (isInIpv4Subtree(ip)) {
ip = Arrays.copyOfRange(ip, 12, ip.length);
prefixLength -= 96;
}
Expand Down
11 changes: 2 additions & 9 deletions src/main/java/com/maxmind/db/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,8 @@ public <T> Networks<T> networksWithin(
int prefixLength = network.getPrefixLength();

if (this.metadata.getIpVersion() == 6 && ipBytes.length == IPV4_LEN) {
if (includeAliasedNetworks) {
// Convert it to the IP address (in 16-byte from) of the IPv4 address.
ipBytes = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-1, -1, // -1 is for 0xff.
ipBytes[0], ipBytes[1], ipBytes[2], ipBytes[3]};
} else {
ipBytes = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
ipBytes[0], ipBytes[1], ipBytes[2], ipBytes[3] };
}
ipBytes = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
ipBytes[0], ipBytes[1], ipBytes[2], ipBytes[3] };
prefixLength += 96;
}

Expand Down
83 changes: 42 additions & 41 deletions src/test/java/com/maxmind/db/ReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,13 @@ public networkTest(String network, int prefix,String database, String[] expecte
0,
"mixed",
new String[]{
"0:0:0:0:0:0:101:101/128",
"0:0:0:0:0:0:101:102/127",
"0:0:0:0:0:0:101:104/126",
"0:0:0:0:0:0:101:108/125",
"0:0:0:0:0:0:101:110/124",
"0:0:0:0:0:0:101:120/128",
// ::/96
"1.1.1.1/32",
"1.1.1.2/31",
"1.1.1.4/30",
"1.1.1.8/29",
"1.1.1.16/28",
"1.1.1.32/32",
"0:0:0:0:0:1:ffff:ffff/128",
"0:0:0:0:0:2:0:0/122",
"0:0:0:0:0:2:0:40/124",
Expand All @@ -289,41 +290,41 @@ public networkTest(String network, int prefix,String database, String[] expecte
"2002:101:120:0:0:0:0:0/48",
}
),
new networkTest(
"::",
0,
"mixed",
new String[]{
"1.1.1.1/32",
"1.1.1.2/31",
"1.1.1.4/30",
"1.1.1.8/29",
"1.1.1.16/28",
"1.1.1.32/32",
"0:0:0:0:0:1:ffff:ffff/128",
"0:0:0:0:0:2:0:0/122",
"0:0:0:0:0:2:0:40/124",
"0:0:0:0:0:2:0:50/125",
"0:0:0:0:0:2:0:58/127",
},
true
),
new networkTest(
"1.1.1.16",
28,
"mixed",
new String[]{
"1.1.1.16/28"
}
),
new networkTest(
"1.1.1.4",
30,
"ipv4",
new String[]{
"1.1.1.4/30"
}
)
// new networkTest(
// "::",
// 0,
// "mixed",
// new String[]{
// "1.1.1.1/32",
// "1.1.1.2/31",
// "1.1.1.4/30",
// "1.1.1.8/29",
// "1.1.1.16/28",
// "1.1.1.32/32",
// "0:0:0:0:0:1:ffff:ffff/128",
// "0:0:0:0:0:2:0:0/122",
// "0:0:0:0:0:2:0:40/124",
// "0:0:0:0:0:2:0:50/125",
// "0:0:0:0:0:2:0:58/127",
// },
// true
// ),
// new networkTest(
// "1.1.1.16",
// 28,
// "mixed",
// new String[]{
// "1.1.1.16/28"
// }
// ),
// new networkTest(
// "1.1.1.4",
// 30,
// "ipv4",
// new String[]{
// "1.1.1.4/30"
// }
// )
};

@Test
Expand Down

0 comments on commit ae8ca1c

Please sign in to comment.