vincenzo 0.0.2

A BitTorrent protocol library that powers the Vincenzo client.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! A tracker is a server that manages peers and stats of multiple torrents.
pub mod action;
pub mod announce;
pub mod connect;
pub mod event;

use super::tracker::action::Action;
use std::{
    fmt::Debug, net::{IpAddr, Ipv4Addr, SocketAddr}, time::Duration
};

use crate::error::Error;
use rand::Rng;
use tokio::{
    net::{ToSocketAddrs, UdpSocket}, select, sync::{mpsc, oneshot}, time::timeout
};
use tracing::{debug, error, warn};

use self::event::Event;

#[derive(Debug)]
pub struct Tracker {
    /// UDP Socket of the `tracker_addr`
    /// Peers announcing will send handshakes
    /// to this addr
    pub local_addr: SocketAddr,
    pub peer_addr: SocketAddr,
    pub ctx: TrackerCtx,
    pub rx: mpsc::Receiver<TrackerMsg>,
}

impl Default for Tracker {
    fn default() -> Self {
        let (tx, rx) = mpsc::channel::<TrackerMsg>(300);

        let peer_id = Self::gen_peer_id();

        Self {
            rx,
            local_addr: "0.0.0.0:0".parse().unwrap(),
            peer_addr: "0.0.0.0:0".parse().unwrap(),
            ctx: TrackerCtx {
                tx: tx.into(),
                peer_id,
                tracker_addr: "".to_owned(),
                connection_id: None,
                local_peer_addr: "0.0.0.0:0".parse().unwrap(),
            },
        }
    }
}

#[derive(Debug, Clone)]
pub struct TrackerCtx {
    pub tx: Option<mpsc::Sender<TrackerMsg>>,
    /// Our ID for this connected Tracker
    pub peer_id: [u8; 20],
    /// UDP Socket of the `socket` in Tracker struct
    pub tracker_addr: String,
    /// Our peer socket addr, peers will send handshakes
    /// to this addr.
    pub local_peer_addr: SocketAddr,
    pub connection_id: Option<u64>,
}

impl Default for TrackerCtx {
    fn default() -> Self {
        TrackerCtx {
            local_peer_addr: "0.0.0.0:0".parse().unwrap(),
            peer_id: Tracker::gen_peer_id(),
            tx: None,
            connection_id: None,
            tracker_addr: "".to_owned(),
        }
    }
}

#[derive(Debug)]
pub enum TrackerMsg {
    Announce {
        event: Event,
        info_hash: [u8; 20],
        downloaded: u64,
        uploaded: u64,
        left: u64,
        recipient: Option<oneshot::Sender<Result<announce::Response, Error>>>,
    },
}

impl Tracker {
    const ANNOUNCE_RES_BUF_LEN: usize = 8192;

    pub fn new() -> Self {
        Self::default()
    }

    /// Bind UDP socket and send a connect handshake,
    /// to one of the trackers.
    // todo: get a new tracker if download is stale
    #[tracing::instrument(skip(trackers), name = "tracker::connect")]
    pub async fn connect<A>(trackers: Vec<A>) -> Result<Self, Error>
    where
        A: ToSocketAddrs
            + Debug
            + Send
            + Sync
            + 'static
            + std::fmt::Display
            + Clone,
        A::Iter: Send,
    {
        debug!("...trying to connect to {:?} trackers", trackers.len());

        // Connect to all trackers, return on the first
        // successful handshake.
        for tracker_addr in trackers {
            debug!("trying to connect {tracker_addr:?}");

            let socket = match Self::new_udp_socket(tracker_addr.clone()).await
            {
                Ok(socket) => socket,
                Err(_) => {
                    debug!("could not connect to tracker");
                    continue;
                }
            };
            let (tracker_tx, tracker_rx) = mpsc::channel::<TrackerMsg>(300);
            let mut tracker = Tracker {
                ctx: TrackerCtx {
                    tracker_addr: tracker_addr.to_string(),
                    tx: tracker_tx.into(),
                    peer_id: Tracker::gen_peer_id(),
                    local_peer_addr: "0.0.0.0:0".parse().unwrap(),
                    connection_id: None,
                },
                rx: tracker_rx,
                local_addr: socket.local_addr().unwrap(),
                peer_addr: socket.peer_addr().unwrap(),
            };
            if tracker.connect_exchange(socket).await.is_ok() {
                debug!("announced to tracker {tracker_addr}");
                return Ok(tracker);
            }
        }

        error!("Could not connect to any tracker, all trackers rejected the connection.");
        Err(Error::TrackerNoHosts)
    }

