Skip to content

Commit 8988ac2

Browse files
authored
protocols/mdns: Fix discovered event emission. (#2065)
mdns keeps rediscovering nodes. this PR changes that to only emit events for new nodes it discovered. In addition we make sure to only send a query if it is really needed. Some logging is added for debugging purposes.
1 parent 5c541a1 commit 8988ac2

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

protocols/mdns/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 0.30.2 [2021-05-06]
2+
3+
- Fix discovered event emission.
4+
[PR 2065](https://github.com/libp2p/rust-libp2p/pull/2065)
5+
16
# 0.30.1 [2021-04-21]
27

38
- Fix timely discovery of peers after listening on a new address.

protocols/mdns/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "libp2p-mdns"
33
edition = "2018"
4-
version = "0.30.1"
4+
version = "0.30.2"
55
description = "Implementation of the libp2p mDNS discovery method"
66
authors = ["Parity Technologies <[email protected]>"]
77
license = "MIT"

protocols/mdns/src/behaviour.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ use async_io::{Async, Timer};
2424
use futures::prelude::*;
2525
use if_watch::{IfEvent, IfWatcher};
2626
use lazy_static::lazy_static;
27+
use libp2p_core::connection::ListenerId;
2728
use libp2p_core::{
2829
address_translation, connection::ConnectionId, multiaddr::Protocol, Multiaddr, PeerId,
2930
};
30-
use libp2p_core::connection::ListenerId;
3131
use libp2p_swarm::{
3232
protocols_handler::DummyProtocolsHandler, NetworkBehaviour, NetworkBehaviourAction,
3333
PollParameters, ProtocolsHandler,
@@ -124,11 +124,7 @@ impl Mdns {
124124
/// Builds a new `Mdns` behaviour.
125125
pub async fn new(config: MdnsConfig) -> io::Result<Self> {
126126
let recv_socket = {
127-
let socket = Socket::new(
128-
Domain::IPV4,
129-
Type::DGRAM,
130-
Some(socket2::Protocol::UDP),
131-
)?;
127+
let socket = Socket::new(Domain::IPV4, Type::DGRAM, Some(socket2::Protocol::UDP))?;
132128
socket.set_reuse_address(true)?;
133129
#[cfg(unix)]
134130
socket.set_reuse_port(true)?;
@@ -169,9 +165,10 @@ impl Mdns {
169165
}
170166

171167
fn inject_mdns_packet(&mut self, packet: MdnsPacket, params: &impl PollParameters) {
172-
self.timeout.set_interval(self.query_interval);
173168
match packet {
174169
MdnsPacket::Query(query) => {
170+
self.timeout.set_interval(self.query_interval);
171+
log::trace!("sending response");
175172
for packet in build_query_response(
176173
query.query_id(),
177174
*params.local_peer_id(),
@@ -214,8 +211,8 @@ impl Mdns {
214211
} else {
215212
self.discovered_nodes
216213
.push((*peer.id(), addr.clone(), new_expiration));
214+
discovered.push((*peer.id(), addr));
217215
}
218-
discovered.push((*peer.id(), addr));
219216
}
220217
}
221218

@@ -271,7 +268,8 @@ impl NetworkBehaviour for Mdns {
271268
}
272269

273270
fn inject_new_listen_addr(&mut self, _id: ListenerId, _addr: &Multiaddr) {
274-
self.send_buffer.push_back(build_query());
271+
self.timeout
272+
.set_interval_at(Instant::now(), self.query_interval);
275273
}
276274

277275
fn poll(
@@ -297,7 +295,8 @@ impl NetworkBehaviour for Mdns {
297295
if let Err(err) = socket.join_multicast_v4(&multicast, &addr) {
298296
log::error!("join multicast failed: {}", err);
299297
} else {
300-
self.send_buffer.push_back(build_query());
298+
self.timeout
299+
.set_interval_at(Instant::now(), self.query_interval);
301300
}
302301
}
303302
}
@@ -332,25 +331,23 @@ impl NetworkBehaviour for Mdns {
332331
_ => {}
333332
}
334333
}
335-
if Pin::new(&mut self.timeout).poll_next(cx).is_ready() {
336-
self.send_buffer.push_back(build_query());
337-
}
338334
// Send responses.
339-
if !self.send_buffer.is_empty() {
340-
while self.send_socket.poll_writable(cx).is_ready() {
341-
if let Some(packet) = self.send_buffer.pop_front() {
342-
match self
343-
.send_socket
344-
.send_to(&packet, *IPV4_MDNS_MULTICAST_ADDRESS)
345-
.now_or_never()
346-
{
347-
Some(Ok(_)) => {}
348-
Some(Err(err)) => log::error!("{}", err),
349-
None => self.send_buffer.push_front(packet),
350-
}
351-
} else {
352-
break;
335+
while self.send_socket.poll_writable(cx).is_ready() {
336+
if let Some(packet) = self.send_buffer.pop_front() {
337+
match self
338+
.send_socket
339+
.send_to(&packet, *IPV4_MDNS_MULTICAST_ADDRESS)
340+
.now_or_never()
341+
{
342+
Some(Ok(_)) => {}
343+
Some(Err(err)) => log::error!("{}", err),
344+
None => self.send_buffer.push_front(packet),
353345
}
346+
} else if Pin::new(&mut self.timeout).poll_next(cx).is_ready() {
347+
log::trace!("sending query");
348+
self.send_buffer.push_back(build_query());
349+
} else {
350+
break;
354351
}
355352
}
356353
// Emit discovered event.

0 commit comments

Comments
 (0)