Replaced getByName/getAllByName

This commit is contained in:
M66B
2024-01-01 22:30:34 +01:00
parent b5f19f9e2d
commit 94c6fca5dc
6 changed files with 46 additions and 14 deletions

View File

@@ -49,6 +49,8 @@ import org.minidns.record.TXT;
import org.minidns.source.AbstractDnsDataSource;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
@@ -275,6 +277,37 @@ public class DnsHelper {
}
}
static InetAddress getByName(Context context, String host) throws UnknownHostException {
return getAllByName(context, host)[0];
}
static InetAddress[] getAllByName(Context context, String host) throws UnknownHostException {
List<InetAddress> result = new ArrayList<>();
boolean[] has46 = ConnectionHelper.has46(context);
if (has46[0])
for (DnsRecord a : lookup(context, host, "a"))
try {
result.add(Inet4Address.getByName(a.response));
} catch (UnknownHostException ex) {
Log.e(ex);
}
if (has46[1])
for (DnsRecord aaaa : lookup(context, host, "aaaa"))
try {
result.add(Inet6Address.getByName(aaaa.response));
} catch (UnknownHostException ex) {
Log.e(ex);
}
if (result.size() == 0)
throw new UnknownHostException(host);
else
return result.toArray(new InetAddress[0]);
}
private static List<String> getDnsServers(Context context) {
List<String> result = new ArrayList<>();
result.add(DEFAULT_DNS);