| 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.MiniDnsException; |
| 14 |
import org.minidns.dnsmessage.DnsMessage; |
| 15 |
import org.minidns.dnsqueryresult.DnsQueryResult.QueryMethod; |
| 16 |
import org.minidns.dnsqueryresult.StandardDnsQueryResult; |
| 17 |
import org.minidns.util.MultipleIoException; |
| 18 |
|
| 19 |
import java.io.DataInputStream; |
| 20 |
import java.io.DataOutputStream; |
| 21 |
import java.io.IOException; |
| 22 |
import java.net.DatagramPacket; |
| 23 |
import java.net.DatagramSocket; |
| 24 |
import java.net.InetAddress; |
| 25 |
import java.net.InetSocketAddress; |
| 26 |
import java.net.Socket; |
| 27 |
import java.net.SocketAddress; |
| 28 |
import java.net.SocketException; |
| 29 |
import java.util.ArrayList; |
| 30 |
import java.util.List; |
| 31 |
import java.util.logging.Level; |
| 32 |
import java.util.logging.Logger; |
| 33 |
|
| 34 |
public class NetworkDataSource extends AbstractDnsDataSource { |
| 35 |
|
| 36 |
protected static final Logger LOGGER = Logger.getLogger(NetworkDataSource.class.getName()); |
| 37 |
|
| 38 |
// TODO: Rename 'message' parameter to query. |
| 39 |
@Override |
| 40 |
public StandardDnsQueryResult query(DnsMessage message, InetAddress address, int port) throws IOException { |
| 41 |
final QueryMode queryMode = getQueryMode(); |
| 42 |
boolean doUdpFirst; |
| 43 |
switch (queryMode) { |
| 44 |
case dontCare: |
| 45 |
case udpTcp: |
| 46 |
doUdpFirst = true; |
| 47 |
break; |
| 48 |
case tcp: |
| 49 |
doUdpFirst = false; |
| 50 |
break; |
| 51 |
default: |
| 52 |
throw new IllegalStateException("Unsupported query mode: " + queryMode); |
| 53 |
} |
| 54 |
|
| 55 |
List<IOException> ioExceptions = new ArrayList<>(2); |
| 56 |
DnsMessage dnsMessage = null; |
| 57 |
|
| 58 |
if (doUdpFirst) { |
| 59 |
try { |
| 60 |
dnsMessage = queryUdp(message, address, port); |
| 61 |
} catch (IOException e) { |
| 62 |
ioExceptions.add(e); |
| 63 |
} |
| 64 |
|
| 65 |
// TODO: This null check could probably be removed by now. |
| 66 |
if (dnsMessage != null && !dnsMessage.truncated) { |
| 67 |
return new StandardDnsQueryResult(address, port, QueryMethod.udp, message, dnsMessage); |
| 68 |
} |
| 69 |
|
| 70 |
assert dnsMessage == null || dnsMessage.truncated || ioExceptions.size() == 1; |
| 71 |
LOGGER.log(Level.FINE, "Fallback to TCP because {0}", |
| 72 |
new Object[] { dnsMessage != null ? "response is truncated" : ioExceptions.get(0) }); |
| 73 |
} |
| 74 |
|
| 75 |
try { |
| 76 |
dnsMessage = queryTcp(message, address, port); |
| 77 |
} catch (IOException e) { |
| 78 |
ioExceptions.add(e); |
| 79 |
MultipleIoException.throwIfRequired(ioExceptions); |
| 80 |
} |
| 81 |
|
| 82 |
return new StandardDnsQueryResult(address, port, QueryMethod.tcp, message, dnsMessage); |
| 83 |
} |
| 84 |
|
| 85 |
protected DnsMessage queryUdp(DnsMessage message, InetAddress address, int port) throws IOException { |
| 86 |
// TODO Use a try-with-resource statement here once miniDNS minimum |
| 87 |
// required Android API level is >= 19 |
| 88 |
DatagramSocket socket = null; |
| 89 |
DatagramPacket packet = message.asDatagram(address, port); |
| 90 |
byte[] buffer = new byte[udpPayloadSize]; |
| 91 |
try { |
| 92 |
socket = createDatagramSocket(); |
| 93 |
socket.setSoTimeout(timeout); |
| 94 |
socket.send(packet); |
| 95 |
packet = new DatagramPacket(buffer, buffer.length); |
| 96 |
socket.receive(packet); |
| 97 |
DnsMessage dnsMessage = new DnsMessage(packet.getData()); |
| 98 |
if (dnsMessage.id != message.id) { |
| 99 |
throw new MiniDnsException.IdMismatch(message, dnsMessage); |
| 100 |
} |
| 101 |
return dnsMessage; |
| 102 |
} finally { |
| 103 |
if (socket != null) { |
| 104 |
socket.close(); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
protected DnsMessage queryTcp(DnsMessage message, InetAddress address, int port) throws IOException { |
| 110 |
// TODO Use a try-with-resource statement here once miniDNS minimum |
| 111 |
// required Android API level is >= 19 |
| 112 |
Socket socket = null; |
| 113 |
try { |
| 114 |
socket = createSocket(); |
| 115 |
SocketAddress socketAddress = new InetSocketAddress(address, port); |
| 116 |
socket.connect(socketAddress, timeout); |
| 117 |
socket.setSoTimeout(timeout); |
| 118 |
DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); |
| 119 |
message.writeTo(dos); |
| 120 |
dos.flush(); |
| 121 |
DataInputStream dis = new DataInputStream(socket.getInputStream()); |
| 122 |
int length = dis.readUnsignedShort(); |
| 123 |
byte[] data = new byte[length]; |
| 124 |
int read = 0; |
| 125 |
while (read < length) { |
| 126 |
read += dis.read(data, read, length - read); |
| 127 |
} |
| 128 |
DnsMessage dnsMessage = new DnsMessage(data); |
| 129 |
if (dnsMessage.id != message.id) { |
| 130 |
throw new MiniDnsException.IdMismatch(message, dnsMessage); |
| 131 |
} |
| 132 |
return dnsMessage; |
| 133 |
} finally { |
| 134 |
if (socket != null) { |
| 135 |
socket.close(); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Create a {@link Socket} using the system default {@link javax.net.SocketFactory}. |
| 142 |
* |
| 143 |
* @return The new {@link Socket} instance |
| 144 |
*/ |
| 145 |
protected Socket createSocket() { |
| 146 |
return new Socket(); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Create a {@link DatagramSocket} using the system defaults. |
| 151 |
* |
| 152 |
* @return The new {@link DatagramSocket} instance |
| 153 |
* @throws SocketException If creation of the {@link DatagramSocket} fails |
| 154 |
*/ |
| 155 |
protected DatagramSocket createDatagramSocket() throws SocketException { |
| 156 |
return new DatagramSocket(); |
| 157 |
} |
| 158 |
} |