Skip to content

Commit

Permalink
WIP on (no branch): 83c6795 *: Prepare v0.48.0 (libp2p#2869)
Browse files Browse the repository at this point in the history
  • Loading branch information
binier authored and dkuehr committed Oct 22, 2023
1 parent ed02a10 commit 417fcfd
Show file tree
Hide file tree
Showing 8 changed files with 404 additions and 3 deletions.
4 changes: 2 additions & 2 deletions core/CHANGELOG.md
Expand Up @@ -419,9 +419,9 @@

## 0.24.0 [2020-11-09]

- Remove `ConnectionInfo` trait and replace it with `PeerId`
- Remove `Connection` trait and replace it with `PeerId`
everywhere. This was already effectively the case because
`ConnectionInfo` was implemented on `PeerId`.
`Connection` was implemented on `PeerId`.

## 0.23.1 [2020-10-20]

Expand Down
14 changes: 14 additions & 0 deletions core/src/connection.rs
Expand Up @@ -20,6 +20,20 @@

use crate::multiaddr::{Multiaddr, Protocol};

pub trait Connection {
/// Should be `Some(_)` if transport itself handles authentication.
fn remote_peer_id(&self) -> Option<crate::PeerId> {
None
}
}

impl<C> Connection for multistream_select::Negotiated<C>
where C: Connection {
fn remote_peer_id(&self) -> Option<crate::PeerId> {
self.inner_completed_io().and_then(|io| io.remote_peer_id())
}
}

/// The endpoint roles associated with a peer-to-peer communication channel.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Endpoint {
Expand Down
9 changes: 9 additions & 0 deletions misc/multistream-select/src/negotiated.rs
Expand Up @@ -51,6 +51,15 @@ pub struct Negotiated<TInner> {
state: State<TInner>,
}

impl<TInner> Negotiated<TInner> {
pub fn inner_completed_io(&self) -> Option<&TInner> {
match &self.state {
State::Completed { io } => Some(io),
_ => None
}
}
}

/// A `Future` that waits on the completion of protocol negotiation.
#[derive(Debug)]
pub struct NegotiatedComplete<TInner> {
Expand Down
2 changes: 1 addition & 1 deletion transports/pnet/Cargo.toml
Expand Up @@ -29,7 +29,7 @@ libp2p-yamux = { workspace = true }
quickcheck = { workspace = true }
tokio = { version = "1.33.0", features = ["full"] }

# Passing arguments to the docsrs builder in order to properly document cfg's.
# Passing arguments to the docsrs builder in order to properly document cfg's.
# More information: https://docs.rs/about/builds#cross-compiling
[package.metadata.docs.rs]
all-features = true
Expand Down
9 changes: 9 additions & 0 deletions transports/pnet/src/crypt_writer.rs
Expand Up @@ -28,6 +28,8 @@ use pin_project::pin_project;
use salsa20::{cipher::StreamCipher, XSalsa20};
use std::{fmt, pin::Pin};

use libp2p_core::Connection;

/// A writer that encrypts and forwards to an inner writer
#[pin_project]
pub(crate) struct CryptWriter<W> {
Expand All @@ -37,6 +39,13 @@ pub(crate) struct CryptWriter<W> {
cipher: XSalsa20,
}

impl<C> Connection for CryptWriter<C>
where C: Connection {
fn remote_peer_id(&self) -> Option<libp2p_core::PeerId> {
self.inner.remote_peer_id()
}
}

impl<W: AsyncWrite> CryptWriter<W> {
/// Creates a new `CryptWriter` with the specified buffer capacity.
pub(crate) fn with_capacity(capacity: usize, inner: W, cipher: XSalsa20) -> CryptWriter<W> {
Expand Down
9 changes: 9 additions & 0 deletions transports/pnet/src/lib.rs
Expand Up @@ -48,6 +48,8 @@ use std::{
task::{Context, Poll},
};

use libp2p_core::Connection;

const KEY_SIZE: usize = 32;
const NONCE_SIZE: usize = 24;
const WRITE_BUFFER_SIZE: usize = 1024;
Expand Down Expand Up @@ -239,6 +241,13 @@ pub struct PnetOutput<S> {
read_cipher: XSalsa20,
}

impl<C> Connection for PnetOutput<C>
where C: Connection {
fn remote_peer_id(&self) -> Option<libp2p_core::PeerId> {
self.inner.remote_peer_id()
}
}

impl<S: AsyncRead + AsyncWrite> PnetOutput<S> {
fn new(inner: S, write_cipher: XSalsa20, read_cipher: XSalsa20) -> Self {
Self {
Expand Down
68 changes: 68 additions & 0 deletions transports/wasm-ext/src/dtls.rs
@@ -0,0 +1,68 @@
use libp2p_core::{Connection, InboundUpgrade, OutboundUpgrade, PeerId, UpgradeInfo};

#[derive(Debug, Clone)]
pub struct NonDTLSConnectionError;

impl std::fmt::Display for NonDTLSConnectionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Tried to authenticate using `DTLSAuthenticated` for non DTLS transport connection!")
}
}

impl std::error::Error for NonDTLSConnectionError {
}

#[derive(Clone)]
pub struct DTLSAuthenticated;


impl DTLSAuthenticated {
pub fn new() -> Self {
Self {}
}
}

impl UpgradeInfo for DTLSAuthenticated {
type Info = &'static str;
// type InfoIter = std::iter::Once<Self::Info>;
type InfoIter = std::iter::Empty<Self::Info>;

fn protocol_info(&self) -> Self::InfoIter {
std::iter::empty()
// std::iter::once("/dtls")
}
}

impl<C> InboundUpgrade<C> for DTLSAuthenticated
where
C: Connection,
{
type Output = (PeerId, C);
type Error = NonDTLSConnectionError;
type Future = std::future::Ready<Result<Self::Output, Self::Error>>;

fn upgrade_inbound(self, socket: C, _: Self::Info) -> Self::Future {
let peer_id_result = socket
.remote_peer_id()
.map(|id| (id, socket))
.ok_or(NonDTLSConnectionError);
std::future::ready(peer_id_result)
}
}

impl<C> OutboundUpgrade<C> for DTLSAuthenticated
where
C: Connection,
{
type Output = (PeerId, C);
type Error = NonDTLSConnectionError;
type Future = std::future::Ready<Result<Self::Output, Self::Error>>;

fn upgrade_outbound(self, socket: C, _: Self::Info) -> Self::Future {
let peer_id_result = socket
.remote_peer_id()
.map(|id| (id, socket))
.ok_or(NonDTLSConnectionError);
std::future::ready(peer_id_result)
}
}

0 comments on commit 417fcfd

Please sign in to comment.