Skip to content

Commit

Permalink
Store different socket families in different net::Address variants (c…
Browse files Browse the repository at this point in the history
…loses #224)
  • Loading branch information
svartalf committed May 28, 2020
1 parent 94c0f78 commit a65ab50
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
8 changes: 5 additions & 3 deletions heim-net/src/nic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use crate::sys;
#[non_exhaustive]
pub enum Address {
/// IPv4 Internet protocols
Inet(net::SocketAddr),
Inet(net::SocketAddrV4),

/// IPv6 Internet protocols
Inet6(net::SocketAddr),
Inet6(net::SocketAddrV6),

/// Link level interface
Link(macaddr::MacAddr),
Expand Down Expand Up @@ -89,5 +89,7 @@ impl fmt::Debug for Nic {
///
/// [Network Interface Cards]: struct.Nic.html
pub async fn nic() -> Result<impl Stream<Item = Result<Nic>>> {
Ok(sys::nic().map_ok(Into::into))
let inner = sys::nic().await?;

Ok(inner.map_ok(Into::into))
}
32 changes: 13 additions & 19 deletions heim-net/src/sys/unix/nic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::net::SocketAddr;

use macaddr::MacAddr;
use nix::ifaddrs;
use nix::net::if_::InterfaceFlags;
Expand Down Expand Up @@ -60,35 +62,27 @@ impl Nic {
}
}

pub fn nic() -> impl Stream<Item = Result<Nic>> {
future::lazy(|_| {
// `nix::ifaddrs` structs are not safe to send between threads,
// so collecting them in a once
// TODO: Can we Pin them maybe?
let iter = ifaddrs::getifaddrs()?;
let interfaces = iter.collect::<Vec<_>>();

Ok(stream::iter(interfaces).map(Ok))
})
.try_flatten_stream()
.try_filter_map(|addr: ifaddrs::InterfaceAddress| {
// Skipping unsupported address families
let result = if addr.address.is_some() {
Some(Nic(addr))
pub async fn nic() -> Result<impl Stream<Item = Result<Nic>>> {
let iter = ifaddrs::getifaddrs()?.filter_map(|addr| {
if addr.address.is_some() {
Some(Ok(Nic(addr)))
} else {
None
};
}
});

future::ok(result)
})
Ok(stream::iter(iter))
}

impl From<&socket::SockAddr> for Address {
fn from(s: &socket::SockAddr) -> Self {
use nix::sys::socket::SockAddr::*;

match *s {
Inet(addr) => Address::Inet(addr.to_std()),
Inet(addr) => match addr.to_std() {
SocketAddr::V4(addr) => Address::Inet(addr),
SocketAddr::V6(addr) => Address::Inet6(addr),
},
Link(addr) => Address::Link(MacAddr::from(addr.addr())),
other => unimplemented!("Unknown sockaddr: {:?}", other),
}
Expand Down
4 changes: 2 additions & 2 deletions heim-net/src/sys/windows/nic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Nic {
}
}

pub fn nic() -> impl Stream<Item = Result<Nic>> {
pub async fn nic() -> Result<impl Stream<Item = Result<Nic>>> {
// TODO: Stub
stream::iter(vec![])
Ok(stream::empty())
}

0 comments on commit a65ab50

Please sign in to comment.