2014-09-19 15:36:32 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2014 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
package io.netty.resolver.dns;
|
|
|
|
|
2015-03-16 07:46:14 +01:00
|
|
|
import io.netty.channel.AddressedEnvelope;
|
2016-06-28 11:44:04 +02:00
|
|
|
import io.netty.channel.Channel;
|
2014-09-19 15:36:32 +02:00
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
|
import io.netty.channel.ChannelFutureListener;
|
2017-04-07 03:09:28 +02:00
|
|
|
import io.netty.channel.ChannelPromise;
|
2015-03-16 07:46:14 +01:00
|
|
|
import io.netty.handler.codec.dns.DatagramDnsQuery;
|
2016-07-29 10:30:08 +02:00
|
|
|
import io.netty.handler.codec.dns.AbstractDnsOptPseudoRrRecord;
|
2014-09-19 15:36:32 +02:00
|
|
|
import io.netty.handler.codec.dns.DnsQuery;
|
|
|
|
import io.netty.handler.codec.dns.DnsQuestion;
|
2015-03-16 07:46:14 +01:00
|
|
|
import io.netty.handler.codec.dns.DnsRecord;
|
2014-09-19 15:36:32 +02:00
|
|
|
import io.netty.handler.codec.dns.DnsResponse;
|
2015-07-12 12:34:05 +02:00
|
|
|
import io.netty.handler.codec.dns.DnsSection;
|
2016-06-28 11:44:04 +02:00
|
|
|
import io.netty.util.concurrent.Future;
|
|
|
|
import io.netty.util.concurrent.GenericFutureListener;
|
2014-09-19 15:36:32 +02:00
|
|
|
import io.netty.util.concurrent.Promise;
|
|
|
|
import io.netty.util.concurrent.ScheduledFuture;
|
|
|
|
import io.netty.util.internal.logging.InternalLogger;
|
|
|
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
|
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2016-02-13 14:06:06 +01:00
|
|
|
import static io.netty.util.internal.ObjectUtil.checkNotNull;
|
|
|
|
|
2014-09-19 15:36:32 +02:00
|
|
|
final class DnsQueryContext {
|
|
|
|
|
|
|
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(DnsQueryContext.class);
|
|
|
|
|
|
|
|
private final DnsNameResolver parent;
|
2015-03-16 07:46:14 +01:00
|
|
|
private final Promise<AddressedEnvelope<DnsResponse, InetSocketAddress>> promise;
|
2014-09-19 15:36:32 +02:00
|
|
|
private final int id;
|
|
|
|
private final DnsQuestion question;
|
2016-07-29 10:30:08 +02:00
|
|
|
private final DnsRecord[] additionals;
|
2015-03-16 07:46:14 +01:00
|
|
|
private final DnsRecord optResource;
|
2015-07-12 12:34:05 +02:00
|
|
|
private final InetSocketAddress nameServerAddr;
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
private final boolean recursionDesired;
|
|
|
|
private volatile ScheduledFuture<?> timeoutFuture;
|
|
|
|
|
|
|
|
DnsQueryContext(DnsNameResolver parent,
|
2015-07-12 12:34:05 +02:00
|
|
|
InetSocketAddress nameServerAddr,
|
2016-02-13 14:06:06 +01:00
|
|
|
DnsQuestion question,
|
2016-07-29 10:30:08 +02:00
|
|
|
DnsRecord[] additionals,
|
2016-02-13 14:06:06 +01:00
|
|
|
Promise<AddressedEnvelope<DnsResponse, InetSocketAddress>> promise) {
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2016-02-13 14:06:06 +01:00
|
|
|
this.parent = checkNotNull(parent, "parent");
|
|
|
|
this.nameServerAddr = checkNotNull(nameServerAddr, "nameServerAddr");
|
|
|
|
this.question = checkNotNull(question, "question");
|
2016-07-29 10:30:08 +02:00
|
|
|
this.additionals = checkNotNull(additionals, "additionals");
|
2016-02-13 14:06:06 +01:00
|
|
|
this.promise = checkNotNull(promise, "promise");
|
2014-09-19 15:36:32 +02:00
|
|
|
recursionDesired = parent.isRecursionDesired();
|
2015-11-08 04:59:01 +01:00
|
|
|
id = parent.queryContextManager.add(this);
|
|
|
|
|
2015-10-02 15:16:16 +02:00
|
|
|
if (parent.isOptResourceEnabled()) {
|
2016-07-29 10:30:08 +02:00
|
|
|
optResource = new AbstractDnsOptPseudoRrRecord(parent.maxPayloadSize(), 0, 0) {
|
|
|
|
// We may want to remove this in the future and let the user just specify the opt record in the query.
|
|
|
|
};
|
2015-10-02 15:16:16 +02:00
|
|
|
} else {
|
|
|
|
optResource = null;
|
|
|
|
}
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
2015-11-08 04:59:01 +01:00
|
|
|
InetSocketAddress nameServerAddr() {
|
|
|
|
return nameServerAddr;
|
|
|
|
}
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2015-11-08 04:59:01 +01:00
|
|
|
DnsQuestion question() {
|
|
|
|
return question;
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
2017-04-07 03:09:28 +02:00
|
|
|
void query(ChannelPromise writePromise) {
|
2015-11-08 04:59:01 +01:00
|
|
|
final DnsQuestion question = question();
|
|
|
|
final InetSocketAddress nameServerAddr = nameServerAddr();
|
2015-03-16 07:46:14 +01:00
|
|
|
final DatagramDnsQuery query = new DatagramDnsQuery(null, nameServerAddr, id);
|
2016-02-13 14:06:06 +01:00
|
|
|
|
2015-03-16 07:46:14 +01:00
|
|
|
query.setRecursionDesired(recursionDesired);
|
2016-02-13 14:06:06 +01:00
|
|
|
|
|
|
|
query.addRecord(DnsSection.QUESTION, question);
|
|
|
|
|
2016-07-29 10:30:08 +02:00
|
|
|
for (DnsRecord record: additionals) {
|
2016-02-13 14:06:06 +01:00
|
|
|
query.addRecord(DnsSection.ADDITIONAL, record);
|
|
|
|
}
|
2016-07-29 10:30:08 +02:00
|
|
|
|
2015-10-02 15:16:16 +02:00
|
|
|
if (optResource != null) {
|
2016-02-13 14:06:06 +01:00
|
|
|
query.addRecord(DnsSection.ADDITIONAL, optResource);
|
2015-10-02 15:16:16 +02:00
|
|
|
}
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
if (logger.isDebugEnabled()) {
|
2014-11-18 21:28:45 +01:00
|
|
|
logger.debug("{} WRITE: [{}: {}], {}", parent.ch, id, nameServerAddr, question);
|
|
|
|
}
|
|
|
|
|
2017-04-07 03:09:28 +02:00
|
|
|
sendQuery(query, writePromise);
|
2014-11-18 21:28:45 +01:00
|
|
|
}
|
|
|
|
|
2017-04-07 03:09:28 +02:00
|
|
|
private void sendQuery(final DnsQuery query, final ChannelPromise writePromise) {
|
2016-06-28 11:44:04 +02:00
|
|
|
if (parent.channelFuture.isDone()) {
|
2017-04-07 03:09:28 +02:00
|
|
|
writeQuery(query, writePromise);
|
2014-11-18 21:28:45 +01:00
|
|
|
} else {
|
2016-06-28 11:44:04 +02:00
|
|
|
parent.channelFuture.addListener(new GenericFutureListener<Future<? super Channel>>() {
|
2014-11-18 21:28:45 +01:00
|
|
|
@Override
|
2016-06-28 11:44:04 +02:00
|
|
|
public void operationComplete(Future<? super Channel> future) throws Exception {
|
2014-11-18 21:28:45 +01:00
|
|
|
if (future.isSuccess()) {
|
2017-04-07 03:09:28 +02:00
|
|
|
writeQuery(query, writePromise);
|
2014-11-18 21:28:45 +01:00
|
|
|
} else {
|
2017-04-07 03:09:28 +02:00
|
|
|
Throwable cause = future.cause();
|
|
|
|
promise.tryFailure(cause);
|
|
|
|
writePromise.setFailure(cause);
|
2014-11-18 21:28:45 +01:00
|
|
|
}
|
2015-07-12 12:34:05 +02:00
|
|
|
}
|
2014-11-18 21:28:45 +01:00
|
|
|
});
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
2014-11-18 21:28:45 +01:00
|
|
|
}
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2017-04-07 03:09:28 +02:00
|
|
|
private void writeQuery(final DnsQuery query, final ChannelPromise writePromise) {
|
|
|
|
final ChannelFuture writeFuture = parent.ch.writeAndFlush(query, writePromise);
|
2014-09-19 15:36:32 +02:00
|
|
|
if (writeFuture.isDone()) {
|
2015-07-12 12:34:05 +02:00
|
|
|
onQueryWriteCompletion(writeFuture);
|
2014-09-19 15:36:32 +02:00
|
|
|
} else {
|
|
|
|
writeFuture.addListener(new ChannelFutureListener() {
|
|
|
|
@Override
|
|
|
|
public void operationComplete(ChannelFuture future) throws Exception {
|
2015-07-12 12:34:05 +02:00
|
|
|
onQueryWriteCompletion(writeFuture);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
private void onQueryWriteCompletion(ChannelFuture writeFuture) {
|
2014-09-19 15:36:32 +02:00
|
|
|
if (!writeFuture.isSuccess()) {
|
2015-07-12 12:34:05 +02:00
|
|
|
setFailure("failed to send a query", writeFuture.cause());
|
2014-09-19 15:36:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Schedule a query timeout task if necessary.
|
|
|
|
final long queryTimeoutMillis = parent.queryTimeoutMillis();
|
|
|
|
if (queryTimeoutMillis > 0) {
|
2016-03-27 14:25:39 +02:00
|
|
|
timeoutFuture = parent.ch.eventLoop().schedule(new Runnable() {
|
2014-09-19 15:36:32 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (promise.isDone()) {
|
|
|
|
// Received a response before the query times out.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
setFailure("query timed out after " + queryTimeoutMillis + " milliseconds", null);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
}, queryTimeoutMillis, TimeUnit.MILLISECONDS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
void finish(AddressedEnvelope<? extends DnsResponse, InetSocketAddress> envelope) {
|
2015-11-08 04:59:01 +01:00
|
|
|
final DnsResponse res = envelope.content();
|
2015-07-12 12:34:05 +02:00
|
|
|
if (res.count(DnsSection.QUESTION) != 1) {
|
|
|
|
logger.warn("Received a DNS response with invalid number of questions: {}", envelope);
|
2014-09-19 15:36:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-08 04:59:01 +01:00
|
|
|
if (!question().equals(res.recordAt(DnsSection.QUESTION))) {
|
2015-07-12 12:34:05 +02:00
|
|
|
logger.warn("Received a mismatching DNS response: {}", envelope);
|
|
|
|
return;
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
setSuccess(envelope);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
private void setSuccess(AddressedEnvelope<? extends DnsResponse, InetSocketAddress> envelope) {
|
2015-11-08 04:59:01 +01:00
|
|
|
parent.queryContextManager.remove(nameServerAddr(), id);
|
2015-07-12 12:34:05 +02:00
|
|
|
|
|
|
|
// Cancel the timeout task.
|
|
|
|
final ScheduledFuture<?> timeoutFuture = this.timeoutFuture;
|
|
|
|
if (timeoutFuture != null) {
|
|
|
|
timeoutFuture.cancel(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Promise<AddressedEnvelope<DnsResponse, InetSocketAddress>> promise = this.promise;
|
|
|
|
if (promise.setUncancellable()) {
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
AddressedEnvelope<DnsResponse, InetSocketAddress> castResponse =
|
|
|
|
(AddressedEnvelope<DnsResponse, InetSocketAddress>) envelope.retain();
|
2017-01-25 12:51:06 +01:00
|
|
|
if (!promise.trySuccess(castResponse)) {
|
|
|
|
// We failed to notify the promise as it was failed before, thus we need to release the envelope
|
|
|
|
envelope.release();
|
|
|
|
}
|
2015-07-12 12:34:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setFailure(String message, Throwable cause) {
|
2015-11-08 04:59:01 +01:00
|
|
|
final InetSocketAddress nameServerAddr = nameServerAddr();
|
|
|
|
parent.queryContextManager.remove(nameServerAddr, id);
|
2015-07-12 12:34:05 +02:00
|
|
|
|
|
|
|
final StringBuilder buf = new StringBuilder(message.length() + 64);
|
|
|
|
buf.append('[')
|
|
|
|
.append(nameServerAddr)
|
|
|
|
.append("] ")
|
|
|
|
.append(message)
|
|
|
|
.append(" (no stack trace available)");
|
|
|
|
|
|
|
|
final DnsNameResolverException e;
|
2017-11-23 08:51:38 +01:00
|
|
|
if (cause == null) {
|
|
|
|
// This was caused by an timeout so use DnsNameResolverTimeoutException to allow the user to
|
|
|
|
// handle it special (like retry the query).
|
|
|
|
e = new DnsNameResolverTimeoutException(nameServerAddr, question(), buf.toString());
|
2017-11-22 22:05:29 +01:00
|
|
|
} else {
|
2017-11-23 08:51:38 +01:00
|
|
|
e = new DnsNameResolverException(nameServerAddr, question(), buf.toString(), cause);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
2015-07-12 12:34:05 +02:00
|
|
|
promise.tryFailure(e);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
}
|