27 SAFE_STRNCPY(error.error_message, message,
sizeof(error.error_message));
39 socket_t client_socket,
const char *operation_name) {
45 log_warn(
"Rate limit exceeded for %s from %s", operation_name, client_ip);
58 const char *packet_name;
60 switch (packet_type) {
63 packet_name =
"IMAGE_FRAME";
71 packet_name =
"AUDIO";
82 packet_name =
"CLIENT_JOIN";
90 packet_name =
"CONTROL";
asciichat_error_t error_code
bool check_and_record_rate_limit(rate_limiter_t *rate_limiter, const char *client_ip, rate_event_type_t event_type, socket_t client_socket, const char *operation_name)
Check rate limit and send error if exceeded.
asciichat_error_t send_error_packet(socket_t sockfd, asciichat_error_t error_code)
Send an ACIP error packet using asciichat_error_t.
bool check_and_record_packet_rate_limit(rate_limiter_t *rate_limiter, const char *client_ip, socket_t client_socket, packet_type_t packet_type)
Map packet type to rate event type and check rate limit.
asciichat_error_t send_error_packet_message(socket_t sockfd, asciichat_error_t error_code, const char *message)
Send an ACIP error packet with custom message.
Network error handling utilities.
#define SAFE_STRNCPY(dst, src, size)
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it.
asciichat_error_t
Error and exit codes - unified status values (0-255)
#define log_warn(...)
Log a WARN message.
int send_packet(socket_t sockfd, packet_type_t type, const void *data, size_t len)
Send a basic packet without encryption.
packet_type_t
Network protocol packet type enumeration.
@ 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_IMAGE_FRAME
Complete RGB image with dimensions.
@ PACKET_TYPE_PONG
Keepalive pong response.
@ PACKET_TYPE_STREAM_START
Client requests to start sending video/audio.
@ PACKET_TYPE_ACIP_ERROR
Generic error response (Discovery Server -> Client)
@ PACKET_TYPE_AUDIO
Single audio packet (legacy)
@ PACKET_TYPE_CLIENT_JOIN
Client announces capability to send media.
@ PACKET_TYPE_CLIENT_CAPABILITIES
Client reports terminal capabilities.
@ PACKET_TYPE_PING
Keepalive ping packet.
@ PACKET_TYPE_AUDIO_BATCH
Batched audio packets for efficiency.
@ PACKET_TYPE_STREAM_STOP
Client stops sending media.
📝 Logging API with multiple log levels and terminal output control
🌐 Core network I/O operations with timeout support
asciichat_error_t rate_limiter_record(rate_limiter_t *limiter, const char *ip_address, rate_event_type_t event_type)
Record a rate limit event.
asciichat_error_t rate_limiter_check(rate_limiter_t *limiter, const char *ip_address, rate_event_type_t event_type, const rate_limit_config_t *config, bool *allowed)
Check if an event from an IP address should be rate limited.
rate_event_type_t
Rate limit event types.
@ RATE_EVENT_CONTROL
Control packets (CAPABILITIES, STREAM_START/STOP, LEAVE)
@ RATE_EVENT_PING
Ping/pong keepalive (PACKET_TYPE_PING, PACKET_TYPE_PONG)
@ RATE_EVENT_AUDIO
Audio packet (PACKET_TYPE_AUDIO, PACKET_TYPE_AUDIO_BATCH)
@ RATE_EVENT_IMAGE_FRAME
Image frame from client (PACKET_TYPE_IMAGE_FRAME)
@ RATE_EVENT_CLIENT_JOIN
Client join request (PACKET_TYPE_CLIENT_JOIN)