Refactored Dns classes

This commit is contained in:
Ziver Koc 2017-02-02 17:39:19 +01:00
parent 71c8d017ca
commit e8491617c6
7 changed files with 163 additions and 166 deletions

View file

@ -0,0 +1,115 @@
package zutil.net.dns;
/**
*
*/
public final class DnsConstants {
public static final class OPCODE {
/** a standard query (QUERY) */
public static final int QUERY = 0;
/** an inverse query (IQUERY) */
public static final int IQUERY = 1;
/** a server status request (STATUS) */
public static final int STATUS = 2;
}
/** DNS Response Codes */
public static final class RCODE {
/**
* No error condition
*/
public static final int NO_ERROR = 0;
/**
* Format error - The name server was
* unable to interpret the query.
*/
public static final int FORMAT_ERROR = 1;
/**
* Server failure - The name server was
* unable to process this query due to a
* problem with the name server.
*/
public static final int SERVER_FAILURE = 2;
/**
* Name Error - Meaningful only for
* responses from an authoritative name
* server, this code signifies that the
* domain name referenced in the query does
* not exist.
*/
public static final int NAME_ERROR = 3;
/**
* Not Implemented - The name server does
* not support the requested kind of query.
*/
public static final int NOT_IMPLEMENTED = 4;
/**
* Refused - The name server refuses to
* perform the specified operation for
* policy reasons.
*/
public static final int REFUSED = 5;
}
public static final class TYPE {
/** a host address */
public static final int A = 1;
/** an authoritative name server */
public static final int NS = 2;
/** a mail destination (Obsolete - use MX) */
public static final int MD = 3;
/** a mail forwarder (Obsolete - use MX) */
public static final int MF = 4;
/** the canonical name for an alias */
public static final int CNAME = 5;
/** marks the start of a zone of authority */
public static final int SOA = 6;
/** a mailbox domain name (EXPERIMENTAL) */
public static final int MB = 7;
/** a mail group member (EXPERIMENTAL) */
public static final int MG = 8;
/** a mail rename domain name (EXPERIMENTAL) */
public static final int MR = 9;
/** a null RR (EXPERIMENTAL) */
public static final int NULL = 10;
/** a well known service description */
public static final int WKS = 11;
/** a domain name pointer */
public static final int PTR = 12;
/** host information */
public static final int HINFO = 13;
/** mailbox or mail list information */
public static final int MINFO = 14;
/** mail exchange */
public static final int MX = 15;
/** text strings */
public static final int TXT = 16;
/** service location record in format {Instance}.{Service}.{Domain}*/
public static final int SRV = 33;
/** A request for a transfer of an entire zone */
public static final int AXFR = 252;
/** A request for mailbox-related records (MB, MG or MR) */
public static final int MAILB = 253;
/** A request for mail agent RRs (Obsolete - see MX) */
public static final int MAILA = 254;
/** A request for all records */
public static final int ANY = 255;
}
public static final class CLASS {
/** the Internet */
public static final int IN = 1;
/** the CSNET class (Obsolete - used only for examples in some obsolete RFCs) */
public static final int CS = 2;
/** the CHAOS class */
public static final int CH = 3;
/** Hesiod [Dyer 87] */
public static final int HS = 4;
/** any class */
public static final int ANY = 255;
}
}

View file

