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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#![warn(missing_docs)]

//! Protocol datagram "messages" and related types
//!
//! The dpmaster protocol consists of a few messages that are passed between game servers and the master server to register a game server:
//! 1. [`heartbeat`](HeartbeatMessage)
//! 2. [`getinfo`](GetInfoMessage)
//! 3. [`infoResponse`](InfoResponseMessage)
//!
//! Then there are message that are passed between game clients and the master server to query game servers:
//! 1. [`getservers`](GetServersMessage)
//! 2. [`getserversResponse`](GetServersResponseMessage)
//!
//! To support [IPv6](https://en.wikipedia.org/wiki/IPv6) there are extended versions of the previous messages:
//! 1. [`getserversExt`](GetServersExtMessage)
//! 2. [`getserversExtResponse`](GetServersExtResponseMessage)

use crate::error::{EmptyError, InvalidByteError, InvalidChallengeError};
use crate::{ProtocolError, Result};

use memchr::memchr2;

fn is_ascii_printable(chr: u8) -> bool {
    chr >= 33 && chr <= 126
}

/// "Password" to authenticate messages
///
/// Contained in a [`getinfo` message](GetInfoMessage) and [`infoResponse` message](InfoResponseMessage).
///
/// The dpmaster protocol uses [UDP](https://en.wikipedia.org/wiki/User_Datagram_Protocol) which is spoofable so,
/// to authenticate datagrams and prevent denial-of-service in the ([`heartbeat`](HeartbeatMessage) →) [`getinfo`](GetInfoMessage) → [`infoResponse`](InfoResponseMessage) chain,
/// a "password" is used that should only be known to the game server and the master server.
#[derive(Debug, PartialEq, Eq)]
pub struct Challenge(Vec<u8>);

impl Challenge {
    /// Creates a new `Challenge` from a container of bytes.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use dpmaster_proto::messages::Challenge;
    /// let challenge = Challenge::new(*b"A_ch4Lleng3")?;
    /// # Ok::<(), dpmaster_proto::error::InvalidChallengeError>(())
    /// ```
    ///
    /// # Errors
    ///
    /// Will return an [EmptyError](crate::error::EmptyError) if the supplied bytes are empty.
    /// ```rust
    /// # use dpmaster_proto::{error::InvalidChallengeError, messages::Challenge};
    /// #
    /// assert!(matches!(Challenge::new(*b"").unwrap_err(), InvalidChallengeError::Empty(..)));
    /// ```
    ///
    /// Will return [InvalidByteError](crate::error::InvalidByteError)
    /// if a supplied byte is not [ASCII](https://en.wikipedia.org/wiki/ASCII) printable (code 33 to 126)
    /// or is one of the disallowed characters `\`, `/`, `;`, `"` or `%`.
    /// ```rust
    /// # use dpmaster_proto::{error::InvalidChallengeError, messages::Challenge};
    /// #
    /// assert!(matches!(Challenge::new(*b"\xFF").unwrap_err(), InvalidChallengeError::InvalidByte(..)));
    /// assert!(matches!(Challenge::new(*b"uhoh;").unwrap_err(), InvalidChallengeError::InvalidByte(..)));
    /// ```
    pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<Self, InvalidChallengeError> {
        let bytes = t.into();

        if bytes.is_empty() {
            return Err(EmptyError(()))?;
        }

        for (offset, byte) in bytes.iter().copied().enumerate() {
            if !is_ascii_printable(byte) || [b'\\', b'/', b';', b'"', b'%'].contains(&byte) {
                return Err(InvalidByteError(offset, bytes))?;
            }
        }

        Ok(Self(bytes))
    }
}

impl<I: std::slice::SliceIndex<[u8]>> std::ops::Index<I> for Challenge {
    type Output = I::Output;

    fn index(&self, index: I) -> &Self::Output {
        std::ops::Index::index(&self.0, index)
    }
}

/// `getinfo` message
///
/// Sent from the master server to a game server in response to a [`heartbeat`](HeartbeatMessage) message from a game server.\
/// Responded to with a [`infoResponse` message](InfoResponseMessage) from the game server.
///
/// Contains a [`Challenge`](Challenge).
#[derive(Debug, PartialEq, Eq)]
pub struct GetInfoMessage {
    challenge: Challenge,
}