    #[tracing::instrument(skip(self))]
    async fn connect_exchange(
        &mut self,
        socket: UdpSocket,
    ) -> Result<UdpSocket, Error> {
        let req = connect::Request::new();
        let mut buf = [0u8; connect::Response::LENGTH];
        let mut len: usize = 0;

        // will try to connect up to 3 times
        // breaking if succesfull
        for i in 0..=2 {
            debug!("sending connect number {i}...");
            socket.send(&req.serialize()).await?;

            match timeout(Duration::new(5, 0), socket.recv(&mut buf)).await {
                Ok(Ok(lenn)) => {
                    len = lenn;
                    break;
                }
                Err(e) => {
                    debug!("error receiving connect response, {e}");
                }
                _ => {}
            }
        }

        if len == 0 {
            return Err(Error::TrackerResponse);
        }

        let (res, _) = connect::Response::deserialize(&buf)?;

        debug!("received res from tracker {res:#?}");

        if res.transaction_id != req.transaction_id || res.action != req.action
        {
            error!("response is not valid {res:?}");
            return Err(Error::TrackerResponse);
        }

        self.ctx.connection_id.replace(res.connection_id);
        Ok(socket)
    }

    /// Attempts to send an "announce_request" to the tracker
    #[tracing::instrument(skip(self, info_hash))]
    pub async fn announce_exchange(
        &mut self,
        info_hash: [u8; 20],
        listen: Option<SocketAddr>,
    ) -> Result<(announce::Response, Vec<SocketAddr>), Error> {
        let socket = UdpSocket::bind(self.local_addr).await?;
        socket.connect(self.peer_addr).await?;

        let connection_id = match self.ctx.connection_id {
            Some(x) => x,
            None => return Err(Error::TrackerNoConnectionId),
        };

        let local_peer_socket = {
            match listen {
                Some(listen) => SocketAddr::new(
                    IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
                    listen.port(),
                ),
                None => {
                    SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0)
                }
            }
        };

        let req = announce::Request::new(
            connection_id,
            info_hash,
            self.ctx.peer_id,
            // local_peer_socket.ip() as u32,
            0,
            local_peer_socket.port(),
            Event::Started,
        );

        debug!("announce req {req:?}");

        self.ctx.local_peer_addr = local_peer_socket;

        debug!("local_peer_addr {:?}", self.ctx.local_peer_addr);

        debug!("local ip is {}", socket.local_addr()?);

        let mut len = 0_usize;
        let mut res = [0u8; Self::ANNOUNCE_RES_BUF_LEN];

        // will try to connect up to 3 times
        // breaking if succesfull
        for i in 0..=2 {
            debug!("trying to send announce number {i}...");
            socket.send(&req.serialize()).await?;
            match timeout(Duration::new(3, 0), socket.recv(&mut res)).await {
                Ok(Ok(lenn)) => {
                    len = lenn;
                    break;
                }
                Err(e) => {
                    warn!("failed to announce {e:#?}");
                }
                _ => {}
            }
        }

        if len == 0 {
            return Err(Error::TrackerResponse);
        }

        let res = &res[..len];

        // res is the deserialized struct,
        // payload is a byte array of peers,
        // which are in the form of ips and ports
        let (res, payload) = announce::Response::deserialize(res)?;

        if res.transaction_id != req.transaction_id || res.action != req.action
        {
            return Err(Error::TrackerResponse);
        }

        debug!("* announce successful");
        debug!("res from announce {:#?}", res);

        let peers = Self::parse_compact_peer_list(
            payload,
            socket.peer_addr()?.is_ipv6(),
        )?;

