diff --git a/CHANGELOG.md b/CHANGELOG.md index 3245357..4e94cf1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# Version 0.2.1 + +- mDNS daemon respond socket to be blocking for simpler send. + # Version 0.2.0 - Public API internally to use the unblocking try_send() to replace send(). diff --git a/Cargo.toml b/Cargo.toml index 036a818..8a418b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mdns-sd" -version = "0.2.0" +version = "0.2.1" authors = ["keepsimple "] edition = "2018" license = "Apache-2.0 OR MIT" diff --git a/src/lib.rs b/src/lib.rs index ed8da9c..2479f4c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -503,7 +503,8 @@ impl ServiceDaemon { } /// Creates a new UDP socket to bind to `port` with REUSEPORT option. -fn new_socket(port: u16) -> Result { +/// `non_block` indicates whether to set O_NONBLOCK for the socket. +fn new_socket(port: u16, non_block: bool) -> Result { let fd = socket( AddressFamily::Inet, SockType::Datagram, @@ -517,8 +518,10 @@ fn new_socket(port: u16) -> Result { setsockopt(fd, sockopt::ReusePort, &true) .map_err(|e| e_fmt!("nix::sys::setsockopt ReusePort failed: {}", e))?; - fcntl::fcntl(fd, fcntl::FcntlArg::F_SETFL(fcntl::OFlag::O_NONBLOCK)) - .map_err(|e| e_fmt!("nix::fcntl O_NONBLOCK: {}", e))?; + if non_block { + fcntl::fcntl(fd, fcntl::FcntlArg::F_SETFL(fcntl::OFlag::O_NONBLOCK)) + .map_err(|e| e_fmt!("nix::fcntl O_NONBLOCK: {}", e))?; + } let ipv4_any = IpAddr::new_v4(0, 0, 0, 0); let inet_addr = InetAddr::new(ipv4_any, port); @@ -570,7 +573,7 @@ struct Zeroconf { impl Zeroconf { fn new(udp_port: u16) -> Result { - let listen_socket = new_socket(udp_port)?; + let listen_socket = new_socket(udp_port, true)?; debug!("created listening socket: {}", &listen_socket); let group_addr = Ipv4Addr::new(224, 0, 0, 251); @@ -581,7 +584,7 @@ impl Zeroconf { // We are not setting specific outgoing interface for this socket. // It will use the default outgoing interface set by the OS. let mut respond_sockets = Vec::new(); - let respond_socket = new_socket(udp_port)?; + let respond_socket = new_socket(udp_port, false)?; respond_sockets.push(respond_socket); let broadcast_addr =