ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
strings.h File Reference

Go to the source code of this file.

Macros

#define SESSION_STRING_MAX_LEN   38
 Maximum length of a session string (without null terminator).
#define SESSION_STRING_BUFFER_SIZE   (SESSION_STRING_MAX_LEN + 1)
 Buffer size needed for session string storage (with null terminator) Used for static character array declarations.
#define ACDS_MAX_UNIQUE_SESSIONS   (2500LL * 5000LL * 5000LL)
 Maximum number of unique session strings that can be generated.

Functions

asciichat_error_t acds_string_generate (char *output, size_t output_size)
 Generate random session string.
asciichat_error_t acds_string_init (void)
 Initialize session string system with cached word validation.
bool is_session_string (const char *str)
 Check if a string is a valid session string.
void acds_strings_destroy (void)
 Cleanup function for session string cache Called by asciichat_shared_destroy() during library cleanup. Safe to call multiple times (idempotent).

Macro Definition Documentation

◆ ACDS_MAX_UNIQUE_SESSIONS

#define ACDS_MAX_UNIQUE_SESSIONS   (2500LL * 5000LL * 5000LL)

Maximum number of unique session strings that can be generated.

Calculated from wordlist entropy: 2500 adjectives × 5000 nouns × 5000 nouns = 62,500,000,000 combinations

Definition at line 52 of file discovery/strings.h.

◆ SESSION_STRING_BUFFER_SIZE

#define SESSION_STRING_BUFFER_SIZE   (SESSION_STRING_MAX_LEN + 1)

Buffer size needed for session string storage (with null terminator) Used for static character array declarations.

Definition at line 44 of file discovery/strings.h.

◆ SESSION_STRING_MAX_LEN

#define SESSION_STRING_MAX_LEN   38

Maximum length of a session string (without null terminator).

Calculated from longest words:

  • Longest adjective: 12 chars (e.g., "affectionate")
  • Longest noun: 12 chars (e.g., "acquaintance")
  • Format: adjective-noun-noun = 12 + 1 + 12 + 1 + 12 = 38 chars

Definition at line 38 of file discovery/strings.h.

Referenced by is_session_string().

Function Documentation

◆ acds_string_generate()

asciichat_error_t acds_string_generate ( char * output,
size_t output_size )

Generate random session string.

Parameters
outputOutput buffer for session string
output_sizeSize of output buffer (should be at least SESSION_STRING_BUFFER_SIZE bytes)
Returns
ASCIICHAT_OK on success, error code otherwise

Generates strings in format: adjective-noun-noun Example: "affectionate-acquaintance-acquaintance" (38 characters max)

Use acds_string_get_max_length() to get the maximum possible length. Entropy: 2500 adjectives * 5000 nouns * 5000 nouns = 62.5 billion combinations

Definition at line 250 of file discovery/strings.c.

250 {
251 if (!output || output_size < SESSION_STRING_BUFFER_SIZE) {
253 "output buffer must be at least %zu bytes",
255 }
256
257 // libsodium is guaranteed to be initialized by acds_string_init() before this
258 // is called No need to call sodium_init() again (redundant initialization
259 // removed)
260
261 // Pick random adjective
262 uint32_t adj_idx = randombytes_uniform((uint32_t)adjectives_count);
263 const char *adj = adjectives[adj_idx];
264
265 // Pick two random nouns
266 uint32_t noun1_idx = randombytes_uniform((uint32_t)nouns_count);
267 uint32_t noun2_idx = randombytes_uniform((uint32_t)nouns_count);
268 const char *noun1 = nouns[noun1_idx];
269 const char *noun2 = nouns[noun2_idx];
270
271 // Format: adjective-noun-noun
272 int written =
273 safe_snprintf(output, output_size, "%s-%s-%s", adj, noun1, noun2);
274 if (written < 0 || (size_t)written >= output_size) {
276 "Session string too long for buffer");
277 }
278
279 log_dev("Generated session string: %s", output);
280 return ASCIICHAT_OK;
281}
const char * adjectives[]
Array of English adjectives for ACDS session naming.
Definition adjectives.c:55
const size_t adjectives_count
Count of adjectives in the adjectives array.
Definition adjectives.c:682
#define SET_ERRNO(code,...)
unsigned int uint32_t
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
@ ERROR_BUFFER_OVERFLOW
#define log_dev(...)
Log a DEV message (most verbose, development only).
Definition log/log.h:551
const char * nouns[]
Array of English nouns for session and client naming.
Definition nouns.c:58
const size_t nouns_count
Count of nouns in the nouns array.
Definition nouns.c:1310
#define SESSION_STRING_BUFFER_SIZE

