ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
connections.c
Go to the documentation of this file.
1#include "connections.h"
2#include "main.h"
3#include "protocol.h"
4#include "render.h"
7#include <options/options.h>
8
10server_connection_open(network_connection_t *connection, void *arg) {
11 server_context_t *server = arg;
12 if (server->rate_limiter) {
13 bool allowed = false;
15 server->rate_limiter, network_connection_peer(connection),
16 RATE_EVENT_CONNECTION, NULL, &allowed);
17 if (result != ASCIICHAT_OK)
18 return result;
19 if (!allowed)
20 return SET_ERRNO(ERROR_RATE_LIMITED, "Connection rate limit exceeded");
21 result = rate_limiter_record(server->rate_limiter,
22 network_connection_peer(connection),
24 if (result != ASCIICHAT_OK)
25 return result;
26 }
28 SAFE_CALLOC(1, sizeof(*state), server_connection_state_t *);
29 network_connection_set_context(connection, state);
30 bool adopted_ready = network_connection_state(connection) == NETWORK_READY;
34 .protocol = NETWORK_PROTOCOL_MEDIA,
35 .handshake = handshake,
36 .initialized = adopted_ready && crypto_handshake_is_ready(handshake),
37 .encrypt_data =
38 adopted_ready &&
40 .identity_keys = g_server_identity_keys,
41 .identity_key_count = g_num_server_identity_keys,
42 .whitelist = g_client_whitelist,
43 .whitelist_count = g_num_whitelisted_clients,
44 .password = GET_OPTION(password),
45 .secure = network_connection_secure(connection),
46 .require_auth = GET_OPTION(password)[0] || g_num_whitelisted_clients ||
47 GET_OPTION(require_server_verify),
48 .require_identity =
49 g_num_whitelisted_clients || GET_OPTION(require_server_verify),
50 .require_encryption = GET_OPTION(encrypt_enabled),
51 .forbid_encryption = GET_OPTION(no_encrypt)};
52 return network_connection_state(connection) == NETWORK_READY
55}
57server_connection_packet(network_connection_t *connection, packet_type_t type,
58 const void *payload, size_t length, void *arg) {
59 server_context_t *server = arg;
63 &state->negotiation, network_connection_transport(connection), type,
64 payload, length);
65 if (result == ASCIICHAT_OK && state->negotiation.ready) {
66 network_connection_ready(connection);
68 }
69 return result;
70 }
71 client_info_t *client = atomic_ptr_load(&state->client);
72 if (!client) {
75 "Expected initial client capabilities");
76 // Authentication and capabilities precede allocation of media slots and
77 // buffers.
78 client =
80 network_connection_peer(connection));
81 if (!client)
82 return SET_ERRNO(ERROR_SESSION_FULL, "Cannot admit client");
83 client->network_connection = connection;
86 client->encrypt_data = state->negotiation.encrypt_data;
88 client->security_ready = true;
89 atomic_ptr_store(&state->client, client);
90 handle_client_capabilities_packet(client, payload, length);
91 return start_transport_client_threads(server, client->client_id) == 0
93 : SET_ERRNO(ERROR_THREAD, "Cannot start client rendering");
94 }
95 return server_dispatch_packet(client, type, payload, length);
96}
98server_connection_closing(network_connection_t *connection, void *arg) {
99 (void)arg;
101 client_info_t *client = state ? atomic_ptr_load(&state->client) : NULL;
102 if (!client)
103 return ASCIICHAT_OK;
104 atomic_store_bool(&client->active, false);
105 atomic_store_bool(&client->event_ready, false);
106 /* Keep transport teardown serialized with the media/control senders. */
107 mutex_lock(&client->send_mutex);
108 client->transport = NULL;
109 client->network_connection = NULL;
110 mutex_unlock(&client->send_mutex);
111 // Render-thread joins and client-buffer destruction are owned by
112 // server_connection_closed()->remove_client(). Doing that work here too
113 // races the closed callback and can tear down the same client twice.
114 return ASCIICHAT_OK;
115}
116static void server_connection_closed(network_connection_t *connection,
117 void *arg) {
119 client_info_t *client = state ? atomic_ptr_load(&state->client) : NULL;
120 if (client) {
121 atomic_store_bool(&client->event_cleanup, true);
122 remove_client(arg, client->client_id);
123 }
124 SAFE_FREE(state);
125 network_connection_set_context(connection, NULL);
126}
128 server_connection_open, server_connection_packet, server_connection_closing,
129 server_connection_closed};
130
132 const char *data, size_t length, uint32_t width,
133 uint32_t height, int sources,
134 const acip_render_settings_t *rendering) {
135 mutex_lock(&client->send_mutex);
136 acip_transport_t *transport = client->transport;
137 if (transport && !atomic_load_bool(&client->shutting_down) &&
138 atomic_load_bool(&client->event_ready)) {
139 if (sources > 0 &&
140 atomic_load_u64(&client->last_sent_grid_sources) != (uint64_t)sources) {
141 acip_send_clear_console(transport);
143 }
145 transport, workspace, data, length, width, height, rendering,
146 (rendering->caps.capabilities & TERM_CAP_MATRIX_RAIN)
148 : 0,
149 client->client_id) == ASCIICHAT_OK)
151 }
152 mutex_unlock(&client->send_mutex);
153}
154
156 size_t length) {
157 mutex_lock(&client->send_mutex);
159 if (client->transport && !atomic_load_bool(&client->shutting_down) &&
162 client->transport, PACKET_TYPE_AUDIO_OPUS_BATCH, data, length,
163 fnv1a_hash_string(client->client_id));
164 mutex_unlock(&client->send_mutex);
165 return result;
166}
#define SET_ERRNO(code,...)
uint64_t atomic_fetch_add_u64(atomic_t *a, uint64_t delta)
Atomically add to a uint64_t and return the previous value.
void atomic_store_bool(atomic_t *a, bool value)
Atomically store a boolean value.
void atomic_ptr_store(atomic_ptr_t *a, void *value)
Atomically store a pointer.
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.
void * atomic_ptr_load(atomic_ptr_t *a)
Atomically load a pointer.
void atomic_store_u64(atomic_t *a, uint64_t value)
Atomically store a uint64_t value.
uint64_t atomic_load_u64(const atomic_t *a)
Atomically load a uint64_t value.
void server_send_video(client_info_t *client, acip_frame_workspace_t *workspace, const char *data, size_t length, uint32_t width, uint32_t height, int sources, const acip_render_settings_t *rendering)
const network_connection_callbacks_t server_network_callbacks
asciichat_error_t server_send_audio(client_info_t *client, const void *data, size_t length)
bool crypto_handshake_is_ready(const crypto_handshake_context_t *ctx)
Check if handshake is complete and encryption is ready.
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
unsigned int uint32_t
#define SAFE_FREE(ptr)
#define SAFE_CALLOC(count, size, cast)
unsigned long long uint64_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:81
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_RATE_LIMITED
Definition error_codes.h:86
@ ERROR_SESSION_FULL
Definition error_codes.h:88
@ ERROR_THREAD
#define ACIP_CRYPTO_HAS_ENCRYPT(mode)
Check if crypto mode includes payload encryption.
Definition packet.h:1171
packet_type_t
Network protocol packet type enumeration.
Definition packet.h:315
@ PACKET_TYPE_AUDIO_OPUS_BATCH
Batched Opus-encoded audio frames.
Definition packet.h:412
@ PACKET_TYPE_CLIENT_CAPABILITIES
Client reports terminal capabilities.
Definition packet.h:419
#define GET_OPTION(field)
Definition options.h:1339
struct acip_transport acip_transport_t
Forward declaration of ACIP transport.
Definition host.h:508
struct client_info client_info_t
struct network_connection network_connection_t
asciichat_error_t acip_send_ascii_frame_contract(acip_transport_t *transport, acip_frame_workspace_t *workspace, const char *frame_data, size_t frame_size, uint32_t width, uint32_t height, const acip_render_settings_t *settings, uint32_t effects, const char *client_id)
asciichat_error_t acip_send_clear_console(acip_transport_t *transport)
Send clear console command to client (server → client).
void network_connection_ready(network_connection_t *c)
network_connection_state_t network_connection_state(network_connection_t *c)
const char * network_connection_peer(network_connection_t *c)
void network_connection_set_context(network_connection_t *c, void *context)
void * network_connection_context(network_connection_t *c)
crypto_handshake_context_t * network_connection_handshake(network_connection_t *c)
bool network_connection_secure(network_connection_t *c)
acip_transport_t * network_connection_transport(network_connection_t *c)
uint8_t network_connection_crypto_mode(network_connection_t *c)
asciichat_error_t network_negotiation_receive(network_negotiation_t *n, acip_transport_t *transport, packet_type_t type, const void *payload, size_t length)
@ NETWORK_PROTOCOL_MEDIA
Definition negotiation.h:10
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
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_CONNECTION
New connection.
Definition rate_limit.h:59
#define FRAME_RENDER_CONTRACT_RAIN
asciichat_error_t packet_send_via_transport(acip_transport_t *transport, packet_type_t type, const void *payload, size_t payload_len, uint32_t client_id)
Definition send.c:183
ACIP shared/bidirectional packet sending functions.
size_t g_num_whitelisted_clients
Number of whitelisted clients.
private_key_t g_server_identity_keys[MAX_SESSION_KEYS]
Global server identity keys array (multi-key support).
public_key_t g_client_whitelist[MAX_CLIENTS]
Global client public key whitelist.
size_t g_num_server_identity_keys
Number of loaded server identity keys.
void handle_client_capabilities_packet(client_info_t *client, const void *data, size_t len)
asciichat_error_t server_send_state(acip_transport_t *transport)
int start_transport_client_threads(server_context_t *server_ctx, const char *client_id)
int remove_client(server_context_t *server_ctx, const char *client_id)
asciichat_error_t server_dispatch_packet(client_info_t *client, packet_type_t type, const void *data, size_t length)
client_info_t * add_transport_client(server_context_t *server_ctx, acip_transport_t *transport, const char *client_ip)
Server packet processing and protocol implementation.
Per-client rendering threads with rate limiting.
Send ASCII frame to client (server → client).
acip render settings keeps the values that move through the subsystem: caps, width,...
crypto_handshake_context_t * crypto_handshake_ctx
network_connection_t * network_connection
char client_id[MAX_CLIENT_ID_LEN]
Cryptographic handshake context structure.
Callbacks and operations used by network connection callbacks. Packet payloads are borrowed until ret...
When code needs to work with network negotiation, it keeps protocol, handshake, identity_keys,...
Definition negotiation.h:23
crypto_handshake_context_t * handshake
Definition negotiation.h:25
network_negotiation_t negotiation
Definition connections.h:7
Server context - encapsulates all server state.
rate_limiter_t * rate_limiter
Connection and packet rate limiter.
⏱️ High-precision timing utilities using sokol_time.h and uthash