| 1 |
/* |
| 2 |
* Copyright 2015-2020 the original author or authors |
| 3 |
* |
| 4 |
* This software is licensed under the Apache License, Version 2.0, |
| 5 |
* the GNU Lesser General Public License version 2 or later ("LGPL") |
| 6 |
* and the WTFPL. |
| 7 |
* You may choose either license to govern your use of this software only |
| 8 |
* upon the condition that you accept all of the terms of either |
| 9 |
* the Apache License 2.0, the LGPL 2.1+ or the WTFPL. |
| 10 |
*/ |
| 11 |
package org.minidns.source; |
| 12 |
|
| 13 |
import org.minidns.DnsCache; |
| 14 |
import org.minidns.MiniDnsFuture; |
| 15 |
import org.minidns.MiniDnsFuture.InternalMiniDnsFuture; |
| 16 |
import org.minidns.dnsmessage.DnsMessage; |
| 17 |
import org.minidns.dnsqueryresult.DnsQueryResult; |
| 18 |
|
| 19 |
import java.io.IOException; |
| 20 |
import java.net.InetAddress; |
| 21 |
|
| 22 |
public abstract class AbstractDnsDataSource implements DnsDataSource { |
| 23 |
|
| 24 |
@Override |
| 25 |
public abstract DnsQueryResult query(DnsMessage message, InetAddress address, int port) throws IOException; |
| 26 |
|
| 27 |
@Override |
| 28 |
public MiniDnsFuture<DnsQueryResult, IOException> queryAsync(DnsMessage message, InetAddress address, int port, OnResponseCallback onResponseCallback) { |
| 29 |
InternalMiniDnsFuture<DnsQueryResult, IOException> future = new InternalMiniDnsFuture<>(); |
| 30 |
DnsQueryResult result; |
| 31 |
try { |
| 32 |
result = query(message, address, port); |
| 33 |
} catch (IOException e) { |
| 34 |
future.setException(e); |
| 35 |
return future; |
| 36 |
} |
| 37 |
future.setResult(result); |
| 38 |
return future; |
| 39 |
} |
| 40 |
|
| 41 |
protected int udpPayloadSize = 1024; |
| 42 |
|
| 43 |
/** |
| 44 |
* DNS timeout. |
| 45 |
*/ |
| 46 |
protected int timeout = 5000; |
| 47 |
|
| 48 |
@Override |
| 49 |
public int getTimeout() { |
| 50 |
return timeout; |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public void setTimeout(int timeout) { |
| 55 |
if (timeout <= 0) { |
| 56 |
throw new IllegalArgumentException("Timeout must be greater than zero"); |
| 57 |
} |
| 58 |
this.timeout = timeout; |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public int getUdpPayloadSize() { |
| 63 |
return udpPayloadSize; |
| 64 |
} |
| 65 |
|
| 66 |
public void setUdpPayloadSize(int udpPayloadSize) { |
| 67 |
if (udpPayloadSize <= 0) { |
| 68 |
throw new IllegalArgumentException("UDP payload size must be greater than zero"); |
| 69 |
} |
| 70 |
this.udpPayloadSize = udpPayloadSize; |
| 71 |
} |
| 72 |
|
| 73 |
private DnsCache cache; |
| 74 |
|
| 75 |
protected final void cacheResult(DnsMessage request, DnsQueryResult response) { |
| 76 |
final DnsCache activeCache = cache; |
| 77 |
if (activeCache == null) { |
| 78 |
return; |
| 79 |
} |
| 80 |
activeCache.put(request, response); |
| 81 |
} |
| 82 |
|
| 83 |
public enum QueryMode { |
| 84 |
/** |
| 85 |
* Perform the query mode that is assumed "best" for that particular case. |
| 86 |
*/ |
| 87 |
dontCare, |
| 88 |
|
| 89 |
/** |
| 90 |
* Try UDP first, and if the result is bigger than the maximum UDP payload size, or if something else goes wrong, fallback to TCP. |
| 91 |
*/ |
| 92 |
udpTcp, |
| 93 |
|
| 94 |
/** |
| 95 |
* Always use only TCP when querying DNS servers. |
| 96 |
*/ |
| 97 |
tcp, |
| 98 |
} |
| 99 |
|
| 100 |
private QueryMode queryMode = QueryMode.dontCare; |
| 101 |
|
| 102 |
public void setQueryMode(QueryMode queryMode) { |
| 103 |
if (queryMode == null) { |
| 104 |
throw new IllegalArgumentException(); |
| 105 |
} |
| 106 |
this.queryMode = queryMode; |
| 107 |
} |
| 108 |
|
| 109 |
public QueryMode getQueryMode() { |
| 110 |
return queryMode; |
| 111 |
} |
| 112 |
|
| 113 |
} |