| 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 |
|
| 47 |
|
| 48 |
|
| 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 |
|
| 72 |
|
| 73 |
|
| 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 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 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 |
|
| 195 |
|
| 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); |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 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 |
|
| 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 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
fprintf(stderr, "\n== destroying rx1; waiting for silence ==\n"); |
| 239 |
rist_destroy(rx1); |
| 240 |
usleep(3000000); |
| 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 |
|
| 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 |
} |