netcode is a secure connection-oriented client/server protocol for multiplayer games built on top of UDP.
Real-time multiplayer games typically use UDP instead of TCP, because they are latency sensitive and typically need the most recent state, and TCP holds the most recent state hostage while waiting for older dropped packets to be retransmitted.
The problem is that UDP doesn't provide any concept of connection, so you want to use UDP you have to to build up your own client connection system, timeouts, sequence numbers and security all by yourself.
netcode fixes this by providing the simplest possible secure connection-oriented approach on top of UDP so you can quickly exchange unreliable unordered packets, without having to build your own connection oriented protocol.
The netcode server manages n slots for clients to connect to, while providing security features like encrypted and signed packets, protection against packet replay attacks, and a novel 'connect token' system that only allows authenticated clients to connect to your server.
Building and testing all these features yourself on top of UDP is complex and error prone, and frankly take a lot of time to do properly, so if you are thinking of building your own game network protocol from scratch, netcode can be a good starting point and save you a lot of time.
- Secure client connection with connect tokens. Only clients you authorize can connect to your server. This is perfect for a game where you perform matchmaking in a web backend then send clients to connect to a server.
- Client slot system. Servers have n slots for clients. Client are assigned to a slot when they connect to the server and are quickly denied connection if all slots are taken.
- Fast clean disconnect on client or server side of connection to quickly open up the slot for a new client, plus timeouts for hard disconnects.
- Encrypted and signed packets. Packets cannot be tampered with or read by parties not involved in the connection. Cryptography is performed by the excellent sodium library.
- Many security features including protection protection against maliciously crafted packets, packet replay attacks and packet amplification attacks.
- Support for packet tagging which can significantly reduce jitter on Wi-Fi routers. Read this article for more details.
Start by generating a random 32 byte private key. Do not share your private key with anybody.
Especially, do not include your private key in your client executable!
Here is a test private key:
static uint8_t private_key[NETCODE_KEY_BYTES] = { 0x60, 0x6a, 0xbe, 0x6e, 0xc9, 0x19, 0x10, 0xea,
0x9a, 0x65, 0x62, 0xf6, 0x6f, 0x2b, 0x30, 0xe4,
0x43, 0x71, 0xd6, 0x2c, 0xd1, 0x99, 0x27, 0x26,
0x6b, 0x3c, 0x60, 0xf4, 0xb7, 0x15, 0xab, 0xa1 };
Create a server with the private key:
char * server_address = "127.0.0.1:40000";
struct netcode_server_config_t server_config;
netcode_default_server_config( &server_config );
memcpy( &server_config.private_key, private_key, NETCODE_KEY_BYTES );
struct netcode_server_t * server = netcode_server_create( server_address, &server_config, time );
if ( !server )
{
printf( "error: failed to create server\n" );
return 1;
}
Then start the server with the number of client slots you want:
netcode_server_start( server, 16 );
To connect a client, your client should hit a REST API to your backend that returns a connect token.
Using a connect token secures your server so that only clients authorized with your backend can connect.
netcode_client_connect( client, connect_token );
Once the client connects to the server, the client is assigned a client index and can exchange encrypted and signed packets with the server.
For more details please see client.c and server.c
This repository holds the implementation of netcode in C.
Other netcode implementations include:
- netcode C# implementation
- netcode Golang implementation
- netcode Rust implementation (updated fork of vvanders/netcode.io)
- netcode Rust implementation (new from scratch Rust implementation)
- netcode for Unity
- netcode for UE4
- netcode for Typescript
If you'd like to create your own implementation of netcode, please read the netcode 1.02 standard.
These people are awesome:
- Val Vanders - Rust Implementation
- Walter Pearce - Rust Implementation
- Isaac Dawson - Golang Implementation
- Alan Stagner - Unity integration, C# implementation
- Jérôme Leclercq - Support for random connect token nonce
- Randy Gaul - Discovered vulnerability in replay protection
- Benny Chen - Typescript Implementation
- Benny Nazimov - Rust implementation
Thanks for your contributions to netcode!
The author of this library is Glenn Fiedler.
Other open source libraries by the same author include: reliable, serialize, and yojimbo.
If you find this software useful, please consider sponsoring it. Thanks!