liveBook · Manning

DNS resolution

As explained in Rust in Action: Systems programming concepts and techniques

Domain name resolution is the process of retrieving network resource records for a domain name from the Domain Name System.

A hostname is useful to people, but a TCP connection needs an IP address. DNS resolution supplies that address by asking a DNS server; that server may ask other servers in turn. DNS requests can travel over TCP, including with TLS, or over UDP. section 8.4.2

A small `resolve` command-line program makes the lookup concrete:

  • Read a domain name and a DNS server address. The `-s` option lets you choose a server instead of the default. section 8.4.2
  • Build a DNS query for an A record—the domain’s IPv4 address—and request recursion. section 8.4.2
  • Use `trust-dns` for protocol handling and a UDP socket for the exchange, then report the resolved address. section 8.4.2
Listing 8.7 Constructing a DNS message in Rust
35 let mut msg = Message::new();                      #1
36 msg
37   .set_id(rand::random::<u16>())                   #2
38   .set_message_type(MessageType::Query)
39   .add_query(                                      #3
40       Query::query(domain_name, RecordType::A)     #4
41   )
42   .set_op_code(OpCode::Query)
43   .set_recursion_desired(true);                    #5

The query-building code shows the message ID, A-record request, and recursion flag without hiding them behind a one-call lookup. section 8.4.2

Resolve a hostname

Run `resolve www.rustinaction.com` to obtain an IPv4 address; the illustrated result is `35.185.44.232`. The particular address is an example, not a value to put into the program. section 8.4.2

The same job appears as `dns.rs` in a larger HTTP client: resolve the domain first, then use the IP address for the network request. The resolver deliberately handles only the portion of DNS needed for this task; `trust-dns` does the harder protocol work. section 8.10 section 8.4.2

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Works through constructing an IPv4 DNS query and a small resolver, while delegating broader protocol handling to `trust-dns`.
Examples
Uses a command-line hostname lookup, an A-record query, and a separate DNS module in an HTTP client.
What it assumes
Starts with Cargo project setup and command-line arguments, but relies on `trust-dns` rather than teaching the full DNS protocol.
What you can do after
Build it from scratch as a command-line resolver using `trust-dns`, or apply the lookup in an HTTP client.
How it connects
Connects domain names and IP addresses to TCP, UDP, DNS queries, and HTTP requests.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.