Debian Codegraph @

test/rist/test_multipath_cname.c

raw symbols SCIP
1 /* librist. Copyright © 2026 SipRadius LLC.
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Multipath safety check for the cname re-association / caller-side
5 * rebind work (issue #188 and its SRP-only hardening).
6 *
7 * A multipath caller-receiver legitimately presents the SAME cname on
8 * every path. The listener-side re-association must therefore NOT
9 * collapse two concurrently-alive paths that share a cname into one
10 * peer record, and the caller-side socket rebind must NOT fire on a
11 * path that is healthy.
12 *
13 * Topology (all on 127.0.0.1, MAIN profile):
14 * sender(listener, streams data)
15 * <-- receiver path A (caller, cname=X, local-port A, weight 5)
16 * <-- receiver path B (caller, cname=X, local-port B, weight 5)
17 *
18 * Both paths belong to ONE receiver ctx and share one cname. They
19 * arrive at the sender as two source tuples under the same listening
20 * parent, i.e. two duplicate-cname siblings.
21 *
22 * Two crypto modes:
23 * psk - both paths use the same shared PSK (listener-side
24 * re-association is SRP-gated, so it never fires here)
25 * srp - both paths authenticate with the SAME username/password
26 * (listener-side re-association CAN fire for SRP, so the
27 * alive-duplicate guard is what must keep the paths apart)
28 *
29 * Usage:
30 * test_multipath_cname [srp]
31 *
32 * Pass conditions:
33 * 1. The sender sees TWO distinct callers and keeps seeing two
34 * across steady state (no spurious cname merge).
35 * 2. The receiver gets the stream.
36 * 3. Neither receiver path rebinds its socket while healthy
37 * (rebind_attempts == 0) and neither is dead.
38 *
39 * Exit codes:
40 * 0 - both paths stayed distinct, data flowed, no spurious rebind
41 * 1 - paths merged, a healthy path rebound, or a path died
42 * 2 - INDETERMINATE: both paths never established / no data
43 * 99 - setup error
44 */
45
46 #include "librist/librist.h"
47 #include "librist/librist_srp.h"
48 #include "rist-private.h"
49 #include <inttypes.h>
50 #include <stdbool.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54
55 #ifdef _WIN32
56 #include <windows.h>
57 #else
58 #include <unistd.h>
59 #endif
60
61 #define CNAME "bonded-1"
62 #define PSK "sharedgroupkey9090"
63 #define SRP_USER "bondeduser"
64 #define SRP_PASS "bondedpass-5566"
65
66 static struct rist_logging_settings *log_settings = NULL;
67
68 static pthread_mutex_t tracker_lock;
69 #define MAX_TRACKED_PEERS 16
70 static struct {
71 struct rist_peer *seen[MAX_TRACKED_PEERS];
72 int count;
73 } tracker;
74
75 static volatile int rx_pkts = 0;
76 static volatile bool sender_run = true;
77
78 static int log_cb(void *arg, enum rist_log_level level, const char *msg) {
79 (void)arg;
80 const char *tag = level <= RIST_LOG_ERROR ? "E" :
81 level <= RIST_LOG_WARN ? "W" :
82 level <= RIST_LOG_INFO ? "I" : "D";
83 fprintf(stderr, "%s| %s", tag, msg);
84 return 0;
85 }
86
87 static void sender_status_cb(void *arg, struct rist_peer *peer,
88 enum rist_connection_status status) {
89 (void)arg;
90 if (status == RIST_CONNECTION_ESTABLISHED ||
91 status == RIST_CLIENT_CONNECTED) {
92 pthread_mutex_lock(&tracker_lock);
93 bool seen = false;
94 for (int i = 0; i < tracker.count; i++)
95 if (tracker.seen[i] == peer) { seen = true; break; }
96 if (!seen && tracker.count < MAX_TRACKED_PEERS) {
97 tracker.seen[tracker.count++] = peer;
98 fprintf(stderr, ">> sender CONNECTED (peer=%p, distinct_callers=%d)\n",
99 (void *)peer, tracker.count);
100 }
101 pthread_mutex_unlock(&tracker_lock);
102 }
103 }
104
105 static int rx_data_cb(void *arg, struct rist_data_block *b) {
106 (void)arg;
107 rx_pkts++;
108 rist_receiver_data_block_free2(&b);
109 return 0;
110 }
111
112 static PTHREAD_START_FUNC(sender_feed, arg) {
113 struct rist_ctx *tx = arg;
114 uint32_t counter = 0;
115 while (sender_run) {
116 struct rist_data_block blk;
117 memset(&blk, 0, sizeof(blk));
118 blk.payload = &counter;
119 blk.payload_len = sizeof(counter);
120 blk.virt_src_port = 1968;
121 rist_sender_data_write(tx, &blk);
122 counter++;
123 usleep(20000); /* ~50 pkt/s */
124 }
125 return 0;
126 }
127
128 static struct rist_peer *add_path(struct rist_ctx *rx, int listen_port,
129 int local_port, const char *crypto, bool srp) {
130 char url[320];
131 snprintf(url, sizeof(url),
132 "rist://127.0.0.1:%d?local-port=%d&cname=%s&weight=5"
133 "&session-timeout=30000&keepalive-interval=1000%s",
134 listen_port, local_port, CNAME, crypto);
135 struct rist_peer_config *pcfg = NULL;
136 if (rist_parse_address2(url, (void *)&pcfg) != 0)
137 return NULL;
138 struct rist_peer *peer = NULL;
139 if (rist_peer_create(rx, &peer, pcfg) != 0) { free(pcfg); return NULL; }
140 free(pcfg);
141 if (srp && rist_enable_eap_srp_2(peer, SRP_USER, SRP_PASS, NULL, NULL) != 0)
142 return NULL;
143 return peer;
144 }
145
146 int main(int argc, char *argv[]) {
147 bool use_srp = (argc > 1 && strcmp(argv[1], "srp") == 0);
148 int listen_port = use_srp ? 19958 : 19960;
149 int path_a_port = use_srp ? 20071 : 20061;
150 int path_b_port = use_srp ? 20072 : 20062;
151 char crypto[96];
152 snprintf(crypto, sizeof(crypto), use_srp ? "" : "&secret=%s&aes-type=128", PSK);
153 char url[320];
154
155 if (rist_logging_set(&log_settings, RIST_LOG_INFO, log_cb, NULL, NULL, stderr) != 0)
156 return 99;
157 memset(&tracker, 0, sizeof(tracker));
158 pthread_mutex_init(&tracker_lock, NULL);
159 fprintf(stderr, "== mode: %s ==\n", use_srp ? "SRP (same user/pass on both paths)"
160 : "shared PSK");
161
162 /* sender: listener, MAIN, streams data. */
163 struct rist_ctx *tx = NULL;
164 if (rist_sender_create(&tx, RIST_PROFILE_MAIN, 0, log_settings) != 0) return 99;
165 rist_connection_status_callback_set(tx, sender_status_cb, NULL);
166 snprintf(url, sizeof(url),
167 "rist://@127.0.0.1:%d?session-timeout=30000&keepalive-interval=1000%s",
168 listen_port, crypto);
169 struct rist_peer_config *tx_pcfg = NULL;
170 if (rist_parse_address2(url, (void *)&tx_pcfg) != 0) { rist_destroy(tx); return 99; }
171 struct rist_peer *tx_peer = NULL;
172 if (rist_peer_create(tx, &tx_peer, tx_pcfg) != 0) { free(tx_pcfg); rist_destroy(tx); return 99; }
173 free(tx_pcfg);
174 if (use_srp && rist_enable_eap_srp_2(tx_peer, SRP_USER, SRP_PASS, NULL, NULL) != 0) {
175 rist_destroy(tx); return 99;
176 }
177 if (rist_start(tx) != 0) { rist_destroy(tx); return 99; }
178
179 pthread_t feeder;
180 pthread_create(&feeder, NULL, sender_feed, tx);
181 usleep(200000);
182
183 /* receiver: ONE ctx, TWO caller paths, same cname. */
184 fprintf(stderr, "\n== receiver brings up two bonded paths (cname=%s) ==\n", CNAME);
185 struct rist_ctx *rx = NULL;
186 if (rist_receiver_create(&rx, RIST_PROFILE_MAIN, log_settings) != 0) {
187 sender_run = false; pthread_join(feeder, NULL); rist_destroy(tx); return 99;
188 }
189 struct rist_peer *path_a = add_path(rx, listen_port, path_a_port, crypto, use_srp);
190 struct rist_peer *path_b = add_path(rx, listen_port, path_b_port, crypto, use_srp);
191 if (!path_a || !path_b) {
192 sender_run = false; pthread_join(feeder, NULL);
193 rist_destroy(rx); rist_destroy(tx); return 99;
194 }
195 if (rist_receiver_data_callback_set2(rx, rx_data_cb, NULL) != 0 ||
196 rist_start(rx) != 0) {
197 sender_run = false; pthread_join(feeder, NULL);
198 rist_destroy(rx); rist_destroy(tx); return 99;
199 }
200
201 usleep(use_srp ? 7000000 : 4000000); /* both handshakes complete (SRP is slower) */
202
203 pthread_mutex_lock(&tracker_lock);
204 int after_setup = tracker.count;
205 pthread_mutex_unlock(&tracker_lock);
206 int data_after_setup = rx_pkts;
207 fprintf(stderr, "after setup: distinct_callers=%d, rx_pkts=%d\n",
208 after_setup, data_after_setup);
209
210 /* steady state: confirm nothing merges or rebinds later. */
211 usleep(4000000);
212
213 pthread_mutex_lock(&tracker_lock);
214 int after_steady = tracker.count;
215 pthread_mutex_unlock(&tracker_lock);
216 uint32_t reb_a = path_a->rebind_attempts;
217 uint32_t reb_b = path_b->rebind_attempts;
218 int dead_a = path_a->dead;
219 int dead_b = path_b->dead;
220 fprintf(stderr, "\n== steady state: distinct_callers=%d, rx_pkts=%d, "
221 "rebind_a=%"PRIu32" rebind_b=%"PRIu32" dead_a=%d dead_b=%d ==\n",
222 after_steady, rx_pkts, reb_a, reb_b, dead_a, dead_b);
223
224 sender_run = false;
225 pthread_join(feeder, NULL);
226 rist_destroy(rx);
227 rist_destroy(tx);
228 free(log_settings);
229
230 if (after_setup < 2 || data_after_setup == 0) {
231 fprintf(stderr, "INDETERMINATE: both paths never established or no "
232 "data flowed (distinct_callers=%d, rx_pkts=%d).\n",
233 after_setup, data_after_setup);
234 return 2;
235 }
236
237 if (after_steady != 2) {
238 fprintf(stderr, "FAIL: expected 2 distinct callers, got %d; the two "
239 "same-cname paths were wrongly merged.\n", after_steady);
240 return 1;
241 }
242 if (reb_a != 0 || reb_b != 0) {
243 fprintf(stderr, "FAIL: a healthy path rebound its socket "
244 "(rebind_a=%"PRIu32", rebind_b=%"PRIu32").\n",
245 reb_a, reb_b);
246 return 1;
247 }
248 if (dead_a || dead_b) {
249 fprintf(stderr, "FAIL: a path died (dead_a=%d, dead_b=%d).\n",
250 dead_a, dead_b);
251 return 1;
252 }
253
254 fprintf(stderr, "\nPASS: two bonded %s paths sharing cname \"%s\" stayed "
255 "distinct (distinct_callers=2), the stream flowed "
256 "(rx_pkts=%d), and neither healthy path rebound.\n",
257 use_srp ? "SRP" : "PSK", CNAME, rx_pkts);
258 return 0;
259 }