ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
session.c File Reference

Go to the source code of this file.

Macros

#define DISCOVERY_SESSION_IDENTITY_PATH_BUFFER_SIZE   256
#define DISCOVERY_SESSION_ERROR_MSG_BUFFER_SIZE   256

Functions

discovery_session_t * discovery_session_create (const discovery_config_t *config)
 Configuration for discovery session.
asciichat_error_t discovery_session_destroy (discovery_session_t *session)
 Destroy discovery session and free resources.
asciichat_error_t discovery_session_apply_handoff (discovery_session_t *session)
 Replace the old role after a committed handoff and media-worker shutdown. Releases borrowed-role resources, resets liveness, and initializes the committed successor connection. Callers must stop media workers before entering this function.
asciichat_error_t discovery_session_start (discovery_session_t *session)
 Start the discovery session.
asciichat_error_t discovery_session_process (discovery_session_t *session, int64_t timeout_ns)
 Process session events (call in main loop).
void discovery_session_stop (discovery_session_t *session)
 Stop the discovery session.
discovery_state_t discovery_session_get_state (const discovery_session_t *session)
 Get current session state.
bool discovery_session_is_active (const discovery_session_t *session)
 Check if session is active (call in progress).
const char * discovery_session_get_string (const discovery_session_t *session)
 Get session string.
bool discovery_session_is_host (const discovery_session_t *session)
 Check if we are the host.
session_host_t * discovery_session_get_host (discovery_session_t *session)
 Get host context (if we are host).
session_participant_t * discovery_session_get_participant (discovery_session_t *session)
 Get participant context (if we are participant).
void discovery_session_host_pong (discovery_session_t *session)
asciichat_error_t discovery_session_check_host_alive (discovery_session_t *session)
 Check host transport availability and advance TCP ping/pong liveness. Uses receiver-clock pong timestamps and bounded consecutive-failure tracking.
asciichat_error_t discovery_session_handle_host_disconnect (discovery_session_t *session, uint32_t disconnect_reason)
 Report host loss and await the authoritative ACDS handoff decision. Records the local recovery deadline origin. Advisory successor plans never authorize immediate promotion or reconnection from this function.

Macro Definition Documentation

◆ DISCOVERY_SESSION_ERROR_MSG_BUFFER_SIZE

#define DISCOVERY_SESSION_ERROR_MSG_BUFFER_SIZE   256

Definition at line 4 of file src/discovery/session.c.

◆ DISCOVERY_SESSION_IDENTITY_PATH_BUFFER_SIZE

#define DISCOVERY_SESSION_IDENTITY_PATH_BUFFER_SIZE   256

Definition at line 3 of file src/discovery/session.c.

Referenced by discovery_session_create().

Function Documentation

◆ discovery_session_apply_handoff()

asciichat_error_t discovery_session_apply_handoff ( discovery_session_t * session)

Replace the old role after a committed handoff and media-worker shutdown. Releases borrowed-role resources, resets liveness, and initializes the committed successor connection. Callers must stop media workers before entering this function.

Returns
ASCIICHAT_OK if no handoff is pending or initialization succeeds.

Definition at line 1138 of file src/discovery/session.c.

1138 {
1139 if (!session || !session->handoff_pending)
1140 return ASCIICHAT_OK;
1141 session->handoff_pending = false;
1142 if (session->participant_ctx) {
1144 session->participant_ctx = NULL;
1145 }
1146 if (session->peer_manager) {
1148 session->peer_manager = NULL;
1149 }
1150 session->is_host = !memcmp(session->host_id, session->participant_id, SESSION_ID_SIZE);
1151 if (!session->is_host && session->host_ctx) {
1153 session->host_ctx = NULL;
1154 }
1155 session->webrtc_transport_ready = false;
1156 session->webrtc_connection_initiated = false;
1157 session->webrtc_retry_attempt = 0;
1158 session->webrtc_disconnected_since_ms = 0;
1159 session->liveness.ping_in_flight = false;
1160 session->liveness.consecutive_failures = 0;
1161 session->liveness.last_ping_sent_ms = session_get_current_time_ms();
1163 if (session->session_type == SESSION_TYPE_WEBRTC) {
1164 asciichat_error_t result = initialize_webrtc_peer_manager(session);
1165 if (result != ASCIICHAT_OK)
1166 return result;
1167 }
1168 ascii_otel_metric(ASCII_OTEL_METRIC_HOST_CHANGES_APPLIED, 1);
1169 session->host_loss_detected_ms = 0;
1170
1172 return ASCIICHAT_OK;
1173}
void atomic_store_u64(atomic_t *a, uint64_t value)
Atomically store a uint64_t value.
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
#define SESSION_ID_SIZE
@ SESSION_TYPE_WEBRTC
WebRTC P2P mesh with STUN/TURN relay.
Definition acds.h:151
asciichat_error_t session_host_destroy(session_host_t *host)
Destroy session host and free resources.
Definition host.c:289
void session_participant_destroy(session_participant_t *p)
Destroy session participant and free resources.
void webrtc_peer_manager_destroy(webrtc_peer_manager_t *manager)
Destroy peer manager and close all connections.
void ascii_otel_metric(ascii_otel_metric_t metric, double value)
Definition otel.c:159
@ DISCOVERY_STATE_CONNECTING_HOST
Connecting to host as participant.
@ DISCOVERY_STATE_STARTING_HOST
Starting as host.
uint64_t webrtc_disconnected_since_ms
Start of a transient peer disconnection.
uint8_t session_type
0 = DIRECT_TCP, 1 = WEBRTC
uint8_t participant_id[SESSION_ID_SIZE]
int webrtc_retry_attempt
Current retry attempt number (0 = initial, 1+ = retries).
bool webrtc_transport_ready
True when DataChannel is open and transport created.
bool webrtc_connection_initiated
True when we've called webrtc_peer_manager_connect().
session_participant_t * participant_ctx
uint64_t host_loss_detected_ms
Local deadline origin while awaiting ACDS recovery.
struct webrtc_peer_manager * peer_manager
The useful part of discovery session is in state, error, waiting_peer_start_ns, is_initiator: togethe...
uint8_t host_id[SESSION_ID_SIZE]
uint32_t consecutive_failures
Number of consecutive ping failures.
atomic_t last_pong_received_ms
Timestamp of last pong received (monotonic).
uint64_t last_ping_sent_ms
Timestamp of last ping sent (monotonic).
bool ping_in_flight
True if waiting for pong.

