DnsClient.NET Documentation

repository·dev·Indexed 21 days ago

https://github.com/michaco/dnsclient.net

A high-performance, open-source .NET library for robust DNS lookups. It supports UDP and TCP protocols, EDNS, TTL-based caching, and automatic nameserver discovery. The library provides support for a wide array of resource records including standard (A, AAAA, MX, TXT), security/DNSSEC (CAA, TLSA, DNSKEY), and AXFR zone transfers. It includes the LookupClient for programmatic queries and the MiniDig command-line sample utility.

Tokens
1.1K
Snippets
4
Records
9
Agent score
73%

What's inside DnsClient.NET

  1. Overview of DnsClient.NET features

    dev

    DnsClient.NET is a high-performance .NET library for DNS lookups. Key capabilities include:

    • Protocol Support: Supports both UDP and TCP. TCP can be configured as a fallback if UDP results are truncated (enabled by default).
    • EDNS Support: Configurable EDNS to adjust UDP buffer sizes and request security-relevant records.
    • Caching:
      • Query result caching based on TTL.
      • Minimum TTL setting to enforce a minimum cache duration.
      • Maximum TTL to cap cache duration.
      • Ability to disable caching entirely.
    • Nameserver Discovery: Automatically discovers nameservers from local system configuration, including network interfaces and Windows NRPT policies.
    • Resilience: Supports multiple DNS endpoints (random or sequential order), configurable retries, and configurable error handling (e.g., deciding whether to throw on NotExistentDomain).
    • Observability: Optional audit trails for responses/exceptions and optional Trace/Logging support.
  2. Use DnsString to normalize domain names

    dev

    The DnsString type is used to normalize domain names used in DNS queries and the results returned from them. When you provide a string as input to a query, using DnsString ensures that the domain name adheres to specific DNS domain name rules through parsing.

    Note that DnsString supports implicit conversion back to a standard string type, making it easy to use in existing string-based logic.

  3. Perform DNS lookups with LookupClient

    dev

    To perform DNS queries, instantiate a LookupClient and use the QueryAsync method. You must specify the domain name and the QueryType (e.g., QueryType.A for IPv4 addresses). The result contains an Answers collection which can be filtered using extension methods like ARecords() to retrieve specific record types.

    var lookup = new LookupClient();
    var result = await lookup.QueryAsync("google.com", QueryType.A);
    
    var record = result.Answers.ARecords().FirstOrDefault();
    var ip = record?.Address;
  4. Build and run the MiniDig sample

    dev

    MiniDig is a command-line utility example that demonstrates how to use the DnsClient library to perform DNS lookups, similar to the Linux dig tool.

    To build and run the sample:

    1. Open a command line (Windows) or bash (Linux/macOS).
    2. Navigate to the /Samples/MiniDig directory.
    3. Execute dotnet restore to restore dependencies.
    4. Execute dotnet run to start the application.

    Note: MiniDig targets .NET 5 and requires the .NET 5 SDK or runtime to be installed.

    cd Samples/MiniDig
    dotnet restore
    dotnet run
  5. Perform DNS lookups with MiniDig

    dev

    You can perform DNS queries by passing a domain name and a record type to the dotnet run command. If no server is specified, MiniDig uses the DNS server configured for your local network adapter.

    Basic Query Syntax: dotnet run <domain> <type>

    Example: To query google.com for all available records (ANY): dotnet run google.com ANY

    dotnet run google.com ANY
  6. Specify custom DNS servers and ports in MiniDig

    dev

    Use the -s switch to override the default local DNS server with a specific IP address or a combination of IP and port.

    • Specify an IP address: dotnet run -s 8.8.8.8 google.com
    • Specify an IP and port: dotnet run -s 127.0.0.1#8600 google.com
    dotnet run -s 8.8.8.8 google.com
    dotnet run -s 127.0.0.1#8600 google.com
  7. Supported DNS resource records

    dev

    DnsClient.NET supports a wide range of DNS resource records, including:

    • Standard Records: A, AAAA, NS, CNAME, SOA, MX, TXT, PTR (for reverse lookups), SRV (service discovery).
    • Security & DNSSEC: CAA, SSHFP, TLSA, RRSIG, NSEC, NSEC3, NSEC3PARAM, DNSKEY, DS.
    • Other Records: MB, MG, MR, WKS, HINFO, MINFO, RP, AFSDB, URI, NULL, NAPTR, CERT.
    • Zone Transfers: Supports AXFR zone transfers, though LookupClient must be configured to TCP mode only for this operation.