ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
discovery.c
Go to the documentation of this file.
1
15
19
21#include <ascii-chat/tui/mdns.h> // For ui_mdns_server_t struct only
23#include <libplatform/thread.h>
24#include <libplatform/mutex.h>
25#include <libplatform/cond.h>
26#include <ascii-chat/log/log.h>
29#include <string.h>
30#include <ctype.h>
31#include <stdlib.h>
32#include <time.h>
33
34// ============================================================================
35// Thread Coordination
36// ============================================================================
37
53
54// ============================================================================
55// Utility Functions
56// ============================================================================
57
59 if (!pubkey || !hex_out)
60 return;
61 for (int i = 0; i < 32; i++) {
62 safe_snprintf(hex_out + (i * 2), 3, "%02x", pubkey[i]);
63 }
64 hex_out[64] = '\0';
65}
66
68 if (!hex_str || !pubkey_out) {
69 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid hex_str or pubkey_out pointer");
71 }
72
73 if (strlen(hex_str) != 64) {
74 SET_ERRNO(ERROR_INVALID_PARAM, "Hex string must be exactly 64 characters");
76 }
77
78 for (int i = 0; i < 32; i++) {
79 char hex_byte[3];
80 hex_byte[0] = hex_str[i * 2];
81 hex_byte[1] = hex_str[i * 2 + 1];
82 hex_byte[2] = '\0';
83
84 if (!isxdigit(hex_byte[0]) || !isxdigit(hex_byte[1])) {
85 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid hex character in string");
87 }
88
89 pubkey_out[i] = (uint8_t)strtol(hex_byte, NULL, 16);
90 }
91
92 return ASCIICHAT_OK;
93}
94
95// NOTE: is_session_string() has been moved to lib/discovery/strings.c with enhanced
96// validation against cached wordlists. See that module for the implementation.
97
98// ============================================================================
99// TXT Record Parsing
100// ============================================================================
101// mDNS Query Implementation (Core Module)
102// ============================================================================
103
115
120static void discovery_mdns_callback(const asciichat_mdns_discovery_t *discovery, void *user_data) {
121 mdns_query_state_t *state = (mdns_query_state_t *)user_data;
122 if (!state || !discovery) {
123 return;
124 }
125
126 // Check if timeout exceeded
127 int64_t elapsed = (int64_t)time_ns_to_ms(time_get_ns()) - state->start_time_ms;
128 if (elapsed > state->timeout_ms) {
129 state->query_complete = true;
130 return;
131 }
132
133 // Only accept services of the right type
134 if (strstr(discovery->type, "_ascii-chat._tcp") == NULL) {
135 return;
136 }
137
138 // Check if we already have this server (avoid duplicates)
139 for (int i = 0; i < state->count; i++) {
140 if (strcmp(state->servers[i].name, discovery->name) == 0 && state->servers[i].port == discovery->port) {
141 if (strncmp(discovery->txt, "session_string=", sizeof("session_string=") - 1) == 0)
142 SAFE_STRNCPY(state->servers[i].session_string, discovery->txt + sizeof("session_string=") - 1, sizeof(state->servers[i].session_string));
143 // Update TTL if newer
144 if (discovery->ttl > state->servers[i].ttl) {
145 state->servers[i].ttl = discovery->ttl;
146 }
147 return; // Already have this server
148 }
149 }
150
151 // Allow metadata updates for existing entries even when the list is full.
152 if (state->count >= state->capacity)
153 return;
154
155 // Add new server to our array
156 ui_mdns_server_t *server = &state->servers[state->count];
157 memset(server, 0, sizeof(ui_mdns_server_t));
158
159 // Copy service information
160 SAFE_STRNCPY(server->name, discovery->name, sizeof(server->name));
161 if (strncmp(discovery->txt, "session_string=", sizeof("session_string=") - 1) == 0)
162 SAFE_STRNCPY(server->session_string, discovery->txt + sizeof("session_string=") - 1, sizeof(server->session_string));
163 SAFE_STRNCPY(server->ipv4, discovery->ipv4, sizeof(server->ipv4));
164 SAFE_STRNCPY(server->ipv6, discovery->ipv6, sizeof(server->ipv6));
165 server->port = discovery->port;
166 server->ttl = discovery->ttl;
167
168 // Prefer IPv4 address as the primary address, fall back to hostname
169 if (discovery->ipv4[0] != '\0') {
170 SAFE_STRNCPY(server->address, discovery->ipv4, sizeof(server->address));
171 } else if (discovery->host[0] != '\0') {
172 SAFE_STRNCPY(server->address, discovery->host, sizeof(server->address));
173 } else if (discovery->ipv6[0] != '\0') {
174 SAFE_STRNCPY(server->address, discovery->ipv6, sizeof(server->address));
175 }
176
177 state->count++;
178 log_debug("mDNS: Found server '%s' at %s:%u", discovery->name, server->address, discovery->port);
179}
180
190ui_mdns_server_t *discovery_mdns_query(int timeout_ms, int max_servers, bool quiet, int *out_count) {
191 if (!out_count) {
192 SET_ERRNO(ERROR_INVALID_PARAM, "out_count pointer is NULL");
193 return NULL;
194 }
195
196 *out_count = 0;
197
198 // Apply defaults
199 if (timeout_ms <= 0) {
200 timeout_ms = 2 * MS_PER_SEC_INT;
201 }
202 if (max_servers <= 0) {
203 max_servers = 20;
204 }
205
206 // Allocate state for collecting servers
207 mdns_query_state_t state;
208 memset(&state, 0, sizeof(state));
209 state.capacity = max_servers;
210 state.timeout_ms = timeout_ms;
211 state.start_time_ms = (int64_t)time_ns_to_ms(time_get_ns());
212
213 // Allocate server array
214 state.servers = SAFE_MALLOC((size_t)state.capacity * sizeof(ui_mdns_server_t), ui_mdns_server_t *);
215 if (!state.servers) {
216 SET_ERRNO(ERROR_MEMORY, "Failed to allocate mDNS discovery server array");
217 return NULL;
218 }
219 memset(state.servers, 0, state.capacity * sizeof(ui_mdns_server_t));
220
221 if (!quiet) {
222 log_info("mDNS: Searching for ascii-chat servers on local network (timeout: %dms)", state.timeout_ms);
223 printf("🔍 Searching for ascii-chat servers on LAN...\n");
224 }
225
226 // Discovery is retried in the live picker; failures remain in logs.
228 bool previous_notifications = errno_notifications_suppress(true);
229 // Initialize mDNS
231 if (!mdns) {
232 log_warn("mDNS: Failed to initialize mDNS - discovery unavailable");
233 errno_notifications_suppress(previous_notifications);
235 return state.servers;
236 }
237
238 // Start mDNS query for _ascii-chat._tcp services
239 asciichat_error_t query_result = asciichat_mdns_query(mdns, "_ascii-chat._tcp.local", discovery_mdns_callback, &state);
240
241 if (query_result != ASCIICHAT_OK) {
242 log_info("mDNS: Query failed - no servers found via service discovery");
244 errno_notifications_suppress(previous_notifications);
246 return state.servers;
247 }
248
249 // Poll for responses until timeout
250 int64_t deadline = state.start_time_ms + state.timeout_ms;
251 while (!state.query_complete && (int64_t)time_ns_to_ms(time_get_ns()) < deadline) {
252 int poll_timeout = (int)(deadline - (int64_t)time_ns_to_ms(time_get_ns()));
253 if (poll_timeout < 0) {
254 poll_timeout = 0;
255 }
256 if (poll_timeout > 100) {
257 poll_timeout = 100; // Check every 100ms
258 }
259 if (asciichat_mdns_update(mdns, poll_timeout) != ASCIICHAT_OK)
260 break;
261 }
262
263 // Cleanup mDNS
265 errno_notifications_suppress(previous_notifications);
267
268 if (!quiet) {
269 if (state.count > 0) {
270 printf("✅ Found %d ascii-chat server%s on LAN\n", state.count, state.count == 1 ? "" : "s");
271 log_info("mDNS: Found %d server(s)", state.count);
272 } else {
273 printf("❌ No ascii-chat servers found on LAN\n");
274 log_info("mDNS: No servers found");
275 }
276 }
277
278 *out_count = state.count;
279 return state.servers;
280}
281
286 SAFE_FREE(servers);
287}
288
289// ============================================================================
290// mDNS Discovery Thread
291// ============================================================================
292
302
304static void *mdns_thread_fn(void *arg) {
306 if (!ctx)
307 return NULL;
308
309 // Call the public mDNS query function in this module
310 int discovered_count = 0;
311 ui_mdns_server_t *discovered_servers = discovery_mdns_query((int)(ctx->timeout_ms > 0 ? ctx->timeout_ms : 2000), 20, true, &discovered_count);
312
313 if (!discovered_servers || discovered_count == 0) {
314 log_debug("mDNS: No servers found (this is normal if no servers are on LAN)");
315 if (discovered_servers) {
316 discovery_mdns_destroy(discovered_servers);
317 }
318 mutex_lock(&ctx->state->lock);
319 ctx->state->mdns_done = true;
320 cond_signal(&ctx->state->signal);
321 mutex_unlock(&ctx->state->lock);
322 SAFE_FREE(ctx);
323 return NULL;
324 }
325
326 // Search for server matching our session string
327 for (int i = 0; i < discovered_count; i++) {
328 ui_mdns_server_t *server = &discovered_servers[i];
329
330 if (strcmp(server->name, ctx->session_string) == 0 || strcmp(server->session_string, ctx->session_string) == 0) {
331 // Found a match!
332 mutex_lock(&ctx->state->lock);
333 {
334 if (!ctx->state->found) {
335 ctx->state->found = true;
336 ctx->state->result->success = true;
337 ctx->state->result->source = DISCOVERY_SOURCE_MDNS;
338 ctx->state->result->server_port = server->port;
339
340 const char *best_addr = (server->ipv4[0] != '\0') ? server->ipv4 : server->ipv6;
341 SAFE_STRNCPY(ctx->state->result->server_address, best_addr, sizeof(ctx->state->result->server_address) - 1);
342 ctx->state->result->server_address[sizeof(ctx->state->result->server_address) - 1] = '\0';
343
344 SAFE_STRNCPY(ctx->state->result->mdns_service_name, server->name, sizeof(ctx->state->result->mdns_service_name) - 1);
345 ctx->state->result->mdns_service_name[sizeof(ctx->state->result->mdns_service_name) - 1] = '\0';
346
347 log_info("mDNS: Found session '%s' at %s:%d", ctx->session_string, ctx->state->result->server_address, ctx->state->result->server_port);
348 cond_signal(&ctx->state->signal);
349 }
350 }
351 mutex_unlock(&ctx->state->lock);
352 break;
353 }
354 }
355
356 discovery_mdns_destroy(discovered_servers);
357
358 mutex_lock(&ctx->state->lock);
359 ctx->state->mdns_done = true;
360 cond_signal(&ctx->state->signal);
361 mutex_unlock(&ctx->state->lock);
362 SAFE_FREE(ctx);
363 return NULL;
364}
365
366// ============================================================================
367// ACDS Discovery Thread
368// ============================================================================
369
378
380static void *acds_thread_fn(void *arg) {
382 if (!ctx)
383 return NULL;
384
385 acds_client_t client;
386 memset(&client, 0, sizeof(client));
387
388 acds_client_config_t client_config;
389 memset(&client_config, 0, sizeof(client_config));
390
391 SAFE_STRNCPY(client_config.server_address, ctx->config->acds_server, sizeof(client_config.server_address) - 1);
392 client_config.server_port = ctx->config->acds_port;
393 client_config.timeout_ms = ctx->config->acds_timeout_ms;
394
395 // Connect to ACDS server
396 asciichat_error_t conn_err = acds_client_connect(&client, &client_config);
397 if (conn_err != ASCIICHAT_OK) {
398 log_debug("ACDS: Failed to connect to %s:%d", ctx->config->acds_server, ctx->config->acds_port);
399 mutex_lock(&ctx->state->lock);
400 ctx->state->acds_done = true;
401 cond_signal(&ctx->state->signal);
402 mutex_unlock(&ctx->state->lock);
403 SAFE_FREE(ctx);
404 return NULL;
405 }
406
407 // Look up session
408 acds_session_lookup_result_t lookup_result;
409 memset(&lookup_result, 0, sizeof(lookup_result));
410
411 asciichat_error_t lookup_err = acds_session_lookup(&client, ctx->session_string, &lookup_result);
412 if (lookup_err != ASCIICHAT_OK) {
413 log_debug("ACDS: Session lookup failed for '%s'", ctx->session_string);
414 acds_client_disconnect(&client);
415 mutex_lock(&ctx->state->lock);
416 ctx->state->acds_done = true;
417 cond_signal(&ctx->state->signal);
418 mutex_unlock(&ctx->state->lock);
419 SAFE_FREE(ctx);
420 return NULL;
421 }
422
423 if (!lookup_result.found) {
424 log_debug("ACDS: Session '%s' not found", ctx->session_string);
425 acds_client_disconnect(&client);
426 mutex_lock(&ctx->state->lock);
427 ctx->state->acds_done = true;
428 cond_signal(&ctx->state->signal);
429 mutex_unlock(&ctx->state->lock);
430 SAFE_FREE(ctx);
431 return NULL;
432 }
433
434 // Verify pubkey if provided
435 if (ctx->config->expected_pubkey) {
436 if (memcmp(lookup_result.host_pubkey, ctx->config->expected_pubkey, ED25519_PUBLIC_KEY_SIZE) != 0) {
437 NOTICE(DANGER, "DISCOVERED HOST KEY MISMATCH",
438 "The discovered session host key does not match the expected key.\n"
439 "Possible man-in-the-middle attack. This discovery result was rejected.");
440 acds_client_disconnect(&client);
441 mutex_lock(&ctx->state->lock);
442 ctx->state->acds_done = true;
443 cond_signal(&ctx->state->signal);
444 mutex_unlock(&ctx->state->lock);
445 SAFE_FREE(ctx);
446 return NULL;
447 }
448 }
449
450 // Join session to get server connection details
451 acds_session_join_params_t join_params;
452 memset(&join_params, 0, sizeof(join_params));
453 join_params.session_string = ctx->session_string;
454
455 if (ctx->config->client_pubkey) {
457 }
458 if (ctx->config->client_seckey) {
460 }
461 if (ctx->config->password) {
462 join_params.has_password = true;
463 SAFE_STRNCPY(join_params.password, ctx->config->password, sizeof(join_params.password) - 1);
464 }
465
466 acds_session_join_result_t join_result;
467 memset(&join_result, 0, sizeof(join_result));
468
469 asciichat_error_t join_err = acds_session_join(&client, &join_params, &join_result);
470 acds_client_disconnect(&client);
471
472 if (join_err != ASCIICHAT_OK || !join_result.success) {
473 log_debug("ACDS: Session join failed: %s", join_result.error_message);
474 mutex_lock(&ctx->state->lock);
475 ctx->state->acds_done = true;
476 cond_signal(&ctx->state->signal);
477 mutex_unlock(&ctx->state->lock);
478 SAFE_FREE(ctx);
479 return NULL;
480 }
481
482 // Found it! Populate result
483 mutex_lock(&ctx->state->lock);
484 {
485 if (!ctx->state->found) {
486 ctx->state->found = true;
487 ctx->state->result->success = true;
488 ctx->state->result->source = DISCOVERY_SOURCE_ACDS;
489
490 memcpy(ctx->state->result->host_pubkey, lookup_result.host_pubkey, ED25519_PUBLIC_KEY_SIZE);
491 memcpy(ctx->state->result->session_id, join_result.session_id, SESSION_ID_SIZE);
492 memcpy(ctx->state->result->participant_id, join_result.participant_id, SESSION_ID_SIZE);
493
494 SAFE_STRNCPY(ctx->state->result->server_address, join_result.server_address, sizeof(ctx->state->result->server_address) - 1);
495 ctx->state->result->server_port = join_result.server_port;
496
497 log_info("ACDS: Found session '%s' at %s:%d", ctx->session_string, ctx->state->result->server_address, ctx->state->result->server_port);
498 cond_signal(&ctx->state->signal);
499 }
500 }
501 mutex_unlock(&ctx->state->lock);
502
503 SAFE_FREE(ctx);
504 return NULL;
505}
506
507// ============================================================================
508// Main Discovery Function
509// ============================================================================
510
512 if (!config)
513 return;
514
515 memset(config, 0, sizeof(*config));
516
517 // Check if debug or release build
518#ifdef NDEBUG
519 // Release: use internet ACDS
520 SAFE_STRNCPY(config->acds_server, "discovery.ascii-chat.com", sizeof(config->acds_server) - 1);
521#else
522 // Debug: use local ACDS
523 SAFE_STRNCPY(config->acds_server, "127.0.0.1", sizeof(config->acds_server) - 1);
524#endif
525
527 config->mdns_timeout_ms = 2 * MS_PER_SEC_INT;
528 config->acds_timeout_ms = 5 * MS_PER_SEC_INT;
529 config->insecure_mode = false;
530 config->expected_pubkey = NULL;
531}
532
533asciichat_error_t discover_session_parallel(const char *session_string, const discovery_config_t *config, discovery_result_t *result) {
534 if (!session_string || !config || !result) {
535 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
536 return ERROR_INVALID_PARAM;
537 }
538
539 // Validate session string format
540 if (!is_session_string(session_string)) {
541 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid session string format");
542 return ERROR_INVALID_PARAM;
543 }
544
545 memset(result, 0, sizeof(*result));
546 log_info("Discovery: Looking up session '%s'", session_string);
547
548 // Initialize thread state
550 memset(&state, 0, sizeof(state));
551 state.result = result;
552 mutex_init(&state.lock, "mdns_resolve");
553 cond_init(&state.signal, "mdns_signal");
554
555 // Determine which discovery methods to use
556 bool use_mdns = true;
557 bool use_acds = config->expected_pubkey != NULL || config->insecure_mode;
558 if (config->insecure_mode && !config->expected_pubkey)
559 NOTICE(DANGER, "DISCOVERY HOST VERIFICATION DISABLED",
560 "Insecure discovery is enabled without an expected host key.\n"
561 "The discovered host identity cannot be verified; this is vulnerable to man-in-the-middle attacks.");
562
563 if (!use_acds) {
564 log_debug("Discovery: mDNS-only mode (no --server-key and no --acds-insecure)");
565 }
566
567 // Spawn mDNS thread
568 platform_thread_t mdns_thread;
569 thread_init(&mdns_thread);
570
571 if (use_mdns) {
572 mdns_thread_context_t *mdns_ctx = SAFE_MALLOC(sizeof(*mdns_ctx), mdns_thread_context_t *);
573 if (!mdns_ctx) {
574 SET_ERRNO(ERROR_MEMORY, "Failed to allocate mDNS context");
575 mutex_destroy(&state.lock);
576 cond_destroy(&state.signal);
577 return ERROR_MEMORY;
578 }
579
580 mdns_ctx->session_string = session_string;
581 mdns_ctx->state = &state;
582 mdns_ctx->expected_pubkey = config->expected_pubkey;
583
584 int thread_err = platform_thread_create(&mdns_thread, "mdns", mdns_thread_fn, mdns_ctx);
585 if (thread_err != 0) {
586 log_warn("Discovery: Failed to spawn mDNS thread");
587 SAFE_FREE(mdns_ctx);
588 use_mdns = false;
589 }
590 }
591
592 // Spawn ACDS thread
593 platform_thread_t acds_thread;
594 thread_init(&acds_thread);
595
596 if (use_acds) {
597 acds_thread_context_t *acds_ctx = SAFE_MALLOC(sizeof(*acds_ctx), acds_thread_context_t *);
598 if (!acds_ctx) {
599 SET_ERRNO(ERROR_MEMORY, "Failed to allocate ACDS context");
600 if (use_mdns && thread_is_initialized(&mdns_thread)) {
601 thread_join(&mdns_thread, NULL);
602 }
603 mutex_destroy(&state.lock);
604 cond_destroy(&state.signal);
605 return ERROR_MEMORY;
606 }
607
608 acds_ctx->session_string = session_string;
609 acds_ctx->state = &state;
610 acds_ctx->config = config;
611
612 int thread_err = platform_thread_create(&acds_thread, "acds_client", acds_thread_fn, acds_ctx);
613 if (thread_err != 0) {
614 log_warn("Discovery: Failed to spawn ACDS thread");
615 SAFE_FREE(acds_ctx);
616 use_acds = false;
617 }
618 }
619
620 // Wait for result (mDNS timeout 2s + ACDS timeout 5s max)
621 uint32_t wait_timeout_ms = config->acds_timeout_ms + 1000;
622 mutex_lock(&state.lock);
623 {
624 uint32_t elapsed_ms = 0;
625 while (!state.found && elapsed_ms < wait_timeout_ms) {
626 // Check if both threads are done
627 if (state.mdns_done && state.acds_done) {
628 break;
629 }
630
631 // Wait with timeout
632 cond_timedwait(&state.signal, &state.lock, 500 * NS_PER_MS_INT);
633 elapsed_ms += 500;
634 }
635 }
636 mutex_unlock(&state.lock);
637
638 // Join threads
639 if (use_mdns && thread_is_initialized(&mdns_thread)) {
640 thread_join(&mdns_thread, NULL);
641 }
642 if (use_acds && thread_is_initialized(&acds_thread)) {
643 thread_join(&acds_thread, NULL);
644 }
645
646 // Cleanup
647 mutex_destroy(&state.lock);
648 cond_destroy(&state.signal);
649
650 // Check result
651 if (!result->success) {
652 SET_ERRNO(ERROR_NOT_FOUND, "Session '%s' not found (mDNS/ACDS timeout)", session_string);
653 return ERROR_NOT_FOUND;
654 }
655
656 log_info("Discovery: Session '%s' discovered via %s", session_string, result->source == DISCOVERY_SOURCE_MDNS ? "mDNS" : "ACDS");
657
658 return ASCIICHAT_OK;
659}
ACIP client-side protocol library and ACDS client connection API.
asciichat_error_t acds_session_lookup(acds_client_t *client, const char *name, acds_session_lookup_result_t *result)
Look up session by string.
asciichat_error_t acds_session_join(acds_client_t *client, const acds_session_join_params_t *params, acds_session_join_result_t *result)
Join an existing session.
asciichat_error_t acds_client_connect(acds_client_t *client, const acds_client_config_t *config)
Connect to ACDS server.
void acds_client_disconnect(acds_client_t *client)
Disconnect from ACDS server.
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)
#define SET_ERRNO(code,...)
@ ASCIICHAT_ERRNO_HANDLED
Application limits and constraints.
bool is_session_string(const char *str)
Check if a string is a valid session string.
asciichat_error_t hex_to_pubkey(const char *hex_str, uint8_t pubkey_out[32])
Convert hex string to Ed25519 public key.
Definition discovery.c:67
void discovery_mdns_destroy(ui_mdns_server_t *servers)
Free memory from mDNS discovery results.
Definition discovery.c:285
void pubkey_to_hex(const uint8_t pubkey[32], char hex_out[(32 *2+1)])
Convert Ed25519 public key to hex string.
Definition discovery.c:58
ui_mdns_server_t * discovery_mdns_query(int timeout_ms, int max_servers, bool quiet, int *out_count)
Public mDNS query function used by both parallel discovery and TUI wrapper.
Definition discovery.c:190
void discovery_config_init_defaults(discovery_config_t *config)
Initialize discovery config with defaults.
Definition discovery.c:511
asciichat_error_t discover_session_parallel(const char *session_string, const discovery_config_t *config, discovery_result_t *result)
Look up session in parallel on mDNS and ACDS.
Definition discovery.c:533
unsigned int uint32_t
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_NOT_FOUND
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define SESSION_ID_SIZE
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
#define log_info(...)
Log an INFO message.
Definition log/log.h:602
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
#define NS_PER_MS_INT
Definition time.h:172
#define MS_PER_SEC_INT
Definition time.h:180
#define OPT_ACDS_PORT_INT_DEFAULT
Default ACDS discovery service port (integer).
Definition options.h:593
📝 Logging API with multiple log levels and terminal output control
asciichat_error_t asciichat_mdns_update(asciichat_mdns_t *mdns, int timeout_ms)
Process pending mDNS events (must be called regularly).
void asciichat_mdns_destroy(asciichat_mdns_t *mdns)
Shutdown mDNS context and cleanup.
asciichat_mdns_t * asciichat_mdns_init(void)
Initialize mDNS context.
asciichat_error_t asciichat_mdns_query(asciichat_mdns_t *mdns, const char *service_type, asciichat_mdns_discovery_callback_fn callback, void *user_data)
Query for services on the local network.
#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
🔤 String Manipulation and Shell Escaping Utilities
acds client config is intentionally a small, concrete record. Fields such as server_port,...
char server_address[256]
ACDS server address (e.g., "discovery.ascii.chat" or "127.0.0.1").
uint32_t timeout_ms
Connection timeout in milliseconds.
uint16_t server_port
ACDS server port (default: 27225).
ACDS client connection handle.
Session join parameters.
uint8_t identity_pubkey[32]
Participant's Ed25519 public key.
bool has_password
Password provided.
char password[128]
Password (if has_password).
const char * session_string
Session to join.
uint8_t identity_seckey[64]
Ed25519 secret key (for signing).
Session join result.
uint8_t participant_id[16]
Participant UUID (if success).
bool success
Join succeeded.
uint16_t server_port
Server port (if success).
char error_message[128+1]
Error message (if !success, null-terminated).
uint8_t session_id[16]
Session UUID (if success).
char server_address[64+1]
Server IP/hostname (if success, null-terminated).
Session lookup result.
bool found
Session exists.
uint8_t host_pubkey[32]
Host's Ed25519 public key.
Context for ACDS discovery thread.
Definition discovery.c:373
discovery_thread_state_t * state
Shared discovery state.
Definition discovery.c:375
const discovery_config_t * config
Discovery configuration.
Definition discovery.c:376
const char * session_string
Session string to look up.
Definition discovery.c:374
When code needs to work with asciichat errno snapshot, it keeps total, copied here instead of scatter...
Service information for advertisement.
Owns one mDNS browse or advertisement session, including its socket, event-loop registration,...
Configuration for discovery session.
const uint8_t * client_seckey
Client's Ed25519 secret key (can be NULL).
Definition discovery.h:136
uint32_t acds_timeout_ms
ACDS lookup timeout (default: 5000ms).
Definition discovery.h:132
uint32_t mdns_timeout_ms
mDNS search timeout (default: 2000ms)
Definition discovery.h:131
uint16_t acds_port
ACDS port (default: 27225).
const char * password
Session password (optional).
char acds_server[256]
ACDS server address (e.g., "localhost" or "discovery.ascii-chat.com").
Definition discovery.h:127
bool insecure_mode
Allow no verification (–acds-insecure flag).
Definition discovery.h:124
const uint8_t * expected_pubkey
Expected server pubkey (NULL = no verification).
Definition discovery.h:123
const uint8_t * client_pubkey
Client's Ed25519 public key (can be NULL).
Definition discovery.h:135
Result from session discovery.
Definition discovery.h:92
char mdns_service_name[256]
mDNS service instance name (e.g., "swift-river-mountain")
Definition discovery.h:103
uint16_t server_port
Server port (typically 27224).
Definition discovery.h:96
uint8_t host_pubkey[32]
Ed25519 public key of discovered server.
Definition discovery.h:94
uint8_t participant_id[16]
Assigned participant ID (from SESSION_JOIN).
Definition discovery.h:108
uint8_t session_id[16]
ACDS session UUID.
Definition discovery.h:107
enum discovery_result_t::@335075210033063074377072145063374373365157111022 source
Which discovery method found the server.
char server_address[256]
Server IP or hostname.
Definition discovery.h:95
bool success
Discovery succeeded.
Definition discovery.h:93
Context for ACDS discovery thread.
Definition discovery.c:45
bool found
Whether a session was found.
Definition discovery.c:51
discovery_result_t * result
Shared discovery result.
Definition discovery.c:48
cond_t signal
Condition variable for signaling.
Definition discovery.c:47
bool mdns_done
Whether mDNS discovery completed.
Definition discovery.c:49
mutex_t lock
Mutex for thread-safe access.
Definition discovery.c:46
bool acds_done
Whether ACDS discovery completed.
Definition discovery.c:50
Internal state for collecting discovered services.
Definition discovery.c:107
ui_mdns_server_t * servers
Array of discovered servers.
Definition discovery.c:108
int count
Number of servers discovered so far.
Definition discovery.c:109
int64_t start_time_ms
When discovery started (for timeout).
Definition discovery.c:111
bool query_complete
Set when discovery completes.
Definition discovery.c:113
int capacity
Allocated capacity.
Definition discovery.c:110
int timeout_ms
Discovery timeout in milliseconds.
Definition discovery.c:112
Context for mDNS discovery thread.
Definition discovery.c:296
discovery_thread_state_t * state
Shared discovery state.
Definition discovery.c:298
uint32_t timeout_ms
Discovery timeout in milliseconds.
Definition discovery.c:300
const uint8_t * expected_pubkey
Expected server public key (optional).
Definition discovery.c:299
const char * session_string
Session string to discover.
Definition discovery.c:297
Discovered server information from mDNS.
Definition tui/mdns.h:66
char name[256]
Service instance name (e.g., "swift-river-canyon").
Definition tui/mdns.h:67
uint32_t ttl
TTL remaining (seconds).
Definition tui/mdns.h:73
char ipv4[16]
IPv4 address (if available).
Definition tui/mdns.h:71
char ipv6[46]
IPv6 address (if available).
Definition tui/mdns.h:72
uint16_t port
Server port number.
Definition tui/mdns.h:70
char address[256]
Server address (IPv4, IPv6, or hostname).
Definition tui/mdns.h:69
char session_string[256]
Session identity advertised in TXT metadata.
Definition tui/mdns.h:68
⏱️ High-precision timing utilities using sokol_time.h and uthash
mDNS service discovery UI with terminal_screen rendering