Search in sources :

Example 6 with Message

use of org.xbill.DNS.Message in project GNS by MobilityFirst.

the class LookupWorker method run.

/**
   * @see java.lang.Thread#run()
   */
@Override
public void run() {
    long startTime = System.currentTimeMillis();
    Message query;
    Message response;
    int maxLength;
    // create a Message from the query data;
    try {
        query = new Message(incomingData);
    } catch (IOException e) {
        // Send out an error response.
        sendResponse(NameResolution.formErrorMessage(incomingData).toWire());
        return;
    }
    // THE MEAT IS IN HERE. Try to get a response from the GNS or DNS servers.
    response = generateReply(query);
    long postStart = System.currentTimeMillis();
    if (response == null) {
        // means we don't need to do anything
        return;
    }
    if (query.getOPT() != null) {
        maxLength = Math.max(query.getOPT().getPayloadSize(), 512);
    } else {
        maxLength = 512;
    }
    NameResolution.getLogger().log(Level.FINE, "Q/R: {0}", NameResolution.queryAndResponseToString(query, response));
    // Send out the response.
    DelayProfiler.updateDelay("LookupWorker.postGenerate", postStart);
    long sendStart = System.currentTimeMillis();
    sendResponse(response.toWire(maxLength));
    DelayProfiler.updateDelay("LookupWorker.sendResponse", sendStart);
    DelayProfiler.updateDelay("LookupWorker", startTime);
}
Also used : Message(org.xbill.DNS.Message) IOException(java.io.IOException)

Example 7 with Message

use of org.xbill.DNS.Message in project GNS by MobilityFirst.

the class NameResolution method forwardToGnsServer.

/**
   * Sends the query to the GNS server.
   *
   * @param gnsServer
   * @param query
   * @return A message with either a good response or an error.
   */
public static Message forwardToGnsServer(SimpleResolver gnsServer, Message query) {
    try {
        Message dnsResponse = gnsServer.send(query);
        NameResolution.getLogger().log(Level.FINE, "DNS response {0} with {1} answer, {2} authoritative and {3} additional records", new Object[] { Rcode.string(dnsResponse.getHeader().getRcode()), dnsResponse.getSectionArray(Section.ANSWER).length, dnsResponse.getSectionArray(Section.AUTHORITY).length, dnsResponse.getSectionArray(Section.ADDITIONAL).length });
        if (isReasonableResponse(dnsResponse)) {
            NameResolution.getLogger().log(Level.FINE, "Outgoing response from DNS: {0}", dnsResponse.toString());
            return dnsResponse;
        }
    } catch (IOException e) {
        NameResolution.getLogger().log(Level.WARNING, "DNS resolution failed for {0}: {1}", new Object[] { query, e });
    }
    return errorMessage(query, Rcode.NXDOMAIN);
}
Also used : Message(org.xbill.DNS.Message) JSONObject(org.json.JSONObject) IOException(java.io.IOException)

Example 8 with Message

use of org.xbill.DNS.Message in project opennms by OpenNMS.

the class DnsMonitor method pollDNS.

private PollStatus pollDNS(final TimeoutTracker timeoutTracker, final int port, final InetAddress address, final String lookup, final List<Integer> fatalCodes, int minAnswers, int maxAnswers) {
    final String addr = InetAddressUtils.str(address);
    for (timeoutTracker.reset(); timeoutTracker.shouldRetry(); timeoutTracker.nextAttempt()) {
        try {
            final Name name = Name.fromString(lookup, Name.root);
            final SimpleResolver resolver = new SimpleResolver();
            resolver.setAddress(new InetSocketAddress(addr, port));
            resolver.setLocalAddress((InetSocketAddress) null);
            double timeout = timeoutTracker.getSoTimeout() / 1000;
            resolver.setTimeout((timeout < 1 ? 1 : (int) timeout));
            final Record question = Record.newRecord(name, Type.A, DClass.IN);
            final Message query = Message.newQuery(question);
            PollStatus status;
            timeoutTracker.startAttempt();
            final Message response = resolver.send(query);
            double responseTime = timeoutTracker.elapsedTimeInMillis();
            final Integer rcode = response.getHeader().getRcode();
            LOG.debug("received response code: {}", rcode);
            if (fatalCodes.contains(rcode)) {
                status = PollStatus.unavailable("Received an invalid DNS response for address: " + addr);
                LOG.debug(status.getReason());
                return status;
            } else if (minAnswers != DEFAULT_MIN_ANSWERS || maxAnswers != DEFAULT_MAX_ANSWERS) {
                int numAnswers = response.getSectionArray(Section.ANSWER).length;
                boolean tooFewAnswers = numAnswers < minAnswers;
                boolean tooManyAnswers = numAnswers > maxAnswers;
                if (tooFewAnswers) {
                    status = PollStatus.unavailable("Response contained only " + numAnswers + " answer(s), but at least " + minAnswers + " answers(s) are needed.");
                    LOG.warn(status.getReason());
                    return status;
                }
                if (tooManyAnswers) {
                    status = PollStatus.unavailable("Response contained " + numAnswers + " answer(s), but " + maxAnswers + " or fewer answers(s) are needed.");
                    LOG.warn(status.getReason());
                    return status;
                }
                status = PollStatus.up(responseTime);
                LOG.debug("valid DNS response received with {} answer(s), responseTime = {}ms", numAnswers, responseTime);
                return status;
            } else {
                status = PollStatus.up(responseTime);
                LOG.debug("valid DNS response received, responseTime = {}ms", responseTime);
                return status;
            }
        } catch (final InterruptedIOException e) {
        // No response received, retry without marking the poll failed. If we get this condition over and over until 
        // the retries are exhausted, it will leave serviceStatus null and we'll get the log message at the bottom 
        } catch (final NoRouteToHostException e) {
            String reason1 = "No route to host exception for address: " + addr;
            LOG.debug(reason1, e);
            return PollStatus.unavailable(reason1);
        } catch (final ConnectException e) {
            String reason1 = "Connection exception for address: " + addr;
            LOG.debug(reason1, e);
            return PollStatus.unavailable(reason1);
        } catch (final IOException e) {
            String reason1 = "IOException while polling address: " + addr + " " + e.getMessage();
            LOG.debug(reason1, e);
            return PollStatus.unavailable(reason1);
        }
    }
    String reason = "Never received valid DNS response for address: " + addr;
    LOG.debug(reason);
    return PollStatus.unavailable(reason);
}
Also used : InterruptedIOException(java.io.InterruptedIOException) PollStatus(org.opennms.netmgt.poller.PollStatus) Message(org.xbill.DNS.Message) InetSocketAddress(java.net.InetSocketAddress) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) SimpleResolver(org.xbill.DNS.SimpleResolver) NoRouteToHostException(java.net.NoRouteToHostException) Name(org.xbill.DNS.Name) Record(org.xbill.DNS.Record) ConnectException(java.net.ConnectException)

