| 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 CNAME "bonded-1" |
| 62 |
#define PSK "sharedgroupkey9090" |
| 63 |
#define SRP_USER "bondeduser" |
| 64 |
#define SRP_PASS "bondedpass-5566" |
| 65 |
|
| 66 |
static struct rist_logging_settings *log_settings = NULL; |
| 67 |
|
| 68 |
static pthread_mutex_t tracker_lock; |
| 69 |
#define MAX_TRACKED_PEERS 16 |
| 70 |
static struct { |
| 71 |
struct rist_peer *seen[MAX_TRACKED_PEERS]; |
| 72 |
int count; |
| 73 |
} tracker; |
| 74 |
|
| 75 |
static volatile int rx_pkts = 0; |
| 76 |
static volatile bool sender_run = true; |
| 77 |
|
| 78 |
static int log_cb(void *arg, enum rist_log_level level, const char *msg) { |
| 79 |
(void)arg; |
| 80 |
const char *tag = level <= RIST_LOG_ERROR ? "E" : |
| 81 |
level <= RIST_LOG_WARN ? "W" : |
| 82 |
level <= RIST_LOG_INFO ? "I" : "D"; |
| 83 |
fprintf(stderr, "%s| %s", tag, msg); |
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
static void sender_status_cb(void *arg, struct rist_peer *peer, |
| 88 |
enum rist_connection_status status) { |
| 89 |
(void)arg; |
| 90 |
if (status == RIST_CONNECTION_ESTABLISHED || |
| 91 |
status == RIST_CLIENT_CONNECTED) { |
| 92 |
pthread_mutex_lock(&tracker_lock); |
| 93 |
bool seen = false; |
| 94 |
for (int i = 0; i < tracker.count; i++) |
| 95 |
if (tracker.seen[i] == peer) { seen = true; break; } |
| 96 |
if (!seen && tracker.count < MAX_TRACKED_PEERS) { |
| 97 |
tracker.seen[tracker.count++] = peer; |
| 98 |
fprintf(stderr, ">> sender CONNECTED (peer=%p, distinct_callers=%d)\n", |
| 99 |
(void *)peer, tracker.count); |
| 100 |
} |
| 101 |
pthread_mutex_unlock(&tracker_lock); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
static int rx_data_cb(void *arg, struct rist_data_block *b) { |
| 106 |
(void)arg; |
| 107 |
rx_pkts++; |
| 108 |
rist_receiver_data_block_free2(&b); |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
static PTHREAD_START_FUNC(sender_feed, arg) { |
| 113 |
struct rist_ctx *tx = arg; |
| 114 |
uint32_t counter = 0; |
| 115 |
while (sender_run) { |
| 116 |
struct rist_data_block blk; |
| 117 |
memset(&blk, 0, sizeof(blk)); |
| 118 |
blk.payload = &counter; |
| 119 |
blk.payload_len = sizeof(counter); |
| 120 |
blk.virt_src_port = 1968; |
| 121 |
rist_sender_data_write(tx, &blk); |
| 122 |
counter++; |
| 123 |
usleep(20000); |
| 124 |
} |
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
static struct rist_peer *add_path(struct rist_ctx *rx, int listen_port, |
| 129 |
int local_port, const char *crypto, bool srp) { |
| 130 |
char url[320]; |
| 131 |
snprintf(url, sizeof(url), |
| 132 |
"rist://127.0.0.1:%d?local-port=%d&cname=%s&weight=5" |
| 133 |
"&session-timeout=30000&keepalive-interval=1000%s", |
| 134 |
listen_port, local_port, CNAME, crypto); |
| 135 |
struct rist_peer_config *pcfg = NULL; |
| 136 |
if (rist_parse_address2(url, (void *)&pcfg) != 0) |
| 137 |
return NULL; |
| 138 |
struct rist_peer *peer = NULL; |
| 139 |
if (rist_peer_create(rx, &peer, pcfg) != 0) { free(pcfg); return NULL; } |
| 140 |
free(pcfg); |
| 141 |
if (srp && rist_enable_eap_srp_2(peer, SRP_USER, SRP_PASS, NULL, NULL) != 0) |
| 142 |
return NULL; |
| 143 |
return peer; |
| 144 |
} |
| 145 |
|
| 146 |
int main(int argc, char *argv[]) { |
| 147 |
bool use_srp = (argc > 1 && strcmp(argv[1], "srp") == 0); |
| 148 |
int listen_port = use_srp ? 19958 : 19960; |
| 149 |
int path_a_port = use_srp ? 20071 : 20061; |
| 150 |
int path_b_port = use_srp ? 20072 : 20062; |
| 151 |
char crypto[96]; |
| 152 |
snprintf(crypto, sizeof(crypto), use_srp ? "" : "&secret=%s&aes-type=128", PSK); |
| 153 |
char url[320]; |
| 154 |
|
| 155 |
if (rist_logging_set(&log_settings, RIST_LOG_INFO, log_cb, NULL, NULL, stderr) != 0) |
| 156 |
return 99; |
| 157 |
memset(&tracker, 0, sizeof(tracker)); |
| 158 |
pthread_mutex_init(&tracker_lock, NULL); |
| 159 |
fprintf(stderr, "== mode: %s ==\n", use_srp ? "SRP (same user/pass on both paths)" |
| 160 |
: "shared PSK"); |
| 161 |
|
| 162 |
|
| 163 |
struct rist_ctx *tx = NULL; |
| 164 |
if (rist_sender_create(&tx, RIST_PROFILE_MAIN, 0, log_settings) != 0) return 99; |
| 165 |
rist_connection_status_callback_set(tx, sender_status_cb, NULL); |
| 166 |
snprintf(url, sizeof(url), |
| 167 |
"rist://@127.0.0.1:%d?session-timeout=30000&keepalive-interval=1000%s", |
| 168 |
listen_port, crypto); |
| 169 |
struct rist_peer_config *tx_pcfg = NULL; |
| 170 |
if (rist_parse_address2(url, (void *)&tx_pcfg) != 0) { rist_destroy(tx); return 99; } |
| 171 |
struct rist_peer *tx_peer = NULL; |
| 172 |
if (rist_peer_create(tx, &tx_peer, tx_pcfg) != 0) { free(tx_pcfg); rist_destroy(tx); return 99; } |
| 173 |
free(tx_pcfg); |
| 174 |
if (use_srp && rist_enable_eap_srp_2(tx_peer, SRP_USER, SRP_PASS, NULL, NULL) != 0) { |
| 175 |
rist_destroy(tx); return 99; |
| 176 |
} |
| 177 |
if (rist_start(tx) != 0) { rist_destroy(tx); return 99; } |
| 178 |
|
| 179 |
pthread_t feeder; |
| 180 |
pthread_create(&feeder, NULL, sender_feed, tx); |
| 181 |
usleep(200000); |
| 182 |
|
| 183 |
|
| 184 |
fprintf(stderr, "\n== receiver brings up two bonded paths (cname=%s) ==\n", CNAME); |
| 185 |
struct rist_ctx *rx = NULL; |
| 186 |
if (rist_receiver_create(&rx, RIST_PROFILE_MAIN, log_settings) != 0) { |
| 187 |
sender_run = false; pthread_join(feeder, NULL); rist_destroy(tx); return 99; |
| 188 |
} |
| 189 |
struct rist_peer *path_a = add_path(rx, listen_port, path_a_port, crypto, use_srp); |
| 190 |
struct rist_peer *path_b = add_path(rx, listen_port, path_b_port, crypto, use_srp); |
| 191 |
if (!path_a || !path_b) { |
| 192 |
sender_run = false; pthread_join(feeder, NULL); |
| 193 |
rist_destroy(rx); rist_destroy(tx); return 99; |
| 194 |
} |
| 195 |
if (rist_receiver_data_callback_set2(rx, rx_data_cb, NULL) != 0 || |
| 196 |
rist_start(rx) != 0) { |
| 197 |
sender_run = false; pthread_join(feeder, NULL); |
| 198 |
rist_destroy(rx); rist_destroy(tx); return 99; |
| 199 |
} |
| 200 |
|
| 201 |
usleep(use_srp ? 7000000 : 4000000); |
| 202 |
|
| 203 |
pthread_mutex_lock(&tracker_lock); |
| 204 |
int after_setup = tracker.count; |
| 205 |
pthread_mutex_unlock(&tracker_lock); |
| 206 |
int data_after_setup = rx_pkts; |
| 207 |
fprintf(stderr, "after setup: distinct_callers=%d, rx_pkts=%d\n", |
| 208 |
after_setup, data_after_setup); |
| 209 |
|
| 210 |
|
| 211 |
usleep(4000000); |
| 212 |
|
| 213 |
pthread_mutex_lock(&tracker_lock); |
| 214 |
int after_steady = tracker.count; |
| 215 |
pthread_mutex_unlock(&tracker_lock); |
| 216 |
uint32_t reb_a = path_a->rebind_attempts; |
| 217 |
uint32_t reb_b = path_b->rebind_attempts; |
| 218 |
int dead_a = path_a->dead; |
| 219 |
int dead_b = path_b->dead; |
| 220 |
fprintf(stderr, "\n== steady state: distinct_callers=%d, rx_pkts=%d, " |
| 221 |
"rebind_a=%"PRIu32" rebind_b=%"PRIu32" dead_a=%d dead_b=%d ==\n", |
| 222 |
after_steady, rx_pkts, reb_a, reb_b, dead_a, 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 (after_setup < 2 || data_after_setup == 0) { |
| 231 |
fprintf(stderr, "INDETERMINATE: both paths never established or no " |
| 232 |
"data flowed (distinct_callers=%d, rx_pkts=%d).\n", |
| 233 |
after_setup, data_after_setup); |
| 234 |
return 2; |
| 235 |
} |
| 236 |
|
| 237 |
if (after_steady != 2) { |
| 238 |
fprintf(stderr, "FAIL: expected 2 distinct callers, got %d; the two " |
| 239 |
"same-cname paths were wrongly merged.\n", after_steady); |
| 240 |
return 1; |
| 241 |
} |
| 242 |
if (reb_a != 0 || reb_b != 0) { |
| 243 |
fprintf(stderr, "FAIL: a healthy path rebound its socket " |
| 244 |
"(rebind_a=%"PRIu32", rebind_b=%"PRIu32").\n", |
| 245 |
reb_a, reb_b); |
| 246 |
return 1; |
| 247 |
} |
| 248 |
if (dead_a || dead_b) { |
| 249 |
fprintf(stderr, "FAIL: a path died (dead_a=%d, dead_b=%d).\n", |
| 250 |
dead_a, dead_b); |
| 251 |
return 1; |
| 252 |
} |
| 253 |
|
| 254 |
fprintf(stderr, "\nPASS: two bonded %s paths sharing cname \"%s\" stayed " |
| 255 |
"distinct (distinct_callers=2), the stream flowed " |
| 256 |
"(rx_pkts=%d), and neither healthy path rebound.\n", |
| 257 |
use_srp ? "SRP" : "PSK", CNAME, rx_pkts); |
| 258 |
return 0; |
| 259 |
} |