Skip to content

Commit

Permalink
allow non_block as option in new_socket (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
keepsimple1 authored Dec 21, 2021
1 parent d899c05 commit 0263f0e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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().
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mdns-sd"
version = "0.2.0"
version = "0.2.1"
authors = ["keepsimple <[email protected]>"]
edition = "2018"
license = "Apache-2.0 OR MIT"
Expand Down
13 changes: 8 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ impl ServiceDaemon {
}

/// Creates a new UDP socket to bind to `port` with REUSEPORT option.
fn new_socket(port: u16) -> Result<RawFd> {
/// `non_block` indicates whether to set O_NONBLOCK for the socket.
fn new_socket(port: u16, non_block: bool) -> Result<RawFd> {
let fd = socket(
AddressFamily::Inet,
SockType::Datagram,
Expand All @@ -517,8 +518,10 @@ fn new_socket(port: u16) -> Result<RawFd> {
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);
Expand Down Expand Up @@ -570,7 +573,7 @@ struct Zeroconf {

impl Zeroconf {
fn new(udp_port: u16) -> Result<Self> {
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);
Expand All @@ -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 =
Expand Down

0 comments on commit 0263f0e

Please sign in to comment.