Search in sources :

Example 6 with DNSMessage

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;
}
Also used : InternalMiniDnsFuture(org.minidns.MiniDnsFuture.InternalMiniDnsFuture) IOException(java.io.IOException) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 7 with DNSMessage

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;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 8 with 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();
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DataOutputStream(java.io.DataOutputStream) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) DataInputStream(java.io.DataInputStream) Socket(java.net.Socket) DatagramSocket(java.net.DatagramSocket) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 9 with DNSMessage

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;
}
Also used : IOException(java.io.IOException) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 10 with DNSMessage

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);
}
Also used : LRUCache(org.minidns.cache.LRUCache) DNSMessage(org.minidns.dnsmessage.DNSMessage) Test(org.junit.Test)

Aggregations

DNSMessage (org.minidns.dnsmessage.DNSMessage)67 Test (org.junit.Test)35 Data (org.minidns.record.Data)16 Record (org.minidns.record.Record)16 IOException (java.io.IOException)15 Question (org.minidns.dnsmessage.Question)14 InetAddress (java.net.InetAddress)6 LRUCache (org.minidns.cache.LRUCache)6 ArrayList (java.util.ArrayList)5 DNSClient (org.minidns.DNSClient)5 LinkedList (java.util.LinkedList)4 EDNS (org.minidns.edns.EDNS)4 RRSIG (org.minidns.record.RRSIG)4 Date (java.util.Date)3 Level (java.util.logging.Level)3 InternalMiniDnsFuture (org.minidns.MiniDnsFuture.InternalMiniDnsFuture)3 DNSName (org.minidns.dnsname.DNSName)3 DNSKEY (org.minidns.record.DNSKEY)3 DatagramSocket (java.net.DatagramSocket)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2