ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
errors.c
Go to the documentation of this file.
1
9#include <string.h>
10
14
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}
37
38bool check_and_record_rate_limit(rate_limiter_t *rate_limiter, const char *client_ip, rate_event_type_t event_type,
39 socket_t client_socket, const char *operation_name) {
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}
53
54bool check_and_record_packet_rate_limit(rate_limiter_t *rate_limiter, const char *client_ip, socket_t client_socket,
55 packet_type_t packet_type) {
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
68 event_type = RATE_EVENT_AUDIO;
69 packet_name = "AUDIO";
70 break;
71
74 event_type = RATE_EVENT_PING;
75 packet_name = "PING";
76 break;
77
79 event_type = RATE_EVENT_CLIENT_JOIN;
80 packet_name = "CLIENT_JOIN";
81 break;
82
87 event_type = RATE_EVENT_CONTROL;
88 packet_name = "CONTROL";
89 break;
90
91 default:
92 // No rate limiting for other packet types
93 return true;
94 }
95
96 // Use the existing check_and_record_rate_limit function
97 return check_and_record_rate_limit(rate_limiter, client_ip, event_type, client_socket, packet_name);
98}
asciichat_error_t error_code
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
asciichat_error_t send_error_packet(socket_t sockfd, asciichat_error_t error_code)
Send an ACIP error packet using asciichat_error_t.
Definition errors.c:11
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.
Definition errors.c:54
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
Network error handling utilities.
acip_error_t
Definition acds.h:1004
#define SAFE_STRNCPY(dst, src, size)
Definition common.h:414
unsigned char uint8_t
Definition common.h:56
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:49
@ ERROR_NETWORK
Definition error_codes.h:77
@ ASCIICHAT_OK
Definition error_codes.h:51
@ ERROR_RATE_LIMITED
Definition error_codes.h:85
@ ERROR_INVALID_PARAM
#define log_warn(...)
Log a WARN message.
Definition log/log.h:574
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:784
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:286
@ PACKET_TYPE_AUDIO_OPUS_BATCH
Batched Opus-encoded audio frames.
Definition packet.h:374
@ PACKET_TYPE_CLIENT_LEAVE
Clean disconnect notification.
Definition packet.h:389
@ PACKET_TYPE_IMAGE_FRAME
Complete RGB image with dimensions.
Definition packet.h:363
@ PACKET_TYPE_PONG
Keepalive pong response.
Definition packet.h:385
@ PACKET_TYPE_STREAM_START
Client requests to start sending video/audio.
Definition packet.h:391
@ PACKET_TYPE_ACIP_ERROR
Generic error response (Discovery Server -> Client)
Definition packet.h:509
@ PACKET_TYPE_CLIENT_JOIN
Client announces capability to send media.
Definition packet.h:387
@ PACKET_TYPE_CLIENT_CAPABILITIES
Client reports terminal capabilities.
Definition packet.h:381
@ PACKET_TYPE_PING
Keepalive ping packet.
Definition packet.h:383
@ PACKET_TYPE_AUDIO_BATCH
Batched audio packets for efficiency.
Definition packet.h:372
@ PACKET_TYPE_STREAM_STOP
Client stops sending media.
Definition packet.h:393
#define INVALID_SOCKET_VALUE
Invalid socket value (POSIX: -1)
Definition socket.h:278
int socket_t
📝 Logging API with multiple log levels and terminal output control
🌐 Core network I/O operations with timeout support
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:145
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:128
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
Rate limiter structure.
Definition rate_limit.c:64