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 * src/avx_wire.c - Wire protocol framing and profile selection
7 */
8  
9 #include "avx_wire.h"
10 #include "avx_platform.h"
11 #include <string.h>
12  
13 static const oavx_wire_profile_t s_default_wire_profile = {
14 .magic_video = OAVX_WIRE_MAGIC_VIDEO,
15 .magic_audio = OAVX_WIRE_MAGIC_AUDIO,
16 .magic_metadata = OAVX_WIRE_MAGIC_METADATA,
17 .magic_udp = OAVX_WIRE_MAGIC_UDP,
18 .compat_magic_video = 0,
19 .compat_magic_audio = 0,
20 .compat_magic_metadata = 0,
21 .compat_magic_udp = 0,
22 .base_port = OAVX_WIRE_DEFAULT_PORT,
23 .mdns_service = OAVX_WIRE_MDNS_SERVICE,
24 .handshake_tag = OAVX_WIRE_HANDSHAKE_TAG,
25 .tally_tag = OAVX_WIRE_TALLY_TAG,
26 .color_info_tag = OAVX_WIRE_COLOR_INFO_TAG,
27 .ptz_tag = OAVX_WIRE_PTZ_TAG,
28 .record_tag = OAVX_WIRE_RECORD_TAG
29 };
30  
31 static oavx_wire_profile_t s_wire_profiles[2] = {
32 {
33 .magic_video = OAVX_WIRE_MAGIC_VIDEO,
34 .magic_audio = OAVX_WIRE_MAGIC_AUDIO,
35 .magic_metadata = OAVX_WIRE_MAGIC_METADATA,
36 .magic_udp = OAVX_WIRE_MAGIC_UDP,
37 .compat_magic_video = 0,
38 .compat_magic_audio = 0,
39 .compat_magic_metadata = 0,
40 .compat_magic_udp = 0,
41 .base_port = OAVX_WIRE_DEFAULT_PORT,
42 .mdns_service = OAVX_WIRE_MDNS_SERVICE,
43 .handshake_tag = OAVX_WIRE_HANDSHAKE_TAG,
44 .tally_tag = OAVX_WIRE_TALLY_TAG,
45 .color_info_tag = OAVX_WIRE_COLOR_INFO_TAG,
46 .ptz_tag = OAVX_WIRE_PTZ_TAG,
47 .record_tag = OAVX_WIRE_RECORD_TAG
48 },
49 {
50 .magic_video = OAVX_WIRE_MAGIC_VIDEO,
51 .magic_audio = OAVX_WIRE_MAGIC_AUDIO,
52 .magic_metadata = OAVX_WIRE_MAGIC_METADATA,
53 .magic_udp = OAVX_WIRE_MAGIC_UDP,
54 .compat_magic_video = 0,
55 .compat_magic_audio = 0,
56 .compat_magic_metadata = 0,
57 .compat_magic_udp = 0,
58 .base_port = OAVX_WIRE_DEFAULT_PORT,
59 .mdns_service = OAVX_WIRE_MDNS_SERVICE,
60 .handshake_tag = OAVX_WIRE_HANDSHAKE_TAG,
61 .tally_tag = OAVX_WIRE_TALLY_TAG,
62 .color_info_tag = OAVX_WIRE_COLOR_INFO_TAG,
63 .ptz_tag = OAVX_WIRE_PTZ_TAG,
64 .record_tag = OAVX_WIRE_RECORD_TAG
65 }
66 };
67 static volatile int s_active_profile_idx = 0;
68 static avx_mutex_t s_wire_profile_lock = AVX_MUTEX_INITIALIZER;
69  
70 OPENAVX_API const oavx_wire_profile_t* avx_wire_get_default_profile(void) {
71 return &s_default_wire_profile;
72 }
73  
74 OPENAVX_API const oavx_wire_profile_t* avx_wire_get_active_profile(void) {
75 int idx = s_active_profile_idx;
76 return &s_wire_profiles[idx];
77 }
78  
79 /* Performs atomic double-buffered swap of wire protocol profile using full memory barrier. */
80 OPENAVX_API void avx_wire_set_active_profile(const oavx_wire_profile_t* profile) {
81 avx_mutex_lock(&s_wire_profile_lock);
82 int next_idx = 1 - s_active_profile_idx;
83 if (profile) {
84 s_wire_profiles[next_idx] = *profile;
85 if (!s_wire_profiles[next_idx].magic_video) s_wire_profiles[next_idx].magic_video = OAVX_WIRE_MAGIC_VIDEO;
86 if (!s_wire_profiles[next_idx].magic_audio) s_wire_profiles[next_idx].magic_audio = OAVX_WIRE_MAGIC_AUDIO;
87 if (!s_wire_profiles[next_idx].magic_metadata) s_wire_profiles[next_idx].magic_metadata = OAVX_WIRE_MAGIC_METADATA;
88 if (!s_wire_profiles[next_idx].base_port) s_wire_profiles[next_idx].base_port = OAVX_WIRE_DEFAULT_PORT;
89 if (!s_wire_profiles[next_idx].mdns_service) s_wire_profiles[next_idx].mdns_service = OAVX_WIRE_MDNS_SERVICE;
90 if (!s_wire_profiles[next_idx].handshake_tag) s_wire_profiles[next_idx].handshake_tag = OAVX_WIRE_HANDSHAKE_TAG;
91 if (!s_wire_profiles[next_idx].tally_tag) s_wire_profiles[next_idx].tally_tag = OAVX_WIRE_TALLY_TAG;
92 if (!s_wire_profiles[next_idx].color_info_tag) s_wire_profiles[next_idx].color_info_tag = OAVX_WIRE_COLOR_INFO_TAG;
93 if (!s_wire_profiles[next_idx].ptz_tag) s_wire_profiles[next_idx].ptz_tag = OAVX_WIRE_PTZ_TAG;
94 if (!s_wire_profiles[next_idx].record_tag) s_wire_profiles[next_idx].record_tag = OAVX_WIRE_RECORD_TAG;
95 } else {
96 s_wire_profiles[next_idx] = s_default_wire_profile;
97 }
98 __sync_synchronize();
99 s_active_profile_idx = next_idx;
100 avx_mutex_unlock(&s_wire_profile_lock);
101 }
102  
103 /* Encodes dotted DNS service name into RFC 1035 length-prefixed label sequence with trailing zero. */
104 OPENAVX_API size_t avx_wire_encode_dns_service(const char* service_str, uint8_t* out_buf, size_t max_len) {
105 if (!service_str || !out_buf || max_len == 0) return 0;
106  
107 size_t out_idx = 0;
108 const char* start = service_str;
109  
110 while (*start != '\0') {
111 const char* dot = strchr(start, '.');
112 size_t len = dot ? (size_t)(dot - start) : strlen(start);
113  
114 if (len > 63 || out_idx + 1 + len >= max_len) return 0;
115  
116 out_buf[out_idx++] = (uint8_t)len;
117 memcpy(&out_buf[out_idx], start, len);
118 out_idx += len;
119  
120 if (!dot) break;
121 start = dot + 1;
122 }
123  
124 if (out_idx < max_len) {
125 out_buf[out_idx++] = 0; /* Null terminator label */
126 } else {
127 return 0;
128 }
129  
130 return out_idx;
131 }
132  
133 /* Scans packet payload for target DNS service label sequence without trailing null byte. */
134 OPENAVX_API bool avx_wire_match_service(const uint8_t* pkt, size_t pkt_len, const char* service_str, size_t* out_offset) {
135 if (!pkt || !service_str || pkt_len < 12) return false;
136  
137 uint8_t target_labels[128];
138 size_t target_len = avx_wire_encode_dns_service(service_str, target_labels, sizeof(target_labels));
139 if (target_len <= 1) return false;
140  
141 /* Don't include the trailing zero in substring match for flexible label matching */
142 size_t match_len = target_len - 1;
143  
144 for (size_t i = 12; i + match_len < pkt_len; ++i) {
145 if (memcmp(&pkt[i], target_labels, match_len) == 0) {
146 if (out_offset) *out_offset = i;
147 return true;
148 }
149 }
150 return false;
151 }