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
// Copyright 2022 Bryant Luk
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Pings a node.
//!
//! The query and response are described in [BEP 5][bep_0005].
//!
//! [bep_0005]: http://bittorrent.org/beps/bep_0005.html

use core::convert::TryFrom;
use serde_bytes::Bytes;
use serde_derive::{Deserialize, Serialize};

use crate::dht::node::{Id, LocalId};

/// The "ping" query method name.
pub const METHOD_PING: &[u8] = b"ping";

/// The arguments for the ping query message.
#[derive(Debug, Deserialize, Serialize)]
pub struct QueryArgs<'a> {
    /// The querying node's ID
    #[serde(borrow)]
    pub id: &'a Bytes,
}

impl<'a> QueryArgs<'a> {
    /// Constructs a new `QueryArgs` based on the local node ID.
    #[must_use]
    #[inline]
    pub fn new(id: &'a LocalId) -> Self {
        Self {
            id: Bytes::new(&(id.0).0),
        }
    }

    /// Returns the querying node's ID.
    #[must_use]
    #[inline]
    pub fn id(&self) -> Option<Id> {
        Id::try_from(self.id.as_ref()).ok()
    }
}

/// The arguments for the ping query message.
#[derive(Debug, Deserialize, Serialize)]
pub struct RespValues<'a> {
    /// The queried node's ID
    #[serde(borrow)]
    pub id: &'a Bytes,
}

impl<'a> RespValues<'a> {
    /// Constructs a new `RespValue` based on the local node ID.
    #[must_use]
    #[inline]
    pub fn new(id: &'a LocalId) -> Self {
        Self {
            id: Bytes::new(&(id.0).0),
        }
    }

    /// Returns the queried node's ID.
    #[must_use]
    #[inline]
    pub fn id(&self) -> Option<Id> {
        Id::try_from(self.id.as_ref()).ok()
    }
}

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

    use crate::dht::krpc::{ser, Error, Msg, Ty};

    #[test]
    fn test_serde_ping_query() -> Result<(), Error> {
        let ping_query = b"d1:ad2:id20:abcdefghij0123456789e1:q4:ping1:t2:aa1:y1:qe";

        let msg: Msg<'_> = bt_bencode::from_slice(ping_query.as_slice())?;
        assert_eq!(msg.tx_id(), b"aa");
        assert_eq!(msg.ty(), Ty::Query);
        assert_eq!(msg.client_version(), None);
        assert_eq!(msg.method_name().unwrap(), METHOD_PING);
        assert_eq!(
            msg.method_name_str(),
            Some(core::str::from_utf8(METHOD_PING).unwrap())
        );

        let query_args: QueryArgs<'_> = msg.args().unwrap()?;
        assert_eq!(query_args.id(), Some(Id::from(*b"abcdefghij0123456789")));

        let ser_query_msg = ser::QueryMsg {
            t: Bytes::new(b"aa"),
            v: None,
            q: Bytes::new(METHOD_PING),
            a: query_args,
        };
        let ser_msg = bt_bencode::to_vec(&ser_query_msg)?;
        assert_eq!(ser_msg, ping_query);

        Ok(())
    }

    #[test]
    fn test_serde_ping_response() -> Result<(), Error> {
        let ping_resp = b"d1:rd2:id20:mnopqrstuvwxyz123456e1:t2:aa1:y1:re";

        let msg: Msg<'_> = bt_bencode::from_slice(&ping_resp[..])?;
        assert_eq!(msg.tx_id(), b"aa");
        assert_eq!(msg.ty(), Ty::Response);
        assert_eq!(msg.client_version(), None);

        let resp_values: RespValues<'_> = msg.values().unwrap()?;
        assert_eq!(resp_values.id(), Some(Id::from(*b"mnopqrstuvwxyz123456")));

        let ser_resp_msg = ser::RespMsg {
            t: Bytes::new(b"aa"),
            v: None,
            r: &resp_values,
        };
        let ser_msg = bt_bencode::to_vec(&ser_resp_msg)?;
        assert_eq!(ser_msg, ping_resp);

        Ok(())
    }
}