ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
stun.c
Go to the documentation of this file.
1
6
8#include <ascii-chat/common.h>
11#include <string.h>
12#include <ctype.h>
13#include <pcre2.h>
14
20
21// Pattern: Match comma-separated entries (trimmed)
22// Matches: optional whitespace, capture non-comma chars (trimmed), optional whitespace, then comma or end
23static const char *STUN_ENTRY_PATTERN = "\\s*([^,]+?)\\s*(?=,|$)";
24
29static pcre2_code *stun_entry_regex_get(void) {
30 return asciichat_pcre2_compile(STUN_ENTRY_PATTERN, PCRE2_MULTILINE);
31}
32
45int stun_servers_parse(const char *csv_servers, const char *default_csv, stun_server_t *out_servers, int max_count) {
46 if (!out_servers || max_count <= 0) {
47 log_warn("Invalid output array or max_count");
48 return -1;
49 }
50
51 // Use defaults if input is empty
52 const char *servers_to_parse = csv_servers;
53 if (!servers_to_parse || servers_to_parse[0] == '\0') {
54 servers_to_parse = default_csv;
55 }
56
57 if (!servers_to_parse || servers_to_parse[0] == '\0') {
58 log_warn("No servers to parse and no defaults provided");
59 return 0;
60 }
61
62 // Get compiled regex (owned by the caller)
63 pcre2_code *regex = stun_entry_regex_get();
64
65 // If regex not available, fall back to manual parsing
66 if (!regex) {
67 log_warn("PCRE2 regex not available, falling back to manual parsing");
68 // Fallback: manual character-by-character parsing (original implementation)
69 int count = 0;
70 const char *current = servers_to_parse;
71 const char *end;
72
73 while (count < max_count && *current != '\0') {
74 while (isspace(*current)) {
75 current++;
76 }
77 if (*current == '\0')
78 break;
79
80 end = strchr(current, ',');
81 if (!end) {
82 end = current + strlen(current);
83 }
84
85 while (end > current && isspace(*(end - 1))) {
86 end--;
87 }
88
89 size_t len = end - current;
90 if (len == 0) {
91 current = *end ? end + 1 : end;
92 continue;
93 }
94
95 if (len >= STUN_URL_BUFFER_SIZE) {
96 log_warn("STUN server URL too long (max %d): %.*s", STUN_URL_BUFFER_SIZE - 1, (int)len, current);
97 return -1;
98 }
99
100 out_servers[count].host_len = (uint8_t)len;
101 memcpy(out_servers[count].host, current, len);
102 out_servers[count].host[len] = '\0';
103 count++;
104
105 current = *end ? end + 1 : end;
106 }
107 return count;
108 }
109
110 // Parse using PCRE2 regex: match each trimmed entry
111 int count = 0;
112 pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(regex, NULL);
113 if (!match_data) {
114 pcre2_code_free(regex);
115 log_warn("Failed to allocate PCRE2 match data");
116 return -1;
117 }
118
119 size_t offset = 0;
120 int rc;
121
122 while (count < max_count) {
123 rc = asciichat_pcre2_match(regex, (PCRE2_SPTR8)servers_to_parse, strlen(servers_to_parse), offset, 0, match_data, NULL);
124
125 if (rc < 0) {
126 // No more matches (PCRE2_ERROR_NOMATCH or other error)
127 break;
128 }
129
130 // Extract the captured group (group 1)
131 size_t len;
132 const char *server_url = asciichat_pcre2_extract_group_ptr(match_data, 1, servers_to_parse, &len);
133
134 if (!server_url) {
135 break; // Group not matched
136 }
137
138 if (len >= STUN_URL_BUFFER_SIZE) {
139 log_warn("STUN server URL too long (max %d): %.*s", STUN_URL_BUFFER_SIZE - 1, (int)len, server_url);
140 pcre2_match_data_free(match_data);
141 pcre2_code_free(regex);
142 return -1;
143 }
144
145 // Copy trimmed entry to output
146 out_servers[count].host_len = (uint8_t)len;
147 memcpy(out_servers[count].host, server_url, len);
148 out_servers[count].host[len] = '\0';
149
150 count++;
151
152 // Move offset to end of this match (need ovector for overall match end)
153 PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
154 offset = ovector[1];
155
156 // Skip comma if present
157 if (offset < strlen(servers_to_parse) && servers_to_parse[offset] == ',') {
158 offset++;
159 }
160 }
161
162 pcre2_match_data_free(match_data);
163 pcre2_code_free(regex);
164 return count;
165}
unsigned char uint8_t
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
stun_server_t
Definition stun.h:78
int stun_servers_parse(const char *csv_servers, const char *default_csv, stun_server_t *out_servers, int max_count)
Parse comma-separated STUN server URLs into stun_server_t array.
Definition stun.c:45
#define STUN_URL_BUFFER_SIZE
STUN server configuration.
Definition stun.h:73
⚙️ Common definitions, error codes, macros, and types shared throughout the application
📝 Logging API with multiple log levels and terminal output control
pcre2_code * asciichat_pcre2_compile(const char *pattern, uint32_t flags)
Definition pcre2.c:16
int asciichat_pcre2_match(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length, PCRE2_SIZE startoffset, uint32_t options, pcre2_match_data *match_data, pcre2_match_context *context)
Definition pcre2.c:161
const char * asciichat_pcre2_extract_group_ptr(pcre2_match_data *match_data, int group_num, const char *subject, size_t *out_len)
Extract numbered capture group as pointer into subject (non-allocating).
Definition pcre2.c:103
PCRE2 compilation and match helpers.
🔤 String Manipulation and Shell Escaping Utilities