| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
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 aPTRrecord naming its instance and, as additional records,TXTrecords carrying its dialable addresses asdnsaddr=...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
- data MDNSConfig = MDNSConfig {
- mcServiceName :: !Text
- mcPort :: !Word16
- mcAnnounceTTL :: !Word32
- mcAdvertiseInterval :: !Int
- mcAnnounceCount :: !Int
- mcAnnounceSpacingMs :: !Int
- mcMulticastLoopback :: !Bool
- mcBindIPv6 :: !Bool
- defaultMDNSConfig :: MDNSConfig
- data MDNSService = MDNSService {
- msConfig :: !MDNSConfig
- msSwitch :: !Switch
- msPeerName :: !Text
- msEvents :: !(TQueue DiscoveryEvent)
- msThreads :: [Async ()]
- msSockets :: [MDNSSocket]
- data MDNSBackend = MDNSBackend {
- mdnsOpenSockets :: !(MDNSConfig -> IO [MDNSSocket])
- mdnsRandomPeerName :: !(IO Text)
- data MDNSSocket = MDNSSocket {
- mdnsGroup :: !SockAddr
- mdnsReceive :: !(IO (ByteString, SockAddr))
- mdnsSend :: !(ByteString -> SockAddr -> IO ())
- mdnsClose :: !(IO ())
- newMDNSService :: Switch -> MDNSConfig -> IO (Either String MDNSService)
- newMDNSServiceWithBackend :: MDNSBackend -> Switch -> MDNSConfig -> IO (Either String MDNSService)
- stopMDNSService :: MDNSService -> IO ()
- mdnsIPv4Group :: ByteString
- mdnsIPv6Group :: ByteString
- mdnsPort :: Word16
- data DiscoveryEvent = DiscoveryEvent {
- discoveryPeerId :: !PeerId
- discoveryAddrs :: ![Multiaddr]
- newDiscoveryQueue :: IO (TQueue DiscoveryEvent)
- tryReadDiscoveryQueue :: TQueue DiscoveryEvent -> IO (Maybe DiscoveryEvent)
- drainDiscoveryQueue :: TQueue DiscoveryEvent -> IO [DiscoveryEvent]
Configuration
data MDNSConfig Source #
mDNS service settings; defaultMDNSConfig tracks the spec
(service _p2p._udp.local, port 5353, both address families).
Constructors
| MDNSConfig | |
Fields
| |
Instances
| Show MDNSConfig Source # | |
Defined in LibP2P.Discovery.MDNS Methods showsPrec :: Int -> MDNSConfig -> ShowS # show :: MDNSConfig -> String # showList :: [MDNSConfig] -> ShowS # | |
| Eq MDNSConfig Source # | |
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.
Constructors
| MDNSBackend | |
Fields
| |
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.
newMDNSServiceWithBackend :: MDNSBackend -> Switch -> MDNSConfig -> IO (Either String MDNSService) Source #
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).
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
| |
Instances
| Show DiscoveryEvent Source # | |
Defined in LibP2P.Discovery.Types Methods showsPrec :: Int -> DiscoveryEvent -> ShowS # show :: DiscoveryEvent -> String # showList :: [DiscoveryEvent] -> ShowS # | |
| Eq DiscoveryEvent Source # | |
Defined in LibP2P.Discovery.Types Methods (==) :: DiscoveryEvent -> DiscoveryEvent -> Bool # (/=) :: DiscoveryEvent -> DiscoveryEvent -> Bool # | |
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.