From 62a61c8df0656945b05798109b0765181e8f3d62 Mon Sep 17 00:00:00 2001 From: Rene Treffer Date: Thu, 3 Apr 2014 22:38:44 +0200 Subject: [PATCH] Allow querying specific dns server by name/ip and port --- src/de/measite/minidns/Client.java | 47 ++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/src/de/measite/minidns/Client.java b/src/de/measite/minidns/Client.java index beb461bec..8bc75d015 100644 --- a/src/de/measite/minidns/Client.java +++ b/src/de/measite/minidns/Client.java @@ -30,6 +30,16 @@ public class Client { */ protected Random random; + /** + * The buffer size for dns replies. + */ + protected int bufferSize = 1500; + + /** + * DNS timeout. + */ + protected int timeout = 5000; + /** * 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. * @param name The DNS name to request. @@ -83,6 +113,17 @@ public class Client { * @throws IOException On IOErrors. */ 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(); message.setQuestions(new Question[]{q}); message.setRecursionDesired(true); @@ -90,10 +131,10 @@ public class Client { byte[] buf = message.toArray(); DatagramSocket socket = new DatagramSocket(); DatagramPacket packet = new DatagramPacket( - buf, buf.length, InetAddress.getByName(host), 53); - socket.setSoTimeout(5000); + buf, buf.length, InetAddress.getByName(host), port); + socket.setSoTimeout(timeout); socket.send(packet); - packet = new DatagramPacket(new byte[513], 513); + packet = new DatagramPacket(new byte[bufferSize], bufferSize); socket.receive(packet); DNSMessage dnsMessage = DNSMessage.parse(packet.getData()); if (dnsMessage.getId() != message.getId()) {