From b20e5912e24c3d3827b514f99b58641d1ef8dd0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Fri, 10 Apr 2020 01:22:13 +0200 Subject: [PATCH] bittorrent_udp: request converters: general cleanup and fixes --- bittorrent_udp/src/converters/requests.rs | 55 ++++++++++++----------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/bittorrent_udp/src/converters/requests.rs b/bittorrent_udp/src/converters/requests.rs index 2ad97a2..d6bd6bb 100644 --- a/bittorrent_udp/src/converters/requests.rs +++ b/bittorrent_udp/src/converters/requests.rs @@ -1,8 +1,7 @@ use byteorder::{ReadBytesExt, WriteBytesExt, NetworkEndian}; use std::convert::TryInto; -use std::io; -use std::io::{Read, Write}; +use std::io::{self, Cursor, Read, Write}; use std::net::Ipv4Addr; use crate::types::*; @@ -10,7 +9,7 @@ use crate::types::*; use super::common::*; -const MAGIC_NUMBER: i64 = 4_497_486_125_440; +const PROTOCOL_IDENTIFIER: i64 = 4_497_486_125_440; #[inline] @@ -20,7 +19,7 @@ pub fn request_to_bytes( ){ match request { Request::Connect(r) => { - bytes.write_i64::(MAGIC_NUMBER).unwrap(); + bytes.write_i64::(PROTOCOL_IDENTIFIER).unwrap(); bytes.write_i32::(0).unwrap(); bytes.write_i32::(r.transaction_id.0).unwrap(); }, @@ -39,7 +38,10 @@ pub fn request_to_bytes( bytes.write_i32::(event_to_i32(r.event)).unwrap(); - bytes.write_all(&r.ip_address.map_or([0; 4], |ip| ip.octets())).unwrap(); + bytes.write_all(&r.ip_address.map_or( + [0; 4], + |ip| ip.octets() + )).unwrap(); bytes.write_u32::(0).unwrap(); // IP bytes.write_u32::(r.key.0).unwrap(); @@ -52,7 +54,7 @@ pub fn request_to_bytes( bytes.write_i32::(2).unwrap(); bytes.write_i32::(r.transaction_id.0).unwrap(); - for info_hash in &r.info_hashes { + for info_hash in r.info_hashes { bytes.write_all(&info_hash.0).unwrap(); } } @@ -67,23 +69,22 @@ pub fn request_from_bytes( bytes: &[u8], max_scrape_torrents: u8, ) -> Result { - let mut bytes = io::Cursor::new(bytes); + let mut cursor = Cursor::new(bytes); - let connection_id = bytes.read_i64::()?; - let action = bytes.read_i32::()?; - let transaction_id = bytes.read_i32::()?; + let connection_id = cursor.read_i64::()?; + let action = cursor.read_i32::()?; + let transaction_id = cursor.read_i32::()?; match action { // Connect 0 => { - if connection_id == MAGIC_NUMBER { + if connection_id == PROTOCOL_IDENTIFIER { Ok(Request::Connect(ConnectRequest { - transaction_id:TransactionId(transaction_id) + transaction_id: TransactionId(transaction_id) })) - } - else { + } else { Ok(Request::Invalid(InvalidRequest { - transaction_id:TransactionId(transaction_id), + transaction_id: TransactionId(transaction_id), message: "Please send protocol identifier in connect request" .to_string() @@ -97,19 +98,19 @@ pub fn request_from_bytes( let mut peer_id = [0; 20]; let mut ip = [0; 4]; - bytes.read_exact(&mut info_hash)?; - bytes.read_exact(&mut peer_id)?; + cursor.read_exact(&mut info_hash)?; + cursor.read_exact(&mut peer_id)?; - let bytes_downloaded = bytes.read_i64::()?; - let bytes_left = bytes.read_i64::()?; - let bytes_uploaded = bytes.read_i64::()?; - let event = bytes.read_i32::()?; + let bytes_downloaded = cursor.read_i64::()?; + let bytes_left = cursor.read_i64::()?; + let bytes_uploaded = cursor.read_i64::()?; + let event = cursor.read_i32::()?; - bytes.read_exact(&mut ip)?; + cursor.read_exact(&mut ip)?; - let key = bytes.read_u32::()?; - let peers_wanted = bytes.read_i32::()?; - let port = bytes.read_u16::()?; + let key = cursor.read_u32::()?; + let peers_wanted = cursor.read_i32::()?; + let port = cursor.read_u16::()?; let opt_ip = if ip == [0; 4] { None @@ -135,8 +136,8 @@ pub fn request_from_bytes( // Scrape 2 => { - let position = bytes.position() as usize; - let inner = bytes.into_inner(); + let position = cursor.position() as usize; + let inner = cursor.into_inner(); let info_hashes = (&inner[position..]).chunks_exact(20) .take(max_scrape_torrents as usize)