From 6c81d7933263005b50aac33122ecc43603f4e92d Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Thu, 14 Apr 2016 16:45:22 +0200 Subject: [PATCH] Changed DNS question and resource to use BinaryStruct --- src/zutil/net/dns/DNSPacketQuestion.java | 63 +++++++++++--------- src/zutil/net/dns/DNSPacketResource.java | 75 ++++++++++++++---------- 2 files changed, 78 insertions(+), 60 deletions(-) diff --git a/src/zutil/net/dns/DNSPacketQuestion.java b/src/zutil/net/dns/DNSPacketQuestion.java index fa61531..503df08 100755 --- a/src/zutil/net/dns/DNSPacketQuestion.java +++ b/src/zutil/net/dns/DNSPacketQuestion.java @@ -44,43 +44,46 @@ public class DNSPacketQuestion implements BinaryStruct { 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | | - / QNAME / + / NAME / / / +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - | QTYPE | + | TYPE | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - | QCLASS | + | CLASS | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ where: - - QNAME a domain name represented as a sequence of labels, where - each label consists of a length octet followed by that - number of octets. The domain name terminates with the - zero length octet for the null label of the root. Note - that this field may be an odd number of octets; no - padding is used. - - QTYPE a two octet code which specifies the type of the query. - The values for this field include all codes valid for a - TYPE field, together with some more general codes which - can match more than one type of RR. - - QCLASS a two octet code that specifies the class of the query. - For example, the QCLASS field is IN for the Internet. */ - @CustomBinaryField(index=10, serializer=QNameSerializer.class) - private String qName; + /** + * a domain name represented as a sequence of labels, where + * each label consists of a length octet followed by that + * number of octets. The domain name terminates with the + * zero length octet for the null label of the root. Note + * that this field may be an odd number of octets; no + * padding is used. + */ + @CustomBinaryField(index=10, serializer=DomainStringSerializer.class) + private String name; + + /** + * a two octet code which specifies the type of the query. + * The values for this field include all codes valid for a + * TYPE field, together with some more general codes which + * can match more than one type of RR. + */ @BinaryField(index=10, length=1) - private int qType; + private int type; + + /** + * a two octet code that specifies the class of the query. + * For example, the QCLASS field is IN for the Internet. + */ @BinaryField(index=20, length=1) - private int qClass; + private int clazz; - - - private static class QNameSerializer implements BinaryFieldSerializer { + public static class DomainStringSerializer implements BinaryFieldSerializer { public String read(InputStream in, BinaryFieldData field) throws IOException { StringBuilder str = new StringBuilder(); @@ -97,10 +100,12 @@ public class DNSPacketQuestion implements BinaryStruct { } 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()); + if (domain != null){ + String[] labels = domain.split("."); + for (String label : labels) { + out.write(label.length()); + out.write(label.getBytes()); + } } out.write(0); } diff --git a/src/zutil/net/dns/DNSPacketResource.java b/src/zutil/net/dns/DNSPacketResource.java index be2b5b8..6080803 100755 --- a/src/zutil/net/dns/DNSPacketResource.java +++ b/src/zutil/net/dns/DNSPacketResource.java @@ -26,9 +26,6 @@ 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. @@ -63,39 +60,55 @@ public class DNSPacketResource implements BinaryStruct { +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ where: + */ - NAME a domain name to which this resource record pertains. - - TYPE two octets containing one of the RR type codes. This - field specifies the meaning of the data in the RDATA - field. - - CLASS two octets which specify the class of the data in the - RDATA field. - - TTL a 32 bit unsigned integer that specifies the time - interval (in seconds) that the resource record may be - cached before it should be discarded. Zero values are - interpreted to mean that the RR can only be used for the - transaction in progress, and should not be cached. - - RDLENGTH an unsigned 16 bit integer that specifies the length in - octets of the RDATA field. - - RDATA a variable length string of octets that describes the - resource. The format of this information varies - according to the TYPE and CLASS of the resource record. - For example, the if the TYPE is A and the CLASS is IN, - the RDATA field is a 4 octet ARPA Internet address. + /** + * a domain name to which this resource record pertains. */ + @CustomBinaryField(index=10, serializer=DNSPacketQuestion.DomainStringSerializer.class) + private String name; + /** + * two octets containing one of the RR type codes. This + * field specifies the meaning of the data in the RDATA + * field. + */ + @BinaryField(index=20, length=16) + private int type; - public static DNSPacketResource read(InputStream in){ - return null; - } + /** + * two octets which specify the class of the data in the + * RDATA field. + */ + @BinaryField(index=30, length=16) + private int clazz; - public void write(OutputStream out) throws IOException { + /** + * a 32 bit unsigned integer that specifies the time + * interval (in seconds) that the resource record may be + * cached before it should be discarded. Zero values are + * interpreted to mean that the RR can only be used for the + * transaction in progress, and should not be cached. + */ + @BinaryField(index=40, length=32) + private int ttl; + + /** + * an unsigned 16 bit integer that specifies the length in + * octets of the RDATA field. + */ + @BinaryField(index=50, length=16) + private int length; + + /** + * a variable length string of octets that describes the + * resource. The format of this information varies + * according to the TYPE and CLASS of the resource record. + * For example, if the TYPE is A and the CLASS is IN, + * the RDATA field is a 4 octet ARPA Internet address. + */ + @VariableLengthBinaryField(index=60, lengthField="length") + private String data; - } }