Fixed a DNS bug and added IPv6 entry

This commit is contained in:
Ziver Koc 2021-08-24 01:24:09 +02:00
parent e42d25fa99
commit 946f5f5133
5 changed files with 157 additions and 19 deletions

View file

@ -92,7 +92,7 @@ public class MulticastDnsServer extends ThreadedUDPNetwork implements ThreadedUD
resource.name = name;
resource.type = type;
resource.clazz = clazz;
//resource.ttl = 10; ???
resource.ttl = 120; // client can cache the record for 2 min
resource.length = data.length;
resource.data = new String(data, StandardCharsets.ISO_8859_1);
@ -118,6 +118,7 @@ public class MulticastDnsServer extends ThreadedUDPNetwork implements ThreadedUD
// Just handle queries and no responses
if (! dnsPacket.getHeader().flagQueryResponse) {
DnsPacket response = handleReceivedPacket(packet.getAddress(), dnsPacket);
if (response != null) {
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
BinaryStructOutputStream out = new BinaryStructOutputStream(outBuffer);
@ -139,6 +140,7 @@ public class MulticastDnsServer extends ThreadedUDPNetwork implements ThreadedUD
protected DnsPacket handleReceivedPacket(InetAddress address, DnsPacket request) {
DnsPacket response = new DnsPacket();
response.getHeader().setDefaultResponseData();
for (DnsPacketQuestion question : request.getQuestions()) {
if (question.name == null) continue;
@ -148,8 +150,9 @@ public class MulticastDnsServer extends ThreadedUDPNetwork implements ThreadedUD
// ------------------------------------------
case DnsConstants.TYPE.A:
case DnsConstants.TYPE.AAAA:
if (entries.containsKey(question.name)) {
logger.finer("Received request for domain: '" + question.name + "' from source: " + address);
logger.finer("Received request for IPv4 domain: '" + question.name + "' from source: " + address);
response.addAnswerRecord(entries.get(question.name));
} else {
logger.finest("Received request for unknown domain: '" + question.name + "' from source: " + address);
@ -175,7 +178,7 @@ public class MulticastDnsServer extends ThreadedUDPNetwork implements ThreadedUD
logger.finer("Received request for service: '" + question.name + "' from source: " + address);
response.addAnswerRecord(entries.get(question.name));
} else {
logger.finer("Received request for unknown pointer: '" + question.name + "' from source: " + address);
logger.finest("Received request for unknown pointer: '" + question.name + "' from source: " + address);
}
break;
}

View file

@ -79,7 +79,7 @@ public final class DnsConstants {
public static final class TYPE {
/** a host address */
/** a IPv4 host address */
public static final int A = 1;
/** an authoritative name server */
public static final int NS = 2;
@ -111,6 +111,8 @@ public final class DnsConstants {
public static final int MX = 15;
/** text strings */
public static final int TXT = 16;
/** a IPv6 host address */
public static final int AAAA = 28;
/** service location record in format {Instance}.{Service}.{Domain}*/
public static final int SRV = 33;
/** A request for a transfer of an entire zone */

View file

@ -68,7 +68,7 @@ public class DnsPacketQuestion implements BinaryStruct {
*
* @see DnsConstants.TYPE
*/
@BinaryField(index=10, length=16)
@BinaryField(index=20, length=16)
public int type;
/**
@ -77,7 +77,7 @@ public class DnsPacketQuestion implements BinaryStruct {
*
* @see DnsConstants.CLASS
*/
@BinaryField(index=20, length=16)
@BinaryField(index=30, length=16)
public int clazz;

View file

@ -38,22 +38,22 @@ public class FQDNStringSerializer implements BinaryFieldSerializer<String> {
public String read(InputStream in, BinaryFieldData field) throws IOException {
StringBuilder str = new StringBuilder();
int c = in.read();
// Is this a pointer
if ((c & 0b1100_0000) == 0b1100_0000) {
int offset = (c & 0b0011_1111) << 8;
offset |= in.read() & 0b1111_1111;
str.append(offset);
}
// Normal Domain String
else {
while (c > 0) {
int c;
while ((c=in.read()) > 0) {
if (str.length() > 0) // Don't add dot to first loop
str.append('.');
if ((c & 0b1100_0000) == 0b1100_0000) {
// This a pointer
int offset = (c & 0b0011_1111) << 8;
offset |= in.read() & 0b1111_1111;
str.append(offset);
} else {
// Normal String
for (int i = 0; i < c; ++i) {
str.append((char) in.read());
}
c = in.read();
if (c > 0)
str.append('.');
}
}
return str.toString();