| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
d. |
| 12 |
|
| 13 |
|
| 14 |
#include <librist/librist.h> |
| 15 |
#include "librist/version.h" |
| 16 |
#include "config.h" |
| 17 |
#if HAVE_SRP_SUPPORT |
| 18 |
#include "librist/librist_srp.h" |
| 19 |
#include "srp_shared.h" |
| 20 |
#endif |
| 21 |
#include "vcs_version.h" |
| 22 |
#include "risturlhelp.h" |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <signal.h> |
| 28 |
#include <stdbool.h> |
| 29 |
#include "getopt-shim.h" |
| 30 |
|
| 31 |
#if defined(__unix) || defined(__APPLE__) |
| 32 |
#include <unistd.h> |
| 33 |
#endif |
| 34 |
#ifdef _WIN32 |
| 35 |
#include <windows.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#define RISTTUNNEL_VERSION "2" |
| 39 |
#define DEFAULT_MTU 1400 |
| 40 |
#define DEFAULT_RTT_MULTIPLIER 3 |
| 41 |
#define DEFAULT_STATS_INTERVAL 1000 |
| 42 |
|
| 43 |
static volatile int keep_running = 1; |
| 44 |
static struct rist_logging_settings logging_settings = LOGGING_SETTINGS_INITIALIZER; |
| 45 |
|
| 46 |
static void sig_handler(int sig) |
| 47 |
{ |
| 48 |
(void)sig; |
| 49 |
keep_running = 0; |
| 50 |
} |
| 51 |
|
| 52 |
static int cb_stats(void *arg, const struct rist_stats *stats_container) |
| 53 |
{ |
| 54 |
(void)arg; |
| 55 |
rist_log(&logging_settings, RIST_LOG_INFO, "%s\n", stats_container->stats_json); |
| 56 |
rist_stats_free(stats_container); |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
#if HAVE_SRP_SUPPORT |
| 61 |
static int cb_auth_connect(void *arg, const char *connecting_ip, |
| 62 |
uint16_t connecting_port, const char *local_ip, |
| 63 |
uint16_t local_port, struct rist_peer *peer) |
| 64 |
{ |
| 65 |
(void)arg; (void)connecting_ip; (void)connecting_port; |
| 66 |
(void)local_ip; (void)local_port; (void)peer; |
| 67 |
rist_log(&logging_settings, RIST_LOG_INFO, |
| 68 |
"Peer %s:%u connected\n", connecting_ip, connecting_port); |
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
static int cb_auth_disconnect(void *arg, struct rist_peer *peer) |
| 73 |
{ |
| 74 |
(void)arg; (void)peer; |
| 75 |
return 0; |
| 76 |
} |
| 77 |
#endif |
| 78 |
|
| 79 |
static void usage(const char *name) |
| 80 |
{ |
| 81 |
fprintf(stderr, |
| 82 |
"risttunnel %s / libRIST %s API %s\n" |
| 83 |
"IP tunnel over RIST with ARQ packet recovery\n\n" |
| 84 |
"Usage: %s [options]\n\n" |
| 85 |
"Required:\n" |
| 86 |
" -l, --local-ip IP/PREFIX Local tunnel IP (e.g. 10.0.0.1/24)\n" |
| 87 |
" -o, --output-url URL RIST URL for sending\n" |
| 88 |
" -b, --input-url URL RIST URL for receiving\n\n" |
| 89 |
"Optional:\n" |
| 90 |
" -i, --interface NAME TUN interface name (default: auto)\n" |
| 91 |
" -m, --mtu INT TUN MTU (default: %d)\n" |
| 92 |
" -M, --rtt-multiplier INT RIST buffer RTT multiplier (default: %d)\n" |
| 93 |
" -s, --secret STRING PSK shared secret\n" |
| 94 |
" -e, --encryption-type INT AES type: 128 or 256 (default: 128)\n" |
| 95 |
" -S, --statsinterval INT Stats interval in ms (default: %d, 0=off)\n" |
| 96 |
" -v, --verbose-level INT Log level 0-6 (default: 3)\n" |
| 97 |
" -p, --profile INT RIST profile: 0=simple, 1=main, 2=advanced (default: 2)\n" |
| 98 |
#if HAVE_SRP_SUPPORT |
| 99 |
" -F, --srpfile PATH EAP-SRP verifier file (server side)\n" |
| 100 |
#endif |
| 101 |
" -h, --help Show this help\n\n" |
| 102 |
"Example (server / listener side):\n" |
| 103 |
" sudo %s -l 10.0.0.1/24 -b rist://@:5000 -o rist://@:5001\n\n" |
| 104 |
"Example (client / caller side):\n" |
| 105 |
" sudo %s -l 10.0.0.2/24 -b rist://server:5001 -o rist://server:5000\n\n" |
| 106 |
"%s", |
| 107 |
RISTTUNNEL_VERSION, librist_version(), librist_api_version(), |
| 108 |
name, DEFAULT_MTU, DEFAULT_RTT_MULTIPLIER, DEFAULT_STATS_INTERVAL, |
| 109 |
name, name, help_urlstr); |
| 110 |
exit(1); |
| 111 |
} |
| 112 |
|
| 113 |
static int parse_ip_prefix(const char *cidr, char *ip_out, size_t ip_len, int *prefix_out) |
| 114 |
{ |
| 115 |
const char *slash = strchr(cidr, '/'); |
| 116 |
if (!slash) { |
| 117 |
strncpy(ip_out, cidr, ip_len - 1); |
| 118 |
*prefix_out = 32; |
| 119 |
return 0; |
| 120 |
} |
| 121 |
|
| 122 |
size_t ip_part_len = (size_t)(slash - cidr); |
| 123 |
if (ip_part_len >= ip_len) |
| 124 |
return -1; |
| 125 |
|
| 126 |
memcpy(ip_out, cidr, ip_part_len); |
| 127 |
ip_out[ip_part_len] = '\0'; |
| 128 |
*prefix_out = atoi(slash + 1); |
| 129 |
|
| 130 |
if (*prefix_out < 0 || *prefix_out > 32) |
| 131 |
return -1; |
| 132 |
|
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
int main(int argc, char *argv[]) |
| 137 |
{ |
| 138 |
int exitcode = 0; |
| 139 |
char *local_ip_cidr = NULL; |
| 140 |
char *interface_name = NULL; |
| 141 |
char *output_url = NULL; |
| 142 |
char *input_url = NULL; |
| 143 |
char *secret = NULL; |
| 144 |
#if HAVE_SRP_SUPPORT |
| 145 |
char *srpfile = NULL; |
| 146 |
#endif |
| 147 |
int mtu = DEFAULT_MTU; |
| 148 |
int rtt_multiplier = DEFAULT_RTT_MULTIPLIER; |
| 149 |
int encryption_type = 0; |
| 150 |
int statsinterval = DEFAULT_STATS_INTERVAL; |
| 151 |
enum rist_log_level loglevel = RIST_LOG_WARN; |
| 152 |
enum rist_profile profile = RIST_DEFAULT_PROFILE; |
| 153 |
struct rist_ctx *sender_ctx = NULL; |
| 154 |
struct rist_ctx *receiver_ctx = NULL; |
| 155 |
int tun_fd = -1; |
| 156 |
|
| 157 |
static struct option long_options[] = { |
| 158 |
{ "local-ip", required_argument, NULL, 'l' }, |
| 159 |
{ "interface", required_argument, NULL, 'i' }, |
| 160 |
{ "output-url", required_argument, NULL, 'o' }, |
| 161 |
{ "input-url", required_argument, NULL, 'b' }, |
| 162 |
{ "mtu", required_argument, NULL, 'm' }, |
| 163 |
{ "rtt-multiplier", required_argument, NULL, 'M' }, |
| 164 |
{ "secret", required_argument, NULL, 's' }, |
| 165 |
{ "encryption-type", required_argument, NULL, 'e' }, |
| 166 |
{ "statsinterval", required_argument, NULL, 'S' }, |
| 167 |
{ "verbose-level", required_argument, NULL, 'v' }, |
| 168 |
{ "profile", required_argument, NULL, 'p' }, |
| 169 |
#if HAVE_SRP_SUPPORT |
| 170 |
{ "srpfile", required_argument, NULL, 'F' }, |
| 171 |
#endif |
| 172 |
{ "help", no_argument, NULL, 'h' }, |
| 173 |
{ NULL, 0, NULL, 0 } |
| 174 |
}; |
| 175 |
|
| 176 |
#if HAVE_SRP_SUPPORT |
| 177 |
const char *optstring = "l:i:o:b:m:M:s:e:S:v:p:F:h"; |
| 178 |
#else |
| 179 |
const char *optstring = "l:i:o:b:m:M:s:e:S:v:p:h"; |
| 180 |
#endif |
| 181 |
|
| 182 |
int opt; |
| 183 |
while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) { |
| 184 |
switch (opt) { |
| 185 |
case 'l': local_ip_cidr = strdup(optarg); break; |
| 186 |
case 'i': interface_name = strdup(optarg); break; |
| 187 |
case 'o': output_url = strdup(optarg); break; |
| 188 |
case 'b': input_url = strdup(optarg); break; |
| 189 |
case 'm': mtu = atoi(optarg); break; |
| 190 |
case 'M': rtt_multiplier = atoi(optarg); break; |
| 191 |
case 's': secret = strdup(optarg); break; |
| 192 |
case 'e': encryption_type = atoi(optarg); break; |
| 193 |
case 'S': statsinterval = atoi(optarg); break; |
| 194 |
case 'v': loglevel = (enum rist_log_level)atoi(optarg); break; |
| 195 |
case 'p': profile = (enum rist_profile)atoi(optarg); break; |
| 196 |
#if HAVE_SRP_SUPPORT |
| 197 |
case 'F': srpfile = strdup(optarg); break; |
| 198 |
#endif |
| 199 |
case 'h': |
| 200 |
default: |
| 201 |
usage(argv[0]); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
if (!local_ip_cidr || !output_url || !input_url) |
| 206 |
usage(argv[0]); |
| 207 |
|
| 208 |
|
| 209 |
if (secret) { |
| 210 |
char url_suffix[512]; |
| 211 |
int enc = encryption_type > 0 ? encryption_type : 128; |
| 212 |
snprintf(url_suffix, sizeof(url_suffix), "%csecret=%s&aes-type=%d", |
| 213 |
strchr(output_url, '?') ? '&' : '?', secret, enc); |
| 214 |
|
| 215 |
char *new_out = malloc(strlen(output_url) + strlen(url_suffix) + 1); |
| 216 |
char *new_in = malloc(strlen(input_url) + strlen(url_suffix) + 1); |
| 217 |
if (!new_out || !new_in) { |
| 218 |
fprintf(stderr, "Out of memory\n"); |
| 219 |
exitcode = 1; |
| 220 |
goto cleanup; |
| 221 |
} |
| 222 |
sprintf(new_out, "%s%s", output_url, url_suffix); |
| 223 |
free(output_url); |
| 224 |
output_url = new_out; |
| 225 |
|
| 226 |
snprintf(url_suffix, sizeof(url_suffix), "%csecret=%s&aes-type=%d", |
| 227 |
strchr(input_url, '?') ? '&' : '?', secret, enc); |
| 228 |
sprintf(new_in, "%s%s", input_url, url_suffix); |
| 229 |
free(input_url); |
| 230 |
input_url = new_in; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
char ip_addr[64]; |
| 235 |
int prefix_len; |
| 236 |
if (parse_ip_prefix(local_ip_cidr, ip_addr, sizeof(ip_addr), &prefix_len) != 0) { |
| 237 |
fprintf(stderr, "Invalid IP/prefix: %s\n", local_ip_cidr); |
| 238 |
exitcode = 1; |
| 239 |
goto cleanup; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
signal(SIGINT, sig_handler); |
| 244 |
signal(SIGTERM, sig_handler); |
| 245 |
#ifdef SIGPIPE |
| 246 |
signal(SIGPIPE, SIG_IGN); |
| 247 |
#endif |
| 248 |
|
| 249 |
|
| 250 |
struct rist_logging_settings *log_ptr; |
| 251 |
if (rist_logging_set(&log_ptr, loglevel, NULL, NULL, NULL, stderr) != 0) { |
| 252 |
fprintf(stderr, "Failed to setup logging\n"); |
| 253 |
exitcode = 1; |
| 254 |
goto cleanup; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
char actual_ifname[64] = {0}; |
| 259 |
tun_fd = rist_tun_open(interface_name, actual_ifname, sizeof(actual_ifname)); |
| 260 |
if (tun_fd < 0) { |
| 261 |
fprintf(stderr, "Failed to open TUN device (are you root?)\n"); |
| 262 |
exitcode = 1; |
| 263 |
goto cleanup; |
| 264 |
} |
| 265 |
|
| 266 |
if (rist_tun_set_mtu(actual_ifname, mtu) != 0 || |
| 267 |
rist_tun_set_ip(actual_ifname, ip_addr, prefix_len) != 0 || |
| 268 |
rist_tun_bring_up(actual_ifname) != 0) { |
| 269 |
fprintf(stderr, "Failed to configure TUN interface %s\n", actual_ifname); |
| 270 |
exitcode = 1; |
| 271 |
goto cleanup; |
| 272 |
} |
| 273 |
|
| 274 |
rist_log(&logging_settings, RIST_LOG_INFO, |
| 275 |
"TUN %s: %s/%d MTU %d\n", actual_ifname, ip_addr, prefix_len, mtu); |
| 276 |
|
| 277 |
*/ |
| 278 |
if (rist_sender_create(&sender_ctx, profile, 0, log_ptr) != 0) { |
| 279 |
fprintf(stderr, "Failed to create RIST sender\n"); |
| 280 |
exitcode = 1; |
| 281 |
goto cleanup; |
| 282 |
} |
| 283 |
if (rtt_multiplier > 0) |
| 284 |
rist_recovery_rtt_multiplier_set(sender_ctx, rtt_multiplier); |
| 285 |
|
| 286 |
rist_sender_data_fd_set(sender_ctx, tun_fd, (size_t)mtu, RIST_DATA_FD_FLAG_TUN); |
| 287 |
|
| 288 |
struct rist_peer_config *sender_peer_cfg = NULL; |
| 289 |
if (rist_parse_address2(output_url, &sender_peer_cfg) != 0) { |
| 290 |
fprintf(stderr, "Failed to parse sender URL: %s\n", output_url); |
| 291 |
exitcode = 1; |
| 292 |
goto cleanup; |
| 293 |
} |
| 294 |
if (mtu < 1400 && sender_peer_cfg->split_mode == LIBRIST_SPLIT_MODE_OFF) { |
| 295 |
rist_log(&logging_settings, RIST_LOG_INFO, |
| 296 |
"MTU %d < 1400: auto-enabling split=half on sender\n", mtu); |
| 297 |
sender_peer_cfg->split_mode = LIBRIST_SPLIT_MODE_HALF; |
| 298 |
} |
| 299 |
struct rist_peer *sender_peer = NULL; |
| 300 |
if (rist_peer_create(sender_ctx, &sender_peer, sender_peer_cfg) != 0) { |
| 301 |
fprintf(stderr, "Failed to create sender peer\n"); |
| 302 |
exitcode = 1; |
| 303 |
goto cleanup; |
| 304 |
} |
| 305 |
if (statsinterval > 0) |
| 306 |
rist_stats_callback_set(sender_ctx, statsinterval, cb_stats, NULL); |
| 307 |
|
| 308 |
*/ |
| 309 |
if (rist_receiver_create(&receiver_ctx, profile, log_ptr) != 0) { |
| 310 |
fprintf(stderr, "Failed to create RIST receiver\n"); |
| 311 |
exitcode = 1; |
| 312 |
goto cleanup; |
| 313 |
} |
| 314 |
if (rtt_multiplier > 0) |
| 315 |
rist_recovery_rtt_multiplier_set(receiver_ctx, rtt_multiplier); |
| 316 |
|
| 317 |
rist_receiver_data_fd_set(receiver_ctx, tun_fd, RIST_DATA_FD_FLAG_TUN); |
| 318 |
|
| 319 |
#if HAVE_SRP_SUPPORT |
| 320 |
if (srpfile) { |
| 321 |
if (rist_auth_handler_set(receiver_ctx, cb_auth_connect, cb_auth_disconnect, receiver_ctx) != 0) { |
| 322 |
fprintf(stderr, "Failed to set auth handler\n"); |
| 323 |
exitcode = 1; |
| 324 |
goto cleanup; |
| 325 |
} |
| 326 |
} |
| 327 |
#endif |
| 328 |
|
| 329 |
struct rist_peer_config *recv_peer_cfg = NULL; |
| 330 |
if (rist_parse_address2(input_url, &recv_peer_cfg) != 0) { |
| 331 |
fprintf(stderr, "Failed to parse receiver URL: %s\n", input_url); |
| 332 |
exitcode = 1; |
| 333 |
goto cleanup; |
| 334 |
} |
| 335 |
if (mtu < 1400 && recv_peer_cfg->merge_mode == LIBRIST_MERGE_MODE_OFF) { |
| 336 |
rist_log(&logging_settings, RIST_LOG_INFO, |
| 337 |
"MTU %d < 1400: auto-enabling merge=auto on receiver\n", mtu); |
| 338 |
recv_peer_cfg->merge_mode = LIBRIST_MERGE_MODE_AUTO; |
| 339 |
} |
| 340 |
struct rist_peer *recv_peer = NULL; |
| 341 |
if (rist_peer_create(receiver_ctx, &recv_peer, recv_peer_cfg) != 0) { |
| 342 |
fprintf(stderr, "Failed to create receiver peer\n"); |
| 343 |
exitcode = 1; |
| 344 |
goto cleanup; |
| 345 |
} |
| 346 |
|
| 347 |
#if HAVE_SRP_SUPPORT |
| 348 |
if (srpfile) { |
| 349 |
if (rist_enable_eap_srp_2(recv_peer, NULL, NULL, user_verifier_lookup, (void *)srpfile) != 0) { |
| 350 |
fprintf(stderr, "Failed to enable EAP-SRP\n"); |
| 351 |
exitcode = 1; |
| 352 |
goto cleanup; |
| 353 |
} |
| 354 |
} |
| 355 |
#endif |
| 356 |
|
| 357 |
if (statsinterval > 0) |
| 358 |
rist_stats_callback_set(receiver_ctx, statsinterval, cb_stats, NULL); |
| 359 |
|
| 360 |
*/ |
| 361 |
if (rist_start(sender_ctx) != 0 || rist_start(receiver_ctx) != 0) { |
| 362 |
fprintf(stderr, "Failed to start RIST\n"); |
| 363 |
exitcode = 1; |
| 364 |
goto cleanup; |
| 365 |
} |
| 366 |
|
| 367 |
rist_log(&logging_settings, RIST_LOG_INFO, |
| 368 |
"Tunnel active: %s/%d via RIST (%s -> %s)\n", |
| 369 |
ip_addr, prefix_len, input_url, output_url); |
| 370 |
|
| 371 |
*/ |
| 372 |
while (keep_running) { |
| 373 |
#if defined(__unix) || defined(__APPLE__) |
| 374 |
sleep(1); |
| 375 |
#else |
| 376 |
Sleep(1000); |
| 377 |
#endif |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
struct rist_data_fd_stats tx_stats, rx_stats; |
| 382 |
rist_data_fd_stats_get(sender_ctx, &tx_stats); |
| 383 |
rist_data_fd_stats_get(receiver_ctx, &rx_stats); |
| 384 |
rist_log(&logging_settings, RIST_LOG_INFO, |
| 385 |
"Shutting down. TX: %lu pkts / %lu bytes, RX: %lu pkts / %lu bytes\n", |
| 386 |
(unsigned long)tx_stats.tx_packets, (unsigned long)tx_stats.tx_bytes, |
| 387 |
(unsigned long)rx_stats.rx_packets, (unsigned long)rx_stats.rx_bytes); |
| 388 |
|
| 389 |
cleanup: |
| 390 |
if (receiver_ctx) |
| 391 |
rist_destroy(receiver_ctx); |
| 392 |
if (sender_ctx) |
| 393 |
rist_destroy(sender_ctx); |
| 394 |
if (tun_fd >= 0) |
| 395 |
rist_tun_close(tun_fd); |
| 396 |
|
| 397 |
rist_logging_unset_global(); |
| 398 |
|
| 399 |
free(local_ip_cidr); |
| 400 |
free(interface_name); |
| 401 |
free(output_url); |
| 402 |
free(input_url); |
| 403 |
free(secret); |
| 404 |
#if HAVE_SRP_SUPPORT |
| 405 |
free(srpfile); |
| 406 |
#endif |
| 407 |
|
| 408 |
return exitcode; |
| 409 |
} |