Skip to content

Commit

Permalink
Add ipv4 handling back
Browse files Browse the repository at this point in the history
  • Loading branch information
faktas2 committed Nov 14, 2023
1 parent 7c6a5e1 commit 326e6ea
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 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 (isInIpv4Subtree(ip)) {
if (!this.includeAliasedNetworks && isInIpv4Subtree(ip)) {
ip = Arrays.copyOfRange(ip, 12, ip.length);
prefixLength -= 96;
}
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/maxmind/db/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,15 @@ public <T> Networks<T> networksWithin(
int prefixLength = network.getPrefixLength();

if (this.metadata.getIpVersion() == 6 && ipBytes.length == IPV4_LEN) {
ipBytes = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
ipBytes[0], ipBytes[1], ipBytes[2], ipBytes[3] };
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] };
}
prefixLength += 96;
}

Expand Down
13 changes: 6 additions & 7 deletions src/test/java/com/maxmind/db/ReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,12 @@ public networkTest(String network, int prefix,String database, String[] expecte
0,
"mixed",
new String[]{
// ::/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: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",
"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 Down

0 comments on commit 326e6ea

Please sign in to comment.