impl GetInfoMessage {
    /// Creates a new `GetInfoMessage` for the given `challenge`.
    pub fn new(challenge: Challenge) -> Self {
        Self { challenge }
    }

    /// Returns the `Challenge` contained in this message.
    pub fn challenge(&self) -> &Challenge {
        &self.challenge
    }
}

/// Maximum number of clients on a game server
///
/// Contained in the [`Info`](Info) of an [`infoResponse` message](InfoResponseMessage).
pub type MaxClientsNumber = std::num::NonZeroU32;

/// Current number of clients on a game server
///
/// Contained in the [`Info`](Info) of an [`infoResponse` message](InfoResponseMessage).
pub type ClientsNumber = u32;

/// Key in a [`Info`](Info) key-value pair
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct InfoKey(Vec<u8>);

impl InfoKey {
    /// Creates a new `InfoKey` from a container of bytes.
    pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<Self> {
        let bytes = t.into();

        Ok(Self(bytes))
    }
}

impl<I: std::slice::SliceIndex<[u8]>> std::ops::Index<I> for InfoKey {
    type Output = I::Output;

    fn index(&self, index: I) -> &Self::Output {
        std::ops::Index::index(&self.0, index)
    }
}

/// Value in a [`Info`](Info) key-value pair
#[derive(Debug, PartialEq, Eq)]
pub struct InfoValue(Vec<u8>);

impl InfoValue {
    /// Creates a new `InfoValue` from a container of bytes.
    pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<Self> {
        let bytes = t.into();

        Ok(Self(bytes))
    }
}

impl<I: std::slice::SliceIndex<[u8]>> std::ops::Index<I> for InfoValue {
    type Output = I::Output;

    fn index(&self, index: I) -> &Self::Output {
        std::ops::Index::index(&self.0, index)
    }
}

/// Map of [`InfoKey`](InfoKey)-[`InfoValue`](InfoValue) pairs
///
/// Contained in an [`infoResponse` message](InfoResponseMessage).
// TODO required and optional keys
#[derive(Debug, PartialEq, Eq)]
pub struct Info(indexmap::IndexMap<InfoKey, InfoValue>);

impl Info {
    // FIXME
    pub fn new() -> Self {
        Self(indexmap::IndexMap::new())
    }

    pub fn insert(&mut self, key: InfoKey, value: InfoValue) {
        self.0.insert(key, value);
    }

    pub fn iter(&self) -> indexmap::map::Iter<'_, InfoKey, InfoValue> {
        self.0.iter()
    }

    pub fn challenge(&self) -> &Challenge {
        todo!();
    }

    pub fn sv_maxclients(&self) -> MaxClientsNumber {
        todo!();
    }

    pub fn protocol(&self) -> ProtocolNumber {
        todo!();
    }

    pub fn clients(&self) -> ClientsNumber {
        todo!();
    }

    pub fn gamename(&self) -> Option<&GameName> {
        todo!();
    }

    pub fn gametype(&self) -> Option<&GameType> {
        todo!();
    }
}

/// `infoResponse` message
///
/// Sent concludingly from a game server to the master server in response to a [`getinfo` message](GetInfoMessage) from the master server.
///
/// Contains [`Info`](Info) metadata.
#[derive(Debug, PartialEq, Eq)]
pub struct InfoResponseMessage {
    info: Info,
}

impl InfoResponseMessage {
    /// Creates a new `InfoResponseMessage` for the given `info`.
    pub fn new(info: Info) -> Self {
        Self { info }
    }

    /// Returns the `Info` contained in this message.
    pub fn info(&self) -> &Info {
        &self.info
    }
}

/// Protocol name
///
/// Contained in a [`heartbeat` message](HeartbeatMessage).
// TODO vs ProtocolNumber, GameName
#[derive(Debug, PartialEq, Eq)]
pub struct ProtocolName(Vec<u8>);

