Debian Codegraph @

test/rist/test_caller_socket_rebind.c

raw symbols SCIP
1 /* librist. Copyright © 2026 SipRadius LLC.
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Coverage for the receiver-caller socket rebind introduced for
5 * upstream issue #188 (silent recovery from sender silence on
6 * plaintext, shared-PSK and EAP-SRP callers).
7 *
8 * The srp mode is the regression for the EAP-SRP no-rebind bug: a
9 * NAT'd/single-port SRP caller whose listener restarts (losing all
10 * session state) must reset its EAP context and re-authenticate on the
11 * fresh socket by itself. Before the fix, SRP callers bailed out of the
12 * rebind and were killed, staying idle forever until a manual restart.
13 *
14 * Usage:
15 * test_caller_socket_rebind [psk|srp]
16 *
17 * Topology (single hop, in-process):
18 * sender(listener, 127.0.0.1:PORT) <-- receiver(caller, plain|psk|srp)
19 *
20 * Sequence:
21 * 1. Spin up sender + receiver, wait for handshake.
22 * 2. Push a data burst and confirm the receiver decodes it.
23 * 3. Snapshot the receiver peer's local socket fd and local port.
24 * 4. Destroy the sender. Receiver now sees silence.
25 * 5. Wait > session_timeout.
26 * 6. Re-create a sender on the same listen port.
27 * 7. Wait a couple more session_timeouts for the rebind +
28 * handshake to fire.
29 * 8. Push another data burst through the recovered path.
30 * 9. PASS when the receiver peer is still alive, its local port
31 * has changed (rebind happened), rebind_attempts > 0, the new
32 * sender's listener saw the reconnected caller, AND fresh data
33 * flowed end to end after recovery. Also checks that the rebind
34 * backoff reset once the peer re-authenticated.
35 *
36 * Without the rebind, the receiver peer is killed and never
37 * recovers without operator intervention.
38 *
39 * Exit codes:
40 * 0 - rebind fired, new sender saw the caller, and data flowed again
41 * 1 - receiver was killed, never rebound, or no data post-recovery
42 * 2 - test setup never reached an initial handshake / data path
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 SRP_USER "rebinduser"
62 #define SRP_PASS "rebindpass-7421"
63
64 static struct rist_logging_settings *log_settings = NULL;
65
66 static struct {
67 bool first_seen;
68 bool second_seen;
69 struct rist_peer *second_peer;
70 } cb_state;
71 static pthread_mutex_t cb_lock;
72
73 /* Count of data blocks the receiver actually decoded/delivered. Proves the
74 * post-reconnect path carries real payload, not just a connection event. */
75 static unsigned rx_data_count;
76
77 static int rx_data_cb(void *arg, struct rist_data_block *b) {
78 (void)arg;
79 pthread_mutex_lock(&cb_lock);
80 rx_data_count++;
81 pthread_mutex_unlock(&cb_lock);
82 rist_receiver_data_block_free2(&b);
83 return 0;
84 }
85
86 static unsigned rx_data_snapshot(void) {
87 pthread_mutex_lock(&cb_lock);
88 unsigned v = rx_data_count;
89 pthread_mutex_unlock(&cb_lock);
90 return v;
91 }
92
93 /* Send a short burst of RTP-ish payload from the sender. */
94 static void send_burst(struct rist_ctx *tx, uint32_t start, int count) {
95 for (int i = 0; i < count; i++) {
96 uint32_t v = start + (uint32_t)i;
97 struct rist_data_block blk;
98 memset(&blk, 0, sizeof(blk));
99 blk.payload = &v;
100 blk.payload_len = sizeof(v);
101 blk.virt_src_port = 1968;
102 rist_sender_data_write(tx, &blk);
103 usleep(20000); /* ~50 pkt/s */
104 }
105 }
106
107 static int log_cb(void *arg, enum rist_log_level level, const char *msg) {
108 (void)arg;
109 const char *tag = level <= RIST_LOG_ERROR ? "E" :
110 level <= RIST_LOG_WARN ? "W" :
111 level <= RIST_LOG_INFO ? "I" : "D";
112 fprintf(stderr, "%s| %s", tag, msg);
113 return 0;
114 }
115
116 static void sender_status_cb(void *arg, struct rist_peer *peer,
117 enum rist_connection_status status)
118 {
119 bool *which = (bool *)arg;
120 if (status == RIST_CONNECTION_ESTABLISHED ||
121 status == RIST_CLIENT_CONNECTED) {
122 pthread_mutex_lock(&cb_lock);
123 *which = true;
124 if (which == &cb_state.second_seen)
125 cb_state.second_peer = peer;
126 pthread_mutex_unlock(&cb_lock);
127 fprintf(stderr, ">> %s sender CONNECTED (peer=%p)\n",
128 which == &cb_state.first_seen ? "first" : "second",
129 (void *)peer);
130 }
131 }
132
133 static struct rist_ctx *spawn_sender(int port, void *cb_arg, const char *crypto, bool srp) {
134 struct rist_ctx *tx = NULL;
135 if (rist_sender_create(&tx, RIST_PROFILE_MAIN, 0, log_settings) != 0)
136 return NULL;
137 if (rist_connection_status_callback_set(tx, sender_status_cb, cb_arg) != 0) {
138 rist_destroy(tx);
139 return NULL;
140 }
141 char url[256];
142 snprintf(url, sizeof(url),
143 "rist://@127.0.0.1:%d?session-timeout=2000&keepalive-interval=500%s",
144 port, crypto);
145 struct rist_peer_config *pcfg = NULL;
146 if (rist_parse_address2(url, (void *)&pcfg) != 0) {
147 rist_destroy(tx);
148 return NULL;
149 }
150 struct rist_peer *peer = NULL;
151 if (rist_peer_create(tx, &peer, pcfg) != 0) {
152 free(pcfg);
153 rist_destroy(tx);
154 return NULL;
155 }
156 free(pcfg);
157 /* Listener side is the SRP authenticator: it supplies the
158 * username/password and creates the verifier internally. */
159 if (srp && rist_enable_eap_srp_2(peer, SRP_USER, SRP_PASS, NULL, NULL) != 0) {
160 rist_destroy(tx);
161 return NULL;
162 }
163 if (rist_start(tx) != 0) {
164 rist_destroy(tx);
165 return NULL;
166 }
167 return tx;
168 }
169
170 int main(int argc, char *argv[]) {
171 bool use_psk = (argc > 1 && strcmp(argv[1], "psk") == 0);
172 bool use_srp = (argc > 1 && strcmp(argv[1], "srp") == 0);
173 const char *crypto_suffix = use_psk ? "&secret=testkey1234&aes-type=128" : "";
174 const int listen_port = use_psk ? 22001 : use_srp ? 22002 : 22000;
175 /* SRP needs a full re-handshake after the restart, so give the
176 * handshake and reconnect windows more slack than plaintext/PSK. */
177 const unsigned handshake_wait = use_srp ? 5000000 : 2000000;
178 const unsigned reconnect_wait = use_srp ? 6000000 : 4000000;
179 memset(&cb_state, 0, sizeof(cb_state));
180 pthread_mutex_init(&cb_lock, NULL);
181
182 if (rist_logging_set(&log_settings, RIST_LOG_INFO, log_cb,
183 NULL, NULL, stderr) != 0) {
184 fprintf(stderr, "logging setup failed\n");
185 return 99;
186 }
187
188 fprintf(stderr, "== mode: %s ==\n",
189 use_psk ? "PSK encrypted" : use_srp ? "SRP authenticated" : "plaintext");
190
191 /* spawn the first sender */
192 fprintf(stderr, "== spawning first sender on :%d ==\n", listen_port);
193 struct rist_ctx *tx1 = spawn_sender(listen_port, &cb_state.first_seen, crypto_suffix, use_srp);
194 if (!tx1) {
195 fprintf(stderr, "sender create failed\n");
196 return 99;
197 }
198 usleep(200000);
199
200 /* spawn the receiver caller, no local-port pin, short
201 * session_timeout so the test runs fast. */
202 fprintf(stderr, "== spawning receiver caller ==\n");
203 struct rist_ctx *rx = NULL;
204 if (rist_receiver_create(&rx, RIST_PROFILE_MAIN, log_settings) != 0) {
205 fprintf(stderr, "rx ctx create failed\n");
206 rist_destroy(tx1);
207 return 99;
208 }
209 char rx_url[256];
210 snprintf(rx_url, sizeof(rx_url),
211 "rist://127.0.0.1:%d?session-timeout=2000&keepalive-interval=500"
212 "&buffer=200&cname=fix2-test%s",
213 listen_port, crypto_suffix);
214 struct rist_peer_config *rx_pcfg = NULL;
215 if (rist_parse_address2(rx_url, (void *)&rx_pcfg) != 0) {
216 fprintf(stderr, "rx url parse failed\n");
217 rist_destroy(rx); rist_destroy(tx1);
218 return 99;
219 }
220 struct rist_peer *rx_peer = NULL;
221 if (rist_peer_create(rx, &rx_peer, rx_pcfg) != 0) {
222 fprintf(stderr, "rx peer create failed\n");
223 free(rx_pcfg); rist_destroy(rx); rist_destroy(tx1);
224 return 99;
225 }
226 free(rx_pcfg);
227 /* Caller side is the SRP authenticatee. */
228 if (use_srp && rist_enable_eap_srp_2(rx_peer, SRP_USER, SRP_PASS, NULL, NULL) != 0) {
229 fprintf(stderr, "rx SRP enable failed\n");
230 rist_destroy(rx); rist_destroy(tx1);
231 return 99;
232 }
233 if (rist_receiver_data_callback_set2(rx, rx_data_cb, NULL) != 0) {
234 fprintf(stderr, "rx data callback set failed\n");
235 rist_destroy(rx); rist_destroy(tx1);
236 return 99;
237 }
238 if (rist_start(rx) != 0) {
239 fprintf(stderr, "rx start failed\n");
240 rist_destroy(rx); rist_destroy(tx1);
241 return 99;
242 }
243
244 /* wait for handshake */
245 usleep(handshake_wait);
246 pthread_mutex_lock(&cb_lock);
247 bool first = cb_state.first_seen;
248 pthread_mutex_unlock(&cb_lock);
249 /* The sender-side connection event is best-effort (in SRP mode the EAP
250 * layer authenticates the peer before the RTCP SDES that drives the
251 * status callback, so it may never fire). The authoritative "setup
252 * reached a working session" signal is real data below. */
253 fprintf(stderr, "== sender connection callback seen: %s ==\n",
254 first ? "yes" : "no (relying on data-flow check)");
255
256 int original_sd = rx_peer->sd;
257 uint16_t original_port = rx_peer->local_port;
258 fprintf(stderr, "== handshake complete: receiver peer sd=%d "
259 "local_port=%u ==\n",
260 original_sd, (unsigned)original_port);
261
262 /* Confirm the pre-outage data path actually carries payload. */
263 send_burst(tx1, 0, 25);
264 usleep(1000000); /* let the receiver buffer (200ms) drain to the cb */
265 if (rx_data_snapshot() == 0) {
266 fprintf(stderr, "INDETERMINATE: no data received before the outage; "
267 "test setup is broken.\n");
268 rist_destroy(rx); rist_destroy(tx1);
269 free(log_settings);
270 return 2;
271 }
272
273 /* destroy the sender: receiver now sees silence */
274 fprintf(stderr, "== destroying first sender, waiting for silence "
275 "(> session_timeout) ==\n");
276 rist_destroy(tx1);
277 usleep(4000000); /* > 2 * session_timeout to make sure the
278 * receiver-side timeout check ran. */
279
280 bool dead_now = rx_peer->dead;
281 uint32_t rebinds = rx_peer->rebind_attempts;
282 int sd_now = rx_peer->sd;
283 uint16_t port_now = rx_peer->local_port;
284 fprintf(stderr, "== after silence window: rebind_attempts=%"PRIu32
285 ", dead=%d, sd=%d (was %d), local_port=%u (was %u) ==\n",
286 rebinds, dead_now, sd_now, original_sd,
287 (unsigned)port_now, (unsigned)original_port);
288
289 if (rebinds == 0) {
290 fprintf(stderr, "FAIL: rebind never fired; rebind_attempts is 0.\n");
291 rist_destroy(rx);
292 free(log_settings);
293 return 1;
294 }
295 if (dead_now) {
296 fprintf(stderr, "FAIL: receiver peer was killed; rebind should "
297 "have kept it alive across the silence.\n");
298 rist_destroy(rx);
299 free(log_settings);
300 return 1;
301 }
302 if (port_now == original_port) {
303 fprintf(stderr, "FAIL: local port did not change; rebind did "
304 "not actually re-bind the socket.\n");
305 rist_destroy(rx);
306 free(log_settings);
307 return 1;
308 }
309
310 /* bring the sender back up; expect the rebound caller to find it */
311 fprintf(stderr, "== bringing a new sender up on :%d ==\n", listen_port);
312 struct rist_ctx *tx2 = spawn_sender(listen_port, &cb_state.second_seen, crypto_suffix, use_srp);
313 if (!tx2) {
314 fprintf(stderr, "second sender create failed\n");
315 rist_destroy(rx);
316 free(log_settings);
317 return 99;
318 }
319
320 /* give the next keepalive + (re-)handshake time to land */
321 usleep(reconnect_wait);
322
323 /* Drive real payload through the re-established path and confirm the
324 * receiver decodes it: a connection event alone does not prove the
325 * (re-)negotiated keys / flow actually work end to end. */
326 unsigned data_before = rx_data_snapshot();
327 send_burst(tx2, 1000, 50);
328 usleep(1200000); /* > buffer so post-recovery payload reaches the cb */
329 unsigned data_after = rx_data_snapshot();
330
331 /* Backoff must reset once the peer re-authenticates (see
332 * rist_peer_authenticate), so a later outage retries promptly instead
333 * of waiting out an ever-growing gap. */
334 uint32_t rebinds_after = rx_peer->rebind_attempts;
335
336 pthread_mutex_lock(&cb_lock);
337 bool second = cb_state.second_seen;
338 pthread_mutex_unlock(&cb_lock);
339
340 rist_destroy(rx);
341 rist_destroy(tx2);
342 free(log_settings);
343
344 /* Authoritative regression signal: real payload crossed the recovered
345 * link. This proves the socket rebind AND the EAP re-handshake AND the
346 * re-negotiated keys all work end to end. */
347 if (data_after <= data_before) {
348 fprintf(stderr, "FAIL: caller reconnected but no data flowed after "
349 "recovery (before=%u after=%u); keys/flow did not "
350 "recover end to end.\n", data_before, data_after);
351 return 1;
352 }
353
354 /* Best-effort corroboration; not authoritative (see note above). */
355 if (!second)
356 fprintf(stderr, "note: sender-side reconnect callback did not fire, "
357 "but data flow confirms recovery.\n");
358
359 if (rebinds_after != 0) {
360 fprintf(stderr, "WARN: rebind_attempts did not reset after "
361 "reconnect (=%"PRIu32"); backoff will keep growing "
362 "across outages.\n", rebinds_after);
363 }
364
365 fprintf(stderr, "PASS: receiver auto-rebound from local_port %u to %u "
366 "after %"PRIu32" attempt(s), the new sender saw the "
367 "reconnected caller, and %u data blocks flowed after "
368 "recovery (backoff reset -> %"PRIu32") without operator "
369 "intervention.\n",
370 (unsigned)original_port, (unsigned)port_now, rebinds,
371 data_after - data_before, rebinds_after);
372 return 0;
373 }