References ascii_otel_metric(), ASCIICHAT_OK, atomic_store_u64(), host_liveness_t::consecutive_failures, DISCOVERY_STATE_CONNECTING_HOST, DISCOVERY_STATE_STARTING_HOST, discovery_session_t::handoff_pending, discovery_session_t::host_ctx, discovery_session_t::host_id, discovery_session_t::host_loss_detected_ms, discovery_session_t::is_host, host_liveness_t::last_ping_sent_ms, host_liveness_t::last_pong_received_ms, discovery_session_t::liveness, discovery_session_t::participant_ctx, discovery_session_t::participant_id, discovery_session_t::peer_manager, host_liveness_t::ping_in_flight, session_host_destroy(), SESSION_ID_SIZE, session_participant_destroy(), discovery_session_t::session_type, SESSION_TYPE_WEBRTC, discovery_session_t::webrtc_connection_initiated, discovery_session_t::webrtc_disconnected_since_ms, webrtc_peer_manager_destroy(), discovery_session_t::webrtc_retry_attempt, and discovery_session_t::webrtc_transport_ready.

◆ discovery_session_check_host_alive()

asciichat_error_t discovery_session_check_host_alive ( discovery_session_t * session)

Check host transport availability and advance TCP ping/pong liveness. Uses receiver-clock pong timestamps and bounded consecutive-failure tracking.

Detect host disconnect (check connection status).

Returns
ASCIICHAT_OK for a live host, or a network error after loss is detected.

Definition at line 2138 of file src/discovery/session.c.

2138 {
2139 if (!session || session->is_host) {
2140 // We are the host, so "host" is always alive
2141 return ASCIICHAT_OK;
2142 }
2143
2144 // For WebRTC sessions, check the WebRTC transport instead of participant_ctx
2145 if (session->session_type == SESSION_TYPE_WEBRTC) {
2146 // If we have a WebRTC transport and it's marked as ready, host is alive
2147 if (session->webrtc_transport_ready) {
2148 return ASCIICHAT_OK;
2149 }
2150 // WebRTC transport not ready yet - might still be connecting
2151 return ERROR_NETWORK;
2152 }
2153
2154 // For TCP sessions, check participant context
2155 // If we don't have a participant context yet, we're not connected
2156 if (!session->participant_ctx) {
2157 return ERROR_NETWORK;
2158 }
2159
2160 // Check migration state - if we're already migrating, host is definitely not alive
2161 if (session->state == DISCOVERY_STATE_MIGRATING) {
2162 return ERROR_NETWORK; // Already detected disconnect
2163 }
2164
2165 uint64_t now_ms = session_get_current_time_ms();
2166
2168 session->liveness.ping_in_flight = false;
2169 session->liveness.consecutive_failures = 0;
2170 }
2171 // Check if we have a ping in flight that timed out
2172 if (session->liveness.ping_in_flight) {
2173 uint64_t ping_age_ms = now_ms - session->liveness.last_ping_sent_ms;
2174 if (ping_age_ms > session->liveness.timeout_ms) {
2175 // Ping timed out
2176 session->liveness.consecutive_failures++;
2177 session->liveness.ping_in_flight = false;
2178 log_warn("Host ping timeout (attempt %u/%u, age=%llu ms)", session->liveness.consecutive_failures, session->liveness.max_failures, (unsigned long long)ping_age_ms);
2179
2180 // Check if we've exceeded failure threshold
2181 if (session->liveness.consecutive_failures >= session->liveness.max_failures) {
2182 log_error("Host declared dead after %u consecutive ping failures", session->liveness.consecutive_failures);
2183 return ERROR_NETWORK;
2184 }
2185 }
2186 }
2187
2188 // Check if it's time to send a new ping
2189 uint64_t time_since_last_ping = now_ms - session->liveness.last_ping_sent_ms;
2190 if (!session->liveness.ping_in_flight && time_since_last_ping >= session->liveness.ping_interval_ms) {
2191 // Send ping to host via participant connection
2193 if (!transport || !acip_transport_is_connected(transport))
2194 return SET_ERRNO(ERROR_NETWORK, "Host transport disconnected");
2195 asciichat_error_t result = acip_send_ping(transport);
2196 if (result == ASCIICHAT_OK) {
2197 session->liveness.last_ping_sent_ms = now_ms;
2198 session->liveness.ping_in_flight = true;
2199 log_debug("Sent ping to host (attempt %u/%u)", session->liveness.consecutive_failures + 1, session->liveness.max_failures);
2200 } else {
2201 log_warn("Failed to send ping to host: %d", result);
2202 session->liveness.consecutive_failures++;
2203 if (session->liveness.consecutive_failures >= session->liveness.max_failures) {
2204 log_error("Host declared dead after %u consecutive send failures", session->liveness.consecutive_failures);
2205 return ERROR_NETWORK;
2206 }
2207 }
2208 }
2209
2210 return ASCIICHAT_OK;
2211}
#define SET_ERRNO(code,...)
uint64_t atomic_load_u64(atomic_t *a)
Atomically load a uint64_t value.
unsigned long long uint64_t
@ ERROR_NETWORK
Definition error_codes.h:76
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
acip_transport_t * session_participant_get_transport(session_participant_t *p)
Get the current transport for the participant.
struct acip_transport acip_transport_t
Forward declaration of ACIP transport.
Definition host.h:508
asciichat_error_t acip_send_ping(acip_transport_t *transport)
Send ping packet.
Definition send.c:372
@ DISCOVERY_STATE_MIGRATING
Host migration in progress.
uint32_t max_failures
Threshold for triggering migration (default: 3).
uint64_t ping_interval_ms
Time between pings (default: 3000ms).
uint64_t timeout_ms
Timeout for ping response (default: 10000ms).

References acip_send_ping(), ASCIICHAT_OK, atomic_load_u64(), host_liveness_t::consecutive_failures, DISCOVERY_STATE_MIGRATING, ERROR_NETWORK, discovery_session_t::is_host, host_liveness_t::last_ping_sent_ms, host_liveness_t::last_pong_received_ms, discovery_session_t::liveness, log_debug, log_error, log_warn, host_liveness_t::max_failures, discovery_session_t::participant_ctx, host_liveness_t::ping_in_flight, host_liveness_t::ping_interval_ms, session_participant_get_transport(), discovery_session_t::session_type, SESSION_TYPE_WEBRTC, SET_ERRNO, discovery_session_t::state, host_liveness_t::timeout_ms, and discovery_session_t::webrtc_transport_ready.

◆ discovery_session_create()

discovery_session_t * discovery_session_create ( const discovery_config_t * config)

Configuration for discovery session.

Create a new discovery session

Parameters
configSession configuration
Returns
New session or NULL on error

Definition at line 72 of file src/discovery/session.c.

