aboutsummaryrefslogtreecommitdiffstats
path: root/src/wire.rs
blob: 3d80c3b405c32c4786196a228c3b1dc54f58fdf4 (plain)
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
//! On-the-wire binary protocol.
//!
//! Defines message header, message types, and
//! serialization for all protocol messages. Maintains
//! the same field layout and byte order for
//! structural reference.

use crate::error::Error;
use crate::id::{ID_LEN, NodeId};

// ── Protocol constants ──────────────────────────────

/// Protocol magic number: 0x7E55 ("TESS").
pub const MAGIC_NUMBER: u16 = 0x7E55;

/// Protocol version.
pub const TESSERAS_DHT_VERSION: u8 = 0;

/// Size of the message header in bytes.
///
/// magic(2) + ver(1) + type(1) + len(2) + reserved(2)
/// + src(32) + dst(32) = 72
pub const HEADER_SIZE: usize = 8 + ID_LEN * 2;

/// Ed25519 signature appended to authenticated packets.
pub const SIGNATURE_SIZE: usize = crate::crypto::SIGNATURE_SIZE;

// ── Address domains ─────────────────────────────────

pub const DOMAIN_LOOPBACK: u16 = 0;
pub const DOMAIN_INET: u16 = 1;
pub const DOMAIN_INET6: u16 = 2;

// ── Node state on the wire ──────────────────────────

pub const STATE_GLOBAL: u16 = 1;
pub const STATE_NAT: u16 = 2;

// ── Message types ───────────────────────────────────

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum MsgType {
    // Datagram
    Dgram = 0x01,

    // Advertise
    Advertise = 0x02,
    AdvertiseReply = 0x03,

    // NAT detection
    NatEcho = 0x11,
    NatEchoReply = 0x12,
    NatEchoRedirect = 0x13,
    NatEchoRedirectReply = 0x14,

    // DTUN
    DtunPing = 0x21,
    DtunPingReply = 0x22,
    DtunFindNode = 0x23,
    DtunFindNodeReply = 0x24,
    DtunFindValue = 0x25,
    DtunFindValueReply = 0x26,
    DtunRegister = 0x27,
    DtunRequest = 0x28,
    DtunRequestBy = 0x29,
    DtunRequestReply = 0x2A,

    // DHT
    DhtPing = 0x41,
    DhtPingReply = 0x42,
    DhtFindNode = 0x43,
    DhtFindNodeReply = 0x44,
    DhtFindValue = 0x45,
    DhtFindValueReply = 0x46,
    DhtStore = 0x47,

    // Proxy
    ProxyRegister = 0x81,
    ProxyRegisterReply = 0x82,
    ProxyStore = 0x83,
    ProxyGet = 0x84,
    ProxyGetReply = 0x85,
    ProxyDgram = 0x86,
    ProxyDgramForwarded = 0x87,
    ProxyRdp = 0x88,
    ProxyRdpForwarded = 0x89,

    // RDP
    Rdp = 0x90,
}

impl MsgType {
    pub fn from_u8(v: u8) -> Result<Self, Error> {
        match v {
            0x01 => Ok(MsgType::Dgram),
            0x02 => Ok(MsgType::Advertise),
            0x03 => Ok(MsgType::AdvertiseReply),
            0x11 => Ok(MsgType::NatEcho),
            0x12 => Ok(MsgType::NatEchoReply),
            0x13 => Ok(MsgType::NatEchoRedirect),
            0x14 => Ok(MsgType::NatEchoRedirectReply),
            0x21 => Ok(MsgType::DtunPing),
            0x22 => Ok(MsgType::DtunPingReply),
            0x23 => Ok(MsgType::DtunFindNode),
            0x24 => Ok(MsgType::DtunFindNodeReply),
            0x25 => Ok(MsgType::DtunFindValue),
            0x26 => Ok(MsgType::DtunFindValueReply),
            0x27 => Ok(MsgType::DtunRegister),
            0x28 => Ok(MsgType::DtunRequest),
            0x29 => Ok(MsgType::DtunRequestBy),
            0x2A => Ok(MsgType::DtunRequestReply),
            0x41 => Ok(MsgType::DhtPing),
            0x42 => Ok(MsgType::DhtPingReply),
            0x43 => Ok(MsgType::DhtFindNode),
            0x44 => Ok(MsgType::DhtFindNodeReply),
            0x45 => Ok(MsgType::DhtFindValue),
            0x46 => Ok(MsgType::DhtFindValueReply),
            0x47 => Ok(MsgType::DhtStore),
            0x81 => Ok(MsgType::ProxyRegister),
            0x82 => Ok(MsgType::ProxyRegisterReply),
            0x83 => Ok(MsgType::ProxyStore),
            0x84 => Ok(MsgType::ProxyGet),
            0x85 => Ok(MsgType::ProxyGetReply),
            0x86 => Ok(MsgType::ProxyDgram),
            0x87 => Ok(MsgType::ProxyDgramForwarded),
            0x88 => Ok(MsgType::ProxyRdp),
            0x89 => Ok(MsgType::ProxyRdpForwarded),
            0x90 => Ok(MsgType::Rdp),
            _ => Err(Error::UnknownMessageType(v)),
        }
    }
}

