| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 74 |
|
| 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 |
|
| 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); |
| 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 |
|
| 158 |
|
| 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 |
|
| 176 |
|
| 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 |
|
| 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 |
|
| 201 |
|
| 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 |
|
| 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 |
|
| 245 |
usleep(handshake_wait); |
| 246 |
pthread_mutex_lock(&cb_lock); |
| 247 |
bool first = cb_state.first_seen; |
| 248 |
pthread_mutex_unlock(&cb_lock); |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 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 |
|
| 263 |
send_burst(tx1, 0, 25); |
| 264 |
usleep(1000000); |
| 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 |
|
| 274 |
fprintf(stderr, "== destroying first sender, waiting for silence " |
| 275 |
"(> session_timeout) ==\n"); |
| 276 |
rist_destroy(tx1); |
| 277 |
usleep(4000000); |
| 278 |
|
| 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 |
|
| 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 |
|
| 321 |
usleep(reconnect_wait); |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
unsigned data_before = rx_data_snapshot(); |
| 327 |
send_burst(tx2, 1000, 50); |
| 328 |
usleep(1200000); |
| 329 |
unsigned data_after = rx_data_snapshot(); |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 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 |
|
| 345 |
|
| 346 |
|
| 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 |
|
| 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 |
} |