ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
connection.h File Reference

Go to the source code of this file.

Data Structures

struct  session_connection_ops_t
struct  session_connection_t
 Groups the callbacks and caller data for session connection ops. The owner invokes these function pointers at lifecycle boundaries, so the referenced context must remain alive until registration is removed. More...

Enumerations

enum  session_connection_state_t { SESSION_CONNECTION_STARTING , SESSION_CONNECTION_RUNNING , SESSION_CONNECTION_STOPPING , SESSION_CONNECTION_STOPPED }

Functions

void session_connection_init (session_connection_t *connection, session_connection_ops_t ops, void *context)
asciichat_error_t session_connection_start (session_connection_t *connection)
void session_connection_request_stop (session_connection_t *connection)
asciichat_error_t session_connection_stop (session_connection_t *connection)
void session_connection_interrupt_transport (acip_transport_t *transport)
asciichat_error_t session_connection_establish (const char *name, const char *address, uint16_t port, const network_connect_config_t *config, network_connection_t **owner, char *password, size_t capacity, network_connection_prepare_fn prepare, void(*detach)(void *), void *context)

Enumeration Type Documentation

◆ session_connection_state_t

Enumerator
SESSION_CONNECTION_STARTING 
SESSION_CONNECTION_RUNNING 
SESSION_CONNECTION_STOPPING 
SESSION_CONNECTION_STOPPED 

Definition at line 8 of file src/common/session/connection.h.

Function Documentation

◆ session_connection_establish()

asciichat_error_t session_connection_establish ( const char * name,
const char * address,
uint16_t port,
const network_connect_config_t * config,
network_connection_t ** owner,
char * password,
size_t capacity,
network_connection_prepare_fn prepare,
void(* detach )(void *),
void * context )

Definition at line 14 of file src/common/session/establish.c.

17 {
18 if (!owner || *owner || !password || capacity < PASSWORD_BUFFER_SIZE || !prepare)
19 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid session establishment parameters");
20 const char *selected_address = address;
21 char fallback_address[URL_BUFFER_SIZE];
22 int fallback_port = GET_OPTION(quic_fallback_port);
23 bool fallback_available = address && strncasecmp(address, "quic://", 7) == 0 && fallback_port > 0;
24 if (fallback_available) {
25 if (!GET_OPTION(server_key)[0] || GET_OPTION(no_encrypt) || GET_OPTION(no_auth))
26 return SET_ERRNO(ERROR_USAGE, "QUIC TCP fallback requires --server-key and ACIP encryption and authentication");
27 connection_endpoint_t endpoint;
28 asciichat_error_t result = connection_endpoint_resolve(address, port, &endpoint);
29 if (result != ASCIICHAT_OK)
30 return result;
31 if (strchr(endpoint.host, ':'))
32 SAFE_SNPRINTF(fallback_address, sizeof(fallback_address), "tcp://[%s]:%d", endpoint.host, fallback_port);
33 else
34 SAFE_SNPRINTF(fallback_address, sizeof(fallback_address), "tcp://%s:%d", endpoint.host, fallback_port);
35 }
36 for (;;) {
37 network_establish_config_t settings = {.name = name,
38 .address = selected_address,
39 .default_port = port,
40 .connect = config,
41 .password = password[0] ? password : NULL,
42 .protocol = NETWORK_PROTOCOL_MEDIA,
43 // Authentication is resolved by the shared security initializer in
44 // the prepare callback. Do not force it for unauthenticated defaults;
45 // discovery participants use this same establishment path.
46 .authenticate = false,
47 .prepare = prepare,
48 .context = context};
51 asciichat_error_t result = network_connection_establish(&settings, owner, &attempt);
52 if (result == ASCIICHAT_OK) {
54 return ASCIICHAT_OK;
55 }
56 if (fallback_available && result == ERROR_NETWORK_TIMEOUT && !*owner) {
58 selected_address = fallback_address;
59 fallback_available = false;
60 log_info("QUIC timed out; trying authenticated TCP fallback on port %d", fallback_port);
61 continue;
62 }
63 if (!attempt.password_failure) {
64 // A failed negotiation cannot produce the first frame that normally
65 // ends the splash. Release it before presenting the connection error.
68 return result;
69 }
70 // Recover only the rejected negotiation; caller and cleanup errors remain pending.
72 if (detach)
73 detach(context);
76 snprintf(target, sizeof(target), "%s:%u", attempt.endpoint.host, attempt.endpoint.port);
77 result = ui_input_password(target, password, capacity, attempt.password_failure & AUTH_FAIL_PASSWORD_INCORRECT);
78 if (result != ASCIICHAT_OK)
79 return result;
80 }
81}
void errno_scope_end(asciichat_errno_scope_t scope, asciichat_errno_outcome_t outcome)
asciichat_errno_scope_t errno_scope_begin(void)
#define SET_ERRNO(code,...)
@ ASCIICHAT_ERRNO_HANDLED
@ ASCIICHAT_ERRNO_DISMISSED
asciichat_error_t connection_endpoint_resolve(const char *input, uint16_t default_port, connection_endpoint_t *endpoint_out)
For connection endpoint, this is the live record that ties protocol, port, has_explicit_port together...
#define SAFE_SNPRINTF(buffer, buffer_size,...)
Definition common.h:507
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
@ ASCIICHAT_OK
Definition error_codes.h:46
@ ERROR_USAGE
Definition error_codes.h:48
@ ERROR_INVALID_PARAM
@ ERROR_NETWORK_TIMEOUT
Definition error_codes.h:75
#define URL_BUFFER_SIZE
#define log_info(...)
Log an INFO message.
Definition log/log.h:608
@ AUTH_FAIL_PASSWORD_INCORRECT
Password verification failed (incorrect password).
Definition packet.h:805
#define GET_OPTION(field)
Safely get a specific option field (lock-free read).
Definition options.h:1336
int splash_intro_done(void)
Signal that intro splash should end (first frame ready to render).
Definition splash.c:426
void splash_wait_for_animation(void)
Wait for splash animation thread to fully exit.
Definition splash.c:451
void network_connection_destroy(network_connection_t **owner)
asciichat_error_t network_connection_establish(const network_establish_config_t *config, network_connection_t **owner, network_establish_result_t *attempt)
@ NETWORK_PROTOCOL_MEDIA
Definition negotiation.h:5
#define PASSWORD_BUFFER_SIZE
Password input buffer size (256 bytes).
Definition sizes.h:158
#define SESSION_ESTABLISH_TARGET_BUFFER_SIZE
When code needs to work with asciichat errno snapshot, it keeps total, copied here instead of scatter...
This is the handoff point for network establish config. Callers fill name, address,...
Definition establish.h:24
connection_endpoint_t endpoint
Definition establish.h:25
asciichat_error_t ui_input_password(const char *target, char *buffer, size_t capacity, bool incorrect)
Definition tui/input.c:750

