Debian Codegraph @

test/rist/test_nat_port_rebind.c

raw symbols SCIP
1 /* librist. Copyright © 2026 SipRadius LLC.
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Coverage for the listener-side cname re-association introduced
5 * for upstream issue #188 (NAT source-port rebind on a calling
6 * receiver), and for the security hardening that restricts it to
7 * authenticated SRP sessions only.
8 *
9 * Topology:
10 * sender(listener, 127.0.0.1:LISTEN_PORT) <-- receiver(caller, src=PORT_A) round 1
11 * <-- receiver(caller, src=PORT_B) round 2
12 *
13 * Both receivers use the same cname so they look like the same
14 * logical caller to the sender. The expected distinct-caller count
15 * on the sender depends on the crypto mode:
16 *
17 * plaintext -> 2 distinct callers (gate refuses to migrate; the
18 * plaintext recovery path is the
19 * receiver-side socket rebind,
20 * covered by
21 * test_caller_socket_rebind.c)
22 * psk -> 2 distinct callers (gate refuses to migrate; under
23 * a shared PSK the cname is not a
24 * per-peer secret, so a forged
25 * cname must NOT be allowed to
26 * take over an existing peer
27 * record -- see the security
28 * reproducer test_psk_cname_hijack.c)
29 * srp -> 1 distinct caller (migration fires; the caller is
30 * authenticated with a per-peer
31 * SRP session, so absorbing the
32 * new source tuple into the
33 * existing peer record is safe)
34 *
35 * The test passes when the observed count matches the expected count
36 * for the chosen mode. A mismatch indicates either a regression in
37 * the SRP-only gate (plaintext/psk silently migrated -> 1 instead of
38 * 2) or in the migration logic itself (srp failed to migrate -> 2
39 * instead of 1).
40 *
41 * Usage:
42 * test_nat_port_rebind [psk|srp]
43 *
44 * Exit codes:
45 * 0 - observed count matched expected count for the chosen mode
46 * 1 - count mismatch (regression in gate or in migration logic)
47 * 2 - round 1 handshake never completed (test setup broken)
48 * 99 - test setup error (logging, ctx create, etc.)
49 */
50
51 #include "librist/librist.h"
52 #include "librist/librist_srp.h"
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <stdbool.h>
57 #include "pthread-shim.h"
58
59 #ifdef _WIN32
60 #include <windows.h>
61 #define usleep(x) Sleep((x)/1000)
62 #else
63 #include <unistd.h>
64 #endif
65
66 #define SRP_USER "rebinduser"
67 #define SRP_PASS "rebindpass-7421"
68
69 static struct rist_logging_settings *log_settings = NULL;
70
71 /* Each distinct rist_peer pointer on the listener side represents
72 * one logical caller arrival (the callback gates on
73 * send_first_connection_event so duplicates per peer do not fire). */
74 #define MAX_TRACKED_PEERS 16
75 static struct {
76 struct rist_peer *seen[MAX_TRACKED_PEERS];
77 int count;
78 } tracker;
79 static pthread_mutex_t tracker_lock;
80
81 static int log_cb(void *arg, enum rist_log_level level, const char *msg) {
82 (void)arg;
83 const char *tag = level <= RIST_LOG_ERROR ? "E" :
84 level <= RIST_LOG_WARN ? "W" :
85 level <= RIST_LOG_INFO ? "I" : "D";
86 fprintf(stderr, "%s| %s", tag, msg);
87 return 0;
88 }
89
90 static void sender_status_cb(void *arg, struct rist_peer *peer,
91 enum rist_connection_status status)
92 {
93 (void)arg;
94 if (status == RIST_CONNECTION_ESTABLISHED ||
95 status == RIST_CLIENT_CONNECTED) {
96 pthread_mutex_lock(&tracker_lock);
97 bool seen = false;
98 for (int i = 0; i < tracker.count; i++) {
99 if (tracker.seen[i] == peer) { seen = true; break; }
100 }
101 if (!seen && tracker.count < MAX_TRACKED_PEERS) {
102 tracker.seen[tracker.count++] = peer;
103 fprintf(stderr, ">> sender CONNECTED (peer=%p, "
104 "distinct_callers=%d)\n",
105 (void *)peer, tracker.count);
106 }
107 pthread_mutex_unlock(&tracker_lock);
108 } else if (status == RIST_CONNECTION_TIMED_OUT ||
109 status == RIST_CLIENT_TIMED_OUT) {
110 fprintf(stderr, ">> sender TIMED_OUT (peer=%p)\n", (void *)peer);
111 }
112 }
113
114 static struct rist_ctx *start_receiver_with_local_port(const char *url, bool srp) {
115 struct rist_ctx *rx = NULL;
116 if (rist_receiver_create(&rx, RIST_PROFILE_MAIN, log_settings) != 0)
117 return NULL;
118 struct rist_peer_config *pcfg = NULL;
119 if (rist_parse_address2(url, (void *)&pcfg) != 0) {
120 rist_destroy(rx);
121 return NULL;
122 }
123 struct rist_peer *peer = NULL;
124 if (rist_peer_create(rx, &peer, pcfg) != 0) {
125 free(pcfg);
126 rist_destroy(rx);
127 return NULL;
128 }
129 free(pcfg);
130 if (srp && rist_enable_eap_srp_2(peer, SRP_USER, SRP_PASS, NULL, NULL) != 0) {
131 rist_destroy(rx);
132 return NULL;
133 }
134 if (rist_start(rx) != 0) {
135 rist_destroy(rx);
136 return NULL;
137 }
138 return rx;
139 }
140
141 int main(int argc, char *argv[]) {
142 bool use_psk = (argc > 1 && strcmp(argv[1], "psk") == 0);
143 bool use_srp = (argc > 1 && strcmp(argv[1], "srp") == 0);
144 const char *crypto_suffix = use_psk ? "&secret=testkey1234&aes-type=128" : "";
145 const char *mode_name = use_psk ? "PSK encrypted"
146 : use_srp ? "SRP authenticated"
147 : "plaintext";
148 int listen_port = use_psk ? 19998 : use_srp ? 19997 : 20998;
149 char tx_url[256];
150 char rx_url[256];
151 snprintf(tx_url, sizeof(tx_url),
152 "rist://@127.0.0.1:%d?session-timeout=30000&keepalive-interval=1000%s",
153 listen_port, crypto_suffix);
154
155 memset(&tracker, 0, sizeof(tracker));
156 pthread_mutex_init(&tracker_lock, NULL);
157
158 if (rist_logging_set(&log_settings, RIST_LOG_INFO, log_cb,
159 NULL, NULL, stderr) != 0) {
160 fprintf(stderr, "logging setup failed\n");
161 return 99;
162 }
163
164 /* sender, listener mode, MAIN profile.
165 * session-timeout is set well above the inter-round gap so the
166 * round-1 peer record is still in the listener's child list when
167 * the round-2 packets arrive but has been silent long enough to
168 * cross the cname-reassociation safeguard window. */
169 struct rist_ctx *tx = NULL;
170 if (rist_sender_create(&tx, RIST_PROFILE_MAIN, 0, log_settings) != 0) {
171 fprintf(stderr, "sender ctx create failed\n");
172 return 99;
173 }
174 if (rist_connection_status_callback_set(tx, sender_status_cb, NULL) != 0) {
175 fprintf(stderr, "status cb set failed\n");
176 rist_destroy(tx);
177 return 99;
178 }
179 fprintf(stderr, "== mode: %s, listen port %d ==\n", mode_name, listen_port);
180 struct rist_peer_config *tx_pcfg = NULL;
181 if (rist_parse_address2(tx_url, (void *)&tx_pcfg) != 0) {
182 fprintf(stderr, "sender url parse failed\n");
183 rist_destroy(tx);
184 return 99;
185 }
186 struct rist_peer *tx_peer = NULL;
187 if (rist_peer_create(tx, &tx_peer, tx_pcfg) != 0) {
188 fprintf(stderr, "sender peer create failed\n");
189 free(tx_pcfg);
190 rist_destroy(tx);
191 return 99;
192 }
193 free(tx_pcfg);
194 /* listener single-user SRP authenticator: knows the same
195 * username/password and creates the verifier internally. */
196 if (use_srp && rist_enable_eap_srp_2(tx_peer, SRP_USER, SRP_PASS, NULL, NULL) != 0) {
197 fprintf(stderr, "sender SRP enable failed\n");
198 rist_destroy(tx);
199 return 99;
200 }
201 if (rist_start(tx) != 0) {
202 fprintf(stderr, "sender start failed\n");
203 rist_destroy(tx);
204 return 99;
205 }
206 usleep(200000); /* let listener bind */
207
208 /* round 1: receiver caller, source port 20001, cname "rebind-test".
209 * session-timeout matches the sender so the receiver-side
210 * silence-recovery path does not fire during this test. */
211 fprintf(stderr, "\n== round 1: receiver from local-port=20001 ==\n");
212 snprintf(rx_url, sizeof(rx_url),
213 "rist://127.0.0.1:%d?local-port=20001&cname=rebind-test"
214 "&session-timeout=30000&keepalive-interval=1000%s",
215 listen_port, crypto_suffix);
216 struct rist_ctx *rx1 = start_receiver_with_local_port(rx_url, use_srp);
217 if (!rx1) {
218 fprintf(stderr, "rx1 setup failed\n");
219 rist_destroy(tx);
220 return 99;
221 }
222
223 /* let handshake complete (SRP needs an extra round trip) */
224 usleep(use_srp ? 5000000 : 3000000);
225
226 pthread_mutex_lock(&tracker_lock);
227 int after_round1 = tracker.count;
228 pthread_mutex_unlock(&tracker_lock);
229 fprintf(stderr, "after round 1: distinct callers on sender = %d\n",
230 after_round1);
231
232 /* tear down round-1 receiver, wait long enough that its peer
233 * record on the sender side has been silent past the
234 * cname-reassociation safeguard window (2 keepalive intervals),
235 * then start round-2 with a different source port and the same
236 * cname. The sibling is still well within session_timeout, so
237 * it is still in the listener's child list when round 2 arrives. */
238 fprintf(stderr, "\n== destroying rx1; waiting for silence ==\n");
239 rist_destroy(rx1);
240 usleep(3000000); /* > 2 * keepalive_interval, < session_timeout */
241
242 fprintf(stderr, "\n== round 2: receiver from local-port=20002, "
243 "same cname ==\n");
244 snprintf(rx_url, sizeof(rx_url),
245 "rist://127.0.0.1:%d?local-port=20002&cname=rebind-test"
246 "&session-timeout=30000&keepalive-interval=1000%s",
247 listen_port, crypto_suffix);
248 struct rist_ctx *rx2 = start_receiver_with_local_port(rx_url, use_srp);
249 if (!rx2) {
250 fprintf(stderr, "rx2 setup failed\n");
251 rist_destroy(tx);
252 return 99;
253 }
254
255 /* let round-2 handshake complete */
256 usleep(use_srp ? 5000000 : 3000000);
257
258 pthread_mutex_lock(&tracker_lock);
259 int after_round2 = tracker.count;
260 pthread_mutex_unlock(&tracker_lock);
261 fprintf(stderr, "\n== after round 2: distinct callers on sender = %d ==\n",
262 after_round2);
263
264 rist_destroy(rx2);
265 rist_destroy(tx);
266 free(log_settings);
267
268 if (after_round1 == 0) {
269 fprintf(stderr, "INDETERMINATE: round 1 handshake never completed.\n");
270 return 2;
271 }
272
273 int expected = use_srp ? 1 : 2;
274 if (after_round2 == expected) {
275 if (use_srp)
276 fprintf(stderr, "PASS: authenticated-SRP migration absorbed "
277 "the new tuple into the existing peer record "
278 "(distinct_callers=%d as expected).\n",
279 after_round2);
280 else
281 fprintf(stderr, "PASS: SRP-only gate refused migration in %s "
282 "mode (distinct_callers=%d as expected; "
283 "receiver-side recovery is covered "
284 "separately).\n", mode_name, after_round2);
285 return 0;
286 }
287 if (use_srp)
288 fprintf(stderr, "FAIL: authenticated-SRP migration did not fire; "
289 "distinct_callers=%d, expected 1.\n",
290 after_round2);
291 else
292 fprintf(stderr, "FAIL: SRP-only gate did NOT refuse migration in "
293 "%s mode; distinct_callers=%d, expected 2.\n",
294 mode_name, after_round2);
295 return 1;
296 }