Skip to content

Commit

Permalink
prepare for a new release (#24)
Browse files Browse the repository at this point in the history
* update nix crate to 0.24.1
  • Loading branch information
keepsimple1 authored May 18, 2022
1 parent 7c53e87 commit 92b951e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Version 0.5.0

- Allow multiple formats for host_ipv4 to create ServiceInfo.
- A breaking change: change `ServiceInfo::new()` to return a `Result<>`.
- Update `nix` dependency to version 0.24.1.

# Version 0.4.3

- Fix a bug in stop-browse
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mdns-sd"
version = "0.4.3"
version = "0.5.0"
authors = ["keepsimple <[email protected]>"]
edition = "2018"
license = "Apache-2.0 OR MIT"
Expand All @@ -17,4 +17,4 @@ default = ["async"]
[dependencies]
flume = { version = "0.10", default-features = false } # channel between threads
log = "0.4.14" # logging
nix = "0.23.1" # socket APIs
nix = "0.24.1" # socket APIs
47 changes: 21 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ use nix::{
sys::{
select::{select, FdSet},
socket::{
bind, recvfrom, sendto, setsockopt, socket, sockopt, AddressFamily, InetAddr, IpAddr,
IpMembershipRequest, Ipv4Addr, MsgFlags, SockAddr, SockFlag, SockType,
bind, recvfrom, sendto, setsockopt, socket, sockopt, AddressFamily,
IpMembershipRequest, MsgFlags, SockFlag, SockType, SockaddrIn,
},
time::{TimeVal, TimeValLike},
},
Expand All @@ -151,6 +151,7 @@ use std::{
collections::{HashMap, HashSet},
convert::TryInto,
fmt,
net::Ipv4Addr,
os::unix::io::RawFd,
str::{self, FromStr},
thread,
Expand Down Expand Up @@ -599,12 +600,10 @@ fn new_socket(port: u16, non_block: bool) -> Result<RawFd> {
.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);
bind(fd, &SockAddr::Inet(inet_addr))
.map_err(|e| e_fmt!("nix::sys::socket::bind failed: {}", e))?;
let ipv4_any = SockaddrIn::new(0, 0, 0, 0, port);
bind(fd, &ipv4_any).map_err(|e| e_fmt!("nix::sys::socket::bind failed: {}", e))?;

debug!("new socket {} bind to {}", &fd, &inet_addr);
debug!("new socket {} bind to {}", &fd, &ipv4_any);
Ok(fd)
}

Expand Down Expand Up @@ -638,7 +637,7 @@ struct Zeroconf {
my_services: HashMap<String, ServiceInfo>,

/// Well-known mDNS IPv4 address and port
broadcast_addr: SockAddr,
broadcast_addr: SockaddrIn,

cache: DnsCache,

Expand Down Expand Up @@ -670,8 +669,7 @@ impl Zeroconf {
let respond_socket = new_socket(udp_port, false)?;
respond_sockets.push(respond_socket);

let broadcast_addr =
SockAddr::new_inet(InetAddr::new(IpAddr::new_v4(224, 0, 0, 251), MDNS_PORT));
let broadcast_addr = SockaddrIn::new(224, 0, 0, 251, MDNS_PORT);

Ok(Self {
listen_socket,
Expand Down Expand Up @@ -831,7 +829,7 @@ impl Zeroconf {
}

/// Sends an outgoing packet, and returns the packet bytes.
fn send(&self, out: &DnsOutgoing, addr: &SockAddr) -> Vec<u8> {
fn send(&self, out: &DnsOutgoing, addr: &SockaddrIn) -> Vec<u8> {
let qtype = if out.is_query() { "query" } else { "response" };
debug!(
"Sending {} to {}: {} questions {} answers {} authorities {} additional",
Expand All @@ -852,7 +850,7 @@ impl Zeroconf {
packet
}

fn send_packet(&self, packet: &[u8], addr: &SockAddr) {
fn send_packet(&self, packet: &[u8], addr: &SockaddrIn) {
for s in self.respond_sockets.iter() {
match sendto(*s, packet, addr, MsgFlags::empty()) {
Ok(sz) => debug!("sent out {} bytes on socket {}", sz, s),
Expand Down Expand Up @@ -1087,7 +1085,7 @@ impl Zeroconf {

/// Deal with incoming response packets. All answers
/// are held in the cache, and listeners are notified.
fn handle_response(&mut self, mut msg: DnsIncoming, src: &SockAddr) {
fn handle_response(&mut self, mut msg: DnsIncoming, src: &SockaddrIn) {
debug!(
"handle_response from {}: {} answers {} authorities {} additionals",
src, &msg.num_answers, &msg.num_authorities, &msg.num_additionals
Expand Down Expand Up @@ -1116,7 +1114,7 @@ impl Zeroconf {
}
}

fn handle_query(&mut self, msg: DnsIncoming, addr: &SockAddr) {
fn handle_query(&mut self, msg: DnsIncoming, addr: &SockaddrIn) {
debug!("handle_query from {}", &addr);
let mut out = DnsOutgoing::new(FLAGS_QR_RESPONSE | FLAGS_AA);

Expand Down Expand Up @@ -1391,6 +1389,7 @@ impl DnsCache {
}
}

/// This trait allows for parsing an input into a set of one or multiple [`Ipv4Addr`].
pub trait AsIpv4Addrs {
fn as_ipv4_addrs(&self) -> Result<HashSet<Ipv4Addr>>;
}
Expand All @@ -1401,6 +1400,8 @@ impl<T: AsIpv4Addrs> AsIpv4Addrs for &T {
}
}

/// Supports one address or multiple addresses separated by `,`.
/// For example: "127.0.0.1,127.0.0.2"
impl AsIpv4Addrs for &str {
fn as_ipv4_addrs(&self) -> Result<HashSet<Ipv4Addr>> {
let mut addrs = HashSet::new();
Expand All @@ -1412,8 +1413,7 @@ impl AsIpv4Addrs for &str {

for addr in iter {
let addr = addr.map_err(|err| Error::ParseIpAddr(err.to_string()))?;

addrs.insert(Ipv4Addr::from_std(&addr));
addrs.insert(addr);
}

Ok(addrs)
Expand All @@ -1426,6 +1426,7 @@ impl AsIpv4Addrs for String {
}
}

/// Support slice. Example: &["127.0.0.1", "127.0.0.2"]
impl<I: AsIpv4Addrs> AsIpv4Addrs for &[I] {
fn as_ipv4_addrs(&self) -> Result<HashSet<Ipv4Addr>> {
let mut addrs = HashSet::new();
Expand All @@ -1446,19 +1447,10 @@ impl AsIpv4Addrs for () {
}
}

impl AsIpv4Addrs for Ipv4Addr {
fn as_ipv4_addrs(&self) -> Result<HashSet<Ipv4Addr>> {
let mut ips = HashSet::new();
ips.insert(*self);

Ok(ips)
}
}

impl AsIpv4Addrs for std::net::Ipv4Addr {
fn as_ipv4_addrs(&self) -> Result<HashSet<Ipv4Addr>> {
let mut ips = HashSet::new();
ips.insert(Ipv4Addr::from_std(self));
ips.insert(*self);

Ok(ips)
}
Expand Down Expand Up @@ -1491,6 +1483,9 @@ impl ServiceInfo {
/// `my_name` is the instance name, without the service type suffix.
/// `properties` are optional key/value pairs for the service.
///
/// `host_ipv4` can be one or more IPv4 addresses, in a type that implements
/// [`AsIpv4Addrs`] trait.
///
/// The host TTL and other TTL are set to default values.
pub fn new<Ip: AsIpv4Addrs>(
ty_domain: &str,
Expand Down
30 changes: 15 additions & 15 deletions tests/addr_parse.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use mdns_sd::AsIpv4Addrs;
use nix::sys::socket::Ipv4Addr;
use std::collections::HashSet;
use std::net::Ipv4Addr;

#[test]
fn test_addr_str() {
assert_eq!(
"127.0.0.1".as_ipv4_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::from_std(&std::net::Ipv4Addr::new(127, 0, 0, 1)));
set.insert(Ipv4Addr::new(127, 0, 0, 1));

set
})
Expand All @@ -19,7 +19,7 @@ fn test_addr_str() {
addr.as_ipv4_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::from_std(&std::net::Ipv4Addr::new(127, 0, 0, 1)));
set.insert(Ipv4Addr::new(127, 0, 0, 1));

set
})
Expand All @@ -30,7 +30,7 @@ fn test_addr_str() {
(&addr).as_ipv4_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::from_std(&std::net::Ipv4Addr::new(127, 0, 0, 1)));
set.insert(Ipv4Addr::new(127, 0, 0, 1));

set
})
Expand All @@ -40,8 +40,8 @@ fn test_addr_str() {
"127.0.0.1,127.0.0.2".as_ipv4_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::from_std(&std::net::Ipv4Addr::new(127, 0, 0, 1)));
set.insert(Ipv4Addr::from_std(&std::net::Ipv4Addr::new(127, 0, 0, 2)));
set.insert(Ipv4Addr::new(127, 0, 0, 1));
set.insert(Ipv4Addr::new(127, 0, 0, 2));

set
})
Expand All @@ -54,7 +54,7 @@ fn test_addr_slice() {
(&["127.0.0.1"][..]).as_ipv4_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::from_std(&std::net::Ipv4Addr::new(127, 0, 0, 1)));
set.insert(Ipv4Addr::new(127, 0, 0, 1));

set
})
Expand All @@ -64,8 +64,8 @@ fn test_addr_slice() {
(&["127.0.0.1", "127.0.0.2"][..]).as_ipv4_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::from_std(&std::net::Ipv4Addr::new(127, 0, 0, 1)));
set.insert(Ipv4Addr::from_std(&std::net::Ipv4Addr::new(127, 0, 0, 2)));
set.insert(Ipv4Addr::new(127, 0, 0, 1));
set.insert(Ipv4Addr::new(127, 0, 0, 2));

set
})
Expand All @@ -75,8 +75,8 @@ fn test_addr_slice() {
(&vec!["127.0.0.1", "127.0.0.2"][..]).as_ipv4_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::from_std(&std::net::Ipv4Addr::new(127, 0, 0, 1)));
set.insert(Ipv4Addr::from_std(&std::net::Ipv4Addr::new(127, 0, 0, 2)));
set.insert(Ipv4Addr::new(127, 0, 0, 1));
set.insert(Ipv4Addr::new(127, 0, 0, 2));

set
})
Expand All @@ -85,23 +85,23 @@ fn test_addr_slice() {

#[test]
fn test_addr_ip() {
let ip = std::net::Ipv4Addr::new(127, 0, 0, 1);
let ip = Ipv4Addr::new(127, 0, 0, 1);

assert_eq!(
ip.as_ipv4_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::from_std(&std::net::Ipv4Addr::new(127, 0, 0, 1)));
set.insert(Ipv4Addr::new(127, 0, 0, 1));

set
})
);

assert_eq!(
Ipv4Addr::from_std(&ip).as_ipv4_addrs(),
(&ip).as_ipv4_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::from_std(&std::net::Ipv4Addr::new(127, 0, 0, 1)));
set.insert(Ipv4Addr::new(127, 0, 0, 1));

set
})
Expand Down

0 comments on commit 92b951e

Please sign in to comment.