References ASCIICHAT_ERRNO_DISMISSED, ASCIICHAT_ERRNO_HANDLED, ASCIICHAT_OK, AUTH_FAIL_PASSWORD_INCORRECT, connection_endpoint_resolve(), network_establish_result_t::endpoint, errno_scope_begin(), errno_scope_end(), ERROR_INVALID_PARAM, ERROR_NETWORK_TIMEOUT, ERROR_USAGE, GET_OPTION, connection_endpoint_t::host, log_info, network_connection_destroy(), network_connection_establish(), NETWORK_PROTOCOL_MEDIA, PASSWORD_BUFFER_SIZE, network_establish_result_t::password_failure, connection_endpoint_t::port, SAFE_SNPRINTF, SESSION_ESTABLISH_TARGET_BUFFER_SIZE, SET_ERRNO, splash_intro_done(), splash_wait_for_animation(), ui_input_password(), and URL_BUFFER_SIZE.

Referenced by client_connection_start(), and session_participant_connect().

◆ session_connection_init()

void session_connection_init ( session_connection_t * connection,
session_connection_ops_t ops,
void * context )

◆ session_connection_interrupt_transport()

void session_connection_interrupt_transport ( acip_transport_t * transport)

Definition at line 44 of file src/common/session/connection.c.

44 {
45 acip_transport_interrupt(transport);
46}

Referenced by server_connection_shutdown().

◆ session_connection_request_stop()

void session_connection_request_stop ( session_connection_t * connection)

Definition at line 19 of file src/common/session/connection.c.

19 {
20 if (!connection || atomic_load_int(&connection->state) == SESSION_CONNECTION_STOPPED)
21 return;
22 atomic_store_int(&connection->state, SESSION_CONNECTION_STOPPING);
23 if (connection->ops.interrupt)
24 connection->ops.interrupt(connection->context);
25}

References session_connection_t::context, session_connection_ops_t::interrupt, session_connection_t::ops, SESSION_CONNECTION_STOPPED, SESSION_CONNECTION_STOPPING, and session_connection_t::state.

Referenced by client_connection_request_stop(), and session_connection_stop().

◆ session_connection_start()

asciichat_error_t session_connection_start ( session_connection_t * connection)

Definition at line 10 of file src/common/session/connection.c.

10 {
11 if (!connection || !thread_equal(connection->owner_thread, thread_self()))
12 return SET_ERRNO(ERROR_INVALID_STATE, "Connection startup requires its controlling thread");
13 int expected = SESSION_CONNECTION_STARTING;
14 if (!atomic_cas_int(&connection->state, &expected, SESSION_CONNECTION_RUNNING))
15 return SET_ERRNO(ERROR_INVALID_STATE, "Connection was stopped during startup");
16 return ASCIICHAT_OK;
17}
@ ERROR_INVALID_STATE

References ASCIICHAT_OK, ERROR_INVALID_STATE, session_connection_t::owner_thread, SESSION_CONNECTION_RUNNING, SESSION_CONNECTION_STARTING, SET_ERRNO, and session_connection_t::state.

Referenced by client_connection_start(), session_participant_connect(), session_participant_set_connection(), and session_participant_set_transport().

◆ session_connection_stop()

asciichat_error_t session_connection_stop ( session_connection_t * connection)

Definition at line 27 of file src/common/session/connection.c.

27 {
28 if (!connection || atomic_load_int(&connection->state) == SESSION_CONNECTION_STOPPED)
29 return ASCIICHAT_OK;
31 if (!thread_equal(connection->owner_thread, thread_self()))
32 return SET_ERRNO(ERROR_INVALID_STATE, "Connection cleanup requires its controlling thread");
33 if (connection->ops.join) {
34 asciichat_error_t result = connection->ops.join(connection->context);
35 if (result != ASCIICHAT_OK)
36 return result;
37 }
38 if (connection->ops.release)
39 connection->ops.release(connection->context);
40 atomic_store_int(&connection->state, SESSION_CONNECTION_STOPPED);
41 return ASCIICHAT_OK;
42}
void session_connection_request_stop(session_connection_t *connection)
asciichat_error_t(* join)(void *context)

References ASCIICHAT_OK, session_connection_t::context, ERROR_INVALID_STATE, session_connection_ops_t::join, session_connection_t::ops, session_connection_t::owner_thread, session_connection_ops_t::release, session_connection_request_stop(), SESSION_CONNECTION_STOPPED, SET_ERRNO, and session_connection_t::state.

Referenced by client_connection_stop(), session_participant_connect(), session_participant_disconnect(), and session_participant_set_transport().