Skip to content

Commit

Permalink
Test that the socket is working before we return it
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamVenner committed Jan 8, 2023
1 parent 505d711 commit dba52c9
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,16 @@ impl MdnsSocket<UdpSocket> {
MDNS_PORT,
)))?;

// Make sure the socket works
socket.set_multicast_if_v4(&Ipv4Addr::UNSPECIFIED)?; // Set to default interface
socket.send_to(&[0], &SocketAddrV4::new(MDNS_V4_IP, MDNS_PORT).into())?; // Send a multicast packet

// If we're only using one interface, set it as the default
if ifaces.len() == 1 {
let addr = ifaces.iter().next().unwrap();
socket.set_multicast_if_v4(addr)?;
}

socket.set_nonblocking(true)?;

Ok(Self::V4(InterfacedMdnsSocket::new(socket.into(), ifaces)))
}

Expand Down Expand Up @@ -197,13 +200,16 @@ impl MdnsSocket<UdpSocket> {
MDNS_PORT,
)))?;

// Make sure the socket works
socket.set_multicast_if_v6(0)?; // Set to default interface
socket.send_to(&[0], &SocketAddr::new(IpAddr::V6(MDNS_V6_IP), MDNS_PORT).into())?; // Send a multicast packet

// If we're only using one interface, set it as the default
if ifaces.len() == 1 {
let iface = ifaces.iter().next().unwrap();
socket.set_multicast_if_v6(iface.as_u32())?;
}

socket.set_nonblocking(true)?;

Ok(Self::V6(InterfacedMdnsSocket::new(socket.into(), ifaces)))
}

Expand Down Expand Up @@ -350,9 +356,17 @@ where
{
fn into_async(self) -> Result<InterfacedMdnsSocket<AsyncUdpSocket, Iface>, std::io::Error> {
Ok(match self {
Self::UniInterface(socket) => InterfacedMdnsSocket::UniInterface(AsyncUdpSocket::from_std(socket)?),
Self::UniInterface(socket) => {
socket.set_nonblocking(true)?;
InterfacedMdnsSocket::UniInterface(AsyncUdpSocket::from_std(socket)?)
}

Self::MultiInterface { socket, ifaces } => InterfacedMdnsSocket::MultiInterface {
socket: AsyncUdpSocket::from_std(socket)?,
socket: {
socket.set_nonblocking(true)?;
AsyncUdpSocket::from_std(socket)?
},

ifaces,
},
})
Expand Down

0 comments on commit dba52c9

Please sign in to comment.