commit b059fbc7b511777f1bbcc81a67e8c1342bcffd5b
parent 0f327bf29338d7a08d404bc989502064ae95ed1f
Author: Daniel Barlow <dan@telent.net>
Date: Mon, 29 Dec 2025 18:17:13 +0000
remove redundant DnsResource.ty field
we can get the resource type from the RData field
Diffstat:
1 file changed, 16 insertions(+), 20 deletions(-)
diff --git a/ecu-esp32/src/wifi.rs b/ecu-esp32/src/wifi.rs
@@ -227,7 +227,6 @@ impl<'a> MdnsResponder<'a> {
#[derive(Clone,Debug)]
enum RData {
- Raw(Vec<u8>),
Srv { target: String, port: u16, priority: u16, weight: u16 },
Ptr(String),
A([u8; 4]),
@@ -249,12 +248,18 @@ impl RData {
&out[..offset+1]
}
+ fn ty(&self) -> ResourceType {
+ match self {
+ RData::Srv { .. } => ResourceType::Srv,
+ RData::Ptr(_name) => ResourceType::Ptr,
+ RData::A(_octets) => ResourceType::A,
+ RData::Txt(_strings) => ResourceType::Txt
+ }
+ }
+
+
fn to_bytes<'a>(&self, out: &'a mut [u8]) -> &'a [u8] {
match self {
- RData::Raw(b) => {
- out[..b.len()].clone_from_slice(&b);
- &out[..b.len()]
- },
RData::Srv { target, port, priority, weight } => {
out[0] = (priority >> 8) as u8;
out[1] = (priority & 0xff) as u8;
@@ -291,8 +296,6 @@ impl RData {
#[derive(Debug)]
struct DnsResource {
name : String,
- ty : ResourceType,
- // class is always IN, ttl depends on the query
data : RData,
data_raw : Vec<u8>
}
@@ -300,21 +303,19 @@ struct DnsResource {
impl DnsResource {
fn new(
name: impl Into<String>,
- ty : ResourceType,
data: RData
) -> Self {
let mut buf : [u8; 80] = [0; 80];
let rdata_raw = Vec::from(data.to_bytes(&mut buf));
Self {
name: name.into(),
- ty,
data,
data_raw: rdata_raw
}
}
fn to_resource_record(&self) -> ResourceRecord<'_> {
- let ttl = match self.ty {
+ let ttl = match self.data.ty() {
ResourceType::A => 120,
ResourceType::Srv => 120,
_ => 4500
@@ -322,7 +323,7 @@ impl DnsResource {
ResourceRecord::new(
&self.name[..],
- self.ty,
+ self.data.ty(),
1,
ttl,
&self.data_raw[..]
@@ -339,12 +340,10 @@ impl DnsDatabase {
let static_records = [
DnsResource::new(
"eculogical.local",
- ResourceType::A,
RData::A(my_ip_address)
),
DnsResource::new(
"Bike._keihin._udp.local",
- ResourceType::Srv,
RData::Srv {
target: String::from("eculogical.local"), port: 5000,
weight: 0, priority: 0
@@ -352,17 +351,14 @@ impl DnsDatabase {
),
DnsResource::new(
"Bike._keihin._udp.local",
- ResourceType::Txt,
RData::Txt(["txtvers=1".into()].into())
),
DnsResource::new(
"_keihin._udp.local",
- ResourceType::Ptr,
RData::Ptr("Bike._keihin._udp.local".into())
),
DnsResource::new(
DISCOVERY_NAME,
- ResourceType::Ptr,
RData::Ptr("_keihin._udp.local".into())
)
];
@@ -372,9 +368,10 @@ impl DnsDatabase {
fn find<'a>(&self, name: impl Into<String>, ty: ResourceType) -> Vec<&DnsResource> {
let name = name.into();
let results : Vec<&DnsResource> =
- self.records.iter().filter( |r|
- ((ty == ResourceType::Wildcard) || (r.ty == ty)) &&
- name == r.name
+ self.records.iter().filter(
+ |r|
+ ((ty == ResourceType::Wildcard) || (r.data.ty() == ty)) &&
+ name == r.name
).collect();
results
}
@@ -384,7 +381,6 @@ impl DnsDatabase {
self.records.iter().filter( |r| r.name != DISCOVERY_NAME ).collect()
}
-
fn additional(&self, r: &DnsResource) -> Vec<&DnsResource> {
match &r.data {
RData::Srv { target, port:_, priority:_, weight:_ } => {