| 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 |
#include "librist/librist.h" |
| 40 |
#include "librist/librist_srp.h" |
| 41 |
#include "rist-private.h" |
| 42 |
#include <inttypes.h> |
| 43 |
#include <stdbool.h> |
| 44 |
#include <stdio.h> |
| 45 |
#include <stdlib.h> |
| 46 |
#include <string.h> |
| 47 |
|
| 48 |
#ifdef _WIN32 |
| 49 |
#include <windows.h> |
| 50 |
#else |
| 51 |
#include <unistd.h> |
| 52 |
#endif |
| 53 |
|
| 54 |
#define LISTEN_PORT 19956 |
| 55 |
#define LEG_A_PORT 20081 |
| 56 |
#define LEG_B_PORT 20082 |
| 57 |
#define LEG_APRIME_PORT 20083 |
| 58 |
#define CNAME "bonded-leg" |
| 59 |
#define SRP_USER "leguser" |
| 60 |
#define SRP_PASS "legpass-3344" |
| 61 |
|
| 62 |
static struct rist_logging_settings *log_settings = NULL; |
| 63 |
|
| 64 |
static pthread_mutex_t tracker_lock; |
| 65 |
#define MAX_TRACKED_PEERS 16 |
| 66 |
static struct { |
| 67 |
struct rist_peer *seen[MAX_TRACKED_PEERS]; |
| 68 |
int count; |
| 69 |
} tracker; |
| 70 |
|
| 71 |
static volatile int rx_pkts = 0; |
| 72 |
static volatile bool sender_run = true; |
| 73 |
|
| 74 |
static int log_cb(void *arg, enum rist_log_level level, const char *msg) { |
| 75 |
(void)arg; |
| 76 |
const char *tag = level <= RIST_LOG_ERROR ? "E" : |
| 77 |
level <= RIST_LOG_WARN ? "W" : |
| 78 |
level <= RIST_LOG_INFO ? "I" : "D"; |
| 79 |
fprintf(stderr, "%s| %s", tag, msg); |
| 80 |
return 0; |
| 81 |
} |
| 82 |
|
| 83 |
static void sender_status_cb(void *arg, struct rist_peer *peer, |
| 84 |
enum rist_connection_status status) { |
| 85 |
(void)arg; |
| 86 |
if (status == RIST_CONNECTION_ESTABLISHED || |
| 87 |
status == RIST_CLIENT_CONNECTED) { |
| 88 |
pthread_mutex_lock(&tracker_lock); |
| 89 |
bool seen = false; |
| 90 |
for (int i = 0; i < tracker.count; i++) |
| 91 |
if (tracker.seen[i] == peer) { seen = true; break; } |
| 92 |
if (!seen && tracker.count < MAX_TRACKED_PEERS) { |
| 93 |
tracker.seen[tracker.count++] = peer; |
| 94 |
fprintf(stderr, ">> sender CONNECTED (peer=%p, distinct_callers=%d)\n", |
| 95 |
(void *)peer, tracker.count); |
| 96 |
} |
| 97 |
pthread_mutex_unlock(&tracker_lock); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
static int rx_data_cb(void *arg, struct rist_data_block *b) { |
| 102 |
(void)arg; |
| 103 |
rx_pkts++; |
| 104 |
rist_receiver_data_block_free2(&b); |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
static PTHREAD_START_FUNC(sender_feed, arg) { |
| 109 |
struct rist_ctx *tx = arg; |
| 110 |
uint32_t counter = 0; |
| 111 |
while (sender_run) { |
| 112 |
struct rist_data_block blk; |
| 113 |
memset(&blk, 0, sizeof(blk)); |
| 114 |
blk.payload = &counter; |
| 115 |
blk.payload_len = sizeof(counter); |
| 116 |
blk.virt_src_port = 1968; |
| 117 |
rist_sender_data_write(tx, &blk); |
| 118 |
counter++; |
| 119 |
usleep(20000); |
| 120 |
} |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
static struct rist_peer *add_srp_leg(struct rist_ctx *rx, int local_port) { |
| 125 |
char url[320]; |
| 126 |
snprintf(url, sizeof(url), |
| 127 |
"rist://127.0.0.1:%d?local-port=%d&cname=%s&weight=5" |
| 128 |
"&session-timeout=30000&keepalive-interval=1000", |
| 129 |
LISTEN_PORT, local_port, CNAME); |
| 130 |
struct rist_peer_config *pcfg = NULL; |
| 131 |
if (rist_parse_address2(url, (void *)&pcfg) != 0) |
| 132 |
return NULL; |
| 133 |
struct rist_peer *peer = NULL; |
| 134 |
if (rist_peer_create(rx, &peer, pcfg) != 0) { free(pcfg); return NULL; } |
| 135 |
free(pcfg); |
| 136 |
if (rist_enable_eap_srp_2(peer, SRP_USER, SRP_PASS, NULL, NULL) != 0) |
| 137 |
return NULL; |
| 138 |
return peer; |
| 139 |
} |
| 140 |
|
| 141 |
int main(void) { |
| 142 |
char url[320]; |
| 143 |
|
| 144 |
if (rist_logging_set(&log_settings, RIST_LOG_INFO, log_cb, NULL, NULL, stderr) != 0) |
| 145 |
return 99; |
| 146 |
memset(&tracker, 0, sizeof(tracker)); |
| 147 |
pthread_mutex_init(&tracker_lock, NULL); |
| 148 |
|
| 149 |
|
| 150 |
struct rist_ctx *tx = NULL; |
| 151 |
if (rist_sender_create(&tx, RIST_PROFILE_MAIN, 0, log_settings) != 0) return 99; |
| 152 |
rist_connection_status_callback_set(tx, sender_status_cb, NULL); |
| 153 |
snprintf(url, sizeof(url), |
| 154 |
"rist://@127.0.0.1:%d?session-timeout=30000&keepalive-interval=1000", |
| 155 |
LISTEN_PORT); |
| 156 |
struct rist_peer_config *tx_pcfg = NULL; |
| 157 |
if (rist_parse_address2(url, (void *)&tx_pcfg) != 0) { rist_destroy(tx); return 99; } |
| 158 |
struct rist_peer *tx_peer = NULL; |
| 159 |
if (rist_peer_create(tx, &tx_peer, tx_pcfg) != 0) { free(tx_pcfg); rist_destroy(tx); return 99; } |
| 160 |
free(tx_pcfg); |
| 161 |
if (rist_enable_eap_srp_2(tx_peer, SRP_USER, SRP_PASS, NULL, NULL) != 0) { |
| 162 |
rist_destroy(tx); return 99; |
| 163 |
} |
| 164 |
if (rist_start(tx) != 0) { rist_destroy(tx); return 99; } |
| 165 |
|
| 166 |
pthread_t feeder; |
| 167 |
pthread_create(&feeder, NULL, sender_feed, tx); |
| 168 |
usleep(200000); |
| 169 |
|
| 170 |
|
| 171 |
fprintf(stderr, "\n== receiver brings up bonded legs A and B (cname=%s) ==\n", CNAME); |
| 172 |
struct rist_ctx *rx = NULL; |
| 173 |
if (rist_receiver_create(&rx, RIST_PROFILE_MAIN, log_settings) != 0) { |
| 174 |
sender_run = false; pthread_join(feeder, NULL); rist_destroy(tx); return 99; |
| 175 |
} |
| 176 |
struct rist_peer *leg_a = add_srp_leg(rx, LEG_A_PORT); |
| 177 |
struct rist_peer *leg_b = add_srp_leg(rx, LEG_B_PORT); |
| 178 |
if (!leg_a || !leg_b) { |
| 179 |
sender_run = false; pthread_join(feeder, NULL); |
| 180 |
rist_destroy(rx); rist_destroy(tx); return 99; |
| 181 |
} |
| 182 |
if (rist_receiver_data_callback_set2(rx, rx_data_cb, NULL) != 0 || |
| 183 |
rist_start(rx) != 0) { |
| 184 |
sender_run = false; pthread_join(feeder, NULL); |
| 185 |
rist_destroy(rx); rist_destroy(tx); return 99; |
| 186 |
} |
| 187 |
|
| 188 |
usleep(7000000); |
| 189 |
|
| 190 |
pthread_mutex_lock(&tracker_lock); |
| 191 |
int distinct_setup = tracker.count; |
| 192 |
pthread_mutex_unlock(&tracker_lock); |
| 193 |
int rx_before = rx_pkts; |
| 194 |
fprintf(stderr, "after setup: distinct_callers=%d, rx_pkts=%d\n", |
| 195 |
distinct_setup, rx_before); |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
fprintf(stderr, "\n== leg A tuple drops (NAT rebind); leg B keeps streaming ==\n"); |
| 200 |
rist_peer_destroy(rx, leg_a); |
| 201 |
usleep(3000000); |
| 202 |
int rx_mid = rx_pkts; |
| 203 |
fprintf(stderr, "while leg A down: rx_pkts=%d (was %d)\n", rx_mid, rx_before); |
| 204 |
|
| 205 |
|
| 206 |
fprintf(stderr, "\n== leg A' reappears on a new local port (same cname/creds) ==\n"); |
| 207 |
struct rist_peer *leg_aprime = add_srp_leg(rx, LEG_APRIME_PORT); |
| 208 |
if (!leg_aprime) { |
| 209 |
sender_run = false; pthread_join(feeder, NULL); |
| 210 |
rist_destroy(rx); rist_destroy(tx); return 99; |
| 211 |
} |
| 212 |
usleep(7000000); |
| 213 |
|
| 214 |
pthread_mutex_lock(&tracker_lock); |
| 215 |
int distinct_rebind = tracker.count; |
| 216 |
pthread_mutex_unlock(&tracker_lock); |
| 217 |
int rx_after = rx_pkts; |
| 218 |
uint32_t reb_b = leg_b->rebind_attempts; |
| 219 |
int dead_b = leg_b->dead; |
| 220 |
fprintf(stderr, "\n== after A' rejoin: distinct_callers=%d, rx_pkts=%d, " |
| 221 |
"leg_b rebind=%"PRIu32" dead=%d ==\n", |
| 222 |
distinct_rebind, rx_after, reb_b, dead_b); |
| 223 |
|
| 224 |
sender_run = false; |
| 225 |
pthread_join(feeder, NULL); |
| 226 |
rist_destroy(rx); |
| 227 |
rist_destroy(tx); |
| 228 |
free(log_settings); |
| 229 |
|
| 230 |
if (distinct_setup < 2 || rx_before == 0) { |
| 231 |
fprintf(stderr, "INDETERMINATE: both legs never established or no data " |
| 232 |
"(distinct_callers=%d, rx_pkts=%d).\n", distinct_setup, rx_before); |
| 233 |
return 2; |
| 234 |
} |
| 235 |
if (rx_mid <= rx_before) { |
| 236 |
fprintf(stderr, "FAIL: stream stalled while leg A was down; bonding did " |
| 237 |
"not keep flowing on leg B (rx %d -> %d).\n", rx_before, rx_mid); |
| 238 |
return 1; |
| 239 |
} |
| 240 |
if (rx_after <= rx_mid) { |
| 241 |
fprintf(stderr, "FAIL: stream stalled after leg A' rejoined " |
| 242 |
"(rx %d -> %d).\n", rx_mid, rx_after); |
| 243 |
return 1; |
| 244 |
} |
| 245 |
if (dead_b) { |
| 246 |
fprintf(stderr, "FAIL: surviving leg B died during the other leg's " |
| 247 |
"rebind.\n"); |
| 248 |
return 1; |
| 249 |
} |
| 250 |
if (reb_b != 0) { |
| 251 |
fprintf(stderr, "FAIL: surviving leg B rebound its own socket " |
| 252 |
"(rebind_attempts=%"PRIu32"); it should have been " |
| 253 |
"undisturbed.\n", reb_b); |
| 254 |
return 1; |
| 255 |
} |
| 256 |
if (distinct_rebind != 3) { |
| 257 |
fprintf(stderr, "FAIL: expected leg A' to rejoin as a separate caller " |
| 258 |
"(3 distinct), got %d; the alive-duplicate guard did not " |
| 259 |
"behave as expected while leg B was alive.\n", distinct_rebind); |
| 260 |
return 1; |
| 261 |
} |
| 262 |
|
| 263 |
fprintf(stderr, "\nPASS: one bonded SRP leg rebound mid-stream while the " |
| 264 |
"other kept delivering (rx %d->%d->%d, no stall); surviving " |
| 265 |
"leg B was undisturbed (not dead, no rebind) and the rebound " |
| 266 |
"leg rejoined as a separate caller (distinct_callers=3, no " |
| 267 |
"wrong merge while a same-cname leg was alive).\n", |
| 268 |
rx_before, rx_mid, rx_after); |
| 269 |
return 0; |
| 270 |
} |