Saurabh Goyal
Saurabh Goyal

Reputation: 647

TCP stream reads random data even when there is nothing being sent from source

I am developing a torrent client which includes -

I have been able to make connection and send handshake and interest messages but when I read from the connection, I am getting random data in the buffer. When I capture the traffic using Wireshark, I see there is no TCP packets being sent from the peer.

Sample -

Code for peer (Look for listen method)

use std::{
    io::{self, Read, Write},
    net::{IpAddr, SocketAddr, TcpStream},
    thread,
    time::Duration,
};

const HANSHAKE_PSTR_LEN: &[u8] = &[19];
const HANSHAKE_PSTR: &[u8] = "BitTorrent Protocol".as_bytes();
const HANSHAKE_RESTRICTED: &[u8] = &[0; 8];

pub trait Peer {
    async fn handshake(
        &mut self,
        torrent_info_hash: &str,
        client_peer_id: &str,
    ) -> Result<(), io::Error>;
    async fn mark_host_interested(&mut self, interested: bool) -> Result<(), io::Error>;
    async fn mark_peer_choked(&mut self, choked: bool) -> Result<(), io::Error>;
    async fn request(&mut self, index: u32, begin: u32, length: u32) -> Result<(), io::Error>;
    async fn cancel(&mut self, index: u32, begin: u32, length: u32) -> Result<(), io::Error>;
    async fn listen(&mut self) -> Result<(), io::Error>;
}

#[derive(Debug)]
pub struct PeerClientState {
    handshake_confirmed: bool,
    choked: bool,
    interested: bool,
}

#[derive(Debug)]
pub struct PeerConnection {
    conn: TcpStream,
    host: PeerClientState,
    peer: PeerClientState,
}

impl PeerConnection {
    pub async fn new(ip: &str, port: u16) -> Result<PeerConnection, io::Error> {
        let conn = TcpStream::connect_timeout(
            &SocketAddr::new(ip.parse::<IpAddr>().unwrap(), port),
            Duration::from_secs(10),
        )?;
        conn.set_read_timeout(Some(Duration::from_secs(10)));
        Ok(PeerConnection {
            conn,
            host: PeerClientState {
                handshake_confirmed: false,
                choked: true,
                interested: false,
            },
            peer: PeerClientState {
                handshake_confirmed: false,
                choked: false,
                interested: false,
            },
        })
    }

    fn log(&self, msg: &str) {
        println!("{}: {msg}", self.conn.peer_addr().unwrap());
    }
}

impl Peer for PeerConnection {
    async fn mark_host_interested(&mut self, interested: bool) -> Result<(), io::Error> {
        let _ = self
            .conn
            .write(&[0, 0, 0, 1, if interested { 2 } else { 3 }])?;
        self.host.interested = interested;
        self.log("interest acked");
        Ok(())
    }

    async fn mark_peer_choked(&mut self, choked: bool) -> Result<(), io::Error> {
        let _ = self.conn.write(&[0, 0, 0, 1, if choked { 0 } else { 1 }])?;
        self.peer.choked = choked;
        self.log("peer choked");
        Ok(())
    }

    async fn request(&mut self, index: u32, begin: u32, length: u32) -> Result<(), io::Error> {
        let msg = [
            &[0, 0, 1, 3],
            &6_u32.to_be_bytes()[..],
            &index.to_be_bytes()[..],
            &begin.to_be_bytes()[..],
            &length.to_be_bytes()[..],
        ]
        .concat();
        let _ = self.conn.write(msg.as_slice())?;
        self.log(format!("piece ({}, {}, {}) requested", index, begin, length).as_str());
        Ok(())
    }

    async fn cancel(&mut self, index: u32, begin: u32, length: u32) -> Result<(), io::Error> {
        let msg = [
            &[0, 0, 1, 3],
            &8_u32.to_be_bytes()[..],
            &index.to_be_bytes()[..],
            &begin.to_be_bytes()[..],
            &length.to_be_bytes()[..],
        ]
        .concat();
        let _ = self.conn.write(msg.as_slice())?;
        self.log(format!("piece ({}, {}, {}) cancelled", index, begin, length).as_str());
        Ok(())
    }

