ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
lib/network/errors.c
Go to the documentation of this file.
1
5
9#include <string.h>
10
71 size_t size) {
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}
99
101 asciichat_error_t error_code) {
102 return send_error_packet_message(sockfd, error_code,
103 asciichat_error_string(error_code));
104}
105
107 asciichat_error_t error_code,
108 const char *message) {
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}
158
160 const char *client_ip,
161 rate_event_type_t event_type,
162 bool *allowed) {
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}
179
182 const char *client_ip,
183 packet_type_t packet_type, bool *allowed) {
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,...)
#define SAFE_STRNCPY(dst, src, size)
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_NETWORK_CONNECT
Definition error_codes.h:79
@ 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_NETWORK
Definition error_codes.h:77
@ ERROR_CRYPTO_VERIFICATION
@ ERROR_BUFFER_FULL
@ ERROR_AUDIO
Definition error_codes.h:66
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:81
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_SIGNATURE
Definition error_codes.h:90
@ ERROR_NETWORK_SIZE
Definition error_codes.h:83
@ ERROR_CRYPTO_AUTH
@ ERROR_RATE_LIMITED
Definition error_codes.h:86
@ ERROR_SESSION_FULL
Definition error_codes.h:88
@ ERROR_INVALID_FRAME
@ ERROR_CRYPTO
@ ERROR_INVALID_PARAM
@ ERROR_BUFFER_OVERFLOW
@ ERROR_INTERNAL
Definition error_codes.h:94
@ ERROR_SESSION_NOT_FOUND
Definition error_codes.h:87
@ ERROR_NETWORK_TIMEOUT
Definition error_codes.h:80
acip_error_t
Definition acds.h:1013
@ 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_INTERNAL
Internal server error.
Definition acds.h:1082
@ 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
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:315
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
#define MAX_ERROR_MESSAGE_LENGTH
Maximum error message length (512 bytes).
Definition packet.h:153
@ 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_ACIP_ERROR
Generic error response (Discovery Server -> Client).
Definition packet.h:547
@ 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_ERROR_MESSAGE
Error packet with asciichat_error_t code and human-readable message.
Definition packet.h:388
@ 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
int socket_t
Network error handling utilities.
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 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.
asciichat_error_t network_error_public_code(asciichat_error_t code)
asciichat_error_t network_error_decode(packet_type_t type, const void *payload, size_t size)
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.
network_error_action_t network_error_action(asciichat_error_t code)
bool network_error_is_local_rejection(asciichat_error_t code)
asciichat_error_t network_error_from_acip(uint8_t code)
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.
📝 Logging API with multiple log levels and terminal output control
🌐 Core network I/O operations with timeout support
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
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
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
struct rate_limiter_s rate_limiter_t
Opaque rate limiter handle.
Definition rate_limit.h:87
🔤 String Manipulation and Shell Escaping Utilities