IPv6 address support
This commit is contained in:
@@ -10,14 +10,12 @@ import java.net.*;
|
||||
import java.nio.*;
|
||||
import java.nio.channels.*;
|
||||
import java.nio.charset.*;
|
||||
import java.util.regex.*;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class Dns{
|
||||
private static final Pattern ipPattern = Pattern.compile("^((0|1\\d?\\d?|2[0-4]?\\d?|25[0-5]?|[3-9]\\d?)\\.){3}(0|1\\d?\\d?|2[0-4]?\\d?|25[0-5]?|[3-9]\\d?)$");
|
||||
private static final int aRecord = 1, srvRecord = 33;
|
||||
private static IntMap<ObjectMap<String, Seq<?>>> cache = new IntMap<>();
|
||||
//TODO remove these
|
||||
//private static Seq<InetSocketAddress> defNameservers = Seq.with(new InetSocketAddress("1.1.1.1", 53), new InetSocketAddress("8.8.8.8", 53));
|
||||
private static IntMap<ObjectMap<String, Seq<?>>> cache = new IntMap<>(); //TODO remove this cache?
|
||||
private static ConcurrentHashMap<String, InetAddress> domainToIp = new ConcurrentHashMap<>();
|
||||
|
||||
static <T> void resolve(int type, String domain, Func<ByteBuffer, T> reader, Cons<Seq<T>> result, Cons<Exception> error){
|
||||
ObjectMap<String, Seq<?>> map;
|
||||
@@ -42,16 +40,31 @@ public class Dns{
|
||||
}, error);
|
||||
}
|
||||
|
||||
//TODO breaks for ipv6
|
||||
//TODO no SRV recrod support
|
||||
//TODO no SRV or AAAA record support
|
||||
static void resolveAddress(String domain, Cons<InetAddress> result, Cons<Exception> error){
|
||||
if(ipPattern.matcher(domain).matches()){
|
||||
|
||||
//since parsing the address may be slow, check the cache first.
|
||||
var cachedIp = domainToIp.get(domain);
|
||||
if(cachedIp != null){
|
||||
try{
|
||||
result.get(InetAddress.getByName(domain));
|
||||
result.get(cachedIp);
|
||||
}catch(Exception e){
|
||||
error.get(e);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
//attempt to resolve ipv4 or ipv6 address
|
||||
byte[] rawAddress = Addresses.getAddress(domain);
|
||||
if(rawAddress != null){
|
||||
try{
|
||||
var address = InetAddress.getByAddress(domain, rawAddress);
|
||||
domainToIp.put(domain, address);
|
||||
result.get(address);
|
||||
}catch(Exception e){
|
||||
error.get(e);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(aRecord, domain, bytes -> {
|
||||
|
||||
Reference in New Issue
Block a user