| 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 |
#include "librist/librist.h" |
| 38 |
#include "rist-private.h" |
| 39 |
#include <inttypes.h> |
| 40 |
#include <stdbool.h> |
| 41 |
#include <stdio.h> |
| 42 |
#include <stdlib.h> |
| 43 |
#include <string.h> |
| 44 |
|
| 45 |
#ifdef _WIN32 |
| 46 |
#include <windows.h> |
| 47 |
#else |
| 48 |
#include <unistd.h> |
| 49 |
#endif |
| 50 |
|
| 51 |
static struct rist_logging_settings *log_settings = NULL; |
| 52 |
|
| 53 |
static struct { |
| 54 |
bool first_seen; |
| 55 |
bool second_seen; |
| 56 |
struct rist_peer *second_peer; |
| 57 |
} cb_state; |
| 58 |
static pthread_mutex_t cb_lock; |
| 59 |
|
| 60 |
static int log_cb(void *arg, enum rist_log_level level, const char *msg) { |
| 61 |
(void)arg; |
| 62 |
const char *tag = level <= RIST_LOG_ERROR ? "E" : |
| 63 |
level <= RIST_LOG_WARN ? "W" : |
| 64 |
level <= RIST_LOG_INFO ? "I" : "D"; |
| 65 |
fprintf(stderr, "%s| %s", tag, msg); |
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
static void sender_status_cb(void *arg, struct rist_peer *peer, |
| 70 |
enum rist_connection_status status) |
| 71 |
{ |
| 72 |
bool *which = (bool *)arg; |
| 73 |
if (status == RIST_CONNECTION_ESTABLISHED || |
| 74 |
status == RIST_CLIENT_CONNECTED) { |
| 75 |
pthread_mutex_lock(&cb_lock); |
| 76 |
*which = true; |
| 77 |
if (which == &cb_state.second_seen) |
| 78 |
cb_state.second_peer = peer; |
| 79 |
pthread_mutex_unlock(&cb_lock); |
| 80 |
fprintf(stderr, ">> %s sender CONNECTED (peer=%p)\n", |
| 81 |
which == &cb_state.first_seen ? "first" : "second", |
| 82 |
(void *)peer); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
static struct rist_ctx *spawn_sender(int port, void *cb_arg, const char *crypto) { |
| 87 |
struct rist_ctx *tx = NULL; |
| 88 |
if (rist_sender_create(&tx, RIST_PROFILE_MAIN, 0, log_settings) != 0) |
| 89 |
return NULL; |
| 90 |
if (rist_connection_status_callback_set(tx, sender_status_cb, cb_arg) != 0) { |
| 91 |
rist_destroy(tx); |
| 92 |
return NULL; |
| 93 |
} |
| 94 |
char url[256]; |
| 95 |
snprintf(url, sizeof(url), |
| 96 |
"rist://@127.0.0.1:%d?session-timeout=2000&keepalive-interval=500%s", |
| 97 |
port, crypto); |
| 98 |
struct rist_peer_config *pcfg = NULL; |
| 99 |
if (rist_parse_address2(url, (void *)&pcfg) != 0) { |
| 100 |
rist_destroy(tx); |
| 101 |
return NULL; |
| 102 |
} |
| 103 |
struct rist_peer *peer = NULL; |
| 104 |
if (rist_peer_create(tx, &peer, pcfg) != 0) { |
| 105 |
free(pcfg); |
| 106 |
rist_destroy(tx); |
| 107 |
return NULL; |
| 108 |
} |
| 109 |
free(pcfg); |
| 110 |
if (rist_start(tx) != 0) { |
| 111 |
rist_destroy(tx); |
| 112 |
return NULL; |
| 113 |
} |
| 114 |
return tx; |
| 115 |
} |
| 116 |
|
| 117 |
int main(int argc, char *argv[]) { |
| 118 |
bool use_psk = (argc > 1 && strcmp(argv[1], "psk") == 0); |
| 119 |
const char *crypto_suffix = use_psk ? "&secret=testkey1234&aes-type=128" : ""; |
| 120 |
const int listen_port = use_psk ? 22001 : 22000; |
| 121 |
memset(&cb_state, 0, sizeof(cb_state)); |
| 122 |
pthread_mutex_init(&cb_lock, NULL); |
| 123 |
|
| 124 |
if (rist_logging_set(&log_settings, RIST_LOG_INFO, log_cb, |
| 125 |
NULL, NULL, stderr) != 0) { |
| 126 |
fprintf(stderr, "logging setup failed\n"); |
| 127 |
return 99; |
| 128 |
} |
| 129 |
|
| 130 |
fprintf(stderr, "== mode: %s ==\n", use_psk ? "PSK encrypted" : "plaintext"); |
| 131 |
|
| 132 |
|
| 133 |
fprintf(stderr, "== spawning first sender on :%d ==\n", listen_port); |
| 134 |
struct rist_ctx *tx1 = spawn_sender(listen_port, &cb_state.first_seen, crypto_suffix); |
| 135 |
if (!tx1) { |
| 136 |
fprintf(stderr, "sender create failed\n"); |
| 137 |
return 99; |
| 138 |
} |
| 139 |
usleep(200000); |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
fprintf(stderr, "== spawning receiver caller ==\n"); |
| 144 |
struct rist_ctx *rx = NULL; |
| 145 |
if (rist_receiver_create(&rx, RIST_PROFILE_MAIN, log_settings) != 0) { |
| 146 |
fprintf(stderr, "rx ctx create failed\n"); |
| 147 |
rist_destroy(tx1); |
| 148 |
return 99; |
| 149 |
} |
| 150 |
char rx_url[256]; |
| 151 |
snprintf(rx_url, sizeof(rx_url), |
| 152 |
"rist://127.0.0.1:%d?session-timeout=2000&keepalive-interval=500" |
| 153 |
"&cname=fix2-test%s", |
| 154 |
listen_port, crypto_suffix); |
| 155 |
struct rist_peer_config *rx_pcfg = NULL; |
| 156 |
if (rist_parse_address2(rx_url, (void *)&rx_pcfg) != 0) { |
| 157 |
fprintf(stderr, "rx url parse failed\n"); |
| 158 |
rist_destroy(rx); rist_destroy(tx1); |
| 159 |
return 99; |
| 160 |
} |
| 161 |
struct rist_peer *rx_peer = NULL; |
| 162 |
if (rist_peer_create(rx, &rx_peer, rx_pcfg) != 0) { |
| 163 |
fprintf(stderr, "rx peer create failed\n"); |
| 164 |
free(rx_pcfg); rist_destroy(rx); rist_destroy(tx1); |
| 165 |
return 99; |
| 166 |
} |
| 167 |
free(rx_pcfg); |
| 168 |
if (rist_start(rx) != 0) { |
| 169 |
fprintf(stderr, "rx start failed\n"); |
| 170 |
rist_destroy(rx); rist_destroy(tx1); |
| 171 |
return 99; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
usleep(2000000); |
| 176 |
pthread_mutex_lock(&cb_lock); |
| 177 |
bool first = cb_state.first_seen; |
| 178 |
pthread_mutex_unlock(&cb_lock); |
| 179 |
if (!first) { |
| 180 |
fprintf(stderr, "INDETERMINATE: first sender never saw a " |
| 181 |
"handshake; test setup is broken.\n"); |
| 182 |
rist_destroy(rx); rist_destroy(tx1); |
| 183 |
free(log_settings); |
| 184 |
return 2; |
| 185 |
} |
| 186 |
|
| 187 |
int original_sd = rx_peer->sd; |
| 188 |
uint16_t original_port = rx_peer->local_port; |
| 189 |
fprintf(stderr, "== handshake complete: receiver peer sd=%d " |
| 190 |
"local_port=%u ==\n", |
| 191 |
original_sd, (unsigned)original_port); |
| 192 |
|
| 193 |
|
| 194 |
fprintf(stderr, "== destroying first sender, waiting for silence " |
| 195 |
"(> session_timeout) ==\n"); |
| 196 |
rist_destroy(tx1); |
| 197 |
usleep(4000000); |
| 198 |
|
| 199 |
|
| 200 |
bool dead_now = rx_peer->dead; |
| 201 |
uint32_t rebinds = rx_peer->rebind_attempts; |
| 202 |
int sd_now = rx_peer->sd; |
| 203 |
uint16_t port_now = rx_peer->local_port; |
| 204 |
fprintf(stderr, "== after silence window: rebind_attempts=%"PRIu32 |
| 205 |
", dead=%d, sd=%d (was %d), local_port=%u (was %u) ==\n", |
| 206 |
rebinds, dead_now, sd_now, original_sd, |
| 207 |
(unsigned)port_now, (unsigned)original_port); |
| 208 |
|
| 209 |
if (rebinds == 0) { |
| 210 |
fprintf(stderr, "FAIL: rebind never fired; rebind_attempts is 0.\n"); |
| 211 |
rist_destroy(rx); |
| 212 |
free(log_settings); |
| 213 |
return 1; |
| 214 |
} |
| 215 |
if (dead_now) { |
| 216 |
fprintf(stderr, "FAIL: receiver peer was killed; rebind should " |
| 217 |
"have kept it alive across the silence.\n"); |
| 218 |
rist_destroy(rx); |
| 219 |
free(log_settings); |
| 220 |
return 1; |
| 221 |
} |
| 222 |
if (port_now == original_port) { |
| 223 |
fprintf(stderr, "FAIL: local port did not change; rebind did " |
| 224 |
"not actually re-bind the socket.\n"); |
| 225 |
rist_destroy(rx); |
| 226 |
free(log_settings); |
| 227 |
return 1; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
fprintf(stderr, "== bringing a new sender up on :%d ==\n", listen_port); |
| 232 |
struct rist_ctx *tx2 = spawn_sender(listen_port, &cb_state.second_seen, crypto_suffix); |
| 233 |
if (!tx2) { |
| 234 |
fprintf(stderr, "second sender create failed\n"); |
| 235 |
rist_destroy(rx); |
| 236 |
free(log_settings); |
| 237 |
return 99; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
usleep(4000000); |
| 242 |
|
| 243 |
pthread_mutex_lock(&cb_lock); |
| 244 |
bool second = cb_state.second_seen; |
| 245 |
pthread_mutex_unlock(&cb_lock); |
| 246 |
|
| 247 |
rist_destroy(rx); |
| 248 |
rist_destroy(tx2); |
| 249 |
free(log_settings); |
| 250 |
|
| 251 |
if (!second) { |
| 252 |
fprintf(stderr, "FAIL: rebind happened (port %u->%u, %"PRIu32 |
| 253 |
" attempts) but the new sender never saw the " |
| 254 |
"reconnected caller; handshake never completed.\n", |
| 255 |
(unsigned)original_port, (unsigned)port_now, rebinds); |
| 256 |
return 1; |
| 257 |
} |
| 258 |
|
| 259 |
fprintf(stderr, "PASS: receiver auto-rebound from local_port %u to %u " |
| 260 |
"after %"PRIu32" attempt(s) and the new sender saw the " |
| 261 |
"reconnected caller without operator intervention.\n", |
| 262 |
(unsigned)original_port, (unsigned)port_now, rebinds); |
| 263 |
return 0; |
| 264 |
} |