Skip to main content

MulticastSocket

Struct MulticastSocket 

Source
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_REUSEADDR enabled
  • Configured with SO_REUSEPORT enabled (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

Source

pub fn new() -> Self

Creates a new MulticastSocket builder with the specified bind address.

§Arguments
  • bind_addr - The local address to bind to. Use 0.0.0.0:5353 to listen on all interfaces on the standard mDNS port.
§Example
use rtci_mdns::MulticastSocket;

let builder = MulticastSocket::new();
Source

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.

Source

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.

Source

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));
Source

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_REUSEADDR enabled (allows multiple processes to bind)
  • SO_REUSEPORT enabled 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_ipv4 names 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 if with_interface had named it: the system’s route for the group, which both a join on INADDR_ANY and a send without IP_MULTICAST_IF follow 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_REUSEPORT is enabled to allow multiple processes to bind to the same port.

Trait Implementations§

Source§

impl Clone for MulticastSocket

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MulticastSocket

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MulticastSocket

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.