Changed DNS question and resource to use BinaryStruct

This commit is contained in:
Ziver Koc 2016-04-14 16:45:22 +02:00
parent dd1b55106b
commit 6c81d79332
2 changed files with 78 additions and 60 deletions

View file

@ -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<String> {
public static class DomainStringSerializer implements BinaryFieldSerializer<String> {
public String read(InputStream in, BinaryFieldData field) throws IOException {
StringBuilder str = new StringBuilder();
@ -97,11 +100,13 @@ public class DNSPacketQuestion implements BinaryStruct {
}
public void write(OutputStream out, String domain, BinaryFieldData field) throws IOException {
if (domain != null){
String[] labels = domain.split(".");
for (String label : labels) {
out.write(label.length());
out.write(label.getBytes());
}
}
out.write(0);
}

View file

@ -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;
public static DNSPacketResource read(InputStream in){
return null;
}
/**
* 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;
/**
* two octets which specify the class of the data in the
* RDATA field.
*/
@BinaryField(index=30, length=16)
private int clazz;
/**
* 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;
public void write(OutputStream out) throws IOException {
}
}