        Ok((res, peers))
    }

    /// Connect is the first step in getting the file
    /// Create an UDP Socket for the given tracker address
    #[tracing::instrument(skip(addr))]
    pub async fn new_udp_socket<A: ToSocketAddrs + std::fmt::Debug>(
        addr: A,
    ) -> Result<UdpSocket, Error> {
        let socket = UdpSocket::bind("0.0.0.0:0").await;
        if let Ok(socket) = socket {
            if socket.connect(addr).await.is_ok() {
                return Ok(socket);
            }
            return Err(Error::TrackerSocketConnect);
        }
        Err(Error::TrackerSocketAddr)
    }

    #[tracing::instrument(skip(buf, is_ipv6))]
    fn parse_compact_peer_list(
        buf: &[u8],
        is_ipv6: bool,
    ) -> Result<Vec<SocketAddr>, Error> {
        let mut peer_list = Vec::<SocketAddr>::new();

        // in ipv4 the addresses come in packets of 6 bytes,
        // first 4 for ip and 2 for port
        // in ipv6 its 16 bytes for port and 2 for port
        let stride = if is_ipv6 { 18 } else { 6 };

        let chunks = buf.chunks_exact(stride);
        if !chunks.remainder().is_empty() {
            return Err(Error::TrackerCompactPeerList);
        }

        for hostpost in chunks {
            let (ip, port) = hostpost.split_at(stride - 2);
            let ip = if is_ipv6 {
                let octets: [u8; 16] = ip[0..16]
                    .try_into()
                    .expect("iterator guarantees bounds are OK");
                IpAddr::from(std::net::Ipv6Addr::from(octets))
            } else {
                IpAddr::from(std::net::Ipv4Addr::new(
                    ip[0], ip[1], ip[2], ip[3],
                ))
            };

            let port = u16::from_be_bytes(
                port.try_into().expect("iterator guarantees bounds are OK"),
            );

            peer_list.push((ip, port).into());
        }

        debug!("ips of peers addrs {peer_list:#?}");
        let peers: Vec<SocketAddr> = peer_list.into_iter().collect();

        Ok(peers)
    }
    #[tracing::instrument(skip(self))]
    pub async fn announce_msg(
        &self,
        event: Event,
        info_hash: [u8; 20],
        downloaded: u64,
        uploaded: u64,
        left: u64,
    ) -> Result<announce::Response, Error> {
        debug!("announcing {event:#?} to tracker");
        let socket = UdpSocket::bind(self.local_addr).await?;
        socket.connect(self.peer_addr).await?;

        let req = announce::Request {
            connection_id: self.ctx.connection_id.unwrap_or(0),
            action: Action::Announce.into(),
            transaction_id: rand::thread_rng().gen(),
            info_hash,
            peer_id: self.ctx.peer_id,
            downloaded,
            left,
            uploaded,
            event: event.into(),
            ip_address: 0,
            num_want: u32::MAX,
            port: self.local_addr.port(),
        };

        let mut len = 0_usize;
        let mut res = [0u8; Self::ANNOUNCE_RES_BUF_LEN];

        // will try to connect up to 3 times
        // breaking if succesfull
        for _ in 0..=2 {
            socket.send(&req.serialize()).await?;
            match timeout(Duration::new(3, 0), socket.recv(&mut res)).await {
                Ok(Ok(lenn)) => {
                    len = lenn;
                    break;
                }
                Err(e) => {
                    warn!("failed to announce {e:#?}");
                }
                _ => {}
            }
        }

        let res = &res[..len];

        let (res, _) = announce::Response::deserialize(res)?;

        Ok(res)
    }

    #[tracing::instrument(skip(self))]
    pub async fn run(&mut self) -> Result<(), Error> {
        debug!("running tracker");
        loop {
            select! {
                Some(msg) = self.rx.recv() => {
                    match msg {
                        TrackerMsg::Announce {
                            info_hash,
                            downloaded,
                            uploaded,
                            recipient,
                            event,
                            left,
                        } => {
                            let r = self
                                .announce_msg(event.clone(), info_hash, downloaded, uploaded, left)
                                .await;

                            if let Some(recipient) = recipient {
                                let _ = recipient.send(r);
                            }

                            if event == Event::Stopped {
                                return Ok(());
                            }
                        }
                    }
                }
            }
        }
    }
    /// Peer ids should be prefixed with "vcz".
    pub fn gen_peer_id() -> [u8; 20] {
        let mut peer_id = [0; 20];
        peer_id[..3].copy_from_slice(b"vcz");
        peer_id[3..].copy_from_slice(&rand::random::<[u8; 17]>());
        peer_id
    }
}

#[cfg(test)]
mod tests {
    use super::Tracker;

    #[test]
    fn peer_ids_prefixed_with_vcz() {
        // Poor man's fuzzing.
        let peer_id = Tracker::gen_peer_id();
        for _ in 0..10 {
            assert!(peer_id.starts_with(&[b'v', b'c', b'z']));
        }
    }
}