AAAA record support

This commit is contained in:
Anuken
2024-01-27 12:50:26 -05:00
parent c7a35ae789
commit db4c861dde

View File

@@ -9,11 +9,10 @@ import arc.util.*;
import java.net.*; import java.net.*;
import java.nio.*; import java.nio.*;
import java.nio.channels.*; import java.nio.channels.*;
import java.nio.charset.*;
import java.util.concurrent.*; import java.util.concurrent.*;
public class Dns{ public class Dns{
private static final int aRecord = 1, srvRecord = 33; private static final int aRecord = 1, aaaaRecord = 28, srvRecord = 33;
private static IntMap<ObjectMap<String, Seq<?>>> cache = new IntMap<>(); //TODO remove this cache? private static IntMap<ObjectMap<String, Seq<?>>> cache = new IntMap<>(); //TODO remove this cache?
private static ConcurrentHashMap<String, InetAddress> domainToIp = new ConcurrentHashMap<>(); private static ConcurrentHashMap<String, InetAddress> domainToIp = new ConcurrentHashMap<>();
@@ -40,7 +39,31 @@ public class Dns{
}, error); }, error);
} }
//TODO no SRV or AAAA record support /*
TODO: SRV record support
resolve(srvRecord, domain, bytes -> {
int priority = bytes.getShort() & 0xFFFF;
int weight = bytes.getShort() & 0xFFFF;
int port = bytes.getShort() & 0xFFFF;
int len;
StringBuilder builder = new StringBuilder();
while((len = bytes.get()) != 0){
for(int j = 0; j < len; j++) builder.append((char)bytes.get());
builder.append('.');
}
builder.delete(builder.length() - 1, builder.length());
return "SRV Record: " + builder + " " + priority + " " + weight + " port=" +port;
}, records -> {
if(records.size > 0){
Log.info("@ has SRV records: @", domain, records);
}
}, e -> {});
*/
//TODO no SRV record support
static void resolveAddress(String domain, Cons<InetAddress> result, Cons<Exception> error){ static void resolveAddress(String domain, Cons<InetAddress> result, Cons<Exception> error){
//since parsing the address may be slow, check the cache first. //since parsing the address may be slow, check the cache first.
@@ -75,6 +98,17 @@ public class Dns{
try{ try{
if(addresses.size > 0){ if(addresses.size > 0){
result.get(InetAddress.getByAddress(addresses.get(0))); result.get(InetAddress.getByAddress(addresses.get(0)));
}else{
//there are no records found - try AAAA instead.
resolve(aaaaRecord, domain, bytes -> {
byte[] address = new byte[16];
bytes.get(address);
return address;
}, addresses2 -> {
try{
if(addresses2.size > 0){
result.get(InetAddress.getByAddress(addresses2.get(0)));
}else{ }else{
//there are no records found //there are no records found
error.get(new UnresolvedAddressException()); error.get(new UnresolvedAddressException());
@@ -84,6 +118,11 @@ public class Dns{
} }
}, error); }, error);
} }
}catch(UnknownHostException unknown){
error.get(unknown);
}
}, error);
}
static <T> void send(Seq<InetSocketAddress> addresses, int index, int type, String domain, Func<ByteBuffer, T> reader, Cons<Seq<T>> recordResult, Cons<Exception> error){ static <T> void send(Seq<InetSocketAddress> addresses, int index, int type, String domain, Func<ByteBuffer, T> reader, Cons<Seq<T>> recordResult, Cons<Exception> error){
short id = (short)new Rand().nextInt(Short.MAX_VALUE); short id = (short)new Rand().nextInt(Short.MAX_VALUE);
@@ -100,7 +139,7 @@ public class Dns{
// Domain // Domain
for(String part : domain.split("\\.")){ for(String part : domain.split("\\.")){
buffer.put((byte) part.length()); buffer.put((byte) part.length());
buffer.put(part.getBytes(StandardCharsets.UTF_8)); buffer.put(part.getBytes(Strings.utf8));
} }
buffer.put((byte) 0); buffer.put((byte) 0);