ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
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
69asciichat_error_t network_error_decode(packet_type_t type, const void *payload, size_t size) {
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}
92
94 return send_error_packet_message(sockfd, error_code, asciichat_error_string(error_code));
95}
96
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}
144
146 rate_event_type_t event_type, bool *allowed) {
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}
162
164 packet_type_t packet_type, bool *allowed) {
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,...)
#define SAFE_STRNCPY(dst, src, size)
Definition common.h:423
unsigned char uint8_t
Definition common.h:60
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
@ ERROR_NETWORK_CONNECT
Definition error_codes.h:74
@ 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_NETWORK
Definition error_codes.h:72
@ ERROR_CRYPTO_VERIFICATION
Definition error_codes.h:97
@ ERROR_BUFFER_FULL
@ ERROR_AUDIO
Definition error_codes.h:62
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:76
@ ERROR_MEMORY
Definition error_codes.h:51
@ ASCIICHAT_OK
Definition error_codes.h:46
@ ERROR_INVALID_SIGNATURE
Definition error_codes.h:84
@ ERROR_NETWORK_SIZE
Definition error_codes.h:77
@ ERROR_CRYPTO_AUTH
Definition error_codes.h:95
@ ERROR_RATE_LIMITED
Definition error_codes.h:80
@ ERROR_SESSION_FULL
Definition error_codes.h:82
@ ERROR_INVALID_FRAME
@ ERROR_CRYPTO
Definition error_codes.h:93
@ ERROR_INVALID_PARAM
@ ERROR_BUFFER_OVERFLOW
@ ERROR_INTERNAL
Definition error_codes.h:87
@ ERROR_SESSION_NOT_FOUND
Definition error_codes.h:81
@ ERROR_NETWORK_TIMEOUT
Definition error_codes.h:75
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:301
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
#define MAX_ERROR_MESSAGE_LENGTH
Maximum error message length (512 bytes).
Definition packet.h:139
@ 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_ACIP_ERROR
Generic error response (Discovery Server -> Client).
Definition packet.h:533
@ 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_ERROR_MESSAGE
Error packet with asciichat_error_t code and human-readable message.
Definition packet.h:374
@ 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
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:782
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
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
struct rate_limiter_s rate_limiter_t
Opaque rate limiter handle.
Definition rate_limit.h:83
🔤 String Manipulation and Shell Escaping Utilities