Example 9 with Message

use of org.xbill.DNS.Message in project camel by apache.

the class DnsWikipediaProducer method process.

@Override
public void process(Exchange exchange) throws Exception {
    SimpleResolver resolver = new SimpleResolver();
    int type = Type.TXT;
    Name name = Name.fromString(String.valueOf(exchange.getIn().getHeader(DnsConstants.TERM)) + ".wp.dg.cx", Name.root);
    Record rec = Record.newRecord(name, type, DClass.IN);
    Message query = Message.newQuery(rec);
    Message response = resolver.send(query);
    Record[] records = response.getSectionArray(Section.ANSWER);
    if (records.length > 0) {
        exchange.getIn().setBody(records[0].rdataToString());
    } else {
        exchange.getIn().setBody(null);
    }
}
Also used : Message(org.xbill.DNS.Message) Record(org.xbill.DNS.Record) SimpleResolver(org.xbill.DNS.SimpleResolver) Endpoint(org.apache.camel.Endpoint) Name(org.xbill.DNS.Name)

Example 10 with Message

use of org.xbill.DNS.Message in project camel by apache.

the class DnsRecordConverter method toRecord.

/**
     * @param ip, like "192.168.1.1"
     * @return the complete DNS record for that IP.
     */
@Converter
public static Record toRecord(String ip) throws IOException {
    Resolver res = new ExtendedResolver();
    Name name = ReverseMap.fromAddress(ip);
    int type = Type.PTR;
    int dclass = DClass.IN;
    Record rec = Record.newRecord(name, type, dclass);
    Message query = Message.newQuery(rec);
    Message response = res.send(query);
    Record[] answers = response.getSectionArray(Section.ANSWER);
    if (answers.length == 0) {
        return null;
    } else {
        return answers[0];
    }
}
Also used : ExtendedResolver(org.xbill.DNS.ExtendedResolver) ExtendedResolver(org.xbill.DNS.ExtendedResolver) Resolver(org.xbill.DNS.Resolver) Message(org.xbill.DNS.Message) Record(org.xbill.DNS.Record) Name(org.xbill.DNS.Name) Converter(org.apache.camel.Converter)

Aggregations

Message (org.xbill.DNS.Message)20 IOException (java.io.IOException)8 Name (org.xbill.DNS.Name)8 Record (org.xbill.DNS.Record)8 Header (org.xbill.DNS.Header)5 ArrayList (java.util.ArrayList)4 RRset (org.xbill.DNS.RRset)4 JSONObject (org.json.JSONObject)3 CERTRecord (org.xbill.DNS.CERTRecord)3 CNAMERecord (org.xbill.DNS.CNAMERecord)3 SimpleResolver (org.xbill.DNS.SimpleResolver)3 InterruptedIOException (java.io.InterruptedIOException)2 Endpoint (org.apache.camel.Endpoint)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 ARecord (org.xbill.DNS.ARecord)2 ExtendedResolver (org.xbill.DNS.ExtendedResolver)2 MXRecord (org.xbill.DNS.MXRecord)2 NSRecord (org.xbill.DNS.NSRecord)2 SetResponse (org.xbill.DNS.SetResponse)2