// ── Response flags ──────────────────────────────────

pub const DATA_ARE_NODES: u8 = 0xa0;
pub const DATA_ARE_VALUES: u8 = 0xa1;
pub const DATA_ARE_NUL: u8 = 0xa2;
pub const GET_BY_UDP: u8 = 0xb0;
pub const GET_BY_RDP: u8 = 0xb1;
pub const DHT_FLAG_UNIQUE: u8 = 0x01;
pub const DHT_GET_NEXT: u8 = 0xc0;
pub const PROXY_GET_SUCCESS: u8 = 0xd0;
pub const PROXY_GET_FAIL: u8 = 0xd1;
pub const PROXY_GET_NEXT: u8 = 0xd2;

// ── Message header ──────────────────────────────────

/// Parsed message header (48 bytes on the wire).
#[derive(Debug, Clone)]
pub struct MsgHeader {
    pub magic: u16,
    pub ver: u8,
    pub msg_type: MsgType,
    pub len: u16,
    pub src: NodeId,
    pub dst: NodeId,
}

impl MsgHeader {
    /// Parse a header from a byte buffer.
    pub fn parse(buf: &[u8]) -> Result<Self, Error> {
        if buf.len() < HEADER_SIZE {
            return Err(Error::BufferTooSmall);
        }

        let magic = u16::from_be_bytes([buf[0], buf[1]]);
        if magic != MAGIC_NUMBER {
            return Err(Error::BadMagic(magic));
        }

        let ver = buf[2];
        if ver != TESSERAS_DHT_VERSION {
            return Err(Error::UnsupportedVersion(ver));
        }

        let msg_type = MsgType::from_u8(buf[3])?;
        let len = u16::from_be_bytes([buf[4], buf[5]]);

        // buf[6..8] reserved

        let src = NodeId::read_from(&buf[8..8 + ID_LEN]);
        let dst = NodeId::read_from(&buf[8 + ID_LEN..8 + ID_LEN * 2]);

        Ok(Self {
            magic,
            ver,
            msg_type,
            len,
            src,
            dst,
        })
    }

    /// Write header to a byte buffer. Returns bytes written
    /// (always HEADER_SIZE).
    pub fn write(&self, buf: &mut [u8]) -> Result<usize, Error> {
        if buf.len() < HEADER_SIZE {
            return Err(Error::BufferTooSmall);
        }

        buf[0..2].copy_from_slice(&MAGIC_NUMBER.to_be_bytes());
        buf[2] = TESSERAS_DHT_VERSION;
        buf[3] = self.msg_type as u8;
        buf[4..6].copy_from_slice(&self.len.to_be_bytes());
        buf[6] = 0; // reserved
        buf[7] = 0;
        self.src.write_to(&mut buf[8..8 + ID_LEN]);
        self.dst.write_to(&mut buf[8 + ID_LEN..8 + ID_LEN * 2]);

        Ok(HEADER_SIZE)
    }

    /// Create a new header for sending.
    pub fn new(
        msg_type: MsgType,
        total_len: u16,
        src: NodeId,
        dst: NodeId,
    ) -> Self {
        Self {
            magic: MAGIC_NUMBER,
            ver: TESSERAS_DHT_VERSION,
            msg_type,
            len: total_len,
            src,
            dst,
        }
    }
}

/// Append Ed25519 signature to a packet buffer.
///
/// Signs the entire buffer (header + body) using the
/// sender's private key. Appends 64-byte signature.
pub fn sign_packet(buf: &mut Vec<u8>, identity: &crate::crypto::Identity) {
    let sig = identity.sign(buf);
    buf.extend_from_slice(&sig);
}

