mirror of
https://github.com/lionsoul2014/ip2region.git
synced 2026-02-01 17:46:15 +00:00
IPv6 impl for java binding is ready
This commit is contained in:
parent
f226ef1f79
commit
9af9c927fe
@ -93,7 +93,9 @@ public class SearchApp {
|
||||
|
||||
Searcher searcher = createSearcher(dbPath, cachePolicy);
|
||||
final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
|
||||
System.out.printf("ip2region xdb searcher test program, cachePolicy: %s\ntype 'quit' to exit\n", cachePolicy);
|
||||
System.out.printf("ip2region xdb searcher test program\n"
|
||||
+ "source xdb: %s (%s, %s)\n"
|
||||
+ "type 'quit' to exit\n", dbPath, searcher.getIPVersion().name, cachePolicy);
|
||||
while ( true ) {
|
||||
System.out.print("ip2region>> ");
|
||||
String line = reader.readLine().trim();
|
||||
|
||||
@ -32,19 +32,23 @@ public class Util
|
||||
try {
|
||||
return InetAddress.getByAddress(ip).getHostAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
return String.format("invalid-ip-address `%s`", ipArrayString(ip));
|
||||
return String.format("invalid-ip-address `%s`", ipJoin(ip));
|
||||
}
|
||||
}
|
||||
|
||||
// implode the byte[] ip with its byte value.
|
||||
public static String ipArrayString(byte[] ip) {
|
||||
public static String ipJoin(byte[] ip) {
|
||||
return bytesToString(ip, 0, ip.length);
|
||||
}
|
||||
|
||||
public static String bytesToString(byte[] buff, int offset, int length) {
|
||||
final StringBuffer sb = new StringBuffer();
|
||||
sb.append("[");
|
||||
for (int i = 0; i < ip.length; i++) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
if (i > 0) {
|
||||
sb.append(',');
|
||||
}
|
||||
sb.append((ip[i] & 0xFF));
|
||||
sb.append((buff[offset+i] & 0xFF));
|
||||
}
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
|
||||
@ -0,0 +1,44 @@
|
||||
package org.lionsoul.ip2region.xdb;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class IPv4Test {
|
||||
|
||||
private static final Log log = Log.getLogger(IPv4Test.class);
|
||||
|
||||
@Test
|
||||
public void testIpSubCompare() throws InetAddressException {
|
||||
final byte[] sip = Util.parseIP("0.255.255.255");
|
||||
final byte[] eip = Util.parseIP("1.0.0.2");
|
||||
final byte[] buff = new byte[Version.IPv4.segmentIndexSize];
|
||||
Version.IPv4.putBytes(buff, 0, sip);
|
||||
Version.IPv4.putBytes(buff, 4, eip);
|
||||
log.infof("bytesToString(buff): %s", Util.bytesToString(buff, 0, 8));
|
||||
|
||||
final byte[] ip = Util.parseIP("1.0.0.0");
|
||||
|
||||
// compare the sip
|
||||
log.infof("ipSubCompare(%s, %s): %d",
|
||||
Util.ipToString(ip),
|
||||
Util.bytesToString(buff, 0, 4),
|
||||
Util.ipSubCompare(ip, buff, 0)
|
||||
);
|
||||
log.infof("IPv4.ipSubCompare(%s, %s): %d",
|
||||
Util.ipToString(ip),
|
||||
Util.bytesToString(buff, 0, 4),
|
||||
Version.IPv4.ipSubCompare(ip, buff, 0)
|
||||
);
|
||||
|
||||
// compare the eip
|
||||
log.infof("ipSubCompare(%s, %s): %d",
|
||||
Util.ipToString(ip),
|
||||
Util.bytesToString(buff, 4, 4),
|
||||
Util.ipSubCompare(ip, buff, 4)
|
||||
);
|
||||
log.infof("IPv4.ipSubCompare(%s, %s): %d",
|
||||
Util.ipToString(ip),
|
||||
Util.bytesToString(buff, 4, 4),
|
||||
Version.IPv4.ipSubCompare(ip, buff, 4)
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -20,7 +20,7 @@ public class UtilTest {
|
||||
|
||||
for (String ip : ips) {
|
||||
final byte[] ipBytes = Util.parseIP(ip);
|
||||
log.debugf("%s(v=%s) => %s", ip, Util.ipArrayString(ipBytes), Util.ipToString(ipBytes));
|
||||
log.debugf("%s(v=%s) => %s", ip, Util.ipJoin(ipBytes), Util.ipToString(ipBytes));
|
||||
}
|
||||
}
|
||||
|
||||
@ -42,4 +42,20 @@ public class UtilTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIpSubCompare() throws InetAddressException {
|
||||
final String[][] ipPairs = new String[][] {
|
||||
{"1.0.0.0", "1.0.0.1"},
|
||||
{"192.168.1.100", "192.168.2.100"},
|
||||
{"10.100.1.10", "11.100.2.10"},
|
||||
{"11.100.1.10", "10.100.2.10"}
|
||||
};
|
||||
|
||||
for (final String[] ips : ipPairs) {
|
||||
final byte[] ip1 = Util.parseIP(ips[0]);
|
||||
final byte[] ip2 = Util.parseIP(ips[1]);
|
||||
log.debugf("ipSubCompare(%s, %s): %d", Util.ipToString(ip1), Util.ipToString(ip2), Util.ipSubCompare(ip1, ip2, 0));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -19,4 +19,23 @@ public class VersionTest {
|
||||
log.debugf("v4: %s", v4);
|
||||
log.debugf("v6: %s", v6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromHeader() throws XdbException {
|
||||
// create the header buffer
|
||||
final byte[] buff1 = new byte[Searcher.HeaderInfoLength];
|
||||
// structure version
|
||||
LittleEndian.put(buff1, 0, 2, 2);
|
||||
LittleEndian.put(buff1, 16, Version.IPv4VersionNo, 2);
|
||||
|
||||
final byte[] buff2 = new byte[Searcher.HeaderInfoLength];
|
||||
LittleEndian.put(buff2, 0, 3, 2);
|
||||
LittleEndian.put(buff2, 16, Version.IPv6VersionNo, 2);
|
||||
|
||||
final Version ver1 = Version.fromHeader(new Header(buff1));
|
||||
final Version ver2 = Version.fromHeader(new Header(buff2));
|
||||
log.debugf("ver1: %s", ver1.toString());
|
||||
log.debugf("ver2: %s", ver2.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user