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
