Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
11 changes: 10 additions & 1 deletion misc/mdns/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ impl<TSubstream> Mdns<TSubstream> {
marker: PhantomData,
})
}
/// Builds a new `Mdns` behaviour with legacy IPFS support
pub fn new_with_legacy() -> io::Result<Mdns<TSubstream>> {
Ok(Mdns {
service: MdnsService::new_with_legacy()?,
discovered_nodes: SmallVec::new(),
closest_expiration: None,
marker: PhantomData,
})
}

/// Returns true if the given `PeerId` is in the list of nodes discovered through mDNS.
pub fn has_node(&self, peer_id: &PeerId) -> bool {
Expand Down Expand Up @@ -240,7 +249,7 @@ where
if let Some(new_addr) = params.nat_traversal(&addr, &observed) {
addrs.push(new_addr);
}
addrs.push(addr);
addrs.push(addr.clone());
}

for addr in addrs {
Expand Down
24 changes: 17 additions & 7 deletions misc/mdns/src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//! Contains methods that handle the DNS encoding and decoding capabilities not available in the
//! `dns_parser` library.

use crate::{META_QUERY_SERVICE, SERVICE_NAME};
use crate::{META_QUERY_SERVICE, SERVICE_NAME, LEGACY_SERVICE_NAME_GO, LEGACY_SERVICE_NAME_JS};
use data_encoding;
use libp2p_core::{Multiaddr, PeerId};
use rand;
Expand Down Expand Up @@ -49,7 +49,19 @@ pub fn decode_character_string(mut from: &[u8]) -> Result<Cow<'_, [u8]>, ()> {

/// Builds the binary representation of a DNS query to send on the network.
pub fn build_query() -> Vec<u8> {
let mut out = Vec::with_capacity(33);
build_query_inner(SERVICE_NAME)
}

/// Builds the binary representations of legacy DNS queries to send on the network.
pub fn build_legacy_queries() -> (Vec<u8>, Vec<u8>) {
return (
build_query_inner(LEGACY_SERVICE_NAME_JS),
build_query_inner(LEGACY_SERVICE_NAME_GO)
)
}

fn build_query_inner(name: &[u8]) -> Vec<u8> {
let mut out = vec![];

// Program-generated transaction ID; unused by our implementation.
append_u16(&mut out, rand::random());
Expand All @@ -67,15 +79,12 @@ pub fn build_query() -> Vec<u8> {

// Our single question.
// The name.
append_qname(&mut out, SERVICE_NAME);
append_qname(&mut out, name);

// Flags.
append_u16(&mut out, 0x0c);
append_u16(&mut out, 0x01);

// Since the output is constant, we reserve the right amount ahead of time.
// If this assert fails, adjust the capacity of `out` in the source code.
debug_assert_eq!(out.capacity(), out.len());
out
}

Expand All @@ -86,7 +95,7 @@ pub fn build_query_response(
id: u16,
peer_id: PeerId,
addresses: impl ExactSizeIterator<Item = Multiaddr>,
ttl: Duration,
ttl: Duration
) -> Result<Vec<u8>, MdnsResponseError> {
// Convert the TTL into seconds.
let ttl = duration_to_secs(ttl);
Expand Down Expand Up @@ -147,6 +156,7 @@ pub fn build_query_response(
Ok(out)
}


/// Builds the response to the DNS query.
pub fn build_service_discovery_response(id: u16, ttl: Duration) -> Vec<u8> {
// Convert the TTL into seconds.
Expand Down
4 changes: 4 additions & 0 deletions misc/mdns/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@

/// Hardcoded name of the mDNS service. Part of the mDNS libp2p specifications.
const SERVICE_NAME: &[u8] = b"_p2p._udp.local";
/// Hardcoded name of the mDNS service as used by legacy IPFS services in GO
const LEGACY_SERVICE_NAME_GO: &[u8] = b"_ipfs-discovery._udp.local";
/// Hardcoded name of the mDNS service as used by legacy IPFS services in JS
const LEGACY_SERVICE_NAME_JS: &[u8] = b"ipfs.local";
/// Hardcoded name of the service used for DNS-SD.
const META_QUERY_SERVICE: &[u8] = b"_services._dns-sd._udp.local";

Expand Down
Loading