From e8e3deb056f382e3ec3281dd198f221d59d914b5 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Tue, 9 Aug 2016 17:57:17 +0200 Subject: [PATCH] Added DNS message compression, it does not handle it right now --- src/zutil/converter/Converter.java | 6 +- src/zutil/net/dns/DNSPacket.java | 3 +- src/zutil/net/dns/DNSPacketQuestion.java | 21 ++- src/zutil/parser/binary/BinaryFieldData.java | 14 ++ test/zutil/net/dns/DNSPacketTest.java | 139 +++++++++++++++++++ 5 files changed, 174 insertions(+), 9 deletions(-) diff --git a/src/zutil/converter/Converter.java b/src/zutil/converter/Converter.java index 3aaacee..f490f4e 100755 --- a/src/zutil/converter/Converter.java +++ b/src/zutil/converter/Converter.java @@ -128,8 +128,9 @@ public class Converter { * * @param hex is the hex value * @return a byte that corresponds to the hex + * @throws IllegalArgumentException if hex is not a valid hex character */ - public static byte hexToByte( char hex ){ + public static byte hexToByte( char hex ) throws IllegalArgumentException{ switch( Character.toLowerCase(hex) ){ case '0': return 0x00; case '1': return 0x01; @@ -147,8 +148,9 @@ public class Converter { case 'd': return 0x0d; case 'e': return 0x0e; case 'f': return 0x0f; + default: + throw new IllegalArgumentException("'"+hex+"' is an illegal hex character only 0-9 and a-f characters allowed"); } - return (byte)0; } /** diff --git a/src/zutil/net/dns/DNSPacket.java b/src/zutil/net/dns/DNSPacket.java index c8de49d..bcafcdc 100755 --- a/src/zutil/net/dns/DNSPacket.java +++ b/src/zutil/net/dns/DNSPacket.java @@ -11,7 +11,8 @@ import java.util.List; /** * This class is a general wrapper for a whole DNS packet. * - * Created by Ziver on 2016-04-11. + * @see rfc1035 + * @author Ziver on 2016-04-11. */ public class DNSPacket { private DNSPacketHeader header; diff --git a/src/zutil/net/dns/DNSPacketQuestion.java b/src/zutil/net/dns/DNSPacketQuestion.java index 6e35df6..862bf79 100755 --- a/src/zutil/net/dns/DNSPacketQuestion.java +++ b/src/zutil/net/dns/DNSPacketQuestion.java @@ -151,13 +151,22 @@ public class DNSPacketQuestion implements BinaryStruct { 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 0) { + for (int i = 0; i < c; ++i) { + str.append((char) in.read()); + } + c = in.read(); + if (c > 0) + str.append('.'); } - c = in.read(); - if (c > 0) - str.append('.'); } return str.toString(); } diff --git a/src/zutil/parser/binary/BinaryFieldData.java b/src/zutil/parser/binary/BinaryFieldData.java index fe38363..dce0693 100755 --- a/src/zutil/parser/binary/BinaryFieldData.java +++ b/src/zutil/parser/binary/BinaryFieldData.java @@ -156,4 +156,18 @@ public class BinaryFieldData { public BinaryFieldSerializer getSerializer(){ return serializer; } + + + @Override + public String toString(){ + return field.getDeclaringClass().getSimpleName() + "::" + field.getName() + + " (" + + (lengthField != null ? + "LengthField: " + lengthField +", LengthMultiplier: "+lengthMultiplier : + length+" bits") + + (serializer != null ? + ", Serializer: " + serializer.getClass().getName() : "") + + ")"; + + } } \ No newline at end of file diff --git a/test/zutil/net/dns/DNSPacketTest.java b/test/zutil/net/dns/DNSPacketTest.java index 6a0f3bc..32ea129 100755 --- a/test/zutil/net/dns/DNSPacketTest.java +++ b/test/zutil/net/dns/DNSPacketTest.java @@ -173,4 +173,143 @@ public class DNSPacketTest { assertEquals("IPv4", new String(new char[]{0x99, 0x6d, 0x07, 0x5a}), answer.data); } + + @Test + public void rawQuery() throws IOException { +/* +Source: http://serverfault.com/questions/173187/what-does-a-dns-request-look-like +The DNS part starts with 24 1a: +0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 ........ ......E. +0010 00 3c 51 e3 40 00 40 11 ea cb 7f 00 00 01 7f 00 .