Raw ↗
+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
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_multinic_test.c - Multi-NIC / Network Interface Binding Test 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 <arpa/inet.h>
16  
17 #include "openavx.h"
18 #include "avx_internal.h"
19 #include "avx_config.h"
20 #include "avx_discovery_server.h"
21  
22 #define DS_TEST_PORT 7958
23  
24 int main(void) {
25 printf("\n=======================================================\n");
26 printf(" OpenAVX Multi-NIC & Network Interface Binding Test\n");
27 printf("=======================================================\n\n");
28  
29 /* --------------------------------------------------------------------- */
30 /* 1. Interface Resolution & Auto-IP Discovery Helper Unit Tests */
31 /* --------------------------------------------------------------------- */
32 printf("[*] 1. Testing Interface Name and IP Resolution Functions...\n");
33 char resolved_ip[64] = {0};
34  
35 /* 1.1 Direct IPv4 literal */
36 bool ok = avx_net_resolve_interface_or_ip("127.0.0.1", resolved_ip, sizeof(resolved_ip));
37 assert(ok);
38 assert(strcmp(resolved_ip, "127.0.0.1") == 0);
39 printf(" [+] Literal '127.0.0.1' validated: %s\n", resolved_ip);
40  
41 /* 1.2 Loopback interface name 'lo' */
42 memset(resolved_ip, 0, sizeof(resolved_ip));
43 ok = avx_net_resolve_interface_or_ip("lo", resolved_ip, sizeof(resolved_ip));
44 assert(ok);
45 assert(strcmp(resolved_ip, "127.0.0.1") == 0);
46 printf(" [+] Interface name 'lo' resolved to: %s\n", resolved_ip);
47  
48 /* 1.3 Invalid / non-existent NIC name */
49 memset(resolved_ip, 0, sizeof(resolved_ip));
50 ok = avx_net_resolve_interface_or_ip("nonexistent_nic_999", resolved_ip, sizeof(resolved_ip));
51 assert(!ok);
52 printf(" [+] Non-existent NIC 'nonexistent_nic_999' correctly rejected\n");
53  
54 /* 1.4 Auto-detect default local active IP */
55 memset(resolved_ip, 0, sizeof(resolved_ip));
56 ok = avx_net_get_default_ip(resolved_ip, sizeof(resolved_ip));
57 assert(ok);
58 assert(resolved_ip[0] != '\0');
59 printf(" [+] Default local active IP auto-detected: %s\n", resolved_ip);
60  
61 /* --------------------------------------------------------------------- */
62 /* 2. JSON Configuration File Network Binding Parsing */
63 /* --------------------------------------------------------------------- */
64 printf("\n[*] 2. Testing openavx.json Network Block Parsing...\n");
65 const char *test_json =
66 "{\n"
67 " \"network\": {\n"
68 " \"bind_ip\": \"192.168.10.55\",\n"
69 " \"interface\": \"eth1\"\n"
70 " }\n"
71 "}";
72 avx_config_t cfg;
73 memset(&cfg, 0, sizeof(cfg));
74 bool parse_ok = avx_config_parse_json(test_json, &cfg);
75 assert(parse_ok);
76 assert(strcmp(cfg.bind_ip, "192.168.10.55") == 0);
77 assert(strcmp(cfg.nic_interface, "eth1") == 0);
78 printf(" [+] JSON 'network.bind_ip' parsed: %s\n", cfg.bind_ip);
79 printf(" [+] JSON 'network.interface' parsed: %s\n", cfg.nic_interface);
80  
81 /* --------------------------------------------------------------------- */
82 /* 3. Explicit Binding via API (oavx_stream_config_t::bind_ip) */
83 /* --------------------------------------------------------------------- */
84 printf("\n[*] 3. Testing Explicit Binding via API (.bind_ip = \"127.0.0.1\")...\n");
85 oavx_context_t *ctx = oavx_context_create();
86 assert(ctx != NULL);
87  
88 oavx_stream_config_t send_cfg = {
89 .name = "MULTINIC_API_TX",
90 .direction = OAVX_DIR_TRANSMIT,
91 .clock_sync = false,
92 .bind_ip = "127.0.0.1"
93 };
94 oavx_stream_t *p_send_api = oavx_stream_open(ctx, &send_cfg);
95 assert(p_send_api != NULL);
96  
97 const oavx_endpoint_t *ep_api = oavx_stream_get_endpoint(p_send_api);
98 assert(ep_api != NULL && ep_api->address != NULL);
99 assert(strstr(ep_api->address, "127.0.0.1:") != NULL);
100 printf(" [+] Sender URL matches bound IP: %s\n", ep_api->address);
101  
102 /* Connect receiver */
103 oavx_stream_config_t recv_cfg = {
104 .name = "MULTINIC_API_RX",
105 .direction = OAVX_DIR_RECEIVE,
106 .endpoint = ep_api->address,
107 .clock_sync = false
108 };
109 oavx_stream_t *p_recv_api = oavx_stream_open(ctx, &recv_cfg);
110 assert(p_recv_api != NULL);
111  
112 usleep(100000); /* 100 ms */
113 assert(oavx_stream_get_connection_count(p_send_api, 500) == 1);
114 printf(" [+] Receiver connected successfully over bound interface!\n");
115  
116 oavx_stream_close(p_recv_api);
117 oavx_stream_close(p_send_api);
118  
119 /* --------------------------------------------------------------------- */
120 /* 4. Interface Name Binding via API (.bind_ip = "lo") */
121 /* --------------------------------------------------------------------- */
122 printf("\n[*] 4. Testing Interface Name Binding via API (.bind_ip = \"lo\")...\n");
123 oavx_stream_config_t send_cfg_iface = {
124 .name = "MULTINIC_IFACE_TX",
125 .direction = OAVX_DIR_TRANSMIT,
126 .clock_sync = false,
127 .bind_ip = "lo"
128 };
129 oavx_stream_t *p_send_iface = oavx_stream_open(ctx, &send_cfg_iface);
130 assert(p_send_iface != NULL);
131  
132 const oavx_endpoint_t *ep_iface = oavx_stream_get_endpoint(p_send_iface);
133 assert(ep_iface != NULL && ep_iface->address != NULL);
134 assert(strstr(ep_iface->address, "127.0.0.1:") != NULL);
135 printf(" [+] Interface 'lo' resolved and bound: %s\n", ep_iface->address);
136 oavx_stream_close(p_send_iface);
137  
138 /* --------------------------------------------------------------------- */
139 /* 5. Environment Variable Precedence (OPENAVX_BIND_IP) */
140 /* --------------------------------------------------------------------- */
141 printf("\n[*] 5. Testing Environment Variable Precedence (OPENAVX_BIND_IP)...\n");
142 setenv("OPENAVX_BIND_IP", "127.0.0.1", 1);
143  
144 oavx_stream_config_t send_cfg_env = {
145 .name = "MULTINIC_ENV_TX",
146 .direction = OAVX_DIR_TRANSMIT,
147 .clock_sync = false,
148 .bind_ip = NULL /* Unset in API: should fall back to OPENAVX_BIND_IP */
149 };
150 oavx_stream_t *p_send_env = oavx_stream_open(ctx, &send_cfg_env);
151 assert(p_send_env != NULL);
152  
153 const oavx_endpoint_t *ep_env = oavx_stream_get_endpoint(p_send_env);
154 assert(ep_env != NULL && ep_env->address != NULL);
155 assert(strstr(ep_env->address, "127.0.0.1:") != NULL);
156 printf(" [+] Sender URL inherited OPENAVX_BIND_IP: %s\n", ep_env->address);
157 oavx_stream_close(p_send_env);
158 unsetenv("OPENAVX_BIND_IP");
159  
160 /* --------------------------------------------------------------------- */
161 /* 6. mDNS Outbound Multicast Egress Binding Test */
162 /* --------------------------------------------------------------------- */
163 printf("\n[*] 6. Testing mDNS Outbound Multicast Egress Binding...\n");
164 int mdns_ret = avx_mdns_broadcast_source_ex("MULTINIC_MDNS_CAM", 7965, "127.0.0.1");
165 assert(mdns_ret == 0);
166 printf(" [+] avx_mdns_broadcast_source_ex() on 127.0.0.1 succeeded!\n");
167  
168 /* --------------------------------------------------------------------- */
169 /* 7. Discovery Server Registration with Bound Media IP */
170 /* --------------------------------------------------------------------- */
171 printf("\n[*] 7. Testing Centralized Discovery Server with Bound IP...\n");
172 avx_discovery_server_t *srv = avx_discovery_server_create("127.0.0.1", DS_TEST_PORT);
173 assert(srv != NULL);
174  
175 char ds_url[64];
176 snprintf(ds_url, sizeof(ds_url), "127.0.0.1:%d", DS_TEST_PORT);
177 setenv("OPENAVX_DISCOVERY_SERVER", ds_url, 1);
178 setenv("OPENAVX_BIND_IP", "127.0.0.1", 1);
179  
180 oavx_stream_config_t ds_send_cfg = {
181 .name = "ENTERPRISE_10G_CAM",
182 .groups = "studio_lan",
183 .direction = OAVX_DIR_TRANSMIT,
184 .clock_sync = false,
185 .bind_ip = "127.0.0.1"
186 };
187 oavx_stream_t *p_send_ds = oavx_stream_open(ctx, &ds_send_cfg);
188 assert(p_send_ds != NULL);
189  
190 usleep(100000); /* 100 ms */
191  
192 char names[16][256];
193 char urls[16][256];
194 int count = avx_discovery_client_query("127.0.0.1", DS_TEST_PORT, names, urls, 16);
195 assert(count >= 1);
196  
197 bool found_source = false;
198 for (int i = 0; i < count; ++i) {
199 if (strcmp(names[i], "ENTERPRISE_10G_CAM") == 0) {
200 printf(" [+] Discovery Server registered source: %s at %s\n", names[i], urls[i]);
201 assert(strstr(urls[i], "127.0.0.1:") != NULL);
202 found_source = true;
203 break;
204 }
205 }
206 assert(found_source);
207  
208 oavx_stream_close(p_send_ds);
209 avx_discovery_server_destroy(srv);
210 unsetenv("OPENAVX_DISCOVERY_SERVER");
211 unsetenv("OPENAVX_BIND_IP");
212  
213 oavx_context_destroy(ctx);
214  
215 printf("\n[PASS] openavx_multinic_test\n\n");
216 return 0;
217 }