libp2p_mdns/
lib.rs

1// Copyright 2018 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21//! Implementation of the libp2p-specific [mDNS](https://github.com/libp2p/specs/blob/master/discovery/mdns.md) protocol.
22//!
23//! mDNS is a protocol defined by [RFC 6762](https://tools.ietf.org/html/rfc6762) that allows
24//! querying nodes that correspond to a certain domain name.
25//!
26//! In the context of libp2p, the mDNS protocol is used to discover other nodes on the local
27//! network that support libp2p.
28//!
29//! # Usage
30//!
31//! This crate provides `TokioMdns`
32//! which implements the `NetworkBehaviour` trait. This struct will automatically discover other
33//! libp2p nodes on the local network.
34
35#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
36
37use std::{
38    net::{Ipv4Addr, Ipv6Addr},
39    time::Duration,
40};
41
42mod behaviour;
43#[cfg(feature = "tokio")]
44pub use crate::behaviour::tokio;
45pub use crate::behaviour::{Behaviour, Event};
46
47/// The DNS service name for all libp2p peers used to query for addresses.
48const SERVICE_NAME: &[u8] = b"_p2p._udp.local";
49/// `SERVICE_NAME` as a Fully Qualified Domain Name.
50const SERVICE_NAME_FQDN: &str = "_p2p._udp.local.";
51/// The meta query for looking up the `SERVICE_NAME`.
52const META_QUERY_SERVICE: &[u8] = b"_services._dns-sd._udp.local";
53/// `META_QUERY_SERVICE` as a Fully Qualified Domain Name.
54const META_QUERY_SERVICE_FQDN: &str = "_services._dns-sd._udp.local.";
55
56pub const IPV4_MDNS_MULTICAST_ADDRESS: Ipv4Addr = Ipv4Addr::new(224, 0, 0, 251);
57pub const IPV6_MDNS_MULTICAST_ADDRESS: Ipv6Addr = Ipv6Addr::new(0xFF02, 0, 0, 0, 0, 0, 0, 0xFB);
58
59/// Configuration for mDNS.
60#[derive(Debug, Clone)]
61pub struct Config {
62    /// TTL to use for mdns records.
63    pub ttl: Duration,
64    /// Interval at which to poll the network for new peers. This isn't
65    /// necessary during normal operation but avoids the case that an
66    /// initial packet was lost and not discovering any peers until a new
67    /// peer joins the network. Receiving an mdns packet resets the timer
68    /// preventing unnecessary traffic.
69    pub query_interval: Duration,
70    /// Use IPv6 instead of IPv4.
71    pub enable_ipv6: bool,
72}
73
74impl Default for Config {
75    fn default() -> Self {
76        Self {
77            ttl: Duration::from_secs(6 * 60),
78            query_interval: Duration::from_secs(5 * 60),
79            enable_ipv6: false,
80        }
81    }
82}