ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
errors.c
Go to the documentation of this file.
1
6#include "network/errors.h"
7#include "network/network.h"
8#include "log/logging.h"
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
70 event_type = RATE_EVENT_AUDIO;
71 packet_name = "AUDIO";
72 break;
73
76 event_type = RATE_EVENT_PING;
77 packet_name = "PING";
78 break;
79
81 event_type = RATE_EVENT_CLIENT_JOIN;
82 packet_name = "CLIENT_JOIN";
83 break;
84
89 event_type = RATE_EVENT_CONTROL;
90 packet_name = "CONTROL";
91 break;
92
93 default:
94 // No rate limiting for other packet types
95 return true;
96 }
97
98 // Use the existing check_and_record_rate_limit function
99 return check_and_record_rate_limit(rate_limiter, client_ip, event_type, client_socket, packet_name);
100}
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.
#define SAFE_STRNCPY(dst, src, size)
Definition common.h:358
unsigned char uint8_t
Definition common.h:56
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it.
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:46
@ ERROR_NETWORK
Definition error_codes.h:69
@ ASCIICHAT_OK
Definition error_codes.h:48
@ ERROR_RATE_LIMITED
Definition error_codes.h:77
@ ERROR_INVALID_PARAM
#define log_warn(...)
Log a WARN message.
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:754
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:281
@ PACKET_TYPE_AUDIO_OPUS_BATCH
Batched Opus-encoded audio frames.
Definition packet.h:359
@ PACKET_TYPE_AUDIO_OPUS
Opus-encoded single audio frame.
Definition packet.h:357
@ PACKET_TYPE_CLIENT_LEAVE
Clean disconnect notification.
Definition packet.h:302
@ PACKET_TYPE_IMAGE_FRAME
Complete RGB image with dimensions.
Definition packet.h:288
@ PACKET_TYPE_PONG
Keepalive pong response.
Definition packet.h:297
@ PACKET_TYPE_STREAM_START
Client requests to start sending video/audio.
Definition packet.h:304
@ PACKET_TYPE_ACIP_ERROR
Generic error response (Discovery Server -> Client)
Definition packet.h:404
@ PACKET_TYPE_AUDIO
Single audio packet (legacy)
Definition packet.h:291
@ PACKET_TYPE_CLIENT_JOIN
Client announces capability to send media.
Definition packet.h:300
@ PACKET_TYPE_CLIENT_CAPABILITIES
Client reports terminal capabilities.
Definition packet.h:293
@ PACKET_TYPE_PING
Keepalive ping packet.
Definition packet.h:295
@ PACKET_TYPE_AUDIO_BATCH
Batched audio packets for efficiency.
Definition packet.h:343
@ PACKET_TYPE_STREAM_STOP
Client stops sending media.
Definition packet.h:306
int socket_t
Socket handle type (POSIX: int)
Definition socket.h:50
#define INVALID_SOCKET_VALUE
Invalid socket value (POSIX: -1)
Definition socket.h:52
📝 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:144
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:127
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:63