ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
src/common/session/establish.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define SESSION_ESTABLISH_TARGET_BUFFER_SIZE 320
4
11#include <stdio.h>
12#include <string.h>
13
14asciichat_error_t session_connection_establish(const char *name, const char *address, uint16_t port,
16 char *password, size_t capacity, network_connection_prepare_fn prepare,
17 void (*detach)(void *), void *context) {
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...
asciichat_error_t(* network_connection_prepare_fn)(network_connection_t *, const connection_endpoint_t *, const char *, void *)
Definition establish.h:5
unsigned short uint16_t
Definition common.h:61
#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
struct network_connection network_connection_t
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)
📝 Logging API with multiple log levels and terminal output control
@ NETWORK_PROTOCOL_MEDIA
Definition negotiation.h:5
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
Password prompting utilities with secure input and formatting.
#define PASSWORD_BUFFER_SIZE
Password input buffer size (256 bytes).
Definition sizes.h:158
Intro and status screen display for ascii-chat with animated rainbow effects.
#define SESSION_ESTABLISH_TARGET_BUFFER_SIZE
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)
🔤 String Manipulation and Shell Escaping Utilities
When code needs to work with asciichat errno snapshot, it keeps total, copied here instead of scatter...
For network connect config, this is the live record that ties timeout_ms, callback_data,...
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