Debian Codegraph @

tools/risttunnel.c

raw symbols SCIP
1 /* librist. Copyright © 2026 SipRadius LLC. All right reserved.
2 * Author: Sergio Ammirata, Ph.D. <[email protected]>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7 /* risttunnel: point-to-point IP tunnel over RIST with ARQ recovery
8 *
9 * This tool uses librist's data_fd API to hand the TUN file descriptor
10 * directly to the library. librist handles all packet forwarding
11 * internally — no application-level read/write loop is needed.
12 */
13
14 #include <librist/librist.h>
15 #include <librist/oob.h>
16 #include <librist/tun.h>
17 #include "librist/version.h"
18 #include "config.h"
19 #if HAVE_SRP_SUPPORT
20 #include "librist/librist_srp.h"
21 #include "srp_shared.h"
22 #endif
23 #include "vcs_version.h"
24 #include "risturlhelp.h"
25 #include "pthread-shim.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <signal.h>
32 #include <stdbool.h>
33 #include "getopt-shim.h"
34
35 #if defined(__unix) || defined(__APPLE__)
36 #include <unistd.h>
37 #include <poll.h>
38 #endif
39 #ifdef _WIN32
40 #include <windows.h>
41 #endif
42
43 #define RISTTUNNEL_VERSION "2"
44 #define DEFAULT_MTU 1400
45 #define DEFAULT_RTT_MULTIPLIER 3
46 #define DEFAULT_STATS_INTERVAL 1000
47
48 static volatile int keep_running = 1;
49 static struct rist_logging_settings logging_settings = LOGGING_SETTINGS_INITIALIZER;
50
51 static void sig_handler(int sig)
52 {
53 (void)sig;
54 keep_running = 0;
55 }
56
57 static int cb_stats(void *arg, const struct rist_stats *stats_container)
58 {
59 (void)arg;
60 rist_log(&logging_settings, RIST_LOG_INFO, "%s\n", stats_container->stats_json);
61 rist_stats_free(stats_container);
62 return 0;
63 }
64
65 /* ---- single-port mode (one UDP port, bidirectional) -------------------
66 * A single RIST connection carries both directions on one socket:
67 * - forward rides the RTP data_fd path -> ARQ-protected
68 * - reverse rides the RIST OOB channel -> best-effort (no ARQ)
69 *
70 * Roles are asymmetric (one connection only has one sender + one receiver):
71 * caller (sender side): data_fd reads TUN -> RTP (forward out);
72 * OOB callback writes inbound OOB -> TUN (reverse in)
73 * listener (receiver side): data_fd writes inbound RTP -> TUN (forward in);
74 * a reader thread reads TUN -> OOB (reverse out)
75 *
76 * The heavy direction of a support tunnel (device -> concentrator: shell
77 * output, log/file pulls) is the forward/RTP/ARQ-protected path; the light
78 * reverse direction (keystrokes/commands) tolerates best-effort OOB.
79 */
80 struct sp_reverse {
81 struct rist_ctx *ctx; /* context that owns the OOB channel */
82 struct rist_peer *volatile peer; /* connected remote peer (listener side) */
83 int tun_fd;
84 int mtu;
85 };
86
87 /* OOB received from the peer -> inject into the local TUN. */
88 static int sp_oob_to_tun(void *arg, const struct rist_oob_block *oob_block)
89 {
90 int tun_fd = *(int *)arg;
91 if (oob_block && oob_block->payload && oob_block->payload_len > 0)
92 rist_tun_write(tun_fd, (const uint8_t *)oob_block->payload,
93 oob_block->payload_len);
94 return 0;
95 }
96
97 /* Listener side: librist only auto-learns oob_current_peer from *received*
98 * OOB. The caller sends only forward RTP, so capture the connecting peer
99 * here and hand it explicitly to rist_oob_write() for the reverse path. */
100 static int sp_conn_cb(void *arg, const char *connecting_ip, uint16_t connecting_port,
101 const char *local_ip, uint16_t local_port, struct rist_peer *peer)
102 {
103 (void)connecting_ip; (void)connecting_port; (void)local_ip; (void)local_port;
104 struct sp_reverse *r = (struct sp_reverse *)arg;
105 if (r)
106 r->peer = peer;
107 return 0;
108 }
109
110 static int sp_disconn_cb(void *arg, struct rist_peer *peer)
111 {
112 struct sp_reverse *r = (struct sp_reverse *)arg;
113 if (r && r->peer == peer)
114 r->peer = NULL;
115 return 0;
116 }
117
118 /* Read packets off the local TUN -> send to the peer over OOB. */
119 static PTHREAD_START_FUNC(sp_tun_to_oob, arg)
120 {
121 struct sp_reverse *r = (struct sp_reverse *)arg;
122 size_t bufsize = (r->mtu > 0 ? (size_t)r->mtu : 1500) + 64;
123 uint8_t *buf = malloc(bufsize);
124 if (!buf)
125 return 0;
126 while (keep_running) {
127 #if defined(__unix) || defined(__APPLE__)
128 struct pollfd pfd = { .fd = r->tun_fd, .events = POLLIN, .revents = 0 };
129 if (poll(&pfd, 1, 200) <= 0)
130 continue; /* timeout -> re-check keep_running */
131 #endif
132 int n = rist_tun_read(r->tun_fd, buf, bufsize);
133 if (n <= 0)
134 continue;
135 if (!r->peer)
136 continue; /* no peer connected yet -> drop */
137 struct rist_oob_block ob;
138 memset(&ob, 0, sizeof(ob));
139 ob.peer = r->peer; /* explicit reverse destination */
140 ob.payload = buf;
141 ob.payload_len = (size_t)n;
142 rist_oob_write(r->ctx, &ob);
143 }
144 free(buf);
145 return 0;
146 }
147
148 #if HAVE_SRP_SUPPORT
149 static int cb_auth_connect(void *arg, const char *connecting_ip,
150 uint16_t connecting_port, const char *local_ip,
151 uint16_t local_port, struct rist_peer *peer)
152 {
153 (void)arg; (void)connecting_ip; (void)connecting_port;
154 (void)local_ip; (void)local_port; (void)peer;
155 rist_log(&logging_settings, RIST_LOG_INFO,
156 "Peer %s:%u connected\n", connecting_ip, connecting_port);
157 return 0;
158 }
159
160 static int cb_auth_disconnect(void *arg, struct rist_peer *peer)
161 {
162 (void)arg; (void)peer;
163 return 0;
164 }
165 #endif
166
167 static void usage(const char *name)
168 {
169 fprintf(stderr,
170 "risttunnel %s / libRIST %s API %s\n"
171 "IP tunnel over RIST with ARQ packet recovery\n\n"
172 "Usage: %s [options]\n\n"
173 "Required:\n"
174 " -l, --local-ip IP/PREFIX Local tunnel IP (e.g. 10.0.0.1/24 or fd00::1/64)\n"
175 " -o, --output-url URL RIST URL for sending\n"
176 " -b, --input-url URL RIST URL for receiving\n\n"
177 "Optional:\n"
178 " -i, --interface NAME TUN interface name (default: auto)\n"
179 " -m, --mtu INT TUN MTU (default: %d)\n"
180 " -M, --rtt-multiplier INT RIST buffer RTT multiplier (default: %d)\n"
181 " -s, --secret STRING PSK shared secret\n"
182 " -e, --encryption-type INT AES type: 128 or 256 (default: 128)\n"
183 " -S, --statsinterval INT Stats interval in ms (default: %d, 0=off)\n"
184 " -v, --verbose-level INT Log level 0-6 (default: 3)\n"
185 " -p, --profile INT RIST profile: 0=simple, 1=main, 2=advanced (default: 2)\n"
186 " -1, --single-port One UDP port (forward=RTP/ARQ, reverse=OOB).\n"
187 " Give exactly one of -o (caller) or -b (listener).\n"
188 #if HAVE_SRP_SUPPORT
189 " -F, --srpfile PATH EAP-SRP verifier file (server side)\n"
190 #endif
191 " -h, --help Show this help\n\n"
192 "Example (server / listener side, two ports):\n"
193 " sudo %s -l 10.0.0.1/24 -b rist://@:5000 -o rist://@:5001\n\n"
194 "Example (client / caller side, two ports):\n"
195 " sudo %s -l 10.0.0.2/24 -b rist://server:5001 -o rist://server:5000\n\n"
196 "Example (single port, listener): sudo %s -1 -l 10.0.0.1/24 -b rist://@:443\n"
197 "Example (single port, caller): sudo %s -1 -l 10.0.0.2/24 -o rist://server:443\n\n"
198 "%s",
199 RISTTUNNEL_VERSION, librist_version(), librist_api_version(),
200 name, DEFAULT_MTU, DEFAULT_RTT_MULTIPLIER, DEFAULT_STATS_INTERVAL,
201 name, name, name, name, help_urlstr);
202 exit(1);
203 }
204
205 static int parse_ip_prefix(const char *cidr, char *ip_out, size_t ip_len, int *prefix_out)
206 {
207 return rist_tun_parse_cidr(cidr, ip_out, ip_len, prefix_out);
208 }
209
210 int main(int argc, char *argv[])
211 {
212 int exitcode = 0;
213 char *local_ip_cidr = NULL;
214 char *interface_name = NULL;
215 char *output_url = NULL;
216 char *input_url = NULL;
217 char *secret = NULL;
218 #if HAVE_SRP_SUPPORT
219 char *srpfile = NULL;
220 #endif
221 int mtu = DEFAULT_MTU;
222 int rtt_multiplier = DEFAULT_RTT_MULTIPLIER;
223 int encryption_type = 0;
224 int statsinterval = DEFAULT_STATS_INTERVAL;
225 enum rist_log_level loglevel = RIST_LOG_WARN;
226 enum rist_profile profile = RIST_DEFAULT_PROFILE;
227 struct rist_ctx *sender_ctx = NULL;
228 struct rist_ctx *receiver_ctx = NULL;
229 int tun_fd = -1;
230 int single_port = 0;
231 pthread_t sp_thread = 0;
232 int sp_thread_started = 0;
233 struct sp_reverse sp_rev = { 0 };
234
235 static struct option long_options[] = {
236 { "local-ip", required_argument, NULL, 'l' },
237 { "interface", required_argument, NULL, 'i' },
238 { "output-url", required_argument, NULL, 'o' },
239 { "input-url", required_argument, NULL, 'b' },
240 { "mtu", required_argument, NULL, 'm' },
241 { "rtt-multiplier", required_argument, NULL, 'M' },
242 { "secret", required_argument, NULL, 's' },
243 { "encryption-type", required_argument, NULL, 'e' },
244 { "statsinterval", required_argument, NULL, 'S' },
245 { "verbose-level", required_argument, NULL, 'v' },
246 { "profile", required_argument, NULL, 'p' },
247 { "single-port", no_argument, NULL, '1' },
248 #if HAVE_SRP_SUPPORT
249 { "srpfile", required_argument, NULL, 'F' },
250 #endif
251 { "help", no_argument, NULL, 'h' },
252 { NULL, 0, NULL, 0 }
253 };
254
255 #if HAVE_SRP_SUPPORT
256 const char *optstring = "l:i:o:b:m:M:s:e:S:v:p:1F:h";
257 #else
258 const char *optstring = "l:i:o:b:m:M:s:e:S:v:p:1h";
259 #endif
260
261 int opt;
262 while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) {
263 switch (opt) {
264 case 'l': local_ip_cidr = strdup(optarg); break;
265 case 'i': interface_name = strdup(optarg); break;
266 case 'o': output_url = strdup(optarg); break;
267 case 'b': input_url = strdup(optarg); break;
268 case 'm': mtu = atoi(optarg); break;
269 case 'M': rtt_multiplier = atoi(optarg); break;
270 case 's': secret = strdup(optarg); break;
271 case 'e': encryption_type = atoi(optarg); break;
272 case 'S': statsinterval = atoi(optarg); break;
273 case 'v': loglevel = (enum rist_log_level)atoi(optarg); break;
274 case 'p': profile = (enum rist_profile)atoi(optarg); break;
275 case '1': single_port = 1; break;
276 #if HAVE_SRP_SUPPORT
277 case 'F': srpfile = strdup(optarg); break;
278 #endif
279 case 'h':
280 default:
281 usage(argv[0]);
282 }
283 }
284
285 if (!local_ip_cidr)
286 usage(argv[0]);
287 if (single_port) {
288 /* one connection only: exactly one of -o (caller) / -b (listener) */
289 if ((!output_url && !input_url) || (output_url && input_url)) {
290 fprintf(stderr, "single-port mode: specify exactly one of "
291 "-o (caller) or -b (listener)\n");
292 usage(argv[0]);
293 }
294 if (profile == RIST_PROFILE_SIMPLE) {
295 fprintf(stderr, "single-port mode requires main or advanced "
296 "profile (OOB is unavailable in simple profile)\n");
297 usage(argv[0]);
298 }
299 } else if (!output_url || !input_url) {
300 usage(argv[0]);
301 }
302
303 /* Append secret/encryption to whichever RIST URLs are present */
304 if (secret) {
305 char url_suffix[512];
306 int enc = encryption_type > 0 ? encryption_type : 128;
307 if (output_url) {
308 snprintf(url_suffix, sizeof(url_suffix), "%csecret=%s&aes-type=%d",
309 strchr(output_url, '?') ? '&' : '?', secret, enc);
310 char *new_out = malloc(strlen(output_url) + strlen(url_suffix) + 1);
311 if (!new_out) {
312 fprintf(stderr, "Out of memory\n");
313 exitcode = 1;
314 goto cleanup;
315 }
316 sprintf(new_out, "%s%s", output_url, url_suffix);
317 free(output_url);
318 output_url = new_out;
319 }
320 if (input_url) {
321 snprintf(url_suffix, sizeof(url_suffix), "%csecret=%s&aes-type=%d",
322 strchr(input_url, '?') ? '&' : '?', secret, enc);
323 char *new_in = malloc(strlen(input_url) + strlen(url_suffix) + 1);
324 if (!new_in) {
325 fprintf(stderr, "Out of memory\n");
326 exitcode = 1;
327 goto cleanup;
328 }
329 sprintf(new_in, "%s%s", input_url, url_suffix);
330 free(input_url);
331 input_url = new_in;
332 }
333 }
334
335 /* Parse IP/prefix */
336 char ip_addr[64];
337 int prefix_len;
338 if (parse_ip_prefix(local_ip_cidr, ip_addr, sizeof(ip_addr), &prefix_len) != 0) {
339 fprintf(stderr, "Invalid IP/prefix: %s\n", local_ip_cidr);
340 exitcode = 1;
341 goto cleanup;
342 }
343
344 /* Signal handling */
345 signal(SIGINT, sig_handler);
346 signal(SIGTERM, sig_handler);
347 #ifdef SIGPIPE
348 signal(SIGPIPE, SIG_IGN);
349 #endif
350
351 /* Logging. Point log_ptr at the static settings like the other tools:
352 * rist_logging_set() only allocates a new struct when *log_ptr is NULL,
353 * otherwise it configures the pointed-to settings in place. Leaving it
354 * uninitialised let a garbage non-NULL value through and caused a write
355 * to an invalid address (intermittent SIGSEGV at startup). */
356 struct rist_logging_settings *log_ptr = &logging_settings;
357 if (rist_logging_set(&log_ptr, loglevel, NULL, NULL, NULL, stderr) != 0) {
358 fprintf(stderr, "Failed to setup logging\n");
359 exitcode = 1;
360 goto cleanup;
361 }
362
363 /* Open and configure TUN device (using librist TUN API) */
364 char actual_ifname[64] = {0};
365 tun_fd = rist_tun_open(interface_name, actual_ifname, sizeof(actual_ifname));
366 if (tun_fd < 0) {
367 fprintf(stderr, "Failed to open TUN device (are you root?)\n");
368 exitcode = 1;
369 goto cleanup;
370 }
371
372 if (rist_tun_set_mtu(actual_ifname, mtu) != 0 ||
373 rist_tun_set_ip(actual_ifname, ip_addr, prefix_len) != 0 ||
374 rist_tun_bring_up(actual_ifname) != 0) {
375 fprintf(stderr, "Failed to configure TUN interface %s\n", actual_ifname);
376 exitcode = 1;
377 goto cleanup;
378 }
379
380 rist_log(&logging_settings, RIST_LOG_INFO,
381 "TUN %s: %s/%d MTU %d\n", actual_ifname, ip_addr, prefix_len, mtu);
382
383 /* RIST sender (TUN → network): hand tun_fd to librist.
384 * Built when -o is present: always in two-port mode, and for the
385 * caller role in single-port mode. */
386 if (output_url) {
387 if (rist_sender_create(&sender_ctx, profile, 0, log_ptr) != 0) {
388 fprintf(stderr, "Failed to create RIST sender\n");
389 exitcode = 1;
390 goto cleanup;
391 }
392 if (rtt_multiplier > 0)
393 rist_recovery_rtt_multiplier_set(sender_ctx, rtt_multiplier);
394
395 rist_sender_data_fd_set(sender_ctx, tun_fd, (size_t)mtu, RIST_DATA_FD_FLAG_TUN);
396
397 struct rist_peer_config *sender_peer_cfg = NULL;
398 if (rist_parse_address2(output_url, &sender_peer_cfg) != 0) {
399 fprintf(stderr, "Failed to parse sender URL: %s\n", output_url);
400 exitcode = 1;
401 goto cleanup;
402 }
403 if (mtu < 1400 && sender_peer_cfg->split_mode == LIBRIST_SPLIT_MODE_OFF) {
404 rist_log(&logging_settings, RIST_LOG_INFO,
405 "MTU %d < 1400: auto-enabling split=half on sender\n", mtu);
406 sender_peer_cfg->split_mode = LIBRIST_SPLIT_MODE_HALF;
407 }
408 struct rist_peer *sender_peer = NULL;
409 if (rist_peer_create(sender_ctx, &sender_peer, sender_peer_cfg) != 0) {
410 fprintf(stderr, "Failed to create sender peer\n");
411 exitcode = 1;
412 goto cleanup;
413 }
414 #if HAVE_SRP_SUPPORT
415 /* Present SRP credentials supplied on the sender URL
416 * (?username=...&password=...), the same way ristsender does.
417 * Without this the credentials are parsed into the peer config but
418 * never handed to EAP, so authentication to an SRP-protected
419 * listener never starts. */
420 if (strlen(sender_peer_cfg->srp_username) > 0 &&
421 strlen(sender_peer_cfg->srp_password) > 0) {
422 if (rist_enable_eap_srp_2(sender_peer, sender_peer_cfg->srp_username,
423 sender_peer_cfg->srp_password, NULL, NULL) != 0) {
424 fprintf(stderr, "Failed to enable EAP-SRP on sender\n");
425 exitcode = 1;
426 goto cleanup;
427 }
428 }
429 #endif
430 if (statsinterval > 0)
431 rist_stats_callback_set(sender_ctx, statsinterval, cb_stats, NULL);
432 }
433
434 /* RIST receiver (network → TUN): hand tun_fd to librist.
435 * Built when -b is present: always in two-port mode, and for the
436 * listener role in single-port mode. */
437 if (input_url) {
438 if (rist_receiver_create(&receiver_ctx, profile, log_ptr) != 0) {
439 fprintf(stderr, "Failed to create RIST receiver\n");
440 exitcode = 1;
441 goto cleanup;
442 }
443 if (rtt_multiplier > 0)
444 rist_recovery_rtt_multiplier_set(receiver_ctx, rtt_multiplier);
445
446 rist_receiver_data_fd_set(receiver_ctx, tun_fd, RIST_DATA_FD_FLAG_TUN);
447
448 #if HAVE_SRP_SUPPORT
449 if (srpfile) {
450 if (rist_auth_handler_set(receiver_ctx, cb_auth_connect, cb_auth_disconnect, receiver_ctx) != 0) {
451 fprintf(stderr, "Failed to set auth handler\n");
452 exitcode = 1;
453 goto cleanup;
454 }
455 }
456 #endif
457
458 struct rist_peer_config *recv_peer_cfg = NULL;
459 if (rist_parse_address2(input_url, &recv_peer_cfg) != 0) {
460 fprintf(stderr, "Failed to parse receiver URL: %s\n", input_url);
461 exitcode = 1;
462 goto cleanup;
463 }
464 if (mtu < 1400 && recv_peer_cfg->merge_mode == LIBRIST_MERGE_MODE_OFF) {
465 rist_log(&logging_settings, RIST_LOG_INFO,
466 "MTU %d < 1400: auto-enabling merge=auto on receiver\n", mtu);
467 recv_peer_cfg->merge_mode = LIBRIST_MERGE_MODE_AUTO;
468 }
469 struct rist_peer *recv_peer = NULL;
470 if (rist_peer_create(receiver_ctx, &recv_peer, recv_peer_cfg) != 0) {
471 fprintf(stderr, "Failed to create receiver peer\n");
472 exitcode = 1;
473 goto cleanup;
474 }
475
476 #if HAVE_SRP_SUPPORT
477 /* Receiver leg acts as an SRP client when credentials are supplied on
478 * the input URL (a two-port client connecting out to a listener);
479 * otherwise it is the server that verifies callers against -F. */
480 if (strlen(recv_peer_cfg->srp_username) > 0 &&
481 strlen(recv_peer_cfg->srp_password) > 0) {
482 if (rist_enable_eap_srp_2(recv_peer, recv_peer_cfg->srp_username,
483 recv_peer_cfg->srp_password, NULL, NULL) != 0) {
484 fprintf(stderr, "Failed to enable EAP-SRP (receiver credentials)\n");
485 exitcode = 1;
486 goto cleanup;
487 }
488 } else if (srpfile) {
489 if (rist_enable_eap_srp_2(recv_peer, NULL, NULL, user_verifier_lookup, (void *)srpfile) != 0) {
490 fprintf(stderr, "Failed to enable EAP-SRP\n");
491 exitcode = 1;
492 goto cleanup;
493 }
494 }
495 #endif
496
497 if (statsinterval > 0)
498 rist_stats_callback_set(receiver_ctx, statsinterval, cb_stats, NULL);
499 }
500
501 /* single-port: enable the OOB channel that carries the reverse
502 * direction on the same socket. The caller installs a callback to
503 * inject inbound OOB into the TUN; the listener only needs OOB
504 * enabled so its reader thread can rist_oob_write() back. */
505 if (single_port) {
506 struct rist_ctx *oob_ctx = sender_ctx ? sender_ctx : receiver_ctx;
507 oob_callback_func_t oob_cb = sender_ctx ? sp_oob_to_tun : NULL;
508 if (rist_oob_callback_set(oob_ctx, oob_cb, &tun_fd) != 0) {
509 fprintf(stderr, "Failed to enable OOB channel for single-port mode\n");
510 exitcode = 1;
511 goto cleanup;
512 }
513 if (receiver_ctx) {
514 /* listener: prep the reverse path and capture the peer on connect */
515 sp_rev.ctx = receiver_ctx;
516 sp_rev.tun_fd = tun_fd;
517 sp_rev.mtu = mtu;
518 sp_rev.peer = NULL;
519 if (rist_auth_handler_set(receiver_ctx, sp_conn_cb, sp_disconn_cb, &sp_rev) != 0) {
520 fprintf(stderr, "Failed to set connect handler for single-port listener\n");
521 exitcode = 1;
522 goto cleanup;
523 }
524 }
525 }
526
527 /* Start whichever contexts exist — librist forwards internally */
528 if ((sender_ctx && rist_start(sender_ctx) != 0) ||
529 (receiver_ctx && rist_start(receiver_ctx) != 0)) {
530 fprintf(stderr, "Failed to start RIST\n");
531 exitcode = 1;
532 goto cleanup;
533 }
534
535 if (single_port)
536 rist_log(&logging_settings, RIST_LOG_INFO,
537 "Tunnel active (single-port %s): %s/%d via RIST %s\n",
538 sender_ctx ? "caller" : "listener", ip_addr, prefix_len,
539 output_url ? output_url : input_url);
540 else
541 rist_log(&logging_settings, RIST_LOG_INFO,
542 "Tunnel active: %s/%d via RIST (%s -> %s)\n",
543 ip_addr, prefix_len, input_url, output_url);
544
545 /* single-port listener: pump the local TUN -> OOB (reverse direction).
546 * The caller's reverse path is handled by the OOB receive callback. */
547 if (single_port && receiver_ctx) {
548 /* sp_rev was populated above; conn_cb fills sp_rev.peer on connect */
549 if (pthread_create(&sp_thread, NULL, sp_tun_to_oob, &sp_rev) != 0) {
550 fprintf(stderr, "Failed to start reverse OOB thread\n");
551 exitcode = 1;
552 goto cleanup;
553 }
554 sp_thread_started = 1;
555 }
556
557 /* Wait for signal — librist handles all forwarding internally */
558 while (keep_running) {
559 #if defined(__unix) || defined(__APPLE__)
560 sleep(1);
561 #else
562 Sleep(1000);
563 #endif
564 }
565
566 if (sp_thread_started) {
567 pthread_join(sp_thread, NULL);
568 sp_thread_started = 0;
569 }
570
571 /* Print stats (only for contexts that exist) */
572 if (sender_ctx) {
573 struct rist_data_fd_stats tx_stats;
574 rist_data_fd_stats_get(sender_ctx, &tx_stats);
575 rist_log(&logging_settings, RIST_LOG_INFO,
576 "Shutting down. TX: %lu pkts / %lu bytes\n",
577 (unsigned long)tx_stats.tx_packets,
578 (unsigned long)tx_stats.tx_bytes);
579 }
580 if (receiver_ctx) {
581 struct rist_data_fd_stats rx_stats;
582 rist_data_fd_stats_get(receiver_ctx, &rx_stats);
583 rist_log(&logging_settings, RIST_LOG_INFO,
584 "Shutting down. RX: %lu pkts / %lu bytes\n",
585 (unsigned long)rx_stats.rx_packets,
586 (unsigned long)rx_stats.rx_bytes);
587 }
588
589 cleanup:
590 if (sp_thread_started)
591 pthread_join(sp_thread, NULL);
592 if (receiver_ctx)
593 rist_destroy(receiver_ctx);
594 if (sender_ctx)
595 rist_destroy(sender_ctx);
596 if (tun_fd >= 0)
597 rist_tun_close(tun_fd);
598
599 rist_logging_unset_global();
600
601 free(local_ip_cidr);
602 free(interface_name);
603 free(output_url);
604 free(input_url);
605 free(secret);
606 #if HAVE_SRP_SUPPORT
607 free(srpfile);
608 #endif
609
610 return exitcode;
611 }