References adjectives, adjectives_count, ASCIICHAT_OK, ERROR_BUFFER_OVERFLOW, ERROR_INVALID_PARAM, log_dev, nouns, nouns_count, SESSION_STRING_BUFFER_SIZE, and SET_ERRNO.

Referenced by options_preset_unified().

◆ acds_string_init()

asciichat_error_t acds_string_init ( void )

Initialize session string system with cached word validation.

Returns
ASCIICHAT_OK on success, error code otherwise

Initializes the random number generator and caches adjectives/nouns in a hashtable for fast validation. Must be called before acds_string_generate() and before any is_session_string() validation calls.

Uses libsodium's randombytes for cryptographically secure randomness. Cleans up cached data on program exit via atexit() handler.

Definition at line 248 of file discovery/strings.c.

248{ return ASCIICHAT_OK; }

References ASCIICHAT_OK.

◆ acds_strings_destroy()

void acds_strings_destroy ( void )

Cleanup function for session string cache Called by asciichat_shared_destroy() during library cleanup. Safe to call multiple times (idempotent).

Definition at line 63 of file discovery/strings.c.

63 {
64 // Shutdown if initialized
65 if (!lifecycle_shutdown(&g_cache_lc)) {
66 return; // Not initialized
67 }
68
69 // Cleanup adjectives cache
70 word_cache_entry_t *adj_entry, *adj_tmp;
71 HASH_ITER(hh, g_adjectives_cache, adj_entry, adj_tmp) {
72 HASH_DEL(g_adjectives_cache, adj_entry);
73 SAFE_FREE(adj_entry->word);
74 SAFE_FREE(adj_entry);
75 }
76 g_adjectives_cache = NULL;
77
78 // Cleanup nouns cache
79 word_cache_entry_t *noun_entry, *noun_tmp;
80 HASH_ITER(hh, g_nouns_cache, noun_entry, noun_tmp) {
81 HASH_DEL(g_nouns_cache, noun_entry);
82 SAFE_FREE(noun_entry->word);
83 SAFE_FREE(noun_entry);
84 }
85 g_nouns_cache = NULL;
86
87 log_dev("Session string word cache cleaned up");
88}
#define SAFE_FREE(ptr)
One owned word in the adjective or noun validation hash table.
char * word
The word string (key).

References log_dev, SAFE_FREE, and word_cache_entry_t::word.

Referenced by asciichat_shared_destroy().

◆ is_session_string()

bool is_session_string ( const char * str)

Check if a string is a valid session string.

Parameters
strString to validate
Returns
true if valid session string (adjective-noun-noun format), false otherwise

A valid session string must:

  • Be in format: adjective-noun-noun
  • Contain exactly 3 words separated by exactly 2 hyphens
  • Have each word be a real adjective or noun from cached wordlists
  • Contain only lowercase ASCII letters and hyphens
  • Be 5-47 characters long
  • Not start or end with hyphen
  • Not contain consecutive hyphens

This function validates against the cached wordlists populated by acds_string_init(). Non-session inputs return false without setting an error. Internal failures set errno.

Definition at line 283 of file discovery/strings.c.

