Search in sources :

Example 1 with DnsMessage

use of org.minidns.dnsmessage.DnsMessage in project minidns by MiniDNS.

the class AbstractDNSClient method query.

public final DNSMessage query(DNSMessage requestMessage, InetAddress address, int port) throws IOException {
    // See if we have the answer to this question already cached
    DNSMessage responseMessage = (cache == null) ? null : cache.get(requestMessage);
    if (responseMessage != null) {
        return responseMessage;
    }
    final Question q = requestMessage.getQuestion();
    final Level TRACE_LOG_LEVEL = Level.FINE;
    LOGGER.log(TRACE_LOG_LEVEL, "Asking {0} on {1} for {2} with:\n{3}", new Object[] { address, port, q, requestMessage });
    try {
        responseMessage = dataSource.query(requestMessage, address, port);
    } catch (IOException e) {
        LOGGER.log(TRACE_LOG_LEVEL, "IOException {0} on {1} while resolving {2}: {3}", new Object[] { address, port, q, e });
        throw e;
    }
    if (responseMessage != null) {
        LOGGER.log(TRACE_LOG_LEVEL, "Response from {0} on {1} for {2}:\n{3}", new Object[] { address, port, q, responseMessage });
    } else {
        // TODO When should this ever happen?
        LOGGER.log(Level.SEVERE, "NULL response from " + address + " on " + port + " for " + q);
    }
    if (responseMessage == null)
        return null;
    onResponseCallback.onResponse(requestMessage, responseMessage);
    return responseMessage;
}
Also used : Question(org.minidns.dnsmessage.Question) Level(java.util.logging.Level) IOException(java.io.IOException) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 2 with DnsMessage

use of org.minidns.dnsmessage.DnsMessage in project minidns by MiniDNS.

the class AbstractDNSClient method queryAsync.

public final MiniDnsFuture<DNSMessage, IOException> queryAsync(DNSMessage requestMessage, InetAddress address, int port) {
    // See if we have the answer to this question already cached
    DNSMessage responseMessage = (cache == null) ? null : cache.get(requestMessage);
    if (responseMessage != null) {
        return MiniDnsFuture.from(responseMessage);
    }
    final Question q = requestMessage.getQuestion();
    final Level TRACE_LOG_LEVEL = Level.FINE;
    LOGGER.log(TRACE_LOG_LEVEL, "Asynchronusly asking {0} on {1} for {2} with:\n{3}", new Object[] { address, port, q, requestMessage });
    return dataSource.queryAsync(requestMessage, address, port, onResponseCallback);
}
Also used : Question(org.minidns.dnsmessage.Question) Level(java.util.logging.Level) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 3 with DnsMessage

use of org.minidns.dnsmessage.DnsMessage in project minidns by MiniDNS.

the class AbstractDNSClient method getQueryFor.

protected DNSMessage getQueryFor(Question q) {
    DNSMessage.Builder messageBuilder = buildMessage(q);
    DNSMessage query = messageBuilder.build();
    return query;
}
Also used : DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 4 with DnsMessage

use of org.minidns.dnsmessage.DnsMessage in project minidns by MiniDNS.

the class AbstractDNSClient method getCachedRecordsFor.

private <D extends Data> Set<D> getCachedRecordsFor(DNSName dnsName, TYPE type) {
    Question dnsNameNs = new Question(dnsName, type);
    DNSMessage queryDnsNameNs = getQueryFor(dnsNameNs);
    DNSMessage cachedResult = cache.get(queryDnsNameNs);
    if (cachedResult == null)
        return Collections.emptySet();
    return cachedResult.getAnswersFor(dnsNameNs);
}
Also used : Question(org.minidns.dnsmessage.Question) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 5 with DnsMessage

use of org.minidns.dnsmessage.DnsMessage in project minidns by MiniDNS.

the class DNSClient method queryAsync.

@Override
protected MiniDnsFuture<DNSMessage, IOException> queryAsync(DNSMessage.Builder queryBuilder) {
    DNSMessage q = newQuestion(queryBuilder).build();
    // While this query method does in fact re-use query(Question, String)
    // we still do a cache lookup here in order to avoid unnecessary
    // findDNS()calls, which are expensive on Android. Note that we do not
    // put the results back into the Cache, as this is already done by
    // query(Question, String).
    DNSMessage responseMessage = (cache == null) ? null : cache.get(q);
    if (responseMessage != null) {
        return MiniDnsFuture.from(responseMessage);
    }
    final List<InetAddress> dnsServerAddresses = getServerAddresses();
    final InternalMiniDnsFuture<DNSMessage, IOException> future = new InternalMiniDnsFuture<>();
    final List<IOException> exceptions = Collections.synchronizedList(new ArrayList<IOException>(dnsServerAddresses.size()));
    // Filter loop.
    Iterator<InetAddress> it = dnsServerAddresses.iterator();
    while (it.hasNext()) {
        InetAddress dns = it.next();
        if (nonRaServers.contains(dns)) {
            it.remove();
            LOGGER.finer("Skipping " + dns + " because it was marked as \"recursion not available\"");
            continue;
        }
    }
    List<MiniDnsFuture<DNSMessage, IOException>> futures = new ArrayList<>(dnsServerAddresses.size());
    // "Main" loop.
    for (InetAddress dns : dnsServerAddresses) {
        if (future.isDone()) {
            for (MiniDnsFuture<DNSMessage, IOException> futureToCancel : futures) {
                futureToCancel.cancel(true);
            }
            break;
        }
        MiniDnsFuture<DNSMessage, IOException> f = queryAsync(q, dns);
        f.onSuccess(new SuccessCallback<DNSMessage>() {

            @Override
            public void onSuccess(DNSMessage result) {
                future.setResult(result);
            }
        });
        f.onError(new ExceptionCallback<IOException>() {

            @Override
            public void processException(IOException exception) {
                exceptions.add(exception);
                if (exceptions.size() == dnsServerAddresses.size()) {
                    future.setException(MultipleIoException.toIOException(exceptions));
                }
            }
        });
        futures.add(f);
    }
    return future;
}
Also used : InternalMiniDnsFuture(org.minidns.MiniDnsFuture.InternalMiniDnsFuture) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) InternalMiniDnsFuture(org.minidns.MiniDnsFuture.InternalMiniDnsFuture) IOException(java.io.IOException) InetAddress(java.net.InetAddress) DNSMessage(org.minidns.dnsmessage.DNSMessage)

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