|
ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
|
π¨ Network protocol packet type definitions and structures More...
Data Structures | |
| struct | packet_header_t |
| Network packet header structure. More... | |
| struct | size_packet_t |
| Terminal size update packet. More... | |
| struct | client_info_packet_t |
| Client information packet structure. More... | |
| struct | stream_header_t |
| Stream header packet structure. More... | |
| struct | client_list_packet_t |
| Client list packet structure. More... | |
| struct | server_state_packet_t |
| Server state packet structure. More... | |
| struct | auth_failure_packet_t |
| Authentication failure packet structure. More... | |
| struct | protocol_version_packet_t |
| Protocol version negotiation packet structure (Packet Type 1) More... | |
| struct | ascii_frame_packet_t |
| ASCII frame packet structure (Packet Type 2) More... | |
| struct | image_frame_packet_t |
| Image frame packet structure (Packet Type 3) More... | |
| struct | audio_batch_packet_t |
| Audio batch packet structure (Packet Type 28) More... | |
| struct | crypto_capabilities_packet_t |
| Crypto capabilities packet structure (Packet Type 14) More... | |
| struct | crypto_parameters_packet_t |
| Crypto parameters packet structure (Packet Type 15) More... | |
| struct | terminal_capabilities_packet_t |
| Terminal capabilities packet structure (Packet Type 5) More... | |
Network Protocol Constants | |
| #define | LARGE_PACKET_THRESHOLD (100 * 1024) |
| Large packet size threshold (100KB) | |
| #define | MAX_PACKET_SIZE ((size_t)5 * 1024 * 1024) |
| Maximum packet size (5MB) | |
| #define | MAX_ERROR_MESSAGE_LENGTH 512 |
| Maximum error message length (512 bytes) | |
| #define | MAX_REMOTE_LOG_MESSAGE_LENGTH 512 |
| Maximum remote log message length (512 bytes) | |
Timeout Configuration Constants | |
Timeout constants for handling packets of different sizes. Large packets require extended timeouts for successful transmission. | |
| #define | BASE_SEND_TIMEOUT 5 |
| Base send timeout in seconds (5 seconds) | |
| #define | LARGE_PACKET_EXTRA_TIMEOUT_PER_MB 0.8 |
| Extra timeout per MB for large packets (0.8 seconds per MB) | |
| #define | MIN_CLIENT_TIMEOUT 10 |
| Minimum client timeout in seconds (10 seconds) | |
| #define | MAX_CLIENT_TIMEOUT 60 |
| Maximum client timeout in seconds (60 seconds) | |
Audio Batching Constants | |
Audio batching configuration for efficient audio transmission. Batched audio reduces packet overhead and improves bandwidth usage. | |
| #define | AUDIO_BATCH_COUNT 32 |
| Number of audio chunks per batch (4 chunks) | |
| #define | AUDIO_BATCH_SAMPLES (AUDIO_SAMPLES_PER_PACKET * AUDIO_BATCH_COUNT) |
| Total samples in audio batch (8192 samples) | |
| #define | AUDIO_BATCH_MS 186 |
| Audio batch duration in milliseconds (~186ms) | |
| #define | AUDIO_SAMPLES_PER_PACKET 256 |
| Samples per audio packet (256 samples) | |
Protocol Constants | |
| #define | PACKET_MAGIC 0xDEADBEEF |
| Packet magic number (0xDEADBEEF) | |
Multi-User Protocol Constants | |
| #define | ASCIICHAT_DEFAULT_DISPLAY_NAME "AsciiChatter" |
| Default display name for clients without a custom name. | |
π¨ Network protocol packet type definitions and structures
The packet types system defines all network protocol messages used in ascii-chat. This includes protocol negotiation, media streaming (video/audio), control messages, cryptographic handshake, and multi-user protocol extensions.
Implementation: lib/network/packet_types.h
Key Features:
Packet types are organized into logical categories:
Protocol Negotiation (Type 1):
Media Packets (Types 2-4, 28):
Control Packets (Types 5-7, 12-13):
Multi-User Protocol (Types 8-11):
Crypto Handshake (Types 14-23):
Crypto Rekeying (Types 25-27):
Message Packets (Types 29-33):
Remote logging packets allow the server and client to forward diagnostic log entries to each other over the established connection. They carry the log level, direction hint, optional flags, and a UTF-8 text payload limited to 512 bytes.
Direction values**:
REMOTE_LOG_DIRECTION_SERVER_TO_CLIENT (1) β Sent by the server to a clientREMOTE_LOG_DIRECTION_CLIENT_TO_SERVER (2) β Sent by the client to the server
Flags**:
REMOTE_LOG_FLAG_TRUNCATED β Sender truncated the payload to fit the limitMessages are encrypted automatically once the crypto handshake completes, but fall back to plaintext during handshake so early errors can still be reported. Receivers must always prefix their local log output with a βremoteβ annotation to make the source of the message obvious.
All packets begin with a standard header:
Header fields:
magic: Must be PACKET_MAGIC (0xDEADBEEF) or packet is invalidtype: Packet type enumeration value (packet_type_t, 1-33)length: Payload data length in bytes (0 for header-only packets)crc32: CRC32 checksum of payload data (not including header)client_id: Identifies source (0 for server, 1-9 for clients)Wire format:
Example packet construction:
Critical function for routing crypto handshake packets:
Returns true for:
Returns false for:
PACKET_TYPE_REMOTE_LOG (33) β encrypted when session keys are ready, plaintext otherwise
Usage:
Warning: Encrypting handshake packets will break the crypto handshake. Always check with packet_is_handshake_type() before encryption. Error packets may be sent plaintext prior to handshake completion (or when encryption is disabled), but must be encrypted once a session key is established.
First packet exchanged between client and server:
Version negotiation flow:
Version compatibility:
Complete ASCII art frame with compression support:
Payload structure:
ascii_frame_packet_t (24 bytes)char data[original_size] or char compressed_data[compressed_size]Compression handling:
Raw RGB image from webcam:
Payload structure:
image_frame_packet_t (24 bytes)rgb_pixel_t pixels[width * height] or compressed dataUsage (client β server):
PACKET_TYPE_AUDIO (type 4) is the legacy single-packet format:
float samples[256]Disadvantages:
PACKET_TYPE_AUDIO_BATCH (type 28) batches multiple audio chunks:
Payload structure:
audio_batch_packet_t (16 bytes)float samples[total_samples] (interleaved if stereo)Benefits:
Example:
Client reports terminal capabilities to server:
Usage:
Maintains connection alive and detects dead connections:
All crypto handshake packets (types 14-23, 25-27) must be sent UNENCRYPTED. Use packet_is_handshake_type() to check before encryption.
Phase 1: Capability Exchange
Phase 2: Key Exchange
Phase 3: Authentication
Phase 4: Handshake Complete
Client announces capability to send video/audio:
Server broadcasts current state to all clients:
ascii-chat uses semantic versioning for protocol compatibility:
Version Format:
Compatibility Rules:
protocol_version=1, revision=0protocol_version=1, revision=X (X can be >= 0)Future Protocol Changes:
All multi-byte fields use network byte order (big-endian):
Platform-specific functions:
htonl(), htons(), ntohl(), ntohs()htonl(), htons(), ntohl(), ntohs() (winsock.h)Structures are packed (no padding) for wire format compatibility:
Warning: Packed structures may not be naturally aligned. Accessing fields may cause unaligned memory access (slower on some CPUs). ascii-chat structures are designed to avoid this issue.
Packet Size Limits:
Timeout Configuration:
Audio Configuration:
Magic Numbers:
| #define ASCIICHAT_DEFAULT_DISPLAY_NAME "AsciiChatter" |
| #define AUDIO_BATCH_COUNT 32 |
| #define AUDIO_BATCH_MS 186 |
| #define AUDIO_BATCH_SAMPLES (AUDIO_SAMPLES_PER_PACKET * AUDIO_BATCH_COUNT) |
| #define AUDIO_SAMPLES_PER_PACKET 256 |
| #define BASE_SEND_TIMEOUT 5 |
| #define LARGE_PACKET_EXTRA_TIMEOUT_PER_MB 0.8 |
| #define LARGE_PACKET_THRESHOLD (100 * 1024) |
| #define MAX_CLIENT_TIMEOUT 60 |
| #define MAX_ERROR_MESSAGE_LENGTH 512 |
| #define MAX_PACKET_SIZE ((size_t)5 * 1024 * 1024) |
| #define MAX_REMOTE_LOG_MESSAGE_LENGTH 512 |
#include <packet.h>
Maximum remote log message length (512 bytes)
Remote logging packets mirror the error packet structure but are intended for diagnostic log forwarding between client and server. Limiting the payload size keeps allocations predictable and prevents log flooding over the network.
| #define MIN_CLIENT_TIMEOUT 10 |
| #define PACKET_MAGIC 0xDEADBEEF |
#include <packet.h>
Authentication failure reason flags.
Bitmask enumeration for authentication failure reasons. Multiple flags can be combined to indicate multiple failure causes.
Definition at line 653 of file packet.h.
| enum packet_type_t |
#include <packet.h>
Network protocol packet type enumeration.
Defines all packet types used in the ascii-chat network protocol. Packet types are organized by category: protocol negotiation, frames, audio, control, crypto handshake, multi-user extensions, messages.
PACKET CATEGORIES:
Definition at line 281 of file packet.h.