Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
317 changes: 237 additions & 80 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ members = [
"swarm-test",
"swarm",
"transports/dns",
"transports/bluetooth",
"transports/noise",
"transports/plaintext",
"transports/pnet",
Expand Down Expand Up @@ -81,6 +82,7 @@ libp2p-connection-limits = { version = "0.6.0", path = "misc/connection-limits"
libp2p-core = { version = "0.43.1", path = "core" }
libp2p-dcutr = { version = "0.14.0", path = "protocols/dcutr" }
libp2p-dns = { version = "0.44.0", path = "transports/dns" }
libp2p-bluetooth = { version = "0.1.0", path = "transports/bluetooth" }
libp2p-floodsub = { version = "0.47.0", path = "protocols/floodsub" }
libp2p-gossipsub = { version = "0.50.0", path = "protocols/gossipsub" }
libp2p-identify = { version = "0.47.0", path = "protocols/identify" }
Expand Down Expand Up @@ -127,7 +129,7 @@ getrandom = "0.2"
if-watch = "3.2.1"
hickory-proto = { version = "0.25.2", default-features = false }
hickory-resolver = { version = "0.25.2", default-features = false }
multiaddr = "0.18.1"
multiaddr = "0.18.2"
multihash = "0.19.1"
multistream-select = { version = "0.13.0", path = "misc/multistream-select" }
prometheus-client = "0.24"
Expand Down
3 changes: 3 additions & 0 deletions libp2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ categories = ["network-programming", "asynchronous"]
[features]
full = [
"autonat",
"bluetooth",
"cbor",
"dcutr",
"dns",
Expand Down Expand Up @@ -52,6 +53,7 @@ full = [
]

autonat = ["dep:libp2p-autonat"]
bluetooth = ["dep:libp2p-bluetooth"]
cbor = ["libp2p-request-response?/cbor"]
dcutr = ["dep:libp2p-dcutr", "libp2p-metrics?/dcutr"]
dns = ["dep:libp2p-dns"]
Expand Down Expand Up @@ -128,6 +130,7 @@ libp2p-webtransport-websys = { workspace = true, optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
libp2p-dns = { workspace = true, optional = true }
libp2p-bluetooth = { workspace = true, optional = true }
libp2p-mdns = { workspace = true, optional = true }
libp2p-memory-connection-limits = { workspace = true, optional = true }
libp2p-quic = { workspace = true, optional = true }
Expand Down
17 changes: 17 additions & 0 deletions libp2p/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ mod tests {
.build();
}

#[test]
#[cfg(all(
feature = "tokio",
feature = "bluetooth",
feature = "noise",
feature = "yamux",
))]
fn bluetooth() {
let _ = SwarmBuilder::with_new_identity()
.with_tokio()
.with_bluetooth(libp2p_noise::Config::new, libp2p_yamux::Config::default)
.unwrap()
.with_behaviour(|_| libp2p_swarm::dummy::Behaviour)
.unwrap()
.build();
}

#[test]
#[cfg(all(feature = "tokio", feature = "quic"))]
fn quic() {
Expand Down
2 changes: 2 additions & 0 deletions libp2p/src/builder/phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod bandwidth_metrics;
mod behaviour;
mod bluetooth;
mod build;
mod dns;
mod identity;
Expand All @@ -16,6 +17,7 @@ mod websocket;
use bandwidth_metrics::*;
pub use behaviour::BehaviourError;
use behaviour::*;
use bluetooth::*;
use build::*;
use dns::*;
use libp2p_core::{muxing::StreamMuxerBox, Transport};
Expand Down
86 changes: 86 additions & 0 deletions libp2p/src/builder/phase/bluetooth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::marker::PhantomData;

use super::*;
use crate::SwarmBuilder;

use libp2p_core::upgrade::{InboundConnectionUpgrade, OutboundConnectionUpgrade};
use libp2p_core::{Negotiated, UpgradeInfo};

pub struct BluetoothPhase<T> {
pub(crate) transport: T,
}

impl<Provider, T> SwarmBuilder<Provider, BluetoothPhase<T>> {
pub(crate) fn without_bluetooth(self) -> SwarmBuilder<Provider, OtherTransportPhase<T>> {
SwarmBuilder {
keypair: self.keypair,
phantom: PhantomData,
phase: OtherTransportPhase {
transport: self.phase.transport,
},
}
}
}

impl<Provider, T: AuthenticatedMultiplexedTransport> SwarmBuilder<Provider, BluetoothPhase<T>> {
pub fn with_other_transport<
Muxer: libp2p_core::muxing::StreamMuxer + Send + 'static,
OtherTransport: Transport<Output = (libp2p_identity::PeerId, Muxer)> + Send + Unpin + 'static,
R: TryIntoTransport<OtherTransport>,
>(
self,
constructor: impl FnOnce(&libp2p_identity::Keypair) -> R,
) -> Result<
SwarmBuilder<Provider, OtherTransportPhase<impl AuthenticatedMultiplexedTransport>>,
R::Error,
>
where
<OtherTransport as Transport>::Error: Send + Sync + 'static,
<OtherTransport as Transport>::Dial: Send,
<OtherTransport as Transport>::ListenerUpgrade: Send,
<Muxer as libp2p_core::muxing::StreamMuxer>::Substream: Send,
<Muxer as libp2p_core::muxing::StreamMuxer>::Error: Send + Sync,
{
self.without_bluetooth().with_other_transport(constructor)
}

pub fn with_behaviour<B, R: TryIntoBehaviour<B>>(
self,
constructor: impl FnOnce(&libp2p_identity::Keypair) -> R,
) -> Result<SwarmBuilder<Provider, SwarmPhase<T, B>>, R::Error> {
self.without_bluetooth()
.without_any_other_transports()
.without_dns()
.without_websocket()
.without_relay()
.with_behaviour(constructor)
}
}

#[cfg(all(not(target_arch = "wasm32"), feature = "tokio", feature = "dns"))]
impl<T: AuthenticatedMultiplexedTransport> SwarmBuilder<super::provider::Tokio, BluetoothPhase<T>> {
pub fn with_dns(
self,
) -> Result<
SwarmBuilder<
super::provider::Tokio,
WebsocketPhase<impl AuthenticatedMultiplexedTransport>,
>,
std::io::Error,
> {
self.without_bluetooth()
.without_any_other_transports()
.with_dns()
}

pub fn with_dns_config(
self,
cfg: libp2p_dns::ResolverConfig,
opts: libp2p_dns::ResolverOpts,
) -> SwarmBuilder<super::provider::Tokio, WebsocketPhase<impl AuthenticatedMultiplexedTransport>>
{
self.without_bluetooth()
.without_any_other_transports()
.with_dns_config(cfg, opts)
}
}
69 changes: 67 additions & 2 deletions libp2p/src/builder/phase/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use std::marker::PhantomData;

#[cfg(all(
not(target_arch = "wasm32"),
any(feature = "tcp", feature = "websocket")
any(feature = "tcp", feature = "websocket", feature = "bluetooth")
))]
use libp2p_core::muxing::{StreamMuxer, StreamMuxerBox};
#[cfg(all(feature = "websocket", not(target_arch = "wasm32")))]
use libp2p_core::Transport;
#[cfg(all(
not(target_arch = "wasm32"),
any(feature = "tcp", feature = "websocket")
any(feature = "tcp", feature = "websocket", feature = "bluetooth")
))]
use libp2p_core::{
upgrade::InboundConnectionUpgrade, upgrade::OutboundConnectionUpgrade, Negotiated, UpgradeInfo,
Expand Down Expand Up @@ -111,6 +111,71 @@ impl<Provider> SwarmBuilder<Provider, TcpPhase> {
},
}
}

