From f13813186f7d106ff32e525b2517227108651dd9 Mon Sep 17 00:00:00 2001 From: Rene Treffer Date: Tue, 22 Jul 2014 23:22:35 +0200 Subject: [PATCH 1/4] Add PTR record parsing --- .../java/de/measite/minidns/DNSMessage.java | 2 +- src/main/java/de/measite/minidns/Record.java | 4 ++++ .../java/de/measite/minidns/record/PTR.java | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/measite/minidns/record/PTR.java diff --git a/src/main/java/de/measite/minidns/DNSMessage.java b/src/main/java/de/measite/minidns/DNSMessage.java index fbee3b19c..01c096450 100644 --- a/src/main/java/de/measite/minidns/DNSMessage.java +++ b/src/main/java/de/measite/minidns/DNSMessage.java @@ -424,7 +424,7 @@ public class DNSMessage { message.authenticData = ((header >> 5) & 1) == 1; message.checkDisabled = ((header >> 4) & 1) == 1; message.responseCode = RESPONSE_CODE.getResponseCode(header & 0xf); - message.receiveTimestamp = System.currentTimeMillis(); + message.receiveTimestamp = System.currentTimeMillis(); int questionCount = dis.readUnsignedShort(); int answerCount = dis.readUnsignedShort(); int nameserverCount = dis.readUnsignedShort(); diff --git a/src/main/java/de/measite/minidns/Record.java b/src/main/java/de/measite/minidns/Record.java index 8f8d0d0e4..f14aa0b8f 100644 --- a/src/main/java/de/measite/minidns/Record.java +++ b/src/main/java/de/measite/minidns/Record.java @@ -9,6 +9,7 @@ import de.measite.minidns.record.AAAA; import de.measite.minidns.record.CNAME; import de.measite.minidns.record.Data; import de.measite.minidns.record.NS; +import de.measite.minidns.record.PTR; import de.measite.minidns.record.SRV; import de.measite.minidns.util.NameUtil; @@ -250,6 +251,9 @@ public class Record { case CNAME: this.payloadData = new CNAME(); break; + case PTR: + this.payloadData = new PTR(); + break; default: System.out.println("Unparsed type " + type); this.payloadData = null; diff --git a/src/main/java/de/measite/minidns/record/PTR.java b/src/main/java/de/measite/minidns/record/PTR.java new file mode 100644 index 000000000..6e2006554 --- /dev/null +++ b/src/main/java/de/measite/minidns/record/PTR.java @@ -0,0 +1,19 @@ +package de.measite.minidns.record; + +import java.io.DataInputStream; +import java.io.IOException; + +import de.measite.minidns.Record.TYPE; +import de.measite.minidns.util.NameUtil; + +/** + * A PTR record is handled like a CNAME + */ +public class PTR extends CNAME { + + @Override + public TYPE getType() { + return TYPE.PTR; + } + +} From b0f8bec1cbfa6d21e0873c63d5213bd12d429bd8 Mon Sep 17 00:00:00 2001 From: Rene Treffer Date: Wed, 23 Jul 2014 00:00:55 +0200 Subject: [PATCH 2/4] Add TXT record --- src/main/java/de/measite/minidns/Record.java | 10 ++- .../java/de/measite/minidns/record/TXT.java | 65 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/measite/minidns/record/TXT.java diff --git a/src/main/java/de/measite/minidns/Record.java b/src/main/java/de/measite/minidns/Record.java index f14aa0b8f..6aaf4f7d0 100644 --- a/src/main/java/de/measite/minidns/Record.java +++ b/src/main/java/de/measite/minidns/Record.java @@ -11,6 +11,7 @@ import de.measite.minidns.record.Data; import de.measite.minidns.record.NS; import de.measite.minidns.record.PTR; import de.measite.minidns.record.SRV; +import de.measite.minidns.record.TXT; import de.measite.minidns.util.NameUtil; /** @@ -231,7 +232,11 @@ public class Record { public void parse(DataInputStream dis, byte[] data) throws IOException { this.name = NameUtil.parse(dis, data); this.type = TYPE.getType(dis.readUnsignedShort()); - this.clazz = CLASS.getClass(dis.readUnsignedShort()); + int clazzValue = dis.readUnsignedShort(); + this.clazz = CLASS.getClass(clazzValue); + if (this.clazz == null) { + System.out.println("Unknown class " + clazzValue); + } this.ttl = (((long)dis.readUnsignedShort()) << 32) + dis.readUnsignedShort(); int payloadLength = dis.readUnsignedShort(); @@ -254,6 +259,9 @@ public class Record { case PTR: this.payloadData = new PTR(); break; + case TXT: + this.payloadData = new TXT(); + break; default: System.out.println("Unparsed type " + type); this.payloadData = null; diff --git a/src/main/java/de/measite/minidns/record/TXT.java b/src/main/java/de/measite/minidns/record/TXT.java new file mode 100644 index 000000000..03e730401 --- /dev/null +++ b/src/main/java/de/measite/minidns/record/TXT.java @@ -0,0 +1,65 @@ +package de.measite.minidns.record; + +import java.io.DataInputStream; +import java.io.IOException; + +import de.measite.minidns.Record.TYPE; +import de.measite.minidns.util.NameUtil; + +/** + * TXT record (actually a binary blob with wrappers for text content). + */ +public class TXT implements Data { + + protected byte[] blob; + + public byte[] getBlob() { + return blob; + } + + public void setBlob(byte[] blob) { + this.blob = blob; + } + + public String getText() { + try { + return (new String(blob, "UTF-8")).intern(); + } catch (Exception e) { + /* Can't happen for UTF-8 unless it's really a blob */ + return null; + } + } + + public void setText(String text) { + try { + this.blob = text.getBytes("UTF-8"); + } catch (Exception e) { + /* Can't happen, UTF-8 IS supported */ + throw new RuntimeException("UTF-8 not supported", e); + } + } + + @Override + public byte[] toByteArray() { + throw new UnsupportedOperationException("Not implemented yet"); + } + + @Override + public void parse(DataInputStream dis, byte[] data, int length) + throws IOException + { + blob = new byte[length]; + dis.readFully(blob); + } + + @Override + public TYPE getType() { + return TYPE.TXT; + } + + @Override + public String toString() { + return "\"" + getText() + "\""; + } + +} From 70d403f0b612f6cb26b8fc194e35c622c5383167 Mon Sep 17 00:00:00 2001 From: Rene Treffer Date: Wed, 23 Jul 2014 21:53:38 +0200 Subject: [PATCH 3/4] Better / more verbose toString --- src/main/java/de/measite/minidns/DNSMessage.java | 5 ++++- src/main/java/de/measite/minidns/Question.java | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/measite/minidns/DNSMessage.java b/src/main/java/de/measite/minidns/DNSMessage.java index 01c096450..ab2535ce1 100644 --- a/src/main/java/de/measite/minidns/DNSMessage.java +++ b/src/main/java/de/measite/minidns/DNSMessage.java @@ -515,7 +515,10 @@ public class DNSMessage { public String toString() { return "-- DNSMessage " + id + " --\n" + - Arrays.toString(answers); + "Q" + Arrays.toString(questions) + + "NS" + Arrays.toString(nameserverRecords) + + "A" + Arrays.toString(answers) + + "ARR" + Arrays.toString(additionalResourceRecords); } } diff --git a/src/main/java/de/measite/minidns/Question.java b/src/main/java/de/measite/minidns/Question.java index 628622510..883003561 100644 --- a/src/main/java/de/measite/minidns/Question.java +++ b/src/main/java/de/measite/minidns/Question.java @@ -134,4 +134,9 @@ public class Question { byte o[] = ((Question)other).toByteArray(); return Arrays.equals(t, o); } + + @Override + public String toString() { + return "Question/" + clazz + "/" + type + ": " + name; + } } From 2bddcc9f997e5b1e565478e99c55488c86a85e76 Mon Sep 17 00:00:00 2001 From: Rene Treffer Date: Wed, 23 Jul 2014 22:43:12 +0200 Subject: [PATCH 4/4] Add parsing for mdns QU records (RFC6762) --- src/main/java/de/measite/minidns/Record.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/measite/minidns/Record.java b/src/main/java/de/measite/minidns/Record.java index 6aaf4f7d0..26af47115 100644 --- a/src/main/java/de/measite/minidns/Record.java +++ b/src/main/java/de/measite/minidns/Record.java @@ -222,6 +222,11 @@ public class Record { */ protected Data payloadData; + /** + * MDNS defines the highest bit of the class as the unicast query bit. + */ + protected boolean unicastQuery; + /** * Parse a given record based on the full message data and the current * stream position. @@ -233,7 +238,8 @@ public class Record { this.name = NameUtil.parse(dis, data); this.type = TYPE.getType(dis.readUnsignedShort()); int clazzValue = dis.readUnsignedShort(); - this.clazz = CLASS.getClass(clazzValue); + this.clazz = CLASS.getClass(clazzValue & 0x7fff); + this.unicastQuery = (clazzValue & 0x8000) > 0; if (this.clazz == null) { System.out.println("Unknown class " + clazzValue); } @@ -298,6 +304,14 @@ public class Record { (q.getName().equals(name)); } + /** + * See if this query/response was a unicast query (highest class bit set). + * @return True if it is a unicast query/response record. + */ + public boolean isUnicastQuery() { + return unicastQuery; + } + /** * The generic record name, e.g. "measite.de". * @return The record name.