Skip to content

Commit

Permalink
Update bt_bencode 0.8; use &[u8] instead of Bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
bluk committed Jan 1, 2024
1 parent c0c7c2a commit 89fc4fc
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 94 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ alloc = ["bt_bencode/alloc", "gen_value/alloc", "serde/alloc", "serde_bytes/allo

[dependencies]
bitvec = { version = "1.0", default-features = false }
bt_bencode = { version = "0.7", default-features = false }
bt_bencode = { version = "0.8", default-features = false }
bytes = { version = "1.1", default-features = false }
crc32c = "0.6"
gen_value = { version = "0.7", default-features = false }
Expand Down
35 changes: 6 additions & 29 deletions src/dht/krpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use serde::{
de::{self, Visitor},
Deserialize, Serialize,
};
use serde_bytes::{ByteBuf, Bytes};

#[cfg(feature = "std")]
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
Expand Down Expand Up @@ -99,28 +98,6 @@ impl<'a> From<&'a [u8]> for Ty {
}
}

impl<'a> From<&'a Bytes> for Ty {
fn from(y: &'a Bytes) -> Self {
match y.as_ref() {
b"q" => Ty::Query,
b"r" => Ty::Response,
b"e" => Ty::Error,
_ => Ty::Unknown,
}
}
}

impl<'a> From<&'a ByteBuf> for Ty {
fn from(y: &'a ByteBuf) -> Self {
match y.as_slice() {
b"q" => Ty::Query,
b"r" => Ty::Response,
b"e" => Ty::Error,
_ => Ty::Unknown,
}
}
}

/// A KRPC message.
#[derive(Debug, serde_derive::Deserialize)]
pub struct Msg<'a> {
Expand Down Expand Up @@ -256,33 +233,33 @@ impl<'a> Msg<'a> {
#[derive(Debug, serde_derive::Deserialize)]
pub struct QueryArgs<'a> {
/// The querying node's ID
#[serde(borrow)]
pub id: &'a Bytes,
#[serde(with = "serde_bytes")]
pub id: &'a [u8],
}

impl<'a> QueryArgs<'a> {
/// Returns the querying node's ID.
#[must_use]
#[inline]
pub fn id(&self) -> Option<Id> {
Id::try_from(self.id.as_ref()).ok()
Id::try_from(self.id).ok()
}
}

/// Generic response value.
#[derive(Debug, serde_derive::Deserialize)]
pub struct RespValues<'a> {
/// The queried node's ID
#[serde(borrow)]
pub id: &'a Bytes,
#[serde(with = "serde_bytes")]
pub id: &'a [u8],
}

impl<'a> RespValues<'a> {
/// Returns the querying node's ID.
#[must_use]
#[inline]
pub fn id(&self) -> Option<Id> {
Id::try_from(self.id.as_ref()).ok()
Id::try_from(self.id).ok()
}
}

Expand Down
53 changes: 26 additions & 27 deletions src/dht/krpc/get_peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ pub const METHOD_GET_PEERS: &[u8] = b"get_peers";
#[derive(Debug, Deserialize, Serialize)]
pub struct QueryArgs<'a> {
/// The querying node's ID
#[serde(borrow)]
pub id: &'a Bytes,
#[serde(with = "serde_bytes")]
pub id: &'a [u8],
/// The `InfoHash` associated with the torrent
#[serde(borrow)]
pub info_hash: &'a Bytes,
#[serde(with = "serde_bytes")]
pub info_hash: &'a [u8],
}

