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
//#![warn(missing_docs)]

//! `dpmaster` protocol
//!
//! ## `dpmaster`
//!
//! `dpmaster`, "an open master server",
//! is both a generic protocol to register game servers with a master server and query them from game clients
//! as well as a reference master server implementation written in the C programming language.
//!
//! This crate implements the `dpmaster` wire protocol as defined in its ["Technical information" documentation](https://hg.icculus.org/molivier/dpmaster/file/tip/doc/techinfo.txt).
//!
//! ## `dpmaster` protocol
//!
//! The `dpmaster` protocol is a custom UDP protocol where datagram packets contain a mix of binary delimiters as well as ASCII text.\
//! Neither the protocol nor its specification are versioned, but they have not changed for a couple of years.
//!
//! This crate implements (de)serialization of (not-yet all) packets, which the spec calls messages:
//! * [`heartbeat` message](crate::messages::HeartbeatMessage)
//! * [`getinfo` message](crate::messages::GetInfoMessage)
//! * [`infoResponse` message](crate::messages::InfoResponseMessage)
//! * [`getservers` message](crate::messages::GetServersMessage)
//! * [`getserversResponse` message](crate::messages::GetServersResponseMessage)
//! * [`getserversExt` message](crate::messages::GetServersExtMessage)
//! * [`getserversExtResponse` message](crate::messages::GetServersExtResponseMessage)
//!
//!
//! ## Message flows
//!
//! The "master server" concept is that there is a central server instance, e.g. at `master.example:27950`, that all game servers report to.\
//! Game servers then register themselves with the master server, e.g. their IPs `192.0.2.1:27960` / `[2001:db8::1]:27964` aswell as some [`Info`](crate::messages::Info) metadata.\
//! Game clients query the master server for a (filtered) list of game servers.
//!
//! ### `heartbeat` message flow
//!
//! The "heartbeat" message flow registers a game server with the master server.\
//! It should be initiated by game servers when their configuration changes and periodically to remain registered with the master server.
//!
//! ❶ Game server sends `heartbeat` message to master server
//!
//! ❷ Master server sends `getinfo` message with new challenge back to game server
//!
//! ❸ Game server sends `infoResponse` message with same challenge back to master server
#![doc=include_str!("../assets/message-flow-heartbeat.svg")]
//!
//! ### `getservers` message flow
//!
//! The "getservers" message flow queries a master server for game servers.\
//! It is initiated by game clients e.g. when they want to display a list of servers in their UI so players can decide which one to connect to.
//!
//! ❶ Game client sends `getservers` message to master server
//!
//! ❷ Master server sends `getserversResponse` message(s) back to game client
#![doc=include_str!("../assets/message-flow-getservers.svg")]
//!
//! ## `dpmaster-proto` crate
//!
//! This crate implements the `dpmaster` protocol wire format, i.e. it allows to serialize and deserialize protocol messages as bytes to and from UDP packets.\
//! It does however not contain logic (["sans I/O"](https://sans-io.readthedocs.io/)), e.g. to implement a master server, game server or game client.\
//!
//! The `dpmaster-codec` crate implements Tokio codecs on top of this protocol crate.\
//! The `dpmaster-game-client-bin` crate implements a "game client" on top of a codec in form of a command-line-interface to query a master server for game servers.

pub mod deserializer;
pub mod error;
pub mod messages;
pub mod serializer;

pub use messages::{
    Challenge, GameName, GameType, GetInfoMessage, GetServersExtResponseMessage,
    GetServersResponseMessage, HeartbeatMessage, Info, InfoKey, InfoResponseMessage, InfoValue,
    ProtocolName,
};

pub use crate::error::ProtocolError;
/// [std::result::Result] alias with [ProtocolError] as `Err`
pub type Result<T, E = ProtocolError> = std::result::Result<T, E>;

#[cfg(test)]
mod tests {
    use super::deserializer::{
        getinfo_message, getservers_message, heartbeat_message, inforesponse_message,
    };
    use super::messages::{
        Challenge, FilterOptions, GameName, GameType, GetInfoMessage, GetServersMessage,
        HeartbeatMessage, Info, InfoKey, InfoResponseMessage, InfoValue, ProtocolName,
    };
    use super::serializer::{
        gen_getinfo_message, gen_getservers_message, gen_heartbeat_message,
        gen_inforesponse_message,
    };
    use cookie_factory::gen_simple;
    use std::io::Cursor;

    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }

    macro_rules! roundtrip_message_test {
        (
            $name:ident {
                message: $message:expr,
                serializer: $serializer:expr,
                deserializer: $deserializer:expr,
            }
        ) => {
            #[test]
            fn $name() {
                let message = $message;
                let serialized_message = $serializer(&message);

                let mut buffer = [0u8; 512];
                let cursor = Cursor::new(&mut buffer[..]);
                let cursor = gen_simple(serialized_message, cursor).unwrap();
                let size = cursor.position() as usize;
                let buffer = cursor.into_inner();

                let (_, deserialized_message) = $deserializer(&buffer[..size]).unwrap();

                assert_eq!(deserialized_message, message);
            }
        };
    }

    macro_rules! roundtrip_heartbeat_message_test {
        (
        $name:ident {
            message: $message:expr
        }
        ) => {
            roundtrip_message_test!($name {
                message: $message,
                serializer: gen_heartbeat_message,
                deserializer: heartbeat_message,
            });
        };
    }

    roundtrip_heartbeat_message_test!(test_roundtrip_heartbeat_message_dp {
        message: HeartbeatMessage::new(ProtocolName::new(b"DarkPlaces".to_vec()).unwrap(),)
    });

    roundtrip_heartbeat_message_test!(test_roundtrip_heartbeat_message_q3a {
        message: HeartbeatMessage::new(ProtocolName::new(b"QuakeArena-1".to_vec()).unwrap(),)
    });

    roundtrip_heartbeat_message_test!(test_roundtrip_heartbeat_message_rtcw {
        message: HeartbeatMessage::new(ProtocolName::new(b"Wolfenstein-1".to_vec()).unwrap(),)
    });

    roundtrip_heartbeat_message_test!(test_roundtrip_heartbeat_message_woet {
        message: HeartbeatMessage::new(ProtocolName::new(b"EnemyTerritory-1".to_vec()).unwrap(),)
    });

    macro_rules! roundtrip_getinfo_message_test {
        (
        $name:ident {
            message: $message:expr
        }
        ) => {
            roundtrip_message_test!($name {
                message: $message,
                serializer: gen_getinfo_message,
                deserializer: getinfo_message,
            });
        };
    }

    roundtrip_getinfo_message_test!(test_roundtrip_getinfo_message {
        message: GetInfoMessage::new(Challenge::new(b"A_ch4Lleng3".to_vec()).unwrap(),)
    });

    macro_rules! roundtrip_inforesponse_message_test {
        (
        $name:ident {
            message: $message:expr
        }
        ) => {
            roundtrip_message_test!($name {
                message: $message,
                serializer: gen_inforesponse_message,
                deserializer: inforesponse_message,
            });
        };
    }

    roundtrip_inforesponse_message_test!(test_roundtrip_inforesponse_message {
        message: InfoResponseMessage::new({
            let mut info = Info::new();
            info.insert(
                InfoKey::new(b"sv_maxclients".to_vec()).unwrap(),
                InfoValue::new(b"8".to_vec()).unwrap(),
            );
            info.insert(
                InfoKey::new(b"clients".to_vec()).unwrap(),
                InfoValue::new(b"0".to_vec()).unwrap(),
            );
            info
        })
    });

    macro_rules! roundtrip_getservers_message_test {
        (
        $name:ident {
            message: $message:expr
        }
        ) => {
            roundtrip_message_test!($name {
                message: $message,
                serializer: gen_getservers_message,
                deserializer: getservers_message,
            });
        };
    }

    roundtrip_getservers_message_test!(test_roundtrip_getservers_message_q3a {
        message: GetServersMessage::new(
            None,
            67,
            FilterOptions::new(Some(GameType::new(b"0".to_vec()).unwrap()), true, true)
        )
    });

    roundtrip_getservers_message_test!(test_roundtrip_getservers_message_woet {
        message: GetServersMessage::new(None, 84, FilterOptions::new(None, false, false))
    });

    roundtrip_getservers_message_test!(test_roundtrip_getservers_message_nexuiz {
        message: GetServersMessage::new(
            Some(GameName::new(b"Nexuiz".to_vec()).unwrap()),
            3,
            FilterOptions::new(None, false, false)
        )
    });

    roundtrip_getservers_message_test!(test_roundtrip_getservers_message_qfusion {
        message: GetServersMessage::new(
            Some(GameName::new(b"qfusion".to_vec()).unwrap()),
            39,
            FilterOptions::new(None, false, true)
        )
    });
}