72 {
73 if (!config) {
74 SET_ERRNO(ERROR_INVALID_PARAM, "config is NULL");
75 return NULL;
76 }
77
79 if (!session) {
80 SET_ERRNO(ERROR_MEMORY, "Failed to allocate discovery session");
81 return NULL;
82 }
83
84 session->state = DISCOVERY_STATE_INIT;
85 session->handoff_generation = 1; // Match consensus_handoff_init().
86 session->acds_transport = NULL;
87 session->peer_manager = NULL;
88 session->webrtc_transport_ready = false;
89 session->webrtc_connection_initiated = false;
90 session->webrtc_retry_attempt = 0;
91 session->webrtc_last_attempt_time_ms = 0;
93 session->stun_servers = NULL;
94 session->stun_count = 0;
95 session->turn_servers = NULL;
96 session->turn_count = 0;
97
98 // Initialize host liveness detection
99 session->liveness.last_ping_sent_ms = 0;
101 session->liveness.consecutive_failures = 0;
102 session->liveness.max_failures = 3;
103 session->liveness.ping_interval_ms = 3 * MS_PER_SEC_INT; // Ping every 3 seconds
104 session->liveness.timeout_ms = 10 * MS_PER_SEC_INT; // 10 second timeout
105 session->liveness.ping_in_flight = false;
106
107 log_info("After CALLOC - participant_ctx=%p, host_ctx=%p", session->participant_ctx, session->host_ctx);
108
109 // Explicit identity credentials apply to signaling as well as media authentication.
110 const options_t *opts = options_get();
111 bool explicit_identity = opts && opts->encrypt_key[0];
113 asciichat_error_t id_result;
114 if (explicit_identity) {
115 SAFE_STRNCPY(identity_path, opts->encrypt_key, sizeof(identity_path));
116 id_result = ASCIICHAT_OK;
117 } else {
118 id_result = acds_identity_default_path(identity_path, sizeof(identity_path));
119 }
120 if (id_result == ASCIICHAT_OK) {
121 asciichat_errno_scope_t load_scope = {0};
122 bool suppressed = false;
123 if (!explicit_identity) {
124 load_scope = errno_scope_begin();
125 suppressed = errno_notifications_suppress(true);
126 }
127 if (!explicit_identity && platform_access(identity_path, PLATFORM_ACCESS_EXISTS) != 0)
128 id_result = ERROR_CONFIG;
129 else
130 id_result = acds_identity_load(identity_path, session->identity_pubkey, session->identity_seckey);
131 if (!explicit_identity) {
134 }
135 if (id_result != ASCIICHAT_OK && !explicit_identity) {
136 // File doesn't exist or is corrupted - generate new key
137 log_info("Identity key not found, generating new key");
138 id_result = acds_identity_generate(session->identity_pubkey, session->identity_seckey);
139 if (id_result == ASCIICHAT_OK) {
140 // Save for future use
141 acds_identity_save(identity_path, session->identity_pubkey, session->identity_seckey);
142 }
143 }
144 }
145
146 if (id_result != ASCIICHAT_OK && explicit_identity) {
147 SAFE_FREE(session);
148 return NULL;
149 }
150 if (id_result != ASCIICHAT_OK) {
151 NOTICE(DANGER, "DISCOVERY IDENTITY UNAVAILABLE", "Failed to load/generate identity key, using zero key");
152 memset(session->identity_pubkey, 0, sizeof(session->identity_pubkey));
153 memset(session->identity_seckey, 0, sizeof(session->identity_seckey));
154 } else {
155 char fingerprint[HEX_STRING_SIZE_32];
156 acds_identity_fingerprint(session->identity_pubkey, fingerprint);
157 log_info("Using identity key with fingerprint: %.16s...", fingerprint);
158 }
159
160 // Load multiple identity keys from options (multi-key protocol support)
161 if (opts && opts->num_identity_keys > 0) {
162 // Load all identity keys from options
163 for (size_t i = 0; i < opts->num_identity_keys && i < MAX_SESSION_KEYS; i++) {
164 uint8_t pubkey[ED25519_PUBLIC_KEY_SIZE] = {0};
165 uint8_t seckey[ED25519_PRIVATE_KEY_SIZE] = {0};
166 asciichat_error_t key_result = acds_identity_load(opts->identity_keys[i], pubkey, seckey);
167 if (key_result == ASCIICHAT_OK) {
168 memcpy(session->identity_pubkeys[session->num_identity_keys], pubkey, sizeof(pubkey));
169 memcpy(session->identity_seckeys[session->num_identity_keys++], seckey, sizeof(seckey));
170 sodium_memzero(seckey, sizeof(seckey));
171 char fingerprint[HEX_STRING_SIZE_32];
172 acds_identity_fingerprint(pubkey, fingerprint);
173 log_info("Loaded identity key #%zu with fingerprint: %.16s...", i + 1, fingerprint);
174 } else {
175 log_warn("Failed to load identity key #%zu from %s", i + 1, opts->identity_keys[i]);
176 }
177 }
178 if (session->num_identity_keys != opts->num_identity_keys) {
179 sodium_memzero(session->identity_seckey, sizeof(session->identity_seckey));
180 sodium_memzero(session->identity_seckeys, sizeof(session->identity_seckeys));
181 SAFE_FREE(session);
182 SET_ERRNO(ERROR_CRYPTO, "Could not load the complete discovery identity key set");
183 return NULL;
184 }
185 memcpy(session->identity_pubkey, session->identity_pubkeys[0], sizeof(session->identity_pubkey));
186 memcpy(session->identity_seckey, session->identity_seckeys[0], sizeof(session->identity_seckey));
187 log_info("Loaded %zu identity key(s) for multi-key protocol", session->num_identity_keys);
188 } else {
189 // Single-key mode: use only the primary identity key
190 memcpy(session->identity_pubkeys[0], session->identity_pubkey, ED25519_PUBLIC_KEY_SIZE);
191 memcpy(session->identity_seckeys[0], session->identity_seckey, sizeof(session->identity_seckey));
192 session->num_identity_keys = 1;
193 }
194
195 // ACDS connection info
196 if (config->acds_url && config->acds_url[0]) {
197 SAFE_STRNCPY(session->acds_url, config->acds_url, sizeof(session->acds_url));
198 }
199 if (config->acds_address && config->acds_address[0]) {
200 SAFE_STRNCPY(session->acds_address, config->acds_address, sizeof(session->acds_address));
201 } else {
202 SAFE_STRNCPY(session->acds_address, "127.0.0.1", sizeof(session->acds_address));
203 }
204 session->acds_port = config->acds_port > 0 ? config->acds_port : ACIP_DISCOVERY_DEFAULT_PORT;
205
206 // Session string (if joining)
207 if (config->session_string && config->session_string[0]) {
208 SAFE_STRNCPY(session->session_string, config->session_string, sizeof(session->session_string));
209 session->is_initiator = false;
210 } else {
211 session->is_initiator = true;
212 }
213 if (config->password && config->password[0]) {
214 SAFE_STRNCPY(session->password, config->password, sizeof(session->password));
215 }
216
217 // Callbacks
218 session->on_state_change = config->on_state_change;
219 session->on_session_ready = config->on_session_ready;
220 session->on_error = config->on_error;
221 session->callback_user_data = config->callback_user_data;
223 session->exit_callback_data = config->exit_callback_data;
224
225 // Initialize mutex for NAT quality protection
226 if (mutex_init(&session->nat_quality_mutex, "discovery_nat_quality") != 0) {
227 log_warn("Failed to initialize NAT quality mutex");
228 }
229
230 log_debug("Discovery session created (initiator=%d, acds=%s:%u)", session->is_initiator, session->acds_address, session->acds_port);
231
232 NAMED_REGISTER_ATOMIC(&session->liveness.last_pong_received_ms, "discovery.last_pong_received_ms", NULL);
233 return session;
234}
void errno_scope_end(asciichat_errno_scope_t scope, asciichat_errno_outcome_t outcome)
bool errno_notifications_suppress(bool suppress)
asciichat_errno_scope_t errno_scope_begin(void)
@ ASCIICHAT_ERRNO_HANDLED
int platform_access(const char *pathname, int mode)
Definition filesystem.c:95
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_CALLOC(count, size, cast)
unsigned char uint8_t
#define NAMED_REGISTER_ATOMIC(a, name, parent_ptr)
Register an atomic_t with automatic format specifier.
@ ERROR_MEMORY
Definition error_codes.h:55
@ ERROR_CONFIG
Definition error_codes.h:56
@ ERROR_CRYPTO
Definition error_codes.h:97
@ ERROR_INVALID_PARAM
#define MAX_SESSION_KEYS
#define log_info(...)
Log an INFO message.
Definition log/log.h:602
#define ACIP_DISCOVERY_DEFAULT_PORT
Discovery server default port (use OPT_ACDS_PORT_INT_DEFAULT from options.h).
Definition acds.h:1102
#define MS_PER_SEC_INT
Definition time.h:180
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
const options_t * options_get(void)
Get pointer to current options struct (lock-free, thread-safe).
Definition rcu.c:495
asciichat_error_t acds_identity_load(const char *path, uint8_t public_key[32], uint8_t secret_key[64])
Load identity from file.
Definition identity.c:36
void acds_identity_fingerprint(const uint8_t public_key[32], char fingerprint[(32 *2+1)])
Compute SHA256 fingerprint of public key.
Definition identity.c:131
asciichat_error_t acds_identity_default_path(char *path_out, size_t path_size)
Get default identity file path for current platform.
Definition identity.c:151
asciichat_error_t acds_identity_save(const char *path, const uint8_t public_key[32], const uint8_t secret_key[64])
Save identity to file.
Definition identity.c:88
asciichat_error_t acds_identity_generate(uint8_t public_key[32], uint8_t secret_key[64])
Generate new Ed25519 keypair.
Definition identity.c:22
#define NOTICE(severity, title,...)
Definition notice.h:56
#define ED25519_PUBLIC_KEY_SIZE
Ed25519 public key size in bytes.
Definition sizes.h:37
#define HEX_STRING_SIZE_32
Hex string size for 32-byte values (64 hex chars + null terminator).
Definition sizes.h:158
#define ED25519_PRIVATE_KEY_SIZE
Ed25519 private key size (seed + public) in bytes.
Definition sizes.h:39
#define DISCOVERY_SESSION_IDENTITY_PATH_BUFFER_SIZE
@ DISCOVERY_STATE_INIT
Initial state.
When code needs to work with asciichat errno snapshot, it keeps total, copied here instead of scatter...
const char * acds_address
ACDS address (default: "127.0.0.1").
void(* on_error)(asciichat_error_t error, const char *message, void *user_data)
uint16_t acds_port
ACDS port (default: 27225).
const char * password
Session password (optional).
discovery_should_exit_fn should_exit_callback
void(* on_state_change)(discovery_state_t new_state, void *user_data)
const char * session_string
Session string to join (or NULL to create).
const char * acds_url
ACDS WebSocket URL (ws:// or wss://, mutually exclusive with acds_port).
void(* on_session_ready)(const char *session_string, void *user_data)
Discovery session context.
char acds_url[512]
instead of TCP)
uint64_t webrtc_last_attempt_time_ms
Timestamp of last connection attempt (monotonic time).
uint8_t identity_pubkeys[MAX_SESSION_KEYS][ED25519_PUBLIC_KEY_SIZE]
All public keys for multi-key protocol.
uint8_t identity_pubkey[ED25519_PUBLIC_KEY_SIZE]
Ed25519 public key for this participant (primary key).
uint8_t identity_seckey[ED25519_PRIVATE_KEY_SIZE]
Ed25519 secret key for signing (primary key).
char password[ACIP_PASSWORD_SIZE]
Session password used when joining password-protected sessions.
uint8_t identity_seckeys[MAX_SESSION_KEYS][ED25519_PRIVATE_KEY_SIZE]
Matching Ed25519 signing keys.
turn_server_t * turn_servers
TURN servers from ACDS.
size_t stun_count
Number of STUN servers.
void(* on_error)(asciichat_error_t error, const char *message, void *user_data)
size_t num_identity_keys
Number of identity keys loaded.
stun_server_t * stun_servers
STUN servers from ACDS.
void(* on_session_ready)(const char *session_string, void *user_data)
char session_string[SESSION_STRING_BUFFER_SIZE]
discovery_should_exit_fn should_exit_callback
size_t turn_count
Number of TURN servers.
acip_transport_t * acds_transport
Persistent transport for ACDS communication.
void(* on_state_change)(discovery_state_t new_state, void *user_data)
size_t num_identity_keys
Number of identity keys loaded (0 means single-key mode using encrypt_key).
Definition options.h:1183
char identity_keys[32][256]
All identity keys (populated when –key is used multiple times).
Definition options.h:1182
char encrypt_key[256]
SSH/GPG key file path (first –key flag, kept for compatibility).
Definition options.h:1172

