2018-11-24 13:55:18 +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.
|
|
|
|
|
2019-11-25 10:45:04 +01:00
|
|
|
use async_std::task;
|
2018-12-10 16:00:16 +01:00
|
|
|
use libp2p::mdns::service::{MdnsPacket, MdnsService};
|
2019-11-25 10:45:04 +01:00
|
|
|
use std::error::Error;
|
2018-11-24 13:55:18 +01:00
|
|
|
|
2019-11-25 10:45:04 +01:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
|
|
// This example provides passive discovery of the libp2p nodes on the
|
|
|
|
// network that send mDNS queries and answers.
|
|
|
|
task::block_on(async move {
|
2020-12-03 13:30:52 +01:00
|
|
|
let mut service = MdnsService::new().await?;
|
2018-11-24 13:55:18 +01:00
|
|
|
loop {
|
2019-11-25 10:45:04 +01:00
|
|
|
let (srv, packet) = service.next().await;
|
2018-11-24 13:55:18 +01:00
|
|
|
match packet {
|
|
|
|
MdnsPacket::Query(query) => {
|
|
|
|
// We detected a libp2p mDNS query on the network. In a real application, you
|
|
|
|
// probably want to answer this query by doing `query.respond(...)`.
|
|
|
|
println!("Detected query from {:?}", query.remote_addr());
|
|
|
|
}
|
|
|
|
MdnsPacket::Response(response) => {
|
|
|
|
// We detected a libp2p mDNS response on the network. Responses are for
|
|
|
|
// everyone and not just for the requester, which makes it possible to
|
|
|
|
// passively listen.
|
|
|
|
for peer in response.discovered_peers() {
|
|
|
|
println!("Discovered peer {:?}", peer.id());
|
|
|
|
// These are the self-reported addresses of the peer we just discovered.
|
|
|
|
for addr in peer.addresses() {
|
|
|
|
println!(" Address = {:?}", addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MdnsPacket::ServiceDiscovery(query) => {
|
|
|
|
// The last possibility is a service detection query from DNS-SD.
|
|
|
|
// Just like `Query`, in a real application you probably want to call
|
|
|
|
// `query.respond`.
|
|
|
|
println!("Detected service query from {:?}", query.remote_addr());
|
|
|
|
}
|
|
|
|
}
|
2019-11-25 10:45:04 +01:00
|
|
|
service = srv
|
2018-11-24 13:55:18 +01:00
|
|
|
}
|
2019-11-25 10:45:04 +01:00
|
|
|
})
|
2018-11-24 13:55:18 +01:00
|
|
|
}
|