+ +
42d8d3b fix(portability): resolve Windows MSVC MF linkage and macOS BSD socket and VideoToolbox compatibility
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_find.c - Network source discovery (mDNS and discovery server client) |
| 7 | */ |
| 8 | |
| 9 | #define _GNU_SOURCE |
| 10 | #include "avx_internal.h" |
| 11 | #include "avx_discovery_server.h" |
| 12 | #include "avx_wire.h" |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include <time.h> |
| 17 | #if defined(AVX_OS_POSIX) |
| 18 | #include <ifaddrs.h> |
| 19 | #include <net/if.h> |
| 20 | #endif |
| 21 | |
| 22 | /* Binds UDP socket to mDNS port 5353 with SO_REUSEADDR and joins 224.0.0.251 on all active network interfaces. */ |
| 23 | static avx_socket_t avx_mdns_open_listener_socket(void) { |
| 24 | avx_socket_t sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 25 | if (sock == AVX_INVALID_SOCKET) return AVX_INVALID_SOCKET; |
| 26 | |
| 27 | int reuse = 1; |
| 28 | setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)); |
| 29 | #ifdef SO_REUSEPORT |
| 30 | setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuse, sizeof(reuse)); |
| 31 | #endif |
| 32 | |
| 33 | avx_socket_set_nonblocking(sock, true); |
| 34 | |
| 35 | struct sockaddr_in bind_addr; |
| 36 | memset(&bind_addr, 0, sizeof(bind_addr)); |
| 37 | bind_addr.sin_family = AF_INET; |
| 38 | bind_addr.sin_port = htons(AVX_MDNS_PORT); |
| 39 | bind_addr.sin_addr.s_addr = htonl(INADDR_ANY); |
| 40 | |
| 41 | if (bind(sock, (struct sockaddr*)&bind_addr, sizeof(bind_addr)) < 0) { |
| 42 | /* Fall back to ephemeral port if 5353 is already held exclusively */ |
| 43 | bind_addr.sin_port = 0; |
| 44 | if (bind(sock, (struct sockaddr*)&bind_addr, sizeof(bind_addr)) < 0) { |
| 45 | avx_socket_close(sock); |
| 46 | return AVX_INVALID_SOCKET; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | struct ip_mreq mreq; |
| 51 | memset(&mreq, 0, sizeof(mreq)); |
| 52 | inet_pton(AF_INET, AVX_MDNS_MCAST_ADDR, &mreq.imr_multiaddr); |
| 53 | mreq.imr_interface.s_addr = htonl(INADDR_ANY); |
| 54 | setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mreq, sizeof(mreq)); |
| 55 | |
| 56 | #if defined(AVX_OS_POSIX) |
| 57 | /* Also join multicast group on all active physical/operational interfaces */ |
| 58 | struct ifaddrs *ifaddr = NULL, *ifa = NULL; |
| 59 | if (getifaddrs(&ifaddr) == 0) { |
| 60 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
| 61 | if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET) continue; |
| 62 | if (!(ifa->ifa_flags & IFF_UP) || (ifa->ifa_flags & IFF_LOOPBACK)) continue; |
| 63 | struct sockaddr_in *s4 = (struct sockaddr_in*)ifa->ifa_addr; |
| 64 | struct ip_mreq if_mreq; |
| 65 | memset(&if_mreq, 0, sizeof(if_mreq)); |
| 66 | inet_pton(AF_INET, AVX_MDNS_MCAST_ADDR, &if_mreq.imr_multiaddr); |
| 67 | if_mreq.imr_interface = s4->sin_addr; |
| 68 | setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&if_mreq, sizeof(if_mreq)); |
| 69 | } |
| 70 | freeifaddrs(ifaddr); |
| 71 | } |
| 72 | #endif |
| 73 | |
| 74 | uint8_t loop = 1; |
| 75 | setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, (const char*)&loop, sizeof(loop)); |
| 76 | |
| 77 | /* Also join on loopback interface explicitly for local processes / test suites */ |
| 78 | struct in_addr loop_addr; |
| 79 | loop_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 80 | struct ip_mreq lo_mreq; |
| 81 | memset(&lo_mreq, 0, sizeof(lo_mreq)); |
| 82 | inet_pton(AF_INET, AVX_MDNS_MCAST_ADDR, &lo_mreq.imr_multiaddr); |
| 83 | lo_mreq.imr_interface = loop_addr; |
| 84 | setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&lo_mreq, sizeof(lo_mreq)); |
| 85 | |
| 86 | return sock; |
| 87 | } |
| 88 | |
| 89 | /* Transmits RFC 6762 mDNS PTR query for target service string to multicast group 224.0.0.251:5353. */ |
| 90 | static void avx_mdns_send_query(avx_socket_t sock, const char* service_str) { |
| 91 | if (sock == AVX_INVALID_SOCKET) return; |
| 92 | |
| 93 | struct sockaddr_in mcast_addr; |
| 94 | memset(&mcast_addr, 0, sizeof(mcast_addr)); |
| 95 | mcast_addr.sin_family = AF_INET; |
| 96 | mcast_addr.sin_port = htons(AVX_MDNS_PORT); |
| 97 | inet_pton(AF_INET, AVX_MDNS_MCAST_ADDR, &mcast_addr.sin_addr); |
| 98 | |
| 99 | uint8_t qpkt[256]; |
| 100 | memset(qpkt, 0, sizeof(qpkt)); |
| 101 | |
| 102 | /* Header: ID=0, Flags=0, Questions=1, Answers=0 */ |
| 103 | qpkt[4] = 0x00; qpkt[5] = 0x01; |
| 104 | |
| 105 | const char *svc = (service_str && service_str[0]) ? service_str : OAVX_WIRE_MDNS_SERVICE; |
| 106 | size_t enc_len = avx_wire_encode_dns_service(svc, &qpkt[12], sizeof(qpkt) - 16); |
| 107 | if (enc_len == 0) return; |
| 108 | size_t idx = 12 + enc_len; |
| 109 | |
| 110 | /* QTYPE: PTR (12) */ |
| 111 | qpkt[idx++] = 0x00; qpkt[idx++] = 0x0c; |
| 112 | /* QCLASS: IN (1) */ |
| 113 | qpkt[idx++] = 0x00; qpkt[idx++] = 0x01; |
| 114 | |
| 115 | sendto(sock, qpkt, idx, 0, (struct sockaddr*)&mcast_addr, sizeof(mcast_addr)); |
| 116 | } |
| 117 | |
| 118 | /* Parses incoming mDNS response packets, extracting PTR, SRV, TXT metadata and updating source cache. */ |
| 119 | static void avx_find_drain_mdns(avx_find_ctx_t *slot) { |
| 120 | if (!slot || slot->mdns_sock < 0) return; |
| 121 | |
| 122 | uint8_t pkt[2048]; |
| 123 | struct sockaddr_in from; |
| 124 | socklen_t from_len = sizeof(from); |
| 125 | |
| 126 | while (1) { |
| 127 | memset(&from, 0, sizeof(from)); |
| 128 | from_len = sizeof(from); |
| 129 | ssize_t n = recvfrom(slot->mdns_sock, pkt, sizeof(pkt), 0, |
| 130 | (struct sockaddr*)&from, &from_len); |
| 131 | if (n <= 12) break; |
| 132 | |
| 133 | uint16_t flags = (uint16_t)((pkt[2] << 8) | pkt[3]); |
| 134 | if ((flags & 0x8000) == 0) continue; /* Not a response */ |
| 135 | |
| 136 | char ip_str[64] = "127.0.0.1"; |
| 137 | if (from.sin_family == AF_INET) { |
| 138 | inet_ntop(AF_INET, &from.sin_addr, ip_str, sizeof(ip_str)); |
| 139 | } |
| 140 | |
| 141 | const char *svc = slot->wire_profile.mdns_service ? slot->wire_profile.mdns_service : OAVX_WIRE_MDNS_SERVICE; |
| 142 | uint8_t s_target_service[128]; |
| 143 | size_t s_target_len = avx_wire_encode_dns_service(svc, s_target_service, sizeof(s_target_service)); |
| 144 | if (s_target_len > 1) s_target_len--; /* Match label prefix without null byte */ |
| 145 | |
| 146 | char found_name[256] = {0}; |
| 147 | uint16_t found_port = 0; |
| 148 | |
| 149 | for (ssize_t i = 12; i + (ssize_t)s_target_len + 10 < n; ++i) { |
| 150 | if (s_target_len > 0 && memcmp(&pkt[i], s_target_service, s_target_len) == 0) { |
| 151 | ssize_t cur = i + (ssize_t)s_target_len; |
| 152 | if (pkt[cur] == 0) cur++; |
| 153 | if (cur + 10 > n) break; |
| 154 | |
| 155 | uint16_t rtype = (uint16_t)((pkt[cur] << 8) | pkt[cur + 1]); |
| 156 | cur += 2; |
| 157 | cur += 2; /* skip rclass */ |
| 158 | cur += 4; /* skip ttl */ |
| 159 | if (cur + 2 > n) break; |
| 160 | uint16_t rdlen = (uint16_t)((pkt[cur] << 8) | pkt[cur + 1]); |
| 161 | cur += 2; |
| 162 | |
| 163 | if (rtype == 0x000c && cur + rdlen <= n && rdlen > 1) { |
| 164 | uint8_t nlen = pkt[cur]; |
| 165 | if (nlen > 0 && nlen < 64 && (ssize_t)(cur + 1 + nlen) <= n) { |
| 166 | memcpy(found_name, &pkt[cur + 1], nlen); |
| 167 | found_name[nlen] = '\0'; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /* Scan for SRV record (Type 0x0021 = 33) */ |
| 174 | for (ssize_t i = 12; i + 10 < n; ++i) { |
| 175 | if (pkt[i] == 0x00 && pkt[i + 1] == 0x21) { |
| 176 | ssize_t srv_rdata = i + 2 + 2 + 4 + 2; |
| 177 | if (srv_rdata + 6 <= n) { |
| 178 | uint16_t p = (uint16_t)((pkt[srv_rdata + 4] << 8) | pkt[srv_rdata + 5]); |
| 179 | if (p >= 1024) { |
| 180 | found_port = p; |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if (found_name[0] != '\0') { |
| 188 | if (found_port == 0) found_port = slot->wire_profile.base_port ? slot->wire_profile.base_port : AVX_DEFAULT_PORT; |
| 189 | |
| 190 | char url[256]; |
| 191 | snprintf(url, sizeof(url), "%s:%u", ip_str, found_port); |
| 192 | |
| 193 | bool exists = false; |
| 194 | for (uint32_t s = 0; s < slot->num_sources; ++s) { |
| 195 | if (strcmp(slot->source_names[s], found_name) == 0 && |
| 196 | strcmp(slot->source_urls[s], url) == 0) { |
| 197 | exists = true; |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if (!exists && slot->num_sources < 64) { |
| 203 | uint32_t idx = slot->num_sources++; |
| 204 | snprintf(slot->source_names[idx], sizeof(slot->source_names[idx]), "%s", found_name); |
| 205 | snprintf(slot->source_urls[idx], sizeof(slot->source_urls[idx]), "%s", url); |
| 206 | slot->sources[idx].name = slot->source_names[idx]; |
| 207 | slot->sources[idx].address = slot->source_urls[idx]; |
| 208 | slot->sources[idx].port = found_port; |
| 209 | slot->sources[idx].group = NULL; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | OPENAVX_API avx_find_instance_t avx_find_create(const avx_find_config_t* p_create_settings) { |
| 216 | avx_mutex_lock(&g_avx_state.global_lock); |
| 217 | if (!g_avx_state.initialized) { |
| 218 | avx_mutex_unlock(&g_avx_state.global_lock); |
| 219 | avx_engine_initialize(); |
| 220 | avx_mutex_lock(&g_avx_state.global_lock); |
| 221 | } |
| 222 | |
| 223 | avx_find_ctx_t* slot = NULL; |
| 224 | for (int i = 0; i < AVX_MAX_SESSIONS; ++i) { |
| 225 | if (!g_avx_state.find_slots[i].in_use) { |
| 226 | slot = &g_avx_state.find_slots[i]; |
| 227 | memset(slot, 0, sizeof(*slot)); |
| 228 | slot->in_use = true; |
| 229 | break; |
| 230 | } |
| 231 | } |
| 232 | avx_mutex_unlock(&g_avx_state.global_lock); |
| 233 | |
| 234 | if (!slot) return NULL; |
| 235 | |
| 236 | avx_mutex_init(&slot->lock); |
| 237 | slot->show_local_sources = p_create_settings ? p_create_settings->show_local_sources : true; |
| 238 | |
| 239 | const char *group_env = getenv("OPENAVX_GROUPS"); |
| 240 | const avx_config_t *cfg = avx_config_get(); |
| 241 | |
| 242 | if (p_create_settings && p_create_settings->groups && p_create_settings->groups[0] != '\0') { |
| 243 | snprintf(slot->groups, sizeof(slot->groups), "%.255s", p_create_settings->groups); |
| 244 | } else if (group_env && group_env[0] != '\0') { |
| 245 | snprintf(slot->groups, sizeof(slot->groups), "%.255s", group_env); |
| 246 | } else if (cfg && cfg->channels_subscribe[0] != '\0') { |
| 247 | snprintf(slot->groups, sizeof(slot->groups), "%.255s", cfg->channels_subscribe); |
| 248 | } |
| 249 | |
| 250 | if (p_create_settings && p_create_settings->extra_ips && p_create_settings->extra_ips[0] != '\0') { |
| 251 | snprintf(slot->extra_ips, sizeof(slot->extra_ips), "%.255s", p_create_settings->extra_ips); |
| 252 | } else if (cfg && cfg->static_peers[0] != '\0') { |
| 253 | snprintf(slot->extra_ips, sizeof(slot->extra_ips), "%.255s", cfg->static_peers); |
| 254 | } |
| 255 | |
| 256 | const oavx_wire_profile_t *wp = (p_create_settings && p_create_settings->wire_profile) |
| 257 | ? p_create_settings->wire_profile : avx_wire_get_active_profile(); |
| 258 | slot->wire_profile = *wp; |
| 259 | |
| 260 | slot->mdns_sock = avx_mdns_open_listener_socket(); |
| 261 | if (slot->mdns_sock != AVX_INVALID_SOCKET) { |
| 262 | avx_mdns_send_query(slot->mdns_sock, slot->wire_profile.mdns_service); |
| 263 | } |
| 264 | |
| 265 | return (avx_find_instance_t)slot; |
| 266 | } |
| 267 | |
| 268 | OPENAVX_API void avx_find_destroy(avx_find_instance_t p_instance) { |
| 269 | if (!p_instance) return; |
| 270 | avx_find_ctx_t* slot = (avx_find_ctx_t*)p_instance; |
| 271 | |
| 272 | avx_mutex_lock(&slot->lock); |
| 273 | if (slot->mdns_sock != AVX_INVALID_SOCKET) { |
| 274 | avx_socket_close(slot->mdns_sock); |
| 275 | slot->mdns_sock = AVX_INVALID_SOCKET; |
| 276 | } |
| 277 | avx_mutex_unlock(&slot->lock); |
| 278 | avx_mutex_destroy(&slot->lock); |
| 279 | |
| 280 | avx_mutex_lock(&g_avx_state.global_lock); |
| 281 | slot->in_use = false; |
| 282 | avx_mutex_unlock(&g_avx_state.global_lock); |
| 283 | } |
| 284 | |
| 285 | /* Refreshes stream registry by draining local mDNS multicast responses and querying central discovery servers. */ |
| 286 | static void avx_find_refresh_sources(avx_find_ctx_t *slot) { |
| 287 | if (!slot) return; |
| 288 | |
| 289 | /* Prevent concurrent discovery queries from blocking concurrent callers */ |
| 290 | if (__sync_lock_test_and_set(&slot->refresh_in_progress, 1) != 0) { |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | /* 1. Snapshot active local senders under global_lock without holding slot->lock |
| 295 | * (Eliminates ABBA lock order inversion between slot->lock and global_lock) */ |
| 296 | struct { |
| 297 | char name[256]; |
| 298 | uint16_t port; |
| 299 | const char *group; |
| 300 | } local_senders[AVX_MAX_SESSIONS]; |
| 301 | int num_local_senders = 0; |
| 302 | |
| 303 | bool show_local = false; |
| 304 | char extra_ips_copy[256] = {0}; |
| 305 | avx_mutex_lock(&slot->lock); |
| 306 | show_local = slot->show_local_sources; |
| 307 | if (slot->extra_ips[0]) { |
| 308 | snprintf(extra_ips_copy, sizeof(extra_ips_copy), "%s", slot->extra_ips); |
| 309 | } |
| 310 | avx_mutex_unlock(&slot->lock); |
| 311 | |
| 312 | if (show_local) { |
| 313 | avx_mutex_lock(&g_avx_state.global_lock); |
| 314 | for (int i = 0; i < AVX_MAX_SESSIONS; ++i) { |
| 315 | avx_send_ctx_t *s = &g_avx_state.send_slots[i]; |
| 316 | if (s->in_use && s->running && s->port > 0 && num_local_senders < AVX_MAX_SESSIONS) { |
| 317 | snprintf(local_senders[num_local_senders].name, sizeof(local_senders[num_local_senders].name), "%s", s->name); |
| 318 | local_senders[num_local_senders].port = s->port; |
| 319 | local_senders[num_local_senders].group = s->groups; |
| 320 | num_local_senders++; |
| 321 | } |
| 322 | } |
| 323 | avx_mutex_unlock(&g_avx_state.global_lock); |
| 324 | } |
| 325 | |
| 326 | /* 2. Query Centralized Discovery Server(s) outside of any lock |
| 327 | * (Prevents blocking network I/O from stalling concurrent finder callers): |
| 328 | * 1. OPENAVX_DISCOVERY_SERVER environment variable |
| 329 | * 2. cfg->discovery_servers from openavx.json |
| 330 | * 3. extra_ips_copy (if provided) |
| 331 | */ |
| 332 | char rem_names[64][256]; |
| 333 | char rem_urls[64][256]; |
| 334 | uint16_t rem_ports[64]; |
| 335 | int total_rem = 0; |
| 336 | |
| 337 | const char *ds_env = getenv("OPENAVX_DISCOVERY_SERVER"); |
| 338 | const avx_config_t *cfg = avx_config_get(); |
| 339 | const char *ds_targets = (ds_env && ds_env[0] != '\0') ? ds_env : |
| 340 | (cfg && cfg->discovery_servers[0] != '\0' ? cfg->discovery_servers : extra_ips_copy); |
| 341 | |
| 342 | if (ds_targets && ds_targets[0] != '\0') { |
| 343 | char targets_copy[512]; |
| 344 | snprintf(targets_copy, sizeof(targets_copy), "%s", ds_targets); |
| 345 | |
| 346 | char *saveptr = NULL; |
| 347 | char *token = strtok_r(targets_copy, ",; \t", &saveptr); |
| 348 | while (token && total_rem < 64) { |
| 349 | char srv_ip[64] = "127.0.0.1"; |
| 350 | uint16_t srv_port = AVX_DISCOVERY_SERVER_DEFAULT_PORT; |
| 351 | char *colon = strchr(token, ':'); |
| 352 | if (colon) { |
| 353 | *colon = '\0'; |
| 354 | srv_port = (uint16_t)atoi(colon + 1); |
| 355 | } |
| 356 | snprintf(srv_ip, sizeof(srv_ip), "%s", token); |
| 357 | |
| 358 | char batch_names[32][256]; |
| 359 | char batch_urls[32][256]; |
| 360 | int n_rem = avx_discovery_client_query(srv_ip, srv_port, batch_names, batch_urls, 32); |
| 361 | for (int i = 0; i < n_rem && total_rem < 64; ++i) { |
| 362 | snprintf(rem_names[total_rem], sizeof(rem_names[total_rem]), "%.255s", batch_names[i]); |
| 363 | snprintf(rem_urls[total_rem], sizeof(rem_urls[total_rem]), "%.255s", batch_urls[i]); |
| 364 | rem_ports[total_rem] = srv_port; |
| 365 | total_rem++; |
| 366 | } |
| 367 | token = strtok_r(NULL, ",; \t", &saveptr); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /* 3. Acquire slot->lock strictly to merge discovered endpoints in-memory and drain mDNS */ |
| 372 | avx_mutex_lock(&slot->lock); |
| 373 | |
| 374 | /* Merge local senders */ |
| 375 | for (int i = 0; i < num_local_senders; ++i) { |
| 376 | bool found = false; |
| 377 | for (uint32_t n = 0; n < slot->num_sources; ++n) { |
| 378 | if (strcmp(slot->source_names[n], local_senders[i].name) == 0) { |
| 379 | found = true; |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | if (!found && slot->num_sources < 64) { |
| 384 | uint32_t idx = slot->num_sources++; |
| 385 | snprintf(slot->source_names[idx], sizeof(slot->source_names[idx]), "%.255s", local_senders[i].name); |
| 386 | snprintf(slot->source_urls[idx], sizeof(slot->source_urls[idx]), "127.0.0.1:%u", local_senders[i].port); |
| 387 | slot->sources[idx].name = slot->source_names[idx]; |
| 388 | slot->sources[idx].address = slot->source_urls[idx]; |
| 389 | slot->sources[idx].port = local_senders[i].port; |
| 390 | slot->sources[idx].group = local_senders[i].group; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /* Merge remote centralized sources */ |
| 395 | for (int i = 0; i < total_rem; ++i) { |
| 396 | bool found = false; |
| 397 | for (uint32_t n = 0; n < slot->num_sources; ++n) { |
| 398 | if (strcmp(slot->source_names[n], rem_names[i]) == 0) { |
| 399 | found = true; |
| 400 | break; |
| 401 | } |
| 402 | } |
| 403 | if (!found && slot->num_sources < 64) { |
| 404 | uint32_t idx = slot->num_sources++; |
| 405 | snprintf(slot->source_names[idx], sizeof(slot->source_names[idx]), "%.255s", rem_names[i]); |
| 406 | snprintf(slot->source_urls[idx], sizeof(slot->source_urls[idx]), "%.255s", rem_urls[i]); |
| 407 | slot->sources[idx].name = slot->source_names[idx]; |
| 408 | slot->sources[idx].address = slot->source_urls[idx]; |
| 409 | slot->sources[idx].port = rem_ports[i]; |
| 410 | slot->sources[idx].group = NULL; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | /* Drain and parse any incoming mDNS multicast response datagrams (RFC 6762 / UDP 5353) */ |
| 415 | avx_find_drain_mdns(slot); |
| 416 | |
| 417 | avx_mutex_unlock(&slot->lock); |
| 418 | slot->refresh_in_progress = 0; |
| 419 | } |
| 420 | |
| 421 | OPENAVX_API const oavx_endpoint_t* avx_find_get_sources(avx_find_instance_t p_instance, uint32_t* p_no_sources) { |
| 422 | if (!p_instance || !p_no_sources) return NULL; |
| 423 | avx_find_ctx_t* slot = (avx_find_ctx_t*)p_instance; |
| 424 | |
| 425 | avx_find_refresh_sources(slot); |
| 426 | |
| 427 | avx_mutex_lock(&slot->lock); |
| 428 | *p_no_sources = slot->num_sources; |
| 429 | const oavx_endpoint_t* p_src = slot->num_sources > 0 ? slot->sources : NULL; |
| 430 | avx_mutex_unlock(&slot->lock); |
| 431 | |
| 432 | return p_src; |
| 433 | } |
| 434 | |
| 435 | bool avx_find_wait(avx_find_instance_t p_instance, uint32_t timeout_in_ms) { |
| 436 | if (!p_instance) return false; |
| 437 | avx_find_ctx_t* slot = (avx_find_ctx_t*)p_instance; |
| 438 | |
| 439 | uint32_t elapsed = 0; |
| 440 | while (elapsed < timeout_in_ms) { |
| 441 | avx_find_refresh_sources(slot); |
| 442 | |
| 443 | avx_mutex_lock(&slot->lock); |
| 444 | uint32_t count = slot->num_sources; |
| 445 | avx_socket_t msock = slot->mdns_sock; |
| 446 | const char *svc = slot->wire_profile.mdns_service; |
| 447 | avx_mutex_unlock(&slot->lock); |
| 448 | |
| 449 | if (count > 0) return true; |
| 450 | |
| 451 | /* Periodically broadcast mDNS PTR query to wake up any silent LAN sources */ |
| 452 | if (msock != AVX_INVALID_SOCKET && (elapsed % 200) == 0) { |
| 453 | avx_mdns_send_query(msock, svc); |
| 454 | } |
| 455 | |
| 456 | avx_sleep_ms(10); |
| 457 | elapsed += 10; |
| 458 | } |
| 459 | |
| 460 | avx_find_refresh_sources(slot); |
| 461 | avx_mutex_lock(&slot->lock); |
| 462 | bool has_sources = slot->num_sources > 0; |
| 463 | avx_mutex_unlock(&slot->lock); |
| 464 | |
| 465 | return has_sources; |
| 466 | } |
| 467 | |
| 468 | /* Broadcasts gratuitous mDNS announcement records across all local interfaces advertising the sender stream. */ |
| 469 | int avx_mdns_broadcast_source_ex(const char* name, uint16_t port, const char* bind_ip) { |
| 470 | avx_socket_t sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 471 | if (sock == AVX_INVALID_SOCKET) return -1; |
| 472 | avx_socket_set_nosigpipe(sock); |
| 473 | |
| 474 | uint8_t mcast_loop = 1; |
| 475 | setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, (const char*)&mcast_loop, sizeof(mcast_loop)); |
| 476 | uint8_t mcast_ttl = 255; |
| 477 | setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&mcast_ttl, sizeof(mcast_ttl)); |
| 478 | |
| 479 | uint32_t src_ip_net = htonl(0x7F000001); |
| 480 | if (bind_ip && bind_ip[0] != '\0' && strcmp(bind_ip, "0.0.0.0") != 0) { |
| 481 | struct in_addr if_addr; |
| 482 | if (inet_pton(AF_INET, bind_ip, &if_addr) > 0) { |
| 483 | setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&if_addr, sizeof(if_addr)); |
| 484 | src_ip_net = if_addr.s_addr; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | struct sockaddr_in mcast_addr; |
| 489 | memset(&mcast_addr, 0, sizeof(mcast_addr)); |
| 490 | mcast_addr.sin_family = AF_INET; |
| 491 | mcast_addr.sin_port = htons(AVX_MDNS_PORT); |
| 492 | inet_pton(AF_INET, AVX_MDNS_MCAST_ADDR, &mcast_addr.sin_addr); |
| 493 | |
| 494 | const oavx_wire_profile_t *prof = avx_wire_get_active_profile(); |
| 495 | const char *svc = prof->mdns_service ? prof->mdns_service : OAVX_WIRE_MDNS_SERVICE; |
| 496 | |
| 497 | uint8_t pkt[1024]; |
| 498 | memset(pkt, 0, sizeof(pkt)); |
| 499 | |
| 500 | /* Header: ID=0, Flags=0x8400 (Response, Authoritative), Questions=0, Answers=1, Auth=0, Addl=1 */ |
| 501 | pkt[2] = 0x84; pkt[3] = 0x00; |
| 502 | pkt[6] = 0x00; pkt[7] = 0x01; /* 1 Answer (PTR) */ |
| 503 | pkt[10] = 0x00; pkt[11] = 0x01; /* 1 Additional (SRV) */ |
| 504 | |
| 505 | size_t idx = 12; |
| 506 | size_t svc_labels_len = avx_wire_encode_dns_service(svc, &pkt[idx], sizeof(pkt) - idx - 64); |
| 507 | if (svc_labels_len == 0) { |
| 508 | avx_socket_close(sock); |
| 509 | return -1; |
| 510 | } |
| 511 | idx += svc_labels_len; |
| 512 | |
| 513 | /* Type: PTR (12), Class: IN (1), TTL: 120s */ |
| 514 | pkt[idx++] = 0x00; pkt[idx++] = 0x0c; |
| 515 | pkt[idx++] = 0x00; pkt[idx++] = 0x01; |
| 516 | pkt[idx++] = 0x00; pkt[idx++] = 0x00; pkt[idx++] = 0x00; pkt[idx++] = 0x78; |
| 517 | |
| 518 | /* Data: target domain name: len + Name + svc_labels */ |
| 519 | const char *src_name = (name && name[0]) ? name : "openavx"; |
| 520 | size_t name_len = strlen(src_name); |
| 521 | if (name_len > 63) name_len = 63; |
| 522 | size_t rd_len = 1 + name_len + svc_labels_len; |
| 523 | pkt[idx++] = (uint8_t)(rd_len >> 8); |
| 524 | pkt[idx++] = (uint8_t)(rd_len & 0xff); |
| 525 | |
| 526 | pkt[idx++] = (uint8_t)name_len; |
| 527 | memcpy(&pkt[idx], src_name, name_len); |
| 528 | idx += name_len; |
| 529 | |
| 530 | memcpy(&pkt[idx], &pkt[12], svc_labels_len); |
| 531 | idx += svc_labels_len; |
| 532 | |
| 533 | /* Additional 1: SRV Record announcing Port */ |
| 534 | /* Name: len + Name + svc_labels */ |
| 535 | pkt[idx++] = (uint8_t)name_len; |
| 536 | memcpy(&pkt[idx], src_name, name_len); |
| 537 | idx += name_len; |
| 538 | |
| 539 | memcpy(&pkt[idx], &pkt[12], svc_labels_len); |
| 540 | idx += svc_labels_len; |
| 541 | |
| 542 | /* Type: SRV (33 / 0x0021), Class: IN (1), TTL: 120s */ |
| 543 | pkt[idx++] = 0x00; pkt[idx++] = 0x21; |
| 544 | pkt[idx++] = 0x00; pkt[idx++] = 0x01; |
| 545 | pkt[idx++] = 0x00; pkt[idx++] = 0x00; pkt[idx++] = 0x00; pkt[idx++] = 0x78; |
| 546 | |
| 547 | /* SRV Target: oavx-src.local */ |
| 548 | static const uint8_t s_target_host[] = { 8, 'o', 'a', 'v', 'x', '-', 's', 'r', 'c', 5, 'l', 'o', 'c', 'a', 'l', 0 }; |
| 549 | uint16_t srv_port = port ? port : (prof->base_port ? prof->base_port : AVX_DEFAULT_PORT); |
| 550 | size_t srv_rd_len = 2 + 2 + 2 + sizeof(s_target_host); |
| 551 | pkt[idx++] = (uint8_t)(srv_rd_len >> 8); |
| 552 | pkt[idx++] = (uint8_t)(srv_rd_len & 0xff); |
| 553 | |
| 554 | pkt[idx++] = 0x00; pkt[idx++] = 0x00; /* Priority 0 */ |
| 555 | pkt[idx++] = 0x00; pkt[idx++] = 0x00; /* Weight 0 */ |
| 556 | pkt[idx++] = (uint8_t)(srv_port >> 8); |
| 557 | pkt[idx++] = (uint8_t)(srv_port & 0xff); |
| 558 | |
| 559 | memcpy(&pkt[idx], s_target_host, sizeof(s_target_host)); |
| 560 | idx += sizeof(s_target_host); |
| 561 | |
| 562 | ssize_t sent = sendto(sock, (const char*)pkt, (int)idx, 0, (struct sockaddr*)&mcast_addr, sizeof(mcast_addr)); |
| 563 | if (sent <= 0) { |
| 564 | /* Fall back to loopback interface if no default multicast route exists (macOS/BSD/CI VMs) */ |
| 565 | struct in_addr lo_if; |
| 566 | lo_if.s_addr = htonl(INADDR_LOOPBACK); |
| 567 | setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&lo_if, sizeof(lo_if)); |
| 568 | sent = sendto(sock, (const char*)pkt, (int)idx, 0, (struct sockaddr*)&mcast_addr, sizeof(mcast_addr)); |
| 569 | } |
| 570 | avx_socket_close(sock); |
| 571 | |
| 572 | if (sent > 0) { |
| 573 | avx_trace_packet("[TX]", "mDNS ANNOUNCE", pkt, idx); |
| 574 | avx_pcap_write_udp(src_ip_net, 5353, htonl(0xE00000FB), 5353, pkt, idx); |
| 575 | return 0; |
| 576 | } |
| 577 | return -1; |
| 578 | } |
| 579 | |
| 580 | int avx_mdns_broadcast_source(const char* name, uint16_t port) { |
| 581 | return avx_mdns_broadcast_source_ex(name, port, NULL); |
| 582 | } |