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

Go to the source code of this file.

Macros

#define SESSION_ESTABLISH_TARGET_BUFFER_SIZE   320

Functions

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)

Macro Definition Documentation

◆ SESSION_ESTABLISH_TARGET_BUFFER_SIZE

#define SESSION_ESTABLISH_TARGET_BUFFER_SIZE   320

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

Referenced by session_connection_establish().

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().