changed indentation

This commit is contained in:
Arlo Filley 2023-08-27 22:01:23 +01:00
parent 7fab25ea9f
commit 60dcd93377
7 changed files with 1086 additions and 1067 deletions

View File

@ -1,5 +1,5 @@
use std::vec; use std::vec;
use log::error; use log::{error, debug};
/// Represents a message in the BitTorrent protocol. /// Represents a message in the BitTorrent protocol.
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -62,6 +62,12 @@ impl FromBuffer for Message {
message_type = buf[4].into(); message_type = buf[4].into();
let end_of_message = 4 + message_length as usize; let end_of_message = 4 + message_length as usize;
if end_of_message > buf.len() {
error!("index too long");
debug!("{buf:?}");
}
payload = Some(buf[5..end_of_message].to_vec()); payload = Some(buf[5..end_of_message].to_vec());
} }

View File

@ -139,11 +139,9 @@ impl FromBuffer for ConnectionMessage {
let transaction_id = i32::from_be_bytes(transaction_id); let transaction_id = i32::from_be_bytes(transaction_id);
let mut connection_id: [u8; 8] = [0; 8]; let mut connection_id: [u8; 8] = [0; 8];
connection_id[..4].copy_from_slice(&buf[8..16]); connection_id[..8].copy_from_slice(&buf[8..16]);
let connection_id = i64::from_be_bytes(connection_id); let connection_id = i64::from_be_bytes(connection_id);
Self { Self {
connection_id, connection_id,
action, action,
@ -153,24 +151,39 @@ impl FromBuffer for ConnectionMessage {
} }
#[derive(Debug)] #[derive(Debug)]
/// Represents an announcement message in the bittorrent udp tracker protocol. /// Represents an announcement message in the BitTorrent UDP tracker protocol.
pub struct AnnounceMessage { pub struct AnnounceMessage {
/// The connection ID used for this tracker communication session.
connection_id: i64, connection_id: i64,
/// The action code representing the type of message (e.g., connect, announce, scrape).
action: i32, action: i32,
/// A unique identifier for this transaction, allowing matching responses to requests.
transaction_id: i32, transaction_id: i32,
/// The 20-byte SHA-1 hash of the info dictionary in the torrent metainfo.
info_hash: [u8; 20], info_hash: [u8; 20],
/// The unique ID identifying the peer/client sending the announce message.
peer_id: [u8; 20], peer_id: [u8; 20],
/// The total amount of data downloaded by the client in this torrent, in bytes.
downloaded: i64, downloaded: i64,
/// The amount of data left to download for the client in this torrent, in bytes.
left: i64, left: i64,
/// The total amount of data uploaded by the client in this torrent, in bytes.
uploaded: i64, uploaded: i64,
/// An event code indicating the purpose of the announce (e.g., started, completed, stopped).
event: i32, event: i32,
/// The IP address of the client, expressed as a 32-bit unsigned integer.
ip: u32, ip: u32,
/// A unique key generated by the client for the tracker to identify the peer.
key: u32, key: u32,
/// The maximum number of peers that the client wants to receive from the tracker.
num_want: i32, num_want: i32,
/// The port on which the client is listening for incoming peer connections.
port: u16, port: u16,
extensions: u16 /// Additional extension flags or data included in the announce message.
extensions: u16,
} }
impl AnnounceMessage { impl AnnounceMessage {
/// Creates a new announce message. /// Creates a new announce message.
pub fn new(connection_id: i64, infohash: &[u8], peerid: &str, total_length: i64) -> Self { pub fn new(connection_id: i64, infohash: &[u8], peerid: &str, total_length: i64) -> Self {