Skip to content

Commit

Permalink
Merge pull request #5 from Menduist/master
Browse files Browse the repository at this point in the history
Add AAAA support & update nimble package structure
  • Loading branch information
ba0f3 authored Jun 7, 2021
2 parents cf85f99 + 4fd5f2a commit 647ed10
Show file tree
Hide file tree
Showing 21 changed files with 30 additions and 6 deletions.
3 changes: 2 additions & 1 deletion dnsclient.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.0.3"
version = "0.1.0"
author = "Huy Doan"
description = "Simple DNS Client & Library"
license = "MIT"
Expand All @@ -9,6 +9,7 @@ installExt = @["nim"]
bin = @["dnsclient"]



# Dependencies

requires "nim >= 0.20.0"
2 changes: 1 addition & 1 deletion src/dnsclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Simple DNS client

import strutils, streams, net, nativesockets, endians
import private/[protocol, records, types]
import dnsclientpkg/[protocol, records, types]

export records, types, TimeoutError

Expand Down
2 changes: 2 additions & 0 deletions src/private/protocol.nim → src/dnsclientpkg/protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ proc parseRR(data: StringStream): ResourceRecord =
case kind
of A:
result = ARecord(name: name, kind: kind)
of AAAA:
result = AAAARecord(name: name, kind: kind)
of CNAME:
result = CNAMERecord(name: name, kind: kind)
of HINFO:
Expand Down
3 changes: 3 additions & 0 deletions src/dnsclientpkg/records.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import streams, strutils, types, utils

include records/[a, aaaa, cname, hinfo, mb, minfo, mr, mx, ns, ptrr, soa, srv, txt]
File renamed without changes.
8 changes: 8 additions & 0 deletions src/dnsclientpkg/records/aaaa.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type AAAARecord* = ref object of ResourceRecord
address_v6*: array[16, uint8]

method toString*(r: AAAARecord): string = ipv6ToString(r.address_v6)

method parse*(r: AAAARecord, data: StringStream) =
for i in 0..<16:
r.address_v6[i] = data.readUInt8()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions src/private/utils.nim → src/dnsclientpkg/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,10 @@ proc getName*(data: StringStream): string =
proc ipv4ToString*(ip: int32): string =
let arr = cast[array[4, uint8]](ip)
arr.join(".")

proc ipv6ToString*(ip6: array[16, uint8]): string =
for i in 0..<8:
result &= ":"
result &= ip6[i * 2].toHex()
result &= ip6[i * 2 + 1].toHex()
result.removePrefix(":")
3 changes: 0 additions & 3 deletions src/private/records.nim

This file was deleted.

8 changes: 7 additions & 1 deletion tests/test1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ test "query A":
let rr = ARecord(resp.answers[0])
assert rr.toString() == "8.8.8.8"

test "query AAAA":
let resp = client.sendQuery("google.fr", AAAA)
assert resp.answers[0].kind == AAAA
let rr = AAAARecord(resp.answers[0])
#assert rr.toString() == "0000:0000:0000:0000:0000:0000:0000:0001" ??

test "query TXT":
let resp = client.sendQuery("txt.example.huy.im", TXT)
assert resp.answers[0].kind == TXT
Expand All @@ -41,4 +47,4 @@ test "query SRV":
assert rr.priority == 10
assert rr.weight == 15
assert rr.port == 25
assert rr.target == "smtp.yandex.ru"
assert rr.target == "smtp.yandex.ru"

0 comments on commit 647ed10

Please sign in to comment.