Skip to main content

netcode/
lib.rs

1/*
2    netcode
3
4    Copyright © 2017 - 2026, Más Bandwidth LLC
5
6    Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7
8        1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9
10        2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
11           in the documentation and/or other materials provided with the distribution.
12
13        3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
14           from this software without specific prior written permission.
15
16    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
22    USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*/
24
25//! **netcode** is a secure client/server protocol for multiplayer games built on top of UDP.
26//!
27//! This crate is a Rust implementation of the [netcode 1.02 standard]. It interoperates with
28//! the [reference C implementation] and other conforming implementations.
29//!
30//! [netcode 1.02 standard]: https://github.com/mas-bandwidth/netcode/blob/main/STANDARD.md
31//! [reference C implementation]: https://github.com/mas-bandwidth/netcode
32//!
33//! # Overview
34//!
35//! A web backend authenticates each client and hands it a [connect token] over HTTPS. The client
36//! uses the connect token to establish a connection with a dedicated server over UDP. Once
37//! connected, the client and server exchange encrypted and signed packets.
38//!
39//! [connect token]: generate_connect_token
40//!
41//! # Example
42//!
43//! ```no_run
44//! use std::net::SocketAddr;
45//!
46//! let private_key = netcode::generate_key();
47//! let protocol_id = 0x1122334455667788;
48//!
49//! let server_address: SocketAddr = "127.0.0.1:40000".parse().unwrap();
50//! let mut server = netcode::Server::new(server_address, protocol_id, &private_key, 0.0).unwrap();
51//! server.start(16).unwrap();
52//!
53//! let client_address: SocketAddr = "0.0.0.0:0".parse().unwrap();
54//! let mut client = netcode::Client::new(client_address, 0.0).unwrap();
55//!
56//! let client_id = 1234;
57//! let user_data = [0u8; netcode::USER_DATA_BYTES];
58//! let connect_token = netcode::generate_connect_token(
59//!     &[server_address],
60//!     &[server_address],
61//!     30,
62//!     5,
63//!     client_id,
64//!     protocol_id,
65//!     &private_key,
66//!     &user_data,
67//! )
68//! .unwrap();
69//!
70//! client.connect(&connect_token).unwrap();
71//!
72//! let mut time = 0.0;
73//! loop {
74//!     client.update(time);
75//!     server.update(time);
76//!     if client.state() == netcode::ClientState::Connected {
77//!         client.send_packet(&[1, 2, 3, 4]).unwrap();
78//!     }
79//!     while let Some((payload, _sequence)) = server.receive_packet(0) {
80//!         println!("server received {} byte packet", payload.len());
81//!     }
82//!     std::thread::sleep(std::time::Duration::from_secs_f64(1.0 / 60.0));
83//!     time += 1.0 / 60.0;
84//! }
85//! ```
86
87#![forbid(unsafe_code)]
88
89mod bytes;
90mod client;
91mod crypto;
92mod error;
93mod packet;
94mod replay;
95mod server;
96mod socket;
97mod token;
98
99#[cfg(fuzzing)]
100pub mod fuzzing;
101
102#[cfg(test)]
103mod wire_compat;
104
105pub use client::{Client, ClientState};
106pub use crypto::generate_key;
107pub use error::Error;
108pub use server::{DisconnectReason, Server, ServerEvent};
109pub use token::generate_connect_token;
110
111/// The size of a connect token in bytes.
112pub const CONNECT_TOKEN_BYTES: usize = 2048;
113
114/// The size of an encryption key in bytes.
115pub const KEY_BYTES: usize = 32;
116
117/// The size of an AEAD authentication tag (HMAC) in bytes.
118pub const MAC_BYTES: usize = 16;
119
120/// The size of the per-client user data block carried in connect tokens.
121pub const USER_DATA_BYTES: usize = 256;
122
123/// The maximum number of server addresses in a connect token.
124pub const MAX_SERVERS_PER_CONNECT: usize = 32;
125
126/// The maximum number of client slots on a server.
127pub const MAX_CLIENTS: usize = 256;
128
129/// The maximum size of a payload packet in bytes.
130pub const MAX_PAYLOAD_BYTES: usize = 1200;
131
132/// A 256-bit encryption key.
133pub type Key = [u8; KEY_BYTES];
134
135/// User data carried from the connect token to the server, opaque to netcode.
136pub type UserData = [u8; USER_DATA_BYTES];
137
138pub(crate) const VERSION_INFO: [u8; 13] = *b"NETCODE 1.02\0";
139pub(crate) const MAX_PACKET_BYTES: usize = 1300;
140pub(crate) const PACKET_SEND_RATE: f64 = 10.0;
141pub(crate) const NUM_DISCONNECT_PACKETS: usize = 10;
142pub(crate) const PACKET_QUEUE_SIZE: usize = 256;