References discovery_config_t::acds_address, discovery_session_t::acds_address, acds_identity_default_path(), acds_identity_fingerprint(), acds_identity_generate(), acds_identity_load(), acds_identity_save(), discovery_config_t::acds_port, discovery_session_t::acds_port, discovery_session_t::acds_transport, discovery_config_t::acds_url, discovery_session_t::acds_url, ACIP_DISCOVERY_DEFAULT_PORT, ASCIICHAT_ERRNO_HANDLED, ASCIICHAT_OK, atomic_store_u64(), discovery_config_t::callback_user_data, discovery_session_t::callback_user_data, host_liveness_t::consecutive_failures, DISCOVERY_SESSION_IDENTITY_PATH_BUFFER_SIZE, DISCOVERY_STATE_INIT, ED25519_PRIVATE_KEY_SIZE, ED25519_PUBLIC_KEY_SIZE, options_state::encrypt_key, errno_notifications_suppress(), errno_scope_begin(), errno_scope_end(), ERROR_CONFIG, ERROR_CRYPTO, ERROR_INVALID_PARAM, ERROR_MEMORY, discovery_config_t::exit_callback_data, discovery_session_t::exit_callback_data, discovery_session_t::handoff_generation, HEX_STRING_SIZE_32, discovery_session_t::host_ctx, options_state::identity_keys, discovery_session_t::identity_pubkey, discovery_session_t::identity_pubkeys, discovery_session_t::identity_seckey, discovery_session_t::identity_seckeys, discovery_session_t::is_initiator, host_liveness_t::last_ping_sent_ms, host_liveness_t::last_pong_received_ms, discovery_session_t::liveness, log_debug, log_info, log_warn, host_liveness_t::max_failures, MAX_SESSION_KEYS, MS_PER_SEC_INT, NAMED_REGISTER_ATOMIC, discovery_session_t::nat_quality_mutex, NOTICE, discovery_session_t::num_identity_keys, options_state::num_identity_keys, discovery_config_t::on_error, discovery_session_t::on_error, discovery_config_t::on_session_ready, discovery_session_t::on_session_ready, discovery_config_t::on_state_change, discovery_session_t::on_state_change, options_get(), discovery_session_t::participant_ctx, discovery_config_t::password, discovery_session_t::password, discovery_session_t::peer_manager, host_liveness_t::ping_in_flight, host_liveness_t::ping_interval_ms, platform_access(), SAFE_CALLOC, SAFE_FREE, SAFE_STRNCPY, discovery_config_t::session_string, discovery_session_t::session_string, SET_ERRNO, discovery_config_t::should_exit_callback, discovery_session_t::should_exit_callback, discovery_session_t::state, discovery_session_t::stun_count, discovery_session_t::stun_servers, host_liveness_t::timeout_ms, discovery_session_t::turn_count, discovery_session_t::turn_servers, discovery_session_t::webrtc_connection_initiated, discovery_session_t::webrtc_disconnected_since_ms, discovery_session_t::webrtc_last_attempt_time_ms, discovery_session_t::webrtc_retry_attempt, and discovery_session_t::webrtc_transport_ready.

