fx-torrent 0.10.3

FX Torrent is a feature rich Bittorrent protocol implementation written in rust supporting Linux, MacOS and Windows.
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
use crate::peer::protocol::{
    Packet, StateType, UtpPacketCapture, UtpSelectiveAck, UtpStream, UtpStreamContext,
};
use crate::peer::protocol::{MAX_PACKET_SIZE, UTP_HEADER_SIZE};
use crate::peer::{Error, Result};
use async_trait::async_trait;
use derive_more::Display;
use fx_handle::Handle;
use log::{debug, trace, warn};
use rand::{rng, RngExt};
use std::collections::HashMap;
use std::fmt::Debug;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Instant;
use tokio::net::{ToSocketAddrs, UdpSocket};
use tokio::select;
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
use tokio::sync::{Mutex, RwLock};
use tokio_util::sync::CancellationToken;

/// The UTP socket identifier.
pub type UtpHandle = Handle;
/// The packet connection identifier.
pub type ConnectionId = u16;
/// The packet sequence number.
pub type SequenceNumber = u16;
/// The uTP socket extensions.
pub type UtpSocketExtensions = Vec<UtpSocketExtension>;

/// The uTorrent transport protocol socket extension.
#[derive(Debug)]
pub enum UtpSocketExtension {
    SelectiveAck(UtpSelectiveAck),
    Capture(UtpPacketCapture),
    Other(Box<dyn UtpSocketExtensionAddon>),
}

impl UtpSocketExtension {
    /// Handle an incoming packet for the uTP extension.
    pub async fn incoming(&self, packet: &mut Packet, stream: &UtpStreamContext) {
        match self {
            Self::SelectiveAck(extension) => extension.incoming(packet, stream).await,
            Self::Capture(extension) => extension.incoming(packet).await,
            Self::Other(extension) => extension.incoming(packet, stream).await,
        }
    }

    /// Handle an outgoing packet for the uTP extension.
    pub async fn outgoing(&self, packet: &mut Packet, stream: &UtpStreamContext) {
        match self {
            Self::SelectiveAck(_) => {
                // TODO: implement
            }
            Self::Capture(extension) => extension.outgoing(packet).await,
            Self::Other(extension) => extension.outgoing(packet, stream).await,
        }
    }
}

impl From<UtpSelectiveAck> for UtpSocketExtension {
    fn from(extension: UtpSelectiveAck) -> Self {
        Self::SelectiveAck(extension)
    }
}

impl From<UtpPacketCapture> for UtpSocketExtension {
    fn from(extension: UtpPacketCapture) -> Self {
        Self::Capture(extension)
    }
}

impl From<Box<dyn UtpSocketExtensionAddon>> for UtpSocketExtension {
    fn from(extension: Box<dyn UtpSocketExtensionAddon>) -> Self {
        Self::Other(extension)
    }
}

/// The uTorrent transport protocol socket extension.
/// An extension can be used to add additional functionality to the uTP socket packets.
#[async_trait]
pub trait UtpSocketExtensionAddon: Debug + Send + Sync {
    /// Handle an incoming uTP packet for the given stream.
    ///
    /// # Arguments
    ///
    /// * `packet` - The packet to handle.
    /// * `stream` - The stream to handle the packet for.
    async fn incoming(&self, packet: &mut Packet, stream: &UtpStreamContext);

    /// Handle an outgoing uTp packet for the given stream.
    /// This is invoked before it's being serialized and sent to the remote peer.
    ///
    /// # Arguments
    ///
    /// * `packet` - The packet to handle.
    /// * `stream` - The stream to handle the packet for.
    async fn outgoing(&self, packet: &mut Packet, stream: &UtpStreamContext);
}

/// A connection identifier of an utp stream.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub(crate) struct UtpConnId {
    pub recv_id: u16,
    pub send_id: u16,
}

impl UtpConnId {
    pub fn new() -> Self {
        let mut thread_ng = rng();
        let mut connection_id_recv: u16;
        let connection_id_send: u16;

        loop {
            connection_id_recv = thread_ng.random();
            if connection_id_recv < u16::MAX {
                connection_id_send = connection_id_recv.saturating_add(1);
                break;
            }
        }

        Self {
            recv_id: connection_id_recv,
            send_id: connection_id_send,
        }
    }
}

/// The uTorrent transport protocol socket which can receive and send packets to multiple peers simultaneously.
#[derive(Debug, Display)]
#[display("{}", inner)]
pub struct UtpSocket {
    inner: Arc<UtpSocketContext>,
}

impl UtpSocket {
    /// Try to bind a new uTP socket on the `addr` provided.
    ///
    /// The address should either be a [std::net::Ipv4Addr] or [std::net::Ipv6Addr] local machine address, and never an external address.
    /// If the socket port is no longer available, the UTP socket cannot be created and an error will be returned.
    ///
    /// # Arguments
    ///
    /// * `addr` - The socket address to bind the uTP socket to.
    /// * `extensions` - The uTP socket extensions to use.
    pub async fn bind<A: ToSocketAddrs>(addr: A, extensions: UtpSocketExtensions) -> Result<Self> {
        let socket = UdpSocket::bind(addr).await?;
        let addr = socket.local_addr()?; // get the bound socket address in case port was 0
        let (incoming_stream_sender, incoming_stream_receiver) = unbounded_channel();
        let id = UtpSocketId {
            handle: Default::default(),
            addr,
        };
        let cancellation_token = CancellationToken::new();
        let inner = Arc::new(UtpSocketContext {
            id,
            addr,
            socket,
            connections: Default::default(),
            incoming_stream_sender,
            incoming_stream_receiver: Mutex::new(incoming_stream_receiver),
            extensions: Arc::new(extensions),
            cancellation_token,
        });

        // start the main loop of the socket a new thread
        let inner_main_loop = inner.clone();
        spawn!("UtpSocket::run", async move {
            inner_main_loop.run(&inner_main_loop).await
        });

        Ok(Self { inner })
    }

    /// Get the local socket address of the uTP connection.
    /// It returns the socket address on which the uTP socket is bound.
    pub fn addr(&self) -> SocketAddr {
        self.inner.addr
    }

    /// Try to connect to the given utp target address.
    /// This will establish a new stream with the given address that can be used to send and receive data.
    ///
    /// It returns an error if the connection for the utp socket couldn't be established.
    pub async fn connect(&self, addr: SocketAddr) -> Result<UtpStream> {
        let mut connections = self.inner.connections.write().await;
        let connection_id: UtpConnId;

        // generate a unique connection id
        loop {
            let id = UtpConnId::new();
            if !connections.contains_key(&id) {
                connection_id = id;
                break;
            }
        }

        let (message_sender, message_receiver) = unbounded_channel();
        let stream = UtpStream::new_outgoing(
            connection_id,
            addr,
            self.inner.clone(),
            message_receiver,
            self.inner.extensions(),
        )
        .await?;

        // store the connection
        debug!("Utp socket {} connected with {}", self, addr);
        connections.insert(connection_id, message_sender);

        Ok(stream)
    }

    /// Receive the next incoming uTP stream of the socket.
    /// These streams can only be received by one caller at a time.
    ///
    /// It returns the next uTP stream if available, else [None].
    pub async fn recv(&self) -> Option<UtpStream> {
        let mut receiver = self.inner.incoming_stream_receiver.lock().await;
        select! {
            _ = self.inner.cancellation_token.cancelled() => None,
            stream = receiver.recv() => stream,
        }
    }

    #[cfg(test)]
    pub(crate) fn context(&self) -> &Arc<UtpSocketContext> {
        &self.inner
    }
}

impl Drop for UtpSocket {
    fn drop(&mut self) {
        debug!("Utp socket {} is being dropped", self);
        self.inner.cancellation_token.cancel();
    }
}

#[derive(Debug, Display)]
#[display("{}", id)]
pub(crate) struct UtpSocketContext {
    /// The unique id of the socket
    id: UtpSocketId,
    /// The underlying socket address of the utp socket
    addr: SocketAddr,
    /// The underlying UDP socket being used by the uTP socket.
    socket: UdpSocket,
    /// The established connections for the utp socket
    connections: RwLock<HashMap<UtpConnId, UnboundedSender<Packet>>>,
    /// The sender of new incoming utp streams.
    incoming_stream_sender: UnboundedSender<UtpStream>,
    /// The receiver of new incoming utp streams.
    incoming_stream_receiver: Mutex<UnboundedReceiver<UtpStream>>,
    /// The uTP extensions of the socket.
    extensions: Arc<UtpSocketExtensions>,
    /// The termination cancellation token
    cancellation_token: CancellationToken,
}

impl UtpSocketContext {
    /// Run the main loop of the utp socket.
    #[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
    async fn run(&self, context: &Arc<UtpSocketContext>) {
        let mut buffer = [0u8; MAX_PACKET_SIZE];

        loop {
            select! {
                _ = self.cancellation_token.cancelled() => break,
                result = self.socket.recv_from(&mut buffer) => {
                    match result {
                        Ok((0, _)) => break, // socket closed
                        Ok((bytes_read, addr)) => {
                            self.on_packet_bytes_received(&buffer[..bytes_read], addr, context).await;
                        },
                        Err(e) => {
                            warn!("Utp socket {} reader failed to receive packet, {}", self, e);
                            break;
                        }
                    }
                }
            }
        }
        debug!("Utp socket {} main loop ended", self);
    }

    /// Returns the unique id of the socket.
    pub fn id(&self) -> &UtpSocketId {
        &self.id
    }

    /// Try to send the given packet over the uTP socket to the given remote peer address.
    ///
    /// # Arguments
    ///
    /// * `packet` - The uTP packet to send.
    /// * `addr` - The remote uTP peer address.
    pub async fn send(&self, packet: Packet, addr: SocketAddr) -> Result<()> {
        // convert the packet into the payload bytes
        let bytes: Vec<u8> = packet.as_bytes()?;

        // verify the packet size
        if bytes.len() > MAX_PACKET_SIZE {
            return Err(Error::TooLarge(MAX_PACKET_SIZE));
        }

        let start_time = Instant::now();
        match self.socket.send_to(&bytes, addr).await {
            Ok(len) => {
                let elapsed = start_time.elapsed();
                trace!(
                    "Utp socket {} ({}) sent {} bytes in {}.{:03}ms",
                    self,
                    addr,
                    len,
                    elapsed.as_millis(),
                    elapsed.subsec_micros() % 1000
                );
                Ok(())
            }
            Err(e) => Err(Error::Io(e)),
        }
    }

    /// Close the given uTP stream connection.
    /// The key will be used to identify the stream and remove the packet sender of the stream.
    pub async fn close_connection(&self, key: UtpConnId) {
        let mut connections = self.connections.write().await;
        connections.remove(&key);
    }

    /// Process a received packet payload from the socket.
    async fn on_packet_bytes_received(
        &self,
        packet_bytes: &[u8],
        addr: SocketAddr,
        context: &Arc<UtpSocketContext>,
    ) {
        if packet_bytes.len() < UTP_HEADER_SIZE {
            trace!(
                "Utp socket {} incoming packet from {} is too small, dropping it",
                self,
                addr
            );
            return;
        }

        let packet_size = packet_bytes.len();
        match Packet::try_from(packet_bytes) {
            Ok(packet) => {
                trace!(
                    "Utp socket {} ({}) received packet (len {}) {:?}",
                    self,
                    addr,
                    packet_size,
                    packet
                );
                // check if the packet is a new incoming connection
                if packet.state_type == StateType::Syn {
                    self.on_incoming_connection(packet, addr, context).await;
                } else {
                    self.on_received_packet(packet).await;
                }
            }
            Err(e) => warn!(
                "Utp socket {} failed to parse packet (len {}), {}",
                self, packet_size, e
            ),
        }
    }

    /// Process a received packet from a remote peer.
    /// The packet will be sent to the relevant uTP stream if found, else it will be dropped.
    async fn on_received_packet(&self, packet: Packet) {
        let mut key_to_remove: Option<UtpConnId> = None;

        {
            // try to find the relevant connection of the received packet
            let connections = self.connections.read().await;
            let connection_id = packet.connection_id;
            if let Some((key, connection_message_sender)) = connections
                .iter()
                .find(|(key, _)| key.recv_id == connection_id)
            {
                if let Err(e) = connection_message_sender.send(packet) {
                    trace!(
                        "Utp socket {} connection {} has been closed, {}",
                        self,
                        connection_id,
                        e
                    );
                    key_to_remove = Some(key.clone());
                }
            } else {
                debug!(
                    "Utp socket {} received packet with unknown connection id {}",
                    self, packet.connection_id
                );
            }
        }

        if let Some(key) = key_to_remove {
            debug!("Utp socket {} dropping connection {:?}", self, key);
            self.connections.write().await.remove(&key);
        }
    }

    /// Try to handle a new incoming uTP connection.
    async fn on_incoming_connection(
        &self,
        packet: Packet,
        addr: SocketAddr,
        context: &Arc<UtpSocketContext>,
    ) {
        let key = UtpConnId {
            recv_id: packet.connection_id + 1,
            send_id: packet.connection_id,
        };
        let ack_number: u16 = packet.sequence_number;
        let (message_sender, message_receiver) = unbounded_channel();

        match UtpStream::new_incoming(
            key,
            addr,
            context.clone(),
            ack_number,
            message_receiver,
            self.extensions(),
        )
        .await
        {
            Ok(stream) => {
                debug!("Utp socket {} established connection with {}", self, addr);
                {
                    let mut connections = self.connections.write().await;
                    connections.insert(key, message_sender);
                }

                let _ = self.incoming_stream_sender.send(stream);
            }
            Err(e) => debug!(
                "Utp socket {} failed to accept incoming connection from {}, {}",
                self, addr, e
            ),
        }
    }

    /// Get the extension instances of the uTP socket.
    /// These will be applied within an uTP stream for every received- and sent packet.
    fn extensions(&self) -> Arc<Vec<UtpSocketExtension>> {
        self.extensions.clone()
    }
}

/// The unique identifier of an uTP socket.
#[derive(Debug, Display, Copy, Clone)]
#[display("{}:{}", handle, addr.port())]
pub struct UtpSocketId {
    /// The unique socket handle
    handle: UtpHandle,
    /// The socket address on which the utp socket is listening
    addr: SocketAddr,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::peer::protocol::Extension;
    use crate::peer::protocol::UtpStreamState;
    use std::net::Ipv4Addr;
    use std::time::{SystemTime, UNIX_EPOCH};

    #[test]
    fn test_utp_conn_id_new() {
        let key = UtpConnId::new();

        let expected_result = key.recv_id + 1;
        let result = key.send_id;

        assert_eq!(
            expected_result, result,
            "expected the send id to be 1 higher than the receive id"
        );
    }

    #[test]
    fn test_state_type_from_integer() {
        let expected_result = StateType::Data;
        let result = StateType::try_from(0);
        assert_eq!(Ok(expected_result), result);

        let expected_result = StateType::Fin;
        let result = StateType::try_from(1);
        assert_eq!(Ok(expected_result), result);

        let expected_result = StateType::State;
        let result = StateType::try_from(2);
        assert_eq!(Ok(expected_result), result);

        let expected_result = StateType::Reset;
        let result = StateType::try_from(3);
        assert_eq!(Ok(expected_result), result);

        let expected_result = StateType::Syn;
        let result = StateType::try_from(4);
        assert_eq!(Ok(expected_result), result);
    }

    #[test]
    fn test_packet_try_from() {
        init_logger!();
        let timestamp_microseconds = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_micros() as u32;
        let packet = Packet {
            state_type: StateType::Syn,
            extension: Extension::None,
            connection_id: 12345,
            timestamp_microseconds,
            timestamp_difference_microseconds: 0,
            window_size: 0,
            sequence_number: 0,
            acknowledge_number: 0,
            payload: Vec::with_capacity(0),
        };

        let bytes: Vec<u8> = packet
            .as_bytes()
            .expect("expected the packet to have been serialized");
        assert_eq!(
            20,
            bytes.len(),
            "expected the header to have a length of 20 bytes"
        );

        let result = Packet::try_from(bytes.as_slice())
            .expect("expected the packet to have been deserialized");

        assert_eq!(packet, result);
    }

    #[tokio::test]
    async fn test_udp_socket_new() {
        init_logger!();
        let addr = SocketAddr::from((Ipv4Addr::LOCALHOST, 0));

        // bind to an ephemeral port on localhost
        let socket = UtpSocket::bind(addr, vec![])
            .await
            .expect("expected an uTP socket");

        // check the bound port on the socket
        let port = socket.addr().port();
        assert_ne!(
            0, port,
            "expected the actual bound port to have been returned"
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_utp_socket_connect() {
        init_logger!();
        let expected_result = UtpStreamState::Connected;
        let (incoming, outgoing) = create_utp_socket_pair!();

        let target_addr = (Ipv4Addr::LOCALHOST, incoming.addr().port()).into();
        let outgoing_stream = outgoing
            .connect(target_addr)
            .await
            .expect("expected an utp stream");

        assert_timeout!(
            Duration::from_secs(1),
            expected_result == outgoing_stream.state().await,
            "expected the outgoing stream to be connected"
        );

        let incoming_stream = incoming.recv().await.expect("expected an uTP stream");
        assert_timeout!(
            Duration::from_secs(1),
            expected_result == incoming_stream.state().await,
            "expected the incoming stream to be connected"
        );
    }

    #[tokio::test]
    async fn test_utp_socket_close_connection() {
        init_logger!();
        let id = UtpConnId::new();
        let (tx, mut rx) = unbounded_channel();
        let socket = create_utp_socket!();
        let context = socket.context();

        {
            let mut connections = context.connections.write().await;
            connections.insert(id, tx);
        }

        // close the connection based on it's id
        context.close_connection(id).await;
        let result = context.connections.read().await;
        assert_eq!(
            0,
            result.len(),
            "expected the connection to have been removed from the socket"
        );

        // check if the receiver is correctly closed
        let result = rx.recv().await;
        assert_eq!(None, result);
    }
}