src/dnsc

Types

DnsClient = object
  ## Dns server IP.
  ## DNS server listening port.
Contains information about the DNS server.
IsNotAnResponseError = object of CatchableError
Raised if not a response (!= QR.Response).
OpCodeNotEqualError = object of CatchableError
Raised if the OpCode is different between the query and the response.
ResponseIdNotEqualError = object of CatchableError
Raised if the query ID does not match the response ID.
ResponseIpNotEqualError = object of CatchableError
Raised if the IP that sent the response is different from the IP that received the query (only UDP).
ResponsePortNotEqualError = object of CatchableError
Raised if the Port that sent the response is different from the Port that received the query (only UDP).
UnexpectedDisconnectionError = object of CatchableError
Raised if an unexpected disconnect occurs (only TCP).

Consts

ndnsDnsServerIp {.strdefine.} = "8.8.8.8"
Default dns server ip for queries. You can change by compiling with -d:ndnsDnsServerIp=1.1.1.1.

Procs

proc dnsQuery(client: DnsClient; msg: Message;
              timeout: Duration = 500.milliseconds; retransmit = false): Future[
    Message] {....stackTrace: false, raises: [], gcsafe, tags: [RootEffect],
               forbids: [].}

Returns a Message of the DNS query response performed using the UDP protocol.

Parameters

  • client is a DnsClient object that contains the IP and Port of the DNS server.
  • msg is a Message object that contains the DNS query.
  • timeout is the maximum waiting time, in milliseconds, to receive the response from the DNS server. When it is negative (less than 0), it will try to receive the response for an unlimited time.
  • retransmit when true, determine the retransmission of the query to TCP protocol when the received response is truncated (header.flags.tc == true).
proc dnsTcpQuery(client: DnsClient; msg: Message;
                 timeout: Duration = 5000.milliseconds): Future[Message] {.
    ...stackTrace: false, raises: [], gcsafe, tags: [RootEffect], forbids: [].}

Returns a Message of the DNS query response performed using the TCP protocol

Parameters

  • client is a DnsClient object that contains the IP and Port of the DNS server.
  • msg is a Message object that contains the DNS query.
  • timeout is the maximum waiting time, in milliseconds, to connect to the DNS server. When it is negative (less than 0), it will try to connect for an unlimited time.
proc getIp(client: DnsClient): string {....raises: [], tags: [], forbids: [].}
Returns the IP defined in the client.
proc getPort(client: DnsClient): Port {....raises: [], tags: [], forbids: [].}
Returns the port defined in the client.
proc initDnsClient(strIp: string = ndnsDnsServerIp; port: Port = Port(53)): DnsClient {.
    ...raises: [ValueError], tags: [], forbids: [].}

Returns a created DnsClient object.

Parameters

  • ip is a DNS server IP. It can be IPv4 or IPv6. It cannot be a domain name.
  • port is a DNS server listening port.
proc initSystemDnsClient(): DnsClient {....raises: [CatchableError], tags: [],
                                        forbids: [].}

Returns a DnsClient object, in which the dns server IP is the first one used by the system. If it is not possible to determine a dns server IP by the system, it will be initialized with ndnsDnsServerIp.

Currently implemented for:

Notes:

  • If your platform is not listed above and uses a resolver configuration file, compile with -d:ndnsUseResolver.
  • It just creates a DnsClient object with the IP used by the system. Does not use the system's native DNS resolution implementation unless the system provides a proxy.
  • The ip field in the DnsClient object does not change automatically if the IP used by the system changes.
proc prepareDnsBL(strIp, dnsbl: string): string {....raises: [ValueError],
    tags: [], forbids: [].}

Returns a domain name for DnsBL query.

Parameters

  • ip is the IP address you want to query. It can be an IPv4 or IPv6. It cannot be a domain name.
  • dnsbl is the domain name that maintains the blacklist.
proc prepareRDns(strIp: string): string {....raises: [ValueError], tags: [],
    forbids: [].}

Returns a domain name for reverse DNS lookup.

Parameters

  • ip is the IP address you want to query. It can be an IPv4 or IPv6. It cannot be a domain name.
proc randId(): uint16 {.inline, ...raises: [], tags: [], forbids: [].}
Returns a uint16, randomly generated, to be used as an id.
proc resolveDnsBL(client: DnsClient; strIp, dnsbl: string;
                  timeout: Duration = 500.milliseconds): Future[seq[string]] {.
    ...stackTrace: false, raises: [], gcsafe, tags: [RootEffect], forbids: [].}

Returns IPv4 addresses. Usually the loopback address (127.0.0.0/24), in which the last octet of IPv4 represents something on the black list.

Parameters

  • client is a DnsClient object that contains the IP and Port of the DNS server.
  • ip is the IPv4 or IPv6 address that you want to know if it is blacklisted.
  • dnsbl is the domain name for DnsBL queries.
  • timeout is the maximum waiting time, in milliseconds, to connect to the DNS server or to receive the response from the DNS server. When it is negative (less than 0), it will try to connect for an unlimited time or to receive the response for an unlimited time.
proc resolveIpv4(client: DnsClient; domain: string;
                 timeout: Duration = 500.milliseconds): Future[seq[string]] {.
    ...stackTrace: false, raises: [], gcsafe, tags: [RootEffect], forbids: [].}

Returns all IPv4 addresses, in a seq[string], that have been resolved from domain. The seq[string] can be empty.

Parameters

  • client is a DnsClient object that contains the IP and Port of the DNS server.
  • domain is the domain name that you wish to obtain IPv4 addresses.
  • timeout is the maximum waiting time, in milliseconds, to connect to the DNS server or to receive the response from the DNS server. When it is negative (less than 0), it will try to connect for an unlimited time or to receive the response for an unlimited time.
proc resolveIpv6(client: DnsClient; domain: string;
                 timeout: Duration = 500.milliseconds): Future[seq[string]] {.
    ...stackTrace: false, raises: [], gcsafe, tags: [RootEffect], forbids: [].}

Returns all IPv6 addresses, in a seq[string], that have been resolved from domain. The seq[string] can be empty.

Parameters

  • client is a DnsClient object that contains the IP and Port of the DNS server.
  • domain is the domain name that you wish to obtain IPv6 addresses.
  • timeout is the maximum waiting time, in milliseconds, to connect to the DNS server or to receive the response from the DNS server. When it is negative (less than 0), it will try to connect for an unlimited time or to receive the response for an unlimited time.
proc resolveRDns(client: DnsClient; strIp: string;
                 timeout: Duration = 500.milliseconds): Future[seq[string]] {.
    ...stackTrace: false, raises: [], gcsafe, tags: [RootEffect], forbids: [].}

Returns all domain names, in a seq[string], which is obtained by the "reverse" query of ip. The seq[string] can be empty.

Parameters

  • client is a DnsClient object that contains the IP and Port of the DNS server.
  • ip is the IPv4 or IPv6 address that is intended to obtain the domain name, which represents the reverse address.
  • timeout is the maximum waiting time, in milliseconds, to connect to the DNS server or to receive the response from the DNS server. When it is negative (less than 0), it will try to connect for an unlimited time or to receive the response for an unlimited time.