Referenced by discovery_main().

◆ discovery_session_destroy()

asciichat_error_t discovery_session_destroy ( discovery_session_t * session)

Destroy discovery session and free resources.

Parameters
sessionSession to destroy

Definition at line 277 of file src/discovery/session.c.

277 {
278 ASCII_OTEL_SCOPE(DISCOVERY_SHUTDOWN);
279 if (!session) {
280 return SET_ERRNO(ERROR_INVALID_PARAM, "session is NULL");
281 }
282
283 asciichat_error_t result = release_participant_transport(session);
284 if (result != ASCIICHAT_OK)
285 return result;
286 // The detector borrows the session and mutex; retain both on a timed join failure.
287 if (session->nat_detection_thread_started) {
288 asciichat_error_t join_result = thread_join_timeout(&session->nat_detection_thread, NULL, 500 * NS_PER_MS_INT);
289 if (join_result != ASCIICHAT_OK)
290 return SET_ERRNO(ERROR_THREAD, "NAT detection worker still owns discovery session");
291 session->nat_detection_thread_started = false;
292 }
293 release_host_control_transport(session);
294
295 // Close ACDS connection
296 if (session->acds_transport) {
298 session->acds_transport = NULL;
299 }
300
301 // Destroy host context if active
302 if (session->host_ctx) {
304 if (result != ASCIICHAT_OK)
305 return result;
306 session->host_ctx = NULL;
307 }
308
309 // Destroy participant context if active
310 if (session->participant_ctx) {
312 session->participant_ctx = NULL;
313 }
314
315 // Clean up WebRTC peer manager (NEW)
316 if (session->peer_manager) {
318 session->peer_manager = NULL;
319 }
320
321 // Clean up STUN/TURN server arrays (NEW)
322 if (session->stun_servers) {
323 SAFE_FREE(session->stun_servers);
324 session->stun_servers = NULL;
325 session->stun_count = 0;
326 }
327
328 if (session->turn_servers) {
329 SAFE_FREE(session->turn_servers);
330 session->turn_servers = NULL;
331 session->turn_count = 0;
332 }
333
334 // Destroy NAT quality mutex
335 mutex_destroy(&session->nat_quality_mutex);
336
338 sodium_memzero(session->password, sizeof(session->password));
339 sodium_memzero(session->identity_seckey, sizeof(session->identity_seckey));
340 sodium_memzero(session->identity_seckeys, sizeof(session->identity_seckeys));
341 SAFE_FREE(session);
342 return ASCIICHAT_OK;
343}
#define NAMED_UNREGISTER(ptr)
Unregister a pointer.
@ ERROR_THREAD
#define NS_PER_MS_INT
Definition time.h:172
void network_connection_destroy(network_connection_t **owner)
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:77
platform_thread_t nat_detection_thread
network_connection_t * acds_connection

References discovery_session_t::acds_connection, discovery_session_t::acds_transport, ASCII_OTEL_SCOPE, ASCIICHAT_OK, ERROR_INVALID_PARAM, ERROR_THREAD, discovery_session_t::host_ctx, discovery_session_t::identity_seckey, discovery_session_t::identity_seckeys, host_liveness_t::last_pong_received_ms, discovery_session_t::liveness, NAMED_UNREGISTER, discovery_session_t::nat_detection_thread, discovery_session_t::nat_detection_thread_started, discovery_session_t::nat_quality_mutex, network_connection_destroy(), NS_PER_MS_INT, discovery_session_t::participant_ctx, discovery_session_t::password, discovery_session_t::peer_manager, SAFE_FREE, session_host_destroy(), session_participant_destroy(), SET_ERRNO, discovery_session_t::stun_count, discovery_session_t::stun_servers, discovery_session_t::turn_count, discovery_session_t::turn_servers, and webrtc_peer_manager_destroy().

Referenced by discovery_main().

◆ discovery_session_get_host()

session_host_t * discovery_session_get_host ( discovery_session_t * session)

Get host context (if we are host).

