From 559b1857b9f841702d591ddcc9ffc1ceb56b6ad1 Mon Sep 17 00:00:00 2001 From: mawalu Date: Sun, 6 Feb 2022 20:48:36 +0100 Subject: [PATCH] Respond with NS record --- example.config | 2 ++ lib/dns.nim | 13 +++++++++---- norbert.nim | 7 +++++++ src/dnsserver.nim | 8 +++++--- src/state.nim | 3 ++- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/example.config b/example.config index 365b1cf..f989f3d 100644 --- a/example.config +++ b/example.config @@ -1,5 +1,7 @@ # base domain for all records baseDomain = "acme.example.com" +# the domain this dns server can be reached at +serverDomain = "dns.example.com" dnsPort = 15353 apiPort = 18000 diff --git a/lib/dns.nim b/lib/dns.nim index 9b0fe63..f3089e7 100644 --- a/lib/dns.nim +++ b/lib/dns.nim @@ -61,7 +61,7 @@ type header*: DnsHeader questions*: seq[DnsQuestion] answer*: seq[DnsRecord] - authroity*: seq[DnsRecord] + authority*: seq[DnsRecord] additional*: seq[DnsRecord] func parseNameField*(data: string, startOffset: uint16): (seq[string], uint16) = @@ -164,7 +164,7 @@ func packResourceRecord*(data: DnsRecord): string = record.add(uint16ToString(data.class.uint16)) record.add(uint32ToString(data.ttl.uint32)) record.add(uint16ToString(data.rdlength.uint16)) - record.add(data.rdata) + record.add((if data.rtype == DnsType.NS: packNameField(data.rdata) else: data.rdata)) return record @@ -186,6 +186,9 @@ func packMessage*(message: DnsMessage): string = for answer in message.answer: encoded.add(packResourceRecord(answer)) + for authroity in message.authority: + encoded.add(packResourceRecord(authroity)) + return encoded func mkRecord*(rtype: DnsType, question: string, answer: string): DnsRecord = @@ -198,14 +201,16 @@ func mkRecord*(rtype: DnsType, question: string, answer: string): DnsRecord = rdata: (if rtype == DnsType.TXT: chr(len(answer)) & answer else: answer) ) -func mkResponse*(id: uint16, question: DnsQuestion, answer: seq[string]): DnsMessage = +func mkResponse*(id: uint16, question: DnsQuestion, answer: seq[string], authority: string, base: string): DnsMessage = return DnsMessage( header: DnsHeader( id: id, qr: DnsQr.RESPONSE, aa: true, rcode: Rcode.NO_ERROR, - ancount: len(answer).uint16 + ancount: len(answer).uint16, + nscount: 1 ), + authority: @[mkRecord(DnsType.NS, base, authority)], answer: answer.map(proc (a: string): DnsRecord = mkRecord(question.qtype, question.qname, a)) ) \ No newline at end of file diff --git a/norbert.nim b/norbert.nim index ad037e2..01a08a8 100644 --- a/norbert.nim +++ b/norbert.nim @@ -22,8 +22,15 @@ proc initConfig(): AppConfig = echo "Error parsing port config" quit 1 + let serverName = configFile.getSectionValue("", "serverDomain") + + if serverName == "": + echo "Missing serverDomain" + quit 1 + let config = AppConfig( base: configFile.getSectionValue("", "baseDomain"), + serverName: serverName, users: newStringTable(), apiPort: Port(apiPort), dnsPort: Port(dnsPort) diff --git a/src/dnsserver.nim b/src/dnsserver.nim index 3b2fca5..e2a69d7 100644 --- a/src/dnsserver.nim +++ b/src/dnsserver.nim @@ -2,7 +2,7 @@ import asyncnet, asyncdispatch, nativesockets import strutils, options, tables, strformat import ../lib/dns, state -proc handleDnsRequest(records: RecordsTable, data: string): Option[string] = +proc handleDnsRequest(records: RecordsTable, data: string, config: AppConfig): Option[string] = let msg = parseMessage(data) echo msg @@ -14,7 +14,9 @@ proc handleDnsRequest(records: RecordsTable, data: string): Option[string] = let response = mkResponse( msg.header.id, question, - records.getOrDefault((name: question.qname.toLowerAscii(), dtype: question.qtype), @[]) + records.getOrDefault((name: question.qname.toLowerAscii(), dtype: question.qtype), @[]), + config.serverName, + config.base ) echo response @@ -31,7 +33,7 @@ proc serveDns*(config: AppConfig) {.async.} = while true: try: let request = await dns.recvFrom(size=512) - let response = handleDnsRequest(records, request.data) + let response = handleDnsRequest(records, request.data, config) if (response.isSome): await dns.sendTo(request.address, request.port, response.unsafeGet) diff --git a/src/state.nim b/src/state.nim index 927c883..94643f9 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 @@ -13,6 +13,7 @@ type AppConfig* = object users*: StringTableRef base*: string + serverName*: string apiPort*: Port dnsPort*: Port