multicast-discovery-socket

Crates.iomulticast-discovery-socket
lib.rsmulticast-discovery-socket
version0.1.3
created_at2025-06-29 13:07:24.036604+00
updated_at2025-07-17 11:40:54.158369+00
descriptionReady to use discovery solution for local networks
homepage
repositoryhttps://github.com/skibon02/multicast-discovery-socket
max_upload_size
id1730748
size78,877
Влад (skibon02)

documentation

README

Multicast discovery socket

crate Docs Apache2/MIT licensed

Documentation

Integrate local client discovery into your rust application with ease!

About

What can it do?

  • Continuously discover local clients AND announce yourself to local networks
  • Hot-change interface detection: automatically detects added/changed network interfaces for stable discovery
  • Support multiple instances on the same host: each application can discover both applications on the same host and on other hosts
  • Keep multicast traffic low: load is proportional to the number of applications on a certain interface (by default 1 UDP packet per 2 seconds)
  • Support multiple ports: built-in support for backup ports for discovery if main port is not available
  • Service port: announcements includes a service port for main communication, making your application independent on bound port
  • Custom advertisement data: any type implementing bincode::Encode + bincode::Decode<()> + Clone

What it cannot do?

  • No support for IPv6. IPv4 is supported only.
  • Work without multicast support. Multicast is required for discovery.

Supported platforms

  • cfg(windows) - Windows OS. Deps: winapi, if_addrs
  • cfg(target_os="linux") - Linux OS. Deps: nix, if_addrs
  • cfg(target_os="android") - Android OS (not tested). Deps: nix, if_addrs
  • other STD - fallback implementation is used (does not support multiple interfaces, only system default interface)

If your platform is not listed here, fallback implementation is used (does not support multiple interfaces, only system default interface)

⚠️ Work in progress

This library works well as a proof of concept, but it is not yet tested enough. You can use it, but you cannot totally rely on it (yet)

Usage

Check the discovery example. Run multiple instances of it on the same host/network and see how they discover each other.

let cfg = MulticastDiscoveryConfig::new(Ipv4Addr::new(239, 37, 37, 37), "multicast-example".into())
    .with_multicast_port(37337)
    .with_backup_ports(62337..62339);

let name = format!("Client {}", rand::random::<u8>());
info!("Running as {name}");
let mut socket = MulticastDiscoverySocket::new(&cfg, Some(12345), name).unwrap();

loop {
    socket.poll(|msg| {
        match msg {
            PollResult::DiscoveredClient {
                addr,
                discover_id,
                adv_data
            } => {
                println!("Discovered client: {} - {:x}: {:?}", addr, discover_id, adv_data);
            }
            PollResult::DisconnectedClient {
                addr,
                discover_id
            } => {
                println!("Disconnected client: {} - {:x}", addr, discover_id);
            }
        }
    });
    thread::sleep(Duration::from_millis(100));
}

License

Licensed under either of

at your option.

References

Support for multiple interfaces is hard to be implemented in cross-platform way.
Windows and Linux support is implemented mostly relying on code from multicast-socket
Thanks to Bruno Tavares for his work and research on multicast support!

Commit count: 0

cargo fmt