pub struct MulticastSocket { /* private fields */ }Expand description
A builder for creating multicast UDP sockets suitable for mDNS.
MulticastSocket provides a convenient way to create properly configured
UDP sockets for mDNS communication. The resulting socket will be:
- Bound to the specified address (typically
0.0.0.0:5353) - Configured with
SO_REUSEADDRenabled - Configured with
SO_REUSEPORTenabled (on supported platforms) - Set to non-blocking mode for async compatibility
- Joined to the mDNS multicast group (224.0.0.251)
- Sending with IP TTL 255, multicast and unicast (RFC 6762 section 11)
§Examples
Basic usage with tokio:
use rtci_mdns::MulticastSocket;
use std::net::SocketAddr;
let bind_addr: SocketAddr = "0.0.0.0:5353".parse().unwrap();
let std_socket = MulticastSocket::new(bind_addr).into_std()?;
let socket = tokio::net::UdpSocket::from_std(std_socket)?;With a specific network interface:
use rtci_mdns::MulticastSocket;
use std::net::{Ipv4Addr, SocketAddr};
let bind_addr: SocketAddr = "0.0.0.0:5353".parse().unwrap();
let interface = Ipv4Addr::new(192, 168, 1, 100);
let std_socket = MulticastSocket::new(bind_addr)
.with_interface(interface)
.into_std()?;Implementations§
Source§impl MulticastSocket
impl MulticastSocket
Sourcepub fn with_multicast_local_ipv4(self, multicast_local_ipv4: Ipv4Addr) -> Self
pub fn with_multicast_local_ipv4(self, multicast_local_ipv4: Ipv4Addr) -> Self
Sets the local IPv4 address to bind the multicast socket to.
Defaults to unspecified (0.0.0.0), letting the OS choose.
Sourcepub fn with_multicast_local_port(self, multicast_local_port: u16) -> Self
pub fn with_multicast_local_port(self, multicast_local_port: u16) -> Self
Sets the local port to bind the multicast socket to.
Defaults to the mDNS port (5353), which is required to receive multicast queries from other hosts; a different port is only useful for testing.
Sourcepub fn with_interface(self, interface: Ipv4Addr) -> Self
pub fn with_interface(self, interface: Ipv4Addr) -> Self
Sets a specific network interface for multicast operations.
If not set, the socket joins the multicast group on all interfaces
(INADDR_ANY).
With an interface the socket also sends by it (IP_MULTICAST_IF, RFC 6762 section 14).
Without one, on Linux, macOS and other systems, it sends by the system’s route for
224.0.0.251. On Windows that route comes from the destination cache, and a process that
sent to the group from 127.0.0.1 leaves it on loopback after it exits, so on Windows a
socket built without an interface (and without a local address of its own,
with_multicast_local_ipv4) joins and sends on the
interface of the default route instead (see into_std). On a Windows host with
several interfaces whose mDNS link is not the default route’s, pass the interface of
the address the application publishes.
§Arguments
interface- The IPv4 address of the network interface to use.
§Example
use rtci_mdns::MulticastSocket;
use std::net::Ipv4Addr;
let builder = MulticastSocket::new()
.with_interface(Ipv4Addr::new(192, 168, 1, 100));Sourcepub fn into_std(self) -> Result<UdpSocket>
pub fn into_std(self) -> Result<UdpSocket>
Converts this builder into a configured std::net::UdpSocket.
This method creates the socket with the following configuration:
SO_REUSEADDRenabled (allows multiple processes to bind)SO_REUSEPORTenabled on Unix platforms (except Solaris/illumos)- Non-blocking mode enabled (for async compatibility)
- Joined to the mDNS multicast group (224.0.0.251), on
with_interface’s interface when one is given, and sending by that interface (IP_MULTICAST_IF), whatever the system’s route for the group says - Without an interface: on Windows, unless
with_multicast_local_ipv4names a local address, joined and sending on the interface of the default route (the address a UDP socket connected to 192.0.2.1, a documentation address, takes; nothing is sent there), as ifwith_interfacehad named it: the system’s route for the group, which both a join onINADDR_ANYand a send withoutIP_MULTICAST_IFfollow there, is left on loopback for the whole host by any process that sent to the group from 127.0.0.1. Where the default route’s address is unspecified or loopback, or there is none, and on other systems: joined on the default interface (INADDR_ANY), sending by the system’s route for the group - IP TTL 255 on what it sends (RFC 6762 section 11)
§Errors
Returns an error if:
- Socket creation fails
- Setting socket options fails
- Binding to the address fails
- Joining the multicast group fails
§Example
use rtci_mdns::MulticastSocket;
use std::net::SocketAddr;
let bind_addr: SocketAddr = "0.0.0.0:5353".parse().unwrap();
let std_socket = MulticastSocket::new(bind_addr).into_std()?;
// Use with tokio:
let socket = tokio::net::UdpSocket::from_std(std_socket)?;§Platform Notes
- On Unix-like systems (except Solaris/illumos),
SO_REUSEPORTis enabled to allow multiple processes to bind to the same port.