Skip to content

Commit 4ed539e

Browse files
committed
Merge branch 'master' into haiko-webrtc-outbound-multistream-nego-fix
2 parents c27930a + f1b4e0b commit 4ed539e

File tree

7 files changed

+160
-24
lines changed

7 files changed

+160
-24
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
9+
## [0.12.2] - 2025-11-28
10+
11+
This release allows all Bitswap CIDs (v1) to pass regardless of the used hash. It also enhances local address checks in the transport manager.
12+
13+
### Changed
14+
15+
- transport-service: Enhance logging with protocol names ([#485](https://github.com/paritytech/litep2p/pull/485))
16+
- bitswap: Reexports for CID ([#486](https://github.com/paritytech/litep2p/pull/486))
17+
- Allow all the Bitswap CIDs (v1) to pass regardless of used hash ([#482](https://github.com/paritytech/litep2p/pull/482))
18+
- transport/manager: Enhance local address checks ([#480](https://github.com/paritytech/litep2p/pull/480))
19+
820
## [0.12.1] - 2025-11-21
921

1022
This release adds support for connecting to multiple Kademlia DHT networks. The change is backward-compatible, no client code modifications should be needed compared to v0.12.0.

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "litep2p"
33
description = "Peer-to-peer networking library"
44
repository = "https://github.com/paritytech/litep2p"
55
license = "MIT"
6-
version = "0.12.1"
6+
version = "0.12.2"
77
edition = "2021"
88

99
# cargo-machete does not detect serde_millis usage, so we ignore the warning

src/protocol/libp2p/bitswap/mod.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,15 @@ use crate::{
2828
PeerId,
2929
};
3030

31-
use cid::Version;
32-
use multihash::Code;
31+
use cid::{Cid, Version};
3332
use prost::Message;
3433
use tokio::sync::mpsc::{Receiver, Sender};
3534
use tokio_stream::{StreamExt, StreamMap};
3635

37-
use std::{collections::HashMap, time::Duration};
38-
39-
pub use cid::Cid;
4036
pub use config::Config;
4137
pub use handle::{BitswapCommand, BitswapEvent, BitswapHandle, ResponseType};
4238
pub use schema::bitswap::{wantlist::WantType, BlockPresenceType};
39+
use std::{collections::HashMap, time::Duration};
4340

4441
mod config;
4542
mod handle;
@@ -167,10 +164,7 @@ impl Bitswap {
167164
_ => return None,
168165
};
169166

170-
(cid.version() == cid::Version::V1
171-
&& cid.hash().code() == u64::from(Code::Blake2b256)
172-
&& cid.hash().size() == 32)
173-
.then_some((cid, want_type))
167+
Some((cid, want_type))
174168
})
175169
.collect::<Vec<_>>();
176170

src/protocol/transport_service.rs

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,11 @@ impl TransportService {
339339
tracing::debug!(
340340
target: LOG_TARGET,
341341
?peer,
342-
protocol = %self.protocol,
343342
?endpoint,
344343
?connection_id,
345-
"connection established",
344+
protocol = %self.protocol,
345+
current_state = ?self.connections.get(&peer),
346+
"on connection established",
346347
);
347348

348349
match self.connections.get_mut(&peer) {
@@ -353,19 +354,38 @@ impl TransportService {
353354
?peer,
354355
?connection_id,
355356
?endpoint,
357+
protocol = %self.protocol,
356358
"ignoring third connection",
357359
);
358360
None
359361
}
360362
None => {
361363
self.keep_alive_tracker.on_connection_established(peer, connection_id);
362364

365+
tracing::trace!(
366+
target: LOG_TARGET,
367+
?peer,
368+
?endpoint,
369+
?connection_id,
370+
protocol = %self.protocol,
371+
"secondary connection established",
372+
);
373+
363374
context.secondary = Some(handle);
364375

365376
None
366377
}
367378
},
368379
None => {
380+
tracing::trace!(
381+
target: LOG_TARGET,
382+
?peer,
383+
?endpoint,
384+
?connection_id,
385+
protocol = %self.protocol,
386+
"primary connection established",
387+
);
388+
369389
self.connections.insert(peer, ConnectionContext::new(handle));
370390

371391
self.keep_alive_tracker.on_connection_established(peer, connection_id);
@@ -381,13 +401,23 @@ impl TransportService {
381401
peer: PeerId,
382402
connection_id: ConnectionId,
383403
) -> Option<TransportEvent> {
404+
tracing::debug!(
405+
target: LOG_TARGET,
406+
?peer,
407+
?connection_id,
408+
protocol = %self.protocol,
409+
current_state = ?self.connections.get(&peer),
410+
"on connection closed",
411+
);
412+
384413
self.keep_alive_tracker.on_connection_closed(peer, connection_id);
385414

386415
let Some(context) = self.connections.get_mut(&peer) else {
387416
tracing::warn!(
388417
target: LOG_TARGET,
389418
?peer,
390419
?connection_id,
420+
protocol = %self.protocol,
391421
"connection closed to a non-existent peer",
392422
);
393423

@@ -398,7 +428,13 @@ impl TransportService {
398428
// if the primary connection was closed, check if there exist a secondary connection
399429
// and if it does, convert the secondary connection a primary connection
400430
if context.primary.connection_id() == &connection_id {
401-
tracing::trace!(target: LOG_TARGET, ?peer, ?connection_id, "primary connection closed");
431+
tracing::trace!(
432+
target: LOG_TARGET,
433+
?peer,
434+
?connection_id,
435+
protocol = %self.protocol,
436+
"primary connection closed"
437+
);
402438

403439
match context.secondary.take() {
404440
None => {
@@ -410,6 +446,7 @@ impl TransportService {
410446
target: LOG_TARGET,
411447
?peer,
412448
?connection_id,
449+
protocol = %self.protocol,
413450
"switch to secondary connection",
414451
);
415452

@@ -425,6 +462,7 @@ impl TransportService {
425462
target: LOG_TARGET,
426463
?peer,
427464
?connection_id,
465+
protocol = %self.protocol,
428466
"secondary connection closed",
429467
);
430468

@@ -436,6 +474,7 @@ impl TransportService {
436474
?peer,
437475
?connection_id,
438476
?connection_state,
477+
protocol = %self.protocol,
439478
"connection closed but it doesn't exist",
440479
);
441480

@@ -448,6 +487,13 @@ impl TransportService {
448487
///
449488
/// Call fails if `Litep2p` doesn't have a known address for the peer.
450489
pub fn dial(&mut self, peer: &PeerId) -> Result<(), ImmediateDialError> {
490+
tracing::trace!(
491+
target: LOG_TARGET,
492+
?peer,
493+
protocol = %self.protocol,
494+
"Dial peer requested",
495+
);
496+
451497
self.transport_handle.dial(peer)
452498
}
453499

@@ -460,6 +506,13 @@ impl TransportService {
460506
/// since `Litep2p` internally keeps track of all peer addresses it has learned through user
461507
/// calling this function, Kademlia peer discoveries and `Identify` responses.
462508
pub fn dial_address(&mut self, address: Multiaddr) -> Result<(), ImmediateDialError> {
509+
tracing::trace!(
510+
target: LOG_TARGET,
511+
?address,
512+
protocol = %self.protocol,
513+
"Dial address requested",
514+
);
515+
463516
self.transport_handle.dial_address(address)
464517
}
465518

src/transport/manager/handle.rs

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use tokio::sync::mpsc::{error::TrySendError, Sender};
4040

4141
use std::{
4242
collections::{HashMap, HashSet},
43+
net::IpAddr,
4344
sync::{
4445
atomic::{AtomicUsize, Ordering},
4546
Arc,
@@ -164,14 +165,66 @@ impl TransportManagerHandle {
164165
}
165166
}
166167

168+
/// Helper to extract IP and Port from a Multiaddr
169+
fn extract_ip_port(addr: &Multiaddr) -> Option<(IpAddr, u16)> {
170+
let mut iter = addr.iter();
171+
let ip = match iter.next() {
172+
Some(Protocol::Ip4(i)) => IpAddr::V4(i),
173+
Some(Protocol::Ip6(i)) => IpAddr::V6(i),
174+
_ => return None,
175+
};
176+
177+
let port = match iter.next() {
178+
Some(Protocol::Tcp(p)) | Some(Protocol::Udp(p)) => p,
179+
_ => return None,
180+
};
181+
182+
Some((ip, port))
183+
}
184+
167185
/// Check if the address is a local listen address and if so, discard it.
168186
fn is_local_address(&self, address: &Multiaddr) -> bool {
187+
// Strip the peer ID if present.
169188
let address: Multiaddr = address
170189
.iter()
171190
.take_while(|protocol| !std::matches!(protocol, Protocol::P2p(_)))
172191
.collect();
173192

174-
self.listen_addresses.read().contains(&address)
193+
// Check for the exact match.
194+
let listen_addresses = self.listen_addresses.read();
195+
if listen_addresses.contains(&address) {
196+
return true;
197+
}
198+
199+
let Some((ip, port)) = Self::extract_ip_port(&address) else {
200+
return false;
201+
};
202+
203+
for listen_address in listen_addresses.iter() {
204+
let Some((listen_ip, listen_port)) = Self::extract_ip_port(listen_address) else {
205+
continue;
206+
};
207+
208+
if port == listen_port {
209+
// Exact IP match.
210+
if listen_ip == ip {
211+
return true;
212+
}
213+
214+
// Check if the listener is binding to any (0.0.0.0) interface
215+
// and the incoming is a loopback address.
216+
if listen_ip.is_unspecified() && ip.is_loopback() {
217+
return true;
218+
}
219+
220+
// Check for ipv4/ipv6 loopback equivalence.
221+
if listen_ip.is_loopback() && ip.is_loopback() {
222+
return true;
223+
}
224+
}
225+
}
226+
227+
false
175228
}
176229

177230
/// Add one or more known addresses for peer.
@@ -745,11 +798,12 @@ mod tests {
745798
let (cmd_tx, _cmd_rx) = channel(64);
746799

747800
let local_peer_id = PeerId::random();
748-
let first_addr: Multiaddr = "/ip6/::1/tcp/8888".parse().expect("valid multiaddress");
749-
let second_addr: Multiaddr = "/ip4/127.0.0.1/tcp/8888".parse().expect("valid multiaddress");
801+
let specific_bind: Multiaddr = "/ip6/::1/tcp/8888".parse().expect("valid multiaddress");
802+
let ipv6_bind: Multiaddr = "/ip4/127.0.0.1/tcp/8888".parse().expect("valid multiaddress");
803+
let wildcard_bind: Multiaddr = "/ip4/0.0.0.0/tcp/9000".parse().unwrap();
750804

751805
let listen_addresses = Arc::new(RwLock::new(
752-
[first_addr.clone(), second_addr.clone()].iter().cloned().collect(),
806+
[specific_bind, wildcard_bind, ipv6_bind].into_iter().collect(),
753807
));
754808
println!("{:?}", listen_addresses);
755809

@@ -762,12 +816,14 @@ mod tests {
762816
public_addresses: PublicAddresses::new(local_peer_id),
763817
};
764818

765-
// local addresses
819+
// Exact matches
820+
assert!(handle
821+
.is_local_address(&"/ip4/127.0.0.1/tcp/8888".parse().expect("valid multiaddress")));
766822
assert!(handle.is_local_address(
767823
&"/ip6/::1/tcp/8888".parse::<Multiaddr>().expect("valid multiaddress")
768824
));
769-
assert!(handle
770-
.is_local_address(&"/ip4/127.0.0.1/tcp/8888".parse().expect("valid multiaddress")));
825+
826+
// Peer ID stripping
771827
assert!(handle.is_local_address(
772828
&"/ip6/::1/tcp/8888/p2p/12D3KooWT2ouvz5uMmCvHJGzAGRHiqDts5hzXR7NdoQ27pGdzp9Q"
773829
.parse()
@@ -778,7 +834,6 @@ mod tests {
778834
.parse()
779835
.expect("valid multiaddress")
780836
));
781-
782837
// same address but different peer id
783838
assert!(handle.is_local_address(
784839
&"/ip6/::1/tcp/8888/p2p/12D3KooWPGxxxQiBEBZ52RY31Z2chn4xsDrGCMouZ88izJrak2T1"
@@ -791,10 +846,29 @@ mod tests {
791846
.expect("valid multiaddress")
792847
));
793848

794-
// different address
849+
// Port collision protection: we listen on 0.0.0.0:9000 and should match any loopback
850+
// address on port 9000.
851+
assert!(
852+
handle.is_local_address(&"/ip4/127.0.0.1/tcp/9000".parse().unwrap()),
853+
"Loopback input should satisfy Wildcard (0.0.0.0) listener"
854+
);
855+
// 8.8.8.8 is a different IP.
856+
assert!(
857+
!handle.is_local_address(&"/ip4/8.8.8.8/tcp/9000".parse().unwrap()),
858+
"Remote IP with same port should NOT be considered local against Wildcard listener"
859+
);
860+
861+
// Port mismatches
862+
assert!(
863+
!handle.is_local_address(&"/ip4/127.0.0.1/tcp/1234".parse().unwrap()),
864+
"Same IP but different port should fail"
865+
);
866+
assert!(
867+
!handle.is_local_address(&"/ip4/0.0.0.0/tcp/1234".parse().unwrap()),
868+
"Wildcard IP but different port should fail"
869+
);
795870
assert!(!handle
796871
.is_local_address(&"/ip4/127.0.0.1/tcp/9999".parse().expect("valid multiaddress")));
797-
// different address
798872
assert!(!handle
799873
.is_local_address(&"/ip4/127.0.0.1/tcp/7777".parse().expect("valid multiaddress")));
800874
}

src/types.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ pub mod multiaddr {
2929
pub mod multihash {
3030
pub use multihash::{Code, Error, Multihash, MultihashDigest};
3131
}
32+
pub mod cid {
33+
pub use cid::{multihash::Multihash, Cid, CidGeneric, Error, Result, Version};
34+
}
3235

3336
pub mod protocol;
3437

0 commit comments

Comments
 (0)