283 {
284 if (!str || str[0] == '\0') {
285 return false;
286 }
287
288 size_t len = strlen(str);
289
290 // Check length bounds (min 4, max defined by SESSION_STRING_MAX_LEN)
291 if (len < 4 || len > SESSION_STRING_MAX_LEN) {
292 return false;
293 }
294
295 // Session strings must be ASCII-only (homograph attack prevention)
296 if (!utf8_is_ascii_only(str)) {
297 return false;
298 }
299
300 // Get compiled regex (lazy initialization)
301 pcre2_code *regex = session_format_regex_get();
302 if (!regex) {
304 "Session string PCRE2 regex failed to initialize");
305 return false;
306 }
307
308 // Validate format using PCRE2 regex
309 pcre2_match_data *match_data =
310 pcre2_match_data_create_from_pattern(regex, NULL);
311 if (!match_data) {
312 log_error("Failed to allocate match data for session string regex");
313 SET_ERRNO(ERROR_MEMORY, "Failed to allocate match data");
314 return false;
315 }
316
317 int match_result =
318 pcre2_match(regex, (PCRE2_SPTR8)str, len, 0, 0, match_data, NULL);
319
320 pcre2_match_data_free(match_data);
321
322 if (match_result < 0) {
323 if (match_result != PCRE2_ERROR_NOMATCH) {
324 SET_ERRNO(ERROR_INTERNAL, "Session string PCRE2 matching failed: %d",
325 match_result);
326 }
327 return false;
328 }
329
330 // Extract the three words by finding hyphens
331 // Format is guaranteed to be: word-word-word where each word is 2-12
332 // lowercase letters
333 const char *hyphen1 = strchr(str, '-');
334 if (!hyphen1) {
335 return false;
336 }
337
338 const char *hyphen2 = strchr(hyphen1 + 1, '-');
339 if (!hyphen2) {
340 return false;
341 }
342
343 // Extract three words
344 size_t adj_len = hyphen1 - str;
345 size_t noun1_len = hyphen2 - hyphen1 - 1;
346 size_t noun2_len = len - (hyphen2 - str) - 1;
347
348 if (adj_len >= 32 || noun1_len >= 32 || noun2_len >= 32) {
349 return false;
350 }
351
355 memcpy(adj, str, adj_len);
356 adj[adj_len] = '\0';
357 memcpy(noun1, hyphen1 + 1, noun1_len);
358 noun1[noun1_len] = '\0';
359 memcpy(noun2, hyphen2 + 1, noun2_len);
360 noun2[noun2_len] = '\0';
361
362 // Lazy initialization: build validation caches on first use
363 // call_once ensures thread-safe exactly-once execution
364 build_validation_caches();
365 // If initialization fails silently (logs warning), hashtables will be empty
366 // and validation will fail, which is safe behavior
367
368 // Validate first word is an adjective
369 word_cache_entry_t *adj_entry = NULL;
370 HASH_FIND_STR(g_adjectives_cache, adj, adj_entry);
371 if (!adj_entry) {
372 return false;
373 }
374
375 // Validate second and third words are nouns
376 word_cache_entry_t *noun_entry1 = NULL;
377 HASH_FIND_STR(g_nouns_cache, noun1, noun_entry1);
378 if (!noun_entry1) {
379 return false;
380 }
381
382 word_cache_entry_t *noun_entry2 = NULL;
383 HASH_FIND_STR(g_nouns_cache, noun2, noun_entry2);
384 if (!noun_entry2) {
385 return false;
386 }
387
388 log_dev("Valid session string: %s", str);
389 return true;
390}
#define DISCOVERY_STRINGS_ADJ_BUFFER_SIZE
#define DISCOVERY_STRINGS_NOUN1_BUFFER_SIZE
#define DISCOVERY_STRINGS_NOUN2_BUFFER_SIZE
#define SESSION_STRING_MAX_LEN
Maximum length of a session string (without null terminator).
@ ERROR_MEMORY
Definition error_codes.h:55
@ ERROR_INTERNAL
Definition error_codes.h:94
#define log_error(...)
Log an ERROR message.
Definition log/log.h:604
bool utf8_is_ascii_only(const char *str)
Check if a string contains only ASCII characters.
Definition utf8.c:141

References DISCOVERY_STRINGS_ADJ_BUFFER_SIZE, DISCOVERY_STRINGS_NOUN1_BUFFER_SIZE, DISCOVERY_STRINGS_NOUN2_BUFFER_SIZE, ERROR_INTERNAL, ERROR_MEMORY, log_dev, log_error, SESSION_STRING_MAX_LEN, SET_ERRNO, and utf8_is_ascii_only().

Referenced by discover_session_parallel(), and parse_client_address().