Skip to content

Commit

Permalink
feat: support name probing and conflict resolution (#265)
Browse files Browse the repository at this point in the history
* update rustc version
* add a new DaemonEvent::Respond
* export RR type definitions
  • Loading branch information
keepsimple1 authored Nov 4, 2024
1 parent 429ecde commit 8b63fd7
Show file tree
Hide file tree
Showing 9 changed files with 1,842 additions and 272 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
os: [ubuntu-20.04, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@1.63.0
- uses: dtolnay/rust-toolchain@1.65.0
with:
components: rustfmt, clippy
- name: Run rustfmt and fail if any warnings
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "mdns-sd"
version = "0.11.5"
authors = ["keepsimple <[email protected]>"]
edition = "2018"
rust-version = "1.63.0"
rust-version = "1.65.0"
license = "Apache-2.0 OR MIT"
repository = "https://github.com/keepsimple1/mdns-sd"
documentation = "https://docs.rs/mdns-sd"
Expand All @@ -17,6 +17,7 @@ logging = ["log"]
default = ["async", "logging"]

[dependencies]
fastrand = "2.1"
flume = { version = "0.11", default-features = false } # channel between threads
if-addrs = { version = "0.13", features = ["link-local"] } # get local IP addresses
log = { version = "0.4", optional = true } # logging
Expand Down
22 changes: 16 additions & 6 deletions examples/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
//!
//! Run with:
//!
//! cargo run --example register <service_type> <instance_name>
//! cargo run --example register <service_type> <instance_name> <hostname> [options]
//!
//! Example:
//!
//! cargo run --example register _my-hello._udp test1
//! cargo run --example register _my-hello._udp instance1 host1
//!
//! Options:
//! "--unregister": automatically unregister after 2 seconds.
Expand All @@ -16,7 +16,9 @@ use mdns_sd::{DaemonEvent, IfKind, ServiceDaemon, ServiceInfo};
use std::{env, thread, time::Duration};

fn main() {
env_logger::init();
// setup env_logger with more precise timestamp.
let mut builder = env_logger::Builder::from_default_env();
builder.format_timestamp_millis().init();

// Simple command line options.
let args: Vec<String> = env::args().collect();
Expand Down Expand Up @@ -52,11 +54,18 @@ fn main() {
return;
}
};
let hostname = match args.get(3) {
Some(arg) => arg,
None => {
print_usage();
return;
}
};

// With `enable_addr_auto()`, we can give empty addrs and let the lib find them.
// If the caller knows specific addrs to use, then assign the addrs here.
let my_addrs = "";
let service_hostname = format!("{}{}", instance_name, &service_type);
let service_hostname = format!("{}.local.", hostname);
let port = 3456;

// The key string in TXT properties is case insensitive. Only the first
Expand Down Expand Up @@ -106,10 +115,11 @@ fn main() {

fn print_usage() {
println!("Usage:");
println!("cargo run --example register <service_type> <instance_name> [--unregister]");
println!("cargo run --example register <service_type> <instance_name> <hostname> [options]");
println!("Options:");
println!("--unregister: automatically unregister after 2 seconds");
println!("--disable-ipv6: not to use IPv6 interfaces.");
println!();
println!("For example:");
println!("cargo run --example register _my-hello._udp test1");
println!("cargo run --example register _my-hello._udp instance1 host1");
}
36 changes: 18 additions & 18 deletions src/dns_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::log::debug;
use crate::{
dns_parser::{
current_time_millis, split_sub_domain, DnsAddress, DnsPointer, DnsRecordBox, DnsSrv,
TYPE_A, TYPE_AAAA, TYPE_NSEC, TYPE_PTR, TYPE_SRV, TYPE_TXT,
RR_TYPE_A, RR_TYPE_AAAA, RR_TYPE_NSEC, RR_TYPE_PTR, RR_TYPE_SRV, RR_TYPE_TXT,
},
service_info::valid_two_addrs_on_intf,
};
Expand Down Expand Up @@ -124,7 +124,7 @@ impl DnsCache {

// If it is PTR with subtype, store a mapping from the instance fullname
// to the subtype in this cache.
if incoming.get_type() == TYPE_PTR {
if incoming.get_type() == RR_TYPE_PTR {
let (_, subtype_opt) = split_sub_domain(&entry_name);
if let Some(subtype) = subtype_opt {
if let Some(ptr) = incoming.any().downcast_ref::<DnsPointer>() {
Expand All @@ -137,11 +137,11 @@ impl DnsCache {

// get the existing records for the type.
let record_vec = match incoming.get_type() {
TYPE_PTR => self.ptr.entry(entry_name).or_default(),
TYPE_SRV => self.srv.entry(entry_name).or_default(),
TYPE_TXT => self.txt.entry(entry_name).or_default(),
TYPE_A | TYPE_AAAA => self.addr.entry(entry_name).or_default(),
TYPE_NSEC => self.nsec.entry(entry_name).or_default(),
RR_TYPE_PTR => self.ptr.entry(entry_name).or_default(),
RR_TYPE_SRV => self.srv.entry(entry_name).or_default(),
RR_TYPE_TXT => self.txt.entry(entry_name).or_default(),
RR_TYPE_A | RR_TYPE_AAAA => self.addr.entry(entry_name).or_default(),
RR_TYPE_NSEC => self.nsec.entry(entry_name).or_default(),
_ => return None,
};

Expand All @@ -168,7 +168,7 @@ impl DnsCache {
should_flush = true;

// additional checks for address records.
if rtype == TYPE_A || rtype == TYPE_AAAA {
if rtype == RR_TYPE_A || rtype == RR_TYPE_AAAA {
if let Some(addr) = r.any().downcast_ref::<DnsAddress>() {
if let Some(addr_b) = incoming.any().downcast_ref::<DnsAddress>() {
should_flush =
Expand Down Expand Up @@ -215,10 +215,10 @@ impl DnsCache {
let mut found = false;
let record_name = record.get_name();
let record_vec = match record.get_type() {
TYPE_PTR => self.ptr.get_mut(record_name),
TYPE_SRV => self.srv.get_mut(record_name),
TYPE_TXT => self.txt.get_mut(record_name),
TYPE_A | TYPE_AAAA => self.addr.get_mut(record_name),
RR_TYPE_PTR => self.ptr.get_mut(record_name),
RR_TYPE_SRV => self.srv.get_mut(record_name),
RR_TYPE_TXT => self.txt.get_mut(record_name),
RR_TYPE_A | RR_TYPE_AAAA => self.addr.get_mut(record_name),
_ => return found,
};
if let Some(record_vec) = record_vec {
Expand Down Expand Up @@ -277,7 +277,7 @@ impl DnsCache {
srv_records.retain(|srv| {
let expired = srv.get_record().is_expired(now);
if expired {
debug!("expired SRV: {}: {}", ty_domain, srv.get_name());
debug!("expired SRV: {}: {:?}", ty_domain, srv);
expired_instances
.entry(ty_domain.to_string())
.or_insert_with(HashSet::new)
Expand All @@ -299,7 +299,7 @@ impl DnsCache {
let expired = x.get_record().is_expired(now);
if expired {
if let Some(dns_ptr) = x.any().downcast_ref::<DnsPointer>() {
debug!("expired PTR: {:?}", dns_ptr);
debug!("expired PTR: domain:{ty_domain} record: {:?}", dns_ptr);
expired_instances
.entry(ty_domain.to_string())
.or_insert_with(HashSet::new)
Expand Down Expand Up @@ -460,10 +460,10 @@ impl DnsCache {
now: u64,
) -> Vec<&'a DnsRecordBox> {
let records_opt = match qtype {
TYPE_PTR => self.get_ptr(name),
TYPE_SRV => self.get_srv(name),
TYPE_A | TYPE_AAAA => self.get_addr(name),
TYPE_TXT => self.get_txt(name),
RR_TYPE_PTR => self.get_ptr(name),
RR_TYPE_SRV => self.get_srv(name),
RR_TYPE_A | RR_TYPE_AAAA => self.get_addr(name),
RR_TYPE_TXT => self.get_txt(name),
_ => None,
};

Expand Down
Loading

0 comments on commit 8b63fd7

Please sign in to comment.