Skip to content

Commit

Permalink
Fixed wrong value returned by CIDRToIPv6 function
Browse files Browse the repository at this point in the history
  • Loading branch information
renfei committed Nov 16, 2023
1 parent 5554aaa commit b95d9df
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Or download the zip file in the warehouse and decompress it by yourself.
<dependency>
<groupId>net.renfei</groupId>
<artifactId>ip2location</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<dependency>
<groupId>net.renfei</groupId>
<artifactId>ip2location</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
</dependency>
```

Expand Down
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.renfei</groupId>
<artifactId>ip2location</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<name>ip2location</name>
<url>http://www.renfei.net</url>
<description>IP2Location Java Component enables applications to get info from IP address such as the visitor’s country, region, city, latitude, longitude, ZIP code, ISP name, domain name, time zone, connection speed, IDD code, area code, weather station code, weather station name, MCC, MNC, mobile brand name, elevation and usage type.</description>
Expand Down Expand Up @@ -57,6 +57,14 @@
<maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.13.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand Down
46 changes: 20 additions & 26 deletions src/main/java/net/renfei/ip2location/IPTools.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.renfei.ip2location;

import org.apache.commons.lang3.StringUtils;

import java.math.BigInteger;
import java.net.InetAddress;
import java.net.Inet4Address;
Expand Down Expand Up @@ -388,8 +390,7 @@ public List<String> IPv6ToCIDR(String IPFrom, String IPTo) throws UnknownHostExc

} while (ipFromBin.compareTo(ipToBin) < 0);

for (Map.Entry<String, Integer>
entry : networks.entrySet()) {
for (Map.Entry<String, Integer> entry : networks.entrySet()) {
result.add(CompressIPv6(BinaryToIP(entry.getKey())) + "/" + entry.getValue());
}

Expand Down Expand Up @@ -464,33 +465,26 @@ public String[] CIDRToIPv6(String CIDR) throws UnknownHostException {
ip = arr[0];
prefix = Integer.parseInt(arr[1]);

String hexStartAddress = ExpandIPv6(ip).replaceAll(":", "");
String hexEndAddress = hexStartAddress;
String[] parts = ExpandIPv6(ip).split("\\:");

int bits = 128 - prefix;
int x;
String y;
int pos = 31;
List<Integer> values;
char[] tmp;
while (bits > 0) {
values = Arrays.asList(4, bits);
x = Integer.parseInt(String.valueOf(hexEndAddress.charAt(pos)), 16);
y = String.format("%x", (x | (int) (Math.pow(2, Collections.min(values)) - 1))); // single hex char

// replace char
tmp = hexEndAddress.toCharArray();
tmp[pos] = y.charAt(0);
hexEndAddress = String.valueOf(tmp);

bits -= 4;
pos -= 1;
String bitStart = StringUtils.repeat('1', prefix) + StringUtils.repeat('0', 128 - prefix);
String bitEnd = StringUtils.repeat('0', prefix) + StringUtils.repeat('1', 128 - prefix);

int chunkSize = 16;

String[] floors = bitStart.split("(?<=\\G.{" + chunkSize + "})");
String[] ceilings = bitEnd.split("(?<=\\G.{" + chunkSize + "})");

List<String> startIP = new ArrayList(8);
List<String> endIP = new ArrayList(8);

for (int x = 0; x < 8; x++) {
startIP.add(Integer.toHexString(Integer.parseInt(parts[x], 16) & Integer.parseInt(floors[x], 2)));
endIP.add(Integer.toHexString(Integer.parseInt(parts[x], 16) | Integer.parseInt(ceilings[x], 2)));
}

hexStartAddress = hexStartAddress.replaceAll("(.{4})", "$1:");
hexStartAddress = hexStartAddress.substring(0, hexStartAddress.length() - 1);
hexEndAddress = hexEndAddress.replaceAll("(.{4})", "$1:");
hexEndAddress = hexEndAddress.substring(0, hexEndAddress.length() - 1);
String hexStartAddress = ExpandIPv6(StringUtils.join(startIP, ":"));
String hexEndAddress = ExpandIPv6(StringUtils.join(endIP, ":"));

return new String[]{hexStartAddress, hexEndAddress};
}
Expand Down

0 comments on commit b95d9df

Please sign in to comment.