From 4368d5f57cc5078d3a793cbf1e965633ff3b01e4 Mon Sep 17 00:00:00 2001 From: Seungyoun Ju Date: Thu, 20 Jun 2024 14:24:32 +0900 Subject: [PATCH] Add with_default_handle_and_ip_list_and_hostname In some platforms, hostname is fixed so system doesn't gurantee hostname conflict when the application uses system hostname. This could be the platform bug but there is a case where the application wants to use specific and unique hostname for their purpose. This change add new function to spawn responder task with the application specific hostname. --- src/lib.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b227e63..15f13fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -90,12 +90,59 @@ impl Responder { /// Spawn a `Responder` task with the provided tokio `Handle`. /// DNS response records will have the reported IPs limited to those passed in here. /// This can be particularly useful on machines with lots of networks created by tools such as docker. + /// + /// # Example + /// ```no_run + /// use libmdns::Responder; + /// + /// # use std::io; + /// # fn main() -> io::Result<()> { + /// let rt = tokio::runtime::Builder::new_current_thread().build().unwrap(); + /// let handle = rt.handle().clone(); + /// let vec: Vec = vec![ + /// "192.168.1.10".parse::().unwrap().into(), + /// std::net::Ipv6Addr::new(0, 0, 0, 0xfe80, 0x1ff, 0xfe23, 0x4567, 0x890a).into(), + /// ]; + /// let responder = Responder::spawn_with_ip_list(&handle, vec)?; + /// # Ok(()) + /// # } + /// ``` pub fn spawn_with_ip_list(handle: &Handle, allowed_ips: Vec) -> io::Result { let (responder, task) = Self::with_default_handle_and_ip_list(allowed_ips)?; handle.spawn(task); Ok(responder) } + /// Spawn a `Responder` task with the provided tokio `Handle`. + /// DNS response records will have the reported IPs limited to those passed in here. + /// This can be particularly useful on machines with lots of networks created by tools such as docker. + /// And SRV field will have specified hostname instead of system hostname. + /// This can be particularly useful if the platform has the fixed hostname and the application + /// should make hostname unique for its purpose. + /// + /// # Example + /// ```no_run + /// use libmdns::Responder; + /// + /// # use std::io; + /// # fn main() -> io::Result<()> { + /// let rt = tokio::runtime::Builder::new_current_thread().build().unwrap(); + /// let handle = rt.handle().clone(); + /// let responder = Responder::spawn_with_ip_list_and_hostname(&handle, Vec::new(), "myUniqueName".to_owned())?; + /// # Ok(()) + /// # } + /// ``` + pub fn spawn_with_ip_list_and_hostname( + handle: &Handle, + allowed_ips: Vec, + hostname: String, + ) -> io::Result { + let (responder, task) = + Self::with_default_handle_and_ip_list_and_hostname(allowed_ips, hostname)?; + handle.spawn(task); + Ok(responder) + } + /// Spawn a `Responder` on the default tokio handle. pub fn with_default_handle() -> io::Result<(Responder, ResponderTask)> { Self::with_default_handle_and_ip_list(Vec::new()) @@ -107,10 +154,29 @@ impl Responder { pub fn with_default_handle_and_ip_list( allowed_ips: Vec, ) -> io::Result<(Responder, ResponderTask)> { - let mut hostname = hostname::get()?.into_string().map_err(|_| { + let hostname = hostname::get()?.into_string().map_err(|_| { io::Error::new(io::ErrorKind::InvalidData, "Hostname not valid unicode") })?; + Self::default_handle(allowed_ips, hostname) + } + + /// Spawn a `Responder` on the default tokio handle. + /// DNS response records will have the reported IPs limited to those passed in here. + /// This can be particularly useful on machines with lots of networks created by tools such as docker. + /// And SRV field will have specified hostname instead of system hostname. + /// This can be particularly useful if the platform has the fixed hostname and the application + /// should make hostname unique for its purpose. + pub fn with_default_handle_and_ip_list_and_hostname( + allowed_ips: Vec, + hostname: String, + ) -> io::Result<(Responder, ResponderTask)> { + Self::default_handle(allowed_ips, hostname) + } + fn default_handle( + allowed_ips: Vec, + mut hostname: String, + ) -> io::Result<(Responder, ResponderTask)> { if !hostname.ends_with(".local") { hostname.push_str(".local"); } @@ -136,7 +202,7 @@ impl Responder { let commands = CommandSender(commands); let responder = Responder { - services: services, + services, commands: RefCell::new(commands.clone()), shutdown: Arc::new(Shutdown(commands)), };