commit bfecc7f4bf7ff3eac53fdacbc9e01220694e03b8
parent 403ff664cc5785d75eb662d0c186c918aba70fc1
Author: Daniel Barlow <dan@telent.net>
Date: Fri, 26 Dec 2025 23:36:29 +0000
extract struct DnsDatabase
Diffstat:
1 file changed, 24 insertions(+), 23 deletions(-)
diff --git a/ecu-esp32/src/wifi.rs b/ecu-esp32/src/wifi.rs
@@ -150,15 +150,21 @@ impl DnsResource {
&self.data_raw[..]
)
}
+}
+
+struct DnsDatabase {
+ records: Vec<DnsResource>
+}
- fn all(my_ip_address : &[u8]) -> Vec<Self> {
+impl DnsDatabase {
+ fn create(my_ip_address : &[u8]) -> Self {
let static_records = [
- Self::new(
+ DnsResource::new(
"eculogical.local",
ResourceType::A,
RData::Raw(my_ip_address.try_into().unwrap())
),
- Self::new(
+ DnsResource::new(
"Bike._keihin._udp.local",
ResourceType::Srv,
RData::Srv {
@@ -166,42 +172,37 @@ impl DnsResource {
weight: 0, priority: 0
}
),
- Self::new(
+ DnsResource::new(
"Bike._keihin._udp.local",
ResourceType::Txt,
RData::Raw(b"\x09txtvers=1".to_vec())
),
- Self::new(
+ DnsResource::new(
"_keihin._udp.local",
ResourceType::Ptr,
RData::Ptr("Bike._keihin._udp.local".into())
),
- Self::new(
+ DnsResource::new(
"_services._dns-sd._udp.local",
ResourceType::Ptr,
RData::Ptr("_keihin._udp.local".into())
)
];
- Vec::from(static_records)
+ Self { records: Vec::from(static_records) }
}
- fn find<'a>(name: Label, ty: ResourceType, resources: &'a Vec<Self>) -> Vec<&'a Self> {
+ fn find<'a>(&self, name: Label, ty: ResourceType) -> Vec<&DnsResource> {
let dotted_name = name.to_string();
- resources.into_iter().filter( |r|
- ((ty == ResourceType::Wildcard) || (r.ty == ty)) &&
- dotted_name == r.name
- ).collect()
+ let results : Vec<&DnsResource> =
+ self.records.iter().filter( |r|
+ ((ty == ResourceType::Wildcard) || (r.ty == ty)) &&
+ dotted_name == r.name
+ ).collect();
+ results
}
}
-fn alloc_ptr_data(name: &[&str]) -> Vec<u8> {
- let mut b : [u8; 80] = [0; 80];
- Vec::from(ecu_portable::ptr_data(name, &mut b))
-}
-
-
-
// RFC: "Multicast DNS responses MUST NOT contain any questions in the
// Question Section. ". However, responses to "legacy" queries (port != 5353)
// should have a question section just like traditional DNS
@@ -249,7 +250,7 @@ async fn send_response(sock: &UdpSocket<'_>,
}
async fn send_responses(sock: &UdpSocket<'_>,
- resources: &Vec<DnsResource>,
+ db: &DnsDatabase,
message: &Message<'_,'_>,
meta: &UdpMetadata) {
@@ -260,7 +261,7 @@ async fn send_responses(sock: &UdpSocket<'_>,
// it's ok to do this for legacy queries but they probably
// don't have multiple questions anyway
for q in message.questions() {
- let relevant = DnsResource::find(q.name(), q.ty(), resources);
+ let relevant = db.find(q.name(), q.ty());
for r in relevant {
log::info!("question {:?} {:?} {:?} flags {:?} from {:?}",
q.name(), q.ty(), q.class(),
@@ -359,7 +360,7 @@ pub async fn start_wifi(spawner: &Spawner,
let mut data_buf = [0; 4096];
- let dns_resources = DnsResource::all(&my_address);
+ let dns_db = DnsDatabase::create(&my_address);
loop {
if sock.may_recv() {
@@ -381,7 +382,7 @@ pub async fn start_wifi(spawner: &Spawner,
match message {
Result::Ok(message) => {
- send_responses(&sock, &dns_resources, &message, &meta).await;
+ send_responses(&sock, &dns_db, &message, &meta).await;
},
Result::Err(err) => {
log::error!("parsing dns message from {:?} : {:?}",