Debian Codegraph @

src/rist.c

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