103#define LARGE_PACKET_THRESHOLD (100 * 1024)
113#define MAX_PACKET_SIZE ((size_t)5 * 1024 * 1024)
122#define MAX_ERROR_MESSAGE_LENGTH 512
132#define MAX_REMOTE_LOG_MESSAGE_LENGTH 512
153#define BASE_SEND_TIMEOUT 5
163#define LARGE_PACKET_EXTRA_TIMEOUT_PER_MB 0.8
173#define MIN_CLIENT_TIMEOUT 10
183#define MAX_CLIENT_TIMEOUT 60
204#define AUDIO_BATCH_COUNT 32
214#define AUDIO_BATCH_SAMPLES (AUDIO_SAMPLES_PER_PACKET * AUDIO_BATCH_COUNT)
223#define AUDIO_BATCH_MS 186
233#define AUDIO_SAMPLES_PER_PACKET 256
251#define PACKET_MAGIC 0xDEADBEEF
433static inline bool packet_is_handshake_type(
packet_type_t type) {
464static inline bool packet_is_precompressed(
packet_type_t type) {
517#define ASCIICHAT_DEFAULT_DISPLAY_NAME "AsciiChatter"
628#define REMOTE_LOG_FLAG_TRUNCATED 0x0001U
694#define FEATURE_RLE_ENCODING 0x01
695#define FEATURE_DELTA_FRAMES 0x02
815#define CLIENT_CAP_VIDEO 0x01
816#define CLIENT_CAP_AUDIO 0x02
817#define CLIENT_CAP_COLOR 0x04
818#define CLIENT_CAP_STRETCH 0x08
829#define STREAM_TYPE_VIDEO 0x01
830#define STREAM_TYPE_AUDIO 0x02
933 char palette_custom[64];
949#define KEX_ALGO_X25519 0x01
950#define AUTH_ALGO_ED25519 0x01
951#define AUTH_ALGO_NONE 0x00
952#define CIPHER_ALGO_XSALSA20_POLY1305 0x01
1282 const char *message);
1295 char *message_buffer,
size_t message_buffer_size,
1296 size_t *out_message_length);
1325 char *message_buffer,
size_t message_buffer_size,
size_t *out_message_length);
1413 const uint16_t *frame_sizes,
int sample_rate,
int frame_duration,
🔌 Cross-platform abstraction layer umbrella header for ascii-chat
asciichat_error_t error_code
asciichat_error_t
Error and exit codes - unified status values (0-255)
#define MAX_CLIENTS
Maximum possible clients (static array size) - actual runtime limit set by –max-clients (1-32)
#define MAX_DISPLAY_NAME_LEN
Maximum display name length in characters.
enum remote_log_direction remote_log_direction_t
Remote log packet direction enumeration.
log_level_t
Logging levels enumeration.
uint32_t pixel_format
Pixel format enum (0=RGB24, 1=RGBA32, 2=BGR24, etc.)
uint8_t hmac_size
HMAC size in bytes (e.g., 32 for HMAC-SHA256)
asciichat_error_t send_packet_secure(socket_t sockfd, packet_type_t type, const void *data, size_t len, crypto_context_t *crypto_ctx)
Send a packet with encryption and compression support.
uint8_t preferred_auth
Preferred authentication algorithm (AUTH_ALGO_*)
uint32_t capabilities
Terminal capabilities bitmask (TERM_CAP_* flags)
uint32_t color_level
Color level enum value (terminal_color_mode_t)
uint16_t supported_auth_algorithms
Supported authentication algorithms bitmask (AUTH_ALGO_*)
uint32_t timestamp
Timestamp when frame was captured (milliseconds since epoch)
uint32_t color_count
Actual color count (16, 256, or 16777216)
uint8_t selected_kex
Selected key exchange algorithm (KEX_ALGO_*)
uint32_t magic
Magic number (PACKET_MAGIC) for packet validation.
uint16_t flags
Additional flags (REMOTE_LOG_FLAG_*)
uint16_t supported_kex_algorithms
Supported key exchange algorithms bitmask (KEX_ALGO_*)
asciichat_error_t packet_parse_remote_log(const void *data, size_t len, log_level_t *out_level, remote_log_direction_t *out_direction, uint16_t *out_flags, char *message_buffer, size_t message_buffer_size, size_t *out_message_length)
Parse a remote log packet payload into components.
uint32_t message_length
Message payload length in bytes (0-512)
uint8_t log_level
Log level associated with the message (log_level_t cast to uint8_t)
uint32_t width
Terminal width in characters.
uint32_t client_count
Number of clients in the list (0 to MAX_CLIENTS)
asciichat_error_t packet_send_error(socket_t sockfd, const crypto_context_t *crypto_ctx, asciichat_error_t error_code, const char *message)
Send an error packet with optional encryption context.
uint8_t preferred_cipher
Preferred cipher algorithm (CIPHER_ALGO_*)
uint32_t client_id
Client ID (0 = server, >0 = client identifier)
asciichat_error_t packet_validate_crc32(const void *data, size_t len, uint32_t expected_crc)
Validate packet CRC32 checksum.
asciichat_error_t packet_send_remote_log(socket_t sockfd, const crypto_context_t *crypto_ctx, log_level_t level, remote_log_direction_t direction, uint16_t flags, const char *message)
Send a remote log packet with optional encryption context.
uint16_t height
Terminal height in characters.
uint8_t selected_cipher
Selected cipher algorithm (CIPHER_ALGO_*)
uint32_t message_length
Length of message payload in bytes (0-512)
asciichat_error_t send_image_frame_packet(socket_t sockfd, const void *image_data, uint16_t width, uint16_t height, uint8_t format)
Send image frame packet.
uint8_t compression_threshold
Compression threshold percentage (0-100, e.g., 80 = compress if >80% size reduction)
uint32_t active_client_count
Number of clients actively sending video/audio streams.
uint16_t auth_public_key_size
Authentication public key size in bytes (e.g., 32 for Ed25519, 1952 for Dilithium3)
uint32_t length
Payload data length in bytes (0 for header-only packets)
uint32_t error_code
Error code from asciichat_error_t enumeration.
void * allocated_buffer
Buffer that needs to be freed by caller (may be NULL if not allocated)
uint8_t selected_auth
Selected authentication algorithm (AUTH_ALGO_*)
uint32_t timestamp
Timestamp when frame/audio was captured (milliseconds since epoch)
uint32_t checksum
CRC32 checksum of original ASCII data.
int send_protocol_version_packet(socket_t sockfd, const protocol_version_packet_t *version)
Send protocol version negotiation packet.
int send_pong_packet(socket_t sockfd)
Send a pong packet (keepalive response)
uint32_t original_size
Size of original uncompressed ASCII data in bytes.
uint32_t height
Terminal height in characters.
uint8_t reason_flags
Bitmask of auth_failure_reason_t values indicating failure causes.
uint32_t connected_client_count
Total number of currently connected clients.
asciichat_error_t send_audio_batch_packet(socket_t sockfd, const float *samples, int num_samples, int batch_count, crypto_context_t *crypto_ctx)
Send a batched audio packet with encryption support.
asciichat_error_t packet_receive(socket_t sockfd, packet_type_t *type, void **data, size_t *len)
Receive a packet with header validation and CRC32 checking.
uint8_t verification_enabled
Server verification enabled flag (1=enabled, 0=disabled)
uint32_t height
Terminal height in characters.
uint16_t protocol_revision
Minor protocol revision (server can be newer)
uint32_t compressed_size
Size of compressed data (0 = not compressed)
uint16_t protocol_version
Major protocol version (must match for compatibility)
asciichat_error_t packet_parse_error_message(const void *data, size_t len, asciichat_error_t *out_error_code, char *message_buffer, size_t message_buffer_size, size_t *out_message_length)
Parse an error packet payload into components.
uint16_t supported_cipher_algorithms
Supported cipher algorithms bitmask (CIPHER_ALGO_*)
uint16_t signature_size
Signature size in bytes (e.g., 64 for Ed25519, 3309 for Dilithium3)
int receive_packet(socket_t sockfd, packet_type_t *type, void **data, size_t *len)
Receive a basic packet without encryption.
asciichat_error_t send_ascii_frame_packet(socket_t sockfd, const char *frame_data, size_t frame_size)
Send ASCII frame packet.
uint32_t stream_type
Stream type bitmask (STREAM_TYPE_VIDEO | STREAM_TYPE_AUDIO)
asciichat_error_t av_send_audio_opus_batch(socket_t sockfd, const uint8_t *opus_data, size_t opus_size, const uint16_t *frame_sizes, int sample_rate, int frame_duration, int frame_count, crypto_context_t *crypto_ctx)
Send Opus-encoded audio batch packet with encryption support.
size_t allocated_size
Size of allocated buffer in bytes.
size_t len
Length of payload data in bytes.
int send_packet(socket_t sockfd, packet_type_t type, const void *data, size_t len)
Send a basic packet without encryption.
asciichat_error_t packet_validate_header(const packet_header_t *header, uint16_t *pkt_type, uint32_t *pkt_len, uint32_t *expected_crc)
Validate packet header and extract information.
uint32_t render_mode
Render mode enum value (foreground/background/half-block)
uint16_t feature_flags
Feature flags bitmask (FEATURE_RLE_ENCODING, etc.)
void * data
Packet payload data (decrypted and decompressed if applicable)
asciichat_error_t packet_send(socket_t sockfd, packet_type_t type, const void *data, size_t len)
Send a packet with header and CRC32 checksum.
uint8_t preferred_kex
Preferred key exchange algorithm (KEX_ALGO_*)
uint8_t nonce_size
Nonce size in bytes (e.g., 24 for XSalsa20)
uint32_t compressed_size
Compressed data size (0 = not compressed, >0 = compressed)
uint16_t width
Terminal width in characters.
packet_recv_result_t receive_packet_secure(socket_t sockfd, void *crypto_ctx, bool enforce_encryption, packet_envelope_t *envelope)
Receive a packet with decryption and decompression support.
uint16_t type
Packet type (packet_type_t enumeration)
int send_crypto_capabilities_packet(socket_t sockfd, const crypto_capabilities_packet_t *caps)
Send crypto capabilities packet.
uint32_t checksum
CRC32 checksum of pixel data.
uint8_t desired_fps
Client's desired frame rate (1-144 FPS)
uint32_t client_id
Unique client identifier (1-9, 0 = server)
uint8_t requires_verification
Server verification requirement flag (1=required, 0=optional)
uint32_t channels
Number of audio channels (1=mono, 2=stereo)
int send_clear_console_packet(socket_t sockfd)
Send a clear console packet.
uint8_t detection_reliable
Detection reliability flag (1=reliable detection, 0=best guess)
uint8_t direction
Direction hint so receivers can annotate origin.
int send_ping_packet(socket_t sockfd)
Send a ping packet (keepalive)
uint32_t height
Image height in pixels.
packet_type_t type
Packet type (from packet_types.h)
uint32_t batch_count
Number of audio chunks in this batch (usually AUDIO_BATCH_COUNT = 32)
uint32_t width
Image width in pixels.
uint32_t flags
Frame flags bitmask (HAS_COLOR, IS_COMPRESSED, etc.)
uint32_t client_id
Client ID that this stream originates from (1-9)
uint32_t sample_rate
Sample rate in Hz (e.g., 44100, 48000)
uint32_t total_samples
Total audio samples across all chunks (typically 8192)
uint8_t compression_algorithms
Supported compression algorithms bitmask (COMPRESS_ALGO_*)
uint32_t width
Terminal width in characters.
bool was_encrypted
True if packet was encrypted (decrypted before envelope creation)
uint32_t palette_type
Palette type enum value (palette_type_t)
int send_crypto_parameters_packet(socket_t sockfd, const crypto_parameters_packet_t *params)
Send crypto parameters packet.
uint32_t capabilities
Client capabilities bitmask (CLIENT_CAP_VIDEO | CLIENT_CAP_AUDIO | CLIENT_CAP_COLOR | CLIENT_CAP_STRE...
uint8_t mac_size
MAC size in bytes (e.g., 16 for Poly1305)
uint16_t shared_secret_size
Shared secret size in bytes (e.g., 32 for X25519)
uint16_t kex_public_key_size
Key exchange public key size in bytes (e.g., 32 for X25519, 1568 for Kyber1024)
uint32_t crc32
CRC32 checksum of payload data (0 if length == 0)
packet_recv_result_t
Packet reception result codes.
uint32_t utf8_support
UTF-8 support flag (0=no UTF-8, 1=UTF-8 supported)
uint8_t supports_encryption
Encryption support flag (1=support encryption, 0=plaintext only)
@ PACKET_RECV_ERROR
Network error occurred.
@ PACKET_RECV_EOF
Connection closed (EOF)
@ PACKET_RECV_SECURITY_VIOLATION
Encryption policy violation (e.g., unencrypted packet when encryption required)
@ PACKET_RECV_SUCCESS
Packet received successfully.
auth_failure_reason_t
Authentication failure reason flags.
packet_type_t
Network protocol packet type enumeration.
@ AUTH_FAIL_PASSWORD_INCORRECT
Password verification failed (incorrect password)
@ AUTH_FAIL_PASSWORD_REQUIRED
Server requires password but client didn't provide one.
@ AUTH_FAIL_CLIENT_KEY_REQUIRED
Server requires client key but client didn't provide one.
@ AUTH_FAIL_CLIENT_KEY_REJECTED
Client key not in whitelist (access denied)
@ AUTH_FAIL_SIGNATURE_INVALID
Client signature verification failed (invalid signature)
@ PACKET_TYPE_ACIP_WEBRTC_ICE
WebRTC ICE candidate (bidirectional)
@ PACKET_TYPE_AUDIO_OPUS_BATCH
Batched Opus-encoded audio frames.
@ PACKET_TYPE_AUDIO_OPUS
Opus-encoded single audio frame.
@ PACKET_TYPE_CLIENT_LEAVE
Clean disconnect notification.
@ PACKET_TYPE_ACIP_SESSION_INFO
Session info response (Discovery Server -> Client)
@ PACKET_TYPE_IMAGE_FRAME
Complete RGB image with dimensions.
@ PACKET_TYPE_CRYPTO_AUTH_RESPONSE
Client -> Server: {HMAC[32]} (UNENCRYPTED)
@ PACKET_TYPE_TEXT_MESSAGE
Text message.
@ PACKET_TYPE_AUDIO_MESSAGE
Audio message.
@ PACKET_TYPE_PONG
Keepalive pong response.
@ PACKET_TYPE_ACIP_SESSION_RECONNECT
Reconnect to session (Client -> Discovery Server)
@ PACKET_TYPE_CRYPTO_HANDSHAKE_COMPLETE
Server -> Client: "encryption ready" (UNENCRYPTED)
@ PACKET_TYPE_STREAM_START
Client requests to start sending video/audio.
@ PACKET_TYPE_CRYPTO_KEY_EXCHANGE_INIT
Server -> Client: {server_pubkey[32]} (UNENCRYPTED)
@ PACKET_TYPE_ACIP_ERROR
Generic error response (Discovery Server -> Client)
@ PACKET_TYPE_AUDIO
Single audio packet (legacy)
@ PACKET_TYPE_CRYPTO_KEY_EXCHANGE_RESP
Client -> Server: {client_pubkey[32]} (UNENCRYPTED)
@ PACKET_TYPE_CRYPTO_REKEY_COMPLETE
Initiator -> Responder: Empty (encrypted with NEW key, but still handshake)
@ PACKET_TYPE_SERVER_STATE
Server sends current state to clients.
@ PACKET_TYPE_ACIP_STRING_RESERVE
Reserve session string (Client -> Discovery Server)
@ PACKET_TYPE_CRYPTO_AUTH_FAILED
Server -> Client: "authentication failed" (UNENCRYPTED)
@ PACKET_TYPE_ENCRYPTED
Encrypted packet (after handshake completion)
@ PACKET_TYPE_CRYPTO_REKEY_REQUEST
Initiator -> Responder: {new_ephemeral_pk[32]} (UNENCRYPTED during rekey)
@ PACKET_TYPE_SIZE_MESSAGE
Terminal size message.
@ PACKET_TYPE_REMOTE_LOG
Bidirectional remote logging packet.
@ PACKET_TYPE_CRYPTO_SERVER_AUTH_RESP
Server -> Client: {HMAC[32]} server proves knowledge (UNENCRYPTED)
@ PACKET_TYPE_PROTOCOL_VERSION
Protocol version and capabilities negotiation.
@ PACKET_TYPE_CLEAR_CONSOLE
Server tells client to clear console.
@ PACKET_TYPE_CRYPTO_NO_ENCRYPTION
Client -> Server: "I want to proceed without encryption" (UNENCRYPTED)
@ PACKET_TYPE_ACIP_SESSION_CREATE
Create new session (Client -> Discovery Server)
@ PACKET_TYPE_ACIP_STRING_RESERVED
String reserved response (Discovery Server -> Client)
@ PACKET_TYPE_ACIP_DISCOVERY_PING
Discovery server ping (keepalive)
@ PACKET_TYPE_CRYPTO_AUTH_CHALLENGE
Server -> Client: {nonce[32]} (UNENCRYPTED)
@ PACKET_TYPE_ASCII_FRAME
Complete ASCII frame with all metadata.
@ PACKET_TYPE_CRYPTO_PARAMETERS
Server -> Client: Chosen algorithms + data sizes (UNENCRYPTED)
@ PACKET_TYPE_ACIP_SESSION_LOOKUP
Lookup session by string (Client -> Discovery Server)
@ PACKET_TYPE_CRYPTO_CAPABILITIES
Client -> Server: Supported crypto algorithms (UNENCRYPTED)
@ PACKET_TYPE_ACIP_SESSION_LEAVE
Leave session (Client -> Discovery Server)
@ PACKET_TYPE_ACIP_WEBRTC_SDP
WebRTC SDP offer/answer (bidirectional)
@ PACKET_TYPE_ACIP_SESSION_JOIN
Join existing session (Client -> Discovery Server)
@ PACKET_TYPE_ACIP_SESSION_CREATED
Session created response (Discovery Server -> Client)
@ PACKET_TYPE_ACIP_STRING_RELEASE
Release string reservation (Client -> Discovery Server)
@ PACKET_TYPE_CLIENT_JOIN
Client announces capability to send media.
@ PACKET_TYPE_CLIENT_CAPABILITIES
Client reports terminal capabilities.
@ PACKET_TYPE_ACIP_STRING_RENEW
Renew string reservation (Client -> Discovery Server)
@ PACKET_TYPE_ERROR_MESSAGE
Error packet with asciichat_error_t code and human-readable message.
@ PACKET_TYPE_PING
Keepalive ping packet.
@ PACKET_TYPE_AUDIO_BATCH
Batched audio packets for efficiency.
@ PACKET_TYPE_CRYPTO_REKEY_RESPONSE
Responder -> Initiator: {new_ephemeral_pk[32]} (UNENCRYPTED during rekey)
@ PACKET_TYPE_ACIP_SESSION_JOINED
Session joined response (Discovery Server -> Client)
@ PACKET_TYPE_STREAM_STOP
Client stops sending media.
@ PACKET_TYPE_ACIP_SESSION_END
End session (Host -> Discovery Server)
📝 Logging API with multiple log levels and terminal output control
Cross-platform socket interface for ascii-chat.
ASCII frame packet structure (Packet Type 2)
Audio batch packet structure (Packet Type 28)
Authentication failure packet structure.
Client information packet structure.
Client list packet structure.
Crypto capabilities packet structure (Packet Type 14)
Cryptographic context structure.
Crypto parameters packet structure (Packet Type 15)
Error packet structure carrying error code and textual description.
Image frame packet structure (Packet Type 3)
Packet envelope containing received packet data.
Protocol version negotiation packet structure (Packet Type 1)
Remote log packet structure carrying log level and message text.
Server state packet structure.
Terminal size update packet.
Terminal capabilities packet structure (Packet Type 5)