ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
errors.h File Reference

Network error handling utilities. More...

Go to the source code of this file.

Functions

asciichat_error_t send_error_packet (socket_t sockfd, asciichat_error_t error_code)
 Send an ACIP error packet using asciichat_error_t.
 
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.
 
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.
 
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.
 

Detailed Description

Network error handling utilities.

Provides helper functions for sending error responses and handling common error patterns in network protocols.

Definition in file errors.h.

Function Documentation

◆ check_and_record_packet_rate_limit()

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.

Maps packet_type_t to the corresponding rate_event_type_t and performs rate limiting check. Sends ERROR_RATE_LIMITED response if exceeded.

Packet type to rate event mapping:

  • IMAGE_FRAME -> RATE_EVENT_IMAGE_FRAME
  • AUDIO, AUDIO_BATCH, AUDIO_OPUS, AUDIO_OPUS_BATCH -> RATE_EVENT_AUDIO
  • PING, PONG -> RATE_EVENT_PING
  • CLIENT_JOIN -> RATE_EVENT_CLIENT_JOIN
  • CLIENT_CAPABILITIES, STREAM_START, STREAM_STOP, CLIENT_LEAVE -> RATE_EVENT_CONTROL
  • All other packets -> No rate limiting (always allowed)
Parameters
rate_limiterRate limiter instance
client_ipClient IP address
client_socketClient socket for sending error response
packet_typePacket type being processed
Returns
true if allowed (and event recorded), false if rate limited

Definition at line 54 of file errors.c.

55 {
56 // Map packet type to rate event type
57 rate_event_type_t event_type;
58 const char *packet_name;
59
60 switch (packet_type) {
62 event_type = RATE_EVENT_IMAGE_FRAME;
63 packet_name = "IMAGE_FRAME";
64 break;
65
70 event_type = RATE_EVENT_AUDIO;
71 packet_name = "AUDIO";
72 break;
73
76 event_type = RATE_EVENT_PING;
77 packet_name = "PING";
78 break;
79
81 event_type = RATE_EVENT_CLIENT_JOIN;
82 packet_name = "CLIENT_JOIN";
83 break;
84
89 event_type = RATE_EVENT_CONTROL;
90 packet_name = "CONTROL";
91 break;
92
93 default:
94 // No rate limiting for other packet types
95 return true;
96 }
97
98 // Use the existing check_and_record_rate_limit function
99 return check_and_record_rate_limit(rate_limiter, client_ip, event_type, client_socket, packet_name);
100}
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.
Definition errors.c:38
@ PACKET_TYPE_AUDIO_OPUS_BATCH
Batched Opus-encoded audio frames.
Definition packet.h:359
@ PACKET_TYPE_AUDIO_OPUS
Opus-encoded single audio frame.
Definition packet.h:357
@ PACKET_TYPE_CLIENT_LEAVE
Clean disconnect notification.
Definition packet.h:302
@ PACKET_TYPE_IMAGE_FRAME
Complete RGB image with dimensions.
Definition packet.h:288
@ PACKET_TYPE_PONG
Keepalive pong response.
Definition packet.h:297
@ PACKET_TYPE_STREAM_START
Client requests to start sending video/audio.
Definition packet.h:304
@ PACKET_TYPE_AUDIO
Single audio packet (legacy)
Definition packet.h:291
@ PACKET_TYPE_CLIENT_JOIN
Client announces capability to send media.
Definition packet.h:300
@ PACKET_TYPE_CLIENT_CAPABILITIES
Client reports terminal capabilities.
Definition packet.h:293
@ PACKET_TYPE_PING
Keepalive ping packet.
Definition packet.h:295
@ PACKET_TYPE_AUDIO_BATCH
Batched audio packets for efficiency.
Definition packet.h:343
@ PACKET_TYPE_STREAM_STOP
Client stops sending media.
Definition packet.h:306
rate_event_type_t
Rate limit event types.
Definition rate_limit.h:46
@ RATE_EVENT_CONTROL
Control packets (CAPABILITIES, STREAM_START/STOP, LEAVE)
Definition rate_limit.h:58
@ RATE_EVENT_PING
Ping/pong keepalive (PACKET_TYPE_PING, PACKET_TYPE_PONG)
Definition rate_limit.h:56
@ RATE_EVENT_AUDIO
Audio packet (PACKET_TYPE_AUDIO, PACKET_TYPE_AUDIO_BATCH)
Definition rate_limit.h:55
@ RATE_EVENT_IMAGE_FRAME
Image frame from client (PACKET_TYPE_IMAGE_FRAME)
Definition rate_limit.h:54
@ RATE_EVENT_CLIENT_JOIN
Client join request (PACKET_TYPE_CLIENT_JOIN)
Definition rate_limit.h:57

References check_and_record_rate_limit(), PACKET_TYPE_AUDIO, PACKET_TYPE_AUDIO_BATCH, PACKET_TYPE_AUDIO_OPUS, PACKET_TYPE_AUDIO_OPUS_BATCH, PACKET_TYPE_CLIENT_CAPABILITIES, PACKET_TYPE_CLIENT_JOIN, PACKET_TYPE_CLIENT_LEAVE, PACKET_TYPE_IMAGE_FRAME, PACKET_TYPE_PING, PACKET_TYPE_PONG, PACKET_TYPE_STREAM_START, PACKET_TYPE_STREAM_STOP, RATE_EVENT_AUDIO, RATE_EVENT_CLIENT_JOIN, RATE_EVENT_CONTROL, RATE_EVENT_IMAGE_FRAME, and RATE_EVENT_PING.

Referenced by process_decrypted_packet().

◆ check_and_record_rate_limit()

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.

Helper function that checks rate limit, sends error response if exceeded, and records the event if allowed. Encapsulates the common pattern:

  1. Check rate limit
  2. Send ERROR_RATE_LIMITED if exceeded
  3. Record event if allowed
Parameters
rate_limiterRate limiter instance
client_ipClient IP address for logging
event_typeType of event being rate limited
client_socketSocket to send error packet on
operation_nameName of operation for logging (e.g., "SESSION_CREATE")
Returns
true if allowed (and event recorded), false if rate limited

Definition at line 38 of file errors.c.

39 {
40 bool allowed = false;
41 asciichat_error_t rate_check = rate_limiter_check(rate_limiter, client_ip, event_type, NULL, &allowed);
42
43 if (rate_check != ASCIICHAT_OK || !allowed) {
44 send_error_packet_message(client_socket, ERROR_RATE_LIMITED, "Rate limit exceeded. Please try again later.");
45 log_warn("Rate limit exceeded for %s from %s", operation_name, client_ip);
46 return false;
47 }
48
49 // Record the rate limit event
50 rate_limiter_record(rate_limiter, client_ip, event_type);
51 return true;
52}
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.
Definition errors.c:15
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:46
@ ASCIICHAT_OK
Definition error_codes.h:48
@ ERROR_RATE_LIMITED
Definition error_codes.h:77
#define log_warn(...)
Log a WARN message.
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.
Definition rate_limit.c:144
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.
Definition rate_limit.c:127

References ASCIICHAT_OK, ERROR_RATE_LIMITED, log_warn, rate_limiter_check(), rate_limiter_record(), and send_error_packet_message().

Referenced by check_and_record_packet_rate_limit().

◆ send_error_packet()

asciichat_error_t send_error_packet ( socket_t  sockfd,
asciichat_error_t  error_code 
)

Send an ACIP error packet using asciichat_error_t.

Converts an asciichat_error_t code to an ACIP error packet and sends it. Uses asciichat_error_string() for the error message.

Parameters
sockfdSocket to send error on
error_codeError code from asciichat_error_t enum
Returns
ASCIICHAT_OK on success, error code on failure

Definition at line 11 of file errors.c.

11 {
12 return send_error_packet_message(sockfd, error_code, asciichat_error_string(error_code));
13}
asciichat_error_t error_code

References error_code, and send_error_packet_message().

◆ send_error_packet_message()

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.

Converts an asciichat_error_t code to an ACIP error packet with a custom error message.

Parameters
sockfdSocket to send error on
error_codeError code from asciichat_error_t enum
messageCustom error message (max 255 chars)
Returns
ASCIICHAT_OK on success, error code on failure

Definition at line 15 of file errors.c.

15 {
16 if (sockfd == INVALID_SOCKET_VALUE) {
17 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid socket");
18 }
19
20 if (!message) {
21 message = asciichat_error_string(error_code);
22 }
23
24 // Construct ACIP error packet
25 acip_error_t error = {0};
26 error.error_code = (uint8_t)error_code;
27 SAFE_STRNCPY(error.error_message, message, sizeof(error.error_message));
28
29 // Send error packet
30 int result = send_packet(sockfd, PACKET_TYPE_ACIP_ERROR, &error, sizeof(error));
31 if (result < 0) {
32 return SET_ERRNO(ERROR_NETWORK, "Failed to send error packet");
33 }
34
35 return ASCIICHAT_OK;
36}
#define SAFE_STRNCPY(dst, src, size)
Definition common.h:358
unsigned char uint8_t
Definition common.h:56
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it.
@ ERROR_NETWORK
Definition error_codes.h:69
@ ERROR_INVALID_PARAM
int send_packet(socket_t sockfd, packet_type_t type, const void *data, size_t len)
Send a basic packet without encryption.
Definition packet.c:754
@ PACKET_TYPE_ACIP_ERROR
Generic error response (Discovery Server -> Client)
Definition packet.h:404
#define INVALID_SOCKET_VALUE
Invalid socket value (POSIX: -1)
Definition socket.h:52

References acip_error_t, ASCIICHAT_OK, error_code, ERROR_INVALID_PARAM, ERROR_NETWORK, INVALID_SOCKET_VALUE, PACKET_TYPE_ACIP_ERROR, SAFE_STRNCPY, send_packet(), and SET_ERRNO.

Referenced by check_and_record_rate_limit(), and send_error_packet().