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);
}
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);
}
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);
}
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);
}
}
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];
}
}
Aggregations