dnsguide/examples/sample3.rs

722 lines
20 KiB
Rust
Raw Normal View History

2017-11-23 20:51:20 +07:00
use std::net::UdpSocket;
2020-06-18 05:45:03 +07:00
use std::net::{Ipv4Addr, Ipv6Addr};
type Error = Box<dyn std::error::Error>;
type Result<T> = std::result::Result<T, Error>;
2017-11-23 20:51:20 +07:00
pub struct BytePacketBuffer {
pub buf: [u8; 512],
2020-06-18 05:45:03 +07:00
pub pos: usize,
2017-11-23 20:51:20 +07:00
}
impl BytePacketBuffer {
pub fn new() -> BytePacketBuffer {
BytePacketBuffer {
buf: [0; 512],
2020-06-18 05:45:03 +07:00
pos: 0,
2017-11-23 20:51:20 +07:00
}
}
fn pos(&self) -> usize {
self.pos
}
fn step(&mut self, steps: usize) -> Result<()> {
self.pos += steps;
Ok(())
}
fn seek(&mut self, pos: usize) -> Result<()> {
self.pos = pos;
Ok(())
}
fn read(&mut self) -> Result<u8> {
if self.pos >= 512 {
2020-06-18 05:45:03 +07:00
return Err("End of buffer".into());
2017-11-23 20:51:20 +07:00
}
let res = self.buf[self.pos];
self.pos += 1;
Ok(res)
}
fn get(&mut self, pos: usize) -> Result<u8> {
if pos >= 512 {
2020-06-18 05:45:03 +07:00
return Err("End of buffer".into());
2017-11-23 20:51:20 +07:00
}
Ok(self.buf[pos])
}
fn get_range(&mut self, start: usize, len: usize) -> Result<&[u8]> {
if start + len >= 512 {
2020-06-18 05:45:03 +07:00
return Err("End of buffer".into());
2017-11-23 20:51:20 +07:00
}
2020-06-18 05:45:03 +07:00
Ok(&self.buf[start..start + len as usize])
2017-11-23 20:51:20 +07:00
}
2020-06-18 05:45:03 +07:00
fn read_u16(&mut self) -> Result<u16> {
let res = ((self.read()? as u16) << 8) | (self.read()? as u16);
2017-11-23 20:51:20 +07:00
Ok(res)
}
2020-06-18 05:45:03 +07:00
fn read_u32(&mut self) -> Result<u32> {
let res = ((self.read()? as u32) << 24)
| ((self.read()? as u32) << 16)
| ((self.read()? as u32) << 8)
| ((self.read()? as u32) << 0);
2017-11-23 20:51:20 +07:00
Ok(res)
}
2020-06-18 05:45:03 +07:00
fn read_qname(&mut self, outstr: &mut String) -> Result<()> {
2017-11-23 20:51:20 +07:00
let mut pos = self.pos();
let mut jumped = false;
let mut delim = "";
2020-06-18 05:45:03 +07:00
let max_jumps = 5;
let mut jumps_performed = 0;
2017-11-23 20:51:20 +07:00
loop {
2020-06-18 05:45:03 +07:00
// Dns Packets are untrusted data, so we need to be paranoid. Someone
// can craft a packet with a cycle in the jump instructions. This guards
// against such packets.
if jumps_performed > max_jumps {
return Err(format!("Limit of {} jumps exceeded", max_jumps).into());
}
let len = self.get(pos)?;
2017-11-23 20:51:20 +07:00
// A two byte sequence, where the two highest bits of the first byte is
// set, represents a offset relative to the start of the buffer. We
// handle this by jumping to the offset, setting a flag to indicate
// that we shouldn't update the shared buffer position once done.
2018-03-19 17:37:16 +07:00
if (len & 0xC0) == 0xC0 {
2017-11-23 20:51:20 +07:00
// When a jump is performed, we only modify the shared buffer
// position once, and avoid making the change later on.
if !jumped {
2020-06-18 05:45:03 +07:00
self.seek(pos + 2)?;
2017-11-23 20:51:20 +07:00
}
2020-06-18 05:45:03 +07:00
let b2 = self.get(pos + 1)? as u16;
2017-11-23 20:51:20 +07:00
let offset = (((len as u16) ^ 0xC0) << 8) | b2;
pos = offset as usize;
jumped = true;
2020-06-18 05:45:03 +07:00
jumps_performed += 1;
2017-11-23 20:51:20 +07:00
continue;
}
pos += 1;
// Names are terminated by an empty label of length 0
if len == 0 {
break;
}
outstr.push_str(delim);
2020-06-18 05:45:03 +07:00
let str_buffer = self.get_range(pos, len as usize)?;
2017-11-23 20:51:20 +07:00
outstr.push_str(&String::from_utf8_lossy(str_buffer).to_lowercase());
delim = ".";
pos += len as usize;
}
if !jumped {
2020-06-18 05:45:03 +07:00
self.seek(pos)?;
2017-11-23 20:51:20 +07:00
}
Ok(())
}
fn write(&mut self, val: u8) -> Result<()> {
if self.pos >= 512 {
2020-06-18 05:45:03 +07:00
return Err("End of buffer".into());
2017-11-23 20:51:20 +07:00
}
self.buf[self.pos] = val;
self.pos += 1;
Ok(())
}
fn write_u8(&mut self, val: u8) -> Result<()> {
2020-06-18 05:45:03 +07:00
self.write(val)?;
2017-11-23 20:51:20 +07:00
Ok(())
}
fn write_u16(&mut self, val: u16) -> Result<()> {
2020-06-18 05:45:03 +07:00
self.write((val >> 8) as u8)?;
self.write((val & 0xFF) as u8)?;
2017-11-23 20:51:20 +07:00
Ok(())
}
fn write_u32(&mut self, val: u32) -> Result<()> {
2020-06-18 05:45:03 +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)?;
2017-11-23 20:51:20 +07:00
Ok(())
}
fn write_qname(&mut self, qname: &str) -> Result<()> {
2020-06-18 06:47:09 +07:00
for label in qname.split('.') {
2017-11-23 20:51:20 +07:00
let len = label.len();
if len > 0x34 {
2020-06-18 05:45:03 +07:00
return Err("Single label exceeds 63 characters of length".into());
2017-11-23 20:51:20 +07:00
}
2020-06-18 05:45:03 +07:00
self.write_u8(len as u8)?;
2017-11-23 20:51:20 +07:00
for b in label.as_bytes() {
2020-06-18 05:45:03 +07:00
self.write_u8(*b)?;
2017-11-23 20:51:20 +07:00
}
}
2020-06-18 05:45:03 +07:00
self.write_u8(0)?;
2017-11-23 20:51:20 +07:00
Ok(())
}
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 05:45:03 +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 05:45:03 +07:00
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2017-11-23 20:51:20 +07:00
pub enum ResultCode {
NOERROR = 0,
FORMERR = 1,
SERVFAIL = 2,
NXDOMAIN = 3,
NOTIMP = 4,
2020-06-18 05:45:03 +07:00
REFUSED = 5,
2017-11-23 20:51:20 +07:00
}
impl ResultCode {
pub fn from_num(num: u8) -> ResultCode {
match num {
1 => ResultCode::FORMERR,
2 => ResultCode::SERVFAIL,
3 => ResultCode::NXDOMAIN,
4 => ResultCode::NOTIMP,
5 => ResultCode::REFUSED,
2020-06-18 05:45:03 +07:00
0 | _ => ResultCode::NOERROR,
2017-11-23 20:51:20 +07:00
}
}
}
2020-06-18 05:45:03 +07:00
#[derive(Clone, Debug)]
2017-11-23 20:51:20 +07:00
pub struct DnsHeader {
pub id: u16, // 16 bits
2020-06-18 05:45:03 +07:00
pub recursion_desired: bool, // 1 bit
pub truncated_message: bool, // 1 bit
2017-11-23 20:51:20 +07:00
pub authoritative_answer: bool, // 1 bit
2020-06-18 05:45:03 +07:00
pub opcode: u8, // 4 bits
pub response: bool, // 1 bit
2017-11-23 20:51:20 +07:00
2020-06-18 05:45:03 +07:00
pub rescode: ResultCode, // 4 bits
pub checking_disabled: bool, // 1 bit
pub authed_data: bool, // 1 bit
pub z: bool, // 1 bit
2017-11-23 20:51:20 +07:00
pub recursion_available: bool, // 1 bit
2020-06-18 05:45:03 +07:00
pub questions: u16, // 16 bits
pub answers: u16, // 16 bits
2017-11-23 20:51:20 +07:00
pub authoritative_entries: u16, // 16 bits
2020-06-18 05:45:03 +07:00
pub resource_entries: u16, // 16 bits
2017-11-23 20:51:20 +07:00
}
impl DnsHeader {
pub fn new() -> DnsHeader {
2020-06-18 05:45:03 +07:00
DnsHeader {
id: 0,
recursion_desired: false,
truncated_message: false,
authoritative_answer: false,
opcode: 0,
response: false,
rescode: ResultCode::NOERROR,
checking_disabled: false,
authed_data: false,
z: false,
recursion_available: false,
questions: 0,
answers: 0,
authoritative_entries: 0,
resource_entries: 0,
}
2017-11-23 20:51:20 +07:00
}
pub fn read(&mut self, buffer: &mut BytePacketBuffer) -> Result<()> {
2020-06-18 05:45:03 +07:00
self.id = buffer.read_u16()?;
2017-11-23 20:51:20 +07:00
2020-06-18 05:45:03 +07:00
let flags = buffer.read_u16()?;
2017-11-23 20:51:20 +07:00
let a = (flags >> 8) as u8;
let b = (flags & 0xFF) as u8;
self.recursion_desired = (a & (1 << 0)) > 0;
self.truncated_message = (a & (1 << 1)) > 0;
self.authoritative_answer = (a & (1 << 2)) > 0;
self.opcode = (a >> 3) & 0x0F;
self.response = (a & (1 << 7)) > 0;
self.rescode = ResultCode::from_num(b & 0x0F);
self.checking_disabled = (b & (1 << 4)) > 0;
self.authed_data = (b & (1 << 5)) > 0;
self.z = (b & (1 << 6)) > 0;
self.recursion_available = (b & (1 << 7)) > 0;
2020-06-18 05:45:03 +07:00
self.questions = buffer.read_u16()?;
self.answers = buffer.read_u16()?;
self.authoritative_entries = buffer.read_u16()?;
self.resource_entries = buffer.read_u16()?;
2017-11-23 20:51:20 +07:00
// Return the constant header size
Ok(())
}
pub fn write(&self, buffer: &mut BytePacketBuffer) -> Result<()> {
2020-06-18 05:45:03 +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 05:45:03 +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)?;
2017-11-23 20:51:20 +07:00
Ok(())
}
}
2020-06-18 05:45:03 +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 05:45:03 +07:00
A, // 1
NS, // 2
2017-11-23 20:51:20 +07:00
CNAME, // 5
2020-06-18 05:45:03 +07:00
MX, // 15
AAAA, // 28
2017-11-23 20:51:20 +07:00
}
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 05:45:03 +07:00
_ => QueryType::UNKNOWN(num),
2017-11-23 20:51:20 +07:00
}
}
}
2020-06-18 05:45:03 +07:00
#[derive(Debug, Clone, PartialEq, Eq)]
2017-11-23 20:51:20 +07:00
pub struct DnsQuestion {
pub name: String,
2020-06-18 05:45:03 +07:00
pub qtype: QueryType,
2017-11-23 20:51:20 +07:00
}
impl DnsQuestion {
pub fn new(name: String, qtype: QueryType) -> DnsQuestion {
2020-11-28 20:29:04 +07:00
DnsQuestion { name, qtype }
2017-11-23 20:51:20 +07:00
}
pub fn read(&mut self, buffer: &mut BytePacketBuffer) -> Result<()> {
2020-06-18 05:45:03 +07:00
buffer.read_qname(&mut self.name)?;
self.qtype = QueryType::from_num(buffer.read_u16()?); // qtype
let _ = buffer.read_u16()?; // class
2017-11-23 20:51:20 +07:00
Ok(())
}
pub fn write(&self, buffer: &mut BytePacketBuffer) -> Result<()> {
2020-06-18 05:45:03 +07:00
buffer.write_qname(&self.name)?;
2017-11-23 20:51:20 +07:00
let typenum = self.qtype.to_num();
2020-06-18 05:45:03 +07:00
buffer.write_u16(typenum)?;
buffer.write_u16(1)?;
2017-11-23 20:51:20 +07:00
Ok(())
}
}
2020-06-18 05:45:03 +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 05:45:03 +07:00
ttl: u32,
2017-11-23 20:51:20 +07:00
}, // 0
A {
domain: String,
addr: Ipv4Addr,
2020-06-18 05:45:03 +07:00
ttl: u32,
2017-11-23 20:51:20 +07:00
}, // 1
NS {
domain: String,
host: String,
2020-06-18 05:45:03 +07:00
ttl: u32,
2017-11-23 20:51:20 +07:00
}, // 2
CNAME {
domain: String,
host: String,
2020-06-18 05:45:03 +07:00
ttl: u32,
2017-11-23 20:51:20 +07:00
}, // 5
MX {
domain: String,
priority: u16,
host: String,
2020-06-18 05:45:03 +07:00
ttl: u32,
2017-11-23 20:51:20 +07:00
}, // 15
AAAA {
domain: String,
addr: Ipv6Addr,
2020-06-18 05:45:03 +07:00
ttl: u32,
2017-11-23 20:51:20 +07:00
}, // 28
}
impl DnsRecord {
pub fn read(buffer: &mut BytePacketBuffer) -> Result<DnsRecord> {
let mut domain = String::new();
2020-06-18 05:45:03 +07:00
buffer.read_qname(&mut domain)?;
2017-11-23 20:51:20 +07:00
2020-06-18 05:45:03 +07:00
let qtype_num = buffer.read_u16()?;
2017-11-23 20:51:20 +07:00
let qtype = QueryType::from_num(qtype_num);
2020-06-18 05:45:03 +07:00
let _ = buffer.read_u16()?;
let ttl = buffer.read_u32()?;
let data_len = buffer.read_u16()?;
2017-11-23 20:51:20 +07:00
match qtype {
2020-06-18 05:45:03 +07:00
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,
);
2017-11-23 20:51:20 +07:00
2020-11-28 20:29:04 +07:00
Ok(DnsRecord::A { domain, addr, ttl })
2020-06-18 05:45:03 +07:00
}
2017-11-23 20:51:20 +07:00
QueryType::AAAA => {
2020-06-18 05:45:03 +07:00
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,
);
2017-11-23 20:51:20 +07:00
2020-11-28 20:29:04 +07:00
Ok(DnsRecord::AAAA { domain, addr, ttl })
2020-06-18 05:45:03 +07:00
}
2017-11-23 20:51:20 +07:00
QueryType::NS => {
let mut ns = String::new();
2020-06-18 05:45:03 +07:00
buffer.read_qname(&mut ns)?;
2017-11-23 20:51:20 +07:00
Ok(DnsRecord::NS {
2020-11-28 20:27:23 +07:00
domain,
2017-11-23 20:51:20 +07:00
host: ns,
2020-11-28 20:27:23 +07:00
ttl,
2017-11-23 20:51:20 +07:00
})
2020-06-18 05:45:03 +07:00
}
2017-11-23 20:51:20 +07:00
QueryType::CNAME => {
let mut cname = String::new();
2020-06-18 05:45:03 +07:00
buffer.read_qname(&mut cname)?;
2017-11-23 20:51:20 +07:00
Ok(DnsRecord::CNAME {
2020-11-28 20:27:23 +07:00
domain,
2017-11-23 20:51:20 +07:00
host: cname,
2020-11-28 20:27:23 +07:00
ttl,
2017-11-23 20:51:20 +07:00
})
2020-06-18 05:45:03 +07:00
}
2017-11-23 20:51:20 +07:00
QueryType::MX => {
2020-06-18 05:45:03 +07:00
let priority = buffer.read_u16()?;
2017-11-23 20:51:20 +07:00
let mut mx = String::new();
2020-06-18 05:45:03 +07:00
buffer.read_qname(&mut mx)?;
2017-11-23 20:51:20 +07:00
Ok(DnsRecord::MX {
2020-11-28 20:27:23 +07:00
domain,
priority,
2017-11-23 20:51:20 +07:00
host: mx,
2020-11-28 20:27:23 +07:00
ttl,
2017-11-23 20:51:20 +07:00
})
2020-06-18 05:45:03 +07:00
}
2017-11-23 20:51:20 +07:00
QueryType::UNKNOWN(_) => {
2020-06-18 05:45:03 +07:00
buffer.step(data_len as usize)?;
2017-11-23 20:51:20 +07:00
Ok(DnsRecord::UNKNOWN {
2020-11-28 20:27:23 +07:00
domain,
2017-11-23 20:51:20 +07:00
qtype: qtype_num,
2020-11-28 20:27:23 +07:00
data_len,
ttl,
2017-11-23 20:51:20 +07:00
})
}
}
}
pub fn write(&self, buffer: &mut BytePacketBuffer) -> Result<usize> {
let start_pos = buffer.pos();
match *self {
2020-06-18 05:45:03 +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)?;
2017-11-23 20:51:20 +07:00
let octets = addr.octets();
2020-06-18 05:45:03 +07:00
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)?;
2017-11-23 20:51:20 +07:00
let pos = buffer.pos();
2020-06-18 05:45:03 +07:00
buffer.write_u16(0)?;
2017-11-23 20:51:20 +07:00
2020-06-18 05:45:03 +07:00
buffer.write_qname(host)?;
2017-11-23 20:51:20 +07:00
let size = buffer.pos() - (pos + 2);
2020-06-18 05:45:03 +07:00
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)?;
2017-11-23 20:51:20 +07:00
let pos = buffer.pos();
2020-06-18 05:45:03 +07:00
buffer.write_u16(0)?;
2017-11-23 20:51:20 +07:00
2020-06-18 05:45:03 +07:00
buffer.write_qname(host)?;
2017-11-23 20:51:20 +07:00
let size = buffer.pos() - (pos + 2);
2020-06-18 05:45:03 +07:00
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)?;
2017-11-23 20:51:20 +07:00
let pos = buffer.pos();
2020-06-18 05:45:03 +07:00
buffer.write_u16(0)?;
2017-11-23 20:51:20 +07:00
2020-06-18 05:45:03 +07:00
buffer.write_u16(priority)?;
buffer.write_qname(host)?;
2017-11-23 20:51:20 +07:00
let size = buffer.pos() - (pos + 2);
2020-06-18 05:45:03 +07:00
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)?;
2017-11-23 20:51:20 +07:00
for octet in &addr.segments() {
2020-06-18 05:45:03 +07:00
buffer.write_u16(*octet)?;
2017-11-23 20:51:20 +07:00
}
2020-06-18 05:45:03 +07:00
}
2017-11-23 20:51:20 +07:00
DnsRecord::UNKNOWN { .. } => {
println!("Skipping record: {:?}", self);
}
}
Ok(buffer.pos() - start_pos)
}
}
#[derive(Clone, Debug)]
pub struct DnsPacket {
pub header: DnsHeader,
pub questions: Vec<DnsQuestion>,
pub answers: Vec<DnsRecord>,
pub authorities: Vec<DnsRecord>,
2020-06-18 05:45:03 +07:00
pub resources: Vec<DnsRecord>,
2017-11-23 20:51:20 +07:00
}
impl DnsPacket {
pub fn new() -> DnsPacket {
DnsPacket {
header: DnsHeader::new(),
questions: Vec::new(),
answers: Vec::new(),
authorities: Vec::new(),
2020-06-18 05:45:03 +07:00
resources: Vec::new(),
2017-11-23 20:51:20 +07:00
}
}
pub fn from_buffer(buffer: &mut BytePacketBuffer) -> Result<DnsPacket> {
let mut result = DnsPacket::new();
2020-06-18 05:45:03 +07:00
result.header.read(buffer)?;
2017-11-23 20:51:20 +07:00
for _ in 0..result.header.questions {
2020-06-18 05:45:03 +07:00
let mut question = DnsQuestion::new("".to_string(), QueryType::UNKNOWN(0));
question.read(buffer)?;
2017-11-23 20:51:20 +07:00
result.questions.push(question);
}
for _ in 0..result.header.answers {
2020-06-18 05:45:03 +07:00
let rec = DnsRecord::read(buffer)?;
2017-11-23 20:51:20 +07:00
result.answers.push(rec);
}
for _ in 0..result.header.authoritative_entries {
2020-06-18 05:45:03 +07:00
let rec = DnsRecord::read(buffer)?;
2017-11-23 20:51:20 +07:00
result.authorities.push(rec);
}
for _ in 0..result.header.resource_entries {
2020-06-18 05:45:03 +07:00
let rec = DnsRecord::read(buffer)?;
2017-11-23 20:51:20 +07:00
result.resources.push(rec);
}
Ok(result)
}
2020-06-18 05:45:03 +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;
2020-06-18 05:45:03 +07:00
self.header.write(buffer)?;
2017-11-23 20:51:20 +07:00
for question in &self.questions {
2020-06-18 05:45:03 +07:00
question.write(buffer)?;
2017-11-23 20:51:20 +07:00
}
for rec in &self.answers {
2020-06-18 05:45:03 +07:00
rec.write(buffer)?;
2017-11-23 20:51:20 +07:00
}
for rec in &self.authorities {
2020-06-18 05:45:03 +07:00
rec.write(buffer)?;
2017-11-23 20:51:20 +07:00
}
for rec in &self.resources {
2020-06-18 05:45:03 +07:00
rec.write(buffer)?;
2017-11-23 20:51:20 +07:00
}
Ok(())
}
}
2020-06-18 05:45:03 +07:00
fn main() -> Result<()> {
2017-11-23 20:51:20 +07:00
let qname = "yahoo.com";
let qtype = QueryType::MX;
let server = ("8.8.8.8", 53);
2020-06-18 05:45:03 +07:00
let socket = UdpSocket::bind(("0.0.0.0", 43210))?;
2017-11-23 20:51:20 +07:00
let mut packet = DnsPacket::new();
packet.header.id = 6666;
packet.header.questions = 1;
packet.header.recursion_desired = true;
2020-06-18 05:45:03 +07:00
packet
.questions
.push(DnsQuestion::new(qname.to_string(), qtype));
2017-11-23 20:51:20 +07:00
let mut req_buffer = BytePacketBuffer::new();
2020-06-18 05:45:03 +07:00
packet.write(&mut req_buffer)?;
socket.send_to(&req_buffer.buf[0..req_buffer.pos], server)?;
2017-11-23 20:51:20 +07:00
let mut res_buffer = BytePacketBuffer::new();
2020-06-18 05:45:03 +07:00
socket.recv_from(&mut res_buffer.buf)?;
2017-11-23 20:51:20 +07:00
2020-06-18 05:45:03 +07:00
let res_packet = DnsPacket::from_buffer(&mut res_buffer)?;
2017-11-23 20:51:20 +07:00
println!("{:?}", res_packet.header);
for q in res_packet.questions {
println!("{:?}", q);
}
for rec in res_packet.answers {
println!("{:?}", rec);
}
for rec in res_packet.authorities {
println!("{:?}", rec);
}
for rec in res_packet.resources {
println!("{:?}", rec);
}
2020-06-18 05:45:03 +07:00
Ok(())
2017-11-23 20:51:20 +07:00
}