Debian Codegraph @

src/rist.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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 the ring capacity in packets:
804 * (65536 << depth). depth is clamped to [MIN, MAX]. Returned as uint64_t
805 * because the top of the range (depth 16) is 2^32, which overflows a 32-bit
806 * size_t; the apply path rejects any size this platform cannot index. */
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 return (uint64_t)UINT16_SIZE << depth;
814 }
815
816 /* Resize the context recovery ring to `packets` (already a power of two in
817 * [UINT16_SIZE, 2^32]). The caller MUST guarantee rist_start() has not
818 * completed, so the sender ring can be reallocated with no data in flight.
819 * Returns 0 on success, -2 on OOM / size unrepresentable on this platform. */
820 static int rist_recovery_depth_apply(struct rist_ctx *ctx, struct rist_common_ctx *cctx, uint64_t packets)
821 {
822 if (packets > (uint64_t)(SIZE_MAX / (sizeof(struct rist_buffer *) + sizeof(uint32_t)))) {
823 rist_log_priv(cctx, RIST_LOG_ERROR,
824 "Recovery depth too large to allocate on this platform (%llu packets)\n",
825 (unsigned long long)packets);
826 return -2;
827 }
828 size_t want = (size_t)packets;
829
830 if (cctx->profile != RIST_PROFILE_ADVANCED) {
831 cctx->recovery_queue_max = want;
832 rist_log_priv(cctx, RIST_LOG_INFO,
833 "Recovery depth request stored (%zu packets); only the Advanced "
834 "profile uses it (Simple/Main are 16-bit, capped at %u packets).\n",
835 want, (unsigned)UINT16_SIZE);
836 return 0;
837 }
838
839 /* The receiver allocates its ring per-flow and reads recovery_queue_max
840 * directly, so nothing more to do there. The sender allocates its ring
841 * at create time, so resize it now - safe because rist_start() has not
842 * run yet and no data is flowing. On OOM the old ring (and
843 * recovery_queue_max) is left untouched. */
844 if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx) {
845 struct rist_sender *sender = ctx->sender_ctx;
846 if (sender->sender_queue_max != want) {
847 struct rist_buffer **nq = calloc(want, sizeof(*nq));
848 uint32_t *ni = calloc(want, sizeof(*ni));
849 if (!nq || !ni) {
850 free(nq);
851 free(ni);
852 rist_log_priv(cctx, RIST_LOG_ERROR,
853 "OOM resizing sender recovery ring to %zu packets\n", want);
854 return -2;
855 }
856 free(sender->sender_queue);
857 free(sender->seq_index);
858 sender->sender_queue = nq;
859 sender->seq_index = ni;
860 sender->sender_queue_max = want;
861 sender->sender_queue_delete_index = 1;
862 atomic_store_explicit(&sender->sender_queue_write_index, 1, memory_order_release);
863 atomic_store_explicit(&sender->sender_queue_read_index, 0, memory_order_release);
864 }
865 }
866
867 cctx->recovery_queue_max = want;
868 rist_log_priv(cctx, RIST_LOG_INFO,
869 "Advanced recovery buffer set to %zu packets (NACK window ~%zu packets, "
870 "~%zu MB of index arrays per context)\n",
871 want, want / 2,
872 (want * (sizeof(struct rist_buffer *) + sizeof(uint32_t))) / (1024 * 1024));
873 return 0;
874 }
875
876 int rist_recovery_depth_set(struct rist_ctx *ctx, uint8_t depth)
877 {
878 struct rist_common_ctx *cctx = rist_struct_get_common(ctx);
879 if (!cctx)
880 return -1;
881
882 if (atomic_load_explicit(&cctx->startup_complete, memory_order_acquire)) {
883 rist_log_priv(cctx, RIST_LOG_ERROR,
884 "rist_recovery_depth_set must be called before rist_start()\n");
885 return -1;
886 }
887
888 if (depth > RIST_RECOVERY_DEPTH_MAX) {
889 rist_log_priv(cctx, RIST_LOG_WARN,
890 "recovery depth %u out of range, clamping to %u\n",
891 (unsigned)depth, (unsigned)RIST_RECOVERY_DEPTH_MAX);
892 depth = RIST_RECOVERY_DEPTH_MAX;
893 }
894
895 return rist_recovery_depth_apply(ctx, cctx, rist_recovery_depth_to_packets(depth));
896 }
897
898 int rist_auth_handler_set(struct rist_ctx *ctx,
899 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),
900 int (*disconn_cb)(void *arg, struct rist_peer *peer),
901 void *arg)
902 {
903 if (RIST_UNLIKELY(!ctx))
904 {
905 rist_log_priv3(RIST_LOG_ERROR, "rist_auth_handler_set call with null ctx!\n");
906 return -1;
907 }
908 struct rist_common_ctx *cctx = rist_struct_get_common(ctx);
909 if (RIST_UNLIKELY(!cctx))
910 return -1;
911 return rist_auth_handler(cctx, conn_cb, disconn_cb, arg);
912 }
913
914 int rist_stats_free(const struct rist_stats *stats_container)
915 {
916 if (!stats_container)
917 return -1;
918 free(stats_container->stats_json);
919 if (stats_container->stats_type == RIST_STATS_RECEIVER_FLOW)
920 free(stats_container->stats.receiver_flow.peers);
921 free((void *)stats_container);
922 return 0;
923 }
924
925 uint32_t rist_peer_get_id(const struct rist_peer *peer)
926 {
927 return peer->adv_peer_id;
928 }
929
930 uint32_t rist_peer_get_cname(const struct rist_peer *peer, const char **cname)
931 {
932 if (peer)
933 {
934 *cname = &peer->cname[0];
935 return (uint32_t)strnlen(*cname, RIST_MAX_STRING_SHORT);
936 }
937 else
938 return 0;
939 }
940
941 int rist_peer_config_free(const struct rist_peer_config **peer_config)
942 {
943 return rist_peer_config_free2((struct rist_peer_config **)peer_config);
944 }
945
946 int rist_peer_config_free2(struct rist_peer_config **peer_config)
947 {
948 if (*peer_config) {
949 free((void *)*peer_config);
950 *peer_config = NULL;
951 }
952 return 0;
953 }
954
955 #if HAVE_SRP_SUPPORT
956 int rist_peer_update_secret(struct rist_peer *peer, const char* password) {
957 pthread_mutex_lock(&peer->peer_lock);
958 size_t password_len = strlen(password);
959 struct rist_key *inactive_key = (peer->key_rx_odd_active) ? &peer->key_tx : &peer->key_tx_odd;
960 rist_log_priv(get_cctx(peer), RIST_LOG_INFO, "Updating passphrase to %s\n", password);
961 _librist_crypto_psk_set_passphrase(inactive_key, (const uint8_t *)password, password_len);
962 struct rist_peer *child = peer->child;
963 while (child != NULL) {
964 pthread_mutex_lock(&child->peer_lock);
965 inactive_key = (child->key_rx_odd_active) ? &child->key_tx : &child->key_tx_odd;
966 _librist_crypto_psk_set_passphrase(inactive_key, (const uint8_t *)password, password_len);
967 child->rolling_over_passphrase = true;
968 pthread_mutex_unlock(&child->peer_lock);
969 rist_eap_send_passphrase(child->eap_ctx, password);
970 child = child->sibling_next;
971 }
972 peer->rolling_over_passphrase = true;
973 pthread_mutex_unlock(&peer->peer_lock);
974 rist_eap_send_passphrase(peer->eap_ctx, password);
975 return 0;
976 }
977 #endif
978
979 int rist_logging_settings_free(const struct rist_logging_settings **logging_settings)
980 {
981 return rist_logging_settings_free2((struct rist_logging_settings **)logging_settings);
982 }
983
984 int rist_logging_settings_free2(struct rist_logging_settings **logging_settings)
985 {
986 if (*logging_settings) {
987 rist_logging_unset_global_if_matches(*logging_settings);
988 free((void *)*logging_settings);
989 *logging_settings = NULL;
990 }
991 return 0;
992 }
993
994 int rist_udp_config_free(const struct rist_udp_config **udp_config)
995 {
996 return rist_udp_config_free2((struct rist_udp_config **)udp_config);
997 }
998
999 int rist_udp_config_free2(struct rist_udp_config **udp_config)
1000 {
1001 if (*udp_config) {
1002 free((void *)*udp_config);
1003 *udp_config = NULL;
1004 }
1005 return 0;
1006 }
1007
1008 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)
1009 {
1010 if (RIST_UNLIKELY(!ctx))
1011 {
1012 rist_log_priv3(RIST_LOG_ERROR, "rist_sender_stats_callback_set call with null ctx!\n");
1013 return -1;
1014 }
1015
1016 if (ctx->mode == RIST_SENDER_MODE && statsinterval != 0)
1017 {
1018 ctx->sender_ctx->sender_stats_callback = stats_cb;
1019 ctx->sender_ctx->sender_stats_callback_argument = arg;
1020 ctx->sender_ctx->stats_report_time = statsinterval * RIST_CLOCK;
1021 }
1022
1023 return 0;
1024 }
1025
1026 int rist_stats_callback_set(struct rist_ctx *ctx, int statsinterval, int (*stats_cb)(void *arg, const struct rist_stats *stats_container), void *arg)
1027 {
1028 if (RIST_UNLIKELY(!ctx))
1029 {
1030 rist_log_priv3(RIST_LOG_ERROR, "rist_stats_callback_set call with null ctx!\n");
1031 return -1;
1032 }
1033 struct rist_common_ctx *cctx = rist_struct_get_common(ctx);
1034 pthread_mutex_lock(&cctx->stats_lock);
1035 if (RIST_UNLIKELY(!cctx))
1036 return -1;
1037
1038 if (statsinterval != 0)
1039 {
1040 cctx->stats_callback = stats_cb;
1041 cctx->stats_callback_argument = arg;
1042 cctx->stats_report_time = statsinterval * RIST_CLOCK;
1043 if (ctx->mode == RIST_RECEIVER_MODE)
1044 {
1045 struct rist_flow *f = cctx->FLOWS;
1046 while (f)
1047 {
1048 f->stats_report_time = statsinterval * RIST_CLOCK;
1049 f = f->next;
1050 }
1051 }
1052 }
1053 pthread_mutex_unlock(&cctx->stats_lock);
1054 return 0;
1055 }
1056
1057 /* Utility functions */
1058 const char *librist_version(void)
1059 {
1060 return LIBRIST_VERSION;
1061 }
1062
1063 const char *librist_api_version(void)
1064 {
1065 return STR(LIBRIST_API_VERSION_MAJOR)"."STR(LIBRIST_API_VERSION_MINOR)"."STR(LIBRIST_API_VERSION_PATCH);
1066 }
1067
1068 int rist_parse_udp_address(const char *url, const struct rist_udp_config **udp_config)
1069 {
1070 return rist_parse_udp_address2(url, (struct rist_udp_config **)udp_config);
1071 }
1072
1073 int rist_parse_udp_address2(const char *url, struct rist_udp_config **udp_config)
1074 {
1075
1076 int ret = 0;
1077 if (*udp_config == NULL)
1078 {
1079 // Default options on new struct (specific for udp url)
1080 struct rist_udp_config *output_udp_config = calloc(1, sizeof(struct rist_udp_config));
1081 output_udp_config->version = RIST_UDP_CONFIG_VERSION;
1082 output_udp_config->stream_id = 0; // Accept all on receiver, auto-generate on sender
1083 ret = parse_url_udp_options(url, output_udp_config);
1084 *udp_config = output_udp_config;
1085 }
1086 else
1087 {
1088 // Update incoming object with url data
1089 struct rist_udp_config *existing_udp_config = (void *)*udp_config;
1090 ret = parse_url_udp_options(url, existing_udp_config);
1091 *udp_config = existing_udp_config;
1092 }
1093
1094 return ret;
1095 }
1096
1097 int rist_peer_config_defaults_set_versioned(struct rist_peer_config *peer_config, int version)
1098 {
1099 if (peer_config)
1100 {
1101 peer_config->version = version;
1102 peer_config->virt_dst_port = RIST_DEFAULT_VIRT_DST_PORT;
1103 peer_config->recovery_mode = RIST_DEFAULT_RECOVERY_MODE;
1104 peer_config->recovery_maxbitrate = RIST_DEFAULT_RECOVERY_MAXBITRATE;
1105 peer_config->recovery_maxbitrate_return = RIST_DEFAULT_RECOVERY_MAXBITRATE_RETURN;
1106 peer_config->recovery_length_min = RIST_DEFAULT_RECOVERY_LENGTH_MIN;
1107 peer_config->recovery_length_max = RIST_DEFAULT_RECOVERY_LENGTH_MAX;
1108 peer_config->recovery_reorder_buffer = RIST_DEFAULT_RECOVERY_REORDER_BUFFER;
1109 peer_config->recovery_rtt_min = RIST_DEFAULT_RECOVERY_RTT_MIN;
1110 peer_config->recovery_rtt_max = RIST_DEFAULT_RECOVERY_RTT_MAX;
1111 peer_config->congestion_control_mode = RIST_DEFAULT_CONGESTION_CONTROL_MODE;
1112 peer_config->min_retries = RIST_DEFAULT_MIN_RETRIES;
1113 peer_config->max_retries = RIST_DEFAULT_MAX_RETRIES;
1114 if (version >= 1)
1115 {
1116 peer_config->split_mode = LIBRIST_SPLIT_MODE_OFF;
1117 peer_config->merge_mode = LIBRIST_MERGE_MODE_OFF;
1118 }
1119 if (version >= 4)
1120 {
1121 peer_config->profile = RIST_DEFAULT_PROFILE;
1122 peer_config->profile_set = 0;
1123 }
1124 if (version >= 5)
1125 {
1126 peer_config->recovery_priority = RIST_DEFAULT_RECOVERY_PRIORITY;
1127 peer_config->recovery_depth = RIST_RECOVERY_DEPTH_DEFAULT;
1128 }
1129 return 0;
1130 }
1131 else
1132 return -1;
1133 }
1134
1135 /* Legacy API wrapper symbol exported for ABI compatibility.
1136 * Since pre-existing compiled binaries calling this function do not pass their
1137 * struct size or compile-time version, we assume version 0 (baseline layout)
1138 * to guarantee that we never write out-of-bounds on legacy client structures. */
1139 int rist_peer_config_defaults_set(struct rist_peer_config *peer_config)
1140 {
1141 return rist_peer_config_defaults_set_versioned(peer_config, 0);
1142 }
1143
1144 int rist_parse_address(const char *url, const struct rist_peer_config **peer_config)
1145 {
1146 return rist_parse_address2(url, (struct rist_peer_config **)peer_config);
1147 }
1148
1149 int rist_parse_address2(const char *url, struct rist_peer_config **peer_config)
1150 {
1151
1152 char * url_local = strdup(url);
1153 int ret = 0;
1154 if (*peer_config == NULL)
1155 {
1156 // Default options on new struct (rist url)
1157 struct rist_peer_config *output_peer_config = calloc(1, sizeof(struct rist_peer_config));
1158 rist_peer_config_defaults_set_versioned(output_peer_config, RIST_PEER_CONFIG_VERSION);
1159 ret = parse_url_options(url_local, output_peer_config);
1160 *peer_config = output_peer_config;
1161 }
1162 else
1163 {
1164 // Update incoming object with url data
1165 struct rist_peer_config *existing_peer_config = (void *)*peer_config;
1166 ret = parse_url_options(url_local, existing_peer_config);
1167 *peer_config = existing_peer_config;
1168 }
1169 free(url_local);
1170
1171 return ret;
1172 }
1173
1174
1175 /* Shared functions with specific implementations, implementations first */
1176 static int rist_receiver_peer_create(struct rist_receiver *ctx,
1177 struct rist_peer **peer, const struct rist_peer_config *config)
1178 {
1179 struct rist_peer *p_rtcp;
1180 struct rist_peer *p = rist_receiver_peer_insert_local(ctx, config);
1181 if (!p)
1182 return -1;
1183
1184 if (config->version >= 1) {
1185 if (ctx->common.PEERS == NULL) {
1186 ctx->merge_mode = config->merge_mode;
1187 } else if (ctx->merge_mode != config->merge_mode) {
1188 rist_log_priv(&ctx->common, RIST_LOG_WARN,
1189 "peer added with merge_mode=%u but the receiver is already "
1190 "running with merge_mode=%u; new peer's merge config ignored\n",
1191 config->merge_mode, ctx->merge_mode);
1192 }
1193 }
1194
1195 p->peer_ssrc = prand_u32();
1196 if (ctx->common.profile == RIST_PROFILE_SIMPLE)
1197 {
1198 if (p->local_port % 2 != 0)
1199 {
1200 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Could not create peer, port must be even!\n");
1201 udpsocket_close(p->sd);
1202 free(p);
1203 return -1;
1204 }
1205
1206 sprintf((char *)config->address, "%s:%d", p->url, p->local_port + 1);
1207 p_rtcp = rist_receiver_peer_insert_local(ctx, config);
1208 p_rtcp->peer_ssrc = p->peer_ssrc;
1209 if (!p_rtcp)
1210 {
1211 udpsocket_close(p->sd);
1212 free(p);
1213 return -1;
1214 }
1215 p_rtcp->is_rtcp = true;
1216 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);
1217
1218 p->peer_rtcp = p_rtcp;
1219 p_rtcp->peer_data = p;
1220 peer_append(p_rtcp);
1221 /* jumpstart communication */
1222 rist_fsm_init_comm(p_rtcp);
1223 }
1224 else
1225 {
1226 p->rist_gre_version = RIST_GRE_VERSION_MIN;
1227 p->is_rtcp = true;
1228 }
1229
1230 p->is_data = true;
1231 peer_append(p);
1232 /* jumpstart communication */
1233 rist_fsm_init_comm(p);
1234
1235 *peer = p;
1236
1237 return 0;
1238 }
1239
1240 static int rist_sender_peer_create(struct rist_sender *ctx,
1241 struct rist_peer **peer, const struct rist_peer_config *config)
1242 {
1243 struct rist_peer *newpeer = rist_sender_peer_insert_local(ctx, config, false);
1244
1245 if (!newpeer)
1246 return -1;
1247
1248 if (config->version >= 1) {
1249 if (ctx->peer_lst_len == 0) {
1250 ctx->split_mode = config->split_mode;
1251 } else if (ctx->split_mode != config->split_mode) {
1252 rist_log_priv(&ctx->common, RIST_LOG_WARN,
1253 "peer added with split_mode=%u but the session is already "
1254 "running with split_mode=%u; new peer's split config ignored\n",
1255 config->split_mode, ctx->split_mode);
1256 }
1257 if (ctx->split_mode == LIBRIST_SPLIT_MODE_HALF) {
1258 rist_log_priv(&ctx->common, RIST_LOG_WARN,
1259 "split=half is active; the receiver MUST be configured with "
1260 "merge=pairs or merge=auto, otherwise downstream consumers "
1261 "will see runt payloads.\n");
1262 }
1263 }
1264
1265 // TODO: Validate config data (virt_dst_port != 0 for example)
1266
1267 newpeer->is_data = true;
1268 if (config->weight != RIST_PEER_WEIGHT_DUPLICATE)
1269 newpeer->w_count = config->weight;
1270 peer_append(newpeer);
1271
1272 if (ctx->common.profile == RIST_PROFILE_SIMPLE)
1273 {
1274 struct rist_peer *peer_rtcp = rist_sender_peer_insert_local(ctx, config, true);
1275 if (!peer_rtcp)
1276 {
1277 rist_peer_remove(&ctx->common, newpeer, NULL);
1278 return -1;
1279 }
1280 peer_rtcp->peer_data = newpeer;
1281 newpeer->peer_rtcp = peer_rtcp;
1282 peer_append(peer_rtcp);
1283 /* jumpstart communication */
1284 rist_fsm_init_comm(peer_rtcp);
1285 /* Authenticate right away */
1286 if (!peer_rtcp->listening)
1287 {
1288 sender_peer_append(ctx, peer_rtcp);
1289 rist_peer_authenticate(peer_rtcp);
1290 }
1291 }
1292 else
1293 {
1294 newpeer->rist_gre_version = RIST_GRE_VERSION_MIN;
1295 newpeer->peer_data = newpeer;
1296 newpeer->is_rtcp = true;
1297 newpeer->compression = config->compression;
1298 }
1299
1300 /* jumpstart communication */
1301 rist_fsm_init_comm(newpeer);
1302 /* Authenticate right away */
1303 if (!newpeer->listening)
1304 {
1305 sender_peer_append(ctx, newpeer);
1306 rist_peer_authenticate(newpeer);
1307 }
1308
1309 *peer = newpeer;
1310
1311 return 0;
1312 }
1313
1314 /* If config carries a ?profile= request (version >= 4 && profile_set),
1315 * either apply it to the context (if unlocked and different) or refuse
1316 * the peer (if locked and different). Caller must hold peerlist_lock.
1317 * Returns 0 on success/no-op, -1 on conflict (caller must abort). */
1318 static int apply_url_profile_override(struct rist_common_ctx *cctx,
1319 const struct rist_peer_config *config)
1320 {
1321 if (config->version < 4 || !config->profile_set)
1322 return 0;
1323 if (config->profile == cctx->profile)
1324 return 0;
1325 if (atomic_load_explicit(&cctx->profile_locked, memory_order_acquire)) {
1326 rist_log_priv(cctx, RIST_LOG_WARN,
1327 "?profile=%d in URL refused: context profile is locked to %d "
1328 "(rist_start has run or a prior peer fixed the profile).\n",
1329 (int)config->profile, (int)cctx->profile);
1330 return -1;
1331 }
1332 enum rist_profile old = cctx->profile;
1333 cctx->profile = config->profile;
1334 if (config->profile == RIST_PROFILE_ADVANCED && old != RIST_PROFILE_ADVANCED)
1335 init_advanced_state(cctx);
1336 rist_log_priv(cctx, RIST_LOG_INFO,
1337 "Context profile changed from %d to %d by ?profile= URL parameter\n",
1338 (int)old, (int)config->profile);
1339 return 0;
1340 }
1341
1342 /* If config carries a ?recovery-depth= request (version >= 5 and not
1343 * DEFAULT), apply it to the context. Best-effort: ignored with a warning if
1344 * rist_start has run (the sender ring cannot be resized with data in flight)
1345 * since it must be set up front. Caller must hold peerlist_lock. Runs after
1346 * apply_url_profile_override so a combined ?profile=2&recovery-depth=5
1347 * URL sees the Advanced profile already in effect. */
1348 static void apply_url_recovery_depth(struct rist_common_ctx *cctx, struct rist_ctx *ctx,
1349 const struct rist_peer_config *config)
1350 {
1351 if (config->version < 5 || config->recovery_depth == RIST_RECOVERY_DEPTH_DEFAULT)
1352 return;
1353 if (atomic_load_explicit(&cctx->startup_complete, memory_order_acquire)) {
1354 rist_log_priv(cctx, RIST_LOG_WARN,
1355 "?recovery-depth in URL ignored: it must be set before rist_start().\n");
1356 return;
1357 }
1358 rist_recovery_depth_apply(ctx, cctx, rist_recovery_depth_to_packets(config->recovery_depth));
1359 }
1360
1361 int rist_peer_create(struct rist_ctx *ctx, struct rist_peer **peer, const struct rist_peer_config *config) {
1362 if (!ctx) {
1363 rist_log_priv3(RIST_LOG_ERROR, "rist_peer_create call with null ctx\n");
1364 return -1;
1365 }
1366 int ret = 0;
1367 struct rist_common_ctx *cctx = NULL;
1368 if (ctx->mode == RIST_RECEIVER_MODE && ctx->receiver_ctx) {
1369 cctx = &ctx->receiver_ctx->common;
1370 pthread_mutex_lock(&cctx->peerlist_lock);
1371 if (apply_url_profile_override(cctx, config) < 0) {
1372 pthread_mutex_unlock(&cctx->peerlist_lock);
1373 return -1;
1374 }
1375 apply_url_recovery_depth(cctx, ctx, config);
1376 ret = rist_receiver_peer_create(ctx->receiver_ctx, peer, config);
1377 }
1378 else if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx) {
1379 cctx = &ctx->sender_ctx->common;
1380 pthread_mutex_lock(&cctx->peerlist_lock);
1381 if (apply_url_profile_override(cctx, config) < 0) {
1382 pthread_mutex_unlock(&cctx->peerlist_lock);
1383 return -1;
1384 }
1385 apply_url_recovery_depth(cctx, ctx, config);
1386 ret =rist_sender_peer_create(ctx->sender_ctx, peer, config);
1387 }
1388 else
1389 return -1;
1390 if (ret == 0)
1391 atomic_store_explicit(&cctx->profile_locked, true, memory_order_release);
1392 pthread_mutex_unlock(&cctx->peerlist_lock);
1393 return ret;
1394 }
1395
1396 int rist_peer_get_socket(struct rist_peer *peer, int *socket, int *socket_extra) {
1397 if (socket == NULL)
1398 return -1;
1399 if (peer->parent != NULL)
1400 return -1;
1401 *socket = peer->sd;
1402 int ret = 0;
1403 if (socket_extra != NULL && get_cctx(peer)->profile == RIST_PROFILE_SIMPLE) {
1404 *socket_extra = peer->peer_rtcp->sd;
1405 ret = 1;
1406 }
1407 return ret;
1408 }
1409
1410 int rist_peer_weight_set(struct rist_ctx *ctx, struct rist_peer *peer, const uint32_t weight)
1411 {
1412 if (!ctx)
1413 {
1414 rist_log_priv3(RIST_LOG_ERROR, "rist_peer_weight_set call with null ctx\n");
1415 return -1;
1416 }
1417
1418 if (peer->parent != NULL)
1419 {
1420 rist_log_priv3(RIST_LOG_ERROR, "rist_peer_weight_set cannot be applied to peer with parent\n");
1421 return -1;
1422 }
1423
1424 if (ctx->mode == RIST_RECEIVER_MODE && ctx->receiver_ctx)
1425 {
1426 struct rist_receiver *rctx = ctx->receiver_ctx;
1427 pthread_mutex_lock(&rctx->common.peerlist_lock);
1428 peer->config.weight = weight;
1429 peer->w_count = weight;
1430 pthread_mutex_unlock(&rctx->common.peerlist_lock);
1431 }
1432 else if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx)
1433 {
1434 struct rist_sender *sctx = ctx->sender_ctx;
1435 pthread_mutex_lock(&sctx->mutex);
1436 pthread_mutex_lock(&sctx->common.peerlist_lock);
1437 uint32_t cur_weight = peer->config.weight;
1438 peer->config.weight = weight;
1439 if ((peer->listening && peer->child != NULL) || !peer->listening) {
1440 sctx->total_weight -= cur_weight;
1441 peer->w_count = peer->config.weight;
1442 sctx->total_weight += peer->config.weight;
1443 }
1444 pthread_mutex_unlock(&sctx->common.peerlist_lock);
1445 pthread_mutex_unlock(&sctx->mutex);
1446 }
1447 else
1448 return -1;
1449
1450 return 0;
1451 }
1452
1453 int rist_peer_destroy(struct rist_ctx *ctx, struct rist_peer *peer) {
1454 if (!ctx) {
1455 rist_log_priv3(RIST_LOG_ERROR, "rist_peer_destroy call with null ctx\n");
1456 return -1;
1457 }
1458 struct rist_common_ctx *cctx = NULL;
1459 if (ctx->mode == RIST_RECEIVER_MODE && ctx->receiver_ctx)
1460 cctx = &ctx->receiver_ctx->common;
1461 else if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx)
1462 cctx = &ctx->sender_ctx->common;
1463 else
1464 return -1;
1465 assert(cctx != NULL);
1466 pthread_mutex_lock(&cctx->peerlist_lock);
1467 int ret = rist_peer_remove(cctx, peer, NULL);
1468 pthread_mutex_unlock(&cctx->peerlist_lock);
1469 return ret;
1470 }
1471
1472 static PTHREAD_START_FUNC(sender_data_fd_read_loop, arg)
1473 {
1474 struct rist_sender *ctx = (struct rist_sender *)arg;
1475 struct rist_ctx sender_wrapper = { .mode = RIST_SENDER_MODE, .sender_ctx = ctx, .receiver_ctx = NULL };
1476 const size_t bufsize = ctx->data_fd_max_packet_size > 0 ? ctx->data_fd_max_packet_size : 1500;
1477 uint8_t *buf = malloc(bufsize);
1478 if (!buf)
1479 return 0;
1480
1481 const bool is_tun = (ctx->data_fd_flags & RIST_DATA_FD_FLAG_TUN) != 0;
1482
1483 #if defined(__unix) || defined(__APPLE__)
1484 struct pollfd pfd = { .fd = ctx->data_fd, .events = POLLIN };
1485 #endif
1486
1487 while (!atomic_load_explicit(&ctx->common.shutdown, memory_order_acquire)) {
1488 #if defined(__unix) || defined(__APPLE__)
1489 int ret = poll(&pfd, 1, 100);
1490 if (ret <= 0)
1491 continue;
1492 #endif
1493 int nread;
1494 if (is_tun)
1495 nread = rist_tun_read(ctx->data_fd, buf, bufsize);
1496 else
1497 nread = (int)read(ctx->data_fd, buf, bufsize);
1498
1499 if (nread <= 0)
1500 continue;
1501
1502 struct rist_data_block data_block = {0};
1503 data_block.payload = buf;
1504 data_block.payload_len = nread;
1505 int sent = rist_sender_data_write(&sender_wrapper, &data_block);
1506 if (sent > 0) {
1507 atomic_fetch_add_explicit(&ctx->data_fd_tx_packets, 1, memory_order_relaxed);
1508 atomic_fetch_add_explicit(&ctx->data_fd_tx_bytes, (uint_fast64_t)nread, memory_order_relaxed);
1509 }
1510 }
1511
1512 free(buf);
1513 return 0;
1514 }
1515
1516 static int rist_sender_start(struct rist_sender *ctx)
1517 {
1518 atomic_store_explicit(&ctx->common.profile_locked, true, memory_order_release);
1519 pthread_mutex_lock(&ctx->mutex);
1520 if (!ctx->protocol_running) {
1521 if (rist_thread_create(&ctx->common, &ctx->sender_thread, NULL, sender_pthread_protocol, (void *)ctx) != 0)
1522 {
1523 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Could not created sender thread.\n");
1524 goto unlock_failed;
1525 }
1526 ctx->protocol_running = true;
1527
1528 if (ctx->data_fd >= 0) {
1529 if (pthread_create(&ctx->data_fd_thread, NULL, sender_data_fd_read_loop, (void *)ctx) != 0) {
1530 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Could not create data fd reader thread.\n");
1531 goto unlock_failed;
1532 }
1533 ctx->data_fd_thread_started = true;
1534 }
1535 } else {
1536 goto unlock_failed;
1537 }
1538 if (ctx->total_weight > 0)
1539 {
1540 ctx->weight_counter = ctx->total_weight;
1541 rist_log_priv(&ctx->common, RIST_LOG_INFO, "Total weight: %lu\n", ctx->total_weight);
1542 }
1543 atomic_store_explicit(&ctx->common.startup_complete, true, memory_order_release);
1544
1545 pthread_mutex_unlock(&ctx->mutex);
1546 return 0;
1547
1548 unlock_failed:
1549 pthread_mutex_unlock(&ctx->mutex);
1550 return -1;
1551 }
1552
1553 static int rist_receiver_start(struct rist_receiver *ctx)
1554 {
1555 atomic_store_explicit(&ctx->common.profile_locked, true, memory_order_release);
1556 pthread_mutex_lock(&ctx->mutex);
1557 if (!ctx->protocol_running)
1558 {
1559 if (rist_thread_create(&ctx->common, &ctx->receiver_thread, NULL, receiver_pthread_protocol, (void *)ctx) != 0)
1560 {
1561 rist_log_priv(&ctx->common, RIST_LOG_ERROR, "Could not create receiver protocol thread.\n");
1562 goto unlock_failed;
1563 }
1564 ctx->protocol_running = true;
1565 } else {
1566 goto unlock_failed;
1567 }
1568
1569 pthread_mutex_unlock(&ctx->mutex);
1570 return 0;
1571
1572 unlock_failed:
1573 pthread_mutex_unlock(&ctx->mutex);
1574 return -1;
1575 }
1576
1577 int rist_start(struct rist_ctx *ctx) {
1578 if (!ctx) {
1579 rist_log_priv3(RIST_LOG_ERROR, "rist_start call with null ctx\n");
1580 return -1;
1581 }
1582 if (ctx->mode == RIST_RECEIVER_MODE && ctx->receiver_ctx)
1583 return rist_receiver_start(ctx->receiver_ctx);
1584 else if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx)
1585 return rist_sender_start(ctx->sender_ctx);
1586 else
1587 return -1;
1588 }
1589
1590 static int rist_sender_destroy(struct rist_sender *ctx)
1591 {
1592 if (!ctx)
1593 {
1594 return -1;
1595 }
1596
1597 rist_log_priv(&ctx->common, RIST_LOG_INFO, "Triggering protocol loop termination\n");
1598 atomic_store_explicit(&ctx->common.shutdown, 1, memory_order_release);
1599
1600 if (ctx->data_fd_thread_started)
1601 pthread_join(ctx->data_fd_thread, NULL);
1602
1603 pthread_mutex_lock(&ctx->mutex);
1604 bool running = ctx->protocol_running;
1605 pthread_mutex_unlock(&ctx->mutex);
1606 if (running)
1607 pthread_join(ctx->sender_thread, NULL);
1608 rist_sender_destroy_local(ctx);
1609
1610 return 0;
1611 }
1612
1613 static int rist_receiver_destroy(struct rist_receiver *ctx)
1614 {
1615 if (!ctx)
1616 {
1617 return -1;
1618 }
1619
1620 rist_log_priv(&ctx->common, RIST_LOG_INFO, "Triggering protocol loop termination\n");
1621 atomic_store_explicit(&ctx->common.shutdown, 1, memory_order_release);
1622 pthread_mutex_lock(&ctx->mutex);
1623 bool running = ctx->protocol_running;
1624 pthread_mutex_unlock(&ctx->mutex);
1625 if (running)
1626 pthread_join(ctx->receiver_thread, NULL);
1627 rist_receiver_destroy_local(ctx);
1628
1629 return 0;
1630 }
1631
1632 int rist_destroy(struct rist_ctx *ctx) {
1633 if (!ctx) {
1634 rist_log_priv3(RIST_LOG_ERROR, "rist_destroy call with null ctx\n");
1635 return -1;
1636 }
1637 if (ctx->mode == RIST_RECEIVER_MODE && ctx->receiver_ctx)
1638 rist_receiver_destroy(ctx->receiver_ctx);
1639 else if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx)
1640 rist_sender_destroy(ctx->sender_ctx);
1641 else
1642 return -1;
1643 free(ctx);
1644 return 0;
1645 }
1646
1647 /* Data fd tunnel API */
1648
1649 int rist_sender_data_fd_set(struct rist_ctx *rist_ctx, int fd,
1650 size_t max_packet_size, uint32_t flags)
1651 {
1652 if (RIST_UNLIKELY(!rist_ctx)) {
1653 rist_log_priv3(RIST_LOG_ERROR, "ctx is null on rist_sender_data_fd_set call!\n");
1654 return -1;
1655 }
1656 if (RIST_UNLIKELY(rist_ctx->mode != RIST_SENDER_MODE || !rist_ctx->sender_ctx)) {
1657 rist_log_priv3(RIST_LOG_ERROR, "rist_sender_data_fd_set call with CTX not set up for sending\n");
1658 return -1;
1659 }
1660 struct rist_sender *ctx = rist_ctx->sender_ctx;
1661 ctx->data_fd = fd;
1662 ctx->data_fd_max_packet_size = max_packet_size;
1663 ctx->data_fd_flags = flags;
1664 return 0;
1665 }
1666
1667 int rist_receiver_data_fd_set(struct rist_ctx *rist_ctx, int fd, uint32_t flags)
1668 {
1669 if (RIST_UNLIKELY(!rist_ctx)) {
1670 rist_log_priv3(RIST_LOG_ERROR, "ctx is null on rist_receiver_data_fd_set call!\n");
1671 return -1;
1672 }
1673 if (RIST_UNLIKELY(rist_ctx->mode != RIST_RECEIVER_MODE || !rist_ctx->receiver_ctx)) {
1674 rist_log_priv3(RIST_LOG_ERROR, "rist_receiver_data_fd_set call with CTX not set up for receiving\n");
1675 return -1;
1676 }
1677 struct rist_receiver *ctx = rist_ctx->receiver_ctx;
1678 ctx->receiver_data_fd = fd;
1679 ctx->receiver_data_fd_flags = flags;
1680 return 0;
1681 }
1682
1683 int rist_data_fd_stats_get(struct rist_ctx *ctx, struct rist_data_fd_stats *stats)
1684 {
1685 if (RIST_UNLIKELY(!ctx || !stats))
1686 return -1;
1687
1688 memset(stats, 0, sizeof(*stats));
1689
1690 if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx) {
1691 stats->tx_packets = atomic_load_explicit(&ctx->sender_ctx->data_fd_tx_packets, memory_order_relaxed);
1692 stats->tx_bytes = atomic_load_explicit(&ctx->sender_ctx->data_fd_tx_bytes, memory_order_relaxed);
1693 } else if (ctx->mode == RIST_RECEIVER_MODE && ctx->receiver_ctx) {
1694 stats->rx_packets = atomic_load_explicit(&ctx->receiver_ctx->data_fd_rx_packets, memory_order_relaxed);
1695 stats->rx_bytes = atomic_load_explicit(&ctx->receiver_ctx->data_fd_rx_bytes, memory_order_relaxed);
1696 } else {
1697 return -1;
1698 }
1699 return 0;
1700 }
1701
1702 int rist_receiver_set_output_fifo_size(struct rist_ctx *ctx, uint32_t desired_size)
1703 {
1704 if (!ctx)
1705 {
1706 rist_log_priv3(RIST_LOG_ERROR, "rist_receiver_set_fifo_size called with null ctx\n");
1707 return -1;
1708 }
1709 if (ctx->mode != RIST_RECEIVER_MODE || !ctx->receiver_ctx)
1710 {
1711 rist_log_priv3(RIST_LOG_ERROR, "rist_receiver_set_fifo_size can only be called on receiver\n");
1712 return -2;
1713 }
1714 if (ctx->receiver_ctx->receiver_thread)
1715 {
1716 rist_log_priv2(ctx->receiver_ctx->common.logging_settings, RIST_LOG_ERROR, "rist_receiver_set_fifo_size must be called before starting\n");
1717 return -3;
1718 }
1719 if (desired_size == 0 || desired_size > 65536)
1720 {
1721 rist_log_priv2(ctx->receiver_ctx->common.logging_settings, RIST_LOG_ERROR, "Desired fifo size must be between 1 and 65536\n");
1722 return -4;
1723 }
1724 if ((desired_size & (desired_size -1)) != 0)
1725 {
1726 rist_log_priv2(ctx->receiver_ctx->common.logging_settings, RIST_LOG_ERROR, "Desired fifo size must be a power of 2\n");
1727 return -4;
1728 }
1729 ctx->receiver_ctx->fifo_queue_size = desired_size;
1730 return 0;
1731 }
1732
1733 int rist_set_opt(struct rist_ctx *ctx, enum rist_opt opt, void* optval1, void* optval2, void* optval3)
1734 {
1735 struct rist_common_ctx *cctx = NULL;
1736 if (ctx->mode == RIST_RECEIVER_MODE && ctx->receiver_ctx != NULL)
1737 cctx = &ctx->receiver_ctx->common;
1738 else if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx != NULL)
1739 cctx = &ctx->sender_ctx->common;
1740 else
1741 return -1;
1742
1743 switch(opt) {
1744 case RIST_OPT_THREAD_CALLBACK:
1745 ;
1746 rist_thread_callback_t *thread_callback = optval1;
1747 if (thread_callback == NULL || thread_callback->thread_callback == NULL || optval3 != NULL)
1748 return -1;
1749 if (atomic_load_explicit(&cctx->startup_complete, memory_order_acquire))
1750 return -1;
1751 cctx->thread_callback = thread_callback->thread_callback;
1752 cctx->thread_callback_arg = optval2;
1753 break;
1754 default:
1755 return -1;
1756 }
1757 return 0;
1758 }