Added a DNSPacket class and Initial implementation of custom BinaryFields
This commit is contained in:
parent
609716adcb
commit
6071ad7c70
7 changed files with 171 additions and 27 deletions
52
src/zutil/net/dns/DNSPacket.java
Executable file
52
src/zutil/net/dns/DNSPacket.java
Executable file
|
|
@ -0,0 +1,52 @@
|
|||
package zutil.net.dns;
|
||||
|
||||
import zutil.parser.binary.BinaryStructOutputStream;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* This class is a general wrapper for a whole DNS packet.
|
||||
*
|
||||
* Created by ezivkoc on 2016-04-11.
|
||||
*/
|
||||
public class DNSPacket {
|
||||
private DNSPacketHeader header;
|
||||
private ArrayList<DNSPacketQuestion> questions;
|
||||
private ArrayList<DNSPacketResource> answerRecords;
|
||||
private ArrayList<DNSPacketResource> nameServers;
|
||||
private ArrayList<DNSPacketResource> additionalRecords;
|
||||
|
||||
|
||||
public DNSPacket(){
|
||||
header = new DNSPacketHeader();
|
||||
questions = new ArrayList<>();
|
||||
answerRecords = new ArrayList<>();
|
||||
nameServers = new ArrayList<>();
|
||||
additionalRecords = new ArrayList<>();
|
||||
}
|
||||
|
||||
|
||||
public static DNSPacket read(InputStream in){
|
||||
return null;
|
||||
}
|
||||
|
||||
public void write(OutputStream out) throws IOException {
|
||||
BinaryStructOutputStream structOut = new BinaryStructOutputStream(out);
|
||||
structOut.write(header);
|
||||
out.flush();
|
||||
|
||||
/*for (DNSPacketQuestion question : questions)
|
||||
question.write(out);
|
||||
for (DNSPacketResource answerRecord : answerRecords)
|
||||
answerRecord.write(out);
|
||||
for (DNSPacketResource nameServer : nameServers)
|
||||
nameServer.write(out);
|
||||
for (DNSPacketResource additionalRecord : additionalRecords)
|
||||
additionalRecord.write(out);*/
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -24,8 +24,14 @@
|
|||
|
||||
package zutil.net.dns;
|
||||
|
||||
import zutil.parser.binary.BinaryFieldData;
|
||||
import zutil.parser.binary.BinaryFieldSerializer;
|
||||
import zutil.parser.binary.BinaryStruct;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2016-02-09.
|
||||
* Reference: http://tools.ietf.org/html/rfc1035
|
||||
|
|
@ -64,6 +70,40 @@ public class DNSPacketQuestion implements BinaryStruct {
|
|||
For example, the QCLASS field is IN for the Internet.
|
||||
*/
|
||||
|
||||
@CustomBinaryField(index=10, serializer=QNameSerializer.class)
|
||||
private String qName;
|
||||
@BinaryField(index=10, length=1)
|
||||
private int qType;
|
||||
@BinaryField(index=20, length=1)
|
||||
private int qClass;
|
||||
|
||||
|
||||
|
||||
|
||||
private static class QNameSerializer implements BinaryFieldSerializer<String> {
|
||||
|
||||
public String read(InputStream in, BinaryFieldData field) throws IOException {
|
||||
StringBuilder str = new StringBuilder();
|
||||
int c = in.read();
|
||||
while (c > 0){
|
||||
for (int i=0; i<c; ++i){
|
||||
str.append((char)in.read());
|
||||
}
|
||||
c = in.read();
|
||||
if (c > 0)
|
||||
str.append('.');
|
||||
}
|
||||
return toString();
|
||||
}
|
||||
|
||||
public void write(OutputStream out, String domain, BinaryFieldData field) throws IOException {
|
||||
String[] labels = domain.split(".");
|
||||
for (String label : labels){
|
||||
out.write(label.length());
|
||||
out.write(label.getBytes());
|
||||
}
|
||||
out.write(0);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,6 +26,10 @@ package zutil.net.dns;
|
|||
|
||||
import zutil.parser.binary.BinaryStruct;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2016-02-09.
|
||||
* Reference: http://tools.ietf.org/html/rfc1035
|
||||
|
|
@ -85,4 +89,13 @@ public class DNSPacketResource implements BinaryStruct {
|
|||
the RDATA field is a 4 octet ARPA Internet address.
|
||||
*/
|
||||
|
||||
|
||||
public static DNSPacketResource read(InputStream in){
|
||||
return null;
|
||||
}
|
||||
|
||||
public void write(OutputStream out) throws IOException {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,11 +53,10 @@ public class MulticastDNSClient extends ThreadedUDPNetwork implements ThreadedUD
|
|||
BinaryStructOutputStream out = new BinaryStructOutputStream(buffer);
|
||||
|
||||
DNSPacketHeader header = new DNSPacketHeader();
|
||||
header.setDefaultQueryData();
|
||||
out.write(header);
|
||||
|
||||
DatagramPacket packet = null;
|
||||
|
||||
packet = new DatagramPacket(
|
||||
DatagramPacket packet = new DatagramPacket(
|
||||
buffer.toByteArray(), buffer.size(),
|
||||
InetAddress.getByName( MDNS_MULTICAST_ADDR ),
|
||||
MDNS_MULTICAST_PORT );
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue