Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add libp2p-mdns #590

Merged
merged 20 commits into from
Nov 24, 2018
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,21 @@ tokio-io = "0.1"

[target.'cfg(not(target_os = "emscripten"))'.dependencies]
libp2p-dns = { path = "./transports/dns" }
libp2p-mdns = { path = "./misc/mdns" }
libp2p-tcp-transport = { path = "./transports/tcp" }
tokio-current-thread = "0.1"

[target.'cfg(target_os = "emscripten")'.dependencies]
stdweb = { version = "0.1.3", default-features = false }

[dev-dependencies]
bigint = "4.2"
env_logger = "0.5.4"
rand = "0.4"
structopt = "0.2"
rand = "0.5"
tokio = "0.1"
tokio-io = "0.1"
tokio-stdin = "0.1"

[workspace]
members = [
"core",
"misc/mdns",
"misc/multiaddr",
"misc/multihash",
"misc/multistream-select",
Expand Down
14 changes: 14 additions & 0 deletions core/src/peer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ impl From<PublicKey> for PeerId {
}
}

impl PartialEq<multihash::Multihash> for PeerId {
#[inline]
fn eq(&self, other: &multihash::Multihash) -> bool {
&self.multihash == other
}
}

impl PartialEq<PeerId> for multihash::Multihash {
#[inline]
fn eq(&self, other: &PeerId) -> bool {
self == &other.multihash
}
}

impl Into<multihash::Multihash> for PeerId {
#[inline]
fn into(self) -> multihash::Multihash {
Expand Down
76 changes: 76 additions & 0 deletions examples/mdns-passive-discovery.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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.

extern crate futures;
extern crate libp2p;
extern crate rand;
extern crate tokio;

use futures::prelude::*;
use libp2p::mdns::{MdnsPacket, MdnsService};
use std::io;

fn main() {
// This example provides passive discovery of the libp2p nodes on the network that send
// mDNS queries and answers.

// We start by creating the service.
let mut service = MdnsService::new().expect("Error while creating mDNS service");

// Create a never-ending `Future` that polls the service for events.
let future = futures::future::poll_fn(move || -> Poll<(), io::Error> {
loop {
// Grab the next available packet from the service.
let packet = match service.poll() {
Async::Ready(packet) => packet,
Async::NotReady => return Ok(Async::NotReady),
};

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());
}
}
}
});

// Blocks the thread until the future runs to completion (which will never happen).
tokio::run(future.map_err(|err| panic!("{:?}", err)));
}
21 changes: 21 additions & 0 deletions misc/mdns/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "libp2p-mdns"
version = "0.1.0"
description = "Implementation of the libp2p mDNS discovery method"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"

[dependencies]
data-encoding = "2.0"
dns-parser = "0.8"
futures = "0.1"
libp2p-core = { path = "../../core" }
multiaddr = { path = "../multiaddr" }
net2 = "0.2"
rand = "0.5"
tokio-reactor = "0.1"
tokio-timer = "0.2"
tokio-udp = "0.1"

[dev-dependencies]
tokio = "0.1"
Loading