-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
200 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use crate::utils::{parse_big_endian, convert_u8_to_bits}; | ||
|
||
pub enum Message { | ||
Choke, | ||
UnChoke, | ||
Interested, | ||
NotInterested, | ||
Have(u32), | ||
Bitfield(Vec<bool>), | ||
Request(u32, u32, u32), | ||
Piece(u32, u32, Vec<u8>), | ||
Cancel(u32), | ||
} | ||
|
||
impl Message { | ||
pub fn from_bytes(data : Vec<u8>) -> Message { | ||
let (msg_id, msg_payload) = data.split_at(1); | ||
|
||
match msg_id { | ||
[0] => Message::Choke, | ||
[1] => Message::UnChoke, | ||
[2] => Message::Interested, | ||
[3] => Message::NotInterested, | ||
[4] => Message::Have(parse_big_endian(&msg_payload[0..4])), | ||
[5] => { | ||
Message::Bitfield(msg_payload.into_iter() | ||
.map(|data_byte| convert_u8_to_bits(data_byte)) | ||
.flatten().collect::<Vec<bool>>()) | ||
}, | ||
[6] => Message::Request( | ||
parse_big_endian(&msg_payload[0..4]), | ||
parse_big_endian(&msg_payload[4..8]), | ||
parse_big_endian(&msg_payload[8..12]), | ||
), | ||
[7] => Message::Piece( | ||
parse_big_endian(&msg_payload[0..4]), | ||
parse_big_endian(&msg_payload[4..8]), | ||
msg_payload[8..].to_owned() | ||
), | ||
[8] => Message::Cancel(parse_big_endian(&msg_payload[0..4])), | ||
_ => panic!("Unsupported msg_type {:?} with data {:?}", msg_id, data) | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod message_parser; | ||
pub mod peer_connection; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use std::net::{IpAddr, Ipv4Addr}; | ||
use std::net::{TcpStream, SocketAddr}; | ||
use std::fmt::Formatter; | ||
use std::io::{Read, Write, Error, ErrorKind}; | ||
use std::time::Duration; | ||
use crate::torrent_meta::TorrentMetadata; | ||
use crate::utils::{PEER_ID, PROTOCOL, parse_big_endian}; | ||
use crate::network::message_parser::Message; | ||
use crate::peer::Peer; | ||
|
||
pub struct PeerConnection { | ||
peer: Peer, | ||
stream: TcpStream, | ||
torrent: TorrentMetadata | ||
} | ||
|
||
impl PeerConnection { | ||
|
||
pub fn new(peer: Peer, torrent_meta:TorrentMetadata) -> Result<PeerConnection, Error> { | ||
println!("Connecting to {}...", &peer); | ||
let addr = SocketAddr::new(peer.ip, peer.port); | ||
match TcpStream::connect_timeout(&addr, Duration::new(10,0)) { | ||
Ok(stream_obj) => { | ||
println!("Connected successfully to {}", &peer); | ||
|
||
Ok(PeerConnection { | ||
peer: peer, | ||
stream: stream_obj, | ||
torrent: torrent_meta | ||
}) | ||
} | ||
Err(e) => panic!("Failed to create a PeerConnection : {}", e) | ||
} | ||
} | ||
|
||
pub fn handshake(&mut self) { | ||
|
||
let mut message = vec![]; | ||
message.push(PROTOCOL.len() as u8); | ||
message.extend(PROTOCOL.bytes()); | ||
message.extend(vec![0;8].into_iter()); | ||
message.extend(self.torrent.info_hash.iter().cloned()); | ||
message.extend(PEER_ID.bytes()); | ||
self.stream.write_all(&message).unwrap(); | ||
|
||
let pstrlen = self.read(1).unwrap(); | ||
let _pstr = self.read(pstrlen[0] as u32).unwrap(); | ||
let _reserved = self.read(8).unwrap(); | ||
let _info_hash = self.read(20).unwrap(); | ||
let _peer_id = self.read(20).unwrap(); | ||
println!("Received handshake"); | ||
} | ||
|
||
pub fn fetch_data(&mut self) { | ||
|
||
match self.read(4) { | ||
Ok(length) => { | ||
let payload = self.read(parse_big_endian(&length.as_slice())); | ||
println!("Length {:?} Payload: {:?}", length, payload); | ||
} | ||
Err(_) => println!("No data received for {}", &self.peer) | ||
} | ||
} | ||
|
||
fn read(&mut self, bytes_to_read: u32) -> Result<Vec<u8>, Error> { | ||
let mut buf = vec![]; | ||
let stream_ref = &mut self.stream; | ||
let mut take = stream_ref.take(bytes_to_read as u64); | ||
let bytes_read = take.read_to_end(&mut buf); | ||
match bytes_read { | ||
Ok(n) => { | ||
if (n as u32) == bytes_to_read { | ||
Ok(buf) | ||
} else { | ||
Err(Error::new(ErrorKind::Other, "No data received")) | ||
} | ||
} | ||
Err(e) => { | ||
Err(e) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod piece; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
use crate::peer::Peer; | ||
use crate::torrent_meta::TorrentMetadata; | ||
use crate::tracker::TrackerResponse; | ||
#[derive(Debug)] | ||
pub struct PeerState { | ||
pub peer_id: u8 | ||
pub peer: Peer | ||
pub mut have: Vec<bool>, | ||
pub mut choked: bool, | ||
pub mut interested: bool, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct TorrentState { | ||
torrent_meta: TorrentMetadata, | ||
tracker_info: TrackerResponse | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.