forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.rs
More file actions
311 lines (265 loc) · 10.4 KB
/
Copy pathrequest.rs
File metadata and controls
311 lines (265 loc) · 10.4 KB
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
// Copied from aquatic_udp_protocol 0.9.0 by Joakim Frostegard (greatest-ape).
// Source: https://crates.io/crates/aquatic_udp_protocol/0.9.0
// Repository: https://github.com/greatest-ape/aquatic
// License: Apache License, Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0)
//
// This in-house crate started from the aquatic 0.9.0 sources that were previously vendored
// under packages/aquatic-udp-protocol.
use std::io::{self, Cursor, Write};
use std::mem::size_of;
use either::Either;
use zerocopy::FromBytes;
use zerocopy::byteorder::network_endian::I32;
use super::announce::AnnounceRequest;
use super::common::*;
use super::connect::{ConnectRequest, PROTOCOL_IDENTIFIER};
pub use super::scrape::ScrapeRequest;
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum Request {
Connect(ConnectRequest),
Announce(AnnounceRequest),
Scrape(ScrapeRequest),
}
impl Request {
pub fn write_bytes(&self, bytes: &mut impl Write) -> Result<(), io::Error> {
match self {
Request::Connect(r) => r.write_bytes(bytes),
Request::Announce(r) => r.write_bytes(bytes),
Request::Scrape(r) => r.write_bytes(bytes),
}
}
pub fn parse_bytes(bytes: &[u8], max_scrape_torrents: u8) -> Result<Self, RequestParseError> {
let action = bytes
.get(8..12)
.map(|bytes| I32::from_bytes(bytes.try_into().unwrap()))
.ok_or_else(|| RequestParseError::unsendable_text("Couldn't parse action"))?;
match action.get() {
0 => {
let mut bytes = Cursor::new(bytes);
let protocol_identifier = read_i64_ne(&mut bytes).map_err(RequestParseError::unsendable_io)?;
let _action = read_i32_ne(&mut bytes).map_err(RequestParseError::unsendable_io)?;
let transaction_id = read_i32_ne(&mut bytes)
.map(TransactionId)
.map_err(RequestParseError::unsendable_io)?;
if protocol_identifier.get() == PROTOCOL_IDENTIFIER {
Ok((ConnectRequest { transaction_id }).into())
} else {
Err(RequestParseError::unsendable_text("Protocol identifier missing"))
}
}
1 => {
let request = AnnounceRequest::read_from_prefix(bytes)
.map_err(|_| RequestParseError::unsendable_text("invalid data"))?
.0;
if request.port.0.get() == 0 {
Err(RequestParseError::sendable_text(
"Port can't be 0",
request.connection_id,
request.transaction_id,
))
} else if !matches!(request.event.0.get(), 0..=3) {
Err(RequestParseError::sendable_text(
"Invalid announce event",
request.connection_id,
request.transaction_id,
))
} else {
Ok(Request::Announce(request))
}
}
2 => {
let mut bytes = Cursor::new(bytes);
let connection_id = read_i64_ne(&mut bytes)
.map(ConnectionId)
.map_err(RequestParseError::unsendable_io)?;
let _action = read_i32_ne(&mut bytes).map_err(RequestParseError::unsendable_io)?;
let transaction_id = read_i32_ne(&mut bytes)
.map(TransactionId)
.map_err(RequestParseError::unsendable_io)?;
let remaining_bytes = {
let position = bytes.position() as usize;
let inner = bytes.into_inner();
&inner[position..]
};
if remaining_bytes.is_empty() {
return Err(RequestParseError::sendable_text(
"Full scrapes are not allowed",
connection_id,
transaction_id,
));
}
let chunks = remaining_bytes.chunks_exact(size_of::<InfoHash>());
if !chunks.remainder().is_empty() {
return Err(RequestParseError::sendable_text(
"Invalid info hash list",
connection_id,
transaction_id,
));
}
let info_hashes = chunks
.map(|chunk| {
let mut bytes = [0u8; 20];
bytes.copy_from_slice(chunk);
InfoHash(bytes)
})
.collect::<Vec<_>>();
let info_hashes = Vec::from(&info_hashes[..(max_scrape_torrents as usize).min(info_hashes.len())]);
Ok((ScrapeRequest {
connection_id,
transaction_id,
info_hashes,
})
.into())
}
_ => Err(RequestParseError::unsendable_text("Invalid action")),
}
}
}
impl From<ConnectRequest> for Request {
fn from(r: ConnectRequest) -> Self {
Self::Connect(r)
}
}
impl From<AnnounceRequest> for Request {
fn from(r: AnnounceRequest) -> Self {
Self::Announce(r)
}
}
impl From<ScrapeRequest> for Request {
fn from(r: ScrapeRequest) -> Self {
Self::Scrape(r)
}
}
#[derive(Debug)]
pub enum RequestParseError {
Sendable {
connection_id: ConnectionId,
transaction_id: TransactionId,
err: &'static str,
},
Unsendable {
err: Either<io::Error, &'static str>,
},
}
impl RequestParseError {
pub fn sendable_text(text: &'static str, connection_id: ConnectionId, transaction_id: TransactionId) -> Self {
Self::Sendable {
connection_id,
transaction_id,
err: text,
}
}
pub fn unsendable_io(err: io::Error) -> Self {
Self::Unsendable { err: Either::Left(err) }
}
pub fn unsendable_text(text: &'static str) -> Self {
Self::Unsendable {
err: Either::Right(text),
}
}
}
#[cfg(test)]
mod tests {
use quickcheck::TestResult;
use quickcheck_macros::quickcheck;
use zerocopy::network_endian::{I32, I64};
use super::*;
use crate::announce::{AnnounceActionPlaceholder, AnnounceEvent};
impl quickcheck::Arbitrary for AnnounceEvent {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
match (bool::arbitrary(g), bool::arbitrary(g)) {
(false, false) => Self::Started,
(true, false) => Self::Started,
(false, true) => Self::Completed,
(true, true) => Self::None,
}
}
}
impl quickcheck::Arbitrary for ConnectRequest {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
Self {
transaction_id: TransactionId(I32::new(i32::arbitrary(g))),
}
}
}
impl quickcheck::Arbitrary for AnnounceRequest {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
let mut peer_id_bytes = [0u8; 20];
for byte in &mut peer_id_bytes {
*byte = u8::arbitrary(g);
}
Self {
connection_id: ConnectionId(I64::new(i64::arbitrary(g))),
action_placeholder: AnnounceActionPlaceholder::default(),
transaction_id: TransactionId(I32::new(i32::arbitrary(g))),
info_hash: InfoHash::arbitrary(g),
peer_id: PeerId(peer_id_bytes),
bytes_downloaded: NumberOfBytes(I64::new(i64::arbitrary(g))),
bytes_uploaded: NumberOfBytes(I64::new(i64::arbitrary(g))),
bytes_left: NumberOfBytes(I64::new(i64::arbitrary(g))),
event: AnnounceEvent::arbitrary(g).into(),
ip_address: Ipv4AddrBytes::arbitrary(g),
key: PeerKey::new(i32::arbitrary(g)),
peers_wanted: NumberOfPeers(I32::new(i32::arbitrary(g))),
port: Port::new(quickcheck::Arbitrary::arbitrary(g)),
}
}
}
impl quickcheck::Arbitrary for ScrapeRequest {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
let info_hashes = (0..u8::arbitrary(g)).map(|_| InfoHash::arbitrary(g)).collect();
Self {
connection_id: ConnectionId(I64::new(i64::arbitrary(g))),
transaction_id: TransactionId(I32::new(i32::arbitrary(g))),
info_hashes,
}
}
}
fn same_after_conversion(request: Request) -> bool {
let mut buf = Vec::new();
request.clone().write_bytes(&mut buf).unwrap();
let r2 = Request::parse_bytes(&buf[..], ::std::u8::MAX).unwrap();
let success = request == r2;
if !success {
::pretty_assertions::assert_eq!(request, r2);
}
success
}
#[quickcheck]
fn test_connect_request_convert_identity(request: ConnectRequest) -> bool {
same_after_conversion(request.into())
}
#[quickcheck]
fn test_announce_request_convert_identity(request: AnnounceRequest) -> bool {
same_after_conversion(request.into())
}
#[quickcheck]
fn test_scrape_request_convert_identity(request: ScrapeRequest) -> TestResult {
if request.info_hashes.is_empty() {
return TestResult::discard();
}
TestResult::from_bool(same_after_conversion(request.into()))
}
#[test]
fn test_various_input_lengths() {
for action in 0i32..4 {
for max_scrape_torrents in 0..3 {
for num_bytes in 0..256 {
let mut request_bytes = ::std::iter::repeat_n(0, num_bytes).collect::<Vec<_>>();
if let Some(action_bytes) = request_bytes.get_mut(8..12) {
action_bytes.copy_from_slice(&action.to_be_bytes())
}
drop(Request::parse_bytes(&request_bytes, max_scrape_torrents));
}
}
}
}
#[test]
fn test_scrape_request_with_no_info_hashes() {
let mut request_bytes = Vec::new();
request_bytes.extend(0i64.to_be_bytes());
request_bytes.extend(2i32.to_be_bytes());
request_bytes.extend(0i32.to_be_bytes());
Request::parse_bytes(&request_bytes, 1).unwrap_err();
}
}