Skip to content

Commit

Permalink
refactor: use z32 encoding for pkarr domain names
Browse files Browse the repository at this point in the history
  • Loading branch information
Frando committed Feb 28, 2024
1 parent 0cbc308 commit 020cc60
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions iroh-dns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ rustls = "0.21"
tokio = { version = "1", features = ["rt", "sync"] }
tracing = "0.1"
url = { version = "2", features = ["serde"] }
z32 = "1.0.3"

[dev-dependencies]
clap = { version = "4.5.1", features = ["derive"] }
Expand Down
19 changes: 10 additions & 9 deletions iroh-dns/examples/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use iroh_dns::{
packet::IROH_NODE_TXT_LABEL,
publish::{Config, Publisher},
resolve::{EXAMPLE_DOMAIN, IROH_TEST_DOMAIN},
to_z32,
};

#[derive(ValueEnum, Clone, Debug, Default, Copy)]
Expand Down Expand Up @@ -71,20 +72,20 @@ async fn main() -> Result<()> {
};
// let an = NodeAnnounce::new(node_id, Some(args.home_derp), vec![]);
publisher.publish_addr_info(&info).await?;
println!("published signed record to {}!", publisher.pkarr_relay());
println!(
"published signed record to {}! Resolve with ",
publisher.pkarr_relay()
);
match args.env {
Env::IrohTest => println!(
"TXT record resolvable at {}",
node_domain(node_id, IROH_TEST_DOMAIN)
),
Env::IrohTest => println!("dig {} TXT", node_domain(&node_id, IROH_TEST_DOMAIN)),
Env::LocalDev => println!(
"TXT record resolvable at {}",
node_domain(node_id, EXAMPLE_DOMAIN)
"dig @localhost -p 5353 {} TXT",
node_domain(&node_id, EXAMPLE_DOMAIN)
),
}
Ok(())
}

fn node_domain(node_id: NodeId, origin: &str) -> String {
format!("{}.{}.{}", IROH_NODE_TXT_LABEL, node_id, origin)
fn node_domain(node_id: &NodeId, origin: &str) -> String {
format!("{}.{}.{}", IROH_NODE_TXT_LABEL, to_z32(node_id), origin)
}
14 changes: 14 additions & 0 deletions iroh-dns/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
use anyhow::{anyhow, Result};
use iroh_net::NodeId;

pub mod discovery;
pub mod packet;
pub mod publish;
pub mod resolve;

pub fn to_z32(node_id: &NodeId) -> String {
z32::encode(node_id.as_bytes())
}

pub fn from_z32(s: &str) -> Result<NodeId> {
let bytes = z32::decode(s.as_bytes()).map_err(|_| anyhow!("invalid z32"))?;
let bytes: &[u8; 32] = &bytes.try_into().map_err(|_| anyhow!("not 32 bytes long"))?;
let node_id = NodeId::from_bytes(bytes)?;
Ok(node_id)
}
4 changes: 3 additions & 1 deletion iroh-dns/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use hickory_proto::error::ProtoError;
use iroh_net::{AddrInfo, NodeAddr, NodeId};
use url::Url;

use crate::from_z32;

pub const IROH_ROOT_ZONE: &str = "iroh";
pub const IROH_NODE_TXT_LABEL: &str = "_iroh_node";
pub const DEFAULT_TTL: u32 = 30;
Expand Down Expand Up @@ -231,7 +233,7 @@ fn is_hickory_node_info_name(name: &hickory_proto::rr::Name) -> Option<NodeId> {
return None;
}
let label = std::str::from_utf8(labels.next().expect("num_labels checked")).ok()?;
let node_id = NodeId::from_str(label).ok()?;
let node_id = from_z32(label).ok()?;
Some(node_id)
}

Expand Down
7 changes: 5 additions & 2 deletions iroh-dns/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use hickory_resolver::{
use iroh_net::{AddrInfo, NodeAddr, NodeId};
use tracing::debug;

use crate::packet::{NodeAnnounce, IROH_NODE_TXT_LABEL};
use crate::{
packet::{NodeAnnounce, IROH_NODE_TXT_LABEL},
to_z32,
};

pub const IROH_TEST_DNS_IPV4: Ipv4Addr = Ipv4Addr::new(5, 75, 181, 3);
pub const IROH_TEST_DOMAIN: &str = "testdns.iroh.link.";
Expand Down Expand Up @@ -93,7 +96,7 @@ impl Resolver {

pub async fn resolve_node_by_id(&self, node_id: NodeId) -> Result<AddrInfo> {
debug!(?node_id, "resolve node by id");
let name = Name::parse(&node_id.to_string(), Some(&self.default_node_origin))?;
let name = Name::parse(&to_z32(&node_id), Some(&self.default_node_origin))?;
let addr = self.resolve_node(name).await;
debug!(?node_id, ?addr, "resolved");
let addr = addr?;
Expand Down

0 comments on commit 020cc60

Please sign in to comment.