From 865d0da0aa2a0caeee2bdfb0a2a6d1c059913f1e Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Wed, 10 Aug 2016 16:56:42 +0200 Subject: [PATCH] Initial MDNS API --- src/zutil/net/dns/DNSResolutionListener.java | 29 ++++++++++++++++ src/zutil/net/dns/MulticastDNSClient.java | 35 +++++++++++++++++--- 2 files changed, 60 insertions(+), 4 deletions(-) create mode 100755 src/zutil/net/dns/DNSResolutionListener.java diff --git a/src/zutil/net/dns/DNSResolutionListener.java b/src/zutil/net/dns/DNSResolutionListener.java new file mode 100755 index 0000000..61930d7 --- /dev/null +++ b/src/zutil/net/dns/DNSResolutionListener.java @@ -0,0 +1,29 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015 Ziver Koc + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package zutil.net.dns; + + +public interface DNSResolutionListener{ + void receivedResponse(DNSPacket packet); + } \ No newline at end of file diff --git a/src/zutil/net/dns/MulticastDNSClient.java b/src/zutil/net/dns/MulticastDNSClient.java index a82b333..1958347 100755 --- a/src/zutil/net/dns/MulticastDNSClient.java +++ b/src/zutil/net/dns/MulticastDNSClient.java @@ -37,6 +37,9 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.DatagramPacket; import java.net.InetAddress; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -53,13 +56,24 @@ public class MulticastDNSClient extends ThreadedUDPNetwork implements ThreadedUD private static final int MDNS_MULTICAST_PORT = 5353; + private HashMap> activeProbes; + private DNSResolutionListener listener; + + public MulticastDNSClient() throws IOException { super(MDNS_MULTICAST_ADDR, MDNS_MULTICAST_PORT); setThread( this ); + + this.activeProbes = new HashMap<>(); + } + + public void setListener(DNSResolutionListener listener){ + this.listener = listener; } - public void sendProbe(String service) throws IOException { + public void sendProbe(String domain) throws IOException { + activeProbes.put(domain, new ArrayList()); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); BinaryStructOutputStream out = new BinaryStructOutputStream(buffer); @@ -67,7 +81,7 @@ public class MulticastDNSClient extends ThreadedUDPNetwork implements ThreadedUD dnsPacket.getHeader().id = (int)(Math.random() * 0xFFFF); dnsPacket.getHeader().setDefaultQueryData(); dnsPacket.addQuestion(new DNSPacketQuestion( - service, + domain, DNSPacketQuestion.QTYPE_A, DNSPacketQuestion.QCLASS_IN)); dnsPacket.write(out); @@ -77,7 +91,7 @@ public class MulticastDNSClient extends ThreadedUDPNetwork implements ThreadedUD InetAddress.getByName( MDNS_MULTICAST_ADDR ), MDNS_MULTICAST_PORT ); - System.out.println("#### Sending Request"); + logger.fine("Sending MDSN probe for domain: " + domain); System.out.println(ByteUtil.toFormattedString(udpPacket.getData(), udpPacket.getOffset(), udpPacket.getLength())); MultiPrintStream.out.dump(dnsPacket,3); @@ -92,11 +106,24 @@ public class MulticastDNSClient extends ThreadedUDPNetwork implements ThreadedUD BinaryStructInputStream in = new BinaryStructInputStream(buffer); DNSPacket dnsPacket = DNSPacket.read(in); - System.out.println("#### Received response from "+packet.getAddress()); System.out.println(ByteUtil.toFormattedString(packet.getData(), packet.getOffset(), packet.getLength())); MultiPrintStream.out.dump(dnsPacket,3); + + if (dnsPacket.getHeader().flagQueryResponse && dnsPacket.getHeader().countAnswerRecord > 0) { + String domain = null; + if (domain != null && activeProbes.containsKey(domain)){ + List list = activeProbes.get(domain); + if (!list.contains(domain)){ + logger.fine("Received MDSN response from: "+packet.getAddress()+", domain: " + domain); + list.add(domain); + if (listener != null) + listener.receivedResponse(dnsPacket); + } + } + } } catch (IOException e){ logger.log(Level.WARNING, null, e); } } + }