Search in sources :

Example 61 with DnsMessage

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

the class NetworkDataSourceWithAccounting method queryTcp.

@Override
protected DNSMessage queryTcp(DNSMessage message, InetAddress address, int port) throws IOException {
    DNSMessage response;
    try {
        response = super.queryTcp(message, address, port);
    } catch (IOException e) {
        failedTcpQueries.incrementAndGet();
        throw e;
    }
    successfulTcpQueries.incrementAndGet();
    tcpResponseSize.addAndGet(response.toArray().length);
    return response;
}
Also used : IOException(java.io.IOException) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 62 with DnsMessage

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

the class DNSWorld method applyZones.

public static DNSWorld applyZones(AbstractDNSClient client, Zone... zones) {
    DNSWorld world = new DNSWorld();
    client.setDataSource(world);
    for (Zone zone : zones) {
        for (RRSet rrSet : zone.getRRSets()) {
            DNSMessage.Builder req = client.buildMessage(new Question(rrSet.name, rrSet.type, rrSet.clazz, false));
            DNSMessage.Builder resp = DNSMessage.builder();
            resp.setAnswers(rrSet.records);
            resp.setAuthoritativeAnswer(true);
            attachGlues(resp, rrSet.records, zone.records);
            attachSignatures(resp, zone.records);
            DNSMessage request = req.build();
            DNSMessage response = resp.build();
            if (zone.isRootZone()) {
                world.addPreparedResponse(new RootAnswerResponse(request, response));
            } else {
                world.addPreparedResponse(new AddressedAnswerResponse(zone.address, request, response));
            }
            if (rrSet.type == TYPE.NS) {
                DNSMessage.Builder hintsResp = DNSMessage.builder();
                hintsResp.setNameserverRecords(rrSet.records);
                hintsResp.setAdditionalResourceRecords(response.additionalSection);
                DNSMessage hintsResponse = hintsResp.build();
                if (zone.isRootZone()) {
                    world.addPreparedResponse(new RootHintsResponse(rrSet.name, hintsResponse));
                } else {
                    world.addPreparedResponse(new AddressedHintsResponse(zone.address, rrSet.name, hintsResponse));
                }
            }
        }
    }
    return world;
}
Also used : Question(org.minidns.dnsmessage.Question) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 63 with DnsMessage

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

the class LRUCacheTest method testOutdatedCacheEntry.

@Test
public void testOutdatedCacheEntry() {
    DNSMessage message = createSampleMessage(1);
    Question q = new Question("", Record.TYPE.A);
    DNSMessage question = q.asQueryMessage();
    lruCache.put(question, message);
    assertNull(lruCache.get(question));
    assertNull(lruCache.get(question));
    assertEquals(1, lruCache.getExpireCount());
    assertEquals(2, lruCache.getMissCount());
}
Also used : Question(org.minidns.dnsmessage.Question) DNSMessage(org.minidns.dnsmessage.DNSMessage) Test(org.junit.Test)

Example 64 with DnsMessage

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

the class DNSClient method query.

@Override
public DNSMessage query(DNSMessage.Builder queryBuilder) throws IOException {
    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 responseMessage;
    }
    List<InetAddress> dnsServerAddresses = getServerAddresses();
    List<IOException> ioExceptions = new ArrayList<>(dnsServerAddresses.size());
    for (InetAddress dns : dnsServerAddresses) {
        if (nonRaServers.contains(dns)) {
            LOGGER.finer("Skipping " + dns + " because it was marked as \"recursion not available\"");
            continue;
        }
        try {
            responseMessage = query(q, dns);
            if (responseMessage == null) {
                continue;
            }
            if (!responseMessage.recursionAvailable) {
                boolean newRaServer = nonRaServers.add(dns);
                if (newRaServer) {
                    LOGGER.warning("The DNS server " + dns + " returned a response without the \"recursion available\" (RA) flag set. This likely indicates a misconfiguration because the server is not suitable for DNS resolution");
                }
                continue;
            }
            if (disableResultFilter) {
                return responseMessage;
            }
            switch(responseMessage.responseCode) {
                case NO_ERROR:
                case NX_DOMAIN:
                    break;
                default:
                    String warning = "Response from " + dns + " asked for " + q.getQuestion() + " with error code: " + responseMessage.responseCode + '.';
                    if (!LOGGER.isLoggable(Level.FINE)) {
                        // Only append the responseMessage is log level is not fine. If it is fine or higher, the
                        // response has already been logged.
                        warning += "\n" + responseMessage;
                    }
                    LOGGER.warning(warning);
                    // TODO Create new IOException and add to ioExceptions.
                    continue;
            }
            return responseMessage;
        } catch (IOException ioe) {
            ioExceptions.add(ioe);
        }
    }
    MultipleIoException.throwIfRequired(ioExceptions);
    // TODO assert that we never return null here.
    return null;
}
Also used : ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) IOException(java.io.IOException) InetAddress(java.net.InetAddress) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 65 with DnsMessage

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

the class ExtendedLRUCache method putExtraCaches.

private final void putExtraCaches(DNSMessage reply, Map<DNSMessage, List<Record<? extends Data>>> extraCaches) {
    for (Entry<DNSMessage, List<Record<? extends Data>>> entry : extraCaches.entrySet()) {
        DNSMessage question = entry.getKey();
        DNSMessage answer = reply.asBuilder().setQuestion(question.getQuestion()).setAuthoritativeAnswer(true).addAnswers(entry.getValue()).build();
        super.putNormalized(question, answer);
    }
}
Also used : List(java.util.List) LinkedList(java.util.LinkedList) 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