impl<'a> QueryArgs<'a> {
Expand All @@ -52,41 +52,41 @@ impl<'a> QueryArgs<'a> {
#[inline]
pub fn new(id: &'a LocalId, info_hash: &'a InfoHash) -> Self {
Self {
id: Bytes::new(&(id.0).0),
info_hash: Bytes::new(&info_hash.0),
id: &(id.0).0,
info_hash: &info_hash.0,
}
}

/// Returns the querying node's ID.
#[must_use]
#[inline]
pub fn id(&self) -> Option<Id> {
Id::try_from(self.id.as_ref()).ok()
Id::try_from(self.id).ok()
}

/// Returns the `InfoHash` for the relevant torrent.
#[must_use]
#[inline]
pub fn info_hash(&self) -> Option<InfoHash> {
InfoHash::try_from(self.info_hash.as_ref()).ok()
InfoHash::try_from(self.info_hash).ok()
}
}

/// The value for the get peers response.
#[derive(Debug, Serialize, Deserialize)]
pub struct RespValues<'a, V> {
/// The queried node's ID
#[serde(borrow)]
pub id: &'a Bytes,
#[serde(with = "serde_bytes")]
pub id: &'a [u8],
/// IPv4 nodes which may have relevant information
#[serde(skip_serializing_if = "Option::is_none", borrow)]
pub nodes: Option<&'a Bytes>,
/// IPv6 nodes which may have relevant information
#[serde(skip_serializing_if = "Option::is_none", borrow)]
pub nodes6: Option<&'a Bytes>,
/// An opaque token which can be used in an announce peer message.
#[serde(borrow)]
pub token: &'a Bytes,
#[serde(with = "serde_bytes")]
pub token: &'a [u8],
/// Peer compact addresses
#[serde(skip_serializing_if = "Option::is_none")]
pub values: Option<V>,
Expand All @@ -98,13 +98,13 @@ impl<'a, V> RespValues<'a, V> {
#[inline]
pub fn new(
id: &'a LocalId,
token: &'a Bytes,
token: &'a [u8],
values: Option<V>,
nodes: Option<&'a Bytes>,
nodes6: Option<&'a Bytes>,
) -> Self {
Self {
id: Bytes::new(&(id.0).0),
id: &(id.0).0,
token,
values,
nodes,
Expand All @@ -116,7 +116,7 @@ impl<'a, V> RespValues<'a, V> {
#[must_use]
#[inline]
pub fn id(&self) -> Option<Id> {
Id::try_from(self.id.as_ref()).ok()
Id::try_from(self.id).ok()
}

/// Returns the token which is used by the queried node for verification.
Expand Down Expand Up @@ -169,18 +169,18 @@ impl<'a, V> RespValues<'a, V> {
}
}

impl<'a> RespValues<'a, Vec<&'a Bytes>> {
impl<'a> RespValues<'a, Vec<&'a [u8]>> {
/// Returns peers' socket addresses for the torrent.
#[must_use]
#[inline]
pub fn values(&'a self) -> Option<impl Iterator<Item = CompactAddr> + 'a> {
self.values.as_ref().map(|values| {
values.iter().filter_map(|&v| match v.len() {
6 => <[u8; 6]>::try_from(v.as_ref())
6 => <[u8; 6]>::try_from(v)
.ok()
.map(CompactAddrV4::from)
.map(CompactAddr::from),
18 => <[u8; 18]>::try_from(v.as_ref())
18 => <[u8; 18]>::try_from(v)
.ok()
.map(CompactAddrV6::from)
.map(CompactAddr::from),
Expand All @@ -193,7 +193,6 @@ impl<'a> RespValues<'a, Vec<&'a Bytes>> {
#[cfg(test)]
mod tests {
use bt_bencode::Error;
use serde_bytes::Bytes;

use super::*;

Expand Down Expand Up @@ -221,9 +220,9 @@ mod tests {
);

let ser_query_msg = ser::QueryMsg {
t: Bytes::new(b"aa"),
t: b"aa",
v: None,
q: Bytes::new(METHOD_GET_PEERS),
q: METHOD_GET_PEERS,
a: query_args,
};
let ser_msg = bt_bencode::to_vec(&ser_query_msg)?;
Expand Down Expand Up @@ -252,7 +251,7 @@ mod tests {
assert_eq!(msg.ty(), Ty::Response);
assert_eq!(msg.client_version(), None);

let resp_values: RespValues<'_, Vec<&'_ Bytes>> = msg.values().unwrap()?;
let resp_values: RespValues<'_, Vec<&'_ [u8]>> = msg.values().unwrap()?;
assert_eq!(resp_values.id(), Some(Id::from(*b"0123456789abcdefghij")));
assert!(resp_values.values().is_none());
assert_eq!(
Expand All @@ -262,7 +261,7 @@ mod tests {
assert!(resp_values.nodes6().is_none());

let ser_resp_msg = ser::RespMsg {
t: Bytes::new(b"aa"),
t: b"aa",
v: None,
r: &resp_values,
};
Expand All @@ -289,15 +288,15 @@ mod tests {
let values: Vec<CompactAddr> = vec![compact_addr_v4.into(), compact_addr_v6.into()];

let resp_values: RespValues<'_, Vec<CompactAddr>> = RespValues {
id: Bytes::new(b"0123456789abcdefghij"),
id: b"0123456789abcdefghij",
nodes: None,
nodes6: None,
token: Bytes::new(b"abcd1234"),
token: b"abcd1234",
values: Some(values),
};

let ser_resp_msg = ser::RespMsg {
t: Bytes::new(b"aa"),
t: b"aa",
v: None,
r: &resp_values,
};
Expand All @@ -308,7 +307,7 @@ mod tests {
assert_eq!(msg.ty(), Ty::Response);
assert_eq!(msg.client_version(), None);

let resp_values: RespValues<'_, Vec<&'_ Bytes>> = msg.values().unwrap().unwrap();
let resp_values: RespValues<'_, Vec<&'_ [u8]>> = msg.values().unwrap().unwrap();
assert_eq!(resp_values.id(), Some(Id::from(*b"0123456789abcdefghij")));
assert_eq!(
resp_values.values().unwrap().collect::<Vec<_>>(),
Expand Down
27 changes: 11 additions & 16 deletions src/dht/krpc/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
//! [bep_0005]: http://bittorrent.org/beps/bep_0005.html
use core::convert::TryFrom;
use serde_bytes::Bytes;
use serde_derive::{Deserialize, Serialize};

use crate::dht::node::{Id, LocalId};
Expand All @@ -25,51 +24,47 @@ pub const METHOD_PING: &[u8] = b"ping";
#[derive(Debug, Deserialize, Serialize)]
pub struct QueryArgs<'a> {
/// The querying node's ID
#[serde(borrow)]
pub id: &'a Bytes,
#[serde(with = "serde_bytes")]
pub id: &'a [u8],
}

impl<'a> QueryArgs<'a> {
/// Constructs a new `QueryArgs` based on the local node ID.
#[must_use]
#[inline]
pub fn new(id: &'a LocalId) -> Self {
Self {
id: Bytes::new(&(id.0).0),
}
Self { id: &(id.0).0 }
}

/// Returns the querying node's ID.
#[must_use]
#[inline]
pub fn id(&self) -> Option<Id> {
Id::try_from(self.id.as_ref()).ok()
Id::try_from(self.id).ok()
}
}

/// The arguments for the ping query message.
#[derive(Debug, Deserialize, Serialize)]
pub struct RespValues<'a> {
/// The queried node's ID
#[serde(borrow)]
pub id: &'a Bytes,
#[serde(with = "serde_bytes")]
pub id: &'a [u8],
}

impl<'a> RespValues<'a> {
/// Constructs a new `RespValue` based on the local node ID.
#[must_use]
#[inline]
pub fn new(id: &'a LocalId) -> Self {
Self {
id: Bytes::new(&(id.0).0),
}
Self { id: &(id.0).0 }
}

/// Returns the queried node's ID.
#[must_use]
#[inline]
pub fn id(&self) -> Option<Id> {
Id::try_from(self.id.as_ref()).ok()
Id::try_from(self.id).ok()
}
}

Expand Down Expand Up @@ -97,9 +92,9 @@ mod tests {
assert_eq!(query_args.id(), Some(Id::from(*b"abcdefghij0123456789")));

let ser_query_msg = ser::QueryMsg {
t: Bytes::new(b"aa"),
t: b"aa".as_slice(),
v: None,
q: Bytes::new(METHOD_PING),
q: METHOD_PING,
a: query_args,
};
let ser_msg = bt_bencode::to_vec(&ser_query_msg)?;
Expand All @@ -121,7 +116,7 @@ mod tests {
assert_eq!(resp_values.id(), Some(Id::from(*b"mnopqrstuvwxyz123456")));

let ser_resp_msg = ser::RespMsg {
t: Bytes::new(b"aa"),
t: b"aa".as_slice(),
v: None,
r: &resp_values,
};
Expand Down
Loading

0 comments on commit 89fc4fc

Please sign in to comment.