Skip to content

Commit

Permalink
feat: support name probing and conflict resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
keepsimple1 committed Oct 19, 2024
1 parent 429ecde commit 9a71f68
Show file tree
Hide file tree
Showing 7 changed files with 1,289 additions and 176 deletions.
5 changes: 3 additions & 2 deletions 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,14 +17,15 @@ 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
polling = "2.1" # select/poll sockets
socket2 = { version = "0.5.5", features = ["all"] } # socket APIs

[dev-dependencies]
env_logger = { version = "= 0.10.2", default-features = false, features= ["humantime"] }
env_logger = "0.11"
fastrand = "2.1"
humantime = "2.1"
test-log = "= 0.2.14"
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");
}
4 changes: 2 additions & 2 deletions src/dns_cache.rs
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 9a71f68

Please sign in to comment.