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 a `Mdns` and `TokioMdns`, depending on the enabled features, which
32//! 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 = "async-io")]
44pub use crate::behaviour::async_io;
45#[cfg(feature = "tokio")]
46pub use crate::behaviour::tokio;
47pub use crate::behaviour::{Behaviour, Event};
48
49/// The DNS service name for all libp2p peers used to query for addresses.
50const SERVICE_NAME: &[u8] = b"_p2p._udp.local";
51/// `SERVICE_NAME` as a Fully Qualified Domain Name.
52const SERVICE_NAME_FQDN: &str = "_p2p._udp.local.";
53/// The meta query for looking up the `SERVICE_NAME`.
54const META_QUERY_SERVICE: &[u8] = b"_services._dns-sd._udp.local";
55/// `META_QUERY_SERVICE` as a Fully Qualified Domain Name.
56const META_QUERY_SERVICE_FQDN: &str = "_services._dns-sd._udp.local.";
57
58pub const IPV4_MDNS_MULTICAST_ADDRESS: Ipv4Addr = Ipv4Addr::new(224, 0, 0, 251);
59pub const IPV6_MDNS_MULTICAST_ADDRESS: Ipv6Addr = Ipv6Addr::new(0xFF02, 0, 0, 0, 0, 0, 0, 0xFB);
60
61/// Configuration for mDNS.
62#[derive(Debug, Clone)]
63pub struct Config {
64 /// TTL to use for mdns records.
65 pub ttl: Duration,
66 /// Interval at which to poll the network for new peers. This isn't
67 /// necessary during normal operation but avoids the case that an
68 /// initial packet was lost and not discovering any peers until a new
69 /// peer joins the network. Receiving an mdns packet resets the timer
70 /// preventing unnecessary traffic.
71 pub query_interval: Duration,
72 /// Use IPv6 instead of IPv4.
73 pub enable_ipv6: bool,
74}
75
76impl Default for Config {
77 fn default() -> Self {
78 Self {
79 ttl: Duration::from_secs(6 * 60),
80 query_interval: Duration::from_secs(5 * 60),
81 enable_ipv6: false,
82 }
83 }
84}