2018-12-10 16:00:16 +01:00
|
|
|
// Copyright 2018 Parity Technologies (UK) Ltd.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
use crate::{SERVICE_NAME, META_QUERY_SERVICE, dns};
|
|
|
|
use dns_parser::{Packet, RData};
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
use either::Either::{Left, Right};
|
|
|
|
use futures::{future, prelude::*};
|
2020-01-10 21:03:59 +08:00
|
|
|
use libp2p_core::{multiaddr::{Multiaddr, Protocol}, PeerId};
|
2020-10-05 10:03:21 +02:00
|
|
|
use log::warn;
|
2020-01-29 11:33:19 +01:00
|
|
|
use std::{convert::TryFrom as _, fmt, io, net::Ipv4Addr, net::SocketAddr, str, time::{Duration, Instant}};
|
2019-09-16 11:08:44 +02:00
|
|
|
use wasm_timer::Interval;
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
use lazy_static::lazy_static;
|
2018-12-10 16:00:16 +01:00
|
|
|
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
pub use dns::{MdnsResponseError, build_query_response, build_service_discovery_response};
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref IPV4_MDNS_MULTICAST_ADDRESS: SocketAddr = SocketAddr::from((
|
|
|
|
Ipv4Addr::new(224, 0, 0, 251),
|
|
|
|
5353,
|
|
|
|
));
|
|
|
|
}
|
2018-12-10 16:00:16 +01:00
|
|
|
|
2020-08-18 14:51:03 +02:00
|
|
|
macro_rules! codegen {
|
|
|
|
($feature_name:expr, $service_name:ident, $udp_socket:ty, $udp_socket_from_std:tt) => {
|
|
|
|
|
2018-12-10 16:00:16 +01:00
|
|
|
/// A running service that discovers libp2p peers and responds to other libp2p peers' queries on
|
|
|
|
/// the local network.
|
|
|
|
///
|
|
|
|
/// # Usage
|
2019-01-26 23:57:53 +01:00
|
|
|
///
|
2018-12-10 16:00:16 +01:00
|
|
|
/// In order to use mDNS to discover peers on the local network, use the `MdnsService`. This is
|
|
|
|
/// done by creating a `MdnsService` then polling it in the same way as you would poll a stream.
|
|
|
|
///
|
|
|
|
/// Polling the `MdnsService` can produce either an `MdnsQuery`, corresponding to an mDNS query
|
|
|
|
/// received by another node on the local network, or an `MdnsResponse` corresponding to a response
|
|
|
|
/// to a query previously emitted locally. The `MdnsService` will automatically produce queries,
|
|
|
|
/// which means that you will receive responses automatically.
|
|
|
|
///
|
|
|
|
/// When you receive an `MdnsQuery`, use the `respond` method to send back an answer to the node
|
|
|
|
/// that emitted the query.
|
|
|
|
///
|
|
|
|
/// When you receive an `MdnsResponse`, use the provided methods to query the information received
|
|
|
|
/// in the response.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use futures::prelude::*;
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
/// # use futures::executor::block_on;
|
|
|
|
/// # use libp2p_core::{identity, Multiaddr, PeerId};
|
2020-08-18 14:51:03 +02:00
|
|
|
/// # use libp2p_mdns::service::{MdnsPacket, build_query_response, build_service_discovery_response};
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
/// # use std::{io, time::Duration, task::Poll};
|
2018-12-10 16:00:16 +01:00
|
|
|
/// # fn main() {
|
2019-03-11 13:42:53 +01:00
|
|
|
/// # let my_peer_id = PeerId::from(identity::Keypair::generate_ed25519().public());
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
/// # let my_listened_addrs: Vec<Multiaddr> = vec![];
|
2020-08-18 14:51:03 +02:00
|
|
|
/// # async {
|
|
|
|
/// # #[cfg(feature = "async-std")]
|
|
|
|
/// # let mut service = libp2p_mdns::service::MdnsService::new().unwrap();
|
|
|
|
/// # #[cfg(feature = "tokio")]
|
|
|
|
/// # let mut service = libp2p_mdns::service::TokioMdnsService::new().unwrap();
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
/// let _future_to_poll = async {
|
|
|
|
/// let (mut service, packet) = service.next().await;
|
2018-12-10 16:00:16 +01:00
|
|
|
///
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
/// match packet {
|
|
|
|
/// MdnsPacket::Query(query) => {
|
|
|
|
/// println!("Query from {:?}", query.remote_addr());
|
|
|
|
/// let resp = build_query_response(
|
|
|
|
/// query.query_id(),
|
|
|
|
/// my_peer_id.clone(),
|
|
|
|
/// vec![].into_iter(),
|
|
|
|
/// Duration::from_secs(120),
|
|
|
|
/// ).unwrap();
|
|
|
|
/// service.enqueue_response(resp);
|
|
|
|
/// }
|
|
|
|
/// MdnsPacket::Response(response) => {
|
|
|
|
/// for peer in response.discovered_peers() {
|
|
|
|
/// println!("Discovered peer {:?}", peer.id());
|
|
|
|
/// for addr in peer.addresses() {
|
|
|
|
/// println!("Address = {:?}", addr);
|
2018-12-10 16:00:16 +01:00
|
|
|
/// }
|
|
|
|
/// }
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
/// }
|
|
|
|
/// MdnsPacket::ServiceDiscovery(disc) => {
|
|
|
|
/// let resp = build_service_discovery_response(
|
|
|
|
/// disc.query_id(),
|
|
|
|
/// Duration::from_secs(120),
|
|
|
|
/// );
|
|
|
|
/// service.enqueue_response(resp);
|
2018-12-10 16:00:16 +01:00
|
|
|
/// }
|
|
|
|
/// }
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
/// };
|
2020-08-18 14:51:03 +02:00
|
|
|
/// # };
|
2018-12-10 16:00:16 +01:00
|
|
|
/// # }
|
2020-08-18 14:51:03 +02:00
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = $feature_name)))]
|
|
|
|
pub struct $service_name {
|
2018-12-10 16:00:16 +01:00
|
|
|
/// Main socket for listening.
|
2020-08-18 14:51:03 +02:00
|
|
|
socket: $udp_socket,
|
|
|
|
|
2018-12-10 16:00:16 +01:00
|
|
|
/// Socket for sending queries on the network.
|
2020-08-18 14:51:03 +02:00
|
|
|
query_socket: $udp_socket,
|
|
|
|
|
2018-12-10 16:00:16 +01:00
|
|
|
/// Interval for sending queries.
|
|
|
|
query_interval: Interval,
|
|
|
|
/// Whether we send queries on the network at all.
|
|
|
|
/// Note that we still need to have an interval for querying, as we need to wake up the socket
|
|
|
|
/// regularly to recover from errors. Otherwise we could simply use an `Option<Interval>`.
|
|
|
|
silent: bool,
|
|
|
|
/// Buffer used for receiving data from the main socket.
|
2020-10-05 10:03:21 +02:00
|
|
|
/// RFC6762 discourages packets larger than the interface MTU, but allows sizes of up to 9000
|
|
|
|
/// bytes, if it can be ensured that all participating devices can handle such large packets.
|
|
|
|
/// For computers with several interfaces and IP addresses responses can easily reach sizes in
|
|
|
|
/// the range of 3000 bytes, so 4096 seems sensible for now. For more information see
|
|
|
|
/// [rfc6762](https://tools.ietf.org/html/rfc6762#page-46).
|
|
|
|
recv_buffer: [u8; 4096],
|
2018-12-10 16:00:16 +01:00
|
|
|
/// Buffers pending to send on the main socket.
|
|
|
|
send_buffers: Vec<Vec<u8>>,
|
|
|
|
/// Buffers pending to send on the query socket.
|
|
|
|
query_send_buffers: Vec<Vec<u8>>,
|
|
|
|
}
|
|
|
|
|
2020-08-18 14:51:03 +02:00
|
|
|
impl $service_name {
|
2018-12-10 16:00:16 +01:00
|
|
|
/// Starts a new mDNS service.
|
2020-08-18 14:51:03 +02:00
|
|
|
pub fn new() -> io::Result<$service_name> {
|
2020-01-15 13:43:09 +01:00
|
|
|
Self::new_inner(false)
|
2018-12-10 16:00:16 +01:00
|
|
|
}
|
|
|
|
|
2020-03-23 12:51:20 +01:00
|
|
|
/// Same as `new`, but we don't automatically send queries on the network.
|
2020-08-18 14:51:03 +02:00
|
|
|
pub fn silent() -> io::Result<$service_name> {
|
2020-01-15 13:43:09 +01:00
|
|
|
Self::new_inner(true)
|
2018-12-10 16:00:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Starts a new mDNS service.
|
2020-08-18 14:51:03 +02:00
|
|
|
fn new_inner(silent: bool) -> io::Result<$service_name> {
|
|
|
|
let std_socket = {
|
2018-12-10 16:00:16 +01:00
|
|
|
#[cfg(unix)]
|
|
|
|
fn platform_specific(s: &net2::UdpBuilder) -> io::Result<()> {
|
|
|
|
net2::unix::UnixUdpBuilderExt::reuse_port(s, true)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
fn platform_specific(_: &net2::UdpBuilder) -> io::Result<()> { Ok(()) }
|
|
|
|
let builder = net2::UdpBuilder::new_v4()?;
|
|
|
|
builder.reuse_address(true)?;
|
|
|
|
platform_specific(&builder)?;
|
|
|
|
builder.bind(("0.0.0.0", 5353))?
|
|
|
|
};
|
|
|
|
|
2020-08-18 14:51:03 +02:00
|
|
|
let socket = $udp_socket_from_std(std_socket)?;
|
|
|
|
// Given that we pass an IP address to bind, which does not need to be resolved, we can
|
|
|
|
// use std::net::UdpSocket::bind, instead of its async counterpart from async-std.
|
|
|
|
let query_socket = $udp_socket_from_std(
|
|
|
|
std::net::UdpSocket::bind((Ipv4Addr::from([0u8, 0, 0, 0]), 0u16))?,
|
|
|
|
)?;
|
|
|
|
|
2018-12-10 16:00:16 +01:00
|
|
|
socket.set_multicast_loop_v4(true)?;
|
|
|
|
socket.set_multicast_ttl_v4(255)?;
|
|
|
|
// TODO: correct interfaces?
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
socket.join_multicast_v4(From::from([224, 0, 0, 251]), Ipv4Addr::UNSPECIFIED)?;
|
2018-12-10 16:00:16 +01:00
|
|
|
|
2020-08-18 14:51:03 +02:00
|
|
|
Ok($service_name {
|
2018-12-10 16:00:16 +01:00
|
|
|
socket,
|
2020-08-18 14:51:03 +02:00
|
|
|
query_socket,
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
query_interval: Interval::new_at(Instant::now(), Duration::from_secs(20)),
|
2018-12-10 16:00:16 +01:00
|
|
|
silent,
|
2020-10-05 10:03:21 +02:00
|
|
|
recv_buffer: [0; 4096],
|
2018-12-10 16:00:16 +01:00
|
|
|
send_buffers: Vec::new(),
|
|
|
|
query_send_buffers: Vec::new(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
pub fn enqueue_response(&mut self, rsp: Vec<u8>) {
|
|
|
|
self.send_buffers.push(rsp);
|
|
|
|
}
|
2018-12-10 16:00:16 +01:00
|
|
|
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
/// Returns a future resolving to itself and the next received `MdnsPacket`.
|
|
|
|
//
|
|
|
|
// **Note**: Why does `next` take ownership of itself?
|
|
|
|
//
|
|
|
|
// `MdnsService::next` needs to be called from within `NetworkBehaviour`
|
2020-01-06 11:13:23 +01:00
|
|
|
// implementations. Given that traits cannot have async methods the
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
// respective `NetworkBehaviour` implementation needs to somehow keep the
|
|
|
|
// Future returned by `MdnsService::next` across classic `poll`
|
|
|
|
// invocations. The instance method `next` can either take a reference or
|
|
|
|
// ownership of itself:
|
|
|
|
//
|
|
|
|
// 1. Taking a reference - If `MdnsService::poll` takes a reference to
|
|
|
|
// `&self` the respective `NetworkBehaviour` implementation would need to
|
|
|
|
// keep both the Future as well as its `MdnsService` instance across poll
|
|
|
|
// invocations. Given that in this case the Future would have a reference
|
|
|
|
// to `MdnsService`, the `NetworkBehaviour` implementation struct would
|
|
|
|
// need to be self-referential which is not possible without unsafe code in
|
|
|
|
// Rust.
|
|
|
|
//
|
|
|
|
// 2. Taking ownership - Instead `MdnsService::next` takes ownership of
|
|
|
|
// self and returns it alongside an `MdnsPacket` once the actual future
|
|
|
|
// resolves, not forcing self-referential structures on the caller.
|
|
|
|
pub async fn next(mut self) -> (Self, MdnsPacket) {
|
|
|
|
loop {
|
|
|
|
// Flush the send buffer of the main socket.
|
|
|
|
while !self.send_buffers.is_empty() {
|
|
|
|
let to_send = self.send_buffers.remove(0);
|
|
|
|
|
|
|
|
match self.socket.send_to(&to_send, *IPV4_MDNS_MULTICAST_ADDRESS).await {
|
|
|
|
Ok(bytes_written) => {
|
|
|
|
debug_assert_eq!(bytes_written, to_send.len());
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
// Errors are non-fatal because they can happen for example if we lose
|
|
|
|
// connection to the network.
|
|
|
|
self.send_buffers.clear();
|
|
|
|
break;
|
|
|
|
}
|
2018-12-10 16:00:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
// Flush the query send buffer.
|
|
|
|
while !self.query_send_buffers.is_empty() {
|
|
|
|
let to_send = self.query_send_buffers.remove(0);
|
|
|
|
|
|
|
|
match self.query_socket.send_to(&to_send, *IPV4_MDNS_MULTICAST_ADDRESS).await {
|
|
|
|
Ok(bytes_written) => {
|
|
|
|
debug_assert_eq!(bytes_written, to_send.len());
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
// Errors are non-fatal because they can happen for example if we lose
|
|
|
|
// connection to the network.
|
|
|
|
self.query_send_buffers.clear();
|
|
|
|
break;
|
|
|
|
}
|
2018-12-10 16:00:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
// Either (left) listen for incoming packets or (right) send query packets whenever the
|
|
|
|
// query interval fires.
|
|
|
|
let selected_output = match futures::future::select(
|
|
|
|
Box::pin(self.socket.recv_from(&mut self.recv_buffer)),
|
|
|
|
Box::pin(self.query_interval.next()),
|
|
|
|
).await {
|
|
|
|
future::Either::Left((recved, _)) => Left(recved),
|
|
|
|
future::Either::Right(_) => Right(()),
|
|
|
|
};
|
|
|
|
|
|
|
|
match selected_output {
|
|
|
|
Left(left) => match left {
|
|
|
|
Ok((len, from)) => {
|
|
|
|
match MdnsPacket::new_from_bytes(&self.recv_buffer[..len], from) {
|
|
|
|
Some(packet) => return (self, packet),
|
|
|
|
None => {},
|
2018-12-10 16:00:16 +01:00
|
|
|
}
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
},
|
2018-12-10 16:00:16 +01:00
|
|
|
Err(_) => {
|
2020-01-06 11:13:23 +01:00
|
|
|
// Errors are non-fatal and can happen if we get disconnected from the network.
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
// The query interval will wake up the task at some point so that we can try again.
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Right(_) => {
|
|
|
|
// Ensure underlying task is woken up on the next interval tick.
|
|
|
|
while let Some(_) = self.query_interval.next().now_or_never() {};
|
|
|
|
|
|
|
|
if !self.silent {
|
|
|
|
let query = dns::build_query();
|
|
|
|
self.query_send_buffers.push(query.to_vec());
|
2018-12-10 16:00:16 +01:00
|
|
|
}
|
|
|
|
}
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
};
|
|
|
|
}
|
2018-12-10 16:00:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 14:51:03 +02:00
|
|
|
impl fmt::Debug for $service_name {
|
2019-02-11 14:58:15 +01:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-08-18 14:51:03 +02:00
|
|
|
fmt.debug_struct("$service_name")
|
2018-12-10 16:00:16 +01:00
|
|
|
.field("silent", &self.silent)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 14:51:03 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "async-std")]
|
|
|
|
codegen!("async-std", MdnsService, async_std::net::UdpSocket, (|socket| Ok::<_, std::io::Error>(async_std::net::UdpSocket::from(socket))));
|
|
|
|
|
2020-11-03 14:09:10 +01:00
|
|
|
// Note: Tokio's UdpSocket::from_std does not set the socket into non-blocking mode.
|
2020-08-18 14:51:03 +02:00
|
|
|
#[cfg(feature = "tokio")]
|
2020-11-03 14:09:10 +01:00
|
|
|
codegen!("tokio", TokioMdnsService, tokio::net::UdpSocket, (|socket: std::net::UdpSocket| { socket.set_nonblocking(true); tokio::net::UdpSocket::from_std(socket) }));
|
2020-08-18 14:51:03 +02:00
|
|
|
|
|
|
|
|
2018-12-10 16:00:16 +01:00
|
|
|
/// A valid mDNS packet received by the service.
|
|
|
|
#[derive(Debug)]
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
pub enum MdnsPacket {
|
2018-12-10 16:00:16 +01:00
|
|
|
/// A query made by a remote.
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
Query(MdnsQuery),
|
2018-12-10 16:00:16 +01:00
|
|
|
/// A response sent by a remote in response to one of our queries.
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
Response(MdnsResponse),
|
2018-12-10 16:00:16 +01:00
|
|
|
/// A request for service discovery.
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
ServiceDiscovery(MdnsServiceDiscovery),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MdnsPacket {
|
|
|
|
fn new_from_bytes(buf: &[u8], from: SocketAddr) -> Option<MdnsPacket> {
|
|
|
|
match Packet::parse(buf) {
|
|
|
|
Ok(packet) => {
|
|
|
|
if packet.header.query {
|
|
|
|
if packet
|
|
|
|
.questions
|
|
|
|
.iter()
|
|
|
|
.any(|q| q.qname.to_string().as_bytes() == SERVICE_NAME)
|
|
|
|
{
|
|
|
|
let query = MdnsPacket::Query(MdnsQuery {
|
|
|
|
from,
|
|
|
|
query_id: packet.header.id,
|
|
|
|
});
|
|
|
|
return Some(query);
|
|
|
|
} else if packet
|
|
|
|
.questions
|
|
|
|
.iter()
|
|
|
|
.any(|q| q.qname.to_string().as_bytes() == META_QUERY_SERVICE)
|
|
|
|
{
|
|
|
|
// TODO: what if multiple questions, one with SERVICE_NAME and one with META_QUERY_SERVICE?
|
|
|
|
let discovery = MdnsPacket::ServiceDiscovery(
|
|
|
|
MdnsServiceDiscovery {
|
|
|
|
from,
|
|
|
|
query_id: packet.header.id,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return Some(discovery);
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let resp = MdnsPacket::Response(MdnsResponse::new (
|
|
|
|
packet,
|
|
|
|
from,
|
|
|
|
));
|
|
|
|
return Some(resp);
|
|
|
|
}
|
|
|
|
}
|
2020-10-05 10:03:21 +02:00
|
|
|
Err(err) => {
|
|
|
|
warn!("Parsing mdns packet failed: {:?}", err);
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-10 16:00:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A received mDNS query.
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
pub struct MdnsQuery {
|
2018-12-10 16:00:16 +01:00
|
|
|
/// Sender of the address.
|
|
|
|
from: SocketAddr,
|
|
|
|
/// Id of the received DNS query. We need to pass this ID back in the results.
|
|
|
|
query_id: u16,
|
|
|
|
}
|
|
|
|
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
impl MdnsQuery {
|
2018-12-10 16:00:16 +01:00
|
|
|
/// Source address of the packet.
|
|
|
|
pub fn remote_addr(&self) -> &SocketAddr {
|
|
|
|
&self.from
|
|
|
|
}
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
|
|
|
|
/// Query id of the packet.
|
|
|
|
pub fn query_id(&self) -> u16 {
|
|
|
|
self.query_id
|
|
|
|
}
|
2018-12-10 16:00:16 +01:00
|
|
|
}
|
|
|
|
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
impl fmt::Debug for MdnsQuery {
|
2019-02-11 14:58:15 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-12-10 16:00:16 +01:00
|
|
|
f.debug_struct("MdnsQuery")
|
|
|
|
.field("from", self.remote_addr())
|
|
|
|
.field("query_id", &self.query_id)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A received mDNS service discovery query.
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
pub struct MdnsServiceDiscovery {
|
2018-12-10 16:00:16 +01:00
|
|
|
/// Sender of the address.
|
|
|
|
from: SocketAddr,
|
|
|
|
/// Id of the received DNS query. We need to pass this ID back in the results.
|
|
|
|
query_id: u16,
|
|
|
|
}
|
|
|
|
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
impl MdnsServiceDiscovery {
|
2018-12-10 16:00:16 +01:00
|
|
|
/// Source address of the packet.
|
|
|
|
pub fn remote_addr(&self) -> &SocketAddr {
|
|
|
|
&self.from
|
|
|
|
}
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
|
|
|
|
/// Query id of the packet.
|
|
|
|
pub fn query_id(&self) -> u16 {
|
|
|
|
self.query_id
|
|
|
|
}
|
2018-12-10 16:00:16 +01:00
|
|
|
}
|
|
|
|
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
impl fmt::Debug for MdnsServiceDiscovery {
|
2019-02-11 14:58:15 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-12-10 16:00:16 +01:00
|
|
|
f.debug_struct("MdnsServiceDiscovery")
|
|
|
|
.field("from", self.remote_addr())
|
|
|
|
.field("query_id", &self.query_id)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A received mDNS response.
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
pub struct MdnsResponse {
|
|
|
|
peers: Vec<MdnsPeer>,
|
2018-12-10 16:00:16 +01:00
|
|
|
from: SocketAddr,
|
|
|
|
}
|
|
|
|
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
impl MdnsResponse {
|
|
|
|
/// Creates a new `MdnsResponse` based on the provided `Packet`.
|
2020-07-27 20:27:33 +00:00
|
|
|
fn new(packet: Packet<'_>, from: SocketAddr) -> MdnsResponse {
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
let peers = packet.answers.iter().filter_map(|record| {
|
2018-12-10 16:00:16 +01:00
|
|
|
if record.name.to_string().as_bytes() != SERVICE_NAME {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let record_value = match record.data {
|
|
|
|
RData::PTR(record) => record.0.to_string(),
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
2019-11-06 16:09:15 +01:00
|
|
|
let mut peer_name = match record_value.rsplitn(4, |c| c == '.').last() {
|
|
|
|
Some(n) => n.to_owned(),
|
|
|
|
None => return None,
|
2018-12-10 16:00:16 +01:00
|
|
|
};
|
|
|
|
|
2019-11-06 16:09:15 +01:00
|
|
|
// if we have a segmented name, remove the '.'
|
|
|
|
peer_name.retain(|c| c != '.');
|
|
|
|
|
2018-12-10 16:00:16 +01:00
|
|
|
let peer_id = match data_encoding::BASE32_DNSCURVE.decode(peer_name.as_bytes()) {
|
|
|
|
Ok(bytes) => match PeerId::from_bytes(bytes) {
|
|
|
|
Ok(id) => id,
|
|
|
|
Err(_) => return None,
|
|
|
|
},
|
|
|
|
Err(_) => return None,
|
|
|
|
};
|
|
|
|
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
Some(MdnsPeer::new (
|
|
|
|
&packet,
|
2018-12-10 16:00:16 +01:00
|
|
|
record_value,
|
|
|
|
peer_id,
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
record.ttl,
|
|
|
|
))
|
|
|
|
}).collect();
|
|
|
|
|
|
|
|
MdnsResponse {
|
|
|
|
peers,
|
|
|
|
from,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the list of peers that have been reported in this packet.
|
|
|
|
///
|
|
|
|
/// > **Note**: Keep in mind that this will also contain the responses we sent ourselves.
|
|
|
|
pub fn discovered_peers(&self) -> impl Iterator<Item = &MdnsPeer> {
|
|
|
|
self.peers.iter()
|
2018-12-10 16:00:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Source address of the packet.
|
|
|
|
#[inline]
|
|
|
|
pub fn remote_addr(&self) -> &SocketAddr {
|
|
|
|
&self.from
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
impl fmt::Debug for MdnsResponse {
|
2019-02-11 14:58:15 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-12-10 16:00:16 +01:00
|
|
|
f.debug_struct("MdnsResponse")
|
|
|
|
.field("from", self.remote_addr())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A peer discovered by the service.
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
pub struct MdnsPeer {
|
|
|
|
addrs: Vec<Multiaddr>,
|
2018-12-10 16:00:16 +01:00
|
|
|
/// Id of the peer.
|
|
|
|
peer_id: PeerId,
|
2019-01-26 23:57:53 +01:00
|
|
|
/// TTL of the record in seconds.
|
|
|
|
ttl: u32,
|
2018-12-10 16:00:16 +01:00
|
|
|
}
|
|
|
|
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
impl MdnsPeer {
|
|
|
|
/// Creates a new `MdnsPeer` based on the provided `Packet`.
|
2020-07-27 20:27:33 +00:00
|
|
|
pub fn new(packet: &Packet<'_>, record_value: String, my_peer_id: PeerId, ttl: u32) -> MdnsPeer {
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
let addrs = packet
|
2018-12-10 16:00:16 +01:00
|
|
|
.additional
|
|
|
|
.iter()
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
.filter_map(|add_record| {
|
|
|
|
if add_record.name.to_string() != record_value {
|
2018-12-10 16:00:16 +01:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let RData::TXT(ref txt) = add_record.data {
|
|
|
|
Some(txt)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.flat_map(|txt| txt.iter())
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
.filter_map(|txt| {
|
2018-12-10 16:00:16 +01:00
|
|
|
// TODO: wrong, txt can be multiple character strings
|
|
|
|
let addr = match dns::decode_character_string(txt) {
|
|
|
|
Ok(a) => a,
|
|
|
|
Err(_) => return None,
|
|
|
|
};
|
|
|
|
if !addr.starts_with(b"dnsaddr=") {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let addr = match str::from_utf8(&addr[8..]) {
|
|
|
|
Ok(a) => a,
|
|
|
|
Err(_) => return None,
|
|
|
|
};
|
|
|
|
let mut addr = match addr.parse::<Multiaddr>() {
|
|
|
|
Ok(a) => a,
|
|
|
|
Err(_) => return None,
|
|
|
|
};
|
|
|
|
match addr.pop() {
|
2020-01-29 11:33:19 +01:00
|
|
|
Some(Protocol::P2p(peer_id)) => {
|
|
|
|
if let Ok(peer_id) = PeerId::try_from(peer_id) {
|
|
|
|
if peer_id != my_peer_id {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
},
|
2018-12-10 16:00:16 +01:00
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
Some(addr)
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
}).collect();
|
|
|
|
|
|
|
|
MdnsPeer {
|
|
|
|
addrs,
|
|
|
|
peer_id: my_peer_id.clone(),
|
|
|
|
ttl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the id of the peer.
|
|
|
|
#[inline]
|
|
|
|
pub fn id(&self) -> &PeerId {
|
|
|
|
&self.peer_id
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the requested time-to-live for the record.
|
|
|
|
#[inline]
|
|
|
|
pub fn ttl(&self) -> Duration {
|
|
|
|
Duration::from_secs(u64::from(self.ttl))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the list of addresses the peer says it is listening on.
|
|
|
|
///
|
|
|
|
/// Filters out invalid addresses.
|
|
|
|
pub fn addresses(&self) -> &Vec<Multiaddr> {
|
|
|
|
&self.addrs
|
2018-12-10 16:00:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
impl fmt::Debug for MdnsPeer {
|
2019-02-11 14:58:15 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-12-10 16:00:16 +01:00
|
|
|
f.debug_struct("MdnsPeer")
|
|
|
|
.field("peer_id", &self.peer_id)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-18 14:51:03 +02:00
|
|
|
macro_rules! testgen {
|
|
|
|
($runtime_name:ident, $service_name:ty, $block_on_fn:tt) => {
|
|
|
|
mod $runtime_name {
|
2020-11-17 11:15:20 +01:00
|
|
|
use libp2p_core::{PeerId, multihash::{Code, MultihashDigest}};
|
2020-08-18 14:51:03 +02:00
|
|
|
use std::time::Duration;
|
|
|
|
use crate::service::MdnsPacket;
|
|
|
|
|
|
|
|
fn discover(peer_id: PeerId) {
|
|
|
|
let fut = async {
|
|
|
|
let mut service = <$service_name>::new().unwrap();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let next = service.next().await;
|
|
|
|
service = next.0;
|
|
|
|
|
|
|
|
match next.1 {
|
|
|
|
MdnsPacket::Query(query) => {
|
|
|
|
let resp = crate::dns::build_query_response(
|
|
|
|
query.query_id(),
|
|
|
|
peer_id.clone(),
|
|
|
|
vec![].into_iter(),
|
|
|
|
Duration::from_secs(120),
|
|
|
|
).unwrap();
|
|
|
|
service.enqueue_response(resp);
|
|
|
|
}
|
|
|
|
MdnsPacket::Response(response) => {
|
|
|
|
for peer in response.discovered_peers() {
|
|
|
|
if peer.id() == &peer_id {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-10 16:00:16 +01:00
|
|
|
}
|
|
|
|
}
|
2020-08-18 14:51:03 +02:00
|
|
|
MdnsPacket::ServiceDiscovery(_) => panic!(
|
|
|
|
"did not expect a service discovery packet",
|
|
|
|
)
|
2018-12-10 16:00:16 +01:00
|
|
|
}
|
|
|
|
}
|
2020-08-18 14:51:03 +02:00
|
|
|
};
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
|
2020-08-18 14:51:03 +02:00
|
|
|
$block_on_fn(Box::pin(fut));
|
|
|
|
}
|
|
|
|
|
|
|
|
// As of today the underlying UDP socket is not stubbed out. Thus tests run in parallel to
|
|
|
|
// this unit tests inter fear with it. Test needs to be run in sequence to ensure test
|
|
|
|
// properties.
|
|
|
|
#[test]
|
|
|
|
fn respect_query_interval() {
|
2020-10-09 20:18:39 +08:00
|
|
|
let own_ips: Vec<std::net::IpAddr> = if_addrs::get_if_addrs().unwrap()
|
2020-08-18 14:51:03 +02:00
|
|
|
.into_iter()
|
|
|
|
.map(|i| i.addr.ip())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let fut = async {
|
|
|
|
let mut service = <$service_name>::new().unwrap();
|
|
|
|
|
|
|
|
let mut sent_queries = vec![];
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let next = service.next().await;
|
|
|
|
service = next.0;
|
|
|
|
|
|
|
|
match next.1 {
|
|
|
|
MdnsPacket::Query(query) => {
|
|
|
|
// Ignore queries from other nodes.
|
|
|
|
let source_ip = query.remote_addr().ip();
|
|
|
|
if !own_ips.contains(&source_ip) {
|
|
|
|
continue;
|
|
|
|
}
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
|
2020-08-18 14:51:03 +02:00
|
|
|
sent_queries.push(query);
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
|
2020-08-18 14:51:03 +02:00
|
|
|
if sent_queries.len() > 1 {
|
|
|
|
return;
|
|
|
|
}
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
}
|
2020-08-18 14:51:03 +02:00
|
|
|
// Ignore response packets. We don't stub out the UDP socket, thus this is
|
|
|
|
// either random noise from the network, or noise from other unit tests
|
|
|
|
// running in parallel.
|
|
|
|
MdnsPacket::Response(_) => {},
|
|
|
|
MdnsPacket::ServiceDiscovery(_) => {
|
|
|
|
panic!("Did not expect a service discovery packet.");
|
|
|
|
},
|
misc/mdns: Update to futures-preview (#1247)
* misc/mdns/service: Use async std with stack pinned futures
* misc/mdns: Define mdns broadcast address as lazy static
* misc/mdns: Drop future before borrowing their arguments again
* misc/mdns: Send queries on query socket, not socket
* misc/mdns: Use poll_next_unpin on query interval stream
* misc/mdns: Ensure underlying task is woken up on next interval tick
* misc/mdns: Wrap match expression in scope to drop future early
* misc/mdns: Adjust 'discovery_ourselves' test
* misc/mdns: Make query interval fire instantly on first tick
This is an optimization only important for short lived use cases, e.g.
unit tests. Instead of waiting for 20 seconds at first, the query
interval fires right away and thereby the service makes progress
instantly.
* misc/mdns: Adjust MdnsService documentation tests
* misc/mdns: Do not drop UDP socket send and reicv futures
Libp2p-mdns uses the async-std crate for network io. This crate only
offers async send and receive functions. In order to use this in non
async/await functions one needs to keep the future returned by the crate
functions around across `poll` invocations.
The future returned by the crate functions references the io resource.
Thus one has to keep both the io resource as well as the future
referencing it. This results in a self-referencing struct which is not
possible to create with safe Rust.
Instead, by having `MdnsService::next` (former `MdnsService::poll`) take
ownership of `self`, the Rust async magic takes care of the above (See
code comments for more details).
As a (negative) side effect, given that `MdnsService::next` takes
ownership of `self`, there is nothing to bind the lifetime of the
returned `MdnsPacket` to. With no better solution in mind, this patch
makes `MdnsPacket` static, not referencing the `MdnsService` receive
buffer.
* misc/mdns: Fix code comments and remove *if Free* TODO
* misc/mdns: Minor refactorings
* misc/mdns: Build responses in behaviour.rs directly
* misc/mdns: Move response ttl duration to constant
* misc/mdns: Remove optimization todo comment
* misc/mdns: Add query interval test
* misc/mdns: Move packet parsing into MdnPacket impl
* misc/mdns: Don't have receiving packets starve the query interval
When we 'await' on receiving a packet on the udp socket without
receiving a single packet we starve the remaining logic of the mdns
service, in this case the logic triggered on the receive interval.
* misc/mdns: Add debug_assert to MaybeBusyMdnsService check
* misc/mdns: Implement Debug for MaybeBusyMdnsService
* misc/mdns: Make ownership note a normal comment, not a doc comment
* misc/mdns: Have discovered_peers return an iterator
2019-11-20 13:25:12 +01:00
|
|
|
}
|
|
|
|
}
|
2020-08-18 14:51:03 +02:00
|
|
|
};
|
2018-12-10 16:00:16 +01:00
|
|
|
|
2020-08-18 14:51:03 +02:00
|
|
|
$block_on_fn(Box::pin(fut));
|
|
|
|
}
|
2019-11-06 16:09:15 +01:00
|
|
|
|
2020-08-18 14:51:03 +02:00
|
|
|
#[test]
|
|
|
|
fn discover_normal_peer_id() {
|
|
|
|
discover(PeerId::random())
|
|
|
|
}
|
2019-11-06 16:09:15 +01:00
|
|
|
|
2020-08-18 14:51:03 +02:00
|
|
|
#[test]
|
|
|
|
fn discover_long_peer_id() {
|
|
|
|
let max_value = String::from_utf8(vec![b'f'; 42]).unwrap();
|
2020-11-17 11:15:20 +01:00
|
|
|
let hash = Code::Identity.digest(max_value.as_ref());
|
2020-08-18 14:51:03 +02:00
|
|
|
discover(PeerId::from_multihash(hash).unwrap())
|
|
|
|
}
|
|
|
|
}
|
2019-11-06 16:09:15 +01:00
|
|
|
}
|
2020-08-18 14:51:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "async-std")]
|
|
|
|
testgen!(
|
|
|
|
async_std,
|
|
|
|
crate::service::MdnsService,
|
|
|
|
(|fut| async_std::task::block_on::<_, ()>(fut))
|
|
|
|
);
|
|
|
|
|
|
|
|
#[cfg(feature = "tokio")]
|
|
|
|
testgen!(
|
|
|
|
tokio,
|
|
|
|
crate::service::TokioMdnsService,
|
|
|
|
(|fut| tokio::runtime::Runtime::new().unwrap().block_on::<futures::future::BoxFuture<()>>(fut))
|
|
|
|
);
|
2018-12-10 16:00:16 +01:00
|
|
|
}
|