    async fn handshake(
        &mut self,
        torrent_info_hash: &str,
        client_peer_id: &str,
    ) -> Result<(), io::Error> {
        let handshake_msg = [
            HANSHAKE_PSTR_LEN,
            HANSHAKE_PSTR,
            HANSHAKE_RESTRICTED,
            torrent_info_hash.as_bytes(),
            client_peer_id.as_bytes(),
        ]
        .concat();
        let _ = self.conn.write(handshake_msg.as_slice())?;
        self.log("handshake sent");
        Ok(())
    }

    async fn listen(&mut self) -> Result<(), io::Error> {
        let mut len_buf = [0_u8; 4];
        let max_size = 1 << 16;
        loop {
            self.conn.read_exact(&mut len_buf).unwrap();
            let len = u32::from_be_bytes(len_buf) as usize;
            self.log(format!("Read - {:?} ({:?})", len_buf, len).as_str());
            if len > 0 && len < max_size {
                let mut data_buf: Vec<u8> = Vec::with_capacity(len);
                self.conn.read_exact(data_buf.as_mut_slice()).unwrap();
                println!("B0 - {}", data_buf[0]);
            }
            thread::sleep(Duration::from_millis(1000));
        }
    }
}

Logs in application

[Peer { ip: "49.207.215.123", port: 12457 }, Peer { ip: "198.44.136.213", port: 26103 }, Peer { ip: "193.160.100.185", port: 27235 }, Peer { ip: "181.214.196.152", port: 25550 }, Peer { ip: "172.93.132.52", port: 59406 }, Peer { ip: "138.199.57.38", port: 49573 }, Peer { ip: "124.121.111.252", port: 51413 }, Peer { ip: "116.88.97.233", port: 46272 }, Peer { ip: "103.231.91.59", port: 11470 }, Peer { ip: "103.198.133.62", port: 45145 }, Peer { ip: "103.198.133.61", port: 45145 }, Peer { ip: "95.181.235.15", port: 12127 }, Peer { ip: "84.247.111.56", port: 14945 }, Peer { ip: "45.85.144.21", port: 44488 }, Peer { ip: "14.198.157.75", port: 47546 }]
14.198.157.75:47546: handshake sent
14.198.157.75:47546: interest acked
116.88.97.233:46272: handshake sent
116.88.97.233:46272: interest acked
116.88.97.233:46272: Read - [98, 31, 206, 186] (1646251706) <--- Unexpected, no packet in network capture.
14.198.157.75:47546: Read - [219, 171, 193, 20] (3685466388)
116.88.97.233:46272: Read - [133, 18, 182, 236] (2232596204) <--- Unexpected, no packet in network capture.
14.198.157.75:47546: Read - [165, 6, 145, 187] (2768671163)
116.88.97.233:46272: Read - [114, 207, 202, 27] (1926220315) <--- Unexpected, no packet in network capture.
14.198.157.75:47546: Read - [236, 179, 197, 25] (3971204377)
116.88.97.233:46272: Read - [214, 115, 123, 114] (3597892466) <--- Unexpected, no packet in network capture.
14.198.157.75:47546: Read - [226, 207, 126, 68] (3805249092)
116.88.97.233:46272: Read - [206, 152, 2, 136] (3466068616)
14.198.157.75:47546: Read - [232, 24, 139, 197] (3893922757)
116.88.97.233:46272: Read - [132, 64, 166, 156] (2218829468)
14.198.157.75:47546: Read - [15, 218, 53, 209] (265958865)
116.88.97.233:46272: Read - [87, 113, 165, 184] (1467065784)
14.198.157.75:47546: Read - [89, 248, 100, 240] (1509450992)
116.88.97.233:46272: Read - [74, 156, 37, 68] (1251747140)
14.198.157.75:47546: Read - [41, 147, 130, 62] (697532990)
116.88.97.233:46272: Read - [114, 164, 13, 195] (1923354051)
14.198.157.75:47546: Read - [248, 8, 13, 57] (4161277241)
116.88.97.233:46272: Read - [216, 141, 156, 223] (3633159391)
14.198.157.75:47546: Read - [91, 135, 214, 102] (1535628902)
116.88.97.233:46272: Read - [66, 136, 89, 84] (1116232020)
14.198.157.75:47546: Read - [87, 22, 105, 21] (1461086485)

Wireshark network capture enter image description here

Upvotes: 0

Views: 175

Answers (0)

Related Questions