Skip to content

Commit

Permalink
fix(multiaddr): handle multiaddr mismatch with/without p2p proto
Browse files Browse the repository at this point in the history
  • Loading branch information
stormshield-frb committed Oct 26, 2023
1 parent fc6efaf commit 59a2d57
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions protocols/identify/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ impl PeerCache {
Some(cache) => cache,
};

let addresses = addresses.filter_map(|a| libp2p_swarm::p2p_addr(Some(peer), a).ok());
cache.put(peer, HashSet::from_iter(addresses));
}

Expand Down
1 change: 1 addition & 0 deletions protocols/kad/src/addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use smallvec::SmallVec;
use std::fmt;

/// A non-empty list of (unique) addresses of a peer in the routing table.
/// Every address must be a fully-qualified /p2p address.
#[derive(Clone)]
pub struct Addresses {
addrs: SmallVec<[Multiaddr; 6]>,
Expand Down
7 changes: 7 additions & 0 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,10 @@ where
/// If the routing table has been updated as a result of this operation,
/// a [`Event::RoutingUpdated`] event is emitted.
pub fn add_address(&mut self, peer: &PeerId, address: Multiaddr) -> RoutingUpdate {
// ensuring address is a fully-qualified /p2p multiaddr
let Ok(address) = libp2p_swarm::p2p_addr(Some(*peer), address) else {
return RoutingUpdate::Failed;
};
let key = kbucket::Key::from(*peer);
match self.kbuckets.entry(&key) {
kbucket::Entry::Present(mut entry, _) => {
Expand Down Expand Up @@ -593,6 +597,9 @@ where
peer: &PeerId,
address: &Multiaddr,
) -> Option<kbucket::EntryView<kbucket::Key<PeerId>, Addresses>> {
let Ok(address) = &libp2p_swarm::p2p_addr(Some(*peer), address.clone()) else {
return None;
};
let key = kbucket::Key::from(*peer);
match self.kbuckets.entry(&key) {
kbucket::Entry::Present(mut entry, _) => {
Expand Down
1 change: 1 addition & 0 deletions protocols/mdns/src/behaviour/iface/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ impl MdnsResponse {

peer.addresses().iter().filter_map(move |address| {
let new_addr = address_translation(address, &observed)?;
let new_addr = libp2p_swarm::p2p_addr(Some(*peer.id()), new_addr).ok()?;

Some((*peer.id(), new_addr, new_expiration))
})
Expand Down
2 changes: 1 addition & 1 deletion swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1918,7 +1918,7 @@ impl NetworkInfo {
///
/// If the given address is not yet a `p2p` address for the given peer,
/// the `/p2p/<peer-id>` protocol is appended to the returned address.
fn p2p_addr(peer: Option<PeerId>, addr: Multiaddr) -> Result<Multiaddr, Multiaddr> {
pub fn p2p_addr(peer: Option<PeerId>, addr: Multiaddr) -> Result<Multiaddr, Multiaddr> {
let peer = match peer {
Some(p) => p,
None => return Ok(addr),
Expand Down

0 comments on commit 59a2d57

Please sign in to comment.