+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
main
| 1 | /* |
| 2 | * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0 |
| 3 | * Copyright (c) 2026 Saif Mukhtar |
| 4 | * |
| 5 | * OpenAVX - Open Audio-Video eXchange |
| 6 | * src/avx_config.c - JSON configuration parser (openavx.json) |
| 7 | */ |
| 8 | |
| 9 | #include "avx_config.h" |
| 10 | #include "avx_platform.h" |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <stdbool.h> |
| 15 | |
| 16 | static avx_mutex_t g_config_lock = AVX_MUTEX_INITIALIZER; |
| 17 | static avx_config_t g_avx_config = {0}; |
| 18 | |
| 19 | static const char* skip_ws(const char* p) { |
| 20 | if (!p) return NULL; |
| 21 | while (*p && (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')) { |
| 22 | p++; |
| 23 | } |
| 24 | return p; |
| 25 | } |
| 26 | |
| 27 | static const char* parse_string(const char* p, char* out, size_t max_len) { |
| 28 | p = skip_ws(p); |
| 29 | if (!p || *p != '"') return NULL; |
| 30 | p++; /* skip opening quote */ |
| 31 | |
| 32 | size_t idx = 0; |
| 33 | while (*p && *p != '"') { |
| 34 | if (*p == '\\') { |
| 35 | p++; |
| 36 | if (!*p) break; |
| 37 | if (*p == '"' || *p == '\\' || *p == '/') { |
| 38 | if (idx + 1 < max_len) out[idx++] = *p; |
| 39 | } else if (*p == 'n') { |
| 40 | if (idx + 1 < max_len) out[idx++] = '\n'; |
| 41 | } else if (*p == 'r') { |
| 42 | if (idx + 1 < max_len) out[idx++] = '\r'; |
| 43 | } else if (*p == 't') { |
| 44 | if (idx + 1 < max_len) out[idx++] = '\t'; |
| 45 | } else { |
| 46 | if (idx + 1 < max_len) out[idx++] = *p; |
| 47 | } |
| 48 | } else { |
| 49 | if (idx + 1 < max_len) out[idx++] = *p; |
| 50 | } |
| 51 | p++; |
| 52 | } |
| 53 | out[idx] = '\0'; |
| 54 | if (*p == '"') { |
| 55 | p++; /* skip closing quote */ |
| 56 | } else { |
| 57 | return NULL; /* Malformed / unclosed string */ |
| 58 | } |
| 59 | return p; |
| 60 | } |
| 61 | |
| 62 | static const char* parse_string_or_array(const char* p, char* out, size_t max_len) { |
| 63 | p = skip_ws(p); |
| 64 | if (!p) return NULL; |
| 65 | |
| 66 | if (*p == '"') { |
| 67 | return parse_string(p, out, max_len); |
| 68 | } |
| 69 | |
| 70 | if (*p == '[') { |
| 71 | p++; /* skip '[' */ |
| 72 | size_t idx = 0; |
| 73 | out[0] = '\0'; |
| 74 | while (p && *p && *p != ']') { |
| 75 | p = skip_ws(p); |
| 76 | if (!p || !*p || *p == ']') break; |
| 77 | if (*p == '"') { |
| 78 | char item[256]; |
| 79 | p = parse_string(p, item, sizeof(item)); |
| 80 | if (!p) break; |
| 81 | size_t ilen = strlen(item); |
| 82 | if (idx > 0 && idx + 1 < max_len) { |
| 83 | out[idx++] = ','; |
| 84 | out[idx] = '\0'; |
| 85 | } |
| 86 | if (idx + ilen < max_len) { |
| 87 | memcpy(out + idx, item, ilen); |
| 88 | idx += ilen; |
| 89 | out[idx] = '\0'; |
| 90 | } |
| 91 | } else if (*p == ',') { |
| 92 | p++; |
| 93 | } else { |
| 94 | p++; |
| 95 | } |
| 96 | } |
| 97 | if (p && *p == ']') p++; |
| 98 | return p; |
| 99 | } |
| 100 | |
| 101 | return p; |
| 102 | } |
| 103 | |
| 104 | static const char* parse_bool(const char* p, bool* out_val) { |
| 105 | p = skip_ws(p); |
| 106 | if (!p) return NULL; |
| 107 | |
| 108 | if (strncmp(p, "true", 4) == 0) { |
| 109 | if (out_val) *out_val = true; |
| 110 | return p + 4; |
| 111 | } |
| 112 | if (strncmp(p, "false", 5) == 0) { |
| 113 | if (out_val) *out_val = false; |
| 114 | return p + 5; |
| 115 | } |
| 116 | return p; |
| 117 | } |
| 118 | |
| 119 | static const char* parse_int(const char* p, int* out_val) { |
| 120 | p = skip_ws(p); |
| 121 | if (!p) return NULL; |
| 122 | |
| 123 | char* endptr = NULL; |
| 124 | long val = strtol(p, &endptr, 10); |
| 125 | if (endptr && endptr != p) { |
| 126 | if (out_val) *out_val = (int)val; |
| 127 | return endptr; |
| 128 | } |
| 129 | return p; |
| 130 | } |
| 131 | |
| 132 | /* Zero-dependency C99 JSON parser extracting discovery servers, network interfaces, and wire profiles. */ |
| 133 | bool avx_config_parse_json(const char* json_str, avx_config_t* out_cfg) { |
| 134 | if (!json_str || !out_cfg) return false; |
| 135 | memset(out_cfg, 0, sizeof(*out_cfg)); |
| 136 | out_cfg->multicast_ttl = 1; |
| 137 | out_cfg->max_clients = 32; |
| 138 | snprintf(out_cfg->transport, sizeof(out_cfg->transport), "tcp"); |
| 139 | |
| 140 | const char* p = json_str; |
| 141 | char scope_stack[8][64]; |
| 142 | int scope_depth = 0; |
| 143 | |
| 144 | while (p && *p) { |
| 145 | p = skip_ws(p); |
| 146 | if (!p || !*p) break; |
| 147 | |
| 148 | if (*p == '{') { |
| 149 | p++; |
| 150 | continue; |
| 151 | } else if (*p == '}') { |
| 152 | if (scope_depth > 0) scope_depth--; |
| 153 | p++; |
| 154 | continue; |
| 155 | } else if (*p == ',' || *p == ':') { |
| 156 | p++; |
| 157 | continue; |
| 158 | } else if (*p == '"') { |
| 159 | char key[64] = {0}; |
| 160 | p = parse_string(p, key, sizeof(key)); |
| 161 | if (!p) break; |
| 162 | |
| 163 | p = skip_ws(p); |
| 164 | if (p && *p == ':') p++; |
| 165 | p = skip_ws(p); |
| 166 | |
| 167 | if (*p == '{') { |
| 168 | /* Enter new nested object scope */ |
| 169 | if (scope_depth < 8) { |
| 170 | snprintf(scope_stack[scope_depth], sizeof(scope_stack[scope_depth]), "%s", key); |
| 171 | scope_depth++; |
| 172 | } |
| 173 | p++; |
| 174 | continue; |
| 175 | } |
| 176 | |
| 177 | /* Identify current enclosing object */ |
| 178 | const char* current_scope = (scope_depth > 0) ? scope_stack[scope_depth - 1] : ""; |
| 179 | bool in_network = (strcmp(current_scope, "network") == 0); |
| 180 | bool in_discovery = (strcmp(current_scope, "discovery") == 0); |
| 181 | bool in_channels_or_sources = (strcmp(current_scope, "channels") == 0 || |
| 182 | strcmp(current_scope, "sources") == 0); |
| 183 | bool in_multicast = (strcmp(current_scope, "multicast") == 0); |
| 184 | bool in_transports = (strcmp(current_scope, "transports") == 0); |
| 185 | |
| 186 | /* Discovery & Peer settings */ |
| 187 | if ((in_discovery || in_network || scope_depth <= 2) && |
| 188 | (strcmp(key, "servers") == 0 || strcmp(key, "discovery_servers") == 0)) { |
| 189 | p = parse_string_or_array(p, out_cfg->discovery_servers, sizeof(out_cfg->discovery_servers)); |
| 190 | } else if ((in_discovery || in_network || scope_depth <= 2) && |
| 191 | (strcmp(key, "static_peers") == 0 || strcmp(key, "peers") == 0)) { |
| 192 | p = parse_string_or_array(p, out_cfg->static_peers, sizeof(out_cfg->static_peers)); |
| 193 | } |
| 194 | /* Channel & Stream distribution domains */ |
| 195 | else if ((in_channels_or_sources || scope_depth <= 2) && |
| 196 | (strcmp(key, "publish") == 0 || strcmp(key, "groups") == 0)) { |
| 197 | p = parse_string_or_array(p, out_cfg->channels_publish, sizeof(out_cfg->channels_publish)); |
| 198 | } else if ((in_channels_or_sources || scope_depth <= 2) && |
| 199 | strcmp(key, "subscribe") == 0) { |
| 200 | p = parse_string_or_array(p, out_cfg->channels_subscribe, sizeof(out_cfg->channels_subscribe)); |
| 201 | } |
| 202 | /* Multicast Layer 3 Transport settings */ |
| 203 | else if (in_multicast && strcmp(key, "enabled") == 0) { |
| 204 | p = parse_bool(p, &out_cfg->multicast_enabled); |
| 205 | } else if (in_multicast && strcmp(key, "ttl") == 0) { |
| 206 | p = parse_int(p, &out_cfg->multicast_ttl); |
| 207 | } else if (in_multicast && strcmp(key, "prefix") == 0) { |
| 208 | p = parse_string(p, out_cfg->multicast_prefix, sizeof(out_cfg->multicast_prefix)); |
| 209 | } else if (in_multicast && strcmp(key, "netmask") == 0) { |
| 210 | p = parse_string(p, out_cfg->multicast_netmask, sizeof(out_cfg->multicast_netmask)); |
| 211 | } |
| 212 | /* Max client fan-out ceiling (top-level or inside sources) */ |
| 213 | else if ((in_channels_or_sources || scope_depth <= 2) && |
| 214 | (strcmp(key, "max_clients") == 0 || strcmp(key, "max_clients_per_stream") == 0)) { |
| 215 | p = parse_int(p, &out_cfg->max_clients); |
| 216 | } |
| 217 | /* Transport mode: "tcp" (default), "rudp", "udp" */ |
| 218 | else if ((in_transports || scope_depth <= 2) && |
| 219 | (strcmp(key, "transport") == 0 || strcmp(key, "mode") == 0)) { |
| 220 | p = parse_string(p, out_cfg->transport, sizeof(out_cfg->transport)); |
| 221 | } |
| 222 | /* Network Interface & IP binding */ |
| 223 | else if ((in_network || scope_depth <= 2) && |
| 224 | (strcmp(key, "bind_ip") == 0 || strcmp(key, "bind_address") == 0 || strcmp(key, "ip") == 0)) { |
| 225 | p = parse_string(p, out_cfg->bind_ip, sizeof(out_cfg->bind_ip)); |
| 226 | } else if ((in_network || scope_depth <= 2) && |
| 227 | (strcmp(key, "interface") == 0 || strcmp(key, "nic") == 0 || strcmp(key, "adapter") == 0)) { |
| 228 | p = parse_string(p, out_cfg->nic_interface, sizeof(out_cfg->nic_interface)); |
| 229 | if (out_cfg->bind_ip[0] == '\0') { |
| 230 | snprintf(out_cfg->bind_ip, sizeof(out_cfg->bind_ip), "%s", out_cfg->nic_interface); |
| 231 | } |
| 232 | } |
| 233 | } else { |
| 234 | /* Skip unhandled token / character */ |
| 235 | p++; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | return true; |
| 240 | } |
| 241 | |
| 242 | static bool check_config_in_dir(const char* dir, char* out_path, size_t max_len) { |
| 243 | if (!dir || dir[0] == '\0') return false; |
| 244 | /* Primary modern standard OpenAVX config file */ |
| 245 | snprintf(out_path, max_len, "%s/openavx.json", dir); |
| 246 | if (access(out_path, R_OK) == 0) return true; |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | /* Locates openavx.json by hierarchical lookup in working directory, ~/.openavx/, and /etc/openavx/. */ |
| 251 | bool avx_config_find_path(char* out_path, size_t max_len) { |
| 252 | if (!out_path || max_len == 0) return false; |
| 253 | |
| 254 | /* 0. Explicit configuration file path */ |
| 255 | const char* config_file = getenv("OPENAVX_CONFIG_FILE"); |
| 256 | if (config_file && config_file[0] != '\0') { |
| 257 | if (access(config_file, R_OK) == 0) { |
| 258 | snprintf(out_path, max_len, "%s", config_file); |
| 259 | return true; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /* 1. Check $OPENAVX_CONFIG_DIR */ |
| 264 | const char* config_dir = getenv("OPENAVX_CONFIG_DIR"); |
| 265 | if (check_config_in_dir(config_dir, out_path, max_len)) return true; |
| 266 | |
| 267 | /* 2. Check $HOME/.openavx */ |
| 268 | const char* home = getenv("HOME"); |
| 269 | if (home && home[0] != '\0') { |
| 270 | char user_dir[512]; |
| 271 | snprintf(user_dir, sizeof(user_dir), "%s/.openavx", home); |
| 272 | if (check_config_in_dir(user_dir, out_path, max_len)) return true; |
| 273 | } |
| 274 | |
| 275 | /* 3. Check /etc/openavx */ |
| 276 | if (check_config_in_dir("/etc/openavx", out_path, max_len)) return true; |
| 277 | |
| 278 | /* 4. Windows ProgramData fallback */ |
| 279 | const char* prog_data = getenv("PROGRAMDATA"); |
| 280 | if (prog_data && prog_data[0] != '\0') { |
| 281 | char win_dir[512]; |
| 282 | snprintf(win_dir, sizeof(win_dir), "%s/OpenAVX", prog_data); |
| 283 | if (check_config_in_dir(win_dir, out_path, max_len)) return true; |
| 284 | } |
| 285 | |
| 286 | out_path[0] = '\0'; |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | void avx_config_init(void) { |
| 291 | avx_mutex_lock(&g_config_lock); |
| 292 | if (g_avx_config.loaded) { |
| 293 | avx_mutex_unlock(&g_config_lock); |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | char path[512] = {0}; |
| 298 | if (avx_config_find_path(path, sizeof(path))) { |
| 299 | FILE* f = fopen(path, "rb"); |
| 300 | if (f) { |
| 301 | fseek(f, 0, SEEK_END); |
| 302 | long sz = ftell(f); |
| 303 | fseek(f, 0, SEEK_SET); |
| 304 | if (sz > 0 && sz < 65536) { |
| 305 | char* buf = (char*)malloc((size_t)sz + 1); |
| 306 | if (buf) { |
| 307 | size_t read_bytes = fread(buf, 1, (size_t)sz, f); |
| 308 | buf[read_bytes] = '\0'; |
| 309 | avx_config_parse_json(buf, &g_avx_config); |
| 310 | snprintf(g_avx_config.config_path, sizeof(g_avx_config.config_path), "%s", path); |
| 311 | g_avx_config.loaded = true; |
| 312 | free(buf); |
| 313 | } |
| 314 | } |
| 315 | fclose(f); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | if (!g_avx_config.loaded) { |
| 320 | snprintf(g_avx_config.transport, sizeof(g_avx_config.transport), "tcp"); |
| 321 | g_avx_config.max_clients = 32; |
| 322 | g_avx_config.multicast_ttl = 1; |
| 323 | } |
| 324 | |
| 325 | const char* env_transport = getenv("OPENAVX_TRANSPORT"); |
| 326 | if (env_transport && env_transport[0] != '\0') { |
| 327 | snprintf(g_avx_config.transport, sizeof(g_avx_config.transport), "%s", env_transport); |
| 328 | } |
| 329 | |
| 330 | avx_mutex_unlock(&g_config_lock); |
| 331 | } |
| 332 | |
| 333 | void avx_config_cleanup(void) { |
| 334 | avx_mutex_lock(&g_config_lock); |
| 335 | memset(&g_avx_config, 0, sizeof(g_avx_config)); |
| 336 | avx_mutex_unlock(&g_config_lock); |
| 337 | } |
| 338 | |
| 339 | const avx_config_t* avx_config_get(void) { |
| 340 | avx_mutex_lock(&g_config_lock); |
| 341 | bool need_init = !g_avx_config.loaded; |
| 342 | avx_mutex_unlock(&g_config_lock); |
| 343 | |
| 344 | if (need_init) { |
| 345 | avx_config_init(); |
| 346 | } |
| 347 | return &g_avx_config; |
| 348 | } |