From 38cdb839d1e35b8927ca957298e68b8616a0f453 Mon Sep 17 00:00:00 2001 From: mawalu Date: Sun, 13 Feb 2022 11:31:07 +0100 Subject: [PATCH] Include question in response --- lib/dns.nim | 14 ++++++++++++++ src/state.nim | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/dns.nim b/lib/dns.nim index 9b0fe63..4c3ad0b 100644 --- a/lib/dns.nim +++ b/lib/dns.nim @@ -142,6 +142,15 @@ func parseQuestion*(data: string, startOffset: uint16): (DnsQuestion, uint16) = qclass: DnsClass(toUint16(data[offset + 3], data[offset + 2])) ), offset + 4) +func packQuestion*(data: DnsQuestion): string = + var question = "" + + question.add(packNameField(data.qname)) + question.add(uint16ToString(data.qtype.uint16)) + question.add(uint16ToString(data.qclass.uint16)) + + return question + # BROKEN func parseResourceRecord*(data: string, startOffset: uint16): (DnsRecord, uint16) = let (names, offset) = parseNameField(data, startOffset) @@ -183,6 +192,9 @@ func parseMessage*(data: string): DnsMessage = func packMessage*(message: DnsMessage): string = var encoded = packHeader(message.header) + for question in message.questions: + encoded.add(packQuestion(question)) + for answer in message.answer: encoded.add(packResourceRecord(answer)) @@ -205,7 +217,9 @@ func mkResponse*(id: uint16, question: DnsQuestion, answer: seq[string]): DnsMes qr: DnsQr.RESPONSE, aa: true, rcode: Rcode.NO_ERROR, + qdcount: 1, ancount: len(answer).uint16 ), + questions: @[question], answer: answer.map(proc (a: string): DnsRecord = mkRecord(question.qtype, question.qname, a)) ) \ No newline at end of file diff --git a/src/state.nim b/src/state.nim index 927c883..3b5e467 100644 --- a/src/state.nim +++ b/src/state.nim @@ -1,4 +1,4 @@ -import tables, strtabs, sequtils, nativesockets, strutils +import tables, strtabs, sequtils, nativesockets import ../lib/dns type