Crate rustdns[][src]

Expand description

rustdns

rustdns is a simple, fast, and fully fledged DNS library for interacting with domain name services at a high or low level.

Features

Usage (low-level library)

use rustdns::Message;
use rustdns::types::*;
use std::net::UdpSocket;
use std::time::Duration;

fn udp_example() -> std::io::Result<()> {
    // A DNS Message can be easily constructed
    let mut m = Message::default();
    m.add_question("bramp.net", Type::A, Class::Internet);
    m.add_extension(Extension {   // Optionally add a EDNS extension
        payload_size: 4096,       // which supports a larger payload size.
        ..Default::default()
    });

    // Setup a UDP socket for sending to a DNS server.
    let socket = UdpSocket::bind("0.0.0.0:0")?;
    socket.set_read_timeout(Some(Duration::new(5, 0)))?;
    socket.connect("8.8.8.8:53")?; // Google's Public DNS Servers

    // Encode the DNS Message as a Vec<u8>.
    let question = m.to_vec()?;

    // Send to the server.
    socket.send(&question)?;

    // Wait for a response from the DNS server.
    let mut resp = [0; 4096];
    let len = socket.recv(&mut resp)?;

    // Take the response bytes and turn it into another DNS Message.
    let answer = Message::from_slice(&resp[0..len])?;

    // Now do something with `answer`, in this case print it!
    println!("DNS Response:\n{}", answer);

    Ok(())
}

If successful something like the following will be printed:

;; ->>HEADER<<- opcode: Query, status: NoError, id: 44857
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
; bramp.net.              IN   A

; ANSWER SECTION:
bramp.net.            299 IN   A      104.21.62.200
bramp.net.            299 IN   A      172.67.138.196

Features

The following optional features are available:

  • clients: Enables the following clients:
    • doh: DNS over HTTPS (DoH) client (rfc8484).
    • json: DNS over HTTPS JSON client
    • tcp: Enables the DNS over TCP client
    • udp: Enables the DNS over UDP client
  • zones: Enable a Zone File Parser

Usage (cli)

To use the demo CLI:

$ cargo run -p dig -- A www.google.com
...
;; ->>HEADER<<- opcode: Query, status: NoError, id: 34327
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
; www.google.com.         IN   A

; ANSWER SECTION:
www.google.com.       110 IN   A      142.250.72.196

$ cargo run -p dig -- AAAA www.google.com
$ cargo run -p dig -- ANY www.google.com
$ cargo run -p dig -- CNAME code.google.com
$ cargo run -p dig -- MX google.com
$ cargo run -p dig -- PTR 4.4.8.8.in-addr.arpa
$ cargo run -p dig -- SOA google.com
$ cargo run -p dig -- SRV _ldap._tcp.google.com
$ cargo run -p dig -- TXT google.com

Testing

$ cargo test --all

$ cargo watch -- cargo test --all -- --nocapture

The test suite is full of stored real life examples, from querying real DNS records. This was generated with cargo run -p generate_tests.

Fuzzing

The library has been extensively fuzzed. Try for yourself:

$ cargo fuzz run from_slice

Test Data

To aid in testing features, I have a set of pre-configured records setup:

DomainDescription
a.bramp.netSingle A record pointing at 127.0.0.1
aaaa.bramp.netSingle AAAA record pointing at ::1
aaaaa.bramp.netOne A record, and one AAAA record resolving to 127.0.0.1 and ::1
cname.bramp.netSingle CNAME record pointing at a.bramp.net
cname-loop1.bramp.netSingle CNAME record pointing at cname-loop2.bramp.net
cname-loop2.bramp.netSingle CNAME record pointing at cname-loop1.bramp.net
mx.bramp.netSingle MX record pointing at a.bramp.net
ns.bramp.netSingle NS record pointing at a.bramp.net
txt.bramp.netSingle TXT Record “A TXT record!”

Releasing

$ cargo test-all-features
$ cargo readme > README.md
$ cargo publish --dry-run
$ cargo publish

TODO (in order of priority)

  • Document UDP/TCP library
  • Client side examples
  • Server side examples
  • DNSSEC: Signing, validating and key generation for DSA, RSA, ECDSA and Ed25519
  • NSID, Cookies, AXFR/IXFR, TSIG, SIG(0)
  • Runtime-independence
  • Change the API to have getters and setters.
  • Change hyper-alpn to support tokio-native-tls for people that want that.
  • Implement more dig features, such as +trace
  • Maybe convert the binary parsing to Nom format.
  • Can I parse these https://www.iana.org/domains/root/files ?

Reference

Modules

Macros

Handy macro for returning a formatted std::io::Error message. TODO Delete

Structs

EDNS(0) extension record as defined in rfc2671 and rfc6891.

Mail EXchanger (MX) record specifies the mail server responsible for accepting email messages on behalf of a domain name.

DNS Message that serves as the root of all DNS requests and responses.

Question struct containing a domain name, question Type and question Class.

Resource Record (RR) returned by DNS servers containing a answer to the question.

Start of Authority (SOA) record containing administrative information about the zone. See rfc1035.

Service (SRV) record, containg hostname and port number information of specified services. See rfc2782.

Stats related to the specific query, optionally filed in by the client and does not change the query behaviour.

Text (TXT) record for arbitrary human-readable text in a DNS record.

Enums

Resource Record Class, for example Internet.

Specifies kind of query in this message. See rfc1035, rfc6895 and DNS Parameters.

Query or Response bit.

Response Codes. See rfc1035 and DNS Parameters.

Recource Record Definitions.

Resource Record Type, for example, A, CNAME or SOA.

Type Definitions

IPv4 Address (A) record.

IPv6 Address (AAAA) record.

Canonical name (CNAME) record, for aliasing one name to another.

Name Server (NS) record for delegating a the given authoritative name servers.

Pointer (PTR) record most commonly used for most common use is for implementing reverse DNS lookups.