Parameters
sessionSession context
Returns
Host context or NULL

Definition at line 2112 of file src/discovery/session.c.

2112 {
2113 if (!session) {
2114 SET_ERRNO(ERROR_INVALID_PARAM, "null session");
2115 return NULL;
2116 }
2117 return session->host_ctx;
2118}

References ERROR_INVALID_PARAM, discovery_session_t::host_ctx, and SET_ERRNO.

◆ discovery_session_get_participant()

session_participant_t * discovery_session_get_participant ( discovery_session_t * session)

Get participant context (if we are participant).

Parameters
sessionSession context
Returns
Participant context or NULL

Definition at line 2120 of file src/discovery/session.c.

2120 {
2121 if (!session) {
2122 SET_ERRNO(ERROR_INVALID_PARAM, "null session");
2123 return NULL;
2124 }
2125 return session->participant_ctx;
2126}

References ERROR_INVALID_PARAM, discovery_session_t::participant_ctx, and SET_ERRNO.

◆ discovery_session_get_state()

discovery_state_t discovery_session_get_state ( const discovery_session_t * session)

Get current session state.

Parameters
sessionSession context
Returns
Current state

Definition at line 2080 of file src/discovery/session.c.

2080 {
2081 if (!session) {
2082 SET_ERRNO(ERROR_INVALID_PARAM, "null session");
2084 }
2085 return session->state;
2086}
@ DISCOVERY_STATE_FAILED
Session failed.

References DISCOVERY_STATE_FAILED, ERROR_INVALID_PARAM, SET_ERRNO, and discovery_session_t::state.

◆ discovery_session_get_string()

const char * discovery_session_get_string ( const discovery_session_t * session)

Get session string.

Parameters
sessionSession context
Returns
Session string or NULL if not yet assigned

Definition at line 2096 of file src/discovery/session.c.

2096 {
2097 if (!session || session->session_string[0] == '\0') {
2098 SET_ERRNO(ERROR_INVALID_PARAM, "null session or session string is empty");
2099 return NULL;
2100 }
2101 return session->session_string;
2102}

References ERROR_INVALID_PARAM, discovery_session_t::session_string, and SET_ERRNO.

◆ discovery_session_handle_host_disconnect()

asciichat_error_t discovery_session_handle_host_disconnect ( discovery_session_t * session,
uint32_t disconnect_reason )

Report host loss and await the authoritative ACDS handoff decision. Records the local recovery deadline origin. Advisory successor plans never authorize immediate promotion or reconnection from this function.

Report host disconnect and await the ACDS recovery decision.

Returns
ASCIICHAT_OK if the report was sent; a send error also fails the session.

Definition at line 2219 of file src/discovery/session.c.

2219 {
2220 if (!session) {
2221 SET_ERRNO(ERROR_INVALID_PARAM, "session is NULL");
2222 return ERROR_INVALID_PARAM;
2223 }
2224
2225 log_warn("Host disconnect detected: reason=%u", disconnect_reason);
2226
2227 session->host_loss_detected_ms = session_get_current_time_ms();
2228
2229 // Notify ACDS of host loss (lightweight notification, no NAT data exchanged!)
2230 acip_host_lost_t host_lost = {0};
2231 memcpy(host_lost.session_id, session->session_id, SESSION_ID_SIZE);
2232 memcpy(host_lost.participant_id, session->participant_id, SESSION_ID_SIZE);
2233 memcpy(host_lost.last_host_id, session->host_id, SESSION_ID_SIZE);
2234 host_lost.disconnect_reason = disconnect_reason;
2235 host_lost.disconnect_time_ms = session->host_loss_detected_ms;
2236
2237 asciichat_error_t result = packet_send_via_transport(session->acds_transport, PACKET_TYPE_ACIP_HOST_LOST, &host_lost, sizeof(host_lost), 0);
2238 if (result != ASCIICHAT_OK) {
2239 set_error(session, result, "Could not report host loss to ACDS");
2240 return result;
2241 }
2242
2243 // A PLAN is advisory. Only ACDS COMMIT authorizes a role change after fresh
2244 // measurement and preparation. Continue dispatching while awaiting that decision.
2245 set_state(session, DISCOVERY_STATE_MIGRATING);
2246
2247 return ASCIICHAT_OK;
2248}
acip_host_lost_t
Definition acds.h:961
@ PACKET_TYPE_ACIP_HOST_LOST
Host lost notification - lightweight (Participant -> ACDS).
Definition packet.h:503
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
uint8_t session_id[SESSION_ID_SIZE]

References discovery_session_t::acds_transport, acip_host_lost_t, ASCIICHAT_OK, DISCOVERY_STATE_MIGRATING, ERROR_INVALID_PARAM, discovery_session_t::host_id, discovery_session_t::host_loss_detected_ms, log_warn, packet_send_via_transport(), PACKET_TYPE_ACIP_HOST_LOST, discovery_session_t::participant_id, discovery_session_t::session_id, SESSION_ID_SIZE, and SET_ERRNO.

◆ discovery_session_host_pong()

void discovery_session_host_pong ( discovery_session_t * session)

Record a host reply using the same clock as liveness deadlines.

Definition at line 2128 of file src/discovery/session.c.

2128 {
2129 if (session)
2130 atomic_store_u64(&session->liveness.last_pong_received_ms, session_get_current_time_ms());
2131}

References atomic_store_u64(), host_liveness_t::last_pong_received_ms, and discovery_session_t::liveness.

◆ discovery_session_is_active()

bool discovery_session_is_active ( const discovery_session_t * session)

Check if session is active (call in progress).

Parameters
sessionSession context
Returns
True if active

Definition at line 2088 of file src/discovery/session.c.

2088 {
2089 if (!session) {
2090 SET_ERRNO(ERROR_INVALID_PARAM, "null session");
2091 return false;
2092 }
2093 return session->state == DISCOVERY_STATE_ACTIVE;
2094}
@ DISCOVERY_STATE_ACTIVE
Session active (call in progress).

References DISCOVERY_STATE_ACTIVE, ERROR_INVALID_PARAM, SET_ERRNO, and discovery_session_t::state.

◆ discovery_session_is_host()

bool discovery_session_is_host ( const discovery_session_t * session)

Check if we are the host.

Parameters
sessionSession context
Returns
True if we are hosting

Definition at line 2104 of file src/discovery/session.c.

2104 {
2105 if (!session) {
2106 SET_ERRNO(ERROR_INVALID_PARAM, "null session");
2107 return false;
2108 }
2109 return session->is_host;
2110}

References ERROR_INVALID_PARAM, discovery_session_t::is_host, and SET_ERRNO.

◆ discovery_session_process()