#[cfg(all(not(target_arch = "wasm32"), feature = "bluetooth"))]
#[allow(clippy::too_many_arguments)]
pub fn with_bluetooth<SecUpgrade, SecStream, SecError, MuxUpgrade, MuxStream, MuxError>(
self,
security_upgrade: SecUpgrade,
multiplexer_upgrade: MuxUpgrade,
) -> Result<
SwarmBuilder<Provider, BluetoothPhase<impl AuthenticatedMultiplexedTransport>>,
SecUpgrade::Error,
>
where
SecStream: futures::AsyncRead + futures::AsyncWrite + Unpin + Send + 'static,
SecError: std::error::Error + Send + Sync + 'static,
SecUpgrade: IntoSecurityUpgrade<libp2p_bluetooth::Channel<Vec<u8>>>,
SecUpgrade::Upgrade: InboundConnectionUpgrade<
Negotiated<libp2p_bluetooth::Channel<Vec<u8>>>,
Output = (libp2p_identity::PeerId, SecStream),
Error = SecError,
> + OutboundConnectionUpgrade<
Negotiated<libp2p_bluetooth::Channel<Vec<u8>>>,
Output = (libp2p_identity::PeerId, SecStream),
Error = SecError,
> + Clone
+ Send
+ 'static,
<SecUpgrade::Upgrade as InboundConnectionUpgrade<
Negotiated<libp2p_bluetooth::Channel<Vec<u8>>>>>::Future: Send,
<SecUpgrade::Upgrade as OutboundConnectionUpgrade<
Negotiated<libp2p_bluetooth::Channel<Vec<u8>>>>>::Future: Send,
<<<SecUpgrade as IntoSecurityUpgrade<libp2p_bluetooth::Channel<Vec<u8>>>>::Upgrade as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send,
<<SecUpgrade as IntoSecurityUpgrade<libp2p_bluetooth::Channel<Vec<u8>>>>::Upgrade as UpgradeInfo>::Info: Send,
MuxStream: StreamMuxer + Send + 'static,
MuxStream::Substream: Send + 'static,
MuxStream::Error: Send + Sync + 'static,
MuxUpgrade: IntoMultiplexerUpgrade<SecStream>,
MuxUpgrade::Upgrade: InboundConnectionUpgrade<
Negotiated<SecStream>,
Output = MuxStream,
Error = MuxError,
> + OutboundConnectionUpgrade<
Negotiated<SecStream>,
Output = MuxStream,
Error = MuxError,
> + Clone
+ Send
+ 'static,
<MuxUpgrade::Upgrade as InboundConnectionUpgrade<Negotiated<SecStream>>>::Future: Send,
<MuxUpgrade::Upgrade as OutboundConnectionUpgrade<Negotiated<SecStream>>>::Future: Send,
MuxError: std::error::Error + Send + Sync + 'static,
<<<MuxUpgrade as IntoMultiplexerUpgrade<SecStream>>::Upgrade as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send,
<<MuxUpgrade as IntoMultiplexerUpgrade<SecStream>>::Upgrade as UpgradeInfo>::Info: Send,
{
Ok(SwarmBuilder {
phase: BluetoothPhase {
transport: libp2p_bluetooth::BluetoothTransport::new()
.upgrade(libp2p_core::upgrade::Version::V1Lazy)
.authenticate(security_upgrade.into_security_upgrade(&self.keypair)?)
.multiplex(multiplexer_upgrade.into_multiplexer_upgrade())
.map(|(peer, muxer), _| (peer, StreamMuxerBox::new(muxer))),
},
keypair: self.keypair,
phantom: PhantomData,
})
}
}

