mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-14 18:41:22 +00:00
mdns: update if-watch to 3.0.0 (#3096)
This commit is contained in:
@ -32,13 +32,7 @@ use futures::StreamExt;
|
||||
use libp2p::{
|
||||
core::upgrade,
|
||||
floodsub::{self, Floodsub, FloodsubEvent},
|
||||
identity,
|
||||
mdns::{
|
||||
MdnsEvent,
|
||||
// `TokioMdns` is available through the `mdns-tokio` feature.
|
||||
TokioMdns,
|
||||
},
|
||||
mplex, noise,
|
||||
identity, mdns, mplex, noise,
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
tcp, Multiaddr, PeerId, Transport,
|
||||
};
|
||||
@ -75,13 +69,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
#[behaviour(out_event = "MyBehaviourEvent")]
|
||||
struct MyBehaviour {
|
||||
floodsub: Floodsub,
|
||||
mdns: TokioMdns,
|
||||
mdns: mdns::tokio::Behaviour,
|
||||
}
|
||||
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
enum MyBehaviourEvent {
|
||||
Floodsub(FloodsubEvent),
|
||||
Mdns(MdnsEvent),
|
||||
Mdns(mdns::Event),
|
||||
}
|
||||
|
||||
impl From<FloodsubEvent> for MyBehaviourEvent {
|
||||
@ -90,17 +84,17 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MdnsEvent> for MyBehaviourEvent {
|
||||
fn from(event: MdnsEvent) -> Self {
|
||||
impl From<mdns::Event> for MyBehaviourEvent {
|
||||
fn from(event: mdns::Event) -> Self {
|
||||
MyBehaviourEvent::Mdns(event)
|
||||
}
|
||||
}
|
||||
|
||||
// Create a Swarm to manage peers and events.
|
||||
let mdns = TokioMdns::new(Default::default())?;
|
||||
let mdns_behaviour = mdns::Behaviour::new(Default::default())?;
|
||||
let behaviour = MyBehaviour {
|
||||
floodsub: Floodsub::new(peer_id),
|
||||
mdns,
|
||||
mdns: mdns_behaviour,
|
||||
};
|
||||
let mut swarm = libp2p_swarm::Swarm::with_tokio_executor(transport, behaviour, peer_id);
|
||||
|
||||
@ -138,12 +132,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(event)) => {
|
||||
match event {
|
||||
MdnsEvent::Discovered(list) => {
|
||||
mdns::Event::Discovered(list) => {
|
||||
for (peer, _) in list {
|
||||
swarm.behaviour_mut().floodsub.add_node_to_partial_view(peer);
|
||||
}
|
||||
}
|
||||
MdnsEvent::Expired(list) => {
|
||||
mdns::Event::Expired(list) => {
|
||||
for (peer, _) in list {
|
||||
if !swarm.behaviour().mdns.has_node(&peer) {
|
||||
swarm.behaviour_mut().floodsub.remove_node_from_partial_view(&peer);
|
||||
|
@ -56,8 +56,7 @@ use futures::{
|
||||
};
|
||||
use libp2p::{
|
||||
floodsub::{self, Floodsub, FloodsubEvent},
|
||||
identity,
|
||||
mdns::{Mdns, MdnsConfig, MdnsEvent},
|
||||
identity, mdns,
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
Multiaddr, PeerId, Swarm,
|
||||
};
|
||||
@ -84,18 +83,18 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
#[behaviour(out_event = "OutEvent")]
|
||||
struct MyBehaviour {
|
||||
floodsub: Floodsub,
|
||||
mdns: Mdns,
|
||||
mdns: mdns::async_io::Behaviour,
|
||||
}
|
||||
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
#[derive(Debug)]
|
||||
enum OutEvent {
|
||||
Floodsub(FloodsubEvent),
|
||||
Mdns(MdnsEvent),
|
||||
Mdns(mdns::Event),
|
||||
}
|
||||
|
||||
impl From<MdnsEvent> for OutEvent {
|
||||
fn from(v: MdnsEvent) -> Self {
|
||||
impl From<mdns::Event> for OutEvent {
|
||||
fn from(v: mdns::Event) -> Self {
|
||||
Self::Mdns(v)
|
||||
}
|
||||
}
|
||||
@ -108,7 +107,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
// Create a Swarm to manage peers and events
|
||||
let mut swarm = {
|
||||
let mdns = Mdns::new(MdnsConfig::default())?;
|
||||
let mdns = mdns::async_io::Behaviour::new(mdns::Config::default())?;
|
||||
let mut behaviour = MyBehaviour {
|
||||
floodsub: Floodsub::new(local_peer_id),
|
||||
mdns,
|
||||
@ -152,7 +151,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
);
|
||||
}
|
||||
SwarmEvent::Behaviour(OutEvent::Mdns(
|
||||
MdnsEvent::Discovered(list)
|
||||
mdns::Event::Discovered(list)
|
||||
)) => {
|
||||
for (peer, _) in list {
|
||||
swarm
|
||||
@ -161,7 +160,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
.add_node_to_partial_view(peer);
|
||||
}
|
||||
}
|
||||
SwarmEvent::Behaviour(OutEvent::Mdns(MdnsEvent::Expired(
|
||||
SwarmEvent::Behaviour(OutEvent::Mdns(mdns::Event::Expired(
|
||||
list
|
||||
))) => {
|
||||
for (peer, _) in list {
|
||||
|
@ -48,8 +48,7 @@ use libp2p::kad::{
|
||||
Quorum, Record,
|
||||
};
|
||||
use libp2p::{
|
||||
development_transport, identity,
|
||||
mdns::{Mdns, MdnsConfig, MdnsEvent},
|
||||
development_transport, identity, mdns,
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
PeerId, Swarm,
|
||||
};
|
||||
@ -71,13 +70,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
#[behaviour(out_event = "MyBehaviourEvent")]
|
||||
struct MyBehaviour {
|
||||
kademlia: Kademlia<MemoryStore>,
|
||||
mdns: Mdns,
|
||||
mdns: mdns::async_io::Behaviour,
|
||||
}
|
||||
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
enum MyBehaviourEvent {
|
||||
Kademlia(KademliaEvent),
|
||||
Mdns(MdnsEvent),
|
||||
Mdns(mdns::Event),
|
||||
}
|
||||
|
||||
impl From<KademliaEvent> for MyBehaviourEvent {
|
||||
@ -86,8 +85,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MdnsEvent> for MyBehaviourEvent {
|
||||
fn from(event: MdnsEvent) -> Self {
|
||||
impl From<mdns::Event> for MyBehaviourEvent {
|
||||
fn from(event: mdns::Event) -> Self {
|
||||
MyBehaviourEvent::Mdns(event)
|
||||
}
|
||||
}
|
||||
@ -97,7 +96,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
// Create a Kademlia behaviour.
|
||||
let store = MemoryStore::new(local_peer_id);
|
||||
let kademlia = Kademlia::new(local_peer_id, store);
|
||||
let mdns = Mdns::new(MdnsConfig::default())?;
|
||||
let mdns = mdns::async_io::Behaviour::new(mdns::Config::default())?;
|
||||
let behaviour = MyBehaviour { kademlia, mdns };
|
||||
Swarm::with_async_std_executor(transport, behaviour, local_peer_id)
|
||||
};
|
||||
@ -116,7 +115,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
SwarmEvent::NewListenAddr { address, .. } => {
|
||||
println!("Listening in {address:?}");
|
||||
},
|
||||
SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(MdnsEvent::Discovered(list))) => {
|
||||
SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(mdns::Event::Discovered(list))) => {
|
||||
for (peer_id, multiaddr) in list {
|
||||
swarm.behaviour_mut().kademlia.add_address(&peer_id, multiaddr);
|
||||
}
|
||||
|
@ -53,11 +53,7 @@ use libp2p::gossipsub::{
|
||||
ValidationMode,
|
||||
};
|
||||
use libp2p::{
|
||||
gossipsub, identity,
|
||||
mdns::{Mdns, MdnsConfig, MdnsEvent},
|
||||
swarm::NetworkBehaviour,
|
||||
swarm::SwarmEvent,
|
||||
PeerId, Swarm,
|
||||
gossipsub, identity, mdns, swarm::NetworkBehaviour, swarm::SwarmEvent, PeerId, Swarm,
|
||||
};
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::error::Error;
|
||||
@ -78,7 +74,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
#[derive(NetworkBehaviour)]
|
||||
struct MyBehaviour {
|
||||
gossipsub: Gossipsub,
|
||||
mdns: Mdns,
|
||||
mdns: mdns::async_io::Behaviour,
|
||||
}
|
||||
|
||||
// To content-address message, we can take the hash of message and use it as an ID.
|
||||
@ -108,7 +104,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
// Create a Swarm to manage peers and events
|
||||
let mut swarm = {
|
||||
let mdns = Mdns::new(MdnsConfig::default())?;
|
||||
let mdns = mdns::async_io::Behaviour::new(mdns::Config::default())?;
|
||||
let behaviour = MyBehaviour { gossipsub, mdns };
|
||||
Swarm::with_async_std_executor(transport, behaviour, local_peer_id)
|
||||
};
|
||||
@ -132,13 +128,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
},
|
||||
event = swarm.select_next_some() => match event {
|
||||
SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(MdnsEvent::Discovered(list))) => {
|
||||
SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(mdns::Event::Discovered(list))) => {
|
||||
for (peer_id, _multiaddr) in list {
|
||||
println!("mDNS discovered a new peer: {peer_id}");
|
||||
swarm.behaviour_mut().gossipsub.add_explicit_peer(&peer_id);
|
||||
}
|
||||
},
|
||||
SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(MdnsEvent::Expired(list))) => {
|
||||
SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(mdns::Event::Expired(list))) => {
|
||||
for (peer_id, _multiaddr) in list {
|
||||
println!("mDNS discover peer has expired: {peer_id}");
|
||||
swarm.behaviour_mut().gossipsub.remove_explicit_peer(&peer_id);
|
||||
|
@ -20,8 +20,7 @@
|
||||
|
||||
use futures::StreamExt;
|
||||
use libp2p::{
|
||||
identity,
|
||||
mdns::{Mdns, MdnsConfig, MdnsEvent},
|
||||
identity, mdns,
|
||||
swarm::{Swarm, SwarmEvent},
|
||||
PeerId,
|
||||
};
|
||||
@ -40,7 +39,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
let transport = libp2p::development_transport(id_keys).await?;
|
||||
|
||||
// Create an MDNS network behaviour.
|
||||
let behaviour = Mdns::new(MdnsConfig::default())?;
|
||||
let behaviour = mdns::async_io::Behaviour::new(mdns::Config::default())?;
|
||||
|
||||
// Create a Swarm that establishes connections through the given transport.
|
||||
// Note that the MDNS behaviour itself will not actually inititiate any connections,
|
||||
@ -50,12 +49,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
loop {
|
||||
match swarm.select_next_some().await {
|
||||
SwarmEvent::Behaviour(MdnsEvent::Discovered(peers)) => {
|
||||
SwarmEvent::Behaviour(mdns::Event::Discovered(peers)) => {
|
||||
for (peer, addr) in peers {
|
||||
println!("discovered {peer} {addr}");
|
||||
}
|
||||
}
|
||||
SwarmEvent::Behaviour(MdnsEvent::Expired(expired)) => {
|
||||
SwarmEvent::Behaviour(mdns::Event::Expired(expired)) => {
|
||||
for (peer, addr) in expired {
|
||||
println!("expired {peer} {addr}");
|
||||
}
|
||||
|
@ -4,11 +4,21 @@
|
||||
|
||||
- Update to `libp2p-swarm` `v0.41.0`.
|
||||
|
||||
- Update to `if-watch` `3.0.0` and both rename `TokioMdns` to `Behaviour` living in `tokio::Behaviour`,
|
||||
and move and rename `Mdns` to `async_io::Behaviour`. See [PR 3096].
|
||||
|
||||
- Remove the remaning `Mdns` prefixes from types as per [discussion 2174].
|
||||
I.e the `Mdns` prefix has been removed from various types like `MdnsEvent`.
|
||||
Users should prefer importing the mdns protocol as a module (`use libp2p::mdns;`),
|
||||
and refer to its types via `mdns::`. For example: `mdns::Behaviour` or `mdns::Event`.
|
||||
|
||||
- Replace `GenMdns`'s `NetworkBehaviour` implemention `inject_*` methods with the new `on_*` methods.
|
||||
See [PR 3011].
|
||||
|
||||
- Use `trust-dns-proto` to parse DNS messages. See [PR 3102].
|
||||
|
||||
[discussion 2174]: https://github.com/libp2p/rust-libp2p/discussions/2174
|
||||
[PR 3096]: https://github.com/libp2p/rust-libp2p/pull/3096
|
||||
[PR 3011]: https://github.com/libp2p/rust-libp2p/pull/3011
|
||||
[PR 3102]: https://github.com/libp2p/rust-libp2p/pull/3102
|
||||
|
||||
|
@ -14,7 +14,7 @@ categories = ["network-programming", "asynchronous"]
|
||||
async-io = { version = "1.3.1", optional = true }
|
||||
data-encoding = "2.3.2"
|
||||
futures = "0.3.13"
|
||||
if-watch = "2.0.0"
|
||||
if-watch = "3.0.0"
|
||||
libp2p-core = { version = "0.38.0", path = "../../core" }
|
||||
libp2p-swarm = { version = "0.41.0", path = "../../swarm" }
|
||||
log = "0.4.14"
|
||||
@ -26,8 +26,8 @@ trust-dns-proto = { version = "0.22.0", default-features = false, features = ["m
|
||||
void = "1.0.2"
|
||||
|
||||
[features]
|
||||
tokio = ["dep:tokio"]
|
||||
async-io = ["dep:async-io"]
|
||||
tokio = ["dep:tokio", "if-watch/tokio"]
|
||||
async-io = ["dep:async-io", "if-watch/smol"]
|
||||
|
||||
[dev-dependencies]
|
||||
async-std = { version = "1.9.0", features = ["attributes"] }
|
||||
|
@ -24,9 +24,9 @@ mod timer;
|
||||
|
||||
use self::iface::InterfaceState;
|
||||
use crate::behaviour::{socket::AsyncSocket, timer::Builder};
|
||||
use crate::MdnsConfig;
|
||||
use crate::Config;
|
||||
use futures::Stream;
|
||||
use if_watch::{IfEvent, IfWatcher};
|
||||
use if_watch::IfEvent;
|
||||
use libp2p_core::{Multiaddr, PeerId};
|
||||
use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm};
|
||||
use libp2p_swarm::{
|
||||
@ -36,32 +36,80 @@ use smallvec::SmallVec;
|
||||
use std::collections::hash_map::{Entry, HashMap};
|
||||
use std::{cmp, fmt, io, net::IpAddr, pin::Pin, task::Context, task::Poll, time::Instant};
|
||||
|
||||
/// An abstraction to allow for compatibility with various async runtimes.
|
||||
pub trait Provider: 'static {
|
||||
/// The Async Socket type.
|
||||
type Socket: AsyncSocket;
|
||||
/// The Async Timer type.
|
||||
type Timer: Builder + Stream;
|
||||
/// The IfWatcher type.
|
||||
type Watcher: Stream<Item = std::io::Result<IfEvent>> + fmt::Debug + Unpin;
|
||||
|
||||
/// Create a new instance of the `IfWatcher` type.
|
||||
fn new_watcher() -> Result<Self::Watcher, std::io::Error>;
|
||||
}
|
||||
|
||||
/// The type of a [`Behaviour`] using the `async-io` implementation.
|
||||
#[cfg(feature = "async-io")]
|
||||
use crate::behaviour::{socket::asio::AsyncUdpSocket, timer::asio::AsyncTimer};
|
||||
pub mod async_io {
|
||||
use super::Provider;
|
||||
use crate::behaviour::{socket::asio::AsyncUdpSocket, timer::asio::AsyncTimer};
|
||||
use if_watch::smol::IfWatcher;
|
||||
|
||||
/// The type of a [`GenMdns`] using the `async-io` implementation.
|
||||
#[cfg(feature = "async-io")]
|
||||
pub type Mdns = GenMdns<AsyncUdpSocket, AsyncTimer>;
|
||||
#[doc(hidden)]
|
||||
pub enum AsyncIo {}
|
||||
|
||||
impl Provider for AsyncIo {
|
||||
type Socket = AsyncUdpSocket;
|
||||
type Timer = AsyncTimer;
|
||||
type Watcher = IfWatcher;
|
||||
|
||||
fn new_watcher() -> Result<Self::Watcher, std::io::Error> {
|
||||
IfWatcher::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub type Behaviour = super::Behaviour<AsyncIo>;
|
||||
}
|
||||
|
||||
/// The type of a [`Behaviour`] using the `tokio` implementation.
|
||||
#[cfg(feature = "tokio")]
|
||||
use crate::behaviour::{socket::tokio::TokioUdpSocket, timer::tokio::TokioTimer};
|
||||
pub mod tokio {
|
||||
use super::Provider;
|
||||
use crate::behaviour::{socket::tokio::TokioUdpSocket, timer::tokio::TokioTimer};
|
||||
use if_watch::tokio::IfWatcher;
|
||||
|
||||
/// The type of a [`GenMdns`] using the `tokio` implementation.
|
||||
#[cfg(feature = "tokio")]
|
||||
pub type TokioMdns = GenMdns<TokioUdpSocket, TokioTimer>;
|
||||
#[doc(hidden)]
|
||||
pub enum Tokio {}
|
||||
|
||||
impl Provider for Tokio {
|
||||
type Socket = TokioUdpSocket;
|
||||
type Timer = TokioTimer;
|
||||
type Watcher = IfWatcher;
|
||||
|
||||
fn new_watcher() -> Result<Self::Watcher, std::io::Error> {
|
||||
IfWatcher::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub type Behaviour = super::Behaviour<Tokio>;
|
||||
}
|
||||
|
||||
/// A `NetworkBehaviour` for mDNS. Automatically discovers peers on the local network and adds
|
||||
/// them to the topology.
|
||||
#[derive(Debug)]
|
||||
pub struct GenMdns<S, T> {
|
||||
pub struct Behaviour<P>
|
||||
where
|
||||
P: Provider,
|
||||
{
|
||||
/// InterfaceState config.
|
||||
config: MdnsConfig,
|
||||
config: Config,
|
||||
|
||||
/// Iface watcher.
|
||||
if_watch: IfWatcher,
|
||||
if_watch: P::Watcher,
|
||||
|
||||
/// Mdns interface states.
|
||||
iface_states: HashMap<IpAddr, InterfaceState<S, T>>,
|
||||
iface_states: HashMap<IpAddr, InterfaceState<P::Socket, P::Timer>>,
|
||||
|
||||
/// List of nodes that we have discovered, the address, and when their TTL expires.
|
||||
///
|
||||
@ -72,19 +120,18 @@ pub struct GenMdns<S, T> {
|
||||
/// Future that fires when the TTL of at least one node in `discovered_nodes` expires.
|
||||
///
|
||||
/// `None` if `discovered_nodes` is empty.
|
||||
closest_expiration: Option<T>,
|
||||
closest_expiration: Option<P::Timer>,
|
||||
}
|
||||
|
||||
impl<S, T> GenMdns<S, T>
|
||||
impl<P> Behaviour<P>
|
||||
where
|
||||
T: Builder,
|
||||
P: Provider,
|
||||
{
|
||||
/// Builds a new `Mdns` behaviour.
|
||||
pub fn new(config: MdnsConfig) -> io::Result<Self> {
|
||||
let if_watch = if_watch::IfWatcher::new()?;
|
||||
pub fn new(config: Config) -> io::Result<Self> {
|
||||
Ok(Self {
|
||||
config,
|
||||
if_watch,
|
||||
if_watch: P::new_watcher()?,
|
||||
iface_states: Default::default(),
|
||||
discovered_nodes: Default::default(),
|
||||
closest_expiration: Default::default(),
|
||||
@ -109,17 +156,16 @@ where
|
||||
*expires = now;
|
||||
}
|
||||
}
|
||||
self.closest_expiration = Some(T::at(now));
|
||||
self.closest_expiration = Some(P::Timer::at(now));
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, T> NetworkBehaviour for GenMdns<S, T>
|
||||
impl<P> NetworkBehaviour for Behaviour<P>
|
||||
where
|
||||
T: Builder + Stream,
|
||||
S: AsyncSocket,
|
||||
P: Provider,
|
||||
{
|
||||
type ConnectionHandler = dummy::ConnectionHandler;
|
||||
type OutEvent = MdnsEvent;
|
||||
type OutEvent = Event;
|
||||
|
||||
fn new_handler(&mut self) -> Self::ConnectionHandler {
|
||||
dummy::ConnectionHandler
|
||||
@ -226,7 +272,7 @@ where
|
||||
}
|
||||
}
|
||||
if !discovered.is_empty() {
|
||||
let event = MdnsEvent::Discovered(DiscoveredAddrsIter {
|
||||
let event = Event::Discovered(DiscoveredAddrsIter {
|
||||
inner: discovered.into_iter(),
|
||||
});
|
||||
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
|
||||
@ -245,13 +291,13 @@ where
|
||||
true
|
||||
});
|
||||
if !expired.is_empty() {
|
||||
let event = MdnsEvent::Expired(ExpiredAddrsIter {
|
||||
let event = Event::Expired(ExpiredAddrsIter {
|
||||
inner: expired.into_iter(),
|
||||
});
|
||||
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
|
||||
}
|
||||
if let Some(closest_expiration) = closest_expiration {
|
||||
let mut timer = T::at(closest_expiration);
|
||||
let mut timer = P::Timer::at(closest_expiration);
|
||||
let _ = Pin::new(&mut timer).poll_next(cx);
|
||||
|
||||
self.closest_expiration = Some(timer);
|
||||
@ -262,7 +308,7 @@ where
|
||||
|
||||
/// Event that can be produced by the `Mdns` behaviour.
|
||||
#[derive(Debug)]
|
||||
pub enum MdnsEvent {
|
||||
pub enum Event {
|
||||
/// Discovered nodes through mDNS.
|
||||
Discovered(DiscoveredAddrsIter),
|
||||
|
||||
|
@ -24,7 +24,7 @@ mod query;
|
||||
use self::dns::{build_query, build_query_response, build_service_discovery_response};
|
||||
use self::query::MdnsPacket;
|
||||
use crate::behaviour::{socket::AsyncSocket, timer::Builder};
|
||||
use crate::MdnsConfig;
|
||||
use crate::Config;
|
||||
use libp2p_core::{Multiaddr, PeerId};
|
||||
use libp2p_swarm::PollParameters;
|
||||
use socket2::{Domain, Socket, Type};
|
||||
@ -74,7 +74,7 @@ where
|
||||
T: Builder + futures::Stream,
|
||||
{
|
||||
/// Builds a new [`InterfaceState`].
|
||||
pub fn new(addr: IpAddr, config: MdnsConfig) -> io::Result<Self> {
|
||||
pub fn new(addr: IpAddr, config: Config) -> io::Result<Self> {
|
||||
log::info!("creating instance on iface {}", addr);
|
||||
let recv_socket = match addr {
|
||||
IpAddr::V4(addr) => {
|
||||
|
@ -38,14 +38,33 @@
|
||||
use std::net::{Ipv4Addr, Ipv6Addr};
|
||||
use std::time::Duration;
|
||||
|
||||
#[deprecated(
|
||||
since = "0.42.0",
|
||||
note = "Use re-exports that omit `Mdns` prefix, i.e. `libp2p::mdns::Config`"
|
||||
)]
|
||||
pub type MdnsConfig = Config;
|
||||
|
||||
#[deprecated(
|
||||
since = "0.42.0",
|
||||
note = "Use re-exports that omit `Mdns` prefix, i.e. `libp2p::mdns::Event`"
|
||||
)]
|
||||
pub type MdnsEvent = Event;
|
||||
|
||||
#[deprecated(
|
||||
since = "0.42.0",
|
||||
note = "Use the async-io prefixed `Mdns`, i.e. `libp2p::mdns::async_io::Mdns`"
|
||||
)]
|
||||
#[cfg(feature = "async-io")]
|
||||
pub type Mdns = async_io::Behaviour;
|
||||
|
||||
mod behaviour;
|
||||
pub use crate::behaviour::{GenMdns, MdnsEvent};
|
||||
pub use crate::behaviour::{Behaviour, Event};
|
||||
|
||||
#[cfg(feature = "async-io")]
|
||||
pub use crate::behaviour::Mdns;
|
||||
pub use crate::behaviour::async_io;
|
||||
|
||||
#[cfg(feature = "tokio")]
|
||||
pub use crate::behaviour::TokioMdns;
|
||||
pub use crate::behaviour::tokio;
|
||||
|
||||
/// The DNS service name for all libp2p peers used to query for addresses.
|
||||
const SERVICE_NAME: &[u8] = b"_p2p._udp.local";
|
||||
@ -61,7 +80,7 @@ pub const IPV6_MDNS_MULTICAST_ADDRESS: Ipv6Addr = Ipv6Addr::new(0xFF02, 0, 0, 0,
|
||||
|
||||
/// Configuration for mDNS.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MdnsConfig {
|
||||
pub struct Config {
|
||||
/// TTL to use for mdns records.
|
||||
pub ttl: Duration,
|
||||
/// Interval at which to poll the network for new peers. This isn't
|
||||
@ -74,7 +93,7 @@ pub struct MdnsConfig {
|
||||
pub enable_ipv6: bool,
|
||||
}
|
||||
|
||||
impl Default for MdnsConfig {
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
ttl: Duration::from_secs(6 * 60),
|
||||
|
@ -21,7 +21,7 @@
|
||||
use futures::StreamExt;
|
||||
use libp2p::{
|
||||
identity,
|
||||
mdns::{Mdns, MdnsConfig, MdnsEvent},
|
||||
mdns::{async_io::Behaviour, Config, Event},
|
||||
swarm::{Swarm, SwarmEvent},
|
||||
PeerId,
|
||||
};
|
||||
@ -30,12 +30,12 @@ use std::time::Duration;
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_discovery_async_std_ipv4() -> Result<(), Box<dyn Error>> {
|
||||
run_discovery_test(MdnsConfig::default()).await
|
||||
run_discovery_test(Config::default()).await
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_discovery_async_std_ipv6() -> Result<(), Box<dyn Error>> {
|
||||
let config = MdnsConfig {
|
||||
let config = Config {
|
||||
enable_ipv6: true,
|
||||
..Default::default()
|
||||
};
|
||||
@ -45,7 +45,7 @@ async fn test_discovery_async_std_ipv6() -> Result<(), Box<dyn Error>> {
|
||||
#[async_std::test]
|
||||
async fn test_expired_async_std() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::try_init().ok();
|
||||
let config = MdnsConfig {
|
||||
let config = Config {
|
||||
ttl: Duration::from_secs(1),
|
||||
query_interval: Duration::from_secs(10),
|
||||
..Default::default()
|
||||
@ -57,17 +57,17 @@ async fn test_expired_async_std() -> Result<(), Box<dyn Error>> {
|
||||
.map_err(|e| Box::new(e) as Box<dyn Error>)
|
||||
}
|
||||
|
||||
async fn create_swarm(config: MdnsConfig) -> Result<Swarm<Mdns>, Box<dyn Error>> {
|
||||
async fn create_swarm(config: Config) -> Result<Swarm<Behaviour>, Box<dyn Error>> {
|
||||
let id_keys = identity::Keypair::generate_ed25519();
|
||||
let peer_id = PeerId::from(id_keys.public());
|
||||
let transport = libp2p::development_transport(id_keys).await?;
|
||||
let behaviour = Mdns::new(config)?;
|
||||
let behaviour = Behaviour::new(config)?;
|
||||
let mut swarm = Swarm::with_async_std_executor(transport, behaviour, peer_id);
|
||||
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
|
||||
Ok(swarm)
|
||||
}
|
||||
|
||||
async fn run_discovery_test(config: MdnsConfig) -> Result<(), Box<dyn Error>> {
|
||||
async fn run_discovery_test(config: Config) -> Result<(), Box<dyn Error>> {
|
||||
env_logger::try_init().ok();
|
||||
let mut a = create_swarm(config.clone()).await?;
|
||||
let mut b = create_swarm(config).await?;
|
||||
@ -75,7 +75,7 @@ async fn run_discovery_test(config: MdnsConfig) -> Result<(), Box<dyn Error>> {
|
||||
let mut discovered_b = false;
|
||||
loop {
|
||||
futures::select! {
|
||||
ev = a.select_next_some() => if let SwarmEvent::Behaviour(MdnsEvent::Discovered(peers)) = ev {
|
||||
ev = a.select_next_some() => if let SwarmEvent::Behaviour(Event::Discovered(peers)) = ev {
|
||||
for (peer, _addr) in peers {
|
||||
if peer == *b.local_peer_id() {
|
||||
if discovered_a {
|
||||
@ -86,7 +86,7 @@ async fn run_discovery_test(config: MdnsConfig) -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
}
|
||||
},
|
||||
ev = b.select_next_some() => if let SwarmEvent::Behaviour(MdnsEvent::Discovered(peers)) = ev {
|
||||
ev = b.select_next_some() => if let SwarmEvent::Behaviour(Event::Discovered(peers)) = ev {
|
||||
for (peer, _addr) in peers {
|
||||
if peer == *a.local_peer_id() {
|
||||
if discovered_b {
|
||||
@ -101,20 +101,20 @@ async fn run_discovery_test(config: MdnsConfig) -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_peer_expiration_test(config: MdnsConfig) -> Result<(), Box<dyn Error>> {
|
||||
async fn run_peer_expiration_test(config: Config) -> Result<(), Box<dyn Error>> {
|
||||
let mut a = create_swarm(config.clone()).await?;
|
||||
let mut b = create_swarm(config).await?;
|
||||
|
||||
loop {
|
||||
futures::select! {
|
||||
ev = a.select_next_some() => if let SwarmEvent::Behaviour(MdnsEvent::Expired(peers)) = ev {
|
||||
ev = a.select_next_some() => if let SwarmEvent::Behaviour(Event::Expired(peers)) = ev {
|
||||
for (peer, _addr) in peers {
|
||||
if peer == *b.local_peer_id() {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
},
|
||||
ev = b.select_next_some() => if let SwarmEvent::Behaviour(MdnsEvent::Expired(peers)) = ev {
|
||||
ev = b.select_next_some() => if let SwarmEvent::Behaviour(Event::Expired(peers)) = ev {
|
||||
for (peer, _addr) in peers {
|
||||
if peer == *a.local_peer_id() {
|
||||
return Ok(());
|
||||
|
@ -20,7 +20,7 @@
|
||||
use futures::StreamExt;
|
||||
use libp2p::{
|
||||
identity,
|
||||
mdns::{MdnsConfig, MdnsEvent, TokioMdns},
|
||||
mdns::{tokio::Behaviour, Config, Event},
|
||||
swarm::{Swarm, SwarmEvent},
|
||||
PeerId,
|
||||
};
|
||||
@ -29,12 +29,12 @@ use std::time::Duration;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_discovery_tokio_ipv4() -> Result<(), Box<dyn Error>> {
|
||||
run_discovery_test(MdnsConfig::default()).await
|
||||
run_discovery_test(Config::default()).await
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_discovery_tokio_ipv6() -> Result<(), Box<dyn Error>> {
|
||||
let config = MdnsConfig {
|
||||
let config = Config {
|
||||
enable_ipv6: true,
|
||||
..Default::default()
|
||||
};
|
||||
@ -44,7 +44,7 @@ async fn test_discovery_tokio_ipv6() -> Result<(), Box<dyn Error>> {
|
||||
#[tokio::test]
|
||||
async fn test_expired_tokio() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::try_init().ok();
|
||||
let config = MdnsConfig {
|
||||
let config = Config {
|
||||
ttl: Duration::from_secs(1),
|
||||
query_interval: Duration::from_secs(10),
|
||||
..Default::default()
|
||||
@ -53,17 +53,17 @@ async fn test_expired_tokio() -> Result<(), Box<dyn Error>> {
|
||||
run_peer_expiration_test(config).await
|
||||
}
|
||||
|
||||
async fn create_swarm(config: MdnsConfig) -> Result<Swarm<TokioMdns>, Box<dyn Error>> {
|
||||
async fn create_swarm(config: Config) -> Result<Swarm<Behaviour>, Box<dyn Error>> {
|
||||
let id_keys = identity::Keypair::generate_ed25519();
|
||||
let peer_id = PeerId::from(id_keys.public());
|
||||
let transport = libp2p::tokio_development_transport(id_keys)?;
|
||||
let behaviour = TokioMdns::new(config)?;
|
||||
let behaviour = Behaviour::new(config)?;
|
||||
let mut swarm = Swarm::with_tokio_executor(transport, behaviour, peer_id);
|
||||
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
|
||||
Ok(swarm)
|
||||
}
|
||||
|
||||
async fn run_discovery_test(config: MdnsConfig) -> Result<(), Box<dyn Error>> {
|
||||
async fn run_discovery_test(config: Config) -> Result<(), Box<dyn Error>> {
|
||||
env_logger::try_init().ok();
|
||||
let mut a = create_swarm(config.clone()).await?;
|
||||
let mut b = create_swarm(config).await?;
|
||||
@ -71,7 +71,7 @@ async fn run_discovery_test(config: MdnsConfig) -> Result<(), Box<dyn Error>> {
|
||||
let mut discovered_b = false;
|
||||
loop {
|
||||
futures::select! {
|
||||
ev = a.select_next_some() => if let SwarmEvent::Behaviour(MdnsEvent::Discovered(peers)) = ev {
|
||||
ev = a.select_next_some() => if let SwarmEvent::Behaviour(Event::Discovered(peers)) = ev {
|
||||
for (peer, _addr) in peers {
|
||||
if peer == *b.local_peer_id() {
|
||||
if discovered_a {
|
||||
@ -82,7 +82,7 @@ async fn run_discovery_test(config: MdnsConfig) -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
}
|
||||
},
|
||||
ev = b.select_next_some() => if let SwarmEvent::Behaviour(MdnsEvent::Discovered(peers)) = ev {
|
||||
ev = b.select_next_some() => if let SwarmEvent::Behaviour(Event::Discovered(peers)) = ev {
|
||||
for (peer, _addr) in peers {
|
||||
if peer == *a.local_peer_id() {
|
||||
if discovered_b {
|
||||
@ -97,7 +97,7 @@ async fn run_discovery_test(config: MdnsConfig) -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_peer_expiration_test(config: MdnsConfig) -> Result<(), Box<dyn Error>> {
|
||||
async fn run_peer_expiration_test(config: Config) -> Result<(), Box<dyn Error>> {
|
||||
let mut a = create_swarm(config.clone()).await?;
|
||||
let mut b = create_swarm(config).await?;
|
||||
let expired_at = tokio::time::sleep(Duration::from_secs(15));
|
||||
@ -109,14 +109,14 @@ async fn run_peer_expiration_test(config: MdnsConfig) -> Result<(), Box<dyn Erro
|
||||
panic!();
|
||||
},
|
||||
ev = a.select_next_some() => match ev {
|
||||
SwarmEvent::Behaviour(MdnsEvent::Expired(peers)) => {
|
||||
SwarmEvent::Behaviour(Event::Expired(peers)) => {
|
||||
for (peer, _addr) in peers {
|
||||
if peer == *b.local_peer_id() {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
SwarmEvent::Behaviour(MdnsEvent::Discovered(peers)) => {
|
||||
SwarmEvent::Behaviour(Event::Discovered(peers)) => {
|
||||
for (peer, _addr) in peers {
|
||||
if peer == *b.local_peer_id() {
|
||||
expired_at.as_mut().reset(tokio::time::Instant::now() + tokio::time::Duration::from_secs(2));
|
||||
@ -126,14 +126,14 @@ async fn run_peer_expiration_test(config: MdnsConfig) -> Result<(), Box<dyn Erro
|
||||
_ => {}
|
||||
},
|
||||
ev = b.select_next_some() => match ev {
|
||||
SwarmEvent::Behaviour(MdnsEvent::Expired(peers)) => {
|
||||
SwarmEvent::Behaviour(Event::Expired(peers)) => {
|
||||
for (peer, _addr) in peers {
|
||||
if peer == *a.local_peer_id() {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
SwarmEvent::Behaviour(MdnsEvent::Discovered(peers)) => {
|
||||
SwarmEvent::Behaviour(Event::Discovered(peers)) => {
|
||||
for (peer, _addr) in peers {
|
||||
if peer == *a.local_peer_id() {
|
||||
expired_at.as_mut().reset(tokio::time::Instant::now() + tokio::time::Duration::from_secs(2));
|
||||
|
Reference in New Issue
Block a user