mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 17:55:36 +00:00
use zerocopy in udp protocol, easy running transfer CI locally
This commit is contained in:
parent
af16a9e682
commit
0e12dd1b13
24 changed files with 783 additions and 652 deletions
|
|
@ -15,7 +15,9 @@ aquatic_peer_id.workspace = true
|
|||
|
||||
byteorder = "1"
|
||||
either = "1"
|
||||
zerocopy = { version = "0.7", features = ["derive"] }
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = "1"
|
||||
quickcheck = "1"
|
||||
quickcheck_macros = "1"
|
||||
|
|
|
|||
|
|
@ -2,45 +2,174 @@ use std::fmt::Debug;
|
|||
use std::net::{Ipv4Addr, Ipv6Addr};
|
||||
|
||||
pub use aquatic_peer_id::{PeerClient, PeerId};
|
||||
use zerocopy::network_endian::{I32, I64, U16, U32};
|
||||
use zerocopy::{AsBytes, FromBytes, FromZeroes};
|
||||
|
||||
pub trait Ip: Clone + Copy + Debug + PartialEq + Eq {}
|
||||
pub trait Ip: Clone + Copy + Debug + PartialEq + Eq + AsBytes {}
|
||||
|
||||
impl Ip for Ipv4Addr {}
|
||||
impl Ip for Ipv6Addr {}
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
|
||||
#[repr(transparent)]
|
||||
pub struct AnnounceInterval(pub I32);
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub struct AnnounceInterval(pub i32);
|
||||
impl AnnounceInterval {
|
||||
pub fn new(v: i32) -> Self {
|
||||
Self(I32::new(v))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
|
||||
#[repr(transparent)]
|
||||
pub struct InfoHash(pub [u8; 20]);
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub struct ConnectionId(pub i64);
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
|
||||
#[repr(transparent)]
|
||||
pub struct ConnectionId(pub I64);
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub struct TransactionId(pub i32);
|
||||
impl ConnectionId {
|
||||
pub fn new(v: i64) -> Self {
|
||||
Self(I64::new(v))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub struct NumberOfBytes(pub i64);
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
|
||||
#[repr(transparent)]
|
||||
pub struct TransactionId(pub I32);
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub struct NumberOfPeers(pub i32);
|
||||
impl TransactionId {
|
||||
pub fn new(v: i32) -> Self {
|
||||
Self(I32::new(v))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub struct NumberOfDownloads(pub i32);
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
|
||||
#[repr(transparent)]
|
||||
pub struct NumberOfBytes(pub I64);
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub struct Port(pub u16);
|
||||
impl NumberOfBytes {
|
||||
pub fn new(v: i64) -> Self {
|
||||
Self(I64::new(v))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub struct PeerKey(pub u32);
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
|
||||
#[repr(transparent)]
|
||||
pub struct NumberOfPeers(pub I32);
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
impl NumberOfPeers {
|
||||
pub fn new(v: i32) -> Self {
|
||||
Self(I32::new(v))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
|
||||
#[repr(transparent)]
|
||||
pub struct NumberOfDownloads(pub I32);
|
||||
|
||||
impl NumberOfDownloads {
|
||||
pub fn new(v: i32) -> Self {
|
||||
Self(I32::new(v))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
|
||||
#[repr(transparent)]
|
||||
pub struct Port(pub U16);
|
||||
|
||||
impl Port {
|
||||
pub fn new(v: u16) -> Self {
|
||||
Self(U16::new(v))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
|
||||
#[repr(transparent)]
|
||||
pub struct PeerKey(pub I32);
|
||||
|
||||
impl PeerKey {
|
||||
pub fn new(v: i32) -> Self {
|
||||
Self(I32::new(v))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
|
||||
#[repr(C, packed)]
|
||||
pub struct ResponsePeer<I: Ip> {
|
||||
pub ip_address: I,
|
||||
pub port: Port,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
|
||||
#[repr(transparent)]
|
||||
pub struct Ipv4AddrBytes(pub [u8; 4]);
|
||||
|
||||
impl Ip for Ipv4AddrBytes {}
|
||||
|
||||
impl Into<Ipv4Addr> for Ipv4AddrBytes {
|
||||
fn into(self) -> Ipv4Addr {
|
||||
Ipv4Addr::from(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<Ipv4AddrBytes> for Ipv4Addr {
|
||||
fn into(self) -> Ipv4AddrBytes {
|
||||
Ipv4AddrBytes(self.octets())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
|
||||
#[repr(transparent)]
|
||||
pub struct Ipv6AddrBytes(pub [u8; 16]);
|
||||
|
||||
impl Ip for Ipv6AddrBytes {}
|
||||
|
||||
impl Into<Ipv6Addr> for Ipv6AddrBytes {
|
||||
fn into(self) -> Ipv6Addr {
|
||||
Ipv6Addr::from(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<Ipv6AddrBytes> for Ipv6Addr {
|
||||
fn into(self) -> Ipv6AddrBytes {
|
||||
Ipv6AddrBytes(self.octets())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_i32_ne(bytes: &mut impl ::std::io::Read) -> ::std::io::Result<I32> {
|
||||
let mut tmp = [0u8; 4];
|
||||
|
||||
bytes.read_exact(&mut tmp)?;
|
||||
|
||||
Ok(I32::from_bytes(tmp))
|
||||
}
|
||||
|
||||
pub fn read_i64_ne(bytes: &mut impl ::std::io::Read) -> ::std::io::Result<I64> {
|
||||
let mut tmp = [0u8; 8];
|
||||
|
||||
bytes.read_exact(&mut tmp)?;
|
||||
|
||||
Ok(I64::from_bytes(tmp))
|
||||
}
|
||||
|
||||
pub fn read_u16_ne(bytes: &mut impl ::std::io::Read) -> ::std::io::Result<U16> {
|
||||
let mut tmp = [0u8; 2];
|
||||
|
||||
bytes.read_exact(&mut tmp)?;
|
||||
|
||||
Ok(U16::from_bytes(tmp))
|
||||
}
|
||||
|
||||
pub fn read_u32_ne(bytes: &mut impl ::std::io::Read) -> ::std::io::Result<U32> {
|
||||
let mut tmp = [0u8; 4];
|
||||
|
||||
bytes.read_exact(&mut tmp)?;
|
||||
|
||||
Ok(U32::from_bytes(tmp))
|
||||
}
|
||||
|
||||
pub fn invalid_data() -> ::std::io::Error {
|
||||
::std::io::Error::new(::std::io::ErrorKind::InvalidData, "invalid data")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl quickcheck::Arbitrary for InfoHash {
|
||||
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
use std::convert::TryInto;
|
||||
use std::io::{self, Cursor, Read, Write};
|
||||
use std::net::Ipv4Addr;
|
||||
use std::io::{self, Cursor, Write};
|
||||
|
||||
use byteorder::{NetworkEndian, ReadBytesExt, WriteBytesExt};
|
||||
use byteorder::{NetworkEndian, WriteBytesExt};
|
||||
use either::Either;
|
||||
use zerocopy::FromZeroes;
|
||||
use zerocopy::{byteorder::network_endian::I32, AsBytes, FromBytes};
|
||||
|
||||
use aquatic_peer_id::PeerId;
|
||||
|
||||
|
|
@ -11,103 +11,6 @@ use super::common::*;
|
|||
|
||||
const PROTOCOL_IDENTIFIER: i64 = 4_497_486_125_440;
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub enum AnnounceEvent {
|
||||
Started,
|
||||
Stopped,
|
||||
Completed,
|
||||
None,
|
||||
}
|
||||
|
||||
impl AnnounceEvent {
|
||||
#[inline]
|
||||
pub fn from_i32(i: i32) -> Self {
|
||||
match i {
|
||||
1 => Self::Completed,
|
||||
2 => Self::Started,
|
||||
3 => Self::Stopped,
|
||||
_ => Self::None,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn to_i32(&self) -> i32 {
|
||||
match self {
|
||||
AnnounceEvent::None => 0,
|
||||
AnnounceEvent::Completed => 1,
|
||||
AnnounceEvent::Started => 2,
|
||||
AnnounceEvent::Stopped => 3,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ConnectRequest {
|
||||
pub transaction_id: TransactionId,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct AnnounceRequest {
|
||||
pub connection_id: ConnectionId,
|
||||
pub transaction_id: TransactionId,
|
||||
pub info_hash: InfoHash,
|
||||
pub peer_id: PeerId,
|
||||
pub bytes_downloaded: NumberOfBytes,
|
||||
pub bytes_uploaded: NumberOfBytes,
|
||||
pub bytes_left: NumberOfBytes,
|
||||
pub event: AnnounceEvent,
|
||||
pub ip_address: Option<Ipv4Addr>,
|
||||
pub key: PeerKey,
|
||||
pub peers_wanted: NumberOfPeers,
|
||||
pub port: Port,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ScrapeRequest {
|
||||
pub connection_id: ConnectionId,
|
||||
pub transaction_id: TransactionId,
|
||||
pub info_hashes: Vec<InfoHash>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum RequestParseError {
|
||||
Sendable {
|
||||
connection_id: ConnectionId,
|
||||
transaction_id: TransactionId,
|
||||
err: Either<io::Error, &'static str>,
|
||||
},
|
||||
Unsendable {
|
||||
err: Either<io::Error, &'static str>,
|
||||
},
|
||||
}
|
||||
|
||||
impl RequestParseError {
|
||||
pub fn sendable_io(err: io::Error, connection_id: i64, transaction_id: i32) -> Self {
|
||||
Self::Sendable {
|
||||
connection_id: ConnectionId(connection_id),
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
err: Either::Left(err),
|
||||
}
|
||||
}
|
||||
pub fn sendable_text(text: &'static str, connection_id: i64, transaction_id: i32) -> Self {
|
||||
Self::Sendable {
|
||||
connection_id: ConnectionId(connection_id),
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
err: Either::Right(text),
|
||||
}
|
||||
}
|
||||
pub fn unsendable_io(err: io::Error) -> Self {
|
||||
Self::Unsendable {
|
||||
err: Either::Left(err),
|
||||
}
|
||||
}
|
||||
pub fn unsendable_text(text: &'static str) -> Self {
|
||||
Self::Unsendable {
|
||||
err: Either::Right(text),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub enum Request {
|
||||
Connect(ConnectRequest),
|
||||
|
|
@ -115,6 +18,115 @@ pub enum Request {
|
|||
Scrape(ScrapeRequest),
|
||||
}
|
||||
|
||||
impl Request {
|
||||
pub fn write(self, bytes: &mut impl Write) -> Result<(), io::Error> {
|
||||
match self {
|
||||
Request::Connect(r) => {
|
||||
bytes.write_i64::<NetworkEndian>(PROTOCOL_IDENTIFIER)?;
|
||||
bytes.write_i32::<NetworkEndian>(0)?;
|
||||
bytes.write_all(r.transaction_id.as_bytes())?;
|
||||
}
|
||||
|
||||
Request::Announce(r) => {
|
||||
bytes.write_all(r.as_bytes())?;
|
||||
}
|
||||
|
||||
Request::Scrape(r) => {
|
||||
bytes.write_all(r.connection_id.as_bytes())?;
|
||||
bytes.write_i32::<NetworkEndian>(2)?;
|
||||
bytes.write_all(r.transaction_id.as_bytes())?;
|
||||
bytes.write_all((*r.info_hashes.as_slice()).as_bytes())?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn from_bytes(bytes: &[u8], max_scrape_torrents: u8) -> Result<Self, RequestParseError> {
|
||||
let action = bytes
|
||||
.get(8..12)
|
||||
.map(|bytes| I32::from_bytes(bytes.try_into().unwrap()))
|
||||
.ok_or_else(|| RequestParseError::unsendable_text("Couldn't parse action"))?;
|
||||
|
||||
match action.get() {
|
||||
// Connect
|
||||
0 => {
|
||||
let mut bytes = Cursor::new(bytes);
|
||||
|
||||
let protocol_identifier =
|
||||
read_i64_ne(&mut bytes).map_err(RequestParseError::unsendable_io)?;
|
||||
let _action = read_i32_ne(&mut bytes).map_err(RequestParseError::unsendable_io)?;
|
||||
let transaction_id = read_i32_ne(&mut bytes)
|
||||
.map(TransactionId)
|
||||
.map_err(RequestParseError::unsendable_io)?;
|
||||
|
||||
if protocol_identifier.get() == PROTOCOL_IDENTIFIER {
|
||||
Ok((ConnectRequest { transaction_id }).into())
|
||||
} else {
|
||||
Err(RequestParseError::unsendable_text(
|
||||
"Protocol identifier missing",
|
||||
))
|
||||
}
|
||||
}
|
||||
// Announce
|
||||
1 => {
|
||||
let request = AnnounceRequest::read_from_prefix(bytes)
|
||||
.ok_or_else(|| RequestParseError::unsendable_text("invalid data"))?;
|
||||
|
||||
// Make sure not to create AnnounceEventBytes with invalid value
|
||||
if matches!(request.event.0.get(), (0..=3)) {
|
||||
Ok(Request::Announce(request))
|
||||
} else {
|
||||
Err(RequestParseError::sendable_text(
|
||||
"Invalid announce event",
|
||||
request.connection_id,
|
||||
request.transaction_id,
|
||||
))
|
||||
}
|
||||
}
|
||||
// Scrape
|
||||
2 => {
|
||||
let mut bytes = Cursor::new(bytes);
|
||||
|
||||
let connection_id = read_i64_ne(&mut bytes)
|
||||
.map(ConnectionId)
|
||||
.map_err(RequestParseError::unsendable_io)?;
|
||||
let _action = read_i32_ne(&mut bytes).map_err(RequestParseError::unsendable_io)?;
|
||||
let transaction_id = read_i32_ne(&mut bytes)
|
||||
.map(TransactionId)
|
||||
.map_err(RequestParseError::unsendable_io)?;
|
||||
|
||||
let remaining_bytes = {
|
||||
let position = bytes.position() as usize;
|
||||
let inner = bytes.into_inner();
|
||||
&inner[position..]
|
||||
};
|
||||
|
||||
let info_hashes = FromBytes::slice_from(remaining_bytes).ok_or_else(|| {
|
||||
RequestParseError::sendable_text(
|
||||
"Invalid info hash list. Note that full scrapes are not allowed",
|
||||
connection_id,
|
||||
transaction_id,
|
||||
)
|
||||
})?;
|
||||
|
||||
let info_hashes = Vec::from(
|
||||
&info_hashes[..(max_scrape_torrents as usize).min(info_hashes.len())],
|
||||
);
|
||||
|
||||
Ok((ScrapeRequest {
|
||||
connection_id,
|
||||
transaction_id,
|
||||
info_hashes,
|
||||
})
|
||||
.into())
|
||||
}
|
||||
|
||||
_ => Err(RequestParseError::unsendable_text("Invalid action")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ConnectRequest> for Request {
|
||||
fn from(r: ConnectRequest) -> Self {
|
||||
Self::Connect(r)
|
||||
|
|
@ -133,173 +145,116 @@ impl From<ScrapeRequest> for Request {
|
|||
}
|
||||
}
|
||||
|
||||
impl Request {
|
||||
pub fn write(self, bytes: &mut impl Write) -> Result<(), io::Error> {
|
||||
match self {
|
||||
Request::Connect(r) => {
|
||||
bytes.write_i64::<NetworkEndian>(PROTOCOL_IDENTIFIER)?;
|
||||
bytes.write_i32::<NetworkEndian>(0)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.transaction_id.0)?;
|
||||
}
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ConnectRequest {
|
||||
pub transaction_id: TransactionId,
|
||||
}
|
||||
|
||||
Request::Announce(r) => {
|
||||
bytes.write_i64::<NetworkEndian>(r.connection_id.0)?;
|
||||
bytes.write_i32::<NetworkEndian>(1)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.transaction_id.0)?;
|
||||
#[derive(PartialEq, Eq, Clone, Debug, AsBytes, FromBytes, FromZeroes)]
|
||||
#[repr(C, packed)]
|
||||
pub struct AnnounceRequest {
|
||||
pub connection_id: ConnectionId,
|
||||
/// This field is only present to enable zero-copy serialization and
|
||||
/// deserialization.
|
||||
pub action_placeholder: AnnounceActionPlaceholder,
|
||||
pub transaction_id: TransactionId,
|
||||
pub info_hash: InfoHash,
|
||||
pub peer_id: PeerId,
|
||||
pub bytes_downloaded: NumberOfBytes,
|
||||
pub bytes_left: NumberOfBytes,
|
||||
pub bytes_uploaded: NumberOfBytes,
|
||||
pub event: AnnounceEventBytes,
|
||||
pub ip_address: Ipv4AddrBytes,
|
||||
pub key: PeerKey,
|
||||
pub peers_wanted: NumberOfPeers,
|
||||
pub port: Port,
|
||||
}
|
||||
|
||||
bytes.write_all(&r.info_hash.0)?;
|
||||
bytes.write_all(&r.peer_id.0)?;
|
||||
/// Note: Request::from_bytes only creates this struct with value 1
|
||||
#[derive(PartialEq, Eq, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
|
||||
#[repr(transparent)]
|
||||
pub struct AnnounceActionPlaceholder(I32);
|
||||
|
||||
bytes.write_i64::<NetworkEndian>(r.bytes_downloaded.0)?;
|
||||
bytes.write_i64::<NetworkEndian>(r.bytes_left.0)?;
|
||||
bytes.write_i64::<NetworkEndian>(r.bytes_uploaded.0)?;
|
||||
|
||||
bytes.write_i32::<NetworkEndian>(r.event.to_i32())?;
|
||||
|
||||
bytes.write_all(&r.ip_address.map_or([0; 4], |ip| ip.octets()))?;
|
||||
|
||||
bytes.write_u32::<NetworkEndian>(r.key.0)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.peers_wanted.0)?;
|
||||
bytes.write_u16::<NetworkEndian>(r.port.0)?;
|
||||
}
|
||||
|
||||
Request::Scrape(r) => {
|
||||
bytes.write_i64::<NetworkEndian>(r.connection_id.0)?;
|
||||
bytes.write_i32::<NetworkEndian>(2)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.transaction_id.0)?;
|
||||
|
||||
for info_hash in r.info_hashes {
|
||||
bytes.write_all(&info_hash.0)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
impl Default for AnnounceActionPlaceholder {
|
||||
fn default() -> Self {
|
||||
Self(I32::new(1))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_bytes(bytes: &[u8], max_scrape_torrents: u8) -> Result<Self, RequestParseError> {
|
||||
let mut cursor = Cursor::new(bytes);
|
||||
/// Note: Request::from_bytes only creates this struct with values 0..=3
|
||||
#[derive(PartialEq, Eq, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
|
||||
#[repr(transparent)]
|
||||
pub struct AnnounceEventBytes(I32);
|
||||
|
||||
let connection_id = cursor
|
||||
.read_i64::<NetworkEndian>()
|
||||
.map_err(RequestParseError::unsendable_io)?;
|
||||
let action = cursor
|
||||
.read_i32::<NetworkEndian>()
|
||||
.map_err(RequestParseError::unsendable_io)?;
|
||||
let transaction_id = cursor
|
||||
.read_i32::<NetworkEndian>()
|
||||
.map_err(RequestParseError::unsendable_io)?;
|
||||
impl From<AnnounceEvent> for AnnounceEventBytes {
|
||||
fn from(value: AnnounceEvent) -> Self {
|
||||
Self(I32::new(match value {
|
||||
AnnounceEvent::None => 0,
|
||||
AnnounceEvent::Completed => 1,
|
||||
AnnounceEvent::Started => 2,
|
||||
AnnounceEvent::Stopped => 3,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
match action {
|
||||
// Connect
|
||||
0 => {
|
||||
if connection_id == PROTOCOL_IDENTIFIER {
|
||||
Ok((ConnectRequest {
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
})
|
||||
.into())
|
||||
} else {
|
||||
Err(RequestParseError::unsendable_text(
|
||||
"Protocol identifier missing",
|
||||
))
|
||||
}
|
||||
}
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub enum AnnounceEvent {
|
||||
Started,
|
||||
Stopped,
|
||||
Completed,
|
||||
None,
|
||||
}
|
||||
|
||||
// Announce
|
||||
1 => {
|
||||
let mut info_hash = [0; 20];
|
||||
let mut peer_id = [0; 20];
|
||||
let mut ip = [0; 4];
|
||||
impl From<AnnounceEventBytes> for AnnounceEvent {
|
||||
fn from(value: AnnounceEventBytes) -> Self {
|
||||
match value.0.get() {
|
||||
1 => Self::Completed,
|
||||
2 => Self::Started,
|
||||
3 => Self::Stopped,
|
||||
_ => Self::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cursor.read_exact(&mut info_hash).map_err(|err| {
|
||||
RequestParseError::sendable_io(err, connection_id, transaction_id)
|
||||
})?;
|
||||
cursor.read_exact(&mut peer_id).map_err(|err| {
|
||||
RequestParseError::sendable_io(err, connection_id, transaction_id)
|
||||
})?;
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ScrapeRequest {
|
||||
pub connection_id: ConnectionId,
|
||||
pub transaction_id: TransactionId,
|
||||
pub info_hashes: Vec<InfoHash>,
|
||||
}
|
||||
|
||||
let bytes_downloaded = cursor.read_i64::<NetworkEndian>().map_err(|err| {
|
||||
RequestParseError::sendable_io(err, connection_id, transaction_id)
|
||||
})?;
|
||||
let bytes_left = cursor.read_i64::<NetworkEndian>().map_err(|err| {
|
||||
RequestParseError::sendable_io(err, connection_id, transaction_id)
|
||||
})?;
|
||||
let bytes_uploaded = cursor.read_i64::<NetworkEndian>().map_err(|err| {
|
||||
RequestParseError::sendable_io(err, connection_id, transaction_id)
|
||||
})?;
|
||||
let event = cursor.read_i32::<NetworkEndian>().map_err(|err| {
|
||||
RequestParseError::sendable_io(err, connection_id, transaction_id)
|
||||
})?;
|
||||
#[derive(Debug)]
|
||||
pub enum RequestParseError {
|
||||
Sendable {
|
||||
connection_id: ConnectionId,
|
||||
transaction_id: TransactionId,
|
||||
err: &'static str,
|
||||
},
|
||||
Unsendable {
|
||||
err: Either<io::Error, &'static str>,
|
||||
},
|
||||
}
|
||||
|
||||
cursor.read_exact(&mut ip).map_err(|err| {
|
||||
RequestParseError::sendable_io(err, connection_id, transaction_id)
|
||||
})?;
|
||||
|
||||
let key = cursor.read_u32::<NetworkEndian>().map_err(|err| {
|
||||
RequestParseError::sendable_io(err, connection_id, transaction_id)
|
||||
})?;
|
||||
let peers_wanted = cursor.read_i32::<NetworkEndian>().map_err(|err| {
|
||||
RequestParseError::sendable_io(err, connection_id, transaction_id)
|
||||
})?;
|
||||
let port = cursor.read_u16::<NetworkEndian>().map_err(|err| {
|
||||
RequestParseError::sendable_io(err, connection_id, transaction_id)
|
||||
})?;
|
||||
|
||||
let opt_ip = if ip == [0; 4] {
|
||||
None
|
||||
} else {
|
||||
Some(Ipv4Addr::from(ip))
|
||||
};
|
||||
|
||||
Ok((AnnounceRequest {
|
||||
connection_id: ConnectionId(connection_id),
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
info_hash: InfoHash(info_hash),
|
||||
peer_id: PeerId(peer_id),
|
||||
bytes_downloaded: NumberOfBytes(bytes_downloaded),
|
||||
bytes_uploaded: NumberOfBytes(bytes_uploaded),
|
||||
bytes_left: NumberOfBytes(bytes_left),
|
||||
event: AnnounceEvent::from_i32(event),
|
||||
ip_address: opt_ip,
|
||||
key: PeerKey(key),
|
||||
peers_wanted: NumberOfPeers(peers_wanted),
|
||||
port: Port(port),
|
||||
})
|
||||
.into())
|
||||
}
|
||||
|
||||
// Scrape
|
||||
2 => {
|
||||
let position = cursor.position() as usize;
|
||||
let inner = cursor.into_inner();
|
||||
|
||||
let info_hashes: Vec<InfoHash> = (&inner[position..])
|
||||
.chunks_exact(20)
|
||||
.take(max_scrape_torrents as usize)
|
||||
.map(|chunk| InfoHash(chunk.try_into().unwrap()))
|
||||
.collect();
|
||||
|
||||
if info_hashes.is_empty() {
|
||||
Err(RequestParseError::sendable_text(
|
||||
"Full scrapes are not allowed",
|
||||
connection_id,
|
||||
transaction_id,
|
||||
))
|
||||
} else {
|
||||
Ok((ScrapeRequest {
|
||||
connection_id: ConnectionId(connection_id),
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
info_hashes,
|
||||
})
|
||||
.into())
|
||||
}
|
||||
}
|
||||
|
||||
_ => Err(RequestParseError::sendable_text(
|
||||
"Invalid action",
|
||||
connection_id,
|
||||
transaction_id,
|
||||
)),
|
||||
impl RequestParseError {
|
||||
pub fn sendable_text(
|
||||
text: &'static str,
|
||||
connection_id: ConnectionId,
|
||||
transaction_id: TransactionId,
|
||||
) -> Self {
|
||||
Self::Sendable {
|
||||
connection_id,
|
||||
transaction_id,
|
||||
err: text,
|
||||
}
|
||||
}
|
||||
pub fn unsendable_io(err: io::Error) -> Self {
|
||||
Self::Unsendable {
|
||||
err: Either::Left(err),
|
||||
}
|
||||
}
|
||||
pub fn unsendable_text(text: &'static str) -> Self {
|
||||
Self::Unsendable {
|
||||
err: Either::Right(text),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -308,6 +263,7 @@ impl Request {
|
|||
mod tests {
|
||||
use quickcheck::TestResult;
|
||||
use quickcheck_macros::quickcheck;
|
||||
use zerocopy::network_endian::{I32, I64, U16};
|
||||
|
||||
use super::*;
|
||||
|
||||
|
|
@ -325,7 +281,7 @@ mod tests {
|
|||
impl quickcheck::Arbitrary for ConnectRequest {
|
||||
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
|
||||
Self {
|
||||
transaction_id: TransactionId(i32::arbitrary(g)),
|
||||
transaction_id: TransactionId(I32::new(i32::arbitrary(g))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -333,18 +289,19 @@ mod tests {
|
|||
impl quickcheck::Arbitrary for AnnounceRequest {
|
||||
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
|
||||
Self {
|
||||
connection_id: ConnectionId(i64::arbitrary(g)),
|
||||
transaction_id: TransactionId(i32::arbitrary(g)),
|
||||
connection_id: ConnectionId(I64::new(i64::arbitrary(g))),
|
||||
action_placeholder: AnnounceActionPlaceholder::default(),
|
||||
transaction_id: TransactionId(I32::new(i32::arbitrary(g))),
|
||||
info_hash: InfoHash::arbitrary(g),
|
||||
peer_id: PeerId::arbitrary(g),
|
||||
bytes_downloaded: NumberOfBytes(i64::arbitrary(g)),
|
||||
bytes_uploaded: NumberOfBytes(i64::arbitrary(g)),
|
||||
bytes_left: NumberOfBytes(i64::arbitrary(g)),
|
||||
event: AnnounceEvent::arbitrary(g),
|
||||
ip_address: None,
|
||||
key: PeerKey(u32::arbitrary(g)),
|
||||
peers_wanted: NumberOfPeers(i32::arbitrary(g)),
|
||||
port: Port(u16::arbitrary(g)),
|
||||
bytes_downloaded: NumberOfBytes(I64::new(i64::arbitrary(g))),
|
||||
bytes_uploaded: NumberOfBytes(I64::new(i64::arbitrary(g))),
|
||||
bytes_left: NumberOfBytes(I64::new(i64::arbitrary(g))),
|
||||
event: AnnounceEvent::arbitrary(g).into(),
|
||||
ip_address: Ipv4AddrBytes::arbitrary(g),
|
||||
key: PeerKey::new(i32::arbitrary(g)),
|
||||
peers_wanted: NumberOfPeers(I32::new(i32::arbitrary(g))),
|
||||
port: Port(U16::new(u16::arbitrary(g))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -356,8 +313,8 @@ mod tests {
|
|||
.collect();
|
||||
|
||||
Self {
|
||||
connection_id: ConnectionId(i64::arbitrary(g)),
|
||||
transaction_id: TransactionId(i32::arbitrary(g)),
|
||||
connection_id: ConnectionId(I64::new(i64::arbitrary(g))),
|
||||
transaction_id: TransactionId(I32::new(i32::arbitrary(g))),
|
||||
info_hashes,
|
||||
}
|
||||
}
|
||||
|
|
@ -372,7 +329,7 @@ mod tests {
|
|||
let success = request == r2;
|
||||
|
||||
if !success {
|
||||
println!("before: {:#?}\nafter: {:#?}", request, r2);
|
||||
::pretty_assertions::assert_eq!(request, r2);
|
||||
}
|
||||
|
||||
success
|
||||
|
|
|
|||
|
|
@ -1,69 +1,138 @@
|
|||
use std::borrow::Cow;
|
||||
use std::convert::TryInto;
|
||||
use std::io::{self, Cursor, Write};
|
||||
use std::net::{Ipv4Addr, Ipv6Addr};
|
||||
use std::io::{self, Write};
|
||||
use std::mem::size_of;
|
||||
|
||||
use byteorder::{NetworkEndian, ReadBytesExt, WriteBytesExt};
|
||||
use byteorder::{NetworkEndian, WriteBytesExt};
|
||||
use zerocopy::{AsBytes, FromBytes, FromZeroes};
|
||||
|
||||
use super::common::*;
|
||||
|
||||
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
|
||||
pub struct TorrentScrapeStatistics {
|
||||
pub seeders: NumberOfPeers,
|
||||
pub completed: NumberOfDownloads,
|
||||
pub leechers: NumberOfPeers,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ConnectResponse {
|
||||
pub connection_id: ConnectionId,
|
||||
pub transaction_id: TransactionId,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct AnnounceResponse<I: Ip> {
|
||||
pub transaction_id: TransactionId,
|
||||
pub announce_interval: AnnounceInterval,
|
||||
pub leechers: NumberOfPeers,
|
||||
pub seeders: NumberOfPeers,
|
||||
pub peers: Vec<ResponsePeer<I>>,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ScrapeResponse {
|
||||
pub transaction_id: TransactionId,
|
||||
pub torrent_stats: Vec<TorrentScrapeStatistics>,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ErrorResponse {
|
||||
pub transaction_id: TransactionId,
|
||||
pub message: Cow<'static, str>,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub enum Response {
|
||||
Connect(ConnectResponse),
|
||||
AnnounceIpv4(AnnounceResponse<Ipv4Addr>),
|
||||
AnnounceIpv6(AnnounceResponse<Ipv6Addr>),
|
||||
AnnounceIpv4(AnnounceResponse<Ipv4AddrBytes>),
|
||||
AnnounceIpv6(AnnounceResponse<Ipv6AddrBytes>),
|
||||
Scrape(ScrapeResponse),
|
||||
Error(ErrorResponse),
|
||||
}
|
||||
|
||||
impl Response {
|
||||
#[inline]
|
||||
pub fn write(&self, bytes: &mut impl Write) -> Result<(), io::Error> {
|
||||
match self {
|
||||
Response::Connect(r) => {
|
||||
bytes.write_i32::<NetworkEndian>(0)?;
|
||||
bytes.write_all(r.as_bytes())?;
|
||||
}
|
||||
Response::AnnounceIpv4(r) => {
|
||||
bytes.write_i32::<NetworkEndian>(1)?;
|
||||
bytes.write_all(r.fixed.as_bytes())?;
|
||||
bytes.write_all((*r.peers.as_slice()).as_bytes())?;
|
||||
}
|
||||
Response::AnnounceIpv6(r) => {
|
||||
bytes.write_i32::<NetworkEndian>(1)?;
|
||||
bytes.write_all(r.fixed.as_bytes())?;
|
||||
bytes.write_all((*r.peers.as_slice()).as_bytes())?;
|
||||
}
|
||||
Response::Scrape(r) => {
|
||||
bytes.write_i32::<NetworkEndian>(2)?;
|
||||
bytes.write_all(r.transaction_id.as_bytes())?;
|
||||
bytes.write_all((*r.torrent_stats.as_slice()).as_bytes())?;
|
||||
}
|
||||
Response::Error(r) => {
|
||||
bytes.write_i32::<NetworkEndian>(3)?;
|
||||
bytes.write_all(r.transaction_id.as_bytes())?;
|
||||
bytes.write_all(r.message.as_bytes())?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_bytes(mut bytes: &[u8], ipv4: bool) -> Result<Self, io::Error> {
|
||||
let action = read_i32_ne(&mut bytes)?;
|
||||
|
||||
match action.get() {
|
||||
// Connect
|
||||
0 => Ok(Response::Connect(
|
||||
ConnectResponse::read_from_prefix(bytes).ok_or_else(invalid_data)?,
|
||||
)),
|
||||
// Announce
|
||||
1 if ipv4 => {
|
||||
let fixed =
|
||||
AnnounceResponseFixedData::read_from_prefix(bytes).ok_or_else(invalid_data)?;
|
||||
|
||||
let peers = if let Some(bytes) = bytes.get(size_of::<AnnounceResponseFixedData>()..)
|
||||
{
|
||||
Vec::from(
|
||||
ResponsePeer::<Ipv4AddrBytes>::slice_from(bytes)
|
||||
.ok_or_else(invalid_data)?,
|
||||
)
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
|
||||
Ok(Response::AnnounceIpv4(AnnounceResponse { fixed, peers }))
|
||||
}
|
||||
1 if !ipv4 => {
|
||||
let fixed =
|
||||
AnnounceResponseFixedData::read_from_prefix(bytes).ok_or_else(invalid_data)?;
|
||||
|
||||
let peers = if let Some(bytes) = bytes.get(size_of::<AnnounceResponseFixedData>()..)
|
||||
{
|
||||
Vec::from(
|
||||
ResponsePeer::<Ipv6AddrBytes>::slice_from(bytes)
|
||||
.ok_or_else(invalid_data)?,
|
||||
)
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
|
||||
Ok(Response::AnnounceIpv6(AnnounceResponse { fixed, peers }))
|
||||
}
|
||||
// Scrape
|
||||
2 => {
|
||||
let transaction_id = read_i32_ne(&mut bytes).map(TransactionId)?;
|
||||
let torrent_stats =
|
||||
Vec::from(TorrentScrapeStatistics::slice_from(bytes).ok_or_else(invalid_data)?);
|
||||
|
||||
Ok((ScrapeResponse {
|
||||
transaction_id,
|
||||
torrent_stats,
|
||||
})
|
||||
.into())
|
||||
}
|
||||
// Error
|
||||
3 => {
|
||||
let transaction_id = read_i32_ne(&mut bytes).map(TransactionId)?;
|
||||
let message = String::from_utf8_lossy(&bytes).into_owned().into();
|
||||
|
||||
Ok((ErrorResponse {
|
||||
transaction_id,
|
||||
message,
|
||||
})
|
||||
.into())
|
||||
}
|
||||
_ => Err(invalid_data()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ConnectResponse> for Response {
|
||||
fn from(r: ConnectResponse) -> Self {
|
||||
Self::Connect(r)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AnnounceResponse<Ipv4Addr>> for Response {
|
||||
fn from(r: AnnounceResponse<Ipv4Addr>) -> Self {
|
||||
impl From<AnnounceResponse<Ipv4AddrBytes>> for Response {
|
||||
fn from(r: AnnounceResponse<Ipv4AddrBytes>) -> Self {
|
||||
Self::AnnounceIpv4(r)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AnnounceResponse<Ipv6Addr>> for Response {
|
||||
fn from(r: AnnounceResponse<Ipv6Addr>) -> Self {
|
||||
impl From<AnnounceResponse<Ipv6AddrBytes>> for Response {
|
||||
fn from(r: AnnounceResponse<Ipv6AddrBytes>) -> Self {
|
||||
Self::AnnounceIpv6(r)
|
||||
}
|
||||
}
|
||||
|
|
@ -80,203 +149,85 @@ impl From<ErrorResponse> for Response {
|
|||
}
|
||||
}
|
||||
|
||||
impl Response {
|
||||
#[inline]
|
||||
pub fn write(&self, bytes: &mut impl Write) -> Result<(), io::Error> {
|
||||
match self {
|
||||
Response::Connect(r) => {
|
||||
bytes.write_i32::<NetworkEndian>(0)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.transaction_id.0)?;
|
||||
bytes.write_i64::<NetworkEndian>(r.connection_id.0)?;
|
||||
}
|
||||
Response::AnnounceIpv4(r) => {
|
||||
bytes.write_i32::<NetworkEndian>(1)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.transaction_id.0)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.announce_interval.0)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.leechers.0)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.seeders.0)?;
|
||||
#[derive(PartialEq, Eq, Clone, Debug, AsBytes, FromBytes, FromZeroes)]
|
||||
#[repr(C, packed)]
|
||||
pub struct ConnectResponse {
|
||||
pub transaction_id: TransactionId,
|
||||
pub connection_id: ConnectionId,
|
||||
}
|
||||
|
||||
for peer in r.peers.iter() {
|
||||
bytes.write_all(&peer.ip_address.octets())?;
|
||||
bytes.write_u16::<NetworkEndian>(peer.port.0)?;
|
||||
}
|
||||
}
|
||||
Response::AnnounceIpv6(r) => {
|
||||
bytes.write_i32::<NetworkEndian>(1)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.transaction_id.0)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.announce_interval.0)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.leechers.0)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.seeders.0)?;
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct AnnounceResponse<I: Ip> {
|
||||
pub fixed: AnnounceResponseFixedData,
|
||||
pub peers: Vec<ResponsePeer<I>>,
|
||||
}
|
||||
|
||||
for peer in r.peers.iter() {
|
||||
bytes.write_all(&peer.ip_address.octets())?;
|
||||
bytes.write_u16::<NetworkEndian>(peer.port.0)?;
|
||||
}
|
||||
}
|
||||
Response::Scrape(r) => {
|
||||
bytes.write_i32::<NetworkEndian>(2)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.transaction_id.0)?;
|
||||
#[derive(PartialEq, Eq, Clone, Debug, AsBytes, FromBytes, FromZeroes)]
|
||||
#[repr(C, packed)]
|
||||
pub struct AnnounceResponseFixedData {
|
||||
pub transaction_id: TransactionId,
|
||||
pub announce_interval: AnnounceInterval,
|
||||
pub leechers: NumberOfPeers,
|
||||
pub seeders: NumberOfPeers,
|
||||
}
|
||||
|
||||
for torrent_stat in r.torrent_stats.iter() {
|
||||
bytes.write_i32::<NetworkEndian>(torrent_stat.seeders.0)?;
|
||||
bytes.write_i32::<NetworkEndian>(torrent_stat.completed.0)?;
|
||||
bytes.write_i32::<NetworkEndian>(torrent_stat.leechers.0)?;
|
||||
}
|
||||
}
|
||||
Response::Error(r) => {
|
||||
bytes.write_i32::<NetworkEndian>(3)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.transaction_id.0)?;
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ScrapeResponse {
|
||||
pub transaction_id: TransactionId,
|
||||
pub torrent_stats: Vec<TorrentScrapeStatistics>,
|
||||
}
|
||||
|
||||
bytes.write_all(r.message.as_bytes())?;
|
||||
}
|
||||
}
|
||||
#[derive(PartialEq, Eq, Debug, Copy, Clone, AsBytes, FromBytes, FromZeroes)]
|
||||
#[repr(C, packed)]
|
||||
pub struct TorrentScrapeStatistics {
|
||||
pub seeders: NumberOfPeers,
|
||||
pub completed: NumberOfDownloads,
|
||||
pub leechers: NumberOfPeers,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_bytes(bytes: &[u8], ipv4: bool) -> Result<Self, io::Error> {
|
||||
let mut cursor = Cursor::new(bytes);
|
||||
|
||||
let action = cursor.read_i32::<NetworkEndian>()?;
|
||||
let transaction_id = cursor.read_i32::<NetworkEndian>()?;
|
||||
|
||||
match action {
|
||||
// Connect
|
||||
0 => {
|
||||
let connection_id = cursor.read_i64::<NetworkEndian>()?;
|
||||
|
||||
Ok((ConnectResponse {
|
||||
connection_id: ConnectionId(connection_id),
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
})
|
||||
.into())
|
||||
}
|
||||
// Announce
|
||||
1 if ipv4 => {
|
||||
let announce_interval = cursor.read_i32::<NetworkEndian>()?;
|
||||
let leechers = cursor.read_i32::<NetworkEndian>()?;
|
||||
let seeders = cursor.read_i32::<NetworkEndian>()?;
|
||||
|
||||
let position = cursor.position() as usize;
|
||||
let inner = cursor.into_inner();
|
||||
|
||||
let peers = inner[position..]
|
||||
.chunks_exact(6)
|
||||
.map(|chunk| {
|
||||
let ip_bytes: [u8; 4] = (&chunk[..4]).try_into().unwrap();
|
||||
let ip_address = Ipv4Addr::from(ip_bytes);
|
||||
let port = (&chunk[4..]).read_u16::<NetworkEndian>().unwrap();
|
||||
|
||||
ResponsePeer {
|
||||
ip_address,
|
||||
port: Port(port),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok((AnnounceResponse {
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
announce_interval: AnnounceInterval(announce_interval),
|
||||
leechers: NumberOfPeers(leechers),
|
||||
seeders: NumberOfPeers(seeders),
|
||||
peers,
|
||||
})
|
||||
.into())
|
||||
}
|
||||
1 if !ipv4 => {
|
||||
let announce_interval = cursor.read_i32::<NetworkEndian>()?;
|
||||
let leechers = cursor.read_i32::<NetworkEndian>()?;
|
||||
let seeders = cursor.read_i32::<NetworkEndian>()?;
|
||||
|
||||
let position = cursor.position() as usize;
|
||||
let inner = cursor.into_inner();
|
||||
|
||||
let peers = inner[position..]
|
||||
.chunks_exact(18)
|
||||
.map(|chunk| {
|
||||
let ip_bytes: [u8; 16] = (&chunk[..16]).try_into().unwrap();
|
||||
let ip_address = Ipv6Addr::from(ip_bytes);
|
||||
let port = (&chunk[16..]).read_u16::<NetworkEndian>().unwrap();
|
||||
|
||||
ResponsePeer {
|
||||
ip_address,
|
||||
port: Port(port),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok((AnnounceResponse {
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
announce_interval: AnnounceInterval(announce_interval),
|
||||
leechers: NumberOfPeers(leechers),
|
||||
seeders: NumberOfPeers(seeders),
|
||||
peers,
|
||||
})
|
||||
.into())
|
||||
}
|
||||
// Scrape
|
||||
2 => {
|
||||
let position = cursor.position() as usize;
|
||||
let inner = cursor.into_inner();
|
||||
|
||||
let stats = inner[position..]
|
||||
.chunks_exact(12)
|
||||
.map(|chunk| {
|
||||
let mut cursor: Cursor<&[u8]> = Cursor::new(&chunk[..]);
|
||||
|
||||
let seeders = cursor.read_i32::<NetworkEndian>().unwrap();
|
||||
let downloads = cursor.read_i32::<NetworkEndian>().unwrap();
|
||||
let leechers = cursor.read_i32::<NetworkEndian>().unwrap();
|
||||
|
||||
TorrentScrapeStatistics {
|
||||
seeders: NumberOfPeers(seeders),
|
||||
completed: NumberOfDownloads(downloads),
|
||||
leechers: NumberOfPeers(leechers),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok((ScrapeResponse {
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
torrent_stats: stats,
|
||||
})
|
||||
.into())
|
||||
}
|
||||
// Error
|
||||
3 => {
|
||||
let position = cursor.position() as usize;
|
||||
let inner = cursor.into_inner();
|
||||
|
||||
Ok((ErrorResponse {
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
message: String::from_utf8_lossy(&inner[position..])
|
||||
.into_owned()
|
||||
.into(),
|
||||
})
|
||||
.into())
|
||||
}
|
||||
_ => Ok((ErrorResponse {
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
message: "Invalid action".into(),
|
||||
})
|
||||
.into()),
|
||||
}
|
||||
}
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ErrorResponse {
|
||||
pub transaction_id: TransactionId,
|
||||
pub message: Cow<'static, str>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use quickcheck_macros::quickcheck;
|
||||
use zerocopy::network_endian::I32;
|
||||
use zerocopy::network_endian::I64;
|
||||
|
||||
use super::*;
|
||||
|
||||
impl quickcheck::Arbitrary for Ipv4AddrBytes {
|
||||
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
|
||||
Self([
|
||||
u8::arbitrary(g),
|
||||
u8::arbitrary(g),
|
||||
u8::arbitrary(g),
|
||||
u8::arbitrary(g),
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
impl quickcheck::Arbitrary for Ipv6AddrBytes {
|
||||
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
|
||||
let mut bytes = [0; 16];
|
||||
|
||||
for byte in bytes.iter_mut() {
|
||||
*byte = u8::arbitrary(g)
|
||||
}
|
||||
|
||||
Self(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
impl quickcheck::Arbitrary for TorrentScrapeStatistics {
|
||||
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
|
||||
Self {
|
||||
seeders: NumberOfPeers(i32::arbitrary(g)),
|
||||
completed: NumberOfDownloads(i32::arbitrary(g)),
|
||||
leechers: NumberOfPeers(i32::arbitrary(g)),
|
||||
seeders: NumberOfPeers(I32::new(i32::arbitrary(g))),
|
||||
completed: NumberOfDownloads(I32::new(i32::arbitrary(g))),
|
||||
leechers: NumberOfPeers(I32::new(i32::arbitrary(g))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -284,8 +235,8 @@ mod tests {
|
|||
impl quickcheck::Arbitrary for ConnectResponse {
|
||||
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
|
||||
Self {
|
||||
connection_id: ConnectionId(i64::arbitrary(g)),
|
||||
transaction_id: TransactionId(i32::arbitrary(g)),
|
||||
connection_id: ConnectionId(I64::new(i64::arbitrary(g))),
|
||||
transaction_id: TransactionId(I32::new(i32::arbitrary(g))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -297,10 +248,12 @@ mod tests {
|
|||
.collect();
|
||||
|
||||
Self {
|
||||
transaction_id: TransactionId(i32::arbitrary(g)),
|
||||
announce_interval: AnnounceInterval(i32::arbitrary(g)),
|
||||
leechers: NumberOfPeers(i32::arbitrary(g)),
|
||||
seeders: NumberOfPeers(i32::arbitrary(g)),
|
||||
fixed: AnnounceResponseFixedData {
|
||||
transaction_id: TransactionId(I32::new(i32::arbitrary(g))),
|
||||
announce_interval: AnnounceInterval(I32::new(i32::arbitrary(g))),
|
||||
leechers: NumberOfPeers(I32::new(i32::arbitrary(g))),
|
||||
seeders: NumberOfPeers(I32::new(i32::arbitrary(g))),
|
||||
},
|
||||
peers,
|
||||
}
|
||||
}
|
||||
|
|
@ -313,7 +266,7 @@ mod tests {
|
|||
.collect();
|
||||
|
||||
Self {
|
||||
transaction_id: TransactionId(i32::arbitrary(g)),
|
||||
transaction_id: TransactionId(I32::new(i32::arbitrary(g))),
|
||||
torrent_stats,
|
||||
}
|
||||
}
|
||||
|
|
@ -328,7 +281,7 @@ mod tests {
|
|||
let success = response == r2;
|
||||
|
||||
if !success {
|
||||
println!("before: {:#?}\nafter: {:#?}", response, r2);
|
||||
::pretty_assertions::assert_eq!(response, r2);
|
||||
}
|
||||
|
||||
success
|
||||
|
|
@ -340,12 +293,16 @@ mod tests {
|
|||
}
|
||||
|
||||
#[quickcheck]
|
||||
fn test_announce_response_ipv4_convert_identity(response: AnnounceResponse<Ipv4Addr>) -> bool {
|
||||
fn test_announce_response_ipv4_convert_identity(
|
||||
response: AnnounceResponse<Ipv4AddrBytes>,
|
||||
) -> bool {
|
||||
same_after_conversion(response.into(), true)
|
||||
}
|
||||
|
||||
#[quickcheck]
|
||||
fn test_announce_response_ipv6_convert_identity(response: AnnounceResponse<Ipv6Addr>) -> bool {
|
||||
fn test_announce_response_ipv6_convert_identity(
|
||||
response: AnnounceResponse<Ipv6AddrBytes>,
|
||||
) -> bool {
|
||||
same_after_conversion(response.into(), false)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue