ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
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 181 of file lib/network/errors.c.

183 {
184 if (!allowed)
185 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing rate-limit decision output");
186 *allowed = false;
187 // Map packet type to rate event type
188 rate_event_type_t event_type;
189
190 switch (packet_type) {
192 event_type = RATE_EVENT_IMAGE_FRAME;
193 break;
194
197 event_type = RATE_EVENT_AUDIO;
198 break;
199
200 case PACKET_TYPE_PING:
201 case PACKET_TYPE_PONG:
202 event_type = RATE_EVENT_PING;
203 break;
204
206 event_type = RATE_EVENT_CLIENT_JOIN;
207 break;
208
213 event_type = RATE_EVENT_CONTROL;
214 break;
215
216 default:
217 // No rate limiting for other packet types
218 *allowed = true;
219 return ASCIICHAT_OK;
220 }
221
222 // Use the existing check_and_record_rate_limit function
223 return check_and_record_rate_limit(rate_limiter, client_ip, event_type,
224 allowed);
225}
#define SET_ERRNO(code,...)
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
@ PACKET_TYPE_AUDIO_OPUS_BATCH
Batched Opus-encoded audio frames.
Definition packet.h:412
@ PACKET_TYPE_CLIENT_LEAVE
Clean disconnect notification.
Definition packet.h:427
@ PACKET_TYPE_IMAGE_FRAME
Complete RGB image with dimensions.
Definition packet.h:399
@ PACKET_TYPE_PONG
Keepalive pong response.
Definition packet.h:423
@ PACKET_TYPE_STREAM_START
Client requests to start sending video/audio.
Definition packet.h:429
@ PACKET_TYPE_CLIENT_JOIN
Client announces capability to send media.
Definition packet.h:425
@ PACKET_TYPE_CLIENT_CAPABILITIES
Client reports terminal capabilities.
Definition packet.h:419
@ PACKET_TYPE_PING
Keepalive ping packet.
Definition packet.h:421
@ PACKET_TYPE_AUDIO_BATCH
Batched audio packets for efficiency.
Definition packet.h:410
@ PACKET_TYPE_STREAM_STOP
Client stops sending media.
Definition packet.h:431
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:52
@ RATE_EVENT_CONTROL
Control packets (CAPABILITIES, STREAM_START/STOP, LEAVE).
Definition rate_limit.h:64
@ RATE_EVENT_PING
Ping/pong keepalive (PACKET_TYPE_PING, PACKET_TYPE_PONG).
Definition rate_limit.h:62
@ RATE_EVENT_AUDIO
Audio packet (PACKET_TYPE_AUDIO, PACKET_TYPE_AUDIO_BATCH).
Definition rate_limit.h:61
@ RATE_EVENT_IMAGE_FRAME
Image frame from client (PACKET_TYPE_IMAGE_FRAME).
Definition rate_limit.h:60
@ RATE_EVENT_CLIENT_JOIN
Client join request (PACKET_TYPE_CLIENT_JOIN).
Definition rate_limit.h:63

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

162 {
163 if (!allowed)
164 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing rate-limit decision output");
165 *allowed = false;
166 asciichat_error_t result =
167 rate_limiter_check(rate_limiter, client_ip, event_type, NULL, allowed);
168 if (result != ASCIICHAT_OK) {
169 *allowed = false;
170 return result;
171 }
172 if (!*allowed)
173 return ASCIICHAT_OK;
174 result = rate_limiter_record(rate_limiter, client_ip, event_type);
175 if (result != ASCIICHAT_OK)
176 *allowed = false;
177 return result;
178}
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
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:161
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_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 70 of file lib/network/errors.c.

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

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

50 {
51 switch (code) {
55 return ERROR_SESSION_FULL;
61 return ERROR_RATE_LIMITED;
66 default:
67 return ERROR_INTERNAL;
68 }
69}
@ ERROR_INVALID_PASSWORD
Definition error_codes.h:89
@ ERROR_ACDS_STRING_INVALID
Definition error_codes.h:93
@ ERROR_ACDS_STRING_TAKEN
Definition error_codes.h:91
@ ERROR_INVALID_SIGNATURE
Definition error_codes.h:90
@ ERROR_INTERNAL
Definition error_codes.h:94
@ ERROR_SESSION_NOT_FOUND
Definition error_codes.h:87
@ 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 ||
13 code == ERROR_NETWORK_SIZE || code == ERROR_BUFFER_OVERFLOW ||
14 code == ERROR_BUFFER_FULL || code == ERROR_MEMORY;
15}
@ ERROR_BUFFER_FULL
@ ERROR_MEMORY
Definition error_codes.h:55
@ ERROR_NETWORK_SIZE
Definition error_codes.h:83
@ 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 100 of file lib/network/errors.c.

101 {
102 return send_error_packet_message(sockfd, error_code,
103 asciichat_error_string(error_code));
104}
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 106 of file lib/network/errors.c.

108 {
109 if (sockfd == INVALID_SOCKET_VALUE) {
110 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid socket");
111 }
112
113 // Keep the legacy parameter for source compatibility; only allowlisted text
114 // goes on the wire.
115 (void)message;
116
117 // Construct ACIP error packet
118 acip_error_t error = {0};
119 switch (error_code) {
121 error.error_code = ACIP_ERROR_SESSION_NOT_FOUND;
122 break;
124 error.error_code = ACIP_ERROR_SESSION_FULL;
125 break;
127 error.error_code = ACIP_ERROR_INVALID_PASSWORD;
128 break;
130 error.error_code = ACIP_ERROR_INVALID_SIGNATURE;
131 break;
133 error.error_code = ACIP_ERROR_RATE_LIMITED;
134 break;
136 error.error_code = ACIP_ERROR_STRING_TAKEN;
137 break;
139 error.error_code = ACIP_ERROR_STRING_INVALID;
140 break;
141 default:
142 error.error_code = ACIP_ERROR_INTERNAL;
143 break;
144 }
145 SAFE_STRNCPY(error.error_message,
146 asciichat_error_string(network_error_public_code(error_code)),
147 sizeof(error.error_message));
148
149 // Send error packet
150 int result =
151 send_packet(sockfd, PACKET_TYPE_ACIP_ERROR, &error, sizeof(error));
152 if (result < 0) {
153 return SET_ERRNO(ERROR_NETWORK, "Failed to send error packet");
154 }
155
156 return ASCIICHAT_OK;
157}
#define SAFE_STRNCPY(dst, src, size)
@ 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:581

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().