use of org.minidns.dnsmessage.DNSMessage in project minidns by MiniDNS.
the class DNSDataSource method queryAsync.
public MiniDnsFuture<DNSMessage, IOException> queryAsync(DNSMessage message, InetAddress address, int port, OnResponseCallback onResponseCallback) {
InternalMiniDnsFuture<DNSMessage, IOException> future = new InternalMiniDnsFuture<>();
DNSMessage result;
try {
result = query(message, address, port);
} catch (IOException e) {
future.setException(e);
return future;
}
future.setResult(result);
return future;
}
use of org.minidns.dnsmessage.DNSMessage in project minidns by MiniDNS.
the class NetworkDataSource method query.
@Override
public DNSMessage query(DNSMessage message, InetAddress address, int port) throws IOException {
List<IOException> ioExceptions = new ArrayList<>(2);
DNSMessage dnsMessage = null;
final QueryMode queryMode = getQueryMode();
boolean doUdpFirst;
switch(queryMode) {
case dontCare:
case udpTcp:
doUdpFirst = true;
break;
case tcp:
doUdpFirst = false;
break;
default:
throw new IllegalStateException("Unsupported query mode: " + queryMode);
}
if (doUdpFirst) {
try {
dnsMessage = queryUdp(message, address, port);
} catch (IOException e) {
ioExceptions.add(e);
}
if (dnsMessage != null && !dnsMessage.truncated) {
return dnsMessage;
}
assert (dnsMessage == null || dnsMessage.truncated || ioExceptions.size() == 1);
LOGGER.log(Level.FINE, "Fallback to TCP because {0}", new Object[] { dnsMessage != null ? "response is truncated" : ioExceptions.get(0) });
}
try {
dnsMessage = queryTcp(message, address, port);
} catch (IOException e) {
ioExceptions.add(e);
MultipleIoException.throwIfRequired(ioExceptions);
}
return dnsMessage;
}
use of org.minidns.dnsmessage.DNSMessage in project minidns by MiniDNS.
the class NetworkDataSource method queryTcp.
protected DNSMessage queryTcp(DNSMessage message, InetAddress address, int port) throws IOException {
// TODO Use a try-with-resource statement here once miniDNS minimum
// required Android API level is >= 19
Socket socket = null;
try {
socket = createSocket();
SocketAddress socketAddress = new InetSocketAddress(address, port);
socket.connect(socketAddress, timeout);
socket.setSoTimeout(timeout);
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
message.writeTo(dos);
dos.flush();
DataInputStream dis = new DataInputStream(socket.getInputStream());
int length = dis.readUnsignedShort();
byte[] data = new byte[length];
int read = 0;
while (read < length) {
read += dis.read(data, read, length - read);
}
DNSMessage dnsMessage = new DNSMessage(data);
if (dnsMessage.id != message.id) {
throw new MiniDNSException.IdMismatch(message, dnsMessage);
}
return dnsMessage;
} finally {
if (socket != null) {
socket.close();
}
}
}
use of org.minidns.dnsmessage.DNSMessage in project minidns by MiniDNS.
the class NetworkDataSourceWithAccounting method query.
@Override
public DNSMessage query(DNSMessage message, InetAddress address, int port) throws IOException {
DNSMessage response;
try {
response = super.query(message, address, port);
} catch (IOException e) {
failedQueries.incrementAndGet();
throw e;
}
successfulQueries.incrementAndGet();
responseSize.addAndGet(response.toArray().length);
return response;
}
use of org.minidns.dnsmessage.DNSMessage in project minidns by MiniDNS.
the class DNSClientTest method testSingleRecordQuery.
@SuppressWarnings("unchecked")
@Test
public void testSingleRecordQuery() throws IOException {
DNSClient client = new DNSClient(new LRUCache(0));
applyStubRecords(client, record("www.example.com", a("127.0.0.1")));
DNSMessage response = client.query("www.example.com", TYPE.A);
assertNotNull(response);
assertEquals(1, response.answerSection.size());
assertEquals(TYPE.A, response.answerSection.get(0).type);
assertArrayEquals(new byte[] { 127, 0, 0, 1 }, ((A) response.answerSection.get(0).payloadData).getIp());
response = client.query("www2.example.com", TYPE.A);
assertNull(response);
response = client.query("www.example.com", TYPE.CNAME);
assertNull(response);
}
Aggregations