Skip to content

Commit

Permalink
Expose Build options publically
Browse files Browse the repository at this point in the history
When trying to use the windows version of the socket, I could not build
with the options.

This was an error where only the *Nix version had public fields to edit.

To fix this, the build options has been promoted to the root of the
crate, so it is used on both systems and they end up with the same
config.

This commit also adds an example on the mdns test to ensure it compiles
using the public methods available on all platforms.

It also changes the defaults of build options to listen to multicast
messages, as it is very common to have multiple processes listening on
the same pc.
  • Loading branch information
bltavares committed Jul 5, 2020
1 parent e7a446f commit 663b65f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 38 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ multicast-socket = { git = "https://github.com/bltavares/multicast-socket" }

*Why not use a version/publish crates.io*: Currently it is using unpublished version of some packages (git versions), which are not allowed when publishing to crates.io. When the new versions are published, this could also be published.

## Targets

Main tier:

- x86_64-unknown-linux-gnu
- x86_64-pc-windows-msvc
- x86_64-apple-darwin

Cross tier:

- armv7-unknown-linux-gnueabihf
- aarch64-linux-android
- mips-unknown-linux-musl
- x86_64-unknown-linux-musl
- aarch64-unknown-linux-gnu

## License

Expand Down
11 changes: 11 additions & 0 deletions examples/mdns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ use std::net::SocketAddrV4;

fn main() {
let mdns_multicast_address = SocketAddrV4::new([224, 0, 0, 251].into(), 5353);

// Validate that building with options works with the public API
let with_options = MulticastSocket::with_options(
mdns_multicast_address,
multicast_socket::all_ipv4_interfaces().unwrap(),
multicast_socket::MulticastOptions {
..Default::default()
},
);
drop(with_options);

let socket = MulticastSocket::all_interfaces(mdns_multicast_address)
.expect("could not create and bind socket");

Expand Down
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

#[cfg(windows)]
mod win;
#[cfg(windows)]
Expand All @@ -7,3 +9,19 @@ pub use win::*;
mod unix;
#[cfg(not(windows))]
pub use unix::*;

pub struct MulticastOptions {
pub read_timeout: Duration,
pub loopback: bool,
pub buffer_size: usize,
}

impl Default for MulticastOptions {
fn default() -> Self {
MulticastOptions {
read_timeout: Duration::from_secs(1),
loopback: true,
buffer_size: 512,
}
}
}
21 changes: 2 additions & 19 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,14 @@ use std::io;
use std::mem;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::os::unix::io::AsRawFd;
use std::time::Duration;

use socket2::{Domain, Protocol, Socket, Type};

use nix::sys::socket as sock;
use nix::sys::uio::IoVec;

pub struct MulticastOptions {
pub read_timeout: Duration,
pub loopback: bool,
pub buffer_size: usize,
}

impl Default for MulticastOptions {
fn default() -> Self {
MulticastOptions {
read_timeout: Duration::from_millis(100),
loopback: false,
buffer_size: 512,
}
}
}

fn create_on_interfaces(
options: MulticastOptions,
options: crate::MulticastOptions,
interfaces: Vec<Ipv4Addr>,
multicast_address: SocketAddrV4,
) -> io::Result<MulticastSocket> {
Expand Down Expand Up @@ -134,7 +117,7 @@ impl MulticastSocket {
pub fn with_options(
multicast_address: SocketAddrV4,
interfaces: Vec<Ipv4Addr>,
options: MulticastOptions,
options: crate::MulticastOptions,
) -> io::Result<Self> {
create_on_interfaces(options, interfaces, multicast_address)
}
Expand Down
21 changes: 2 additions & 19 deletions src/win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::mem;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::os::windows::prelude::*;
use std::ptr;
use std::time::Duration;

use socket2::{Domain, Protocol, Socket, Type};

Expand Down Expand Up @@ -131,24 +130,8 @@ fn set_pktinfo(socket: RawSocket, payload: bool) -> io::Result<()> {
unsafe { setsockopt(socket, IPPROTO_IP, IP_PKTINFO, payload as c_int) }
}

pub struct MulticastOptions {
pub read_timeout: Duration,
loopback: bool,
buffer_size: usize,
}

impl Default for MulticastOptions {
fn default() -> Self {
MulticastOptions {
read_timeout: Duration::from_millis(100),
loopback: false,
buffer_size: 512,
}
}
}

fn create_on_interfaces(
options: MulticastOptions,
options: crate::MulticastOptions,
interfaces: Vec<Ipv4Addr>,
multicast_address: SocketAddrV4,
) -> io::Result<MulticastSocket> {
Expand Down Expand Up @@ -228,7 +211,7 @@ impl MulticastSocket {
pub fn with_options(
multicast_address: SocketAddrV4,
interfaces: Vec<Ipv4Addr>,
options: MulticastOptions,
options: crate::MulticastOptions,
) -> io::Result<Self> {
create_on_interfaces(options, interfaces, multicast_address)
}
Expand Down

0 comments on commit 663b65f

Please # to comment.