hs-libp2p-0.2.0.0: Haskell implementation of the libp2p networking stack
Safe HaskellNone
LanguageGHC2021

LibP2P.Discovery.MDNS

Description

mDNS local peer discovery (specsdiscoverymdns.md), the zero-configuration discovery mechanism for peers on one link.

Peers multicast DNS messages to the 224.0.0.251:5353 (IPv4) / ff02::fb:5353 (IPv6) groups under the DNS-SD service name _p2p._udp.local:

  • a peer starting up queries _p2p._udp.local PTR; every peer (itself included — it hears its own query via multicast loopback) answers with a PTR record naming its instance and, as additional records, TXT records carrying its dialable addresses as dnsaddr=... strings;
  • queries coming from the mDNS port are answered on the multicast group; legacy-unicast queries (other source ports, RFC 6762 §6.7) are answered unicast, with their transaction id kept;
  • peers also announce unsolicited responses at start-up and periodically, so a peer that only listens still hears its neighbours;
  • on shutdown a goodbye (records with TTL 0) is sent.

Service name, port and announcement cadence are on MDNSConfig; the defaults track the spec, while tests pin a different service name and port to stay independent of any real mDNS responder on the machine.

The DNS wire format (header, questions, records, name compression) is implemented in LibP2P.Discovery.MDNS.Message. Discovered peers are delivered as DiscoveryEvent on the service's STM queue, the same shape the Rendezvous module uses (see "LibP2P.Discovery.Rendezvous").

Platform note: multicast group membership and the multicast socket options live in Network.Socket.Options as CustomSockOpts on network >= 3.2 (the named constants were removed upstream); the option numbers used here are the Linux ones.

Synopsis

Configuration

data MDNSConfig Source #

mDNS service settings; defaultMDNSConfig tracks the spec (service _p2p._udp.local, port 5353, both address families).

Constructors

MDNSConfig 

Fields

  • mcServiceName :: !Text

    DNS-SD service name; the default is _p2p._udp.local. A private network (pnet) would use _p2p-fingerprint._udp.local.

  • mcPort :: !Word16

    UDP port; the default is 5353.

  • mcAnnounceTTL :: !Word32

    TTL carried by this service's records, in seconds. Records are re-announced, so this only bounds how long peers remember us if we vanish unannounced; the default (4500s) matches RFC 6762 §17.

  • mcAdvertiseInterval :: !Int

    Seconds between periodic re-announcements; 0 disables them.

  • mcAnnounceCount :: !Int

    How many times the start-up announcement is repeated, spaced by mcAnnounceSpacingMs (multicast is lossy).

  • mcAnnounceSpacingMs :: !Int
     
  • mcMulticastLoopback :: !Bool

    Whether our own multicasts loop back to us. On, peers sharing the host (and every test) discover each other; our own query coming back is answered by ourselves, which processResponse filters out.

  • mcBindIPv6 :: !Bool

    Whether to open the IPv6 (ff02::fb) socket in addition to the IPv4 one. Failing one stack does not fail the service.

Instances

Instances details
Show MDNSConfig Source # 
Instance details

Defined in LibP2P.Discovery.MDNS

Eq MDNSConfig Source # 
Instance details

Defined in LibP2P.Discovery.MDNS

Service

data MDNSService Source #

A running mDNS discovery service.

Constructors

MDNSService 

Fields

data MDNSBackend Source #

Injectable socket and peer-name operations used by deterministic tests and by platforms that need custom multicast setup.

data MDNSSocket Source #

A socket joined to its mDNS group. skGroup carries the family and the group address so sends and replies stay on the socket's own stack.

Constructors

MDNSSocket 

Fields

newMDNSService :: Switch -> MDNSConfig -> IO (Either String MDNSService) Source #

Start the mDNS service: open the multicast sockets (IPv4, and IPv6 if configured and possible), join the groups, and start the responder threads. Returns Left only when no socket could be opened at all; failing one stack (an IPv4-only host, for example) degrades the service to the other.

stopMDNSService :: MDNSService -> IO () Source #

Stop the service: send the goodbye (records with TTL 0), cancel the threads, close the sockets.

Well-known mDNS constants

mdnsIPv4Group :: ByteString Source #

224.0.0.251 — the IPv4 mDNS group (RFC 6762 §4).

mdnsIPv6Group :: ByteString Source #

ff02::fb — the IPv6 (link-local) mDNS group (RFC 6762 §4).

mdnsPort :: Word16 Source #

5353 — the mDNS UDP port (RFC 6762 §6).

Discovery events (shared with the Rendezvous module)

data DiscoveryEvent Source #

A peer found by a discovery mechanism, with the addresses it advertised. Deduplication is the consumer's job, not the queue's: discovery runs concurrently with the Switch's peer store, and suppressing re-announcements would hide refreshed address lists. A TQueue is unbounded, so producing never blocks; applications are expected to drain or poll at their own cadence.

Constructors

DiscoveryEvent 

Fields

newDiscoveryQueue :: IO (TQueue DiscoveryEvent) Source #

Create a discovery event queue.

tryReadDiscoveryQueue :: TQueue DiscoveryEvent -> IO (Maybe DiscoveryEvent) Source #

Read one event if any are queued.

drainDiscoveryQueue :: TQueue DiscoveryEvent -> IO [DiscoveryEvent] Source #

Drain every currently queued event, in order.