@ -11,7 +11,7 @@ import java.util.List;
/**
* This class is a general wrapper for a whole DNS packet.
*
* @see <a href="https://tools.ietf.org/html/rfc1035">rfc1035</a>
* @see <a href="https://tools.ietf.org/html/rfc1035">DNS Spec (rfc1035)</a>
* @author Ziver on 2016-04-11.
*/
public class DnsPacket {

View file

@ -27,20 +27,10 @@ package zutil.net.dns;
import zutil.parser.binary.BinaryStruct;
/**
* Created by Ziver on 2016-02-09.
* Reference: http://tools.ietf.org/html/rfc1035
* @see <a href="http://tools.ietf.org/html/rfc1035">DNS Spec (rfc1035)</a>
* @author Ziver
*/
public class DnsPacketHeader implements BinaryStruct {
public static final int OPCODE_QUERY = 0;
public static final int OPCODE_IQUERY = 1;
public static final int OPCODE_STATUS = 2;
public static final int RCODE_NO_ERROR = 0;
public static final int RCODE_FORMAT_ERROR = 1;
public static final int RCODE_SERVER_FAILURE = 2;
public static final int RCODE_NAME_ERROR = 3;
public static final int RCODE_NOT_IMPLEMENTED = 4;
public static final int RCODE_REFUSED = 5;
/*
Header section format
@ -81,11 +71,8 @@ public class DnsPacketHeader implements BinaryStruct {
public boolean flagQueryResponse;
/**
* A four bit field that specifies kind of query in this message.
* <pre>
* 0 a standard query (QUERY)
* 1 an inverse query (IQUERY)
* 2 a server status request (STATUS)
* </pre>
*
* @see DnsConstants.OPCODE
*/
@BinaryField(index=11, length=4)
public int flagOperationCode;
@ -127,24 +114,8 @@ public class DnsPacketHeader implements BinaryStruct {
/**
* this field is set as part of responses.
* The values have the following interpretation:
* <pre>
* 0 No error condition
* 1 Format error - The name server was
* unable to interpret the query.
* 2 Server failure - The name server was
* unable to process this query due to a
* problem with the name server.
* 3 Name Error - Meaningful only for
* responses from an authoritative name
* server, this code signifies that the
* domain name referenced in the query does
* not exist.
* 4 Not Implemented - The name server does
* not support the requested kind of query.
* 5 Refused - The name server refuses to
* perform the specified operation for
* policy reasons.
*</pre>
*
* @see DnsConstants.RCODE
*/
@BinaryField(index=17, length=4)
public int flagResponseCode;

View file

@ -33,63 +33,10 @@ import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by Ziver on 2016-02-09.
* Reference: http://tools.ietf.org/html/rfc1035
* @see <a href="http://tools.ietf.org/html/rfc1035">DNS Spec (rfc1035)</a>
* @author Ziver
*/
public class DnsPacketQuestion implements BinaryStruct {
/** a host address */
public static final int QTYPE_A = 1;
/** an authoritative name server */
public static final int QTYPE_NS = 2;
/** a mail destination (Obsolete - use MX) */
public static final int QTYPE_MD = 3;
/** a mail forwarder (Obsolete - use MX) */
public static final int QTYPE_MF = 4;
/** the canonical name for an alias */
public static final int QTYPE_CNAME = 5;
/** marks the start of a zone of authority */
public static final int QTYPE_SOA = 6;
/** a mailbox domain name (EXPERIMENTAL) */
public static final int QTYPE_MB = 7;
/** a mail group member (EXPERIMENTAL) */
public static final int QTYPE_MG = 8;
/** a mail rename domain name (EXPERIMENTAL) */
public static final int QTYPE_MR = 9;
/** a null RR (EXPERIMENTAL) */
public static final int QTYPE_NULL = 10;
/** a well known service description */
public static final int QTYPE_WKS = 11;
/** a domain name pointer */
public static final int QTYPE_PTR = 12;
/** host information */
public static final int QTYPE_HINFO = 13;
/** mailbox or mail list information */
public static final int QTYPE_MINFO = 14;
/** mail exchange */
public static final int QTYPE_MX = 15;
/** text strings */
public static final int QTYPE_TXT = 16;
/** service location record */
public static final int QTYPE_SRV = 33;
/** A request for a transfer of an entire zone */
public static final int QTYPE_AXFR = 252;
/** A request for mailbox-related records (MB, MG or MR) */
public static final int QTYPE_MAILB = 253;
/** A request for mail agent RRs (Obsolete - see MX) */
public static final int QTYPE_MAILA = 254;
/** A request for all records */
public static final int QTYPE_ANY = 255;
/** the Internet */
public static final int QCLASS_IN = 1;
/** the CSNET class (Obsolete - used only for examples in some obsolete RFCs) */
public static final int QCLASS_CS = 2;
/** the CHAOS class */
public static final int QCLASS_CH = 3;
/** Hesiod [Dyer 87] */
public static final int QCLASS_HS = 4;
/** any class */
public static final int QCLASS_ANY = 255;
/*
Question section format
@ -117,31 +64,35 @@ public class DnsPacketQuestion implements BinaryStruct {
* padding is used.
*/
@CustomBinaryField(index=10, serializer=FQDNStringSerializer.class)
public String qName;
public 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.
*
* @see DnsConstants.TYPE
*/
@BinaryField(index=10, length=16)
public int qType;
public int type;
/**
* a two octet code that specifies the class of the query.
* For example, the QCLASS field is IN for the Internet.
*
* @see DnsConstants.CLASS
*/
@BinaryField(index=20, length=16)
public int qClass;
public int clazz;
public DnsPacketQuestion() {}
public DnsPacketQuestion(String qName, int qType, int qClass) {
this.qName = qName;
this.qType = qType;
this.qClass = qClass;
public DnsPacketQuestion(String name, int type, int clazz) {
this.name = name;
this.type = type;
this.clazz = clazz;
}

View file

@ -28,56 +28,11 @@ import zutil.parser.binary.BinaryStruct;
/**
* Created by Ziver on 2016-02-09.
* Reference: http://tools.ietf.org/html/rfc1035
* @see <a href="http://tools.ietf.org/html/rfc1035">DNS Spec (rfc1035)</a>
* @author Ziver
*/
public class DnsPacketResource implements BinaryStruct {
/** a host address */
public static final int TYPE_A = 1;
/** an authoritative name server */
public static final int TYPE_NS = 2;
/** a mail destination (Obsolete - use MX) */
public static final int TYPE_MD = 3;
/** a mail forwarder (Obsolete - use MX) */
public static final int TYPE_MF = 4;
/** the canonical name for an alias */
public static final int TYPE_CNAME = 5;
/** marks the start of a zone of authority */
public static final int TYPE_SOA = 6;
/** a mailbox domain name (EXPERIMENTAL) */
public static final int TYPE_MB = 7;
/** a mail group member (EXPERIMENTAL) */
public static final int TYPE_MG = 8;
/** a mail rename domain name (EXPERIMENTAL) */
public static final int TYPE_MR = 9;
/** a null RR (EXPERIMENTAL) */
public static final int TYPE_NULL = 10;
/** a well known service description */
public static final int TYPE_WKS = 11;
/** a domain name pointer */
public static final int TYPE_PTR = 12;
/** host information */
public static final int TYPE_HINFO = 13;
/** mailbox or mail list information */
public static final int TYPE_MINFO = 14;
/** mail exchange */
public static final int TYPE_MX = 15;
/** text strings */
public static final int TYPE_TXT = 16;
/** the Internet */
public static final int CLASS_IN = 1;
/** the CSNET class (Obsolete - used only for examples in some obsolete RFCs) */
public static final int CLASS_CS = 2;
/** the CHAOS class */
public static final int CLASS_CH = 3;
/** Hesiod [Dyer 87] */
public static final int CLASS_HS = 4;
/*
The answer, authority, and additional sections all share the same
format: a variable number of resource records, where the number of
@ -117,6 +72,8 @@ public class DnsPacketResource implements BinaryStruct {
* two octets containing one of the RR type codes. This
* field specifies the meaning of the data in the RDATA
* field.
*
* @see DnsConstants.TYPE
*/
@BinaryField(index=20, length=16)
public int type;
@ -124,6 +81,8 @@ public class DnsPacketResource implements BinaryStruct {
/**
* two octets which specify the class of the data in the
* RDATA field.
*
* @see DnsConstants.CLASS
*/
@BinaryField(index=30, length=16)
public int clazz;

View file

@ -44,7 +44,9 @@ import java.util.logging.Logger;
* This class implements a MDNS Client. MDNS is a version
* of the DNS protocol but used a Zeroconf application.
*
* Created by Ziver
* @see <a href="http://tools.ietf.org/html/rfc1035">DNS Spec (rfc1035)</a>
* @see <a href="https://tools.ietf.org/html/rfc6763">DNS-SD Spec (rfc6763)</a>
* @author Ziver
*/
public class MulticastDnsClient extends ThreadedUDPNetwork implements ThreadedUDPNetworkThread{
private static final Logger logger = LogUtil.getLogger();
@ -70,7 +72,7 @@ public class MulticastDnsClient extends ThreadedUDPNetwork implements ThreadedUD
public void sendProbe(String domain) throws IOException {
int id = 0;//(int)(Math.random() * 0xFFFF);
int id = 0; // Needs to be zero when doing multicast
activeProbes.add(id);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
BinaryStructOutputStream out = new BinaryStructOutputStream(buffer);
@ -80,8 +82,8 @@ public class MulticastDnsClient extends ThreadedUDPNetwork implements ThreadedUD
dnsPacket.getHeader().setDefaultQueryData();
dnsPacket.addQuestion(new DnsPacketQuestion(
domain,
DnsPacketQuestion.QTYPE_SRV,
DnsPacketQuestion.QCLASS_IN));
DnsConstants.TYPE.SRV,
DnsConstants.CLASS.IN));
dnsPacket.write(out);
DatagramPacket udpPacket = new DatagramPacket(

View file

@ -34,8 +34,6 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import static org.junit.Assert.*;
import static zutil.net.dns.DnsPacketQuestion.*;
import static zutil.net.dns.DnsPacketResource.*;
/**
* Created by Ziver
@ -94,7 +92,8 @@ public class DnsPacketTest {
public void writeRequestDnsPacketTest() throws IOException {
DnsPacket packet = new DnsPacket();
packet.getHeader().setDefaultQueryData();
packet.addQuestion(new DnsPacketQuestion("appletv.local", QTYPE_A, QCLASS_IN));
packet.addQuestion(new DnsPacketQuestion(
"appletv.local", DnsConstants.TYPE.A, DnsConstants.CLASS.IN));
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
BinaryStructOutputStream out = new BinaryStructOutputStream(buffer);
@ -167,8 +166,8 @@ public class DnsPacketTest {
assertEquals("No Of Answer records", 1, packet.getAnswerRecords().size());
DnsPacketResource answer = packet.getAnswerRecords().get(0);
assertEquals("NAME", "appletv.local", answer.name);
assertEquals("TYPE", TYPE_A, answer.type);
assertEquals("CLASS", CLASS_IN, answer.clazz);
assertEquals("TYPE", DnsConstants.TYPE.A, answer.type);
assertEquals("CLASS", DnsConstants.CLASS.IN, answer.clazz);
assertEquals("TTL", 30720, answer.ttl);
assertEquals("IPv4", new String(new char[]{0x99, 0x6d, 0x07, 0x5a}), answer.data);
}
@ -212,9 +211,9 @@ Domain Name System (query)
header.countQuestion = 1;
DnsPacketQuestion question = new DnsPacketQuestion();
question.qName = "www.google.com";
question.qType = QTYPE_A;
question.qClass = QCLASS_IN;
question.name = "www.google.com";
question.type = DnsConstants.TYPE.A;
question.clazz = DnsConstants.CLASS.IN;
packet.addQuestion(question);
ByteArrayOutputStream buff = new ByteArrayOutputStream();
@ -309,27 +308,27 @@ Domain Name System (response)
// Query
DnsPacketQuestion question = packet.getQuestions().get(0);
assertEquals("qNAME", "www.google.com", question.qName);
assertEquals("qType", DnsPacketQuestion.QTYPE_A, question.qType);
assertEquals("qClass", DnsPacketQuestion.QCLASS_IN, question.qClass);
assertEquals("qNAME", "www.google.com", question.name);
assertEquals("type", DnsConstants.TYPE.A, question.type);
assertEquals("clazz", DnsConstants.CLASS.IN, question.clazz);
// Answer
DnsPacketResource answer = packet.getAnswerRecords().get(0);
assertEquals("NAME", "12", answer.name);
assertEquals("TYPE", TYPE_CNAME, answer.type);
assertEquals("CLASS", CLASS_IN, answer.clazz);
assertEquals("TYPE", DnsConstants.TYPE.CNAME, answer.type);
assertEquals("CLASS", DnsConstants.CLASS.IN, answer.clazz);
assertEquals("TTL", 337977, answer.ttl);
answer = packet.getAnswerRecords().get(1);
assertEquals("NAME", "44", answer.name);
assertEquals("TYPE", TYPE_A, answer.type);
assertEquals("CLASS", CLASS_IN, answer.clazz);
assertEquals("TYPE", DnsConstants.TYPE.A, answer.type);
assertEquals("CLASS", DnsConstants.CLASS.IN, answer.clazz);
assertEquals("TTL", 227, answer.ttl);
answer = packet.getAnswerRecords().get(2);
assertEquals("NAME", "44", answer.name);
assertEquals("TYPE", TYPE_A, answer.type);
assertEquals("CLASS", CLASS_IN, answer.clazz);
assertEquals("TYPE", DnsConstants.TYPE.A, answer.type);
assertEquals("CLASS", DnsConstants.CLASS.IN, answer.clazz);
assertEquals("TTL", 227, answer.ttl);
}
}