inital working dns
This commit is contained in:
parent
86f9c6c036
commit
b7676d0636
16 changed files with 1354 additions and 0 deletions
365
Cargo.lock
generated
Normal file
365
Cargo.lock
generated
Normal file
|
@ -0,0 +1,365 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "async-recursion"
|
||||||
|
version = "1.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3b015a331cc64ebd1774ba119538573603427eaace0a1950c423ab971f903796"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "autocfg"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitflags"
|
||||||
|
version = "1.3.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bytes"
|
||||||
|
version = "1.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hermit-abi"
|
||||||
|
version = "0.2.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.139"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lock_api"
|
||||||
|
version = "0.4.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
"scopeguard",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "log"
|
||||||
|
version = "0.4.17"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "mio"
|
||||||
|
version = "0.8.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"log",
|
||||||
|
"wasi",
|
||||||
|
"windows-sys 0.45.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num_cpus"
|
||||||
|
version = "1.15.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b"
|
||||||
|
dependencies = [
|
||||||
|
"hermit-abi",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "packet"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "parking_lot"
|
||||||
|
version = "0.12.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
|
||||||
|
dependencies = [
|
||||||
|
"lock_api",
|
||||||
|
"parking_lot_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "parking_lot_core"
|
||||||
|
version = "0.9.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9069cbb9f99e3a5083476ccb29ceb1de18b9118cafa53e90c9551235de2b9521"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"redox_syscall",
|
||||||
|
"smallvec",
|
||||||
|
"windows-sys 0.45.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pin-project-lite"
|
||||||
|
version = "0.2.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.51"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.23"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "redox_syscall"
|
||||||
|
version = "0.2.16"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "resolver"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"async-recursion",
|
||||||
|
"packet",
|
||||||
|
"tokio",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "scopeguard"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "signal-hook-registry"
|
||||||
|
version = "1.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "smallvec"
|
||||||
|
version = "1.10.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "socket2"
|
||||||
|
version = "0.4.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "02e2d2db9033d13a1567121ddd7a095ee144db4e1ca1b1bda3419bc0da294ebd"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "1.0.109"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tokio"
|
||||||
|
version = "1.25.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c8e00990ebabbe4c14c08aca901caed183ecd5c09562a12c824bb53d3c3fd3af"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
"bytes",
|
||||||
|
"libc",
|
||||||
|
"memchr",
|
||||||
|
"mio",
|
||||||
|
"num_cpus",
|
||||||
|
"parking_lot",
|
||||||
|
"pin-project-lite",
|
||||||
|
"signal-hook-registry",
|
||||||
|
"socket2",
|
||||||
|
"tokio-macros",
|
||||||
|
"windows-sys 0.42.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tokio-macros"
|
||||||
|
version = "1.8.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasi"
|
||||||
|
version = "0.11.0+wasi-snapshot-preview1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi"
|
||||||
|
version = "0.3.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-i686-pc-windows-gnu",
|
||||||
|
"winapi-x86_64-pc-windows-gnu",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-i686-pc-windows-gnu"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-x86_64-pc-windows-gnu"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-sys"
|
||||||
|
version = "0.42.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7"
|
||||||
|
dependencies = [
|
||||||
|
"windows_aarch64_gnullvm",
|
||||||
|
"windows_aarch64_msvc",
|
||||||
|
"windows_i686_gnu",
|
||||||
|
"windows_i686_msvc",
|
||||||
|
"windows_x86_64_gnu",
|
||||||
|
"windows_x86_64_gnullvm",
|
||||||
|
"windows_x86_64_msvc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-sys"
|
||||||
|
version = "0.45.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
|
||||||
|
dependencies = [
|
||||||
|
"windows-targets",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-targets"
|
||||||
|
version = "0.42.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7"
|
||||||
|
dependencies = [
|
||||||
|
"windows_aarch64_gnullvm",
|
||||||
|
"windows_aarch64_msvc",
|
||||||
|
"windows_i686_gnu",
|
||||||
|
"windows_i686_msvc",
|
||||||
|
"windows_x86_64_gnu",
|
||||||
|
"windows_x86_64_gnullvm",
|
||||||
|
"windows_x86_64_msvc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_gnullvm"
|
||||||
|
version = "0.42.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_msvc"
|
||||||
|
version = "0.42.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_gnu"
|
||||||
|
version = "0.42.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_msvc"
|
||||||
|
version = "0.42.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnu"
|
||||||
|
version = "0.42.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnullvm"
|
||||||
|
version = "0.42.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_msvc"
|
||||||
|
version = "0.42.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wrapper"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"resolver",
|
||||||
|
"tokio",
|
||||||
|
]
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "wrapper"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
resolver = { path = "resolver"}
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
7
packet/Cargo.lock
generated
Normal file
7
packet/Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "packet"
|
||||||
|
version = "0.1.0"
|
6
packet/Cargo.toml
Normal file
6
packet/Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "packet"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
192
packet/src/buffer.rs
Normal file
192
packet/src/buffer.rs
Normal file
|
@ -0,0 +1,192 @@
|
||||||
|
use super::Result;
|
||||||
|
|
||||||
|
pub struct PacketBuffer {
|
||||||
|
pub buf: [u8; 512],
|
||||||
|
pub pos: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PacketBuffer {
|
||||||
|
pub fn new() -> PacketBuffer {
|
||||||
|
PacketBuffer {
|
||||||
|
buf: [0; 512],
|
||||||
|
pos: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pos(&self) -> usize {
|
||||||
|
self.pos
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn step(&mut self, steps: usize) -> Result<()> {
|
||||||
|
self.pos += steps;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn seek(&mut self, pos: usize) -> Result<()> {
|
||||||
|
self.pos = pos;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read(&mut self) -> Result<u8> {
|
||||||
|
if self.pos >= 512 {
|
||||||
|
return Err("End of buffer".into());
|
||||||
|
}
|
||||||
|
let res = self.buf[self.pos];
|
||||||
|
self.pos += 1;
|
||||||
|
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(&mut self, pos: usize) -> Result<u8> {
|
||||||
|
if pos >= 512 {
|
||||||
|
return Err("End of buffer".into());
|
||||||
|
}
|
||||||
|
Ok(self.buf[pos])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_range(&mut self, start: usize, len: usize) -> Result<&[u8]> {
|
||||||
|
if start + len >= 512 {
|
||||||
|
return Err("End of buffer".into());
|
||||||
|
}
|
||||||
|
Ok(&self.buf[start..start + len as usize])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_u16(&mut self) -> Result<u16> {
|
||||||
|
let res = ((self.read()? as u16) << 8) | (self.read()? as u16);
|
||||||
|
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub 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);
|
||||||
|
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_qname(&mut self, outstr: &mut String) -> Result<()> {
|
||||||
|
let mut pos = self.pos();
|
||||||
|
let mut jumped = false;
|
||||||
|
|
||||||
|
let mut delim = "";
|
||||||
|
let max_jumps = 5;
|
||||||
|
let mut jumps_performed = 0;
|
||||||
|
loop {
|
||||||
|
// 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)?;
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
if (len & 0xC0) == 0xC0 {
|
||||||
|
// When a jump is performed, we only modify the shared buffer
|
||||||
|
// position once, and avoid making the change later on.
|
||||||
|
if !jumped {
|
||||||
|
self.seek(pos + 2)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let b2 = self.get(pos + 1)? as u16;
|
||||||
|
let offset = (((len as u16) ^ 0xC0) << 8) | b2;
|
||||||
|
pos = offset as usize;
|
||||||
|
jumped = true;
|
||||||
|
jumps_performed += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
pos += 1;
|
||||||
|
|
||||||
|
// Names are terminated by an empty label of length 0
|
||||||
|
if len == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
outstr.push_str(delim);
|
||||||
|
|
||||||
|
let str_buffer = self.get_range(pos, len as usize)?;
|
||||||
|
outstr.push_str(&String::from_utf8_lossy(str_buffer).to_lowercase());
|
||||||
|
|
||||||
|
delim = ".";
|
||||||
|
|
||||||
|
pos += len as usize;
|
||||||
|
}
|
||||||
|
|
||||||
|
if !jumped {
|
||||||
|
self.seek(pos)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write(&mut self, val: u8) -> Result<()> {
|
||||||
|
if self.pos >= 512 {
|
||||||
|
return Err("End of buffer".into());
|
||||||
|
}
|
||||||
|
self.buf[self.pos] = val;
|
||||||
|
self.pos += 1;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write_u8(&mut self, val: u8) -> Result<()> {
|
||||||
|
self.write(val)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write_u16(&mut self, val: u16) -> Result<()> {
|
||||||
|
self.write((val >> 8) as u8)?;
|
||||||
|
self.write((val & 0xFF) as u8)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write_u32(&mut self, val: u32) -> Result<()> {
|
||||||
|
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)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write_qname(&mut self, qname: &str) -> Result<()> {
|
||||||
|
for label in qname.split('.') {
|
||||||
|
let len = label.len();
|
||||||
|
if len > 0x34 {
|
||||||
|
return Err("Single label exceeds 63 characters of length".into());
|
||||||
|
}
|
||||||
|
|
||||||
|
self.write_u8(len as u8)?;
|
||||||
|
for b in label.as_bytes() {
|
||||||
|
self.write_u8(*b)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.write_u8(0)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set(&mut self, pos: usize, val: u8) -> Result<()> {
|
||||||
|
self.buf[pos] = val;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_u16(&mut self, pos: usize, val: u16) -> Result<()> {
|
||||||
|
self.set(pos, (val >> 8) as u8)?;
|
||||||
|
self.set(pos + 1, (val & 0xFF) as u8)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
102
packet/src/header.rs
Normal file
102
packet/src/header.rs
Normal file
|
@ -0,0 +1,102 @@
|
||||||
|
use super::{buffer::PacketBuffer, Result, result::ResultCode};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct DnsHeader {
|
||||||
|
pub id: u16, // 16 bits
|
||||||
|
|
||||||
|
pub recursion_desired: bool, // 1 bit
|
||||||
|
pub truncated_message: bool, // 1 bit
|
||||||
|
pub authoritative_answer: bool, // 1 bit
|
||||||
|
pub opcode: u8, // 4 bits
|
||||||
|
pub response: bool, // 1 bit
|
||||||
|
|
||||||
|
pub rescode: ResultCode, // 4 bits
|
||||||
|
pub checking_disabled: bool, // 1 bit
|
||||||
|
pub authed_data: bool, // 1 bit
|
||||||
|
pub z: bool, // 1 bit
|
||||||
|
pub recursion_available: bool, // 1 bit
|
||||||
|
|
||||||
|
pub questions: u16, // 16 bits
|
||||||
|
pub answers: u16, // 16 bits
|
||||||
|
pub authoritative_entries: u16, // 16 bits
|
||||||
|
pub resource_entries: u16, // 16 bits
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DnsHeader {
|
||||||
|
pub fn new() -> DnsHeader {
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read(&mut self, buffer: &mut PacketBuffer) -> Result<()> {
|
||||||
|
self.id = buffer.read_u16()?;
|
||||||
|
|
||||||
|
let flags = buffer.read_u16()?;
|
||||||
|
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;
|
||||||
|
|
||||||
|
self.questions = buffer.read_u16()?;
|
||||||
|
self.answers = buffer.read_u16()?;
|
||||||
|
self.authoritative_entries = buffer.read_u16()?;
|
||||||
|
self.resource_entries = buffer.read_u16()?;
|
||||||
|
|
||||||
|
// Return the constant header size
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write(&self, buffer: &mut PacketBuffer) -> Result<()> {
|
||||||
|
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(
|
||||||
|
(self.rescode as u8)
|
||||||
|
| ((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)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
157
packet/src/lib.rs
Normal file
157
packet/src/lib.rs
Normal file
|
@ -0,0 +1,157 @@
|
||||||
|
use std::net::IpAddr;
|
||||||
|
|
||||||
|
use self::{header::DnsHeader, question::DnsQuestion, record::DnsRecord, query::QueryType};
|
||||||
|
|
||||||
|
type Error = Box<dyn std::error::Error>;
|
||||||
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
|
mod buffer;
|
||||||
|
mod header;
|
||||||
|
mod query;
|
||||||
|
mod question;
|
||||||
|
mod record;
|
||||||
|
mod result;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Packet {
|
||||||
|
pub header: DnsHeader,
|
||||||
|
pub questions: Vec<DnsQuestion>,
|
||||||
|
pub answers: Vec<DnsRecord>,
|
||||||
|
pub authorities: Vec<DnsRecord>,
|
||||||
|
pub resources: Vec<DnsRecord>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub use buffer::PacketBuffer;
|
||||||
|
pub use result::ResultCode;
|
||||||
|
|
||||||
|
pub use query::QueryType as PacketType;
|
||||||
|
pub use question::DnsQuestion as PacketQuestion;
|
||||||
|
|
||||||
|
impl Packet {
|
||||||
|
pub fn new() -> Packet {
|
||||||
|
Packet {
|
||||||
|
header: DnsHeader::new(),
|
||||||
|
questions: Vec::new(),
|
||||||
|
answers: Vec::new(),
|
||||||
|
authorities: Vec::new(),
|
||||||
|
resources: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_buffer(buffer: &mut PacketBuffer) -> Result<Packet> {
|
||||||
|
let mut result = Packet::new();
|
||||||
|
result.header.read(buffer)?;
|
||||||
|
|
||||||
|
for _ in 0..result.header.questions {
|
||||||
|
let mut question = DnsQuestion::new("".to_string(), QueryType::UNKNOWN(0));
|
||||||
|
question.read(buffer)?;
|
||||||
|
result.questions.push(question);
|
||||||
|
}
|
||||||
|
|
||||||
|
for _ in 0..result.header.answers {
|
||||||
|
let rec = DnsRecord::read(buffer)?;
|
||||||
|
result.answers.push(rec);
|
||||||
|
}
|
||||||
|
for _ in 0..result.header.authoritative_entries {
|
||||||
|
let rec = DnsRecord::read(buffer)?;
|
||||||
|
result.authorities.push(rec);
|
||||||
|
}
|
||||||
|
for _ in 0..result.header.resource_entries {
|
||||||
|
let rec = DnsRecord::read(buffer)?;
|
||||||
|
result.resources.push(rec);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write(&mut self, buffer: &mut PacketBuffer) -> Result<()> {
|
||||||
|
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;
|
||||||
|
|
||||||
|
self.header.write(buffer)?;
|
||||||
|
|
||||||
|
for question in &self.questions {
|
||||||
|
question.write(buffer)?;
|
||||||
|
}
|
||||||
|
for rec in &self.answers {
|
||||||
|
rec.write(buffer)?;
|
||||||
|
}
|
||||||
|
for rec in &self.authorities {
|
||||||
|
rec.write(buffer)?;
|
||||||
|
}
|
||||||
|
for rec in &self.resources {
|
||||||
|
rec.write(buffer)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// It's useful to be able to pick a random A record from a packet. When we
|
||||||
|
/// get multiple IP's for a single name, it doesn't matter which one we
|
||||||
|
/// choose, so in those cases we can now pick one at random.
|
||||||
|
pub fn get_random_a(&self) -> Option<IpAddr> {
|
||||||
|
self.answers
|
||||||
|
.iter()
|
||||||
|
.filter_map(|record| match record {
|
||||||
|
DnsRecord::A { addr, .. } => Some(IpAddr::V4(*addr)),
|
||||||
|
DnsRecord::AAAA { addr, .. } => Some(IpAddr::V6(*addr)),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.next()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A helper function which returns an iterator over all name servers in
|
||||||
|
/// the authorities section, represented as (domain, host) tuples
|
||||||
|
fn get_ns<'a>(&'a self, qname: &'a str) -> impl Iterator<Item = (&'a str, &'a str)> {
|
||||||
|
self.authorities
|
||||||
|
.iter()
|
||||||
|
// In practice, these are always NS records in well formed packages.
|
||||||
|
// Convert the NS records to a tuple which has only the data we need
|
||||||
|
// to make it easy to work with.
|
||||||
|
.filter_map(|record| match record {
|
||||||
|
DnsRecord::NS { domain, host, .. } => Some((domain.as_str(), host.as_str())),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
// Discard servers which aren't authoritative to our query
|
||||||
|
.filter(move |(domain, _)| qname.ends_with(*domain))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// We'll use the fact that name servers often bundle the corresponding
|
||||||
|
/// A records when replying to an NS query to implement a function that
|
||||||
|
/// returns the actual IP for an NS record if possible.
|
||||||
|
pub fn get_resolved_ns(&self, qname: &str) -> Option<IpAddr> {
|
||||||
|
// Get an iterator over the nameservers in the authorities section
|
||||||
|
self.get_ns(qname)
|
||||||
|
// Now we need to look for a matching A record in the additional
|
||||||
|
// section. Since we just want the first valid record, we can just
|
||||||
|
// build a stream of matching records.
|
||||||
|
.flat_map(|(_, host)| {
|
||||||
|
self.resources
|
||||||
|
.iter()
|
||||||
|
// Filter for A records where the domain match the host
|
||||||
|
// of the NS record that we are currently processing
|
||||||
|
.filter_map(move |record| match record {
|
||||||
|
DnsRecord::A { domain, addr, .. } if domain == host => Some(IpAddr::V4(*addr)),
|
||||||
|
DnsRecord::AAAA { domain, addr, .. } if domain == host => Some(IpAddr::V6(*addr)),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.map(|addr| addr)
|
||||||
|
// Finally, pick the first valid entry
|
||||||
|
.next()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// However, not all name servers are as that nice. In certain cases there won't
|
||||||
|
/// be any A records in the additional section, and we'll have to perform *another*
|
||||||
|
/// lookup in the midst. For this, we introduce a method for returning the host
|
||||||
|
/// name of an appropriate name server.
|
||||||
|
pub fn get_unresolved_ns<'a>(&'a self, qname: &'a str) -> Option<&'a str> {
|
||||||
|
// Get an iterator over the nameservers in the authorities section
|
||||||
|
self.get_ns(qname)
|
||||||
|
.map(|(_, host)| host)
|
||||||
|
// Finally, pick the first valid entry
|
||||||
|
.next()
|
||||||
|
}
|
||||||
|
}
|
33
packet/src/query.rs
Normal file
33
packet/src/query.rs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
#[derive(PartialEq, Eq, Debug, Clone, Hash, Copy)]
|
||||||
|
pub enum QueryType {
|
||||||
|
UNKNOWN(u16),
|
||||||
|
A, // 1
|
||||||
|
NS, // 2
|
||||||
|
CNAME, // 5
|
||||||
|
MX, // 15
|
||||||
|
AAAA, // 28
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
_ => QueryType::UNKNOWN(num),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
31
packet/src/question.rs
Normal file
31
packet/src/question.rs
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
use super::{buffer::PacketBuffer, query::QueryType, Result};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct DnsQuestion {
|
||||||
|
pub name: String,
|
||||||
|
pub qtype: QueryType,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DnsQuestion {
|
||||||
|
pub fn new(name: String, qtype: QueryType) -> DnsQuestion {
|
||||||
|
DnsQuestion { name, qtype }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read(&mut self, buffer: &mut PacketBuffer) -> Result<()> {
|
||||||
|
buffer.read_qname(&mut self.name)?;
|
||||||
|
self.qtype = QueryType::from_num(buffer.read_u16()?); // qtype
|
||||||
|
let _ = buffer.read_u16()?; // class
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write(&self, buffer: &mut PacketBuffer) -> Result<()> {
|
||||||
|
buffer.write_qname(&self.name)?;
|
||||||
|
|
||||||
|
let typenum = self.qtype.to_num();
|
||||||
|
buffer.write_u16(typenum)?;
|
||||||
|
buffer.write_u16(1)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
227
packet/src/record.rs
Normal file
227
packet/src/record.rs
Normal file
|
@ -0,0 +1,227 @@
|
||||||
|
use std::net::{Ipv4Addr, Ipv6Addr};
|
||||||
|
|
||||||
|
use super::{query::QueryType, buffer::PacketBuffer, Result};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub enum DnsRecord {
|
||||||
|
UNKNOWN {
|
||||||
|
domain: String,
|
||||||
|
qtype: u16,
|
||||||
|
data_len: u16,
|
||||||
|
ttl: u32,
|
||||||
|
}, // 0
|
||||||
|
A {
|
||||||
|
domain: String,
|
||||||
|
addr: Ipv4Addr,
|
||||||
|
ttl: u32,
|
||||||
|
}, // 1
|
||||||
|
NS {
|
||||||
|
domain: String,
|
||||||
|
host: String,
|
||||||
|
ttl: u32,
|
||||||
|
}, // 2
|
||||||
|
CNAME {
|
||||||
|
domain: String,
|
||||||
|
host: String,
|
||||||
|
ttl: u32,
|
||||||
|
}, // 5
|
||||||
|
MX {
|
||||||
|
domain: String,
|
||||||
|
priority: u16,
|
||||||
|
host: String,
|
||||||
|
ttl: u32,
|
||||||
|
}, // 15
|
||||||
|
AAAA {
|
||||||
|
domain: String,
|
||||||
|
addr: Ipv6Addr,
|
||||||
|
ttl: u32,
|
||||||
|
}, // 28
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DnsRecord {
|
||||||
|
pub fn read(buffer: &mut PacketBuffer) -> 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, addr, 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, addr, ttl })
|
||||||
|
}
|
||||||
|
QueryType::NS => {
|
||||||
|
let mut ns = String::new();
|
||||||
|
buffer.read_qname(&mut ns)?;
|
||||||
|
|
||||||
|
Ok(DnsRecord::NS {
|
||||||
|
domain,
|
||||||
|
host: ns,
|
||||||
|
ttl,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
QueryType::CNAME => {
|
||||||
|
let mut cname = String::new();
|
||||||
|
buffer.read_qname(&mut cname)?;
|
||||||
|
|
||||||
|
Ok(DnsRecord::CNAME {
|
||||||
|
domain,
|
||||||
|
host: cname,
|
||||||
|
ttl,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
QueryType::MX => {
|
||||||
|
let priority = buffer.read_u16()?;
|
||||||
|
let mut mx = String::new();
|
||||||
|
buffer.read_qname(&mut mx)?;
|
||||||
|
|
||||||
|
Ok(DnsRecord::MX {
|
||||||
|
domain,
|
||||||
|
priority,
|
||||||
|
host: mx,
|
||||||
|
ttl,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
QueryType::UNKNOWN(_) => {
|
||||||
|
buffer.step(data_len as usize)?;
|
||||||
|
|
||||||
|
Ok(DnsRecord::UNKNOWN {
|
||||||
|
domain,
|
||||||
|
qtype: qtype_num,
|
||||||
|
data_len,
|
||||||
|
ttl,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write(&self, buffer: &mut PacketBuffer) -> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(buffer.pos() - start_pos)
|
||||||
|
}
|
||||||
|
}
|
22
packet/src/result.rs
Normal file
22
packet/src/result.rs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||||
|
pub enum ResultCode {
|
||||||
|
NOERROR = 0,
|
||||||
|
FORMERR = 1,
|
||||||
|
SERVFAIL = 2,
|
||||||
|
NXDOMAIN = 3,
|
||||||
|
NOTIMP = 4,
|
||||||
|
REFUSED = 5,
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
0 | _ => ResultCode::NOERROR,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
9
resolver/Cargo.toml
Normal file
9
resolver/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
name = "resolver"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
packet = { path = "../packet" }
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
async-recursion = "1"
|
23
resolver/src/config.rs
Normal file
23
resolver/src/config.rs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
use std::net::IpAddr;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Config {
|
||||||
|
fallback: IpAddr
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let fallback = "9.9.9.9".parse::<IpAddr>().expect("Failed to create default ns fallback");
|
||||||
|
Self { fallback }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_fallback_ns(&self) -> &IpAddr {
|
||||||
|
return &self.fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_fallback_ns(&mut self, addr: &IpAddr) {
|
||||||
|
self.fallback = addr.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
33
resolver/src/lib.rs
Normal file
33
resolver/src/lib.rs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use server::handle_query;
|
||||||
|
use tokio::net::UdpSocket;
|
||||||
|
use packet::Result;
|
||||||
|
|
||||||
|
mod server;
|
||||||
|
mod config;
|
||||||
|
|
||||||
|
pub use config::Config as Config;
|
||||||
|
|
||||||
|
pub struct DnsResolver {
|
||||||
|
config: Config
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DnsResolver {
|
||||||
|
|
||||||
|
pub fn new(config: Config) -> Self {
|
||||||
|
Self { config }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn bind(self, addr: SocketAddr) -> Result<()> {
|
||||||
|
|
||||||
|
let socket = UdpSocket::bind(addr).await?;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match handle_query(&socket, &self.config).await {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(e) => eprintln!("An error occurred: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
121
resolver/src/server.rs
Normal file
121
resolver/src/server.rs
Normal file
|
@ -0,0 +1,121 @@
|
||||||
|
use std::net::IpAddr;
|
||||||
|
use async_recursion::async_recursion;
|
||||||
|
use packet::{PacketType, Packet, Result, PacketQuestion, PacketBuffer, ResultCode};
|
||||||
|
use tokio::net::UdpSocket;
|
||||||
|
|
||||||
|
use crate::config::Config;
|
||||||
|
|
||||||
|
|
||||||
|
async fn lookup(qname: &str, qtype: PacketType, server: (IpAddr, u16)) -> Result<Packet> {
|
||||||
|
let socket = UdpSocket::bind("0.0.0.0:43210").await?;
|
||||||
|
|
||||||
|
let mut packet = Packet::new();
|
||||||
|
|
||||||
|
packet.header.id = 6666;
|
||||||
|
packet.header.questions = 1;
|
||||||
|
packet.header.recursion_desired = true;
|
||||||
|
packet
|
||||||
|
.questions
|
||||||
|
.push(PacketQuestion::new(qname.to_string(), qtype));
|
||||||
|
|
||||||
|
let mut req_buffer = PacketBuffer::new();
|
||||||
|
packet.write(&mut req_buffer)?;
|
||||||
|
socket.send_to(&req_buffer.buf[0..req_buffer.pos], server).await?;
|
||||||
|
|
||||||
|
let mut res_buffer = PacketBuffer::new();
|
||||||
|
socket.recv_from(&mut res_buffer.buf).await?;
|
||||||
|
|
||||||
|
Packet::from_buffer(&mut res_buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_recursion]
|
||||||
|
async fn recursive_lookup(qname: &str, qtype: PacketType, config: &Config) -> Result<Packet> {
|
||||||
|
|
||||||
|
let mut ns = config.get_fallback_ns().clone();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
println!("attempting lookup of {:?} {} with ns {}", qtype, qname, ns);
|
||||||
|
|
||||||
|
let ns_copy = ns;
|
||||||
|
|
||||||
|
let server = (ns_copy, 53);
|
||||||
|
let response = lookup(qname, qtype, server).await?;
|
||||||
|
|
||||||
|
if !response.answers.is_empty() && response.header.rescode == ResultCode::NOERROR {
|
||||||
|
return Ok(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
if response.header.rescode == ResultCode::NXDOMAIN {
|
||||||
|
return Ok(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(new_ns) = response.get_resolved_ns(qname) {
|
||||||
|
ns = new_ns;
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let new_ns_name = match response.get_unresolved_ns(qname) {
|
||||||
|
Some(x) => x,
|
||||||
|
None => return Ok(response),
|
||||||
|
};
|
||||||
|
|
||||||
|
let recursive_response = recursive_lookup(&new_ns_name, PacketType::A, config).await?;
|
||||||
|
|
||||||
|
if let Some(new_ns) = recursive_response.get_random_a() {
|
||||||
|
ns = new_ns;
|
||||||
|
} else {
|
||||||
|
return Ok(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn handle_query(socket: &UdpSocket, config: &Config) -> Result<()> {
|
||||||
|
let mut req_buffer = PacketBuffer::new();
|
||||||
|
|
||||||
|
let (_, src) = socket.recv_from(&mut req_buffer.buf).await?;
|
||||||
|
|
||||||
|
let mut request = Packet::from_buffer(&mut req_buffer)?;
|
||||||
|
|
||||||
|
let mut packet = Packet::new();
|
||||||
|
packet.header.id = request.header.id;
|
||||||
|
packet.header.recursion_desired = true;
|
||||||
|
packet.header.recursion_available = true;
|
||||||
|
packet.header.response = true;
|
||||||
|
|
||||||
|
if let Some(question) = request.questions.pop() {
|
||||||
|
println!("Received query: {:?}", question);
|
||||||
|
|
||||||
|
if let Ok(result) = recursive_lookup(&question.name, question.qtype, config).await {
|
||||||
|
packet.questions.push(question.clone());
|
||||||
|
packet.header.rescode = result.header.rescode;
|
||||||
|
|
||||||
|
for rec in result.answers {
|
||||||
|
println!("Answer: {:?}", rec);
|
||||||
|
packet.answers.push(rec);
|
||||||
|
}
|
||||||
|
for rec in result.authorities {
|
||||||
|
println!("Authority: {:?}", rec);
|
||||||
|
packet.authorities.push(rec);
|
||||||
|
}
|
||||||
|
for rec in result.resources {
|
||||||
|
println!("Resource: {:?}", rec);
|
||||||
|
packet.resources.push(rec);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
packet.header.rescode = ResultCode::SERVFAIL;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
packet.header.rescode = ResultCode::FORMERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut res_buffer = PacketBuffer::new();
|
||||||
|
packet.write(&mut res_buffer)?;
|
||||||
|
|
||||||
|
let len = res_buffer.pos();
|
||||||
|
let data = res_buffer.get_range(0, len)?;
|
||||||
|
|
||||||
|
socket.send_to(data, src).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
18
src/main.rs
Normal file
18
src/main.rs
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
|
use resolver::{DnsResolver, Config};
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main () {
|
||||||
|
|
||||||
|
let config = Config::new();
|
||||||
|
|
||||||
|
let resolver = DnsResolver::new(config);
|
||||||
|
|
||||||
|
let addr = "[::]:2000".parse::<SocketAddr>()
|
||||||
|
.expect("Failed to create binding");
|
||||||
|
|
||||||
|
resolver.bind(addr).await
|
||||||
|
.expect("Failed to start dns server");
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue