ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
rate_limit.h
Go to the documentation of this file.
1
35
36#pragma once
37
39#include <stdbool.h>
40#include <stddef.h>
41#include <stdint.h>
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
69
81
88
96typedef struct {
97 asciichat_error_t (*check)(void *backend_data, const char *ip_address, rate_event_type_t event_type, const rate_limit_config_t *config, bool *allowed);
98
99 asciichat_error_t (*record)(void *backend_data, const char *ip_address, rate_event_type_t event_type);
100
102
103 void (*destroy)(void *backend_data);
105
110
111// ============================================================================
112// Rate Limiter Creation
113// ============================================================================
114
124
134rate_limiter_t *rate_limiter_create_sqlite(const char *db_path);
135
146void rate_limiter_set_sqlite_db(rate_limiter_t *limiter, void *db);
147
154
155// ============================================================================
156// Rate Limiting Operations
157// ============================================================================
158
171asciichat_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);
172
183asciichat_error_t rate_limiter_record(rate_limiter_t *limiter, const char *ip_address, rate_event_type_t event_type);
184
196
197// ============================================================================
198// Utility Functions
199// ============================================================================
200
208
209#ifdef __cplusplus
210}
211#endif
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
unsigned int uint32_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
const rate_limit_config_t DEFAULT_RATE_LIMITS[RATE_EVENT_MAX]
Default rate limits for each event type.
Definition rate_limit.c:33
rate_event_type_t
One sliding-window rate-limit policy.
Definition rate_limit.h:52
@ RATE_EVENT_SESSION_LOOKUP
Session lookup.
Definition rate_limit.h:55
@ RATE_EVENT_CONTROL
Control packets (CAPABILITIES, STREAM_START/STOP, LEAVE).
Definition rate_limit.h:64
@ RATE_EVENT_SESSION_JOIN
Session join.
Definition rate_limit.h:56
@ RATE_EVENT_CONNECTION
New connection.
Definition rate_limit.h:59
@ RATE_EVENT_PING
Ping/pong keepalive (PACKET_TYPE_PING, PACKET_TYPE_PONG).
Definition rate_limit.h:62
@ RATE_EVENT_SESSION_CREATE
Session creation.
Definition rate_limit.h:54
@ RATE_EVENT_AUDIO
Audio packet (PACKET_TYPE_AUDIO, PACKET_TYPE_AUDIO_BATCH).
Definition rate_limit.h:61
@ RATE_EVENT_MAX
Sentinel value.
Definition rate_limit.h:67
@ 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
@ RATE_EVENT_HTTP_REQUEST
HTTP requests from a client identity.
Definition rate_limit.h:65
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
struct rate_limiter_s rate_limiter_t
Opaque rate limiter handle.
Definition rate_limit.h:87
void rate_limiter_destroy(rate_limiter_t *limiter)
Destroy rate limiter and free resources.
Definition rate_limit.c:126
void rate_limiter_set_sqlite_db(rate_limiter_t *limiter, void *db)
Set SQLite database handle for rate limiter.
Definition rate_limit.c:117
rate_limiter_t * rate_limiter_create_memory(void)
Create in-memory rate limiter.
Definition rate_limit.c:83
const char * rate_limiter_event_type_string(rate_event_type_t event_type)
Get event type string for logging/database storage.
Definition rate_limit.c:193
rate_limiter_t * rate_limiter_create_sqlite(const char *db_path)
Create SQLite-backed rate limiter.
Definition rate_limit.c:100
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
asciichat_error_t rate_limiter_prune(rate_limiter_t *limiter, uint32_t max_age_secs)
Clean up old rate limit events.
Definition rate_limit.c:177
One sliding-window rate-limit policy.
Definition rate_limit.h:77
uint32_t max_events
Maximum events allowed.
Definition rate_limit.h:78
uint32_t window_secs
Time window in seconds.
Definition rate_limit.h:79
Backend callbacks implementing rate-limit checks, recording, cleanup, and destruction.
Definition rate_limit.h:96
void(* destroy)(void *backend_data)
Definition rate_limit.h:103
asciichat_error_t(* cleanup)(void *backend_data, uint32_t max_age_secs)
Definition rate_limit.h:101
asciichat_error_t(* record)(void *backend_data, const char *ip_address, rate_event_type_t event_type)
Definition rate_limit.h:99
asciichat_error_t(* check)(void *backend_data, const char *ip_address, rate_event_type_t event_type, const rate_limit_config_t *config, bool *allowed)
Definition rate_limit.h:97
Private dispatch object joining a rate-limit backend to its operations table.
Definition rate_limit.c:74
void * backend_data
Backend-specific data.
Definition rate_limit.c:76