Initial MDNS API
This commit is contained in:
parent
5e1f2d65e6
commit
865d0da0aa
2 changed files with 60 additions and 4 deletions
29
src/zutil/net/dns/DNSResolutionListener.java
Executable file
29
src/zutil/net/dns/DNSResolutionListener.java
Executable file
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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<String,List<String>> 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<String>());
|
||||
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<String> 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue