forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathannounce.rs
115 lines (100 loc) · 3.01 KB
/
announce.rs
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
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use serde::{Deserialize, Serialize};
use torrust_tracker::core::peer::Peer;
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Announce {
pub complete: u32,
pub incomplete: u32,
pub interval: u32,
#[serde(rename = "min interval")]
pub min_interval: u32,
pub peers: Vec<DictionaryPeer>, // Peers using IPV4 and IPV6
}
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct DictionaryPeer {
pub ip: String,
#[serde(rename = "peer id")]
#[serde(with = "serde_bytes")]
pub peer_id: Vec<u8>,
pub port: u16,
}
impl From<Peer> for DictionaryPeer {
fn from(peer: Peer) -> Self {
DictionaryPeer {
peer_id: peer.peer_id.to_bytes().to_vec(),
ip: peer.peer_addr.ip().to_string(),
port: peer.peer_addr.port(),
}
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct DeserializedCompact {
pub complete: u32,
pub incomplete: u32,
pub interval: u32,
#[serde(rename = "min interval")]
pub min_interval: u32,
#[serde(with = "serde_bytes")]
pub peers: Vec<u8>,
}
impl DeserializedCompact {
pub fn from_bytes(bytes: &[u8]) -> Result<Self, serde_bencode::Error> {
serde_bencode::from_bytes::<DeserializedCompact>(bytes)
}
}
#[derive(Debug, PartialEq)]
pub struct Compact {
// code-review: there could be a way to deserialize this struct directly
// by using serde instead of doing it manually. Or at least using a custom deserializer.
pub complete: u32,
pub incomplete: u32,
pub interval: u32,
pub min_interval: u32,
pub peers: CompactPeerList,
}
#[derive(Debug, PartialEq)]
pub struct CompactPeerList {
peers: Vec<CompactPeer>,
}
impl CompactPeerList {
pub fn new(peers: Vec<CompactPeer>) -> Self {
Self { peers }
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct CompactPeer {
ip: Ipv4Addr,
port: u16,
}
impl CompactPeer {
pub fn new(socket_addr: &SocketAddr) -> Self {
match socket_addr.ip() {
IpAddr::V4(ip) => Self {
ip,
port: socket_addr.port(),
},
IpAddr::V6(_ip) => panic!("IPV6 is not supported for compact peer"),
}
}
pub fn new_from_bytes(bytes: &[u8]) -> Self {
Self {
ip: Ipv4Addr::new(bytes[0], bytes[1], bytes[2], bytes[3]),
port: u16::from_be_bytes([bytes[4], bytes[5]]),
}
}
}
impl From<DeserializedCompact> for Compact {
fn from(compact_announce: DeserializedCompact) -> Self {
let mut peers = vec![];
for peer_bytes in compact_announce.peers.chunks_exact(6) {
peers.push(CompactPeer::new_from_bytes(peer_bytes));
}
Self {
complete: compact_announce.complete,
incomplete: compact_announce.incomplete,
interval: compact_announce.interval,
min_interval: compact_announce.min_interval,
peers: CompactPeerList::new(peers),
}
}
}