+ +
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 | * test/openavx_mdns_discovery_test.c - Passive LAN mDNS Listener Verification Suite |
| 7 | */ |
| 8 | |
| 9 | #define _GNU_SOURCE |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <assert.h> |
| 14 | #include <unistd.h> |
| 15 | #include <time.h> |
| 16 | |
| 17 | #include "openavx.h" |
| 18 | |
| 19 | /* Internal prototype for broadcasting mDNS packet */ |
| 20 | extern int avx_mdns_broadcast_source(const char* name, uint16_t port); |
| 21 | |
| 22 | int main(void) { |
| 23 | printf("\n=== OpenAVX Passive mDNS Discovery Tests ===\n\n"); |
| 24 | |
| 25 | /* 1. Initialize openavx */ |
| 26 | printf("[*] 1. Initializing OpenAVX Context...\n"); |
| 27 | oavx_context_t *ctx = oavx_context_create(); |
| 28 | assert(ctx != NULL); |
| 29 | |
| 30 | /* 2. Create Finder */ |
| 31 | printf("[*] 2. Spawning Discovery Grid...\n"); |
| 32 | oavx_discovery_t *p_find = oavx_discovery_create(ctx, NULL); |
| 33 | assert(p_find != NULL); |
| 34 | |
| 35 | /* Initially, no sources should be present */ |
| 36 | oavx_endpoint_t sources[32]; |
| 37 | size_t count = oavx_discovery_get_sources(p_find, sources, 32); |
| 38 | printf(" [+] Initial sources count: %zu\n", count); |
| 39 | |
| 40 | /* 3. Simulate an external LAN camera broadcasting mDNS announcement */ |
| 41 | printf("[*] 3. Broadcasting external mDNS announcement: \"STUDIO_CAMERA_1\" on port 7965...\n"); |
| 42 | int b_res = avx_mdns_broadcast_source("STUDIO_CAMERA_1", 7965); |
| 43 | assert(b_res == 0); |
| 44 | |
| 45 | /* Also broadcast a second camera */ |
| 46 | printf("[*] 4. Broadcasting second external mDNS announcement: \"ROBOTIC_PTZ_1\" on port 7968...\n"); |
| 47 | b_res = avx_mdns_broadcast_source("ROBOTIC_PTZ_1", 7968); |
| 48 | assert(b_res == 0); |
| 49 | |
| 50 | /* 5. Wait for sources via mDNS listener */ |
| 51 | printf("[*] 5. Waiting for mDNS listener to capture multicast responses...\n"); |
| 52 | bool found = oavx_discovery_wait(p_find, 1500); |
| 53 | assert(found); |
| 54 | |
| 55 | count = oavx_discovery_get_sources(p_find, sources, 32); |
| 56 | assert(count >= 2); |
| 57 | |
| 58 | printf(" [+] Discovered %zu mDNS source(s) over LAN multicast:\n", count); |
| 59 | bool found_cam1 = false; |
| 60 | bool found_ptz1 = false; |
| 61 | |
| 62 | for (size_t i = 0; i < count; ++i) { |
| 63 | printf(" [%zu] \"%s\" @ %s\n", i, sources[i].name, sources[i].address); |
| 64 | if (strcmp(sources[i].name, "STUDIO_CAMERA_1") == 0) { |
| 65 | found_cam1 = true; |
| 66 | assert(strstr(sources[i].address, ":7965") != NULL); |
| 67 | } |
| 68 | if (strcmp(sources[i].name, "ROBOTIC_PTZ_1") == 0) { |
| 69 | found_ptz1 = true; |
| 70 | assert(strstr(sources[i].address, ":7968") != NULL); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | assert(found_cam1); |
| 75 | assert(found_ptz1); |
| 76 | printf(" [+] Successfully verified PTR name and SRV port resolution!\n"); |
| 77 | |
| 78 | /* 6. Destroy Finder */ |
| 79 | printf("[*] 6. Tearing down discovery grid...\n"); |
| 80 | oavx_discovery_destroy(p_find); |
| 81 | oavx_context_destroy(ctx); |
| 82 | |
| 83 | printf("\n[PASS] openavx_mdns_discovery_test\n\n"); |
| 84 | return 0; |
| 85 | } |