dnsguide/chapter3.md

486 lines
12 KiB
Markdown
Raw Normal View History

2017-11-23 20:52:49 +07:00
3 - Adding more Record Types
============================
2017-11-23 20:51:20 +07:00
Let's use our program to do a lookup for ''yahoo.com''.
```rust
let qname = "www.yahoo.com";
```
Running it yields:
```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: 3,
authoritative_entries: 0,
resource_entries: 0
}
DnsQuestion {
name: "www.yahoo.com",
qtype: A
}
UNKNOWN {
domain: "www.yahoo.com",
qtype: 5,
data_len: 15,
ttl: 259
}
A {
domain: "fd-fp3.wg1.b.yahoo.com",
addr: 46.228.47.115,
ttl: 19
}
A {
domain: "fd-fp3.wg1.b.yahoo.com",
addr: 46.228.47.114,
ttl: 19
}
```
That's odd -- we're getting an UNKNOWN record as well as two A records. The
UNKNOWN record, with query type 5 is a CNAME. There are quite a few DNS record
types, many of which doesn't see any use in practice. That said, let's have
a look at a few essential ones:
| ID | Name | Description | Encoding |
| --- | ----- | -------------------------------------------------------- | ------------------------------------------------ |
| 1 | A | Alias - Mapping names to IP addresses | Preamble + Four bytes for IPv4 adress |
| 2 | NS | Name Server - The DNS server address for a domain | Preamble + Label Sequence |
| 5 | CNAME | Canonical Name - Maps names to names | Preamble + Label Sequence |
| 15 | MX | Mail eXchange - The host of the mail server for a domain | Preamble + 2-bytes for priority + Label Sequence |
| 28 | AAAA | IPv6 alias | Premable + Sixteen bytes for IPv6 adress |
### Extending QueryType with more record types
Let's go ahead and add them to our code! First we'll update our `QueryType`
enum:
```rust
2020-06-18 06:47:09 +07:00
#[derive(PartialEq, Eq, Debug, Clone, Hash, Copy)]
2017-11-23 20:51:20 +07:00
pub enum QueryType {
UNKNOWN(u16),
2020-06-18 06:47:09 +07:00
A, // 1
NS, // 2
2017-11-23 20:51:20 +07:00
CNAME, // 5
2020-06-18 06:47:09 +07:00
MX, // 15
AAAA, // 28
2017-11-23 20:51:20 +07:00
}
```
We'll also need to change our utility functions.
```rust
impl QueryType {
pub fn to_num(&self) -> u16 {
match *self {
QueryType::UNKNOWN(x) => x,
QueryType::A => 1,
QueryType::NS => 2,
QueryType::CNAME => 5,
QueryType::MX => 15,
QueryType::AAAA => 28,
}
}
pub fn from_num(num: u16) -> QueryType {
match num {
1 => QueryType::A,
2 => QueryType::NS,
5 => QueryType::CNAME,
15 => QueryType::MX,
28 => QueryType::AAAA,
2020-06-18 06:47:09 +07:00
_ => QueryType::UNKNOWN(num),
2017-11-23 20:51:20 +07:00
}
}
}
```
### Extending DnsRecord for reading new record types
Now we need a way of holding the data for these records, so we'll make some
modifications to `DnsRecord`.
```rust
2020-06-18 06:47:09 +07:00
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
2017-11-23 20:51:20 +07:00
#[allow(dead_code)]
pub enum DnsRecord {
UNKNOWN {
domain: String,
qtype: u16,
data_len: u16,
2020-06-18 06:47:09 +07:00
ttl: u32,
2017-11-23 20:51:20 +07:00
}, // 0
A {
domain: String,
addr: Ipv4Addr,
2020-06-18 06:47:09 +07:00
ttl: u32,
2017-11-23 20:51:20 +07:00
}, // 1
NS {
domain: String,
host: String,
2020-06-18 06:47:09 +07:00
ttl: u32,
2017-11-23 20:51:20 +07:00
}, // 2
CNAME {
domain: String,
host: String,
2020-06-18 06:47:09 +07:00
ttl: u32,
2017-11-23 20:51:20 +07:00
}, // 5
MX {
domain: String,
priority: u16,
host: String,
2020-06-18 06:47:09 +07:00
ttl: u32,
2017-11-23 20:51:20 +07:00
}, // 15
AAAA {
domain: String,
addr: Ipv6Addr,
2020-06-18 06:47:09 +07:00
ttl: u32,
2017-11-23 20:51:20 +07:00
}, // 28
}
```
Here comes the bulk of the work. We'll need to extend the functions for writing
and reading records. Starting with read, we amend it with additional code for
each record type. First off, we've got the common preamble:
```rust
2020-06-18 06:47:09 +07:00
impl DnsRecord {
pub fn read(buffer: &mut BytePacketBuffer) -> Result<DnsRecord> {
let mut domain = String::new();
buffer.read_qname(&mut domain)?;
let qtype_num = buffer.read_u16()?;
let qtype = QueryType::from_num(qtype_num);
let _ = buffer.read_u16()?;
let ttl = buffer.read_u32()?;
let data_len = buffer.read_u16()?;
match qtype {
QueryType::A => {
let raw_addr = buffer.read_u32()?;
let addr = Ipv4Addr::new(
((raw_addr >> 24) & 0xFF) as u8,
((raw_addr >> 16) & 0xFF) as u8,
((raw_addr >> 8) & 0xFF) as u8,
((raw_addr >> 0) & 0xFF) as u8,
);
Ok(DnsRecord::A {
domain: domain,
addr: addr,
ttl: ttl,
})
}
QueryType::AAAA => {
let raw_addr1 = buffer.read_u32()?;
let raw_addr2 = buffer.read_u32()?;
let raw_addr3 = buffer.read_u32()?;
let raw_addr4 = buffer.read_u32()?;
let addr = Ipv6Addr::new(
((raw_addr1 >> 16) & 0xFFFF) as u16,
((raw_addr1 >> 0) & 0xFFFF) as u16,
((raw_addr2 >> 16) & 0xFFFF) as u16,
((raw_addr2 >> 0) & 0xFFFF) as u16,
((raw_addr3 >> 16) & 0xFFFF) as u16,
((raw_addr3 >> 0) & 0xFFFF) as u16,
((raw_addr4 >> 16) & 0xFFFF) as u16,
((raw_addr4 >> 0) & 0xFFFF) as u16,
);
Ok(DnsRecord::AAAA {
domain: domain,
addr: addr,
ttl: ttl,
})
}
QueryType::NS => {
let mut ns = String::new();
buffer.read_qname(&mut ns)?;
Ok(DnsRecord::NS {
domain: domain,
host: ns,
ttl: ttl,
})
}
QueryType::CNAME => {
let mut cname = String::new();
buffer.read_qname(&mut cname)?;
Ok(DnsRecord::CNAME {
domain: domain,
host: cname,
ttl: ttl,
})
}
QueryType::MX => {
let priority = buffer.read_u16()?;
let mut mx = String::new();
buffer.read_qname(&mut mx)?;
Ok(DnsRecord::MX {
domain: domain,
priority: priority,
host: mx,
ttl: ttl,
})
}
QueryType::UNKNOWN(_) => {
buffer.step(data_len as usize)?;
Ok(DnsRecord::UNKNOWN {
domain: domain,
qtype: qtype_num,
data_len: data_len,
ttl: ttl,
})
}
2017-11-23 20:51:20 +07:00
}
}
2020-06-18 06:47:09 +07:00
- snip -
2017-11-23 20:51:20 +07:00
}
```
2018-03-15 20:42:42 +07:00
It's a bit of a mouthful, but there are no especially complicated records in
their own right -- it's seeing them all together that makes it look a bit
unwieldy.
2017-11-23 20:51:20 +07:00
### Extending BytePacketBuffer for setting values in place
Before we move on to writing records, we'll have to add two more functions to
`BytePacketBuffer`:
```rust
impl BytePacketBuffer {
- snip -
fn set(&mut self, pos: usize, val: u8) -> Result<()> {
self.buf[pos] = val;
Ok(())
}
fn set_u16(&mut self, pos: usize, val: u16) -> Result<()> {
2020-06-18 06:47:09 +07:00
self.set(pos, (val >> 8) as u8)?;
self.set(pos + 1, (val & 0xFF) as u8)?;
2017-11-23 20:51:20 +07:00
Ok(())
}
}
```
2020-06-18 06:47:09 +07:00
When writing the labels of a record, we don't know ahead of time the number of
bytes needed, since we might end up using jumps to compress the size. We'll
solve this by writing a zero size and then going back to fill in the size
needed.
2017-11-23 20:51:20 +07:00
### Extending DnsRecord for writing new record types
Now we can amend `DnsRecord::write`. Here's our new function:
```rust
2020-06-18 06:47:09 +07:00
impl DnsRecord {
- snip -
pub fn write(&self, buffer: &mut BytePacketBuffer) -> Result<usize> {
let start_pos = buffer.pos();
match *self {
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)?;
let octets = addr.octets();
buffer.write_u8(octets[0])?;
buffer.write_u8(octets[1])?;
buffer.write_u8(octets[2])?;
buffer.write_u8(octets[3])?;
}
DnsRecord::NS {
ref domain,
ref host,
ttl,
} => {
buffer.write_qname(domain)?;
buffer.write_u16(QueryType::NS.to_num())?;
buffer.write_u16(1)?;
buffer.write_u32(ttl)?;
let pos = buffer.pos();
buffer.write_u16(0)?;
buffer.write_qname(host)?;
let size = buffer.pos() - (pos + 2);
buffer.set_u16(pos, size as u16)?;
}
DnsRecord::CNAME {
ref domain,
ref host,
ttl,
} => {
buffer.write_qname(domain)?;
buffer.write_u16(QueryType::CNAME.to_num())?;
buffer.write_u16(1)?;
buffer.write_u32(ttl)?;
let pos = buffer.pos();
buffer.write_u16(0)?;
buffer.write_qname(host)?;
let size = buffer.pos() - (pos + 2);
buffer.set_u16(pos, size as u16)?;
}
DnsRecord::MX {
ref domain,
priority,
ref host,
ttl,
} => {
buffer.write_qname(domain)?;
buffer.write_u16(QueryType::MX.to_num())?;
buffer.write_u16(1)?;
buffer.write_u32(ttl)?;
let pos = buffer.pos();
buffer.write_u16(0)?;
buffer.write_u16(priority)?;
buffer.write_qname(host)?;
let size = buffer.pos() - (pos + 2);
buffer.set_u16(pos, size as u16)?;
}
DnsRecord::AAAA {
ref domain,
ref addr,
ttl,
} => {
buffer.write_qname(domain)?;
buffer.write_u16(QueryType::AAAA.to_num())?;
buffer.write_u16(1)?;
buffer.write_u32(ttl)?;
buffer.write_u16(16)?;
for octet in &addr.segments() {
buffer.write_u16(*octet)?;
}
}
DnsRecord::UNKNOWN { .. } => {
println!("Skipping record: {:?}", self);
2017-11-23 20:51:20 +07:00
}
}
2020-06-18 06:47:09 +07:00
Ok(buffer.pos() - start_pos)
}
2017-11-23 20:51:20 +07:00
}
```
Again, quite a bit of extra code, but thankfully the last thing we've got to
do. We're still not using the write part, but it'll come in handy once we write
our server.
### Testing the new record types
Now we're ready to retry our ''yahoo.com'' query:
```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: 3,
authoritative_entries: 0,
resource_entries: 0
}
DnsQuestion {
name: "www.yahoo.com",
qtype: A
}
CNAME {
domain: "www.yahoo.com",
host: "fd-fp3.wg1.b.yahoo.com",
ttl: 3
}
A {
domain: "fd-fp3.wg1.b.yahoo.com",
addr: 46.228.47.115,
ttl: 19
}
A {
domain: "fd-fp3.wg1.b.yahoo.com",
addr: 46.228.47.114,
ttl: 19
}
```
For good measure, let's try doing an MX lookup as well:
```rust
let qname = "yahoo.com";
let qtype = QueryType::MX;
```
Which yields:
```text
- snip -
DnsQuestion {
name: "yahoo.com",
qtype: MX
}
MX {
domain: "yahoo.com",
priority: 1,
host: "mta6.am0.yahoodns.net",
ttl: 1794
}
MX {
domain: "yahoo.com",
priority: 1,
host: "mta7.am0.yahoodns.net",
ttl: 1794
}
MX {
domain: "yahoo.com",
priority: 1,
host: "mta5.am0.yahoodns.net",
ttl: 1794
}
```
2018-03-19 17:33:11 +07:00
Next up: [Chapter 4 - Baby's first DNS server](/chapter4.md)