/// Verify Ed25519 signature on a received packet.
///
/// The last 64 bytes of `buf` are the signature.
/// `sender_pubkey` is the sender's 32-byte Ed25519
/// public key.
///
/// Returns `true` if the signature is valid.
pub fn verify_packet(
    buf: &[u8],
    sender_pubkey: &[u8; crate::crypto::PUBLIC_KEY_SIZE],
) -> bool {
    if buf.len() < HEADER_SIZE + SIGNATURE_SIZE {
        return false;
    }
    let (data, sig) = buf.split_at(buf.len() - SIGNATURE_SIZE);
    crate::crypto::Identity::verify(sender_pubkey, data, sig)
}

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

    fn make_header() -> MsgHeader {
        MsgHeader::new(
            MsgType::DhtPing,
            52,
            NodeId::from_bytes([0xAA; ID_LEN]),
            NodeId::from_bytes([0xBB; ID_LEN]),
        )
    }

    #[test]
    fn header_roundtrip() {
        let hdr = make_header();
        let mut buf = [0u8; HEADER_SIZE];
        hdr.write(&mut buf).unwrap();
        let parsed = MsgHeader::parse(&buf).unwrap();

        assert_eq!(parsed.magic, MAGIC_NUMBER);
        assert_eq!(parsed.ver, TESSERAS_DHT_VERSION);
        assert_eq!(parsed.msg_type, MsgType::DhtPing);
        assert_eq!(parsed.len, 52);
        assert_eq!(parsed.src, hdr.src);
        assert_eq!(parsed.dst, hdr.dst);
    }

    #[test]
    fn reject_bad_magic() {
        let mut buf = [0u8; HEADER_SIZE];
        buf[0] = 0xBA;
        buf[1] = 0xBE; // wrong magic
        let err = MsgHeader::parse(&buf);
        assert!(matches!(err, Err(Error::BadMagic(0xBABE))));
    }

    #[test]
    fn reject_bad_version() {
        let hdr = make_header();
        let mut buf = [0u8; HEADER_SIZE];
        hdr.write(&mut buf).unwrap();
        buf[2] = 99; // bad version
        let err = MsgHeader::parse(&buf);
        assert!(matches!(err, Err(Error::UnsupportedVersion(99))));
    }

    #[test]
    fn reject_unknown_type() {
        let hdr = make_header();
        let mut buf = [0u8; HEADER_SIZE];
        hdr.write(&mut buf).unwrap();
        buf[3] = 0xFF; // unknown type
        let err = MsgHeader::parse(&buf);
        assert!(matches!(err, Err(Error::UnknownMessageType(0xFF))));
    }

    #[test]
    fn reject_truncated() {
        let err = MsgHeader::parse(&[0u8; 10]);
        assert!(matches!(err, Err(Error::BufferTooSmall)));
    }

    #[test]
    fn all_msg_types_roundtrip() {
        let types = [
            MsgType::Dgram,
            MsgType::Advertise,
            MsgType::AdvertiseReply,
            MsgType::NatEcho,
            MsgType::NatEchoReply,
            MsgType::NatEchoRedirect,
            MsgType::NatEchoRedirectReply,
            MsgType::DtunPing,
            MsgType::DtunPingReply,
            MsgType::DtunFindNode,
            MsgType::DtunFindNodeReply,
            MsgType::DtunFindValue,
            MsgType::DtunFindValueReply,
            MsgType::DtunRegister,
            MsgType::DtunRequest,
            MsgType::DtunRequestBy,
            MsgType::DtunRequestReply,
            MsgType::DhtPing,
            MsgType::DhtPingReply,
            MsgType::DhtFindNode,
            MsgType::DhtFindNodeReply,
            MsgType::DhtFindValue,
            MsgType::DhtFindValueReply,
            MsgType::DhtStore,
            MsgType::ProxyRegister,
            MsgType::ProxyRegisterReply,
            MsgType::ProxyStore,
            MsgType::ProxyGet,
            MsgType::ProxyGetReply,
            MsgType::ProxyDgram,
            MsgType::ProxyDgramForwarded,
            MsgType::ProxyRdp,
            MsgType::ProxyRdpForwarded,
            MsgType::Rdp,
        ];

        for &t in &types {
            let val = t as u8;
            let parsed = MsgType::from_u8(val).unwrap();
            assert_eq!(parsed, t, "roundtrip failed for 0x{val:02x}");
        }
    }
}