impl ProtocolName {
    /// Creates a new `ProtocolName` from a container of bytes.
    pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<Self> {
        let bytes = t.into();
        Ok(Self(bytes))
    }
}

impl<I: std::slice::SliceIndex<[u8]>> std::ops::Index<I> for ProtocolName {
    type Output = I::Output;

    fn index(&self, index: I) -> &Self::Output {
        std::ops::Index::index(&self.0, index)
    }
}

impl std::default::Default for ProtocolName {
    fn default() -> Self {
        Self::new(b"DarkPlaces".to_vec()).expect("known value to be valid")
    }
}

/// `heartbeat` message
///
/// Sent initially from game servers to the master server.\
/// Responded to with a [`getinfo` message](GetInfoMessage) from the master server.
///
/// Contains a [`ProtocolName`](ProtocolName).
#[derive(Debug, PartialEq, Eq)]
pub struct HeartbeatMessage {
    protocol_name: ProtocolName,
}

impl HeartbeatMessage {
    /// Creates a new `HeartbeatMessage` for the given `protocol_name`.
    pub fn new(protocol_name: ProtocolName) -> Self {
        Self { protocol_name }
    }

    /// Returns the `ProtocolName` contained in this message.
    pub fn protocol_name(&self) -> &ProtocolName {
        &self.protocol_name
    }
}

/// Protocol number
///
/// Contained in a [`getservers` message](GetServersMessage), [`getserversExt`](GetServersExtMessage)\
/// and in the [`Info`](Info) of an [`infoResponse` message](InfoResponseMessage).
// TODO vs ProtocolName, GameType
pub type ProtocolNumber = u32;

/// Game name
///
/// Contained in a [`getservers` message](GetServersMessage), [`getserversExt`](GetServersExtMessage)\
/// and in the [`Info`](Info) of an [`infoResponse` message](InfoResponseMessage).
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct GameName(Vec<u8>);

impl GameName {
    /// Creates a new `GameName` from a container of bytes.
    ///
    /// Game names can contain neither null bytes nor whitespace.
    ///
    /// # Examples
    /// ```
    /// use dpmaster_proto::GameName;
    /// let game_name = GameName::new(b"Nexuiz".to_vec());
    /// assert!(game_name.is_ok());
    /// ```
    ///
    /// # Errors
    /// This function will return an error if the supplied bytes contain a
    /// null/`0` byte or whitespace/`' '`.
    /// The [`ProtocolError::InvalidGameName`] error will include the invalid byte
    /// as well as the first offset it occurred at.
    /// ```
    /// use dpmaster_proto::{GameName, ProtocolError};
    /// let game_name = GameName::new(b"invalid example".to_vec());
    /// assert_eq!(game_name, Err(ProtocolError::InvalidGameName {byte: b' ', offset: 7}));
    /// ```
    // FIXME: Comparing private fields in a public doctest feels wrong. Maybe compare debug output instead?
    pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<Self> {
        let bytes = t.into();
        match memchr2(b'\0', b' ', &bytes) {
            Some(i) => Err(ProtocolError::InvalidGameName {
                offset: i,
                byte: bytes[i],
            }),
            None => Ok(Self(bytes)),
        }
    }
}

impl<I: std::slice::SliceIndex<[u8]>> std::ops::Index<I> for GameName {
    type Output = I::Output;

    fn index(&self, index: I) -> &Self::Output {
        std::ops::Index::index(&self.0, index)
    }
}

impl std::str::FromStr for GameName {
    type Err = ProtocolError;

    fn from_str(s: &str) -> Result<Self> {
        Ok(Self::new(s.as_bytes().to_vec())?)
    }
}

/// Game type
///
/// Contained in the [`FilterOptions`](FilterOptions) of a [`getservers` message](GetServersMessage),
/// [`FilterExtOptions`](FilterExtOptions) of an [`getserversExt` message](GetServersExtMessage)\
/// and [`Info`](Info) of an [`infoResponse` message](InfoResponseMessage).
// TODO vs GameName, ProtocolNumber
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct GameType(Vec<u8>);

impl GameType {
    /// Creates a new `GameType` from a container of bytes.
    pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<Self> {
        let bytes = t.into();
        Ok(Self(bytes))
    }
}

impl<I: std::slice::SliceIndex<[u8]>> std::ops::Index<I> for GameType {
    type Output = I::Output;

    fn index(&self, index: I) -> &Self::Output {
        std::ops::Index::index(&self.0, index)
    }
}

impl std::str::FromStr for GameType {
    type Err = ProtocolError;

    fn from_str(s: &str) -> Result<Self> {
        Ok(Self::new(s.as_bytes().to_vec())?)
    }
}

/// Filter options for a [`getservers` message](GetServersMessage)
///
/// Contains a [`GameType`](GameType) and "empty" / "full" options.
///
/// IPv4-only variant of [`FilterExtOptions`](FilterExtOptions).
#[derive(Debug, PartialEq, Eq)]
pub struct FilterOptions {
    /// `gametype=X` filter option
    gametype: Option<GameType>,
    /// empty servers option
    empty: bool,
    /// full servers option
    full: bool,
}

impl FilterOptions {
    /// Creates a new `FilterOptions` for the given `gametype`, `empty` / `full` options.
    pub fn new(gametype: Option<GameType>, empty: bool, full: bool) -> Self {
        Self {
            gametype,
            empty,
            full,
        }
    }

    /// Returns the `GameType` option contained in this filter.
    pub fn gametype(&self) -> Option<&GameType> {
        self.gametype.as_ref()
    }

    /// Returns the "empty" option contained in this filter.
    pub fn empty(&self) -> bool {
        self.empty
    }

    /// Returns the "full" option contained in this filter.
    pub fn full(&self) -> bool {
        self.full
    }
}

/// `getservers` message
///
/// Sent initially from a game client to the master server.\
/// Responded to with a [`getserversResponse` message](GetServersResponseMessage) from the master server.
///
/// Contains a [`GameName`](GameName), [`ProtocolNumber`](ProtocolNumber) and [`FilterOptions`](FilterOptions).
///
/// IPv4-only variant of the [`getserversExt` message](GetServersExtMessage).
#[derive(Debug, PartialEq, Eq)]
pub struct GetServersMessage {
    game_name: Option<GameName>,
    protocol_number: ProtocolNumber,
    filter_options: FilterOptions,
}

impl GetServersMessage {
    /// Creates a new `GetServersMessage` for the given `game_name`, `protocol_number` and `filter_options`.
    pub fn new(
        game_name: Option<GameName>,
        protocol_number: ProtocolNumber,
        filter_options: FilterOptions,
    ) -> Self {
        Self {
            game_name,
            protocol_number,
            filter_options,
        }
    }

    /// Returns the `GameName` contained in this message.
    pub fn game_name(&self) -> Option<&GameName> {
        self.game_name.as_ref()
    }

    /// Returns the `ProtocolNumber` contained in this message.
    pub fn protocol_number(&self) -> ProtocolNumber {
        self.protocol_number
    }

    /// Returns the `FilterOptions` contained in this message.
    pub fn filter_options(&self) -> &FilterOptions {
        &self.filter_options
    }
}

/// `getserversResponse` message
///
/// Sent concludingly from the master server to a game client in response to a [`getservers` message](GetServersMessage) from the game client.
///
/// Contains a list of [`SocketAddrV4`](std::net::SocketAddrV4) and End-of-Transmission flag.
///
/// IPv4-only variant of the [`getserversExtResponse` message](GetServersExtResponseMessage).
#[derive(Debug, PartialEq, Eq)]
pub struct GetServersResponseMessage {
    servers: Vec<std::net::SocketAddrV4>,
    eot: bool,
}

impl GetServersResponseMessage {
    /// Creates a new `GetServersResponseMessage` for the given `servers` and "eot" flag.
    pub fn new(servers: Vec<std::net::SocketAddrV4>, eot: bool) -> Self {
        Self { servers, eot }
    }

    /// Returns the server socket addresses contained in this message.
    pub fn servers(&self) -> &[std::net::SocketAddrV4] {
        &self.servers[..]
    }

    /// Returns the EOT flag contained in this message.
    pub fn eot(&self) -> bool {
        self.eot
    }
}

/// Filter options for a [`getserversExt` message](GetServersExtMessage)
///
/// Contains a [`GameType`](GameType), "empty" / "full" and "ipv4" / "ipv6" options.
///
/// IPv6-enabled variant of [`FilterOptions`](FilterOptions).
pub struct FilterExtOptions {
    /// `gametype=X` filter option
    gametype: Option<GameType>,
    /// empty servers option
    empty: bool,
    /// full servers option
    full: bool,
    // IPv4 servers option
    ipv4: bool,
    // IPv6 servers option
    ipv6: bool,
}

impl FilterExtOptions {
    /// Creates a new `FilterExtOptions` for the given `gametype`, `empty` / `full` and `ìpv4` / `ipv6` options.
    pub fn new(
        gametype: Option<GameType>,
        empty: bool,
        full: bool,
        ipv4: bool,
        ipv6: bool,
    ) -> Self {
        Self {
            gametype,
            empty,
            full,
            ipv4,
            ipv6,
        }
    }

    /// Returns the `GameType` option contained in this filter.
    pub fn gametype(&self) -> Option<&GameType> {
        self.gametype.as_ref()
    }

    /// Returns the "empty" option contained in this filter.
    pub fn empty(&self) -> bool {
        self.empty
    }

    /// Returns the "empty" option contained in this filter.
    pub fn full(&self) -> bool {
        self.full
    }

    /// Returns the "ipv4" option contained in this filter.
    pub fn ipv4(&self) -> bool {
        self.ipv4
    }

    /// Returns the "ipv6" option contained in this filter.
    pub fn ipv6(&self) -> bool {
        self.ipv6
    }
}

/// `getserversExt` message
///
/// Sent initially from a game client to the master server.\
/// Responded to with a [`getserversExtResponse` messsage](GetServersExtResponseMessage) from the master server.
///
/// Contains a [`GameName`](GameName), [`ProtocolNumber`](ProtocolNumber) and [`FilterExtOptions`](FilterExtOptions).
///
/// IPv6-enabled variant of the [`getservers` message](GetServersMessage).
pub struct GetServersExtMessage {
    game_name: GameName,
    protocol_number: ProtocolNumber,
    filter_options: FilterExtOptions,
}

impl GetServersExtMessage {
    /// Creates a new `GetServersMessage` for the given `game_name`, `protocol_number` and `filter_options`.
    pub fn new(
        game_name: GameName,
        protocol_number: ProtocolNumber,
        filter_options: FilterExtOptions,
    ) -> Self {
        Self {
            game_name,
            protocol_number,
            filter_options,
        }
    }

    /// Returns the `GameName` contained in this message.
    pub fn game_name(&self) -> &GameName {
        &self.game_name
    }

    /// Returns the `ProtocolNumber` contained in this message.
    pub fn protocol_number(&self) -> ProtocolNumber {
        self.protocol_number
    }

    /// Returns the `FilterExtOptions` contained in this message.
    pub fn filter_options(&self) -> &FilterExtOptions {
        &self.filter_options
    }
}

/// `getserversExtResponse` message
///
/// Sent concludingly from the master server to a game client in response to a [`getserversExt` message](GetServersExtMessage) from the game client.
///
/// Contains a list of [`SocketAddr`](std::net::SocketAddr) and End-of-Transmission flag.
///
/// IPv6-enabled variant of the [`getserversResponse` message](GetServersResponseMessage).
pub struct GetServersExtResponseMessage {
    servers: Vec<std::net::SocketAddr>,
    eot: bool,
}

impl GetServersExtResponseMessage {
    /// Creates a new `GetServersResponseMessage` for the given `servers` and "eot" flag.
    pub fn new(servers: Vec<std::net::SocketAddr>, eot: bool) -> Self {
        Self { servers, eot }
    }

    /// Returns the server socket addresses contained in this message.
    pub fn servers(&self) -> &[std::net::SocketAddr] {
        &self.servers
    }

    /// Returns the EOT flag contained in this message.
    pub fn eot(&self) -> bool {
        self.eot
    }
}