Skip to main content

Crate iroh_mdns_address_lookup

Crate iroh_mdns_address_lookup 

Source
Expand description

An address lookup service that uses an mdns-like service to discover and lookup the addresses of local endpoints.

This allows you to use an mdns-like swarm discovery service to find address information about endpoints that are on your local network, no relay or outside internet needed. See the swarm-discovery crate for more details.

When MdnsAddressLookup is enabled, it’s possible to get a list of the locally discovered endpoints by filtering a list of RemoteInfos.

In order to get a list of locally discovered addresses, you must call MdnsAddressLookup::subscribe to subscribe to a stream of discovered addresses.

use iroh::{Endpoint, endpoint::presets};
use iroh_mdns_address_lookup::{DiscoveryEvent, MdnsAddressLookup};
use n0_future::StreamExt;

#[tokio::main]
async fn main() {
    let endpoint = Endpoint::bind(presets::Minimal).await.unwrap();

    // Register the Address Lookupwith the endpoint
    let mdns = MdnsAddressLookup::builder().build(endpoint.id()).unwrap();
    endpoint.address_lookup().unwrap().add(mdns.clone());

    // Subscribe to the mdns discovery events
    let mut events = mdns.subscribe().await;
    while let Some(event) = events.next().await {
        match event {
            DiscoveryEvent::Discovered { endpoint_info, .. } => {
                println!("MDNS discovered: {:?}", endpoint_info);
            }
            DiscoveryEvent::Expired { endpoint_id } => {
                println!("MDNS expired: {endpoint_id}");
            }
            _ => {}
        }
    }
}

§Filtering

By default, MdnsAddressLookup publishes all addresses it receives: direct IP addresses and up to one RelayUrl. The following constraints apply regardless of any user-supplied filter:

  • Only the first RelayUrl in the address set is published.
  • A RelayUrl longer than 249 bytes is silently dropped.

You can supply an AddrFilter via MdnsAddressLookupBuilder::addr_filter to control which addresses are published and in what order. The filter is applied before the constraints above, so for example you can use it to exclude relay URLs entirely or to prioritize certain addresses.

Structs§

MdnsAddressLookup
Address Lookup using swarm-discovery, a variation on mdns.
MdnsAddressLookupBuilder
Builder for MdnsAddressLookup.

Enums§

DiscoveryEvent
An event emitted from the MdnsAddressLookup service.

Constants§

NAME
Name of this address lookup service.