ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
errors.c File Reference

Network error handling utilities implementation. More...

Go to the source code of this file.

Functions

bool network_error_is_local_rejection (asciichat_error_t code)
network_error_action_t network_error_action (asciichat_error_t code)
asciichat_error_t network_error_public_code (asciichat_error_t code)
asciichat_error_t network_error_from_acip (uint8_t code)
asciichat_error_t network_error_decode (packet_type_t type, const void *payload, size_t size)
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 a public message.
asciichat_error_t check_and_record_rate_limit (rate_limiter_t *rate_limiter, const char *client_ip, rate_event_type_t event_type, bool *allowed)
 Check rate limit and record an allowed event.
asciichat_error_t check_and_record_packet_rate_limit (rate_limiter_t *rate_limiter, const char *client_ip, packet_type_t packet_type, bool *allowed)
 Map packet type to rate event type and check rate limit.

Detailed Description

Network error handling utilities implementation.

Definition in file lib/network/errors.c.

Function Documentation

◆ check_and_record_packet_rate_limit()

asciichat_error_t check_and_record_packet_rate_limit ( rate_limiter_t * rate_limiter,
const char * client_ip,
packet_type_t packet_type,
bool * allowed )

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. The caller owns any error response and recovery scope.

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
packet_typePacket type being processed
allowedOutput quota decision; false on failure
Returns
ASCIICHAT_OK for a completed decision, typed failure otherwise

Definition at line 163 of file lib/network/errors.c.

164 {
165 if (!allowed)
166 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing rate-limit decision output");
167 *allowed = false;
168 // Map packet type to rate event type
169 rate_event_type_t event_type;
170
171 switch (packet_type) {
173 event_type = RATE_EVENT_IMAGE_FRAME;
174 break;
175
178 event_type = RATE_EVENT_AUDIO;
179 break;
180
181 case PACKET_TYPE_PING:
182 case PACKET_TYPE_PONG:
183 event_type = RATE_EVENT_PING;
184 break;
185
187 event_type = RATE_EVENT_CLIENT_JOIN;
188 break;
189
194 event_type = RATE_EVENT_CONTROL;
195 break;
196
197 default:
198 // No rate limiting for other packet types
199 *allowed = true;
200 return ASCIICHAT_OK;
201 }
202
203 // Use the existing check_and_record_rate_limit function
204 return check_and_record_rate_limit(rate_limiter, client_ip, event_type, allowed);
205}
#define SET_ERRNO(code,...)
@ ASCIICHAT_OK
Definition error_codes.h:46
@ ERROR_INVALID_PARAM
@ PACKET_TYPE_AUDIO_OPUS_BATCH
Batched Opus-encoded audio frames.
Definition packet.h:398
@ PACKET_TYPE_CLIENT_LEAVE
Clean disconnect notification.
Definition packet.h:413
@ PACKET_TYPE_IMAGE_FRAME
Complete RGB image with dimensions.
Definition packet.h:385
@ PACKET_TYPE_PONG
Keepalive pong response.
Definition packet.h:409
@ PACKET_TYPE_STREAM_START
Client requests to start sending video/audio.
Definition packet.h:415
@ PACKET_TYPE_CLIENT_JOIN
Client announces capability to send media.
Definition packet.h:411
@ PACKET_TYPE_CLIENT_CAPABILITIES
Client reports terminal capabilities.
Definition packet.h:405
@ PACKET_TYPE_PING
Keepalive ping packet.
Definition packet.h:407
@ PACKET_TYPE_AUDIO_BATCH
Batched audio packets for efficiency.
Definition packet.h:396
@ PACKET_TYPE_STREAM_STOP
Client stops sending media.
Definition packet.h:417
asciichat_error_t check_and_record_rate_limit(rate_limiter_t *rate_limiter, const char *client_ip, rate_event_type_t event_type, bool *allowed)
Check rate limit and record an allowed event.
rate_event_type_t
One sliding-window rate-limit policy.
Definition rate_limit.h:48
@ RATE_EVENT_CONTROL
Control packets (CAPABILITIES, STREAM_START/STOP, LEAVE).
Definition rate_limit.h:60
@ RATE_EVENT_PING
Ping/pong keepalive (PACKET_TYPE_PING, PACKET_TYPE_PONG).
Definition rate_limit.h:58
@ RATE_EVENT_AUDIO
Audio packet (PACKET_TYPE_AUDIO, PACKET_TYPE_AUDIO_BATCH).
Definition rate_limit.h:57
@ RATE_EVENT_IMAGE_FRAME
Image frame from client (PACKET_TYPE_IMAGE_FRAME).
Definition rate_limit.h:56
@ RATE_EVENT_CLIENT_JOIN
Client join request (PACKET_TYPE_CLIENT_JOIN).
Definition rate_limit.h:59