asciichat_error_t discovery_session_process ( discovery_session_t * session,
int64_t timeout_ns )

Process session events (call in main loop).

Handles incoming ACDS messages, negotiation, and state transitions.

Parameters
sessionSession context
timeout_nsMax time to wait for events in nanoseconds (0 = non-blocking)
Returns
ASCIICHAT_OK on success, error on failure

Definition at line 1996 of file src/discovery/session.c.

1996 {
1997 if (!session) {
1998 return SET_ERRNO(ERROR_INVALID_PARAM, "session is NULL");
1999 }
2000
2001 // Transport callbacks publish readiness; ownership changes and worker joins
2002 // execute here on the session's controlling thread.
2004 if (result != ASCIICHAT_OK)
2005 return result;
2006
2007 result = discovery_session_send_keepalive(session);
2008 if (result != ASCIICHAT_OK) {
2009 return result;
2010 }
2011
2013 result = discovery_handoff_sample(session);
2014 if (result != ASCIICHAT_OK)
2015 return result;
2016 }
2017
2018 // State handlers preserve deferred failures: recording a failed state does not
2019 // necessarily return an error until the next iteration processes that state.
2020 switch (session->state) {
2022 return discovery_session_wait_for_peer(session, timeout_ns);
2024 return discovery_session_negotiate(session);
2026 return discovery_session_start_host(session);
2028 return session->session_type == SESSION_TYPE_WEBRTC ? discovery_session_connect_webrtc(session) : discovery_session_connect_tcp(session);
2030 return discovery_session_process_active(session, timeout_ns);
2032 return discovery_session_process_migration(session, timeout_ns);
2034 // Session has failed - return error on further processing
2035 return session->error != ASCIICHAT_OK ? session->error : ERROR_NETWORK;
2036
2038 // Session has ended gracefully - return OK
2039 return ASCIICHAT_OK;
2040
2045 // These states are handled during discovery_session_start(), not in the process loop
2046 // If we reach here, something went wrong - sleep and wait
2047 log_debug("State %d should be handled by discovery_session_start", session->state);
2048 platform_sleep_us(timeout_ns / 1000);
2049 break;
2050
2051 default:
2052 return SET_ERRNO(ERROR_INVALID_STATE, "Invalid session state: %d", session->state);
2053 }
2054
2055 return ASCIICHAT_OK;
2056}
@ ERROR_INVALID_STATE
asciichat_error_t webrtc_peer_manager_dispatch_ready(webrtc_peer_manager_t *manager)
@ DISCOVERY_STATE_ENDED
Session ended.
@ DISCOVERY_STATE_JOINING_SESSION
Joining existing session.
@ DISCOVERY_STATE_NEGOTIATING
NAT negotiation in progress.
@ DISCOVERY_STATE_CREATING_SESSION
Creating new session.
@ DISCOVERY_STATE_CONNECTING_ACDS
Connecting to ACDS.
@ DISCOVERY_STATE_WAITING_PEER
Waiting for peer to join (initiator).

References ASCIICHAT_OK, DISCOVERY_STATE_ACTIVE, DISCOVERY_STATE_CONNECTING_ACDS, DISCOVERY_STATE_CONNECTING_HOST, DISCOVERY_STATE_CREATING_SESSION, DISCOVERY_STATE_ENDED, DISCOVERY_STATE_FAILED, DISCOVERY_STATE_INIT, DISCOVERY_STATE_JOINING_SESSION, DISCOVERY_STATE_MIGRATING, DISCOVERY_STATE_NEGOTIATING, DISCOVERY_STATE_STARTING_HOST, DISCOVERY_STATE_WAITING_PEER, discovery_session_t::error, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, ERROR_NETWORK, log_debug, discovery_session_t::peer_manager, discovery_session_t::session_type, SESSION_TYPE_WEBRTC, SET_ERRNO, discovery_session_t::state, and webrtc_peer_manager_dispatch_ready().

◆ discovery_session_start()

asciichat_error_t discovery_session_start ( discovery_session_t * session)

Start the discovery session.

Connects to ACDS and either creates or joins a session.

Parameters
sessionSession context
Returns
ASCIICHAT_OK on success

Definition at line 1433 of file src/discovery/session.c.

