dnsguide/chapter2.md

312 lines
7.9 KiB
Markdown
Raw Normal View History

2017-11-23 20:52:49 +07:00
2 - Building a stub resolver
============================
2016-07-13 19:35:16 +07:00
2020-06-09 05:06:43 +07:00
While it's slightly satisfying to know that we're able to successfully parse DNS
2016-07-13 19:35:16 +07:00
packets, it's not much use to just read them off disk. As our next step, we'll
use it to build a `stub resolver`, which is a DNS client that doesn't feature
any built-in support for recursive lookup and that will only work with a DNS
server that does. Later we'll implement an actual recursive resolver to lose
the need for a server.
### Extending BytePacketBuffer for writing
In order to be able to service a query, we need to be able to not just read
packets, but also write them. To do so, we'll need to extend `BytePacketBuffer`
with some additional methods:
```rust
2017-11-23 20:51:20 +07:00
impl BytePacketBuffer {
2016-07-13 19:35:16 +07:00
2017-11-23 20:51:20 +07:00
- snip -
2016-07-13 19:35:16 +07:00
2017-11-23 20:51:20 +07:00
fn write(&mut self, val: u8) -> Result<()> {
2016-07-13 19:35:16 +07:00
if self.pos >= 512 {
2020-06-18 06:47:09 +07:00
return Err("End of buffer".into());
2016-07-13 19:35:16 +07:00
}
self.buf[self.pos] = val;
self.pos += 1;
2017-11-23 20:51:20 +07:00
Ok(())
2016-07-13 19:35:16 +07:00
}
2017-11-23 20:51:20 +07:00
fn write_u8(&mut self, val: u8) -> Result<()> {
2020-06-18 06:47:09 +07:00
self.write(val)?;
2016-07-13 19:35:16 +07:00
2017-11-23 20:51:20 +07:00
Ok(())
2016-07-13 19:35:16 +07:00
}
2017-11-23 20:51:20 +07:00
fn write_u16(&mut self, val: u16) -> Result<()> {
2020-06-18 06:47:09 +07:00
self.write((val >> 8) as u8)?;
self.write((val & 0xFF) as u8)?;
2016-07-13 19:35:16 +07:00
2017-11-23 20:51:20 +07:00
Ok(())
2016-07-13 19:35:16 +07:00
}
2017-11-23 20:51:20 +07:00
fn write_u32(&mut self, val: u32) -> Result<()> {
2020-06-18 06:47:09 +07:00
self.write(((val >> 24) & 0xFF) as u8)?;
self.write(((val >> 16) & 0xFF) as u8)?;
self.write(((val >> 8) & 0xFF) as u8)?;
self.write(((val >> 0) & 0xFF) as u8)?;
2016-07-13 19:35:16 +07:00
2017-11-23 20:51:20 +07:00
Ok(())
2016-07-13 19:35:16 +07:00
}
```
We'll also need a function for writing query names in labeled form:
```rust
2017-11-23 20:51:20 +07:00
fn write_qname(&mut self, qname: &str) -> Result<()> {
2020-06-18 06:47:09 +07:00
for label in qname.split('.') {
2016-07-13 19:35:16 +07:00
let len = label.len();
if len > 0x34 {
2020-06-18 06:47:09 +07:00
return Err("Single label exceeds 63 characters of length".into());
2016-07-13 19:35:16 +07:00
}
2020-06-18 06:47:09 +07:00
self.write_u8(len as u8)?;
2016-07-13 19:35:16 +07:00
for b in label.as_bytes() {
2020-06-18 06:47:09 +07:00
self.write_u8(*b)?;
2016-07-13 19:35:16 +07:00
}
}
2020-06-18 06:47:09 +07:00
self.write_u8(0)?;
2016-07-13 19:35:16 +07:00
2017-11-23 20:51:20 +07:00
Ok(())
2016-07-13 19:35:16 +07:00
}
} // End of BytePacketBuffer
```
### Extending DnsHeader for writing
Building on our new functions we can extend our protocol representation
structs. Starting with `DnsHeader`:
```rust
2017-11-23 20:51:20 +07:00
impl DnsHeader {
2016-07-13 19:35:16 +07:00
2017-11-23 20:51:20 +07:00
- snip -
2016-07-13 19:35:16 +07:00
2017-11-23 20:51:20 +07:00
pub fn write(&self, buffer: &mut BytePacketBuffer) -> Result<()> {
2020-06-18 06:47:09 +07:00
buffer.write_u16(self.id)?;
buffer.write_u8(
(self.recursion_desired as u8)
| ((self.truncated_message as u8) << 1)
| ((self.authoritative_answer as u8) << 2)
| (self.opcode << 3)
| ((self.response as u8) << 7) as u8,
)?;
buffer.write_u8(
2020-06-18 07:23:42 +07:00
(self.rescode as u8)
2020-06-18 06:47:09 +07:00
| ((self.checking_disabled as u8) << 4)
| ((self.authed_data as u8) << 5)
| ((self.z as u8) << 6)
| ((self.recursion_available as u8) << 7),
)?;
buffer.write_u16(self.questions)?;
buffer.write_u16(self.answers)?;
buffer.write_u16(self.authoritative_entries)?;
buffer.write_u16(self.resource_entries)?;
2016-07-13 19:35:16 +07:00
2017-11-23 20:51:20 +07:00
Ok(())
2016-07-13 19:35:16 +07:00
}
}
```
### Extending DnsQuestion for writing
Moving on to `DnsQuestion`:
```rust
2017-11-23 20:51:20 +07:00
impl DnsQuestion {
2016-07-13 19:35:16 +07:00
2017-11-23 20:51:20 +07:00
- snip -
2016-07-13 19:35:16 +07:00
2017-11-23 20:51:20 +07:00
pub fn write(&self, buffer: &mut BytePacketBuffer) -> Result<()> {
2020-06-18 06:47:09 +07:00
buffer.write_qname(&self.name)?;
2016-07-13 19:35:16 +07:00
let typenum = self.qtype.to_num();
2020-06-18 06:47:09 +07:00
buffer.write_u16(typenum)?;
buffer.write_u16(1)?;
2016-07-13 19:35:16 +07:00
2017-11-23 20:51:20 +07:00
Ok(())
2016-07-13 19:35:16 +07:00
}
}
```
### Extending DnsRecord for writing
`DnsRecord` is for now quite compact as well, although we'll eventually add
quite a bit of code here to handle different record types:
```rust
2017-11-23 20:51:20 +07:00
impl DnsRecord {
- snip -
2016-07-13 19:35:16 +07:00
2017-11-23 20:51:20 +07:00
pub fn write(&self, buffer: &mut BytePacketBuffer) -> Result<usize> {
2016-07-13 19:35:16 +07:00
let start_pos = buffer.pos();
match *self {
2020-06-18 06:47:09 +07:00
DnsRecord::A {
ref domain,
ref addr,
ttl,
} => {
buffer.write_qname(domain)?;
buffer.write_u16(QueryType::A.to_num())?;
buffer.write_u16(1)?;
buffer.write_u32(ttl)?;
buffer.write_u16(4)?;
2016-07-13 19:35:16 +07:00
let octets = addr.octets();
2020-06-18 06:47:09 +07:00
buffer.write_u8(octets[0])?;
buffer.write_u8(octets[1])?;
buffer.write_u8(octets[2])?;
buffer.write_u8(octets[3])?;
}
2016-07-13 19:35:16 +07:00
DnsRecord::UNKNOWN { .. } => {
println!("Skipping record: {:?}", self);
}
}
Ok(buffer.pos() - start_pos)
}
}
```
### Extending DnsPacket for writing
2017-11-23 20:51:20 +07:00
Putting it all together in `DnsPacket`:
2016-07-13 19:35:16 +07:00
```rust
2017-11-23 20:51:20 +07:00
impl DnsPacket {
2016-07-13 19:35:16 +07:00
2017-11-23 20:51:20 +07:00
- snip -
2016-07-13 19:35:16 +07:00
2020-06-18 06:47:09 +07:00
pub fn write(&mut self, buffer: &mut BytePacketBuffer) -> Result<()> {
2017-11-23 20:51:20 +07:00
self.header.questions = self.questions.len() as u16;
self.header.answers = self.answers.len() as u16;
self.header.authoritative_entries = self.authorities.len() as u16;
self.header.resource_entries = self.resources.len() as u16;
2016-07-13 19:35:16 +07:00
2020-06-18 06:47:09 +07:00
self.header.write(buffer)?;
2016-07-13 19:35:16 +07:00
for question in &self.questions {
2020-06-18 06:47:09 +07:00
question.write(buffer)?;
2016-07-13 19:35:16 +07:00
}
for rec in &self.answers {
2020-06-18 06:47:09 +07:00
rec.write(buffer)?;
2016-07-13 19:35:16 +07:00
}
for rec in &self.authorities {
2020-06-18 06:47:09 +07:00
rec.write(buffer)?;
2016-07-13 19:35:16 +07:00
}
for rec in &self.resources {
2020-06-18 06:47:09 +07:00
rec.write(buffer)?;
2016-07-13 19:35:16 +07:00
}
2017-11-23 20:51:20 +07:00
Ok(())
2016-07-13 19:35:16 +07:00
}
}
```
### Implementing a stub resolver
We're ready to implement our stub resolver. Rust includes a convenient
2018-03-15 20:42:42 +07:00
`UDPSocket` which does most of the work.
2016-07-13 19:35:16 +07:00
```rust
2020-06-18 06:47:09 +07:00
fn main() -> Result<()> {
2016-07-13 19:35:16 +07:00
// Perform an A query for google.com
let qname = "google.com";
let qtype = QueryType::A;
// Using googles public DNS server
let server = ("8.8.8.8", 53);
// Bind a UDP socket to an arbitrary port
2020-06-18 06:47:09 +07:00
let socket = UdpSocket::bind(("0.0.0.0", 43210))?;
2017-11-23 20:51:20 +07:00
2018-03-15 20:20:10 +07:00
// Build our query packet. It's important that we remember to set the
// `recursion_desired` flag. As noted earlier, the packet id is arbitrary.
2016-07-13 19:35:16 +07:00
let mut packet = DnsPacket::new();
packet.header.id = 6666;
packet.header.questions = 1;
packet.header.recursion_desired = true;
2020-06-18 06:47:09 +07:00
packet
.questions
.push(DnsQuestion::new(qname.to_string(), qtype));
2016-07-13 19:35:16 +07:00
2018-03-15 20:20:10 +07:00
// Use our new write method to write the packet to a buffer...
let mut req_buffer = BytePacketBuffer::new();
2020-06-18 06:47:09 +07:00
packet.write(&mut req_buffer)?;
2016-07-13 19:35:16 +07:00
2018-03-15 20:20:10 +07:00
// ...and send it off to the server using our socket:
2020-06-18 06:47:09 +07:00
socket.send_to(&req_buffer.buf[0..req_buffer.pos], server)?;
2016-07-13 19:35:16 +07:00
2018-03-15 20:20:10 +07:00
// To prepare for receiving the response, we'll create a new `BytePacketBuffer`,
// and ask the socket to write the response directly into our buffer.
2016-07-13 19:35:16 +07:00
let mut res_buffer = BytePacketBuffer::new();
2020-06-18 06:47:09 +07:00
socket.recv_from(&mut res_buffer.buf)?;
2016-07-13 19:35:16 +07:00
2018-03-15 20:20:10 +07:00
// As per the previous section, `DnsPacket::from_buffer()` is then used to
// actually parse the packet after which we can print the response.
2020-06-18 06:47:09 +07:00
let res_packet = DnsPacket::from_buffer(&mut res_buffer)?;
println!("{:#?}", res_packet.header);
2016-07-13 19:35:16 +07:00
for q in res_packet.questions {
2020-06-18 06:47:09 +07:00
println!("{:#?}", q);
2016-07-13 19:35:16 +07:00
}
for rec in res_packet.answers {
2020-06-18 06:47:09 +07:00
println!("{:#?}", rec);
2016-07-13 19:35:16 +07:00
}
for rec in res_packet.authorities {
2020-06-18 06:47:09 +07:00
println!("{:#?}", rec);
2016-07-13 19:35:16 +07:00
}
for rec in res_packet.resources {
2020-06-18 06:47:09 +07:00
println!("{:#?}", rec);
2016-07-13 19:35:16 +07:00
}
2020-06-18 06:47:09 +07:00
Ok(())
2016-07-13 19:35:16 +07:00
}
```
Running it will print:
```text
DnsHeader {
id: 6666,
recursion_desired: true,
truncated_message: false,
authoritative_answer: false,
opcode: 0,
response: true,
rescode: NOERROR,
checking_disabled: false,
authed_data: false,
z: false,
recursion_available: true,
questions: 1,
answers: 1,
authoritative_entries: 0,
resource_entries: 0
}
DnsQuestion {
name: "google.com",
qtype: A
}
A {
domain: "google.com",
addr: 216.58.209.110,
ttl: 79
}
```
2018-03-19 17:33:11 +07:00
The next chapter covers implementing a richer set of record types: [Chapter 3 - Adding more Record Types](/chapter3.md)