steamkit-rs 0.1.2

Rust port of SteamKit2, a .NET library that aims to interoperate with the Steam network.
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
//! Steam UDP packets, plus the packet header and the two handshake payloads.
//!
//! # Wire format
//!
//! Every UDP datagram is a 36 byte little-endian header followed by the
//! payload:
//!
//! ```text
//! offset  size  field
//! 0       4     magic          (u32, always 0x31305356)
//! 4       2     payload_size   (u16)
//! 6       1     packet_type    (u8, EUdpPacketType)
//! 7       1     flags          (u8)
//! 8       4     source_conn_id (u32)
//! 12      4     dest_conn_id   (u32)
//! 16      4     seq_this       (u32)
//! 20      4     seq_ack        (u32)
//! 24      4     packets_in_msg (u32)
//! 28      4     msg_start_seq  (u32)
//! 32      4     msg_size       (u32)
//! 36      ..    payload
//! ```

use crate::base::generated::steam_language::EUdpPacketType;
use crate::error::SteamError;

/// The maximum payload size of a UDP packet: `0x4DC` (1244) bytes.
pub const MAX_PAYLOAD: usize = 0x4DC;

fn read_u32(data: &[u8], cursor: &mut usize) -> Result<u32, SteamError> {
    let bytes = read_bytes(data, cursor, 4)?;
    Ok(u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
}

fn read_u16(data: &[u8], cursor: &mut usize) -> Result<u16, SteamError> {
    let bytes = read_bytes(data, cursor, 2)?;
    Ok(u16::from_le_bytes([bytes[0], bytes[1]]))
}

fn read_u8(data: &[u8], cursor: &mut usize) -> Result<u8, SteamError> {
    Ok(read_bytes(data, cursor, 1)?[0])
}

fn read_bytes<'a>(
    data: &'a [u8],
    cursor: &mut usize,
    len: usize,
) -> Result<&'a [u8], SteamError> {
    let end = cursor
        .checked_add(len)
        .ok_or_else(|| SteamError::Malformed("UDP header length overflow".to_string()))?;
    if end > data.len() {
        return Err(SteamError::Malformed(
            "UDP packet is too short to contain a header".to_string(),
        ));
    }
    let slice = &data[*cursor..end];
    *cursor = end;
    Ok(slice)
}

fn packet_type_from_u8(value: u8) -> EUdpPacketType {
    match value {
        0 => EUdpPacketType::Invalid,
        1 => EUdpPacketType::ChallengeReq,
        2 => EUdpPacketType::Challenge,
        3 => EUdpPacketType::Connect,
        4 => EUdpPacketType::Accept,
        5 => EUdpPacketType::Disconnect,
        6 => EUdpPacketType::Data,
        7 => EUdpPacketType::Datagram,
        8 => EUdpPacketType::Max,
        _ => EUdpPacketType::Invalid,
    }
}

fn packet_type_to_u8(value: EUdpPacketType) -> u8 {
    match value {
        EUdpPacketType::Invalid => 0,
        EUdpPacketType::ChallengeReq => 1,
        EUdpPacketType::Challenge => 2,
        EUdpPacketType::Connect => 3,
        EUdpPacketType::Accept => 4,
        EUdpPacketType::Disconnect => 5,
        EUdpPacketType::Data => 6,
        EUdpPacketType::Datagram => 7,
        EUdpPacketType::Max => 8,
    }
}

/// The header of a Steam UDP packet.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UdpHeader {
    /// Always [`UdpHeader::MAGIC`] for valid packets.
    pub magic: u32,
    /// The size of the payload that follows this header.
    pub payload_size: u16,
    /// The type of this packet.
    pub packet_type: EUdpPacketType,
    /// Protocol flags (unused).
    pub flags: u8,
    /// The connection id of the sender.
    pub source_conn_id: u32,
    /// The connection id of the receiver.
    pub dest_conn_id: u32,
    /// The sequence number of this packet.
    pub seq_this: u32,
    /// The highest sequence number received by the sender.
    pub seq_ack: u32,
    /// The number of packets this message is split into.
    pub packets_in_msg: u32,
    /// The sequence number of the first packet of this message.
    pub msg_start_seq: u32,
    /// The total size of the message this packet belongs to.
    pub msg_size: u32,
}

impl UdpHeader {
    /// The magic value that identifies a Steam UDP packet: `0x31305356`.
    pub const MAGIC: u32 = 0x31305356;

    /// The size of the serialized header in bytes.
    pub const SERIALIZED_SIZE: usize = 36;

    /// Creates a header with the default field values.
    pub fn new() -> Self {
        Self {
            magic: UdpHeader::MAGIC,
            payload_size: 0,
            packet_type: EUdpPacketType::Invalid,
            flags: 0,
            source_conn_id: 512,
            dest_conn_id: 0,
            seq_this: 0,
            seq_ack: 0,
            packets_in_msg: 0,
            msg_start_seq: 0,
            msg_size: 0,
        }
    }

    /// Serializes the header into its 36 byte little-endian wire form.
    pub fn serialize(&self) -> Vec<u8> {
        let mut out = Vec::with_capacity(UdpHeader::SERIALIZED_SIZE);
        out.extend_from_slice(&self.magic.to_le_bytes());
        out.extend_from_slice(&self.payload_size.to_le_bytes());
        out.push(packet_type_to_u8(self.packet_type));
        out.push(self.flags);
        out.extend_from_slice(&self.source_conn_id.to_le_bytes());
        out.extend_from_slice(&self.dest_conn_id.to_le_bytes());
        out.extend_from_slice(&self.seq_this.to_le_bytes());
        out.extend_from_slice(&self.seq_ack.to_le_bytes());
        out.extend_from_slice(&self.packets_in_msg.to_le_bytes());
        out.extend_from_slice(&self.msg_start_seq.to_le_bytes());
        out.extend_from_slice(&self.msg_size.to_le_bytes());
        out
    }

    /// Deserializes the header from the front of `data`. Fails as soon as the
    /// data runs out; fields read before that keep their new values.
    pub fn deserialize(&mut self, data: &[u8]) -> Result<(), SteamError> {
        let mut cursor = 0usize;

        self.magic = read_u32(data, &mut cursor)?;
        self.payload_size = read_u16(data, &mut cursor)?;
        let packet_type = read_u8(data, &mut cursor)?;
        self.packet_type = packet_type_from_u8(packet_type);
        self.flags = read_u8(data, &mut cursor)?;
        self.source_conn_id = read_u32(data, &mut cursor)?;
        self.dest_conn_id = read_u32(data, &mut cursor)?;
        self.seq_this = read_u32(data, &mut cursor)?;
        self.seq_ack = read_u32(data, &mut cursor)?;
        self.packets_in_msg = read_u32(data, &mut cursor)?;
        self.msg_start_seq = read_u32(data, &mut cursor)?;
        self.msg_size = read_u32(data, &mut cursor)?;

        Ok(())
    }
}

impl Default for UdpHeader {
    fn default() -> Self {
        Self::new()
    }
}

/// The challenge the server sends during the UDP handshake.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ChallengeData {
    /// The challenge value, to be XORed with [`ChallengeData::CHALLENGE_MASK`].
    pub challenge_value: u32,
    /// The server's load (unused).
    pub server_load: u32,
}

impl ChallengeData {
    /// The mask that is XORed with the challenge value.
    pub const CHALLENGE_MASK: u32 = 0xA426DF2B;

    /// Deserializes the challenge from the front of `data`.
    pub fn deserialize(data: &[u8]) -> Result<Self, SteamError> {
        if data.len() < 8 {
            return Err(SteamError::Malformed(
                "UDP challenge payload is shorter than 8 bytes".to_string(),
            ));
        }
        Ok(Self {
            challenge_value: u32::from_le_bytes([data[0], data[1], data[2], data[3]]),
            server_load: u32::from_le_bytes([data[4], data[5], data[6], data[7]]),
        })
    }

    /// Serializes the challenge into its 8 byte little-endian wire form.
    pub fn serialize(&self) -> Vec<u8> {
        let mut out = Vec::with_capacity(8);
        out.extend_from_slice(&self.challenge_value.to_le_bytes());
        out.extend_from_slice(&self.server_load.to_le_bytes());
        out
    }
}

/// The connect request sent during the UDP handshake.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ConnectData {
    /// The masked challenge value.
    pub challenge_value: u32,
}

impl ConnectData {
    /// The mask that is XORed with the challenge value.
    pub const CHALLENGE_MASK: u32 = ChallengeData::CHALLENGE_MASK;

    /// Deserializes the connect data from the front of `data`.
    pub fn deserialize(data: &[u8]) -> Result<Self, SteamError> {
        if data.len() < 4 {
            return Err(SteamError::Malformed(
                "UDP connect payload is shorter than 4 bytes".to_string(),
            ));
        }
        Ok(Self {
            challenge_value: u32::from_le_bytes([data[0], data[1], data[2], data[3]]),
        })
    }

    /// Serializes the connect data into its 4 byte little-endian wire form.
    pub fn serialize(&self) -> Vec<u8> {
        self.challenge_value.to_le_bytes().to_vec()
    }
}

/// A Steam UDP packet: a [`UdpHeader`] plus its payload.
#[derive(Clone, Debug)]
pub struct UdpPacket {
    /// The packet header.
    pub header: UdpHeader,
    payload: Option<Vec<u8>>,
}

impl UdpPacket {
    /// Creates a packet of the given type with no payload. The caller fills in
    /// the header.
    pub fn new(packet_type: EUdpPacketType) -> Self {
        let mut header = UdpHeader::new();
        header.packet_type = packet_type;

        Self {
            header,
            payload: Some(Vec::new()),
        }
    }

    /// Creates a packet of the given type carrying the given payload.
    pub fn with_payload(
        packet_type: EUdpPacketType,
        payload: Vec<u8>,
    ) -> Result<Self, SteamError> {
        let mut packet = Self::new(packet_type);
        packet.set_payload(payload)?;
        Ok(packet)
    }

    /// Creates a packet of the given type carrying `length` bytes taken from
    /// `source` starting at `offset`.
    pub fn with_payload_slice(
        packet_type: EUdpPacketType,
        source: &[u8],
        offset: usize,
        length: usize,
    ) -> Result<Self, SteamError> {
        let mut packet = Self::new(packet_type);
        packet.set_payload_slice(source, offset, length)?;
        Ok(packet)
    }

    /// Parses a received datagram.
    ///
    /// A malformed header or a bad magic gives a packet with no payload, so
    /// [`UdpPacket::is_valid`] returns `false`. SteamKit2 treats a truncated
    /// datagram as a valid empty packet instead; dropping it is safer.
    pub fn from_bytes(data: &[u8]) -> Self {
        let mut header = UdpHeader::new();

        if header.deserialize(data).is_err() {
            return Self {
                header,
                payload: None,
            };
        }

        if header.magic != UdpHeader::MAGIC {
            return Self {
                header,
                payload: None,
            };
        }

        let length = header.payload_size as usize;
        if length > MAX_PAYLOAD {
            // SteamKit2 throws here; we just drop the packet.
            return Self {
                header,
                payload: None,
            };
        }

        // A datagram shorter than `payload_size` gives a zero-padded payload,
        // same as SteamKit2.
        let mut payload = vec![0u8; length];
        let available = data
            .len()
            .saturating_sub(UdpHeader::SERIALIZED_SIZE)
            .min(length);
        payload[..available].copy_from_slice(
            &data[UdpHeader::SERIALIZED_SIZE..UdpHeader::SERIALIZED_SIZE + available],
        );

        header.payload_size = length as u16;
        header.msg_size = length as u32;

        Self {
            header,
            payload: Some(payload),
        }
    }

    /// Whether this instance is valid.
    pub fn is_valid(&self) -> bool {
        self.header.magic == UdpHeader::MAGIC
            && self.header.payload_size as usize <= MAX_PAYLOAD
            && self.payload.is_some()
    }

    /// The payload of this packet, or an empty slice when the packet is invalid.
    pub fn payload(&self) -> &[u8] {
        self.payload.as_deref().unwrap_or(&[])
    }

    /// Replaces the payload and updates the header.
    pub fn set_payload(&mut self, payload: Vec<u8>) -> Result<(), SteamError> {
        if payload.len() > MAX_PAYLOAD {
            return Err(SteamError::InvalidArgument(
                "Payload length exceeds 0x4DC maximum".to_string(),
            ));
        }

        self.header.payload_size = payload.len() as u16;
        self.header.msg_size = payload.len() as u32;
        self.payload = Some(payload);

        Ok(())
    }

    /// Replaces the payload with `length` bytes of `source` starting at
    /// `offset`, and updates the header.
    pub fn set_payload_slice(
        &mut self,
        source: &[u8],
        offset: usize,
        length: usize,
    ) -> Result<(), SteamError> {
        if length > MAX_PAYLOAD {
            return Err(SteamError::InvalidArgument(
                "Payload length exceeds 0x4DC maximum".to_string(),
            ));
        }

        let end = offset.saturating_add(length).min(source.len());
        let mut payload = vec![0u8; length];
        if end > offset {
            payload[..end - offset].copy_from_slice(&source[offset..end]);
        }

        self.header.payload_size = length as u16;
        self.header.msg_size = length as u32;
        self.payload = Some(payload);

        Ok(())
    }

    /// Serializes the packet.
    pub fn get_data(&self) -> Vec<u8> {
        let mut out = self.header.serialize();
        out.extend_from_slice(self.payload());
        out
    }
}