Allow querying specific dns server by name/ip and port

This commit is contained in:
Rene Treffer 2014-04-03 22:38:44 +02:00
parent 114bda4d3f
commit 62a61c8df0
1 changed files with 44 additions and 3 deletions

View File

@ -30,6 +30,16 @@ public class Client {
*/ */
protected Random random; protected Random random;
/**
* The buffer size for dns replies.
*/
protected int bufferSize = 1500;
/**
* DNS timeout.
*/
protected int timeout = 5000;
/** /**
* Create a new DNS client. * Create a new DNS client.
*/ */
@ -41,6 +51,26 @@ public class Client {
} }
} }
/**
* Query a nameserver for a single entry.
* @param name The DNS name to request.
* @param type The DNS type to request (SRV, A, AAAA, ...).
* @param clazz The class of the request (usually IN for Internet).
* @param host The DNS server host.
* @param port The DNS server port.
* @return
* @throws IOException On IO Errors.
*/
public DNSMessage query(String name, TYPE type, CLASS clazz, String host, int port)
throws IOException
{
Question q = new Question();
q.setClazz(clazz);
q.setType(type);
q.setName(name);
return query(q, host, port);
}
/** /**
* Query a nameserver for a single entry. * Query a nameserver for a single entry.
* @param name The DNS name to request. * @param name The DNS name to request.
@ -83,6 +113,17 @@ public class Client {
* @throws IOException On IOErrors. * @throws IOException On IOErrors.
*/ */
public DNSMessage query(Question q, String host) throws IOException { public DNSMessage query(Question q, String host) throws IOException {
return query(q, host, 53);
}
/**
* Query a specific server for one entry.
* @param q The question section of the DNS query.
* @param host The dns server host.
* @param port the dns port.
* @throws IOException On IOErrors.
*/
public DNSMessage query(Question q, String host, int port) throws IOException {
DNSMessage message = new DNSMessage(); DNSMessage message = new DNSMessage();
message.setQuestions(new Question[]{q}); message.setQuestions(new Question[]{q});
message.setRecursionDesired(true); message.setRecursionDesired(true);
@ -90,10 +131,10 @@ public class Client {
byte[] buf = message.toArray(); byte[] buf = message.toArray();
DatagramSocket socket = new DatagramSocket(); DatagramSocket socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket( DatagramPacket packet = new DatagramPacket(
buf, buf.length, InetAddress.getByName(host), 53); buf, buf.length, InetAddress.getByName(host), port);
socket.setSoTimeout(5000); socket.setSoTimeout(timeout);
socket.send(packet); socket.send(packet);
packet = new DatagramPacket(new byte[513], 513); packet = new DatagramPacket(new byte[bufferSize], bufferSize);
socket.receive(packet); socket.receive(packet);
DNSMessage dnsMessage = DNSMessage.parse(packet.getData()); DNSMessage dnsMessage = DNSMessage.parse(packet.getData());
if (dnsMessage.getId() != message.getId()) { if (dnsMessage.getId() != message.getId()) {