renetcode2/
lib.rs

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
//! Renetcode is a simple connection based client/server protocol agnostic to the transport layer,
//! was developed be used in games with UDP in mind. Implements the Netcode 1.a2 standard with `renet2`
//! extensions. The standard is available [here][standard] and the original implementation in C++ is
//! available in the [netcode][netcode] repository. The extensions are available in `NETCODE_EXTENSIONS.md`.
//!
//! Has the following feature:
//! - Encrypted and signed packets
//! - Secure client connection with connect tokens
//! - Connection based protocol
//!
//! and protects the game server from the following attacks:
//! - Zombie clients
//! - Man in the middle
//! - DDoS amplification
//! - Packet replay attacks
//!
//! [standard]: https://github.com/networkprotocol/netcode/blob/master/STANDARD.md
//! [netcode]: https://github.com/networkprotocol/netcode
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
mod client;
mod crypto;
mod error;
mod packet;
mod replay_protection;
mod serialize;
mod server;
mod token;

pub use client::{ClientAuthentication, DisconnectReason, NetcodeClient};
pub use crypto::generate_random_bytes;
pub use error::NetcodeError;
pub use packet::{Packet, PacketType};
pub use server::{NetcodeServer, ServerAuthentication, ServerConfig, ServerResult, ServerSocketConfig};
pub use token::{ConnectToken, TokenGenerationError};

use std::time::Duration;

const NETCODE_VERSION_INFO: &[u8; 13] = b"NETCODE 1.a2\0"; //Netcode v1.02 with renet2 extensions (version 'a')
const NETCODE_MAX_CLIENTS: usize = 1024;
const NETCODE_MAX_PENDING_CLIENTS: usize = NETCODE_MAX_CLIENTS * 4;

const NETCODE_ADDRESS_NONE: u8 = 0;
const NETCODE_ADDRESS_IPV4: u8 = 1;
const NETCODE_ADDRESS_IPV6: u8 = 2;

const NETCODE_CONNECT_TOKEN_PRIVATE_BYTES: usize = 1024;
/// The maximum number of bytes that a netcode packet can contain.
pub const NETCODE_MAX_PACKET_BYTES: usize = 1400;
/// The maximum number of bytes that a payload can have when generating a payload packet.
pub const NETCODE_MAX_PAYLOAD_BYTES: usize = 1300;

/// The number of bytes in a private key;
pub const NETCODE_KEY_BYTES: usize = 32;
const NETCODE_MAC_BYTES: usize = 16;
/// The number of bytes that an user data can contain in the ConnectToken.
pub const NETCODE_USER_DATA_BYTES: usize = 256;
const NETCODE_CHALLENGE_TOKEN_BYTES: usize = 300;
const NETCODE_CONNECT_TOKEN_XNONCE_BYTES: usize = 24;

const NETCODE_ADDITIONAL_DATA_SIZE: usize = 13 + 8 + 8;
const NETCODE_SEND_RATE: Duration = Duration::from_millis(250);

/// The tag size of encoded (unencrypted) packets.
const ENCODED_PACKET_TAG_BYTES: usize = 8;