1433 {
1434 log_info("session=%p", session);
1435
1436 if (!session) {
1437 return SET_ERRNO(ERROR_INVALID_PARAM, "session is NULL");
1438 }
1439
1440 log_info("is_initiator=%d, session_string='%s'", session->is_initiator, session->session_string);
1441
1442 // Check if we should exit before starting blocking operations
1443 if (session->should_exit_callback && session->should_exit_callback(session->exit_callback_data)) {
1444 log_info("Exiting early due to should_exit_callback (before ACDS connect)");
1445 return ASCIICHAT_OK; // Return success to allow clean shutdown
1446 }
1447
1448 log_info("Calling connect_to_acds...");
1449 // Connect to ACDS
1450 asciichat_error_t result = connect_to_acds(session);
1451 if (result != ASCIICHAT_OK) {
1452 log_error("connect_to_acds FAILED %s", asciichat_error_format(NULL, result));
1453 set_state(session, DISCOVERY_STATE_FAILED);
1454 return result;
1455 }
1456 log_info("connect_to_acds succeeded");
1457
1458 // Check again after connection
1459 if (session->should_exit_callback && session->should_exit_callback(session->exit_callback_data)) {
1460 log_info("Exiting early due to should_exit_callback (after ACDS connect)");
1461 return ASCIICHAT_OK; // Return success to allow clean shutdown
1462 }
1463
1465
1466 // Perform crypto handshake with ACDS (same as client-server handshake)
1467 log_info("Performing crypto handshake with ACDS...");
1469
1470 result = crypto_handshake_init("discovery_client", handshake_ctx, false);
1471 if (result != ASCIICHAT_OK) {
1472 log_error("Handshake init failed - %s", asciichat_error_format(NULL, result));
1473 set_error(session, result, "Crypto handshake init failed");
1474 return result;
1475 }
1476
1477 bool has_identity = memcmp(session->identity_pubkey, (uint8_t[32]){0}, ED25519_PUBLIC_KEY_SIZE) != 0;
1478 // Store identity key in handshake context
1479 handshake_ctx->client_public_key.type = has_identity ? KEY_TYPE_ED25519 : KEY_TYPE_UNKNOWN;
1480 handshake_ctx->client_private_key.type = handshake_ctx->client_public_key.type;
1481 memcpy(handshake_ctx->client_public_key.key, session->identity_pubkey, ED25519_PUBLIC_KEY_SIZE);
1482 memcpy(handshake_ctx->client_private_key.key.ed25519, session->identity_seckey, ED25519_PRIVATE_KEY_SIZE);
1483 memcpy(handshake_ctx->client_private_key.public_key, session->identity_pubkey, sizeof(session->identity_pubkey));
1484
1485 crypto_policy_t signaling_policy = {
1486 .transport_secure = strncmp(session->acds_url, "wss://", 6) == 0,
1487 .has_identity = has_identity,
1488 };
1489 uint8_t mode;
1490 result = crypto_policy_resolve(&signaling_policy, &mode);
1491 if (result == ASCIICHAT_OK)
1492 result = network_client_negotiate(session->acds_transport, handshake_ctx, mode, NETWORK_PROTOCOL_DISCOVERY);
1493 if (result != ASCIICHAT_OK) {
1494 set_error(session, result, "Discovery handshake failed");
1495 return result;
1496 }
1497
1498 // Check again after handshake
1499 if (session->should_exit_callback && session->should_exit_callback(session->exit_callback_data)) {
1500 log_info("Exiting early due to should_exit_callback (after handshake)");
1501 return ASCIICHAT_OK; // Return success to allow clean shutdown
1502 }
1503
1504 // Create or join session
1505 if (session->is_initiator) {
1506 log_info("Calling create_session - participant_ctx before=%p", session->participant_ctx);
1507 result = create_session(session);
1508 log_info("create_session returned - participant_ctx after=%p, result=%d", session->participant_ctx, result);
1509
1510 if (result == ASCIICHAT_OK) {
1511 // Initiator also needs to register itself as a participant so it can receive PARTICIPANT_JOINED notifications
1512 // when other peers join. This makes the initiator discoverable to other participants.
1513 log_info("Initiator: now registering self as participant in created session");
1514 result = join_session(session);
1515 log_info("Initiator: join_session result=%d", result);
1516 }
1517 return result;
1518 } else {
1519 log_info("Calling join_session - participant_ctx before=%p", session->participant_ctx);
1520 asciichat_error_t result = join_session(session);
1521 log_info("join_session returned - participant_ctx after=%p, state=%d, result=%d", session->participant_ctx, session->state, result);
1522 return result;
1523 }
1524}
const char * asciichat_error_format(const char *message, asciichat_error_t code)
asciichat_error_t crypto_handshake_init(const char *name, crypto_handshake_context_t *ctx, bool is_server)
Initialize named crypto handshake context.
uint8_t key[CRYPTO_PUBLIC_KEY_SIZE]
Definition key_types.h:78
uint8_t ed25519[ED25519_PRIVATE_KEY_SIZE]
Definition key_types.h:104
key_type_t type
Definition key_types.h:102
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]
Definition key_types.h:109
key_type_t type
Definition key_types.h:77
union private_key_t::@040247167361101132173265304166142033301213115260 key
@ KEY_TYPE_UNKNOWN
Definition key_types.h:57
@ KEY_TYPE_ED25519
Definition key_types.h:58
#define NS_PER_SEC_INT
Definition time.h:173
crypto_handshake_context_t * network_connection_handshake(network_connection_t *c)
asciichat_error_t network_client_negotiate(acip_transport_t *transport, crypto_handshake_context_t *ctx, uint8_t mode, network_protocol_t protocol)
@ NETWORK_PROTOCOL_DISCOVERY
Definition negotiation.h:9
asciichat_error_t crypto_policy_resolve(const crypto_policy_t *policy, uint8_t *mode)
Definition policy.c:4
uint64_t receive_timeout_ns
Optional TCP receive timeout override; zero uses the normal polling timeout.
Cryptographic handshake context structure.
The useful part of crypto policy is in transport_secure, encrypt, no_encrypt, no_auth: together they ...
Definition policy.h:17

References discovery_session_t::acds_connection, discovery_session_t::acds_transport, discovery_session_t::acds_url, asciichat_error_format(), ASCIICHAT_OK, crypto_handshake_context_t::client_private_key, crypto_handshake_context_t::client_public_key, crypto_handshake_init(), crypto_policy_resolve(), DISCOVERY_STATE_FAILED, private_key_t::ed25519, ED25519_PRIVATE_KEY_SIZE, ED25519_PUBLIC_KEY_SIZE, ERROR_INVALID_PARAM, discovery_session_t::exit_callback_data, discovery_session_t::identity_pubkey, discovery_session_t::identity_seckey, discovery_session_t::is_initiator, private_key_t::key, public_key_t::key, KEY_TYPE_ED25519, KEY_TYPE_UNKNOWN, log_error, log_info, network_client_negotiate(), network_connection_handshake(), NETWORK_PROTOCOL_DISCOVERY, NS_PER_SEC_INT, discovery_session_t::participant_ctx, private_key_t::public_key, acip_transport::receive_timeout_ns, discovery_session_t::session_string, SET_ERRNO, discovery_session_t::should_exit_callback, discovery_session_t::state, private_key_t::type, and public_key_t::type.

◆ discovery_session_stop()

void discovery_session_stop ( discovery_session_t * session)

Stop the discovery session.

Parameters
sessionSession context

Definition at line 2058 of file src/discovery/session.c.

2058 {
2059 ASCII_OTEL_SCOPE(DISCOVERY_SHUTDOWN);
2060 if (!session) {
2061 SET_ERRNO(ERROR_INVALID_PARAM, "session is NULL");
2062 return;
2063 }
2064
2065 log_info("Stopping discovery session...");
2066
2067 // Send SESSION_LEAVE if connected
2068 if (session->acds_transport && session->session_id[0] != 0) {
2069 acip_session_leave_t leave_msg;
2070 memset(&leave_msg, 0, sizeof(leave_msg));
2071 memcpy(leave_msg.session_id, session->session_id, SESSION_ID_SIZE);
2072 memcpy(leave_msg.participant_id, session->participant_id, SESSION_ID_SIZE);
2073
2074 packet_send_via_transport(session->acds_transport, PACKET_TYPE_ACIP_SESSION_LEAVE, &leave_msg, sizeof(leave_msg), 0);
2075 }
2076
2077 set_state(session, DISCOVERY_STATE_ENDED);
2078}
acip_session_leave_t
Definition acds.h:529
@ PACKET_TYPE_ACIP_SESSION_LEAVE
Leave session (Client -> Discovery Server).
Definition packet.h:457

References discovery_session_t::acds_transport, acip_session_leave_t, ASCII_OTEL_SCOPE, DISCOVERY_STATE_ENDED, ERROR_INVALID_PARAM, log_info, packet_send_via_transport(), PACKET_TYPE_ACIP_SESSION_LEAVE, discovery_session_t::participant_id, discovery_session_t::session_id, SESSION_ID_SIZE, and SET_ERRNO.

Referenced by discovery_main().