|
ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
|
📡 Shared protocol parsing utilities for server and client packet handlers More...
Files | |
| file | packet_parsing.c |
| 📡 Shared packet parsing utilities implementation | |
| file | packet_parsing.h |
| Shared packet parsing utilities to eliminate duplication between server and client handlers. | |
Macros | |
| #define | PACKET_MAX_FRAME_SIZE (256 * 1024 * 1024) |
| Maximum frame size (256MB) - prevents memory exhaustion attacks. | |
| #define | PACKET_MAX_DIMENSION 32768 |
| Maximum frame dimension (32768x32768) - prevents overflow. | |
Frame Decoding Functions | |
Unified frame data decoding for both compressed and uncompressed formats | |
| char * | packet_decode_frame_data_malloc (const char *frame_data_ptr, size_t frame_data_len, bool is_compressed, uint32_t original_size, uint32_t compressed_size) |
| Decode frame data (malloc version for client handlers) | |
| asciichat_error_t | packet_decode_frame_data_buffer (const char *frame_data_ptr, size_t frame_data_len, bool is_compressed, void *output_buffer, size_t output_size, uint32_t original_size, uint32_t compressed_size) |
| Decode frame data (fixed buffer version for server handlers) | |
Frame Dimension Validation | |
Validation helpers for frame dimensions with overflow checking | |
| asciichat_error_t | packet_validate_frame_dimensions (uint32_t width, uint32_t height, size_t *out_rgb_size) |
| Validate frame dimensions and calculate RGB buffer size. | |
Opus Audio Batch Parsing | |
Helpers for parsing Opus audio batch packets | |
| asciichat_error_t | packet_parse_opus_batch (const void *packet_data, size_t packet_len, const uint8_t **out_opus_data, size_t *out_opus_size, const uint16_t **out_frame_sizes, int *out_sample_rate, int *out_frame_duration, int *out_frame_count) |
| Parse Opus audio batch packet header and extract frame data. | |
📡 Shared protocol parsing utilities for server and client packet handlers
This module provides reusable utilities for parsing and validating protocol packets, used by both server (src/server/protocol.c) and client (src/client/protocol.c) handlers.
Server handlers use these for incoming client packets:
Client handlers use these for incoming server packets:
All integer calculations use safe_size_mul() and overflow checking to prevent buffer overflows from malicious or malformed packets.
| #define PACKET_MAX_DIMENSION 32768 |
#include <packet_parsing.c>
Maximum frame dimension (32768x32768) - prevents overflow.
Definition at line 31 of file packet_parsing.c.
| #define PACKET_MAX_FRAME_SIZE (256 * 1024 * 1024) |
#include <packet_parsing.c>
Maximum frame size (256MB) - prevents memory exhaustion attacks.
Definition at line 25 of file packet_parsing.c.
| asciichat_error_t packet_decode_frame_data_buffer | ( | const char * | frame_data_ptr, |
| size_t | frame_data_len, | ||
| bool | is_compressed, | ||
| void * | output_buffer, | ||
| size_t | output_size, | ||
| uint32_t | original_size, | ||
| uint32_t | compressed_size | ||
| ) |
#include <packet_parsing.h>
Decode frame data (fixed buffer version for server handlers)
Decodes frame data into a pre-allocated fixed-size buffer. Used when buffer allocation is managed separately (e.g., ring buffers).
| frame_data_ptr | Pointer to frame data (compressed or uncompressed) |
| frame_data_len | Actual size of frame_data_ptr buffer |
| is_compressed | True if data is zstd-compressed |
| output_buffer | Pre-allocated output buffer |
| output_size | Size of output_buffer in bytes |
| original_size | Expected decompressed/uncompressed size |
| compressed_size | Expected compressed size (if is_compressed=true) |
Definition at line 89 of file packet_parsing.c.
References ASCIICHAT_OK, decompress_data(), ERROR_BUFFER_FULL, ERROR_COMPRESSION, ERROR_INVALID_PARAM, ERROR_NETWORK_SIZE, log_debug, and SET_ERRNO.
| char * packet_decode_frame_data_malloc | ( | const char * | frame_data_ptr, |
| size_t | frame_data_len, | ||
| bool | is_compressed, | ||
| uint32_t | original_size, | ||
| uint32_t | compressed_size | ||
| ) |
#include <packet_parsing.h>
Decode frame data (malloc version for client handlers)
Handles both compressed (zstd) and uncompressed frame formats. Allocates buffer using SAFE_MALLOC that caller must free. Validates sizes to prevent memory exhaustion attacks.
Frame formats supported:
SIZE VALIDATION:
ERROR HANDLING:
| frame_data_ptr | Pointer to frame data (compressed or uncompressed) |
| frame_data_len | Actual size of frame_data_ptr buffer |
| is_compressed | True if data is zstd-compressed, false if raw |
| original_size | Expected decompressed/uncompressed size in bytes |
| compressed_size | Expected compressed size (validated only if is_compressed=true) |
Definition at line 33 of file packet_parsing.c.
References ASCIICHAT_OK, decompress_data(), ERROR_COMPRESSION, ERROR_MEMORY, ERROR_NETWORK_SIZE, format_bytes_pretty(), log_debug, log_error, PACKET_MAX_FRAME_SIZE, SAFE_FREE, SAFE_MALLOC, and SET_ERRNO.
| asciichat_error_t packet_parse_opus_batch | ( | const void * | packet_data, |
| size_t | packet_len, | ||
| const uint8_t ** | out_opus_data, | ||
| size_t * | out_opus_size, | ||
| const uint16_t ** | out_frame_sizes, | ||
| int * | out_sample_rate, | ||
| int * | out_frame_duration, | ||
| int * | out_frame_count | ||
| ) |
#include <packet_parsing.h>
Parse Opus audio batch packet header and extract frame data.
Parses PACKET_TYPE_AUDIO_OPUS_BATCH packet and extracts metadata and Opus data. The Opus data and frame_sizes pointers point into the packet_data buffer (NOT copied), so packet_data must remain valid while using these outputs.
Packet format:
| packet_data | Packet payload data |
| packet_len | Packet payload length |
| out_opus_data | Output: Pointer to Opus-encoded data within packet (NOT copied) |
| out_opus_size | Output: Size of Opus-encoded data |
| out_frame_sizes | Output: Pointer to frame sizes array within packet (NOT copied) |
| out_sample_rate | Output: Sample rate in Hz |
| out_frame_duration | Output: Frame duration in milliseconds |
| out_frame_count | Output: Number of frames in batch |
Definition at line 173 of file packet_parsing.c.
References ASCIICHAT_OK, ERROR_INVALID_PARAM, ERROR_NETWORK_PROTOCOL, NET_TO_HOST_U32, and SET_ERRNO.
Referenced by handle_audio_opus_batch_packet().
| asciichat_error_t packet_validate_frame_dimensions | ( | uint32_t | width, |
| uint32_t | height, | ||
| size_t * | out_rgb_size | ||
| ) |
#include <packet_parsing.h>
Validate frame dimensions and calculate RGB buffer size.
Performs comprehensive validation of frame dimensions:
OVERFLOW PROTECTION:
| width | Frame width in pixels |
| height | Frame height in pixels |
| out_rgb_size | Output parameter: calculated RGB size (width*height*3) |
Definition at line 130 of file packet_parsing.c.
References ASCIICHAT_OK, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, ERROR_MEMORY, format_bytes_pretty(), PACKET_MAX_DIMENSION, PACKET_MAX_FRAME_SIZE, safe_size_mul, and SET_ERRNO.