You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A pure Nim Domain Name System (DNS) client implemented with dnsprotocol.
This implementation has synchronous and asynchronous (async) procedures (procs) for transmitting data over the internet, using both UDP and TCP protocol.
For a "real-life" async example, see resolver.nim. In this example I have made as many comments as possible, even if they look silly. I think it might help someone, as a similar example I provided privately for a newcomer to Nim. It can also be compiled with -d:showLoopLog to show the async workflow.
Advanced Use
Creating a Message object with a QType.A query for the domain name nim-lang.org, transmitting the Message and receiving the response (not async):
import ndns
let header = initHeader(randId(), rd =true)
let question = initQuestion("nim-lang.org", QType.A, QClass.IN)
# If the last character of "nim-lang.org" is not a '.', the initializer will# add, as it is called the DNS root.let msg = initMessage(header, @[question])# The initializer automatically changes `header.qdcount` to `1'u16`let client = initDnsClient()
var rmsg = dnsQuery(client, msg)
echo repr(rmsg)
Creating a Message object with a QType.A query for the domain name nim-lang.org, transmitting the Message and receiving the response (async):
import asyncdispatch, ndns
let header = initHeader(randId(), rd =true)
let question = initQuestion("nim-lang.org", QType.A, QClass.IN)
# If the last character of "nim-lang.org" is not a '.', the initializer will# add, as it is called the DNS root.let msg = initMessage(header, @[question])# The initializer automatically changes `header.qdcount` to `1'u16`let client = initDnsClient()
var rmsg = waitFor dnsAsyncQuery(client, msg)
echo repr(rmsg)
Using System DNS Server
You can initialize the DNS client with the DNS resolver server used by the system. To do this, start the client with initSystemDnsClient.
import ndns
let client = initSystemDnsClient()
echo resolveIpv4(client, "nim-lang.org")