| 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 |
#include "librist/librist.h" |
| 48 |
#include "librist/librist_srp.h" |
| 49 |
#include "rist-private.h" |
| 50 |
#include <inttypes.h> |
| 51 |
#include <stdbool.h> |
| 52 |
#include <stdio.h> |
| 53 |
#include <stdlib.h> |
| 54 |
#include <string.h> |
| 55 |
#ifdef _WIN32 |
| 56 |
#include <windows.h> |
| 57 |
#else |
| 58 |
#include <unistd.h> |
| 59 |
#endif |
| 60 |
|
| 61 |
#define SRP_USER "restartuser" |
| 62 |
#define SRP_PASS "restartpass-9137" |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
#define SESSION_TIMEOUT_MS 30000 |
| 68 |
#define KEEPALIVE_MS 500 |
| 69 |
|
| 70 |
static struct rist_logging_settings *log_settings = NULL; |
| 71 |
static pthread_mutex_t cb_lock; |
| 72 |
static unsigned rx_data_count; |
| 73 |
|
| 74 |
static int rx_data_cb(void *arg, struct rist_data_block *b) { |
| 75 |
(void)arg; |
| 76 |
pthread_mutex_lock(&cb_lock); |
| 77 |
rx_data_count++; |
| 78 |
pthread_mutex_unlock(&cb_lock); |
| 79 |
rist_receiver_data_block_free2(&b); |
| 80 |
return 0; |
| 81 |
} |
| 82 |
|
| 83 |
static unsigned rx_data_snapshot(void) { |
| 84 |
pthread_mutex_lock(&cb_lock); |
| 85 |
unsigned v = rx_data_count; |
| 86 |
pthread_mutex_unlock(&cb_lock); |
| 87 |
return v; |
| 88 |
} |
| 89 |
|
| 90 |
static void send_burst(struct rist_ctx *tx, uint32_t start, int count) { |
| 91 |
for (int i = 0; i < count; i++) { |
| 92 |
uint32_t v = start + (uint32_t)i; |
| 93 |
struct rist_data_block blk; |
| 94 |
memset(&blk, 0, sizeof(blk)); |
| 95 |
blk.payload = &v; |
| 96 |
blk.payload_len = sizeof(v); |
| 97 |
blk.virt_src_port = 1968; |
| 98 |
rist_sender_data_write(tx, &blk); |
| 99 |
usleep(20000); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
static int log_cb(void *arg, enum rist_log_level level, const char *msg) { |
| 104 |
(void)arg; |
| 105 |
const char *tag = level <= RIST_LOG_ERROR ? "E" : |
| 106 |
level <= RIST_LOG_WARN ? "W" : |
| 107 |
level <= RIST_LOG_INFO ? "I" : "D"; |
| 108 |
fprintf(stderr, "%s| %s", tag, msg); |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
static struct rist_ctx *spawn_authenticator(int port) { |
| 114 |
struct rist_ctx *tx = NULL; |
| 115 |
if (rist_sender_create(&tx, RIST_PROFILE_MAIN, 0, log_settings) != 0) |
| 116 |
return NULL; |
| 117 |
char url[256]; |
| 118 |
snprintf(url, sizeof(url), |
| 119 |
"rist://@127.0.0.1:%d?session-timeout=%d&keepalive-interval=%d", |
| 120 |
port, SESSION_TIMEOUT_MS, KEEPALIVE_MS); |
| 121 |
struct rist_peer_config *pcfg = NULL; |
| 122 |
if (rist_parse_address2(url, (void *)&pcfg) != 0) { rist_destroy(tx); return NULL; } |
| 123 |
struct rist_peer *peer = NULL; |
| 124 |
if (rist_peer_create(tx, &peer, pcfg) != 0) { free(pcfg); rist_destroy(tx); return NULL; } |
| 125 |
free(pcfg); |
| 126 |
if (rist_enable_eap_srp_2(peer, SRP_USER, SRP_PASS, NULL, NULL) != 0) { rist_destroy(tx); return NULL; } |
| 127 |
if (rist_start(tx) != 0) { rist_destroy(tx); return NULL; } |
| 128 |
return tx; |
| 129 |
} |
| 130 |
|
| 131 |
int main(void) { |
| 132 |
const int listen_port = 22012; |
| 133 |
memset(&rx_data_count, 0, sizeof(rx_data_count)); |
| 134 |
pthread_mutex_init(&cb_lock, NULL); |
| 135 |
|
| 136 |
if (rist_logging_set(&log_settings, RIST_LOG_INFO, log_cb, NULL, NULL, stderr) != 0) { |
| 137 |
fprintf(stderr, "logging setup failed\n"); |
| 138 |
return 99; |
| 139 |
} |
| 140 |
|
| 141 |
fprintf(stderr, "== spawning first authenticator (listener) on :%d ==\n", listen_port); |
| 142 |
struct rist_ctx *tx1 = spawn_authenticator(listen_port); |
| 143 |
if (!tx1) { fprintf(stderr, "authenticator create failed\n"); return 99; } |
| 144 |
usleep(200000); |
| 145 |
|
| 146 |
fprintf(stderr, "== spawning caller (authenticatee) ==\n"); |
| 147 |
struct rist_ctx *rx = NULL; |
| 148 |
if (rist_receiver_create(&rx, RIST_PROFILE_MAIN, log_settings) != 0) { |
| 149 |
fprintf(stderr, "rx ctx create failed\n"); rist_destroy(tx1); return 99; |
| 150 |
} |
| 151 |
char rx_url[256]; |
| 152 |
snprintf(rx_url, sizeof(rx_url), |
| 153 |
"rist://127.0.0.1:%d?session-timeout=%d&keepalive-interval=%d" |
| 154 |
"&buffer=200&cname=restart-test", |
| 155 |
listen_port, SESSION_TIMEOUT_MS, KEEPALIVE_MS); |
| 156 |
struct rist_peer_config *rx_pcfg = NULL; |
| 157 |
if (rist_parse_address2(rx_url, (void *)&rx_pcfg) != 0) { |
| 158 |
fprintf(stderr, "rx url parse failed\n"); rist_destroy(rx); rist_destroy(tx1); return 99; |
| 159 |
} |
| 160 |
struct rist_peer *rx_peer = NULL; |
| 161 |
if (rist_peer_create(rx, &rx_peer, rx_pcfg) != 0) { |
| 162 |
fprintf(stderr, "rx peer create failed\n"); free(rx_pcfg); rist_destroy(rx); rist_destroy(tx1); return 99; |
| 163 |
} |
| 164 |
free(rx_pcfg); |
| 165 |
if (rist_enable_eap_srp_2(rx_peer, SRP_USER, SRP_PASS, NULL, NULL) != 0) { |
| 166 |
fprintf(stderr, "rx SRP enable failed\n"); rist_destroy(rx); rist_destroy(tx1); return 99; |
| 167 |
} |
| 168 |
if (rist_receiver_data_callback_set2(rx, rx_data_cb, NULL) != 0) { |
| 169 |
fprintf(stderr, "rx data callback set failed\n"); rist_destroy(rx); rist_destroy(tx1); return 99; |
| 170 |
} |
| 171 |
if (rist_start(rx) != 0) { |
| 172 |
fprintf(stderr, "rx start failed\n"); rist_destroy(rx); rist_destroy(tx1); return 99; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
usleep(5000000); |
| 177 |
|
| 178 |
|
| 179 |
send_burst(tx1, 0, 25); |
| 180 |
usleep(1000000); |
| 181 |
if (rx_data_snapshot() == 0) { |
| 182 |
fprintf(stderr, "INDETERMINATE: no data before restart; setup broken.\n"); |
| 183 |
rist_destroy(rx); rist_destroy(tx1); free(log_settings); return 2; |
| 184 |
} |
| 185 |
fprintf(stderr, "== initial auth + data path OK (%u blocks) ==\n", rx_data_snapshot()); |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
fprintf(stderr, "== restarting authenticator (fresh EAP state, same port) ==\n"); |
| 191 |
rist_destroy(tx1); |
| 192 |
usleep(200000); |
| 193 |
struct rist_ctx *tx2 = spawn_authenticator(listen_port); |
| 194 |
if (!tx2) { |
| 195 |
fprintf(stderr, "second authenticator create failed\n"); |
| 196 |
rist_destroy(rx); free(log_settings); return 99; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
usleep(8000000); |
| 202 |
|
| 203 |
unsigned data_before = rx_data_snapshot(); |
| 204 |
send_burst(tx2, 1000, 50); |
| 205 |
usleep(1200000); |
| 206 |
unsigned data_after = rx_data_snapshot(); |
| 207 |
|
| 208 |
uint32_t rebinds = rx_peer->rebind_attempts; |
| 209 |
bool dead = rx_peer->dead; |
| 210 |
|
| 211 |
rist_destroy(rx); |
| 212 |
rist_destroy(tx2); |
| 213 |
free(log_settings); |
| 214 |
|
| 215 |
fprintf(stderr, "== after restart: data_before=%u data_after=%u " |
| 216 |
"rebind_attempts=%"PRIu32" dead=%d ==\n", |
| 217 |
data_before, data_after, rebinds, dead); |
| 218 |
|
| 219 |
if (data_after <= data_before) { |
| 220 |
fprintf(stderr, "FAIL: caller did NOT recover after the authenticator " |
| 221 |
"restart; no data flowed (locked in SUCCESS, refusing " |
| 222 |
"the fresh IDENTITY forever).\n"); |
| 223 |
return 1; |
| 224 |
} |
| 225 |
if (rebinds != 0) { |
| 226 |
fprintf(stderr, "FAIL: recovery came from the socket rebind " |
| 227 |
"(rebind_attempts=%"PRIu32"), not the identity re-auth " |
| 228 |
"path this test targets; tighten the timing.\n", rebinds); |
| 229 |
return 1; |
| 230 |
} |
| 231 |
|
| 232 |
fprintf(stderr, "PASS: caller re-authenticated against the restarted " |
| 233 |
"authenticator on its own (%u blocks flowed after restart, " |
| 234 |
"no socket rebind) -- no operator intervention.\n", |
| 235 |
data_after - data_before); |
| 236 |
return 0; |
| 237 |
} |