vortex-dht 0.1.0

An implementation of Bittorrents distributed hash table built on top of io-uring
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
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use bytes::{BufMut, BytesMut};
use serde_bytes::ByteBuf;
use serde_derive::{Deserialize, Serialize};
use time::OffsetDateTime;

use crate::node::{Node, NodeId, NodeStatus};

use super::{error::KrpcError, Rpc};
// TODO try to avoid allocations for ids
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum Query {
    FindNode {
        id: ByteBuf,
        target: ByteBuf,
    },
    GetPeers {
        id: ByteBuf,
        info_hash: ByteBuf,
    },
    AnnouncePeer {
        id: ByteBuf,
        implied_port: bool,
        info_hash: ByteBuf,
        port: u16,
        token: ByteBuf,
    },
    Ping {
        id: ByteBuf,
    },
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum Answer {
    GetPeers {
        id: ByteBuf,
        token: ByteBuf,
        #[serde(skip_serializing_if = "Option::is_none")]
        values: Option<Vec<ByteBuf>>,
        #[serde(skip_serializing_if = "Option::is_none")]
        nodes: Option<ByteBuf>,
    },
    FindNode {
        id: ByteBuf,
        nodes: ByteBuf,
    },
    // For both ping and announce peer
    QueriedNodeId {
        id: ByteBuf,
    },
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct KrpcPacket {
    pub t: ByteBuf,
    pub y: char,
    // TODO Borrow instead
    #[serde(skip_serializing_if = "Option::is_none")]
    pub q: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub a: Option<Query>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r: Option<Answer>,
    #[serde(deserialize_with = "super::error::deserialize_error")]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(serialize_with = "super::error::serialize_error")]
    #[serde(default)]
    pub e: Option<KrpcError>,
}

#[derive(Debug)]
pub struct Ping {
    pub id: NodeId,
}

impl Rpc for Ping {
    type Response = Pong;

    fn into_packet(self, transaction_id: ByteBuf) -> KrpcPacket {
        KrpcPacket {
            t: transaction_id,
            y: 'q',
            q: Some("ping".to_string()),
            a: Some(Query::Ping {
                id: ByteBuf::from(self.id.as_bytes()),
            }),
            r: None,
            e: None,
        }
    }
}

#[derive(Debug)]
pub struct Pong {
    pub id: NodeId,
}

impl TryFrom<Answer> for Pong {
    type Error = KrpcError;

    fn try_from(answer: Answer) -> Result<Self, Self::Error> {
        match answer {
            Answer::QueriedNodeId { id } => Ok(Pong {
                id: id.as_slice().into(),
            }),
            _ => Err(KrpcError::protocol(
                "Unexpected answer, expected pong".to_owned(),
            )),
        }
    }
}

#[derive(Debug)]
pub struct FindNodes {
    pub id: NodeId,
    pub target: NodeId,
}

impl Rpc for FindNodes {
    type Response = FindNodesResponse;

    fn into_packet(self, transaction_id: ByteBuf) -> KrpcPacket {
        KrpcPacket {
            t: transaction_id,
            y: 'q',
            q: Some("find_node".to_string()),
            a: Some(Query::FindNode {
                id: ByteBuf::from(self.id.as_bytes()),
                target: ByteBuf::from(self.target.as_bytes()),
            }),
            r: None,
            e: None,
        }
    }
}

#[derive(Debug)]
pub struct FindNodesResponse {
    pub id: NodeId,
    pub nodes: Vec<Node>,
}

impl TryFrom<Answer> for FindNodesResponse {
    type Error = KrpcError;

    fn try_from(answer: Answer) -> Result<Self, Self::Error> {
        match answer {
            Answer::FindNode { id, nodes } => {
                let nodes = deserialize_compact_nodes(nodes);
                Ok(FindNodesResponse {
                    id: id.as_slice().into(),
                    nodes,
                })
            }
            _ => Err(KrpcError::protocol(
                "Unexpected answer, expected find nodes response".to_owned(),
            )),
        }
    }
}

#[derive(Debug)]
pub struct GetPeers {
    pub id: NodeId,
    pub info_hash: [u8; 20],
}

impl Rpc for GetPeers {
    type Response = GetPeersResponse;

    fn into_packet(self, transaction_id: ByteBuf) -> KrpcPacket {
        KrpcPacket {
            t: transaction_id,
            y: 'q',
            q: Some("get_peers".to_string()),
            a: Some(Query::GetPeers {
                id: ByteBuf::from(self.id.as_bytes()),
                info_hash: ByteBuf::from(self.info_hash),
            }),
            r: None,
            e: None,
        }
    }
}

pub(crate) fn serialize_compact_peers(peers: &[SocketAddr]) -> Vec<ByteBuf> {
    let mut result = Vec::with_capacity(peers.len());
    for peer in peers {
        let mut compact_peer = ByteBuf::with_capacity(6);
        match peer.ip() {
            IpAddr::V4(ip) => {
                compact_peer.put(ip.octets().as_slice());
                compact_peer.put_u16(peer.port());
                result.push(compact_peer);
            }
            IpAddr::V6(_) => {
                log::warn!("Only Ipv4 addresses can be serialized as compact peers");
            }
        }
    }
    result
}

// Will skip compact peer info that isn't 6 bytes long
pub(crate) fn deserialize_compact_peers(peers: Vec<ByteBuf>) -> Vec<SocketAddr> {
    peers
        .into_iter()
        .filter_map(|bytes| {
            if bytes.len() != 6 {
                log::warn!("Compact peer info wasn't 6 bytes long");
                None
            } else {
                Some(SocketAddr::new(
                    IpAddr::V4(Ipv4Addr::new(bytes[0], bytes[1], bytes[2], bytes[3])),
                    u16::from_be_bytes([bytes[4], bytes[5]]),
                ))
            }
        })
        .collect()
}

#[derive(Debug)]
pub enum GetPeersResponseBody {
    Nodes(Vec<Node>),
    Peers(Vec<SocketAddr>),
}

#[derive(Debug)]
pub struct GetPeersResponse {
    pub id: NodeId,
    pub token: ByteBuf,
    pub body: GetPeersResponseBody,
}

impl TryFrom<Answer> for GetPeersResponse {
    type Error = KrpcError;

    fn try_from(answer: Answer) -> Result<Self, Self::Error> {
        match answer {
            Answer::GetPeers {
                id,
                token,
                values,
                nodes,
            } => {
                if let Some(peers) = values {
                    return Ok(GetPeersResponse {
                        id: id.as_slice().into(),
                        token,
                        body: GetPeersResponseBody::Peers(deserialize_compact_peers(peers)),
                    });
                }

                if let Some(nodes) = nodes {
                    let nodes = deserialize_compact_nodes(nodes);

                    return Ok(GetPeersResponse {
                        id: id.as_slice().into(),
                        token,
                        body: GetPeersResponseBody::Nodes(nodes),
                    });
                }
                Err(KrpcError::protocol(
                    "Response contained neither nodes nor peers".to_string(),
                ))
            }
            Answer::FindNode { id, nodes } => {
                let nodes = deserialize_compact_nodes(nodes);

                Ok(GetPeersResponse {
                    id: id.as_slice().into(),
                    token: ByteBuf::new(),
                    body: GetPeersResponseBody::Nodes(nodes),
                })
            }
            _ => Err(KrpcError::protocol(
                "Unexpected response from get_peers".to_string(),
            )),
        }
    }
}

#[derive(Debug)]
pub struct AnnouncePeer {
    pub id: NodeId,
    pub info_hash: [u8; 20],
    pub implied_port: bool,
    pub port: u16,
    pub token: ByteBuf,
}

impl Rpc for AnnouncePeer {
    type Response = AnnounceResponse;

    fn into_packet(self, transaction_id: ByteBuf) -> KrpcPacket {
        KrpcPacket {
            t: transaction_id,
            y: 'q',
            q: Some("announce_peer".to_string()),
            a: Some(Query::AnnouncePeer {
                id: ByteBuf::from(self.id.as_bytes()),
                implied_port: self.implied_port,
                info_hash: ByteBuf::from(self.info_hash),
                port: self.port,
                token: self.token,
            }),
            r: None,
            e: None,
        }
    }
}

#[derive(Debug)]
pub struct AnnounceResponse {
    pub id: NodeId,
}

impl TryFrom<Answer> for AnnounceResponse {
    type Error = KrpcError;

    fn try_from(answer: Answer) -> Result<Self, Self::Error> {
        match answer {
            Answer::QueriedNodeId { id } => Ok(AnnounceResponse {
                id: id.as_slice().into(),
            }),
            _ => Err(KrpcError::protocol(
                "Unexpected answer, expected announce peer response".to_owned(),
            )),
        }
    }
}

fn deserialize_compact_nodes(bytes: ByteBuf) -> Vec<Node> {
    if (bytes.len() % 26) != 0 {
        log::warn!("Invalid compact node buffer, not divisible by 26");
    }
    bytes
        .chunks_exact(26)
        .map(|chunk| {
            // Seems to be working? Should i reverse this?
            let id = chunk[..20].into();
            let ip: IpAddr = [chunk[20], chunk[21], chunk[22], chunk[23]].into();
            let port = u16::from_be_bytes([chunk[24], chunk[25]]);
            Node {
                id,
                addr: (ip, port).into(),
                last_seen: OffsetDateTime::now_utc(),
                last_status: NodeStatus::Unknown,
            }
        })
        .collect()
}

pub(crate) fn serialize_compact_nodes(nodes: &[Node]) -> ByteBuf {
    let mut result = BytesMut::with_capacity(nodes.len() * (20 + 4 + 2));

    for node in nodes {
        result.put(node.id.as_bytes().as_slice());
        let IpAddr::V4(ip_v4) = node.addr.ip() else {
            log::error!("Only IPv4 addresses are supported for nodes");
            continue;
        };
        result.put(ip_v4.octets().as_slice());
        result.put_u16(node.addr.port());
    }
    ByteBuf::from(result)
}

#[cfg(test)]
mod test {

    use super::*;
    use crate::{
        generate_node_id,
        node::{ID_MAX, ID_ZERO},
        token_store::TokenStore,
    };

    #[test]
    fn roundtrip_ping() {
        let packet = KrpcPacket {
            t: ByteBuf::from(*b"ta"),
            y: 'q',
            q: Some("ping".to_string()),
            a: Some(Query::Ping {
                id: ByteBuf::from(crate::node::ID_MAX.as_bytes()),
            }),
            r: None,
            e: None,
        };
        let encoded = serde_bencoded::to_vec(&packet).unwrap();
        assert_eq!(packet, serde_bencoded::from_bytes(&encoded).unwrap());
    }

    #[test]
    fn serialize_get_peers() {
        // Token store spawns refresh task
        tokio_uring::start(async {
            let token_store = TokenStore::new();
            let ip = Ipv4Addr::new(123, 20, 13, 3);
            let peers = vec![
                "127.0.2.1:6666".parse().unwrap(),
                "127.1.2.1:1337".parse().unwrap(),
            ];
            let packet = KrpcPacket {
                t: ByteBuf::from(*b"ta"),
                y: 'r',
                q: None,
                a: None,
                r: Some(Answer::GetPeers {
                    id: serde_bytes::ByteBuf::from(generate_node_id().as_bytes()),
                    token: serde_bytes::ByteBuf::from(token_store.generate(ip).to_vec()),
                    values: Some(serialize_compact_peers(&peers)),
                    nodes: None,
                }),
                e: None,
            };
            let encoded = serde_bencoded::to_vec(&packet).unwrap();
            assert_eq!(packet, serde_bencoded::from_bytes(&encoded).unwrap());
        });
    }

    #[test]
    fn basic() {
        let encoded: &[u8] = &[
            100, 50, 58, 105, 112, 54, 58, 83, 249, 52, 208, 5, 57, 49, 58, 114, 100, 50, 58, 105,
            100, 50, 48, 58, 50, 245, 78, 105, 115, 81, 255, 74, 236, 41, 205, 186, 171, 242, 251,
            227, 70, 124, 194, 103, 101, 49, 58, 116, 50, 58, 121, 121, 49, 58, 121, 49, 58, 114,
            101,
        ];
        let decoded: KrpcPacket = serde_bencoded::from_bytes(encoded).unwrap();
        assert_eq!(
            decoded,
            KrpcPacket {
                t: ByteBuf::from([121, 121]),
                y: 'r',
                q: None,
                a: None,
                r: Some(Answer::QueriedNodeId {
                    id: ByteBuf::from([
                        50, 245, 78, 105, 115, 81, 255, 74, 236, 41, 205, 186, 171, 242, 251, 227,
                        70, 124, 194, 103
                    ])
                }),
                e: None,
            }
        );
    }

    #[test]
    fn roundtrip_compact_nodes() {
        let nodes = vec![
            Node {
                id: NodeId::new_in_range(&ID_ZERO, &ID_MAX),
                addr: "127.0.2.1:6666".parse().unwrap(),
                last_status: NodeStatus::Good,
                last_seen: OffsetDateTime::now_utc(),
            },
            Node {
                id: NodeId::new_in_range(&ID_ZERO, &ID_MAX),
                addr: "127.1.2.1:1337".parse().unwrap(),
                last_status: NodeStatus::Good,
                last_seen: OffsetDateTime::now_utc(),
            },
        ];

        let compact = serialize_compact_nodes(&nodes);
        let deserialized = deserialize_compact_nodes(compact);

        for (a, b) in deserialized.iter().zip(nodes.iter()) {
            assert_eq!(a.id, b.id);
            assert_eq!(a.addr, b.addr);
        }
    }

    #[test]
    fn roundtrip_compact_peers() {
        let peers = vec![
            "127.0.2.1:6666".parse().unwrap(),
            "127.1.2.1:1337".parse().unwrap(),
        ];

        let compact = serialize_compact_peers(&peers);
        let deserialized = deserialize_compact_peers(compact);

        for (a, b) in deserialized.iter().zip(peers.iter()) {
            assert_eq!(a, b);
        }
    }
}