Skip to content

Commit d6fa8b3

Browse files
refactor(iroh): use simple-dns in test-utils
1 parent 546b281 commit d6fa8b3

3 files changed

Lines changed: 47 additions & 39 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

iroh/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ sync_wrapper = { version = "1.0.2", features = ["futures"] }
7878

7979
# non-wasm-in-browser dependencies
8080
[target.'cfg(not(all(target_family = "wasm", target_os = "unknown")))'.dependencies]
81-
hickory-proto = "0.25.2"
81+
simple-dns = "0.9.3"
8282
portmapper = { version = "0.15", optional = true, default-features = false }
8383
noq = { version = "0.17.0", default-features = false, features = ["runtime-tokio", "rustls"] }
8484
tokio = { version = "1", features = [

iroh/src/test_utils.rs

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,8 @@ pub(crate) mod dns_server {
190190
net::{Ipv4Addr, SocketAddr},
191191
};
192192

193-
use hickory_proto::{
194-
op::{Message, header::MessageType},
195-
serialize::binary::BinDecodable,
196-
};
197193
use n0_future::future::Boxed as BoxFuture;
194+
use simple_dns::{Packet, PacketFlag};
198195
use tokio::{net::UdpSocket, sync::oneshot};
199196
use tracing::{debug, error, warn};
200197

@@ -204,20 +201,23 @@ pub(crate) mod dns_server {
204201
pub trait QueryHandler: Send + Sync + 'static {
205202
fn resolve(
206203
&self,
207-
query: &Message,
208-
reply: &mut Message,
204+
query: &Packet<'_>,
205+
reply: &mut Packet<'static>,
209206
) -> impl Future<Output = std::io::Result<()>> + Send;
210207
}
211208

212209
pub type QueryHandlerFunction = Box<
213-
dyn Fn(&Message, &mut Message) -> BoxFuture<std::io::Result<()>> + Send + Sync + 'static,
210+
dyn Fn(&Packet<'_>, &mut Packet<'static>) -> BoxFuture<std::io::Result<()>>
211+
+ Send
212+
+ Sync
213+
+ 'static,
214214
>;
215215

216216
impl QueryHandler for QueryHandlerFunction {
217217
fn resolve(
218218
&self,
219-
query: &Message,
220-
reply: &mut Message,
219+
query: &Packet<'_>,
220+
reply: &mut Packet<'static>,
221221
) -> impl Future<Output = std::io::Result<()>> + Send {
222222
(self)(query, reply)
223223
}
@@ -267,13 +267,13 @@ pub(crate) mod dns_server {
267267
}
268268

269269
async fn handle_datagram(&self, from: SocketAddr, buf: &[u8]) -> std::io::Result<()> {
270-
let packet = Message::from_bytes(buf)?;
271-
debug!(queries = ?packet.queries(), %from, "received query");
272-
let mut reply = packet.clone();
273-
reply.set_message_type(MessageType::Response);
270+
let packet = Packet::parse(buf).map_err(std::io::Error::other)?;
271+
debug!(questions = ?packet.questions, %from, "received query");
272+
let mut reply = Packet::new_reply(packet.id());
273+
reply.set_flags(PacketFlag::RECURSION_DESIRED | PacketFlag::RECURSION_AVAILABLE);
274274
self.resolver.resolve(&packet, &mut reply).await?;
275275
debug!(?reply, %from, "send reply");
276-
let buf = reply.to_vec()?;
276+
let buf = reply.build_bytes_vec().map_err(std::io::Error::other)?;
277277
let len = self.socket.send_to(&buf, from).await?;
278278
assert_eq!(len, buf.len(), "failed to send complete packet");
279279
Ok(())
@@ -449,12 +449,12 @@ pub(crate) mod pkarr_dns_state {
449449

450450
pub fn resolve_dns(
451451
&self,
452-
query: &hickory_proto::op::Message,
453-
reply: &mut hickory_proto::op::Message,
452+
query: &simple_dns::Packet<'_>,
453+
reply: &mut simple_dns::Packet<'static>,
454454
ttl: u32,
455455
) -> std::io::Result<()> {
456-
for query in query.queries() {
457-
let domain_name = query.name().to_string();
456+
for question in &query.questions {
457+
let domain_name = question.qname.to_string();
458458
let Some(endpoint_id) = endpoint_id_from_domain_name(&domain_name) else {
459459
continue;
460460
};
@@ -464,9 +464,9 @@ pub(crate) mod pkarr_dns_state {
464464
let endpoint_info = EndpointInfo::from_pkarr_signed_packet(packet)
465465
.map_err(std::io::Error::other)?;
466466
for record in
467-
endpoint_info_to_hickory_records(&endpoint_info, &self.origin, ttl)
467+
endpoint_info_to_dns_records(&endpoint_info, &self.origin, ttl)
468468
{
469-
reply.add_answer(record);
469+
reply.answers.push(record);
470470
}
471471
}
472472
Ok::<_, std::io::Error>(())
@@ -479,8 +479,8 @@ pub(crate) mod pkarr_dns_state {
479479
impl QueryHandler for State {
480480
fn resolve(
481481
&self,
482-
query: &hickory_proto::op::Message,
483-
reply: &mut hickory_proto::op::Message,
482+
query: &simple_dns::Packet<'_>,
483+
reply: &mut simple_dns::Packet<'static>,
484484
) -> impl Future<Output = std::io::Result<()>> + Send {
485485
const TTL: u32 = 30;
486486
let res = self.resolve_dns(query, reply, TTL);
@@ -506,32 +506,40 @@ pub(crate) mod pkarr_dns_state {
506506
Some(endpoint_id)
507507
}
508508

509-
/// Converts a [`EndpointInfo`]into a [`hickory_resolver::proto::rr::Record`] DNS record.
510-
fn endpoint_info_to_hickory_records(
509+
/// Converts an [`EndpointInfo`] into simple-dns [`ResourceRecord`]s.
510+
fn endpoint_info_to_dns_records(
511511
endpoint_info: &EndpointInfo,
512512
origin: &str,
513513
ttl: u32,
514-
) -> impl Iterator<Item = hickory_proto::rr::Record> + 'static {
514+
) -> Vec<simple_dns::ResourceRecord<'static>> {
515515
let txt_strings = endpoint_info.to_txt_strings();
516-
let records = to_hickory_records(txt_strings, endpoint_info.endpoint_id, origin, ttl);
517-
records.collect::<Vec<_>>().into_iter()
516+
to_dns_records(txt_strings, endpoint_info.endpoint_id, origin, ttl)
518517
}
519518

520-
/// Converts to a list of [`hickory_resolver::proto::rr::Record`] resource records.
521-
fn to_hickory_records(
519+
/// Converts TXT strings to simple-dns [`ResourceRecord`]s.
520+
fn to_dns_records(
522521
txt_strings: Vec<String>,
523522
endpoint_id: EndpointId,
524523
origin: &str,
525524
ttl: u32,
526-
) -> impl Iterator<Item = hickory_proto::rr::Record> + '_ {
527-
use hickory_proto::rr;
525+
) -> Vec<simple_dns::ResourceRecord<'static>> {
526+
use simple_dns::{
527+
CLASS, Name, ResourceRecord,
528+
rdata::{RData, TXT},
529+
};
528530
let name = format!("{IROH_TXT_NAME}.{}.{origin}", endpoint_id.to_z32());
529-
let name = rr::Name::from_utf8(name).expect("invalid name");
530-
txt_strings.into_iter().map(move |s| {
531-
let txt = rr::rdata::TXT::new(vec![s]);
532-
let rdata = rr::RData::TXT(txt);
533-
rr::Record::from_rdata(name.clone(), ttl, rdata)
534-
})
531+
let name = Name::new(&name).expect("invalid name").into_owned();
532+
txt_strings
533+
.into_iter()
534+
.map(move |s| {
535+
let txt = TXT::new()
536+
.with_string(&s)
537+
.expect("invalid TXT string")
538+
.into_owned();
539+
let rdata = RData::TXT(txt);
540+
ResourceRecord::new(name.clone(), CLASS::IN, ttl, rdata)
541+
})
542+
.collect()
535543
}
536544

537545
#[cfg(test)]

0 commit comments

Comments
 (0)