Skip to content

Commit

Permalink
core/: Remove deprecated read/write functions (#2213)
Browse files Browse the repository at this point in the history
Co-authored-by: Max Inden <mail@max-inden.de>
  • Loading branch information
thomaseizinger and mxinden authored Sep 6, 2021
1 parent c161acf commit 6924e5e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 111 deletions.
4 changes: 4 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
- Report abortion of pending connection through `DialError`,
`UnknownPeerDialError` or `IncomingConnectionError` (see [PR 2191]).

- Remove deprecated functions `upgrade::write_one`, `upgrade::write_with_len_prefix`
and `upgrade::read_one` (see [PR 2213]).

[PR 2145]: https://github.com/libp2p/rust-libp2p/pull/2145
[PR 2213]: https://github.com/libp2p/rust-libp2p/pull/2213
[PR 2142]: https://github.com/libp2p/rust-libp2p/pull/2142
[PR 2137]: https://github.com/libp2p/rust-libp2p/pull/2137
[PR 2183]: https://github.com/libp2p/rust-libp2p/pull/2183
Expand Down
2 changes: 0 additions & 2 deletions core/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ mod transfer;

use futures::future::Future;

#[allow(deprecated)]
pub use self::transfer::ReadOneError;
pub use self::{
apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply},
denied::DeniedUpgrade,
Expand Down
110 changes: 1 addition & 109 deletions core/src/upgrade/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//! Contains some helper futures for creating upgrades.

use futures::prelude::*;
use std::{error, fmt, io};
use std::io;

// TODO: these methods could be on an Ext trait to AsyncWrite

Expand All @@ -40,42 +40,6 @@ pub async fn write_length_prefixed(
Ok(())
}

/// Send a message to the given socket, then shuts down the writing side.
///
/// > **Note**: Prepends a variable-length prefix indicate the length of the message. This is
/// > compatible with what `read_one` expects.
///
#[deprecated(
since = "0.29.0",
note = "Use `write_length_prefixed` instead. You will need to manually close the stream using `socket.close().await`."
)]
#[allow(dead_code)]
pub async fn write_one(
socket: &mut (impl AsyncWrite + Unpin),
data: impl AsRef<[u8]>,
) -> Result<(), io::Error> {
write_varint(socket, data.as_ref().len()).await?;
socket.write_all(data.as_ref()).await?;
socket.close().await?;
Ok(())
}

/// Send a message to the given socket with a length prefix appended to it. Also flushes the socket.
///
/// > **Note**: Prepends a variable-length prefix indicate the length of the message. This is
/// > compatible with what `read_one` expects.
#[deprecated(since = "0.29.0", note = "Use `write_length_prefixed` instead.")]
#[allow(dead_code)]
pub async fn write_with_len_prefix(
socket: &mut (impl AsyncWrite + Unpin),
data: impl AsRef<[u8]>,
) -> Result<(), io::Error> {
write_varint(socket, data.as_ref().len()).await?;
socket.write_all(data.as_ref()).await?;
socket.flush().await?;
Ok(())
}

/// Writes a variable-length integer to the `socket`.
///
/// > **Note**: Does **NOT** flush the socket.
Expand Down Expand Up @@ -162,78 +126,6 @@ pub async fn read_length_prefixed(
Ok(buf)
}

/// Reads a length-prefixed message from the given socket.
///
/// The `max_size` parameter is the maximum size in bytes of the message that we accept. This is
/// necessary in order to avoid DoS attacks where the remote sends us a message of several
/// gigabytes.
///
/// > **Note**: Assumes that a variable-length prefix indicates the length of the message. This is
/// > compatible with what `write_one` does.
#[deprecated(since = "0.29.0", note = "Use `read_length_prefixed` instead.")]
#[allow(dead_code, deprecated)]
pub async fn read_one(
socket: &mut (impl AsyncRead + Unpin),
max_size: usize,
) -> Result<Vec<u8>, ReadOneError> {
let len = read_varint(socket).await?;
if len > max_size {
return Err(ReadOneError::TooLarge {
requested: len,
max: max_size,
});
}

let mut buf = vec![0; len];
socket.read_exact(&mut buf).await?;
Ok(buf)
}

/// Error while reading one message.
#[derive(Debug)]
#[deprecated(
since = "0.29.0",
note = "Use `read_length_prefixed` instead of `read_one` to avoid depending on this type."
)]
pub enum ReadOneError {
/// Error on the socket.
Io(std::io::Error),
/// Requested data is over the maximum allowed size.
TooLarge {
/// Size requested by the remote.
requested: usize,
/// Maximum allowed.
max: usize,
},
}

#[allow(deprecated)]
impl From<std::io::Error> for ReadOneError {
fn from(err: std::io::Error) -> ReadOneError {
ReadOneError::Io(err)
}
}

#[allow(deprecated)]
impl fmt::Display for ReadOneError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
ReadOneError::Io(ref err) => write!(f, "{}", err),
ReadOneError::TooLarge { .. } => write!(f, "Received data size over maximum"),
}
}
}

#[allow(deprecated)]
impl error::Error for ReadOneError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
ReadOneError::Io(ref err) => Some(err),
ReadOneError::TooLarge { .. } => None,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 6924e5e

Please sign in to comment.