References ASCIICHAT_OK, check_and_record_rate_limit(), ERROR_INVALID_PARAM, PACKET_TYPE_AUDIO_BATCH, 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, RATE_EVENT_PING, and SET_ERRNO.

Referenced by process_decrypted_packet().

◆ check_and_record_rate_limit()

asciichat_error_t check_and_record_rate_limit ( rate_limiter_t * rate_limiter,
const char * client_ip,
rate_event_type_t event_type,
bool * allowed )

Check rate limit and record an allowed event.

Helper function that checks rate limit and records the event if allowed. Encapsulates the common pattern:

  1. Check rate limit
  2. Return allowed=false 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
allowedOutput quota decision; false on failure
Returns
ASCIICHAT_OK for a completed decision, typed failure otherwise

Definition at line 145 of file lib/network/errors.c.

146 {
147 if (!allowed)
148 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing rate-limit decision output");
149 *allowed = false;
150 asciichat_error_t result = rate_limiter_check(rate_limiter, client_ip, event_type, NULL, allowed);
151 if (result != ASCIICHAT_OK) {
152 *allowed = false;
153 return result;
154 }
155 if (!*allowed)
156 return ASCIICHAT_OK;
157 result = rate_limiter_record(rate_limiter, client_ip, event_type);
158 if (result != ASCIICHAT_OK)
159 *allowed = false;
160 return result;
161}
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
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:162
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:138

References ASCIICHAT_OK, ERROR_INVALID_PARAM, rate_limiter_check(), rate_limiter_record(), and SET_ERRNO.

Referenced by check_and_record_packet_rate_limit().

◆ network_error_action()

network_error_action_t network_error_action ( asciichat_error_t code)

Definition at line 15 of file lib/network/errors.c.

15 {
16 switch (code) {
17 case ERROR_AUDIO:
21 case ERROR_NETWORK:
25 default:
26 return NETWORK_ERROR_STOP;
27 }
28}
@ ERROR_NETWORK_CONNECT
Definition error_codes.h:74
@ ERROR_NETWORK
Definition error_codes.h:72
@ ERROR_AUDIO
Definition error_codes.h:62
@ ERROR_RATE_LIMITED
Definition error_codes.h:80
@ ERROR_SESSION_FULL
Definition error_codes.h:82
@ ERROR_NETWORK_TIMEOUT
Definition error_codes.h:75

References ERROR_AUDIO, ERROR_NETWORK, ERROR_NETWORK_CONNECT, ERROR_NETWORK_TIMEOUT, ERROR_RATE_LIMITED, ERROR_SESSION_FULL, NETWORK_ERROR_CONTINUE, NETWORK_ERROR_RETRY, and NETWORK_ERROR_STOP.

Referenced by EMSCRIPTEN_BINDINGS().

◆ network_error_decode()

asciichat_error_t network_error_decode ( packet_type_t type,
const void * payload,
size_t size )

Decode either discovery or application error packets with bounded payload validation.

Definition at line 69 of file lib/network/errors.c.

69 {
71 if (!payload)
72 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Missing remote error payload");
73 if (type == PACKET_TYPE_ACIP_ERROR) {
74 if (size != sizeof(acip_error_t))
75 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Invalid discovery error payload length");
76 code = network_error_from_acip(((const acip_error_t *)payload)->error_code);
77 } else if (type == PACKET_TYPE_ERROR_MESSAGE) {
78 char message[MAX_ERROR_MESSAGE_LENGTH + 1];
79 asciichat_error_t result = packet_parse_error_message(payload, size, &code, message, sizeof(message), NULL);
80 if (result != ASCIICHAT_OK)
81 return result;
82 code = network_error_public_code(code);
83 /* Preserve the server's sanitized diagnostic. The previous code parsed
84 * this message and then discarded it, making policy/protocol failures
85 * indistinguishable from real authentication failures. */
86 return SET_ERRNO(code, "Remote request rejected: %s", message[0] ? message : asciichat_error_string(code));
87 } else {
88 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Unexpected rejection packet type");
89 }
90 return SET_ERRNO(code, "Remote request rejected: %s", asciichat_error_string(code));
91}
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:76
acip_error_t
Definition acds.h:1013
#define MAX_ERROR_MESSAGE_LENGTH
Maximum error message length (512 bytes).
Definition packet.h:139
@ PACKET_TYPE_ACIP_ERROR
Generic error response (Discovery Server -> Client).
Definition packet.h:533
@ PACKET_TYPE_ERROR_MESSAGE
Error packet with asciichat_error_t code and human-readable message.
Definition packet.h:374
asciichat_error_t network_error_public_code(asciichat_error_t code)
asciichat_error_t network_error_from_acip(uint8_t code)
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.
Definition packet.c:782

References acip_error_t, ASCIICHAT_OK, ERROR_NETWORK_PROTOCOL, MAX_ERROR_MESSAGE_LENGTH, network_error_from_acip(), network_error_public_code(), packet_parse_error_message(), PACKET_TYPE_ACIP_ERROR, PACKET_TYPE_ERROR_MESSAGE, and SET_ERRNO.

Referenced by acds_protocol_receive().

◆ network_error_from_acip()

asciichat_error_t network_error_from_acip ( uint8_t code)

Definition at line 49 of file lib/network/errors.c.

49 {
50 switch (code) {
54 return ERROR_SESSION_FULL;
60 return ERROR_RATE_LIMITED;
65 default:
66 return ERROR_INTERNAL;
67 }
68}
@ ERROR_INVALID_PASSWORD
Definition error_codes.h:83
@ ERROR_ACDS_STRING_INVALID
Definition error_codes.h:86
@ ERROR_ACDS_STRING_TAKEN
Definition error_codes.h:85
@ ERROR_INVALID_SIGNATURE
Definition error_codes.h:84
@ ERROR_INTERNAL
Definition error_codes.h:87
@ ERROR_SESSION_NOT_FOUND
Definition error_codes.h:81
@ ACIP_ERROR_RATE_LIMITED
Too many requests from this IP.
Definition acds.h:1079
@ ACIP_ERROR_STRING_TAKEN
Requested string already reserved.
Definition acds.h:1080
@ ACIP_ERROR_STRING_INVALID
String format invalid.
Definition acds.h:1081
@ ACIP_ERROR_INVALID_PASSWORD
Password verification failed.
Definition acds.h:1077
@ ACIP_ERROR_SESSION_NOT_FOUND
Session does not exist.
Definition acds.h:1075
@ ACIP_ERROR_SESSION_FULL
Session has reached max participants.
Definition acds.h:1076
@ ACIP_ERROR_INVALID_SIGNATURE
Identity signature invalid.
Definition acds.h:1078

References ACIP_ERROR_INVALID_PASSWORD, ACIP_ERROR_INVALID_SIGNATURE, ACIP_ERROR_RATE_LIMITED, ACIP_ERROR_SESSION_FULL, ACIP_ERROR_SESSION_NOT_FOUND, ACIP_ERROR_STRING_INVALID, ACIP_ERROR_STRING_TAKEN, ERROR_ACDS_STRING_INVALID, ERROR_ACDS_STRING_TAKEN, ERROR_INTERNAL, ERROR_INVALID_PASSWORD, ERROR_INVALID_SIGNATURE, ERROR_RATE_LIMITED, ERROR_SESSION_FULL, and ERROR_SESSION_NOT_FOUND.

Referenced by acds_protocol_receive(), and network_error_decode().

◆ network_error_is_local_rejection()

bool network_error_is_local_rejection ( asciichat_error_t code)

True only for local packet construction failures that have not reached transport I/O.

Definition at line 11 of file lib/network/errors.c.

11 {
12 return code == ERROR_INVALID_PARAM || code == ERROR_INVALID_FRAME || code == ERROR_NETWORK_SIZE ||
13 code == ERROR_BUFFER_OVERFLOW || code == ERROR_BUFFER_FULL || code == ERROR_MEMORY;
14}
@ ERROR_BUFFER_FULL
@ ERROR_MEMORY
Definition error_codes.h:51
@ ERROR_NETWORK_SIZE
Definition error_codes.h:77
@ ERROR_INVALID_FRAME
@ ERROR_BUFFER_OVERFLOW

References ERROR_BUFFER_FULL, ERROR_BUFFER_OVERFLOW, ERROR_INVALID_FRAME, ERROR_INVALID_PARAM, ERROR_MEMORY, and ERROR_NETWORK_SIZE.

Referenced by threaded_send_audio_batch_packet(), threaded_send_audio_opus(), threaded_send_audio_opus_batch(), threaded_send_image_frame(), threaded_send_image_frame_encoded(), and threaded_send_packet().

◆ network_error_public_code()

asciichat_error_t network_error_public_code ( asciichat_error_t code)

◆ 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 93 of file lib/network/errors.c.

93 {
94 return send_error_packet_message(sockfd, error_code, asciichat_error_string(error_code));
95}
asciichat_error_t send_error_packet_message(socket_t sockfd, asciichat_error_t error_code, const char *message)
Send an ACIP error packet with a public message.

References 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 a public message.

Maps application codes to discovery wire codes and uses their fixed public text. The legacy message argument is ignored to avoid exposing internal diagnostics.

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

Definition at line 97 of file lib/network/errors.c.

97 {
98 if (sockfd == INVALID_SOCKET_VALUE) {
99 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid socket");
100 }
101
102 // Keep the legacy parameter for source compatibility; only allowlisted text goes on the wire.
103 (void)message;
104
105 // Construct ACIP error packet
106 acip_error_t error = {0};
107 switch (error_code) {
109 error.error_code = ACIP_ERROR_SESSION_NOT_FOUND;
110 break;
112 error.error_code = ACIP_ERROR_SESSION_FULL;
113 break;
115 error.error_code = ACIP_ERROR_INVALID_PASSWORD;
116 break;
118 error.error_code = ACIP_ERROR_INVALID_SIGNATURE;
119 break;
121 error.error_code = ACIP_ERROR_RATE_LIMITED;
122 break;
124 error.error_code = ACIP_ERROR_STRING_TAKEN;
125 break;
127 error.error_code = ACIP_ERROR_STRING_INVALID;
128 break;
129 default:
130 error.error_code = ACIP_ERROR_INTERNAL;
131 break;
132 }
133 SAFE_STRNCPY(error.error_message, asciichat_error_string(network_error_public_code(error_code)),
134 sizeof(error.error_message));
135
136 // Send error packet
137 int result = send_packet(sockfd, PACKET_TYPE_ACIP_ERROR, &error, sizeof(error));
138 if (result < 0) {
139 return SET_ERRNO(ERROR_NETWORK, "Failed to send error packet");
140 }
141
142 return ASCIICHAT_OK;
143}
#define SAFE_STRNCPY(dst, src, size)
Definition common.h:423
@ ACIP_ERROR_INTERNAL
Internal server error.
Definition acds.h:1082
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:607

References ACIP_ERROR_INTERNAL, ACIP_ERROR_INVALID_PASSWORD, ACIP_ERROR_INVALID_SIGNATURE, ACIP_ERROR_RATE_LIMITED, ACIP_ERROR_SESSION_FULL, ACIP_ERROR_SESSION_NOT_FOUND, ACIP_ERROR_STRING_INVALID, ACIP_ERROR_STRING_TAKEN, acip_error_t, ASCIICHAT_OK, ERROR_ACDS_STRING_INVALID, ERROR_ACDS_STRING_TAKEN, ERROR_INVALID_PARAM, ERROR_INVALID_PASSWORD, ERROR_INVALID_SIGNATURE, ERROR_NETWORK, ERROR_RATE_LIMITED, ERROR_SESSION_FULL, ERROR_SESSION_NOT_FOUND, network_error_public_code(), PACKET_TYPE_ACIP_ERROR, SAFE_STRNCPY, send_packet(), and SET_ERRNO.

Referenced by send_error_packet().