#[cfg(all(not(target_arch = "wasm32"), feature = "quic", feature = "tokio"))]
Expand Down
3 changes: 3 additions & 0 deletions libp2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ pub use libp2p_allow_block_list as allow_block_list;
#[cfg(feature = "autonat")]
#[doc(inline)]
pub use libp2p_autonat as autonat;
#[cfg(all(feature = "bluetooth", not(target_arch = "wasm32")))]
#[doc(inline)]
pub use libp2p_bluetooth as bluetooth;
#[doc(inline)]
pub use libp2p_connection_limits as connection_limits;
#[doc(inline)]
Expand Down
39 changes: 39 additions & 0 deletions transports/bluetooth/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "libp2p-bluetooth"
edition.workspace = true
rust-version = { workspace = true }
description = "Bluetooth transport protocol for libp2p"
version = "0.1.0"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
keywords = ["peer-to-peer", "libp2p", "networking"]
categories = ["network-programming", "asynchronous"]

[features]
default = []

[dependencies]
fnv = "1.0"
futures = { workspace = true }
libp2p-core = { workspace = true }
multiaddr = { workspace = true }
parking_lot = "0.12.3"
rand = "0.8"
rw-stream-sink = { workspace = true }
log = "0.4"
uuid = "1.0"
simplersble = "0.10"
tokio = { version = "1", features = ["sync", "rt", "time"] }
tokio-stream = "0.1"
bytes = "1.0"
async-trait = "0.1"

[target.'cfg(target_os = "macos")'.dependencies]
objc2 = "0.5"
objc2-core-bluetooth = { version = "0.2", features = ["all"] }
objc2-foundation = { version = "0.2", features = ["all"] }
block2 = "0.5"

[lints]
workspace = true
Loading
Loading