From 9f3099311cd5eafd320efec56edad0df763f0bf1 Mon Sep 17 00:00:00 2001 From: Walker Crouse Date: Fri, 20 Aug 2021 13:02:12 -0400 Subject: [PATCH 1/2] Fix ServiceType logic Signed-off-by: Walker Crouse --- README.md | 2 +- zeroconf/src/service_type.rs | 43 ++++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 4a7fd01..a005644 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ browse services. On Linux: ```bash -$ sudo apt install xorg-dev libxcb-shape0-dev libxcb-xfixes0-dev clang +$ sudo apt install xorg-dev libxcb-shape0-dev libxcb-xfixes0-dev clang avahi-daemon libavahi-client-dev ``` ## TODO diff --git a/zeroconf/src/service_type.rs b/zeroconf/src/service_type.rs index dcd1caa..19547bb 100644 --- a/zeroconf/src/service_type.rs +++ b/zeroconf/src/service_type.rs @@ -40,26 +40,40 @@ impl ServiceType { Err("invalid character: .".into()) } else if part.contains(",") { Err("invalid character: ,".into()) + } else if part.is_empty() { + Err("cannot be empty".into()) } else { Ok(part) } } + + fn lstrip_underscore(s: &str) -> &str { + if s.starts_with("_") { + &s[1..] + } else { + s + } + } } impl ToString for ServiceType { fn to_string(&self) -> String { - format!("_{}._{}{}", self.name, self.protocol, { + format!( + "_{}._{}{}", + self.name, + self.protocol, if !self.sub_types.is_empty() { format!(",_{}", self.sub_types.join(",_")) } else { "".to_string() } - }) + ) } } impl FromStr for ServiceType { type Err = crate::error::Error; + fn from_str(s: &str) -> Result { let parts: Vec<&str> = s.split(",").collect(); if parts.is_empty() { @@ -67,23 +81,17 @@ impl FromStr for ServiceType { } let head: Vec<&str> = parts[0].split(".").collect(); - let mut name = head[0]; - if name.starts_with("_") { - name = &name[1..]; - } - let mut protocol = head[1]; - if protocol.starts_with("_") { - protocol = &protocol[1..]; + if head.len() != 2 { + return Err("invalid name and protocol".into()); } + let name = Self::lstrip_underscore(head[0]); + let protocol = Self::lstrip_underscore(head[1]); + let mut sub_types: Vec<&str> = vec![]; if parts.len() > 1 { for i in 1..parts.len() { - let mut sub_type = parts[i]; - if sub_type.starts_with("_") { - sub_type = &sub_type[1..]; - } - sub_types.push(sub_type); + sub_types.push(Self::lstrip_underscore(parts[i])); } } @@ -101,6 +109,13 @@ mod tests { ServiceType::new("http", ".tcp").expect_err("invalid character: .".into()); ServiceType::new(",http", "tcp").expect_err("invalid character: ,".into()); ServiceType::new("http", ",tcp").expect_err("invalid character: ,".into()); + ServiceType::new("", "tcp").expect_err("cannot be empty".into()); + ServiceType::new("http", "").expect_err("cannot be empty".into()); + } + + #[test] + fn must_have_name_and_protocol() { + ServiceType::from_str("_http").expect_err("invalid name and protocol".into()); } #[test] From 1c26bb3e93c4ee47977319fff9076bab75413a4f Mon Sep 17 00:00:00 2001 From: Walker Crouse Date: Fri, 20 Aug 2021 13:20:20 -0400 Subject: [PATCH 2/2] Fix ServiceType for macOS Signed-off-by: Walker Crouse --- examples/Cargo.lock | 2 +- zeroconf/src/macos/browser.rs | 3 ++- zeroconf/src/macos/service.rs | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/Cargo.lock b/examples/Cargo.lock index 219b55b..97fce50 100644 --- a/examples/Cargo.lock +++ b/examples/Cargo.lock @@ -502,7 +502,7 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "zeroconf" -version = "0.8.2" +version = "0.9.2" dependencies = [ "avahi-sys", "bonjour-sys", diff --git a/zeroconf/src/macos/browser.rs b/zeroconf/src/macos/browser.rs index 6b9bda4..f39f38e 100644 --- a/zeroconf/src/macos/browser.rs +++ b/zeroconf/src/macos/browser.rs @@ -265,10 +265,11 @@ unsafe fn handle_get_address_info( let hostname = c_str::copy_raw(hostname); let domain = bonjour_util::normalize_domain(&ctx.resolved_domain.take().unwrap()); + let kind = bonjour_util::normalize_domain(&ctx.resolved_kind.take().unwrap()); let result = ServiceDiscovery::builder() .name(ctx.resolved_name.take().unwrap()) - .service_type(ServiceType::from_str(&ctx.resolved_kind.take().unwrap())?) + .service_type(ServiceType::from_str(&kind)?) .domain(domain) .host_name(hostname) .address(ip) diff --git a/zeroconf/src/macos/service.rs b/zeroconf/src/macos/service.rs index 033fdec..cedab4a 100644 --- a/zeroconf/src/macos/service.rs +++ b/zeroconf/src/macos/service.rs @@ -158,10 +158,11 @@ unsafe fn handle_register( } let domain = bonjour_util::normalize_domain(c_str::raw_to_str(domain)); + let kind = bonjour_util::normalize_domain(c_str::raw_to_str(regtype)); let result = ServiceRegistration::builder() .name(c_str::copy_raw(name)) - .service_type(ServiceType::from_str(&c_str::copy_raw(regtype))?) + .service_type(ServiceType::from_str(&kind)?) .domain(domain) .build() .expect("could not build ServiceRegistration");