Debian Codegraph @

src/rist.c

raw symbols SCIP
1 /* librist. Copyright © 2019-2020 SipRadius LLC. All right reserved.
2 * Author: Daniele Lacamera <[email protected]>
3 * Author: Kuldeep Singh Dhaka <[email protected]>
4 * Author: Sergio Ammirata <[email protected]>
5 * Author: Gijs Peskens <[email protected]>
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 */
9
10 #define LIBRIST_INTERNAL 1
11 #include "rist-private.h"
12 #include "log-private.h"
13 #include "udp-private.h"
14 #include "vcs_version.h"
15 #include "rist-thread.h"
16 #include "proto/rist_time.h"
17 #include <librist/version.h>
18 #include "crypto/crypto-private.h"
19 #include "crypto/psk.h"
20 #if HAVE_SRP_SUPPORT
21 #include "proto/eap.h"
22 #endif
23 #include <assert.h>
24 #include "librist/tun.h"
25 #include "librist/tunnel.h"
26 #ifdef _WIN32
27 #include <processthreadsapi.h>
28 #else
29 #include <unistd.h>
30 #include <poll.h>
31 #endif
32
33 #undef RIST_DEPRECATED
34
35 /* Receiver functions */
36
37 int rist_receiver_create(struct rist_ctx **_ctx, enum rist_profile profile,
38 struct rist_logging_settings *logging_settings)
39 {
40 struct rist_ctx *rist_ctx = calloc(1, sizeof(*rist_ctx));
41 if (!rist_ctx)
42 {
43 rist_log_priv2(logging_settings, RIST_LOG_ERROR, "Could not create ctx object, OOM!\n");
44 return -1;
45 }
46 if (profile == RIST_PROFILE_ADVANCED)
47 {
48 rist_log_priv2(logging_settings, RIST_LOG_INFO, "Using Advanced profile (VSF TR-06-3)\n");
49 }
50 struct rist_receiver *ctx = calloc(1, sizeof(*ctx));
51 if (!ctx)
52 {
53 rist_log_priv2(logging_settings, RIST_LOG_ERROR, "Could not create ctx object, OOM!\n");
54 free(rist_ctx);
55 return -1;
56 }
57 rist_ctx->mode = RIST_RECEIVER_MODE;
58 rist_ctx->receiver_ctx = ctx;
59 ctx->receiver_data_fd = -1;
60 ctx->id = (intptr_t)ctx;
61 if (init_common_ctx(&ctx->common, profile))
62 goto fail;
63
64 ctx->common.recovery_rtt_multiplier = 7;
65 ctx->common.logging_settings = logging_settings;
66 ctx->common.stats_report_time = (uint64_t)1000 * (uint64_t)RIST_CLOCK;
67 ctx->fifo_queue_size = RIST_DATAOUT_QUEUE_BUFFERS;
68 rist_log_priv(&ctx->common, RIST_LOG_INFO, "RIST Receiver Library version:%s \n", LIBRIST_VERSION);
69
70 if (logging_settings && logging_settings->log_level == RIST_LOG_SIMULATE)
71 {
72 ctx->simulate_loss = true;
73 ctx->loss_percentage = 1;//Actually 0.1%
74 rist_log_priv(&ctx->common, RIST_LOG_WARN, "RIST receiver has been configured with self-imposed (outgoing) packet loss (0.1%%)\n");
75 }
76
77 ctx->common.receiver_id = ctx->id;
78 ctx->common.sender_id = 0;
79
80 if (logging_settings && logging_settings->log_level >= RIST_LOG_DEBUG)
81 ctx->common.debug = true;
82
83 rist_log_priv(&ctx->common, RIST_LOG_INFO, "Starting in receiver mode\n");
84
85 int ret = pthread_cond_init(&ctx->condition, NULL);
86 if (ret)
87 {
88 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Error %d calling pthread_cond_init\n", ret);
89 goto fail;
90 }
91 ret = pthread_mutex_init(&ctx->mutex, NULL);
92 if (ret)
93 {
94 pthread_cond_destroy(&ctx->condition);
95 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Error %d calling pthread_mutex_init\n", ret);
96 goto fail;
97 }
98
99 *_ctx = rist_ctx;
100
101 return 0;
102
103 fail:
104 free(ctx);
105 free(rist_ctx);
106 ctx = NULL;
107 return -1;
108 }
109
110 int rist_receiver_nack_type_set(struct rist_ctx *rist_ctx, enum rist_nack_type nack_type)
111 {
112 if (RIST_UNLIKELY(!rist_ctx))
113 {
114 rist_log_priv3(RIST_LOG_ERROR, "ctx is null on rist_receiver_nack_type_set call!\n");
115 return -1;
116 }
117 if (RIST_UNLIKELY(rist_ctx->mode != RIST_RECEIVER_MODE || !rist_ctx->receiver_ctx))
118 {
119 rist_log_priv3(RIST_LOG_ERROR, "rist_receiver_nack_type_set call with CTX not set up for receiving\n");
120 return -1;
121 }
122 struct rist_receiver *ctx = rist_ctx->receiver_ctx;
123 ctx->nack_type = nack_type;
124 return 0;
125 }
126
127 /* Find the flow with the most queued data. Caller MUST hold
128 * ctx->common.flows_lock on entry; the lock remains held on return so
129 * the returned pointer stays valid until the caller releases it. */
130 static struct rist_flow *rist_get_longest_flow_locked(struct rist_receiver *ctx, ssize_t *num)
131 {
132 ssize_t num_loop = 0;
133 struct rist_flow *f = NULL;
134 struct rist_flow *f_loop = ctx->common.FLOWS;
135 while (f_loop) {
136 struct rist_flow *nextflow = f_loop->next;
137 unsigned long reader_index = atomic_load_explicit(&f_loop->dataout_fifo_queue_read_index, memory_order_relaxed);
138 unsigned long write_index = atomic_load_explicit(&f_loop->dataout_fifo_queue_write_index, memory_order_acquire);
139
140 num_loop = (write_index - reader_index)&(ctx->fifo_queue_size -1);
141 if (num_loop > *num)
142 {
143 f = f_loop;
144 *num = num_loop;
145 }
146 f_loop = nextflow;
147 }
148 return f;
149 }
150
151 int rist_receiver_data_read(struct rist_ctx *ctx, const struct rist_data_block **data_block, int timeout)
152 {
153 return rist_receiver_data_read2(ctx, (struct rist_data_block **)data_block, timeout);
154 }
155
156 int rist_receiver_data_read2(struct rist_ctx *rist_ctx, struct rist_data_block **data_buffer, int timeout)
157 {
158 if (RIST_UNLIKELY(!rist_ctx))
159 {
160 rist_log_priv3(RIST_LOG_ERROR, "ctx is null on rist_receiver_data_read call!\n");
161 return -1;
162 }
163 if (RIST_UNLIKELY(rist_ctx->mode != RIST_RECEIVER_MODE || !rist_ctx->receiver_ctx))
164 {
165 rist_log_priv3(RIST_LOG_ERROR, "rist_receiver_data_read call with CTX not set up for receiving\n");
166 return -2;
167 }
168
169 struct rist_receiver *ctx = rist_ctx->receiver_ctx;
170
171 struct rist_data_block *data_block = NULL;
172
173 ssize_t num = 0;
174 /* Hold flows_lock across the flow lookup and data read to prevent
175 * rist_delete_flow from freeing the flow underneath us. */
176 pthread_mutex_lock(&ctx->common.flows_lock);
177 struct rist_flow *f = rist_get_longest_flow_locked(ctx, &num);
178 if (!num && timeout > 0)
179 {
180 pthread_mutex_unlock(&ctx->common.flows_lock);
181 pthread_mutex_lock(&(ctx->mutex));
182 pthread_cond_timedwait_ms(&(ctx->condition), &(ctx->mutex), timeout);
183 pthread_mutex_unlock(&(ctx->mutex));
184 num = 0;
185 pthread_mutex_lock(&ctx->common.flows_lock);
186 f = rist_get_longest_flow_locked(ctx, &num);
187 }
188
189 if (RIST_UNLIKELY(!num || !f))
190 {
191 pthread_mutex_unlock(&ctx->common.flows_lock);
192 return 0;
193 }
194
195 /* Lock the flow's own mutex while still holding flows_lock, then
196 * release flows_lock. This guarantees the flow pointer is valid
197 * when we dereference it: rist_delete_flow takes flows_lock before
198 * unlinking and freeing the flow. */
199 pthread_mutex_lock(&f->mutex);
200 pthread_mutex_unlock(&ctx->common.flows_lock);
201
202 unsigned long dataout_read_index = atomic_load_explicit(&f->dataout_fifo_queue_read_index, memory_order_relaxed);
203 size_t write_index = atomic_load_explicit(&f->dataout_fifo_queue_write_index, memory_order_acquire);
204 if (write_index != dataout_read_index)
205 {
206 do {
207 num = (atomic_load_explicit(&f->dataout_fifo_queue_write_index, memory_order_acquire) - dataout_read_index) &(ctx->fifo_queue_size -1);
208 if (atomic_compare_exchange_weak(&f->dataout_fifo_queue_read_index, &dataout_read_index, (dataout_read_index +1)&(ctx->fifo_queue_size -1)))
209 {
210 data_block = f->dataout_fifo_queue[dataout_read_index];
211 f->dataout_fifo_queue[dataout_read_index] = NULL;
212 break;
213 }
214 } while (num > 0);
215 }
216 bool overflow = atomic_load_explicit(&f->fifo_overflow, memory_order_relaxed);
217 pthread_mutex_unlock(&f->mutex);
218
219 if (data_block == NULL && num > 0)
220 {
221 rist_log_priv3(RIST_LOG_ERROR, "[rist_receiver_data_read2] data_block is NULL but num > 0 ! num=%zd, dataout_read_index=%lu, write_index=%zu\n", num, dataout_read_index, write_index);
222 return -3;
223 }
224
225 *data_buffer = data_block;
226
227 if (RIST_UNLIKELY(overflow))
228 data_block->flags |= RIST_DATA_FLAGS_OVERFLOW;
229
230 return (int)num;
231 }
232
233 void rist_receiver_data_block_free(struct rist_data_block **const block)
234 {
235 rist_receiver_data_block_free2((struct rist_data_block **)block);
236 }
237
238 void rist_receiver_data_block_free2(struct rist_data_block **block)
239 {
240 /* free(NULL)-style no-op: *block may already be NULL on the data_fd
241 * delivery path (free_data_block nulls it). Mirror its guard. */
242 if (block == NULL)
243 return;
244 struct rist_data_block *b = *block;
245 if (b == NULL)
246 return;
247 if (b->ref != NULL)
248 free_data_block(block);
249 }
250
251 uint32_t rist_flow_id_create(void)
252 {
253 uint32_t u32 = prand_u32();
254 u32 &= ~(1UL << 0);
255
256 return u32;
257 }
258
259 int rist_receiver_data_notify_fd_set(struct rist_ctx *rist_ctx, int fd)
260 {
261 if (RIST_UNLIKELY(!rist_ctx))
262 {
263 rist_log_priv3(RIST_LOG_ERROR, "ctx is null on rist_receiver_data_notify_fd_set call!\n");
264 return -1;
265 }
266 if (RIST_UNLIKELY(rist_ctx->mode != RIST_RECEIVER_MODE || !rist_ctx->receiver_ctx))
267 {
268 rist_log_priv3(RIST_LOG_ERROR, "rist_receiver_data_notify_fd_set call with CTX not set up for receiving\n");
269 return -1;
270 }
271 struct rist_receiver *ctx = rist_ctx->receiver_ctx;
272 ctx->receiver_data_ready_notify_fd = fd;
273 return 0;
274 }
275
276 int rist_connection_status_callback_set(struct rist_ctx *ctx, connection_status_callback_t connection_status_callback,
277 void *arg)
278 {
279 if (RIST_UNLIKELY(!ctx))
280 {
281 rist_log_priv3(RIST_LOG_ERROR, "ctx is null on rist_connection_status_callback_set call!\n");
282 return -1;
283 }
284 struct rist_common_ctx *cctx = NULL;
285 if (ctx->mode == RIST_RECEIVER_MODE && ctx->receiver_ctx) {
286 cctx = &ctx->receiver_ctx->common;
287 }
288 else if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx) {
289 cctx = &ctx->sender_ctx->common;
290 }
291 else {
292 rist_log_priv3(RIST_LOG_ERROR, "Unknown error in rist_connection_status_callback_set call!\n");
293 return -1;
294 }
295 cctx->connection_status_callback = connection_status_callback;
296 cctx->connection_status_callback_argument = arg;
297 return 0;
298 }
299
300 int rist_receiver_data_callback_set(struct rist_ctx *rist_ctx,receiver_data_callback_t data_callback, void *arg)
301 {
302 return rist_receiver_data_callback_set2(
303 rist_ctx, (receiver_data_callback2_t)data_callback, arg);
304 }
305
306 int rist_receiver_data_callback_set2(struct rist_ctx *rist_ctx,
307 receiver_data_callback2_t data_callback,
308 void *arg) {
309 if (RIST_UNLIKELY(!rist_ctx)) {
310 rist_log_priv3(RIST_LOG_ERROR,
311 "ctx is null on rist_receiver_data_callback_set call!\n");
312 return -1;
313 }
314 if (RIST_UNLIKELY(rist_ctx->mode != RIST_RECEIVER_MODE ||
315 !rist_ctx->receiver_ctx)) {
316 rist_log_priv3(RIST_LOG_ERROR, "rist_receiver_data_callback_set call with "
317 "CTX not set up for receiving\n");
318 return -1;
319 }
320 struct rist_receiver *ctx = rist_ctx->receiver_ctx;
321 ctx->receiver_data_callback = data_callback;
322 ctx->receiver_data_callback_argument = arg;
323 return 0;
324 }
325
326 int rist_receiver_session_timeout_callback_set(struct rist_ctx *rist_ctx,
327 receiver_session_timeout_callback_t session_timeout_callback,
328 void *arg) {
329 if (RIST_UNLIKELY(!rist_ctx)) {
330 rist_log_priv3(RIST_LOG_ERROR,
331 "ctx is null on rist_session_timeout_callback_set call!\n");
332 return -1;
333 }
334 if (RIST_UNLIKELY(rist_ctx->mode != RIST_RECEIVER_MODE ||
335 !rist_ctx->receiver_ctx)) {
336 rist_log_priv3(RIST_LOG_ERROR, "rist_receiver_session_timeout_callback_set call with "
337 "CTX not set up for receiving\n");
338 return -1;
339 }
340 struct rist_receiver *ctx = rist_ctx->receiver_ctx;
341 ctx->receiver_session_timeout_callback = session_timeout_callback;
342 ctx->receiver_session_timeout_callback_argument = arg;
343 return 0;
344 }
345
346 int rist_receiver_flow_attr_callback_set(struct rist_ctx *rist_ctx,
347 receiver_flow_attr_callback_t cb,
348 void *arg)
349 {
350 if (RIST_UNLIKELY(!rist_ctx)) {
351 rist_log_priv3(RIST_LOG_ERROR,
352 "ctx is null on rist_receiver_flow_attr_callback_set call!\n");
353 return -1;
354 }
355 if (RIST_UNLIKELY(rist_ctx->mode != RIST_RECEIVER_MODE ||
356 !rist_ctx->receiver_ctx)) {
357 rist_log_priv3(RIST_LOG_ERROR, "rist_receiver_flow_attr_callback_set call with "
358 "CTX not set up for receiving\n");
359 return -1;
360 }
361 struct rist_receiver *ctx = rist_ctx->receiver_ctx;
362 ctx->receiver_flow_attr_callback = cb;
363 ctx->receiver_flow_attr_callback_argument = arg;
364 return 0;
365 }
366
367 /* Sender functions */
368 int rist_sender_create(struct rist_ctx **_ctx, enum rist_profile profile,
369 uint32_t flow_id, struct rist_logging_settings *logging_settings)
370 {
371 int ret;
372
373 if (profile == RIST_PROFILE_ADVANCED)
374 {
375 rist_log_priv2(logging_settings, RIST_LOG_INFO, "Using Advanced profile (VSF TR-06-3)\n");
376 }
377
378 if (flow_id % 2 != 0)
379 {
380 rist_log_priv2(logging_settings, RIST_LOG_ERROR, "Flow ID must be an even number!\n");
381 return -1;
382 }
383
384 struct rist_ctx *rist_ctx = calloc(1, sizeof(*rist_ctx));
385 if (!rist_ctx)
386 {
387 rist_log_priv2(logging_settings, RIST_LOG_ERROR, "Could not create ctx object, OOM!\n");
388 return -1;
389 }
390 struct rist_sender *ctx = calloc(1, sizeof(*ctx));
391 if (!ctx)
392 {
393 rist_log_priv2(logging_settings, RIST_LOG_ERROR, "Could not create ctx object, OOM!\n");
394 free(rist_ctx);
395 return -1;
396 }
397 rist_ctx->mode = RIST_SENDER_MODE;
398 rist_ctx->sender_ctx = ctx;
399 ctx->data_fd = -1;
400
401 ctx->id = (intptr_t)ctx;
402 if (init_common_ctx(&ctx->common, profile))
403 {
404 free(ctx);
405 free(rist_ctx);
406 ctx = NULL;
407 return -1;
408 }
409
410 ctx->common.recovery_rtt_multiplier = 7;
411 ctx->common.logging_settings = logging_settings;
412 ctx->common.stats_report_time = (uint64_t)1000 * (uint64_t)RIST_CLOCK;
413 //ctx->common.seq = 9159579;
414 //ctx->common.seq = RIST_SERVER_QUEUE_BUFFERS - 25000;
415
416 if (!ctx->sender_retry_queue)
417 {
418 ctx->sender_retry_queue = calloc(RIST_RETRY_QUEUE_BUFFERS, sizeof(*ctx->sender_retry_queue));
419 if (RIST_UNLIKELY(!ctx->sender_retry_queue))
420 {
421 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Could not create sender retry buffer of size %u MB, OOM\n",
422 (unsigned)(RIST_RETRY_QUEUE_BUFFERS * sizeof(ctx->sender_retry_queue[0])) / 1000000);
423 ret = -1;
424 goto free_ctx_and_ret;
425 }
426
427 ctx->sender_retry_queue_write_index = 1;
428 ctx->sender_retry_queue_read_index = 0;
429 ctx->sender_retry_queue_size = RIST_RETRY_QUEUE_BUFFERS;
430 }
431
432 ctx->sender_queue_delete_index = 1;
433 /* Advanced uses the (runtime-tunable) configured capacity; Simple/Main
434 * keep the default ring. Heap-allocated so the size can be tuned via
435 * rist_recovery_depth_set() before rist_start(). */
436 ctx->sender_queue_max = (ctx->common.profile == RIST_PROFILE_ADVANCED)
437 ? ctx->common.recovery_queue_max : RIST_SERVER_QUEUE_BUFFERS;
438 ctx->sender_queue = calloc(ctx->sender_queue_max, sizeof(*ctx->sender_queue));
439 ctx->seq_index = calloc(ctx->sender_queue_max, sizeof(*ctx->seq_index));
440 if (RIST_UNLIKELY(!ctx->sender_queue || !ctx->seq_index))
441 {
442 rist_log_priv(&ctx->common, RIST_LOG_ERROR,
443 "Could not allocate sender recovery ring (%zu entries), OOM\n",
444 ctx->sender_queue_max);
445 ret = -1;
446 goto free_ctx_and_ret;
447 }
448 /* Advanced sender keeps a parallel 16-bit-RTP retransmit index so a
449 * peer that negotiated down to Main (which NACKs in the RTP domain) can
450 * still be served. Main/Simple senders index by RTP directly already. */
451 if (ctx->common.profile == RIST_PROFILE_ADVANCED) {
452 ctx->seq_rtp_index = calloc((size_t)UINT16_MAX + 1, sizeof(*ctx->seq_rtp_index));
453 if (RIST_UNLIKELY(!ctx->seq_rtp_index)) {
454 rist_log_priv(&ctx->common, RIST_LOG_ERROR,
455 "Could not allocate sender RTP recovery index, OOM\n");
456 ret = -1;
457 goto free_ctx_and_ret;
458 }
459 }
460 atomic_init(&ctx->sender_queue_write_index, 1);
461 atomic_init(&ctx->sender_queue_read_index, 0);
462
463 rist_log_priv(&ctx->common, RIST_LOG_INFO, "RIST Sender Library %s\n", LIBRIST_VERSION);
464
465 ctx->common.sender_id = ctx->id;
466 ctx->common.receiver_id = 0;
467
468 if (logging_settings && logging_settings->log_level == RIST_LOG_SIMULATE)
469 {
470 ctx->simulate_loss = true;
471 ctx->loss_percentage = 1;//Actually 0.1%
472 rist_log_priv(&ctx->common, RIST_LOG_WARN, "RIST Sender has been configured with self-imposed (outgoing) packet loss (0.1%%)\n");
473 }
474
475 if (logging_settings && logging_settings->log_level >= RIST_LOG_DEBUG)
476 {
477 ctx->common.debug = true;
478 }
479
480 if (flow_id == 0)
481 flow_id = rist_flow_id_create();
482
483 ctx->adv_flow_id = flow_id;
484
485 ret = pthread_cond_init(&ctx->condition, NULL);
486 if (ret)
487 {
488 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Error %d initializing pthread_condition\n", ret);
489 goto free_ctx_and_ret;
490 }
491
492 ret = pthread_mutex_init(&ctx->mutex, NULL);
493 if (ret)
494 {
495 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Error %d initializing pthread_mutex\n", ret);
496 goto free_ctx_and_ret;
497 }
498
499 ret = pthread_mutex_init(&ctx->queue_lock, NULL);
500 if (ret)
501 {
502 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Error %d initializing pthread_mutex\n", ret);
503 goto free_ctx_and_ret;
504 }
505
506 ctx->sender_initialized = true;
507
508 *_ctx = rist_ctx;
509 return 0;
510
511 // Failed!
512 free_ctx_and_ret:
513 if (ctx) {
514 free(ctx->sender_retry_queue);
515 free(ctx->sender_queue);
516 free(ctx->seq_index);
517 free(ctx->seq_rtp_index);
518 }
519 free(ctx);
520 free(rist_ctx);
521 return ret;
522 }
523
524 int rist_sender_flow_id_get(struct rist_ctx *rist_ctx, uint32_t *flow_id)
525 {
526 if (RIST_UNLIKELY(!rist_ctx))
527 {
528 rist_log_priv3(RIST_LOG_ERROR, "rist_sender_flow_id_get call with null context");
529 return -1;
530 }
531 if (RIST_UNLIKELY(rist_ctx->mode != RIST_SENDER_MODE || !rist_ctx->sender_ctx))
532 {
533 rist_log_priv3(RIST_LOG_ERROR, "rist_sender_flow_id_get call with ctx not set up for sending\n");
534 return -1;
535 }
536 struct rist_sender *ctx = rist_ctx->sender_ctx;
537 *flow_id = ctx->adv_flow_id;
538 return 0;
539 }
540
541 int rist_sender_npd_get(const struct rist_ctx *rist_ctx, bool *npd)
542 {
543 if (RIST_UNLIKELY(!rist_ctx))
544 {
545 rist_log_priv3(RIST_LOG_ERROR, "rist_sender_npd_get call with null context");
546 return -1;
547 }
548 if (RIST_UNLIKELY(rist_ctx->mode != RIST_SENDER_MODE || !rist_ctx->sender_ctx))
549 {
550 rist_log_priv3(RIST_LOG_ERROR, "rist_sender_npd_get call with ctx not set up for sending\n");
551 return -1;
552 }
553 const struct rist_sender *ctx = rist_ctx->sender_ctx;
554 *npd = ctx->null_packet_suppression;
555 return 0;
556 }
557
558 int rist_sender_npd_enable(struct rist_ctx *rist_ctx)
559 {
560 if (RIST_UNLIKELY(!rist_ctx))
561 {
562 rist_log_priv3(RIST_LOG_ERROR, "rist_sender_npd_enable call with null context");
563 return -1;
564 }
565 if (RIST_UNLIKELY(rist_ctx->mode != RIST_SENDER_MODE || !rist_ctx->sender_ctx))
566 {
567 rist_log_priv3(RIST_LOG_ERROR, "rist_sender_npd_enable call with ctx not set up for sending\n");
568 return -1;
569 }
570 struct rist_sender *ctx = rist_ctx->sender_ctx;
571 ctx->null_packet_suppression = true;
572 rist_log_priv2(ctx->common.logging_settings, RIST_LOG_INFO, "Enabled NULL Packet deletion\n");
573 return 0;
574 }
575
576 int rist_sender_npd_disable(struct rist_ctx *rist_ctx)
577 {
578 if (RIST_UNLIKELY(!rist_ctx))
579 {
580 rist_log_priv3(RIST_LOG_ERROR, "rist_sender_npd_disable call with null context");
581 return -1;
582 }
583 if (RIST_UNLIKELY(rist_ctx->mode != RIST_SENDER_MODE || !rist_ctx->sender_ctx))
584 {
585 rist_log_priv3(RIST_LOG_ERROR, "rist_sender_npd_disable call with ctx not set up for sending\n");
586 return -1;
587 }
588 struct rist_sender *ctx = rist_ctx->sender_ctx;
589 ctx->null_packet_suppression = false;
590 rist_log_priv2(ctx->common.logging_settings, RIST_LOG_INFO, "Disabled NULL Packet deletion\n");
591 return 0;
592 }
593
594 int rist_sender_flow_id_set(struct rist_ctx *rist_ctx, uint32_t flow_id)
595 {
596 if (RIST_UNLIKELY(!rist_ctx))
597 {
598 rist_log_priv3(RIST_LOG_ERROR, "rist_sender_flow_id_set call with null context");
599 return -1;
600 }
601 if (RIST_UNLIKELY(rist_ctx->mode != RIST_SENDER_MODE || !rist_ctx->sender_ctx))
602 {
603 rist_log_priv3(RIST_LOG_ERROR, "rist_sender_flow_id_set call with ctx not set up for sending\n");
604 return -1;
605 }
606 struct rist_sender *ctx = rist_ctx->sender_ctx;
607 //Make sure LSB = 0
608 flow_id &= ~1UL;
609 ctx->adv_flow_id = flow_id;
610 for (size_t i = 0; i < ctx->peer_lst_len; i++)
611 {
612 ctx->peer_lst[i]->peer_ssrc = ctx->peer_lst[i]->adv_flow_id = flow_id;
613 }
614 return 0;
615 }
616
617 int rist_sender_data_write(struct rist_ctx *rist_ctx, const struct rist_data_block *data_block)
618 {
619 if (RIST_UNLIKELY(!rist_ctx))
620 {
621 rist_log_priv3(RIST_LOG_ERROR, "rist_sender_data_write call with null context\n");
622 return -1;
623 }
624 if (RIST_UNLIKELY(rist_ctx->mode != RIST_SENDER_MODE || !rist_ctx->sender_ctx))
625 {
626 rist_log_priv3(RIST_LOG_ERROR, "rist_sender_data_write call with ctx not set up for sending\n");
627 return -1;
628 }
629 struct rist_sender *ctx = rist_ctx->sender_ctx;
630 // max protocol overhead for data is gre-header plus gre-reduced-mode-header plus rtp-header
631 // 16 + 4 + 12 = 32
632
633 if (data_block->payload_len <= 0 || data_block->payload_len > (RIST_MAX_PACKET_SIZE - 32))
634 {
635 rist_log_priv(&ctx->common, RIST_LOG_ERROR,
636 "Dropping pipe packet of size %d, max is %d.\n", data_block->payload_len, RIST_MAX_PACKET_SIZE - 32);
637 return -1;
638 }
639
640 if (RIST_UNLIKELY((data_block->flags & RIST_DATA_FLAGS_USE_SEQ) &&
641 ctx->split_mode != LIBRIST_SPLIT_MODE_OFF)) {
642 rist_log_priv(&ctx->common, RIST_LOG_ERROR,
643 "RIST_DATA_FLAGS_USE_SEQ cannot be used with split mode; "
644 "the library must control the wire sequence for pair "
645 "splitting. Use ts_ntp to preserve source timestamps.\n");
646 return -1;
647 }
648
649 uint64_t ts_ntp = data_block->ts_ntp == 0 ? timestampNTP_u64() : data_block->ts_ntp;
650
651 const uint8_t *payload_to_send = data_block->payload;
652 size_t payload_len = data_block->payload_len;
653
654 bool actually_split = (ctx->split_mode != LIBRIST_SPLIT_MODE_OFF);
655 size_t first_len = 0;
656 size_t last_len = 0;
657 if (actually_split) {
658 if (ctx->split_mode == LIBRIST_SPLIT_MODE_AUTO) {
659 const uint8_t *p = payload_to_send;
660 if (payload_len >= 2 * 188 && payload_len % 188 == 0 && p[0] == 0x47) {
661 size_t ts_count = payload_len / 188;
662 size_t first_ts = ts_count / 2;
663 if (first_ts == 0)
664 first_ts = 1;
665 first_len = first_ts * 188;
666 last_len = payload_len - first_len;
667 } else {
668 ctx->stats_split_fallback_not_ts++;
669 first_len = payload_len / 2;
670 last_len = payload_len - first_len;
671 }
672 } else {
673 first_len = payload_len / 2;
674 last_len = payload_len - first_len;
675 }
676 }
677
678 uint32_t seq_rtp_first;
679 uint32_t seq_rtp_last = 0;
680 if (data_block->flags & RIST_DATA_FLAGS_USE_SEQ) {
681 seq_rtp_first = (uint32_t)data_block->seq;
682 } else if (actually_split) {
683 if (ctx->common.seq_rtp & 1)
684 ctx->common.seq_rtp++;
685 seq_rtp_first = ctx->common.seq_rtp++;
686 seq_rtp_last = ctx->common.seq_rtp++;
687 } else {
688 seq_rtp_first = ctx->common.seq_rtp++;
689 }
690 seq_rtp_first &= UINT16_MAX;
691 seq_rtp_last &= UINT16_MAX;
692
693 int ret;
694 if (actually_split) {
695 ret = rist_sender_enqueue(ctx, payload_to_send, first_len, ts_ntp,
696 data_block->virt_src_port,
697 data_block->virt_dst_port, seq_rtp_first);
698 if (ret == 0) {
699 ret = rist_sender_enqueue(ctx, payload_to_send + first_len, last_len,
700 ts_ntp, data_block->virt_src_port,
701 data_block->virt_dst_port, seq_rtp_last);
702 if (ret == 0) {
703 ctx->stats_pairs_emitted++;
704 } else {
705 rist_log_priv(&ctx->common, RIST_LOG_WARN,
706 "Split write: first half (seq %u) enqueued but second "
707 "half (seq %u) failed (ret=%d); receiver will see an "
708 "orphan.\n", seq_rtp_first, seq_rtp_last, ret);
709 }
710 }
711 } else {
712 ret = rist_sender_enqueue(ctx, payload_to_send, payload_len, ts_ntp,
713 data_block->virt_src_port,
714 data_block->virt_dst_port, seq_rtp_first);
715 }
716 // Wake up data/nack output thread when data comes in
717 if (pthread_cond_signal(&ctx->condition))
718 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Call to pthread_cond_signal failed.\n");
719
720 if (ret < 0)
721 return ret;
722 else
723 return (int)data_block->payload_len;
724 }
725
726 /* Shared OOB functions -> Tunneled IP packets within GRE */
727 int rist_oob_read(struct rist_ctx *ctx, const struct rist_oob_block **oob_block)
728 {
729 if (!ctx)
730 {
731 rist_log_priv3(RIST_LOG_ERROR, "ctx is null on rist_oob_read call!\n");
732 return -1;
733 }
734 if (!oob_block)
735 {
736 rist_log_priv3(RIST_LOG_ERROR, "oob_block is null on rist_oob_read call!\n");
737 return -1;
738 }
739 struct rist_common_ctx *cctx = rist_struct_get_common(ctx);
740 if (!cctx)
741 return -1;
742
743 *oob_block = NULL;
744
745 if (!cctx->oob_data_enabled)
746 {
747 rist_log_priv(cctx, RIST_LOG_ERROR,
748 "rist_oob_read called but oob data is not enabled; call rist_oob_callback_set first\n");
749 return -1;
750 }
751 if (cctx->oob_data_callback)
752 {
753 rist_log_priv(cctx, RIST_LOG_ERROR,
754 "rist_oob_read cannot be used while an oob callback is installed\n");
755 return -1;
756 }
757
758 return rist_oob_dequeue_rx(cctx, oob_block);
759 }
760
761 int rist_oob_write(struct rist_ctx *ctx, const struct rist_oob_block *oob_block)
762 {
763 if (RIST_UNLIKELY(!ctx))
764 {
765 rist_log_priv3(RIST_LOG_ERROR, "rist_oob_write call with null ctx!\n");
766 return -1;
767 }
768 struct rist_common_ctx *cctx = rist_struct_get_common(ctx);
769 if (RIST_UNLIKELY(!cctx))
770 return -1;
771 // max protocol overhead for data is gre-header, 16 max
772 if (oob_block->payload_len <= 0 || oob_block->payload_len > (RIST_MAX_PACKET_SIZE - 16))
773 {
774 rist_log_priv(cctx, RIST_LOG_ERROR,
775 "Dropping oob packet of size %d, max is %d.\n", oob_block->payload_len, RIST_MAX_PACKET_SIZE - 16);
776 return -1;
777 }
778 struct rist_peer *peer = oob_block->peer;
779 if (peer == NULL)
780 peer = cctx->oob_current_peer;
781 if (peer)
782 return rist_oob_enqueue(cctx, peer, oob_block->payload, oob_block->payload_len);
783 else
784 {
785 rist_log_priv(cctx, RIST_LOG_WARN,
786 "No oob peer, dropping packet of size %d\n", oob_block->payload_len);
787 return 0;
788 }
789 }
790
791 int rist_oob_callback_set(struct rist_ctx *ctx,
792 int (*oob_callback)(void *arg, const struct rist_oob_block *oob_block),
793 void *arg)
794 {
795 if (RIST_UNLIKELY(!ctx))
796 {
797 rist_log_priv3(RIST_LOG_ERROR, "rist_oob_callback_set call with null ctx!\n");
798 return -1;
799 }
800 struct rist_common_ctx *cctx = rist_struct_get_common(ctx);
801 if (RIST_UNLIKELY(!cctx))
802 return -1;
803 else if (cctx->profile == RIST_PROFILE_SIMPLE)
804 {
805 rist_log_priv(cctx, RIST_LOG_ERROR, "Out-of-band data is not support for simple profile\n");
806 return -1;
807 }
808 if (pthread_rwlock_init(&cctx->oob_queue_lock, NULL) != 0)
809 {
810 rist_log_priv(cctx, RIST_LOG_ERROR, "Failed to init ctx->common.oob_queue_lock\n");
811 return -1;
812 }
813 cctx->oob_data_enabled = true;
814 cctx->oob_data_callback = oob_callback;
815 cctx->oob_data_callback_argument = arg;
816 cctx->oob_queue_write_index = 0;
817 cctx->oob_queue_read_index = 0;
818 cctx->oob_rx_queue_write_index = 0;
819 cctx->oob_rx_queue_read_index = 0;
820 cctx->oob_rx_current = NULL;
821
822 return 0;
823 }
824
825 /* Shared functions */
826
827 int rist_jitter_max_set(struct rist_ctx *ctx, int t)
828 {
829 struct rist_common_ctx *cctx = rist_struct_get_common(ctx);
830 if (!cctx)
831 return -1;
832 return rist_max_jitter_set(cctx, t);
833 }
834
835 int rist_recovery_rtt_multiplier_set(struct rist_ctx *ctx, int multiplier)
836 {
837 struct rist_common_ctx *cctx = rist_struct_get_common(ctx);
838 if (!cctx)
839 return -1;
840 return rist_recovery_rtt_multiplier_set_internal(cctx, multiplier);
841 }
842
843 /* Translate a recovery-depth exponent into ring capacity in packets
844 * (65536 << depth), clamped to [MIN, MAX] and to the platform maximum
845 * (rist_recovery_depth_platform_max) so the result always fits size_t.
846 * Returned as uint64_t so the arithmetic is exact regardless of size_t width. */
847 static uint64_t rist_recovery_depth_to_packets(int depth)
848 {
849 if (depth < RIST_RECOVERY_DEPTH_MIN)
850 depth = RIST_RECOVERY_DEPTH_MIN;
851 if (depth > RIST_RECOVERY_DEPTH_MAX)
852 depth = RIST_RECOVERY_DEPTH_MAX;
853 int platform_max = rist_recovery_depth_platform_max();
854 if (depth > platform_max)
855 depth = platform_max;
856 return (uint64_t)UINT16_SIZE << depth;
857 }
858
859 /* Resize the context recovery ring to `packets` (already a power of two in
860 * [UINT16_SIZE, 2^32]). The caller MUST guarantee rist_start() has not
861 * completed, so the sender ring can be reallocated with no data in flight.
862 * Returns 0 on success, -2 on OOM / size unrepresentable on this platform. */
863 static int rist_recovery_depth_apply(struct rist_ctx *ctx, struct rist_common_ctx *cctx, uint64_t packets)
864 {
865 if (packets > (uint64_t)(SIZE_MAX / (sizeof(struct rist_buffer *) + sizeof(uint32_t)))) {
866 rist_log_priv(cctx, RIST_LOG_ERROR,
867 "Recovery depth too large to allocate on this platform (%llu packets)\n",
868 (unsigned long long)packets);
869 return -2;
870 }
871 size_t want = (size_t)packets;
872
873 if (cctx->profile != RIST_PROFILE_ADVANCED) {
874 cctx->recovery_queue_max = want;
875 rist_log_priv(cctx, RIST_LOG_INFO,
876 "Recovery depth request stored (%zu packets); only the Advanced "
877 "profile uses it (Simple/Main are 16-bit, capped at %u packets).\n",
878 want, (unsigned)UINT16_SIZE);
879 return 0;
880 }
881
882 /* The receiver allocates its ring per-flow and reads recovery_queue_max
883 * directly, so nothing more to do there. The sender allocates its ring
884 * at create time, so resize it now - safe because rist_start() has not
885 * run yet and no data is flowing. On OOM the old ring (and
886 * recovery_queue_max) is left untouched. */
887 if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx) {
888 struct rist_sender *sender = ctx->sender_ctx;
889 if (sender->sender_queue_max != want) {
890 struct rist_buffer **nq = calloc(want, sizeof(*nq));
891 uint32_t *ni = calloc(want, sizeof(*ni));
892 if (!nq || !ni) {
893 free(nq);
894 free(ni);
895 rist_log_priv(cctx, RIST_LOG_ERROR,
896 "OOM resizing sender recovery ring to %zu packets\n", want);
897 return -2;
898 }
899 free(sender->sender_queue);
900 free(sender->seq_index);
901 sender->sender_queue = nq;
902 sender->seq_index = ni;
903 sender->sender_queue_max = want;
904 sender->sender_queue_delete_index = 1;
905 atomic_store_explicit(&sender->sender_queue_write_index, 1, memory_order_release);
906 atomic_store_explicit(&sender->sender_queue_read_index, 0, memory_order_release);
907 }
908 }
909
910 cctx->recovery_queue_max = want;
911 rist_log_priv(cctx, RIST_LOG_INFO,
912 "Advanced recovery buffer set to %zu packets (NACK window ~%zu packets, "
913 "~%zu MB of index arrays per context)\n",
914 want, want / 2,
915 (want * (sizeof(struct rist_buffer *) + sizeof(uint32_t))) / (1024 * 1024));
916 return 0;
917 }
918
919 int rist_recovery_depth_set(struct rist_ctx *ctx, uint8_t depth)
920 {
921 struct rist_common_ctx *cctx = rist_struct_get_common(ctx);
922 if (!cctx)
923 return -1;
924
925 if (atomic_load_explicit(&cctx->startup_complete, memory_order_acquire)) {
926 rist_log_priv(cctx, RIST_LOG_ERROR,
927 "rist_recovery_depth_set must be called before rist_start()\n");
928 return -1;
929 }
930
931 if (depth > RIST_RECOVERY_DEPTH_MAX) {
932 rist_log_priv(cctx, RIST_LOG_WARN,
933 "recovery depth %u out of range, clamping to %u\n",
934 (unsigned)depth, (unsigned)RIST_RECOVERY_DEPTH_MAX);
935 depth = RIST_RECOVERY_DEPTH_MAX;
936 }
937
938 int platform_max = rist_recovery_depth_platform_max();
939 if (depth > platform_max) {
940 rist_log_priv(cctx, RIST_LOG_WARN,
941 "recovery depth %u exceeds what this platform can address, "
942 "capping to %d\n", (unsigned)depth, platform_max);
943 depth = platform_max;
944 }
945
946 return rist_recovery_depth_apply(ctx, cctx, rist_recovery_depth_to_packets(depth));
947 }
948
949 int rist_auth_handler_set(struct rist_ctx *ctx,
950 int (*conn_cb)(void *arg, const char *connecting_ip, uint16_t connecting_port, const char *local_ip, uint16_t local_port, struct rist_peer *peer),
951 int (*disconn_cb)(void *arg, struct rist_peer *peer),
952 void *arg)
953 {
954 if (RIST_UNLIKELY(!ctx))
955 {
956 rist_log_priv3(RIST_LOG_ERROR, "rist_auth_handler_set call with null ctx!\n");
957 return -1;
958 }
959 struct rist_common_ctx *cctx = rist_struct_get_common(ctx);
960 if (RIST_UNLIKELY(!cctx))
961 return -1;
962 return rist_auth_handler(cctx, conn_cb, disconn_cb, arg);
963 }
964
965 int rist_stats_free(const struct rist_stats *stats_container)
966 {
967 if (!stats_container)
968 return -1;
969 free(stats_container->stats_json);
970 if (stats_container->stats_type == RIST_STATS_RECEIVER_FLOW)
971 free(stats_container->stats.receiver_flow.peers);
972 free((void *)stats_container);
973 return 0;
974 }
975
976 uint32_t rist_peer_get_id(const struct rist_peer *peer)
977 {
978 if (!peer)
979 return 0;
980 return peer->adv_peer_id;
981 }
982
983 uint32_t rist_peer_get_cname(const struct rist_peer *peer, const char **cname)
984 {
985 if (peer && cname)
986 {
987 *cname = &peer->cname[0];
988 return (uint32_t)strnlen(*cname, sizeof(peer->cname));
989 }
990 else
991 return 0;
992 }
993
994 int rist_peer_config_free(const struct rist_peer_config **peer_config)
995 {
996 return rist_peer_config_free2((struct rist_peer_config **)peer_config);
997 }
998
999 int rist_peer_config_free2(struct rist_peer_config **peer_config)
1000 {
1001 if (*peer_config) {
1002 free((void *)*peer_config);
1003 *peer_config = NULL;
1004 }
1005 return 0;
1006 }
1007
1008 #if HAVE_SRP_SUPPORT
1009 int rist_peer_update_secret(struct rist_peer *peer, const char* password) {
1010 pthread_mutex_lock(&peer->peer_lock);
1011 size_t password_len = strlen(password);
1012 struct rist_key *inactive_key = (peer->key_rx_odd_active) ? &peer->key_tx : &peer->key_tx_odd;
1013 rist_log_priv(get_cctx(peer), RIST_LOG_INFO, "Updating passphrase for peer #%u\n", peer->adv_peer_id);
1014 _librist_crypto_psk_set_passphrase(inactive_key, (const uint8_t *)password, password_len);
1015 struct rist_peer *child = peer->child;
1016 while (child != NULL) {
1017 pthread_mutex_lock(&child->peer_lock);
1018 inactive_key = (child->key_rx_odd_active) ? &child->key_tx : &child->key_tx_odd;
1019 _librist_crypto_psk_set_passphrase(inactive_key, (const uint8_t *)password, password_len);
1020 child->rolling_over_passphrase = true;
1021 pthread_mutex_unlock(&child->peer_lock);
1022 rist_eap_send_passphrase(child->eap_ctx, password);
1023 child = child->sibling_next;
1024 }
1025 peer->rolling_over_passphrase = true;
1026 pthread_mutex_unlock(&peer->peer_lock);
1027 rist_eap_send_passphrase(peer->eap_ctx, password);
1028 return 0;
1029 }
1030 #endif
1031
1032 int rist_logging_settings_free(const struct rist_logging_settings **logging_settings)
1033 {
1034 return rist_logging_settings_free2((struct rist_logging_settings **)logging_settings);
1035 }
1036
1037 int rist_logging_settings_free2(struct rist_logging_settings **logging_settings)
1038 {
1039 if (*logging_settings) {
1040 rist_logging_unset_global_if_matches(*logging_settings);
1041 free((void *)*logging_settings);
1042 *logging_settings = NULL;
1043 }
1044 return 0;
1045 }
1046
1047 int rist_udp_config_free(const struct rist_udp_config **udp_config)
1048 {
1049 return rist_udp_config_free2((struct rist_udp_config **)udp_config);
1050 }
1051
1052 int rist_udp_config_free2(struct rist_udp_config **udp_config)
1053 {
1054 if (*udp_config) {
1055 free((void *)*udp_config);
1056 *udp_config = NULL;
1057 }
1058 return 0;
1059 }
1060
1061 int rist_sender_stats_callback_set(struct rist_ctx *ctx, int statsinterval, int (*stats_cb)(void *arg, uint16_t version, char *stats_json, uint32_t json_size), void *arg)
1062 {
1063 if (RIST_UNLIKELY(!ctx))
1064 {
1065 rist_log_priv3(RIST_LOG_ERROR, "rist_sender_stats_callback_set call with null ctx!\n");
1066 return -1;
1067 }
1068
1069 if (ctx->mode == RIST_SENDER_MODE && statsinterval != 0)
1070 {
1071 ctx->sender_ctx->sender_stats_callback = stats_cb;
1072 ctx->sender_ctx->sender_stats_callback_argument = arg;
1073 ctx->sender_ctx->stats_report_time = statsinterval * RIST_CLOCK;
1074 }
1075
1076 return 0;
1077 }
1078
1079 int rist_stats_callback_set(struct rist_ctx *ctx, int statsinterval, int (*stats_cb)(void *arg, const struct rist_stats *stats_container), void *arg)
1080 {
1081 if (RIST_UNLIKELY(!ctx))
1082 {
1083 rist_log_priv3(RIST_LOG_ERROR, "rist_stats_callback_set call with null ctx!\n");
1084 return -1;
1085 }
1086 struct rist_common_ctx *cctx = rist_struct_get_common(ctx);
1087 if (RIST_UNLIKELY(!cctx))
1088 return -1;
1089 pthread_mutex_lock(&cctx->stats_lock);
1090
1091 if (statsinterval != 0)
1092 {
1093 cctx->stats_callback = stats_cb;
1094 cctx->stats_callback_argument = arg;
1095 cctx->stats_report_time = statsinterval * RIST_CLOCK;
1096 if (ctx->mode == RIST_RECEIVER_MODE)
1097 {
1098 struct rist_flow *f = cctx->FLOWS;
1099 while (f)
1100 {
1101 f->stats_report_time = statsinterval * RIST_CLOCK;
1102 f = f->next;
1103 }
1104 }
1105 else if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx)
1106 {
1107 /* sender loop reads its own copy, not the common one */
1108 ctx->sender_ctx->stats_report_time = statsinterval * RIST_CLOCK;
1109 }
1110 }
1111 pthread_mutex_unlock(&cctx->stats_lock);
1112 return 0;
1113 }
1114
1115 /* Utility functions */
1116 const char *librist_version(void)
1117 {
1118 return LIBRIST_VERSION;
1119 }
1120
1121 const char *librist_api_version(void)
1122 {
1123 return STR(LIBRIST_API_VERSION_MAJOR)"."STR(LIBRIST_API_VERSION_MINOR)"."STR(LIBRIST_API_VERSION_PATCH);
1124 }
1125
1126 int rist_parse_udp_address(const char *url, const struct rist_udp_config **udp_config)
1127 {
1128 return rist_parse_udp_address2(url, (struct rist_udp_config **)udp_config);
1129 }
1130
1131 int rist_parse_udp_address2(const char *url, struct rist_udp_config **udp_config)
1132 {
1133
1134 int ret = 0;
1135 if (*udp_config == NULL)
1136 {
1137 // Default options on new struct (specific for udp url)
1138 struct rist_udp_config *output_udp_config = calloc(1, sizeof(struct rist_udp_config));
1139 if (!output_udp_config)
1140 return -1;
1141 output_udp_config->version = RIST_UDP_CONFIG_VERSION;
1142 output_udp_config->stream_id = 0; // Accept all on receiver, auto-generate on sender
1143 ret = parse_url_udp_options(url, output_udp_config);
1144 *udp_config = output_udp_config;
1145 }
1146 else
1147 {
1148 // Update incoming object with url data
1149 struct rist_udp_config *existing_udp_config = (void *)*udp_config;
1150 ret = parse_url_udp_options(url, existing_udp_config);
1151 *udp_config = existing_udp_config;
1152 }
1153
1154 return ret;
1155 }
1156
1157 int rist_peer_config_defaults_set_versioned(struct rist_peer_config *peer_config, int version)
1158 {
1159 if (peer_config)
1160 {
1161 peer_config->version = version;
1162 peer_config->virt_dst_port = RIST_DEFAULT_VIRT_DST_PORT;
1163 peer_config->recovery_mode = RIST_DEFAULT_RECOVERY_MODE;
1164 peer_config->recovery_maxbitrate = RIST_DEFAULT_RECOVERY_MAXBITRATE;
1165 peer_config->recovery_maxbitrate_return = RIST_DEFAULT_RECOVERY_MAXBITRATE_RETURN;
1166 peer_config->recovery_length_min = RIST_DEFAULT_RECOVERY_LENGTH_MIN;
1167 peer_config->recovery_length_max = RIST_DEFAULT_RECOVERY_LENGTH_MAX;
1168 peer_config->recovery_reorder_buffer = RIST_DEFAULT_RECOVERY_REORDER_BUFFER;
1169 peer_config->recovery_rtt_min = RIST_DEFAULT_RECOVERY_RTT_MIN;
1170 peer_config->recovery_rtt_max = RIST_DEFAULT_RECOVERY_RTT_MAX;
1171 peer_config->congestion_control_mode = RIST_DEFAULT_CONGESTION_CONTROL_MODE;
1172 peer_config->min_retries = RIST_DEFAULT_MIN_RETRIES;
1173 peer_config->max_retries = RIST_DEFAULT_MAX_RETRIES;
1174 if (version >= 1)
1175 {
1176 peer_config->split_mode = LIBRIST_SPLIT_MODE_OFF;
1177 peer_config->merge_mode = LIBRIST_MERGE_MODE_OFF;
1178 }
1179 if (version >= 4)
1180 {
1181 peer_config->profile = RIST_DEFAULT_PROFILE;
1182 peer_config->profile_set = 0;
1183 }
1184 if (version >= 5)
1185 {
1186 peer_config->recovery_priority = RIST_DEFAULT_RECOVERY_PRIORITY;
1187 peer_config->recovery_depth = RIST_RECOVERY_DEPTH_DEFAULT;
1188 }
1189 if (version >= 6)
1190 {
1191 peer_config->rtt_drop = RIST_DEFAULT_RTT_DROP;
1192 peer_config->rtt_restore = RIST_DEFAULT_RTT_RESTORE;
1193 peer_config->rtt_drop_settle = RIST_DEFAULT_RTT_DROP_SETTLE;
1194 peer_config->rtt_drop_trickle = RIST_DEFAULT_RTT_DROP_TRICKLE;
1195 }
1196 return 0;
1197 }
1198 else
1199 return -1;
1200 }
1201
1202 /* Legacy API wrapper symbol exported for ABI compatibility.
1203 * Since pre-existing compiled binaries calling this function do not pass their
1204 * struct size or compile-time version, we assume version 0 (baseline layout)
1205 * to guarantee that we never write out-of-bounds on legacy client structures. */
1206 int rist_peer_config_defaults_set(struct rist_peer_config *peer_config)
1207 {
1208 return rist_peer_config_defaults_set_versioned(peer_config, 0);
1209 }
1210
1211 int rist_parse_address(const char *url, const struct rist_peer_config **peer_config)
1212 {
1213 return rist_parse_address2(url, (struct rist_peer_config **)peer_config);
1214 }
1215
1216 int rist_parse_address2(const char *url, struct rist_peer_config **peer_config)
1217 {
1218 if (!url)
1219 return -1;
1220 char * url_local = strdup(url);
1221 int ret = 0;
1222 if (*peer_config == NULL)
1223 {
1224 // Default options on new struct (rist url)
1225 struct rist_peer_config *output_peer_config = calloc(1, sizeof(struct rist_peer_config));
1226 rist_peer_config_defaults_set_versioned(output_peer_config, RIST_PEER_CONFIG_VERSION);
1227 ret = parse_url_options(url_local, output_peer_config);
1228 *peer_config = output_peer_config;
1229 }
1230 else
1231 {
1232 // Update incoming object with url data
1233 struct rist_peer_config *existing_peer_config = (void *)*peer_config;
1234 ret = parse_url_options(url_local, existing_peer_config);
1235 *peer_config = existing_peer_config;
1236 }
1237 free(url_local);
1238
1239 return ret;
1240 }
1241
1242
1243 /* Shared functions with specific implementations, implementations first */
1244 static int rist_receiver_peer_create(struct rist_receiver *ctx,
1245 struct rist_peer **peer, const struct rist_peer_config *config)
1246 {
1247 struct rist_peer *p_rtcp;
1248 struct rist_peer *p = rist_receiver_peer_insert_local(ctx, config);
1249 if (!p)
1250 return -1;
1251
1252 if (config->version >= 1) {
1253 if (ctx->common.PEERS == NULL) {
1254 ctx->merge_mode = config->merge_mode;
1255 } else if (ctx->merge_mode != config->merge_mode) {
1256 rist_log_priv(&ctx->common, RIST_LOG_WARN,
1257 "peer added with merge_mode=%u but the receiver is already "
1258 "running with merge_mode=%u; new peer's merge config ignored\n",
1259 config->merge_mode, ctx->merge_mode);
1260 }
1261 }
1262
1263 p->peer_ssrc = prand_u32();
1264 if (ctx->common.profile == RIST_PROFILE_SIMPLE)
1265 {
1266 if (p->local_port % 2 != 0)
1267 {
1268 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Could not create peer, port must be even!\n");
1269 udpsocket_close(p->sd);
1270 free(p);
1271 return -1;
1272 }
1273
1274 snprintf((char *)config->address, sizeof(config->address), "%s:%d", p->url, p->local_port + 1);
1275 p_rtcp = rist_receiver_peer_insert_local(ctx, config);
1276 p_rtcp->peer_ssrc = p->peer_ssrc;
1277 if (!p_rtcp)
1278 {
1279 udpsocket_close(p->sd);
1280 free(p);
1281 return -1;
1282 }
1283 p_rtcp->is_rtcp = true;
1284 rist_log_priv(&ctx->common, RIST_LOG_INFO, "Created RTCP peer: host %s, port %d, new_url %s, %" PRIu32 "\n", p_rtcp->url, p_rtcp->local_port, config->address, p_rtcp->adv_peer_id);
1285
1286 p->peer_rtcp = p_rtcp;
1287 p_rtcp->peer_data = p;
1288 peer_append(p_rtcp);
1289 /* jumpstart communication */
1290 rist_fsm_init_comm(p_rtcp);
1291 }
1292 else
1293 {
1294 p->rist_gre_version = RIST_GRE_VERSION_MIN;
1295 p->is_rtcp = true;
1296 }
1297
1298 p->is_data = true;
1299 peer_append(p);
1300 /* jumpstart communication */
1301 rist_fsm_init_comm(p);
1302
1303 *peer = p;
1304
1305 return 0;
1306 }
1307
1308 static int rist_sender_peer_create(struct rist_sender *ctx,
1309 struct rist_peer **peer, const struct rist_peer_config *config)
1310 {
1311 struct rist_peer *newpeer = rist_sender_peer_insert_local(ctx, config, false);
1312
1313 if (!newpeer)
1314 return -1;
1315
1316 if (config->version >= 1) {
1317 if (ctx->peer_lst_len == 0) {
1318 ctx->split_mode = config->split_mode;
1319 } else if (ctx->split_mode != config->split_mode) {
1320 rist_log_priv(&ctx->common, RIST_LOG_WARN,
1321 "peer added with split_mode=%u but the session is already "
1322 "running with split_mode=%u; new peer's split config ignored\n",
1323 config->split_mode, ctx->split_mode);
1324 }
1325 if (ctx->split_mode == LIBRIST_SPLIT_MODE_HALF) {
1326 rist_log_priv(&ctx->common, RIST_LOG_WARN,
1327 "split=half is active; the receiver MUST be configured with "
1328 "merge=pairs or merge=auto, otherwise downstream consumers "
1329 "will see runt payloads.\n");
1330 }
1331 }
1332
1333 // TODO: Validate config data (virt_dst_port != 0 for example)
1334
1335 newpeer->is_data = true;
1336 if (config->weight != RIST_PEER_WEIGHT_DUPLICATE)
1337 newpeer->w_count = config->weight;
1338 peer_append(newpeer);
1339
1340 if (ctx->common.profile == RIST_PROFILE_SIMPLE)
1341 {
1342 struct rist_peer *peer_rtcp = rist_sender_peer_insert_local(ctx, config, true);
1343 if (!peer_rtcp)
1344 {
1345 rist_peer_remove(&ctx->common, newpeer, NULL);
1346 return -1;
1347 }
1348 peer_rtcp->peer_data = newpeer;
1349 newpeer->peer_rtcp = peer_rtcp;
1350 peer_append(peer_rtcp);
1351 /* jumpstart communication */
1352 rist_fsm_init_comm(peer_rtcp);
1353 /* Authenticate right away */
1354 if (!peer_rtcp->listening)
1355 {
1356 sender_peer_append(ctx, peer_rtcp);
1357 rist_peer_authenticate(peer_rtcp);
1358 }
1359 }
1360 else
1361 {
1362 newpeer->rist_gre_version = RIST_GRE_VERSION_MIN;
1363 newpeer->peer_data = newpeer;
1364 newpeer->is_rtcp = true;
1365 newpeer->compression = config->compression;
1366 }
1367
1368 /* jumpstart communication */
1369 rist_fsm_init_comm(newpeer);
1370 /* Authenticate right away */
1371 if (!newpeer->listening)
1372 {
1373 sender_peer_append(ctx, newpeer);
1374 rist_peer_authenticate(newpeer);
1375 }
1376
1377 *peer = newpeer;
1378
1379 return 0;
1380 }
1381
1382 /* If config carries a ?profile= request (version >= 4 && profile_set),
1383 * either apply it to the context (if unlocked and different) or refuse
1384 * the peer (if locked and different). Caller must hold peerlist_lock.
1385 * Returns 0 on success/no-op, -1 on conflict (caller must abort). */
1386 static int apply_url_profile_override(struct rist_common_ctx *cctx,
1387 const struct rist_peer_config *config)
1388 {
1389 if (config->version < 4 || !config->profile_set)
1390 return 0;
1391 if (config->profile == cctx->profile)
1392 return 0;
1393 if (atomic_load_explicit(&cctx->profile_locked, memory_order_acquire)) {
1394 rist_log_priv(cctx, RIST_LOG_WARN,
1395 "?profile=%d in URL refused: context profile is locked to %d "
1396 "(rist_start has run or a prior peer fixed the profile).\n",
1397 (int)config->profile, (int)cctx->profile);
1398 return -1;
1399 }
1400 enum rist_profile old = cctx->profile;
1401 cctx->profile = config->profile;
1402 if (config->profile == RIST_PROFILE_ADVANCED && old != RIST_PROFILE_ADVANCED)
1403 init_advanced_state(cctx);
1404 rist_log_priv(cctx, RIST_LOG_INFO,
1405 "Context profile changed from %d to %d by ?profile= URL parameter\n",
1406 (int)old, (int)config->profile);
1407 return 0;
1408 }
1409
1410 /* Latch the context-wide CBR output setting; a second request for a different
1411 * value is refused rather than resolved. Applied after rist_start() too, since
1412 * flows copy the value as they are created. Caller must hold peerlist_lock.
1413 * Returns 0 on success or no-op, -1 on conflict. */
1414 static int set_cbr_output_locked(struct rist_common_ctx *cctx, bool want,
1415 const char *origin)
1416 {
1417 if (cctx->cbr_output_set) {
1418 if (want == cctx->cbr_output)
1419 return 0;
1420 rist_log_priv(cctx, RIST_LOG_ERROR,
1421 "CBR output pacing %d from %s refused: already set to %d, and a "
1422 "context's outputs share one release schedule.\n",
1423 want ? 1 : 0, origin, cctx->cbr_output ? 1 : 0);
1424 return -1;
1425 }
1426
1427 /* Latch even on a value equal to the default, else 0 then 1 both succeed. */
1428 cctx->cbr_output_set = true;
1429 if (want == cctx->cbr_output)
1430 return 0;
1431 cctx->cbr_output = want;
1432 rist_log_priv(cctx, RIST_LOG_INFO,
1433 "CBR output pacing %s by %s\n", want ? "enabled" : "disabled", origin);
1434 return 0;
1435 }
1436
1437 int rist_receiver_set_cbr_output(struct rist_ctx *rist_ctx, bool enable)
1438 {
1439 if (RIST_UNLIKELY(!rist_ctx)) {
1440 rist_log_priv3(RIST_LOG_ERROR,
1441 "ctx is null on rist_receiver_set_cbr_output call!\n");
1442 return -1;
1443 }
1444 if (RIST_UNLIKELY(rist_ctx->mode != RIST_RECEIVER_MODE ||
1445 !rist_ctx->receiver_ctx)) {
1446 rist_log_priv3(RIST_LOG_ERROR,
1447 "rist_receiver_set_cbr_output: CTX not set up for receiving\n");
1448 return -1;
1449 }
1450 struct rist_common_ctx *cctx = &rist_ctx->receiver_ctx->common;
1451 pthread_mutex_lock(&cctx->peerlist_lock);
1452 int ret = set_cbr_output_locked(cctx, enable, "rist_receiver_set_cbr_output()");
1453 pthread_mutex_unlock(&cctx->peerlist_lock);
1454 return ret;
1455 }
1456
1457 /* If config carries a ?recovery-depth= request (version >= 5 and not
1458 * DEFAULT), apply it to the context. Best-effort: ignored with a warning if
1459 * rist_start has run (the sender ring cannot be resized with data in flight)
1460 * since it must be set up front. Caller must hold peerlist_lock. Runs after
1461 * apply_url_profile_override so a combined ?profile=2&recovery-depth=5
1462 * URL sees the Advanced profile already in effect. */
1463 static void apply_url_recovery_depth(struct rist_common_ctx *cctx, struct rist_ctx *ctx,
1464 const struct rist_peer_config *config)
1465 {
1466 if (config->version < 5 || config->recovery_depth == RIST_RECOVERY_DEPTH_DEFAULT)
1467 return;
1468 if (atomic_load_explicit(&cctx->startup_complete, memory_order_acquire)) {
1469 rist_log_priv(cctx, RIST_LOG_WARN,
1470 "?recovery-depth in URL ignored: it must be set before rist_start().\n");
1471 return;
1472 }
1473 rist_recovery_depth_apply(ctx, cctx, rist_recovery_depth_to_packets(config->recovery_depth));
1474 }
1475
1476 int rist_peer_create(struct rist_ctx *ctx, struct rist_peer **peer, const struct rist_peer_config *config) {
1477 if (!ctx) {
1478 rist_log_priv3(RIST_LOG_ERROR, "rist_peer_create call with null ctx\n");
1479 return -1;
1480 }
1481 int ret = 0;
1482 struct rist_common_ctx *cctx = NULL;
1483 if (ctx->mode == RIST_RECEIVER_MODE && ctx->receiver_ctx) {
1484 cctx = &ctx->receiver_ctx->common;
1485 pthread_mutex_lock(&cctx->peerlist_lock);
1486 if (apply_url_profile_override(cctx, config) < 0) {
1487 pthread_mutex_unlock(&cctx->peerlist_lock);
1488 return -1;
1489 }
1490 apply_url_recovery_depth(cctx, ctx, config);
1491 ret = rist_receiver_peer_create(ctx->receiver_ctx, peer, config);
1492 }
1493 else if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx) {
1494 cctx = &ctx->sender_ctx->common;
1495 pthread_mutex_lock(&cctx->peerlist_lock);
1496 if (apply_url_profile_override(cctx, config) < 0) {
1497 pthread_mutex_unlock(&cctx->peerlist_lock);
1498 return -1;
1499 }
1500 apply_url_recovery_depth(cctx, ctx, config);
1501 ret =rist_sender_peer_create(ctx->sender_ctx, peer, config);
1502 }
1503 else
1504 return -1;
1505 if (ret == 0)
1506 atomic_store_explicit(&cctx->profile_locked, true, memory_order_release);
1507 pthread_mutex_unlock(&cctx->peerlist_lock);
1508 return ret;
1509 }
1510
1511 int rist_peer_get_socket(struct rist_peer *peer, int *socket, int *socket_extra) {
1512 if (peer == NULL || socket == NULL)
1513 return -1;
1514 if (peer->parent != NULL)
1515 return -1;
1516 *socket = peer->sd;
1517 int ret = 0;
1518 if (socket_extra != NULL && get_cctx(peer)->profile == RIST_PROFILE_SIMPLE) {
1519 *socket_extra = peer->peer_rtcp->sd;
1520 ret = 1;
1521 }
1522 return ret;
1523 }
1524
1525 int rist_peer_weight_set(struct rist_ctx *ctx, struct rist_peer *peer, const uint32_t weight)
1526 {
1527 if (!ctx)
1528 {
1529 rist_log_priv3(RIST_LOG_ERROR, "rist_peer_weight_set call with null ctx\n");
1530 return -1;
1531 }
1532
1533 if (peer->parent != NULL)
1534 {
1535 rist_log_priv3(RIST_LOG_ERROR, "rist_peer_weight_set cannot be applied to peer with parent\n");
1536 return -1;
1537 }
1538
1539 if (ctx->mode == RIST_RECEIVER_MODE && ctx->receiver_ctx)
1540 {
1541 struct rist_receiver *rctx = ctx->receiver_ctx;
1542 pthread_mutex_lock(&rctx->common.peerlist_lock);
1543 peer->config.weight = weight;
1544 peer->w_count = weight;
1545 pthread_mutex_unlock(&rctx->common.peerlist_lock);
1546 }
1547 else if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx)
1548 {
1549 struct rist_sender *sctx = ctx->sender_ctx;
1550 pthread_mutex_lock(&sctx->mutex);
1551 pthread_mutex_lock(&sctx->common.peerlist_lock);
1552 uint32_t cur_weight = peer->config.weight;
1553 peer->config.weight = weight;
1554 if ((peer->listening && peer->child != NULL) || !peer->listening) {
1555 sctx->total_weight -= cur_weight;
1556 peer->w_count = peer->config.weight;
1557 sctx->total_weight += peer->config.weight;
1558 }
1559 pthread_mutex_unlock(&sctx->common.peerlist_lock);
1560 pthread_mutex_unlock(&sctx->mutex);
1561 }
1562 else
1563 return -1;
1564
1565 return 0;
1566 }
1567
1568 int rist_peer_destroy(struct rist_ctx *ctx, struct rist_peer *peer) {
1569 if (!ctx) {
1570 rist_log_priv3(RIST_LOG_ERROR, "rist_peer_destroy call with null ctx\n");
1571 return -1;
1572 }
1573 struct rist_common_ctx *cctx = NULL;
1574 if (ctx->mode == RIST_RECEIVER_MODE && ctx->receiver_ctx)
1575 cctx = &ctx->receiver_ctx->common;
1576 else if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx)
1577 cctx = &ctx->sender_ctx->common;
1578 else
1579 return -1;
1580 assert(cctx != NULL);
1581 pthread_mutex_lock(&cctx->peerlist_lock);
1582 int ret = rist_peer_remove(cctx, peer, NULL);
1583 pthread_mutex_unlock(&cctx->peerlist_lock);
1584 return ret;
1585 }
1586
1587 static PTHREAD_START_FUNC(sender_data_fd_read_loop, arg)
1588 {
1589 struct rist_sender *ctx = (struct rist_sender *)arg;
1590 struct rist_ctx sender_wrapper = { .mode = RIST_SENDER_MODE, .sender_ctx = ctx, .receiver_ctx = NULL };
1591 const size_t bufsize = ctx->data_fd_max_packet_size > 0 ? ctx->data_fd_max_packet_size : 1500;
1592 uint8_t *buf = malloc(bufsize);
1593 if (!buf)
1594 return 0;
1595
1596 const bool is_tun = (ctx->data_fd_flags & RIST_DATA_FD_FLAG_TUN) != 0;
1597
1598 #if defined(__unix) || defined(__APPLE__)
1599 struct pollfd pfd = { .fd = ctx->data_fd, .events = POLLIN };
1600 #endif
1601
1602 while (!atomic_load_explicit(&ctx->common.shutdown, memory_order_acquire)) {
1603 #if defined(__unix) || defined(__APPLE__)
1604 int ret = poll(&pfd, 1, 100);
1605 if (ret <= 0)
1606 continue;
1607 #endif
1608 int nread;
1609 if (is_tun)
1610 nread = rist_tun_read(ctx->data_fd, buf, bufsize);
1611 else
1612 nread = (int)read(ctx->data_fd, buf, bufsize);
1613
1614 if (nread <= 0)
1615 continue;
1616
1617 struct rist_data_block data_block = {0};
1618 data_block.payload = buf;
1619 data_block.payload_len = nread;
1620 int sent = rist_sender_data_write(&sender_wrapper, &data_block);
1621 if (sent > 0) {
1622 atomic_fetch_add_explicit(&ctx->data_fd_tx_packets, 1, memory_order_relaxed);
1623 atomic_fetch_add_explicit(&ctx->data_fd_tx_bytes, (uint_fast64_t)nread, memory_order_relaxed);
1624 }
1625 }
1626
1627 free(buf);
1628 return 0;
1629 }
1630
1631 static int rist_sender_start(struct rist_sender *ctx)
1632 {
1633 atomic_store_explicit(&ctx->common.profile_locked, true, memory_order_release);
1634 pthread_mutex_lock(&ctx->mutex);
1635 if (!ctx->protocol_running) {
1636 if (rist_thread_create(&ctx->common, &ctx->sender_thread, NULL, sender_pthread_protocol, (void *)ctx) != 0)
1637 {
1638 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Could not created sender thread.\n");
1639 goto unlock_failed;
1640 }
1641 ctx->protocol_running = true;
1642
1643 if (ctx->data_fd >= 0) {
1644 if (pthread_create(&ctx->data_fd_thread, NULL, sender_data_fd_read_loop, (void *)ctx) != 0) {
1645 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Could not create data fd reader thread.\n");
1646 goto unlock_failed;
1647 }
1648 ctx->data_fd_thread_started = true;
1649 }
1650 } else {
1651 goto unlock_failed;
1652 }
1653 if (ctx->total_weight > 0)
1654 {
1655 ctx->weight_counter = ctx->total_weight;
1656 rist_log_priv(&ctx->common, RIST_LOG_INFO, "Total weight: %lu\n", ctx->total_weight);
1657 }
1658 atomic_store_explicit(&ctx->common.startup_complete, true, memory_order_release);
1659
1660 pthread_mutex_unlock(&ctx->mutex);
1661 return 0;
1662
1663 unlock_failed:
1664 pthread_mutex_unlock(&ctx->mutex);
1665 return -1;
1666 }
1667
1668 static int rist_receiver_start(struct rist_receiver *ctx)
1669 {
1670 atomic_store_explicit(&ctx->common.profile_locked, true, memory_order_release);
1671 pthread_mutex_lock(&ctx->mutex);
1672 if (!ctx->protocol_running)
1673 {
1674 if (rist_thread_create(&ctx->common, &ctx->receiver_thread, NULL, receiver_pthread_protocol, (void *)ctx) != 0)
1675 {
1676 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Could not create receiver protocol thread.\n");
1677 goto unlock_failed;
1678 }
1679 ctx->protocol_running = true;
1680 } else {
1681 goto unlock_failed;
1682 }
1683
1684 pthread_mutex_unlock(&ctx->mutex);
1685 return 0;
1686
1687 unlock_failed:
1688 pthread_mutex_unlock(&ctx->mutex);
1689 return -1;
1690 }
1691
1692 int rist_start(struct rist_ctx *ctx) {
1693 if (!ctx) {
1694 rist_log_priv3(RIST_LOG_ERROR, "rist_start call with null ctx\n");
1695 return -1;
1696 }
1697 if (ctx->mode == RIST_RECEIVER_MODE && ctx->receiver_ctx)
1698 return rist_receiver_start(ctx->receiver_ctx);
1699 else if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx)
1700 return rist_sender_start(ctx->sender_ctx);
1701 else
1702 return -1;
1703 }
1704
1705 static int rist_sender_destroy(struct rist_sender *ctx)
1706 {
1707 if (!ctx)
1708 {
1709 return -1;
1710 }
1711
1712 rist_log_priv(&ctx->common, RIST_LOG_INFO, "Triggering protocol loop termination\n");
1713 atomic_store_explicit(&ctx->common.shutdown, 1, memory_order_release);
1714
1715 if (ctx->data_fd_thread_started)
1716 pthread_join(ctx->data_fd_thread, NULL);
1717
1718 pthread_mutex_lock(&ctx->mutex);
1719 bool running = ctx->protocol_running;
1720 pthread_mutex_unlock(&ctx->mutex);
1721 if (running)
1722 pthread_join(ctx->sender_thread, NULL);
1723 rist_sender_destroy_local(ctx);
1724
1725 return 0;
1726 }
1727
1728 static int rist_receiver_destroy(struct rist_receiver *ctx)
1729 {
1730 if (!ctx)
1731 {
1732 return -1;
1733 }
1734
1735 rist_log_priv(&ctx->common, RIST_LOG_INFO, "Triggering protocol loop termination\n");
1736 atomic_store_explicit(&ctx->common.shutdown, 1, memory_order_release);
1737 pthread_mutex_lock(&ctx->mutex);
1738 bool running = ctx->protocol_running;
1739 pthread_mutex_unlock(&ctx->mutex);
1740 if (running)
1741 pthread_join(ctx->receiver_thread, NULL);
1742 rist_receiver_destroy_local(ctx);
1743
1744 return 0;
1745 }
1746
1747 int rist_destroy(struct rist_ctx *ctx) {
1748 if (!ctx) {
1749 rist_log_priv3(RIST_LOG_ERROR, "rist_destroy call with null ctx\n");
1750 return -1;
1751 }
1752 if (ctx->mode == RIST_RECEIVER_MODE && ctx->receiver_ctx)
1753 rist_receiver_destroy(ctx->receiver_ctx);
1754 else if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx)
1755 rist_sender_destroy(ctx->sender_ctx);
1756 else
1757 return -1;
1758 free(ctx);
1759 return 0;
1760 }
1761
1762 /* Data fd tunnel API */
1763
1764 int rist_sender_data_fd_set(struct rist_ctx *rist_ctx, int fd,
1765 size_t max_packet_size, uint32_t flags)
1766 {
1767 if (RIST_UNLIKELY(!rist_ctx)) {
1768 rist_log_priv3(RIST_LOG_ERROR, "ctx is null on rist_sender_data_fd_set call!\n");
1769 return -1;
1770 }
1771 if (RIST_UNLIKELY(rist_ctx->mode != RIST_SENDER_MODE || !rist_ctx->sender_ctx)) {
1772 rist_log_priv3(RIST_LOG_ERROR, "rist_sender_data_fd_set call with CTX not set up for sending\n");
1773 return -1;
1774 }
1775 struct rist_sender *ctx = rist_ctx->sender_ctx;
1776 ctx->data_fd = fd;
1777 ctx->data_fd_max_packet_size = max_packet_size;
1778 ctx->data_fd_flags = flags;
1779 return 0;
1780 }
1781
1782 int rist_receiver_data_fd_set(struct rist_ctx *rist_ctx, int fd, uint32_t flags)
1783 {
1784 if (RIST_UNLIKELY(!rist_ctx)) {
1785 rist_log_priv3(RIST_LOG_ERROR, "ctx is null on rist_receiver_data_fd_set call!\n");
1786 return -1;
1787 }
1788 if (RIST_UNLIKELY(rist_ctx->mode != RIST_RECEIVER_MODE || !rist_ctx->receiver_ctx)) {
1789 rist_log_priv3(RIST_LOG_ERROR, "rist_receiver_data_fd_set call with CTX not set up for receiving\n");
1790 return -1;
1791 }
1792 struct rist_receiver *ctx = rist_ctx->receiver_ctx;
1793 ctx->receiver_data_fd = fd;
1794 ctx->receiver_data_fd_flags = flags;
1795 return 0;
1796 }
1797
1798 int rist_data_fd_stats_get(struct rist_ctx *ctx, struct rist_data_fd_stats *stats)
1799 {
1800 if (RIST_UNLIKELY(!ctx || !stats))
1801 return -1;
1802
1803 memset(stats, 0, sizeof(*stats));
1804
1805 if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx) {
1806 stats->tx_packets = atomic_load_explicit(&ctx->sender_ctx->data_fd_tx_packets, memory_order_relaxed);
1807 stats->tx_bytes = atomic_load_explicit(&ctx->sender_ctx->data_fd_tx_bytes, memory_order_relaxed);
1808 } else if (ctx->mode == RIST_RECEIVER_MODE && ctx->receiver_ctx) {
1809 stats->rx_packets = atomic_load_explicit(&ctx->receiver_ctx->data_fd_rx_packets, memory_order_relaxed);
1810 stats->rx_bytes = atomic_load_explicit(&ctx->receiver_ctx->data_fd_rx_bytes, memory_order_relaxed);
1811 } else {
1812 return -1;
1813 }
1814 return 0;
1815 }
1816
1817 int rist_receiver_set_output_fifo_size(struct rist_ctx *ctx, uint32_t desired_size)
1818 {
1819 if (!ctx)
1820 {
1821 rist_log_priv3(RIST_LOG_ERROR, "rist_receiver_set_fifo_size called with null ctx\n");
1822 return -1;
1823 }
1824 if (ctx->mode != RIST_RECEIVER_MODE || !ctx->receiver_ctx)
1825 {
1826 rist_log_priv3(RIST_LOG_ERROR, "rist_receiver_set_fifo_size can only be called on receiver\n");
1827 return -2;
1828 }
1829 if (ctx->receiver_ctx->receiver_thread)
1830 {
1831 rist_log_priv2(ctx->receiver_ctx->common.logging_settings, RIST_LOG_ERROR, "rist_receiver_set_fifo_size must be called before starting\n");
1832 return -3;
1833 }
1834 if (desired_size == 0 || desired_size > 65536)
1835 {
1836 rist_log_priv2(ctx->receiver_ctx->common.logging_settings, RIST_LOG_ERROR, "Desired fifo size must be between 1 and 65536\n");
1837 return -4;
1838 }
1839 if ((desired_size & (desired_size -1)) != 0)
1840 {
1841 rist_log_priv2(ctx->receiver_ctx->common.logging_settings, RIST_LOG_ERROR, "Desired fifo size must be a power of 2\n");
1842 return -4;
1843 }
1844 ctx->receiver_ctx->fifo_queue_size = desired_size;
1845 return 0;
1846 }
1847
1848 int rist_set_opt(struct rist_ctx *ctx, enum rist_opt opt, void* optval1, void* optval2, void* optval3)
1849 {
1850 struct rist_common_ctx *cctx = NULL;
1851 if (ctx->mode == RIST_RECEIVER_MODE && ctx->receiver_ctx != NULL)
1852 cctx = &ctx->receiver_ctx->common;
1853 else if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx != NULL)
1854 cctx = &ctx->sender_ctx->common;
1855 else
1856 return -1;
1857
1858 switch(opt) {
1859 case RIST_OPT_THREAD_CALLBACK:
1860 ;
1861 rist_thread_callback_t *thread_callback = optval1;
1862 if (thread_callback == NULL || thread_callback->thread_callback == NULL || optval3 != NULL)
1863 return -1;
1864 if (atomic_load_explicit(&cctx->startup_complete, memory_order_acquire))
1865 return -1;
1866 cctx->thread_callback = thread_callback->thread_callback;
1867 cctx->thread_callback_arg = optval2;
1868 break;
1869 default:
1870 return -1;
1871 }
1872 return 0;
1873 }