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

I am developing a torrent client which includes –

  • Finding peers from tracker
  • Making a TCP connection with them
  • Sending a handshake message for the torrent data
  • Sending an interest message for the torrent data
  • Listening to messages from peers for data as well as control messages.

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 –

  • Peer 116.88.97.233:46272
  • Capture shows no packets being sent from peer to local after the interest message (len=5) sent from host to peer.
  • Logs show continuous reads from peer with first 4 bytes being random data. This is
    the unexpected part.

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật