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
use std::io::Cursor;
use std::task::Context;

use num_traits::ToPrimitive;
use omnom::WriteExt;
use slog::warn;
use tsproto_packets::packets::*;

use crate::algorithms as algs;
use crate::connection::{Connection, Event, StreamItem};
use crate::resend::{PartialPacketId, Resender};
use crate::{Error, Result, MAX_FRAGMENTS_LENGTH, MAX_QUEUE_LEN};

/// Encodes outgoing packets.
///
/// This part does the compression, encryption and fragmentation.
#[derive(Clone, Debug, Default)]
pub struct PacketCodec {
	/// The next packet id that should be sent.
	///
	/// This list is indexed by the `PacketType`, `PacketType::Init` is an
	/// invalid index.
	pub outgoing_p_ids: [PartialPacketId; 8],
	/// Used for incoming out-of-order packets.
	///
	/// Only used for `Command` and `CommandLow` packets.
	pub receive_queue: [Vec<Vec<u8>>; 2],
	/// Used for incoming fragmented packets.
	///
	/// Contains the accumulated data from fragmented packets, the header of the
	/// first packet is at the beginning, all other headers are stripped away.
	/// Only used for `Command` and `CommandLow` packets.
	pub fragmented_queue: [Option<Vec<u8>>; 2],
	/// The next packet id that is expected.
	///
	/// Works like the `outgoing_p_ids`.
	pub incoming_p_ids: [PartialPacketId; 8],
}

impl PacketCodec {
	/// Handle a packet for a specific connection.
	///
	/// This part does the defragmentation, decryption and decompression.
	pub fn handle_udp_packet(
		con: &mut Connection, cx: &mut Context, mut packet_data: Vec<u8>,
	) -> Result<()> {
		let mut ack = false;

		let dir = if con.is_client { Direction::S2C } else { Direction::C2S };
		let packet = InPacket::new(dir, &packet_data);
		let p_type = packet.header().packet_type();
		let type_i = p_type.to_usize().unwrap();
		let id = packet.header().packet_id();
		let (in_recv_win, gen_id, cur_next, limit) = con.in_receive_window(p_type, id);

		con.resender.handle_loss_incoming(&packet, in_recv_win, cur_next);

		if let Some(params) = &con.params {
			if p_type == PacketType::Init {
				con.stream_items.push_back(StreamItem::Error(Error::UnexpectedInitPacket));
				return Ok(());
			}
			if !con.is_client {
				let c_id = packet.header().client_id().unwrap();
				// Accept any client id for the first few acks
				let is_first_ack = gen_id == 0 && id <= 3 && p_type == PacketType::Ack;
				if c_id != params.c_id && !is_first_ack {
					con.stream_items.push_back(StreamItem::Error(Error::WrongClientId(c_id)));
					return Ok(());
				}
			}
		}

		// Ignore range for acks and audio packets
		if [PacketType::Ack, PacketType::AckLow, PacketType::Voice, PacketType::VoiceWhisper]
			.contains(&p_type)
			|| in_recv_win
		{
			if !packet.header().flags().contains(Flags::UNENCRYPTED) {
				if p_type == PacketType::Ack && id == 1 && con.is_client {
					// This is the ack packet for the clientinit, we take the
					// initserver as ack instead.
					return Ok(());
				}

				// If it is the first ack packet of a client, try to fake
				// decrypt it.
				let new_content = if (p_type == PacketType::Ack && id <= 1 && con.is_client)
					|| con.params.is_none()
				{
					match algs::decrypt_fake(&packet).or_else(|_| {
						if let Some(params) = &mut con.params {
							// Decrypt the packet
							algs::decrypt(&packet, gen_id, &params.shared_iv, &mut params.key_cache)
						} else {
							// Failed to fake decrypt the packet
							Err(Error::WrongMac { p_type, generation_id: gen_id, packet_id: id })
						}
					}) {
						Ok(r) => r,
						Err(e) => {
							con.stream_items.push_back(StreamItem::Error(e));
							return Ok(());
						}
					}
				} else if let Some(params) = &mut con.params {
					// Decrypt the packet
					match algs::decrypt(&packet, gen_id, &params.shared_iv, &mut params.key_cache) {
						Ok(r) => r,
						Err(e) => {
							con.stream_items.push_back(StreamItem::Error(e));
							return Ok(());
						}
					}
				} else {
					// Failed to fake decrypt the packet
					con.stream_items.push_back(StreamItem::Error(Error::WrongMac {
						p_type,
						generation_id: gen_id,
						packet_id: id,
					}));
					return Ok(());
				};

				let start = packet.header().data().len();
				(&mut packet_data[start..]).copy_from_slice(&new_content);
			} else if algs::must_encrypt(p_type) {
				// Check if it is ok for the packet to be unencrypted
				con.stream_items.push_back(StreamItem::Error(Error::UnallowedUnencryptedPacket));
				return Ok(());
			}

			let packet = InPacket::new(dir, &packet_data);
			match p_type {
				PacketType::Command | PacketType::CommandLow => {
					ack = true;

					let commands = Self::handle_command_packet(con, packet_data)?;

					// Be careful with command packets, they are guaranteed to
					// be in the right order now.
					for c in commands {
						// Send again
						let packet = InPacket::new(dir, &c);
						let event = Event::ReceivePacket(&packet);
						con.send_event(&event);

						let item = match InCommandBuf::try_new(dir, c) {
							Ok(c) => {
								// initivexpand2 is the ack for the last init packet
								if con.is_client
									&& c.data().packet().content().starts_with(b"initivexpand2 ")
								{
									Resender::ack_packet(con, cx, PacketType::Init, 4);
								} else if con.is_client
									&& c.data().packet().content().starts_with(b"initserver ")
								{
									// initserver acks clientinit
									Resender::ack_packet(con, cx, PacketType::Command, 2);
								} else if !con.is_client
									&& c.data().packet().content().starts_with(b"clientek ")
								{
									// clientek acks initivexpand2
									Resender::ack_packet(con, cx, PacketType::Command, 0);
								}
								StreamItem::Command(c)
							}
							Err(e) => return Err(Error::PacketParse("command", e)),
						};
						con.stream_items.push_back(item);
					}
				}
				_ => {
					if p_type == PacketType::Ping {
						ack = true;
					}
					// Update packet ids
					let in_ids = &mut con.codec.incoming_p_ids;
					if p_type != PacketType::Init {
						let (id, next_gen) = id.overflowing_add(1);
						let generation_id = if next_gen { gen_id + 1 } else { gen_id };
						in_ids[type_i] = PartialPacketId { generation_id, packet_id: id };
					}

					match packet.ack_packet() {
						Ok(Some(ack_id)) => {
							// Remove command packet from send queue if the fitting ack is received.
							let p_type = if p_type == PacketType::Ack {
								PacketType::Command
							} else if p_type == PacketType::AckLow {
								PacketType::CommandLow
							} else if p_type == PacketType::Pong {
								PacketType::Ping
							} else {
								p_type
							};
							Resender::ack_packet(con, cx, p_type, ack_id);
						}
						Ok(None) => {}
						Err(e) => {
							con.stream_items.push_back(StreamItem::Error(Error::CreateAck(e)));
							return Ok(());
						}
					}

					// Send event after handling acks
					let event = Event::ReceivePacket(&packet);
					con.send_event(&event);

					if p_type.is_voice() {
						con.stream_items.push_back(match InAudioBuf::try_new(dir, packet_data) {
							Ok(r) => StreamItem::Audio(r),
							Err(e) => StreamItem::Error(Error::PacketParse("audio", e)),
						});
					} else if p_type == PacketType::Init {
						con.stream_items.push_back(if con.is_client {
							match InS2CInitBuf::try_new(dir, packet_data) {
								Ok(r) => StreamItem::S2CInit(r),
								Err(e) => StreamItem::Error(Error::PacketParse("s2cinit", e)),
							}
						} else {
							match InC2SInitBuf::try_new(dir, packet_data) {
								Ok(r) => StreamItem::C2SInit(r),
								Err(e) => StreamItem::Error(Error::PacketParse("c2sinit", e)),
							}
						});
					}
				}
			}
		} else {
			// Send an ack for the case when it was lost
			if p_type == PacketType::Command || p_type == PacketType::CommandLow {
				ack = true;
				con.resender.handle_loss_resend_ack();
			}
			con.stream_items.push_back(StreamItem::Error(Error::NotInReceiveWindow {
				id,
				next: cur_next,
				limit,
				p_type,
			}));
		}

		// Send ack
		if ack {
			con.send_ack_packet(cx, OutAck::new(dir.reverse(), p_type, id))?;
		}

		Ok(())
	}

	/// Handle `Command` and `CommandLow` packets.
	///
	/// They have to be handled in the right order.
	fn handle_command_packet(
		con: &mut Connection, mut packet_data: Vec<u8>,
	) -> Result<Vec<Vec<u8>>> {
		let dir = if con.is_client { Direction::S2C } else { Direction::C2S };
		let mut packet = InPacket::new(dir, &packet_data);
		let header = packet.header();
		let p_type = header.packet_type();
		let mut id = header.packet_id();
		let type_i = p_type.to_usize().unwrap();
		let cmd_i = if p_type == PacketType::Command { 0 } else { 1 };
		let r_queue = &mut con.codec.receive_queue[cmd_i];
		let frag_queue = &mut con.codec.fragmented_queue[cmd_i];
		let in_ids = &mut con.codec.incoming_p_ids[type_i];
		let cur_next = in_ids.packet_id;
		if cur_next == id {
			// In order
			let mut packets = Vec::new();
			loop {
				// Update next packet id
				*in_ids = *in_ids + 1;

				let flags = packet.header().flags();
				let res_packet = if flags.contains(Flags::FRAGMENTED) {
					if let Some(mut frag_queue) = frag_queue.take() {
						// Last fragmented packet
						frag_queue.extend_from_slice(packet.content());
						let header = InPacket::new(dir, &frag_queue);
						// Decompress
						if header.header().flags().contains(Flags::COMPRESSED) {
							let decompressed = quicklz::decompress(
								&mut Cursor::new(header.content()),
								crate::MAX_DECOMPRESSED_SIZE,
							)
							.map_err(Error::DecompressPacket)?;
							let start = header.header().data().len();
							frag_queue.truncate(start);
							frag_queue.extend_from_slice(&decompressed);
						}

						Some(frag_queue)
					} else {
						// Enqueue
						*frag_queue = Some(packet_data);
						None
					}
				} else if let Some(frag_queue) = frag_queue {
					// The packet is fragmented
					if frag_queue.len() < MAX_FRAGMENTS_LENGTH {
						frag_queue.extend_from_slice(packet.content());
						None
					} else {
						return Err(Error::MaxLengthExceeded("fragment queue"));
					}
				} else {
					// Decompress
					if flags.contains(Flags::COMPRESSED) {
						let decompressed = quicklz::decompress(
							&mut Cursor::new(packet.content()),
							crate::MAX_DECOMPRESSED_SIZE,
						)
						.map_err(Error::DecompressPacket)?;
						let start = packet.header().data().len();
						packet_data.truncate(start);
						packet_data.extend_from_slice(&decompressed);
					};
					Some(packet_data)
				};
				if let Some(p) = res_packet {
					packets.push(p);
				}

				// Check if there are following packets in the receive queue.
				id = id.wrapping_add(1);
				if let Some(pos) =
					r_queue.iter().position(|p| InHeader::new(dir, p).packet_id() == id)
				{
					packet_data = r_queue.remove(pos);
					packet = InPacket::new(dir, &packet_data);
				} else {
					break;
				}
			}

			// The first packets should be returned first
			packets.reverse();
			Ok(packets)
		} else {
			// Out of order
			warn!(con.logger, "Out of order command packet"; "got" => id,
				"expected" => cur_next);
			let (limit, next_gen) = cur_next.overflowing_add(MAX_QUEUE_LEN);
			if (!next_gen && id >= cur_next && id < limit)
				|| (next_gen && (id >= cur_next || id < limit))
			{
				r_queue.push(packet_data);
				Ok(vec![])
			} else {
				Err(Error::MaxLengthExceeded("command queue"))
			}
		}
	}

	pub fn encode_packet(con: &mut Connection, mut packet: OutPacket) -> Result<Vec<OutUdpPacket>> {
		let p_type = packet.header().packet_type();
		let type_i = p_type.to_usize().unwrap();

		// TODO Needed, commands should set their own flag?
		if (p_type == PacketType::Command || p_type == PacketType::CommandLow) && con.is_client {
			// Set newprotocol flag
			packet.flags(packet.header().flags() | Flags::NEWPROTOCOL);
		}

		let p_id = if p_type == PacketType::Init {
			PartialPacketId { generation_id: 0, packet_id: 0 }
		} else {
			con.codec.outgoing_p_ids[type_i]
		};
		// We fake encrypt the first command packet of the server (id 0) and the
		// first command packet of the client (id 1, clientek).
		let mut fake_encrypt = p_type == PacketType::Command
			&& p_id.generation_id == 0
			&& ((!con.is_client && p_id.packet_id == 0)
				|| (con.is_client && p_id.packet_id == 1 && {
					// Test if it is a clientek packet
					packet.content().starts_with(b"clientek")
				}));
		// Also fake encrypt the first ack of the client, which is the response
		// for the initivexpand2 packet.
		fake_encrypt |= con.is_client
			&& p_type == PacketType::Ack
			&& p_id.generation_id == 0
			&& p_id.packet_id == 0;

		// Get values from parameters
		let should_encrypt;
		let c_id;
		if let Some(params) = con.params.as_mut() {
			should_encrypt = algs::should_encrypt(p_type, params.voice_encryption);
			c_id = params.c_id;
		} else {
			should_encrypt = algs::should_encrypt(p_type, false);
			if should_encrypt {
				fake_encrypt = true;
			}
			c_id = 0;
		}

		// Client id for clients
		if con.is_client {
			packet.client_id(c_id);
		}

		if !should_encrypt && !fake_encrypt {
			packet.flags(packet.header().flags() | Flags::UNENCRYPTED);
			if let Some(params) = con.params.as_mut() {
				packet.mac().copy_from_slice(&params.shared_mac);
			}
		}

		// Compress and split packet
		let packets = if p_type == PacketType::Command || p_type == PacketType::CommandLow {
			algs::compress_and_split(con.is_client, packet)
		} else {
			// Set the inner packet id for voice packets
			if con.is_client && (p_type == PacketType::Voice || p_type == PacketType::VoiceWhisper)
			{
				(&mut packet.content_mut()[..2])
					.write_be(con.codec.outgoing_p_ids[type_i].packet_id)
					.unwrap();
			}

			vec![packet]
		};

		let packets = packets
			.into_iter()
			.map(|mut packet| -> Result<_> {
				// Get packet id
				let p_id = if p_type == PacketType::Init {
					PartialPacketId { generation_id: 0, packet_id: 0 }
				} else {
					packet.packet_id(con.codec.outgoing_p_ids[type_i].packet_id);
					con.codec.outgoing_p_ids[type_i]
				};

				// Encrypt if necessary
				if fake_encrypt {
					algs::encrypt_fake(&mut packet)?;
				} else if should_encrypt {
					// The params are set
					let params = con.params.as_mut().unwrap();
					algs::encrypt(
						&mut packet,
						p_id.generation_id,
						&params.shared_iv,
						&mut params.key_cache,
					)?;
				}

				// Increment outgoing_p_ids
				let p_id = p_id + 1;
				if p_type != PacketType::Init {
					con.codec.outgoing_p_ids[type_i] = p_id;
				}
				Ok(OutUdpPacket::new(p_id.generation_id, packet))
			})
			.collect::<Result<Vec<_>>>()?;
		Ok(packets)
	}
}