aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock80
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs24
3 files changed, 94 insertions, 11 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 510addf..dc8f1fc 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -125,7 +125,7 @@ dependencies = [
"hex",
"rand",
"thiserror",
- "trust-dns-proto",
+ "trust-dns-proto 0.21.2",
"url",
]
@@ -154,6 +154,18 @@ dependencies = [
]
[[package]]
+name = "enum-as-inner"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c9720bba047d567ffc8a3cba48bf19126600e249ab7f128e9233e6376976a116"
+dependencies = [
+ "heck",
+ "proc-macro2",
+ "quote",
+ "syn 1.0.109",
+]
+
+[[package]]
name = "filetime"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -558,6 +570,12 @@ dependencies = [
]
[[package]]
+name = "once_cell"
+version = "1.18.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
+
+[[package]]
name = "paste"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -673,6 +691,7 @@ dependencies = [
"rsdsl_dhcp4d",
"serde_json",
"thiserror",
+ "trust-dns-proto 0.22.0",
]
[[package]]
@@ -895,6 +914,38 @@ dependencies = [
]
[[package]]
+name = "tracing"
+version = "0.1.37"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8"
+dependencies = [
+ "cfg-if",
+ "pin-project-lite",
+ "tracing-attributes",
+ "tracing-core",
+]
+
+[[package]]
+name = "tracing-attributes"
+version = "0.1.26"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.27",
+]
+
+[[package]]
+name = "tracing-core"
+version = "0.1.31"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a"
+dependencies = [
+ "once_cell",
+]
+
+[[package]]
name = "trust-dns-proto"
version = "0.21.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -903,7 +954,7 @@ dependencies = [
"async-trait",
"cfg-if",
"data-encoding",
- "enum-as-inner",
+ "enum-as-inner 0.4.0",
"futures-channel",
"futures-io",
"futures-util",
@@ -919,6 +970,31 @@ dependencies = [
]
[[package]]
+name = "trust-dns-proto"
+version = "0.22.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4f7f83d1e4a0e4358ac54c5c3681e5d7da5efc5a7a632c90bb6d6669ddd9bc26"
+dependencies = [
+ "async-trait",
+ "cfg-if",
+ "data-encoding",
+ "enum-as-inner 0.5.1",
+ "futures-channel",
+ "futures-io",
+ "futures-util",
+ "idna 0.2.3",
+ "ipnet",
+ "lazy_static",
+ "rand",
+ "smallvec",
+ "thiserror",
+ "tinyvec",
+ "tokio",
+ "tracing",
+ "url",
+]
+
+[[package]]
name = "unicode-bidi"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 24d736e..d8eab8e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,3 +13,4 @@ notify = "5.1.0"
rsdsl_dhcp4d = { git = "https://github.com/rsdsl/dhcp4d.git", version = "0.1.6" }
serde_json = "1.0"
thiserror = "1.0"
+trust-dns-proto = "0.22.0"
diff --git a/src/main.rs b/src/main.rs
index f30f84a..dfe4ed9 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -15,6 +15,7 @@ use dns_message_parser::{Dns, Flags, Opcode, RCode};
use notify::event::{AccessKind, AccessMode, CreateKind};
use notify::{Event, EventKind, RecursiveMode, Watcher};
use rsdsl_dhcp4d::lease::Lease;
+use trust_dns_proto::rr::Name;
const UPSTREAM: &str = "8.8.8.8:53";
@@ -122,7 +123,10 @@ fn handle_query(
let (lan, fwd): (_, Vec<Question>) =
msg.questions.into_iter().partition(|q| {
- match is_dhcp_known(q.domain_name.to_string(), leases.clone()) {
+ match is_dhcp_known(
+ &Name::from_utf8(q.domain_name.to_string()).expect("not a valid UTF-8 domain name"),
+ leases.clone(),
+ ) {
Ok(known) => known,
Err(e) => {
println!("can't read dhcp config, ignoring {}: {}", q.domain_name, e);
@@ -137,9 +141,12 @@ fn handle_query(
.collect();
let lan_resp = lan.into_iter().filter_map(|q| {
- if q.q_type == QType::A || q.q_type == QType::ALL {
+ let hostname =
+ Name::from_utf8(q.domain_name.to_string()).expect("not a valid UTF-8 domain name");
+
+ if q.q_type == QType::A {
let net_id = subnet_id(&raddr.ip());
- let lease = dhcp_lease(q.domain_name.to_string(), net_id, leases.clone())
+ let lease = dhcp_lease(&hostname, net_id, leases.clone())
.unwrap()
.unwrap();
@@ -229,10 +236,9 @@ fn handle_query(
Ok(())
}
-fn find_lease(hostname: String, leases: impl Iterator<Item = Lease>) -> Option<Lease> {
+fn find_lease(hostname: &Name, leases: impl Iterator<Item = Lease>) -> Option<Lease> {
for lease in leases {
- let lease_name = lease.hostname.clone().map(|name| name + ".");
- if lease_name == Some(hostname.clone()) {
+ if lease.hostname.clone().map(|name| name + ".") == Some(hostname.to_utf8()) {
return Some(lease);
}
}
@@ -241,14 +247,14 @@ fn find_lease(hostname: String, leases: impl Iterator<Item = Lease>) -> Option<L
}
fn dhcp_lease(
- hostname: String,
+ hostname: &Name,
net_id: u8,
leases: Arc<RwLock<Vec<Lease>>>,
) -> Result<Option<Lease>> {
let leases = leases.read().unwrap();
let same_subnet = find_lease(
- hostname.clone(),
+ hostname,
leases
.clone()
.into_iter()
@@ -260,7 +266,7 @@ fn dhcp_lease(
Ok(same_subnet.or(any))
}
-fn is_dhcp_known(hostname: String, leases: Arc<RwLock<Vec<Lease>>>) -> Result<bool> {
+fn is_dhcp_known(hostname: &Name, leases: Arc<RwLock<Vec<Lease>>>) -> Result<bool> {
Ok(dhcp_lease(hostname, u8::MAX, leases)?.is_some())
}