GameNetworkingSockets

repository·master·Indexed 27 days ago

https://github.com/valvesoftware/gamenetworkingsockets

A high-performance, secure transport layer for games providing reliable and unreliable message delivery, NAT traversal for P2P, and advanced congestion control. It is an open-source version of the networking API used in the Steamworks SDK, featuring AES-GCM-256 encryption, Curve25519 key exchange, and IPv6 support. The library is written in C++ and provides a plain C interface for language bindings.

Tokens
8.8K
Snippets
13
Records
56
Agent score
94%

What's inside GameNetworkingSockets

  1. Overview of GameNetworkingSockets features

    master

    GameNetworkingSockets is a transport layer designed for games. It provides a connection-oriented API (similar to TCP) but is message-oriented (similar to UDP).

    Key features include:

    • Reliability Layer: Supports both reliable and unreliable message types. It uses an 'ack vector' model (similar to Google QUIC) for efficient retransmission and handles fragmentation/reassembly for messages larger than the MTU.
    • Security: Implements AES-GCM-256 per packet and Curve25519 for key exchange and certificate signatures.
    • Traffic Control: Supports head-of-line blocking control and bandwidth sharing via multiple message 'lanes' using strict priority or weighted fair queueing.
    • P2P & NAT Traversal: Supports peer-to-peer networking with NAT traversal via Google WebRTC's ICE implementation. It includes the ISteamNetworkingMessages interface for porting UDP-based code to P2P.
    • Simulation Tools: Includes tools for simulating packet latency and loss, along with detailed statistics measurement.
    • IPv6 Support: Full support for IPv6.
  2. Understand the SNP Wire Format Frame Structure

    master
    The SNP (Steam Networking Sockets) data payload is a sequence of frames. Each frame begins with an 8-bit field that defines the frame type and flags. The payload is transport-agnostic and does not include packet numbers or transport-specific information. Frames are used to encode unreliable messages, reliable streams, acknowledgments (Acks), and control signals like 'Stop waiting' or 'Select lane'.
  3. P2P Infrastructure Requirements

    master

    To successfully implement P2P connections using this library, you must provide or integrate the following infrastructure:

    1. Signaling Service: A side channel to exchange rendezvous messages (see Signaling Service).
    2. STUN Server(s): Used to help peers discover their public IP addresses and navigate firewalls.
    3. Relay Fallback (TURN): If NAT piercing fails, traffic must be relayed via TURN servers. Note that TURN servers also function as STUN servers.
    4. Identity & Matchmaking: A system to assign identities to hosts, authenticate them, and facilitate matchmaking (this is external to the transport library).
  4. Enable P2P and ICE support

    master

    To use peer-to-peer (P2P) connections with ICE (Interactive Connectivity Establishment) for NAT traversal, ensure ICE is enabled during your build. It is enabled by default, but can be explicitly configured via vcpkg or CMake.

    To use the native ICE client implementation (default):

    • Use the vcpkg feature ice.
    • Or use the CMake flag -DENABLE_ICE=ON.

    To use the Google WebRTC ICE implementation instead of the native one:

    • Use the CMake flag -DUSE_STEAMWEBRTC=ON. This will automatically pull down the required Google WebRTC git submodules.
  5. Install dependencies on macOS using Homebrew

    master

    Use Homebrew to install the required OpenSSL and Protobuf dependencies.

    OpenSSL

    GameNetworkingSockets requires OpenSSL 1.1 or later. If you encounter the error Dependency libcrypto found: NO, you must force the installation of version 1.1.

    Standard installation:

    brew install openssl
    export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opt/openssl/lib/pkgconfig

    If version 1.1 is required:

    brew install openssl@1.1
    export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opt/openssl@1.1/lib/pkgconfig

    Protobuf

    brew install protobuf
  6. Use ed25519-donna for digital signatures

    master

    The ed25519-donna library provides performant, constant-time implementations of the Ed25519 Elliptic Curve Digital Signature Algorithm.

    To use the library, include ed25519.h and link against the compiled object file (e.g., ed25519.o). If using OpenSSL, you must also link against -lssl -lcrypto.

    #include "ed25519.h"
    
    // Generate a private key
    ed25519_secret_key sk;
    randombytes(sk, sizeof(ed25519_secret_key));
    
    // Generate a public key
    ed25519_public_key pk;
    ed25519_publickey(sk, pk);
    
    // Sign a message
    ed25519_signature sig;
    ed25519_sign(message, message_len, sk, pk, signature);
    
    // Verify a signature
    int valid = ed25519_sign_open(message, message_len, pk, signature) == 0;
  7. Run and interpret ed25519-donna fuzzer output

    master

    When running the fuzzer, successful passes result in occasional status dots and a 64-bit progress count (displayed every 0x20000 passes).

    Example successful output:

    fuzzing:  ref10 curved25519 curved25519-sse2
    
    ................................ [0000000000020000]
    ................................ [0000000000040000]

    Handling Errors: If an implementation disagrees with the ref10 implementation, the program will dump:

    1. The random data used (sk for Secret Key, m for Message).
    2. The data generated by the ref10 implementation (pk for Public Key, sig for Signature, and valid status).
    3. Diffs of the ed25519-donna data against the ref10 data.
  8. Compile curve25519-donna

    master

    The curve25519-donna implementation provides performant, portable 32-bit and 64-bit implementations of the Curve25519 elliptic curve. All implementations are constant-time regarding secret data. No configuration is required before compilation.

    To use named versions (e.g., to avoid symbol collisions), define CURVE25519_SUFFIX during compilation. For example, using -DCURVE25519_SUFFIX=_sse2 will result in functions like curve25519_donna_sse2.

    ##### 32-bit
    gcc curve25519.c -m32 -O3 -c
    
    ##### 64-bit
    gcc curve25519.c -m64 -O3 -c
    
    ##### SSE2
    gcc curve25519.c -m32 -O3 -c -DCURVE25519_SSE2 -msse2
    gcc curve25519.c -m64 -O3 -c -DCURVE25519_SSE2
  9. Build GameNetworkingSockets on Windows with Visual Studio and vcpkg

    master

    The recommended way to build on Windows is using vcpkg in manifest mode. These instructions assume vcpkg is installed as a subfolder in your workspace.

    Note: Run these commands from a Visual Studio command prompt so the compiler can be located.

    1. Bootstrap vcpkg

    git clone https://github.com/microsoft/vcpkg
    .\vcpkg\bootstrap-vcpkg.bat

    2. Install Dependencies (Optional Step)

    To install dependencies into your local vcpkg folder before running CMake:

    .\vcpkg\vcpkg install --triplet=x64-windows

    To use the libsodium backend, add the feature flag:

    .\vcpkg\vcpkg install --triplet=x64-windows --x-feature=libsodium

    3. Configure and Build

    To create a minimal build:

    cmake -S . -B build -G Ninja

    To build with examples, tests, and P2P/ICE support (via WebRTC submodule):

    cmake -S . -B build -G Ninja -DBUILD_EXAMPLES=ON -DBUILD_TESTS=ON -DUSE_STEAMWEBRTC=ON

    Finally, compile the projects:

    cd build
    ninja
    cmake -S . -B build -G Ninja -DBUILD_EXAMPLES=ON -DBUILD_TESTS=ON -DUSE_STEAMWEBRTC=ON
    cd build
    ninja