Skip to content

Binary Protocol Migration

Implementation Status: ✅ IMPLEMENTED

The binary protocol has been fully implemented in rvpn-core/src/protocol/.

Current State: Binary Protocol (IMPLEMENTED)

The protocol now uses a custom binary format for all message serialization:

// Binary message framing: [4 bytes length][N bytes payload]
// Implemented in rvpn-core/src/protocol/codec.rs

Message Format

All messages use a simple 4-byte length prefix:

[4 bytes: message length (big-endian)] [N bytes: message payload]

Message Types

0x01: ProxyConnect

[1 byte: type = 0x01]
[8 bytes: connection_id (big-endian u64)]
[1 byte: host length]
[N bytes: host (UTF-8)]
[2 bytes: port (big-endian u16)]

0x02: ProxyResponse

[1 byte: type = 0x02]
[8 bytes: connection_id (big-endian u64)]
[1 byte: success (0x00 = false, 0x01 = true)]
[2 bytes: error length (0 if no error)]
[N bytes: error message (UTF-8, optional)]

0x03: ProxyData

[1 byte: type = 0x03]
[8 bytes: connection_id (big-endian u64)]
[4 bytes: data length (big-endian u32)]
[N bytes: data (raw binary)]
[1 byte: close flag (0x00 = false, 0x01 = true)]

0x04: RatchetMessage

[1 byte: type = 0x04]
[1 byte: has_dh_public (0x00 = false, 0x01 = true)]
[32 bytes: dh_public (only if has_dh_public = 0x01)]
[4 bytes: message_number (big-endian u32)]
[4 bytes: previous_chain_length (big-endian u32)]
[12 bytes: nonce (raw)]
[4 bytes: ciphertext length (big-endian u32)]
[N bytes: ciphertext (raw binary)]

Size Comparison

ProxyData Example (1KB payload)

Format Size Overhead
JSON ~1,400 bytes Base64 + field names
Binary ~1,020 bytes 20 byte header
Savings ~27%

RatchetMessage Example (1KB ciphertext)

Format Size Overhead
JSON ~1,500 bytes Base64 + field names + struct wrapping
Binary ~1,060 bytes 48 byte header
Savings ~29%

Implementation Details

Files Modified

  1. rvpn-core/src/protocol/mod.rs - Added codec module
  2. rvpn-core/src/protocol/codec.rs - Binary codec implementation
  3. rvpn-server/src/handler.rs - Uses binary codec
  4. rvpn-client/src/tunnel.rs - Uses binary codec
  5. rvpn-client/src/socks5.rs - Uses binary codec

Benefits Achieved

  • Bandwidth: ~30% reduction in message sizes
  • Latency: Faster serialization/deserialization
  • CPU: Eliminated base64 encoding/decoding overhead
  • Memory: More predictable buffer sizes