Debian Codegraph @

src/rist-private.h

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* librist. Copyright © 2019-2020 SipRadius LLC. All right reserved.
2 * Author: Kuldeep Singh Dhaka <[email protected]>
3 * Author: Sergio Ammirata, Ph.D. <[email protected]>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8 #ifndef RIST_RIST_PRIVATE_H
9 #define RIST_RIST_PRIVATE_H
10
11 #include "common/attributes.h"
12
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <sys/types.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <stdbool.h>
19 #include "stdio-shim.h"
20 #include <fcntl.h>
21 #include <inttypes.h>
22 #include <stddef.h>
23 #include "time-shim.h"
24 #include "pthread-shim.h"
25 #include "socket-shim.h"
26 #include "libevsocket.h"
27 #include "librist.h"
28 #include "librist/transport.h"
29 #include "udpsocket.h"
30 #include "crypto/psk.h"
31 #include <errno.h>
32 #include <stdatomic.h>
33 #include "librist/logging.h"
34 #include "proto/gre.h"
35 #include "proto/adv.h"
36
37 struct cJSON;
38
39 #undef RIST_DEPRECATED
40
41 #define UINT16_SIZE (UINT16_MAX + 1)
42
43 /* Forward sequence-number gap. short_seq (Simple/Main) flows wrap at 16 bits;
44 * full 32-bit (Advanced) flows use the true difference so a genuine >64k gap is
45 * not truncated. Inputs are modular counters, so the subtraction is unsigned
46 * (well-defined wrap) before the optional 16-bit mask. */
47 static inline uint32_t rist_seq_gap(uint32_t current, uint32_t last,
48 bool short_seq)
49 {
50 uint32_t gap = current - last;
51 return short_seq ? (gap & UINT16_MAX) : gap;
52 }
53
54 /* Next expected sequence number after `last`. short_seq (Simple/Main) flows
55 * wrap at 16 bits; 32-bit (Advanced) flows use the natural successor. */
56 static inline uint32_t rist_seq_next(uint32_t last, bool short_seq)
57 {
58 uint32_t next = last + 1;
59 return short_seq ? (next & UINT16_MAX) : next;
60 }
61
62 // These control the memory footprint and buffer capacity of the lib
63 // They MUST be a power of two or wrap-around index calculations will break
64 // RIST_SERVER_QUEUE_BUFFERS is the DEFAULT Advanced-profile recovery-ring
65 // capacity (in packets), == UINT16_SIZE << RIST_RECOVERY_DEPTH_DEFAULT (8x).
66 // The ring is heap-allocated per flow/sender and can be resized at runtime to
67 // UINT16_SIZE << depth via rist_recovery_depth_set() / ?recovery-depth=, up to
68 // RIST_RECOVERY_QUEUE_MAX (depth 16, the full 32-bit space). Large depths are
69 // limited by available RAM. Simple/Main are always UINT16_SIZE.
70 #define RIST_SERVER_QUEUE_BUFFERS ((UINT16_SIZE) * 8)
71 #define RIST_RECOVERY_QUEUE_MAX ((uint64_t)(UINT16_SIZE) << 16)
72 #define RIST_RETRY_QUEUE_BUFFERS ((UINT16_SIZE) * 4)
73 #define RIST_OOB_QUEUE_BUFFERS ((UINT16_SIZE) * 2)
74 #define RIST_DATAOUT_QUEUE_BUFFERS (1024)
75 // This will restrict the use of the library to the configured maximum packet size
76 #define RIST_MAX_PACKET_SIZE (10000)
77 #define RIST_RTT_MIN (3)
78
79 /* nack requests are sent every time a data packet is received. */
80 /* this timer will be triggered to ensure we output nacks even when there is no data coming in */
81 #define RIST_MAX_JITTER (5) /* In milliseconds */
82 #define RIST_PING_INTERVAL (100) /* In milliseconds, how long to space ping requests */
83 #define RIST_PBKDF2_HMAC_SHA256_ITERATIONS (1024)
84 #define RIST_AES_KEY_REUSE_TIMES UINT32_MAX
85 #define RIST_MAX_HOSTNAME (128)
86 #define RTCP_FB_FCI_GENERIC_NACK_SIZE (4)
87 #define RIST_MAX_NACKS (200)
88 #define RIST_MAX_NACKS_BYTES RIST_MAX_NACKS*RTCP_FB_FCI_GENERIC_NACK_SIZE
89 // Maximum offset before the payload that the code can use to put in headers
90 //#define RIST_MAX_PAYLOAD_OFFSET (sizeof(struct rist_gre_key_seq) + sizeof(struct rist_protocol_hdr))
91 #define RIST_MAX_HEADER_SIZE 32
92 #define CHECK_BIT(var,pos) !!((var) & (1<<(pos)))
93
94 #define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
95 #define BYTE_TO_BINARY(byte) \
96 (byte & 0x80 ? '1' : '0'), \
97 (byte & 0x40 ? '1' : '0'), \
98 (byte & 0x20 ? '1' : '0'), \
99 (byte & 0x10 ? '1' : '0'), \
100 (byte & 0x08 ? '1' : '0'), \
101 (byte & 0x04 ? '1' : '0'), \
102 (byte & 0x02 ? '1' : '0'), \
103 (byte & 0x01 ? '1' : '0')
104
105 #define STALE_FLOW_TIME (60L * 1000L * RIST_CLOCK) /* in milliseconds */
106
107 enum rist_peer_state {
108 RIST_PEER_STATE_IDLE = 0,
109 RIST_PEER_STATE_PING = 1,
110 RIST_PEER_STATE_CONNECT = 2
111 };
112 struct rist_buffer {
113 void *data;
114 size_t size;
115 uint8_t type;
116 uint16_t src_port;
117 uint16_t dst_port;
118 uint64_t source_time;
119 int8_t use_seq;
120 uint32_t seq;
121 uint16_t seq_rtp;
122
123 uint64_t time;//Time we received the packet
124 uint64_t packet_time;//Timestamp based on the RTP time of the packet
125 uint64_t target_output_time;//packet_time + buffer
126
127 uint8_t fragment_number;
128 uint8_t fragment_final;
129 // TODO: These four are only used by sender ... do I split buffer into sender and receiver?
130 uint64_t last_retry_request;
131 uint8_t transmit_count;
132 uint16_t ts_null_bytes;
133 struct rist_peer *peer;
134
135 struct rist_buffer *next_free;
136 size_t alloc_size;
137 bool free;
138 bool retry_queued;
139 };
140
•141 /* Largest recovery depth whose ring (UINT16_SIZE << depth packets) is
142 * addressable as size_t: the two parallel index arrays cost
143 * sizeof(ptr) + sizeof(uint32_t) per slot. Depth 16 (2^32) only fits a 64-bit
144 * size_t; on a 32-bit size_t the exponent is reduced until it fits. Shared with
145 * the recovery-depth unit test. */
146 static inline int rist_recovery_depth_platform_max(void)
147 {
148 const uint64_t per = (uint64_t)sizeof(struct rist_buffer *) + sizeof(uint32_t);
149 const uint64_t limit = (uint64_t)SIZE_MAX / per;
150 int depth = RIST_RECOVERY_DEPTH_MAX;
151 while (depth > RIST_RECOVERY_DEPTH_MIN &&
152 ((uint64_t)UINT16_SIZE << depth) > limit)
153 depth--;
154 return depth;
155 }
156
157 struct rist_missing_buffer {
158 uint32_t seq;
159 uint64_t next_nack;
160 uint64_t insertion_time;
161 uint32_t nack_count;
162 struct rist_peer *peer;
163 struct rist_missing_buffer *next;
164 };
165
166 struct rist_bandwidth_estimation {
167 size_t bytes;
168 size_t bytes_fast;
169 uint64_t last_bitrate_calctime;
170 uint64_t last_bitrate_calctime_fast;
171 size_t eight_times_bitrate_fast;
172 size_t eight_times_bitrate;
173 size_t bitrate;
174 size_t bitrate_fast;
175 };
176
177 struct rist_peer_flow_stats {
178 uint32_t lost;
179 uint32_t received;
180 uint32_t dupe;
181 uint32_t dropped_full;
182 uint32_t dropped_late;
183 size_t buffer_duration_count;
184 uint32_t buffer_duration[2048];
185
186 uint32_t missing;
187 uint32_t retries;
188 uint32_t recovered;
189 uint32_t reordered;
190 uint32_t dups;
191 uint32_t recovered_0nack;
192 uint32_t recovered_1nack;
193 uint32_t recovered_2nack;
194 uint32_t recovered_3nack;
195 uint32_t recovered_morenack;
196 uint32_t recovered_sum;
197 uint32_t recovered_average;
198 int32_t recovered_slope;
199 uint32_t recovered_slope_inverted;
200
201 /* Inter-packet spacing */
202 uint64_t min_ips;
203 uint64_t max_ips;
204 uint64_t cur_ips;
205 uint32_t avg_count;
206 uint64_t total_ips;
207
208 /* Per-byte counters */
209 uint64_t received_bytes;
210 };
211
212 struct rist_peer_sender_stats {
213 uint64_t sent;
214 uint32_t received;
215 uint32_t retrans;
216 uint32_t bloat_skip;
217 uint32_t bandwidth_skip;
218 uint32_t retrans_skip;
219 uint64_t ts_null;
220 uint64_t sent_bytes;
221 uint64_t retransmitted_bytes;
222 };
223
224 struct rist_peer_receiver_stats {
225 uint32_t sent_rtcp;
226 uint32_t received_rtcp;
227 uint64_t received;
228 uint64_t ts_null;
229 uint64_t received_bytes;
230 };
231
232 struct nacks {
233 uint32_t array[RIST_MAX_NACKS];
234 size_t counter;
235 };
236
237 struct rist_flow {
238 atomic_int shutdown;
239 int max_output_jitter;
240
241 struct rist_buffer **receiver_queue; /* output queue, heap-allocated to receiver_queue_max */
242
243 pthread_rwlock_t queue_lock;
244
245 bool receiver_queue_has_items;
246 bool flow_auto_buffer_scaling;
247 bool currently_scaling_buffer;
248 atomic_ulong receiver_queue_size; /* size in bytes */
249 uint64_t recovery_buffer_ticks; /* size in ticks */
250 uint64_t stats_report_time; /* in ticks */
251 atomic_ulong receiver_queue_output_idx; /* next packet to output */
252 size_t receiver_queue_max;
253 bool flag_flow_buffer_start;
254
255 /* Missing incoming packets, waiting for retransmission */
256 struct rist_missing_buffer *missing;
257 struct rist_missing_buffer *missing_tail;
258 uint32_t missing_counter;
259
260 struct rist_peer_flow_stats stats_instant;
261 struct rist_peer_flow_stats stats_total;//TODO: use the total stats!
262 struct rist_bandwidth_estimation bw;
263 struct rist_bandwidth_estimation bw_tsnull;
264 struct rist_bandwidth_estimation bw_rejected;
265 struct rist_bandwidth_estimation bw_retries;
266 uint64_t stats_next_time;
267 uint64_t checks_next_time;
268
269 /* Missing queue max size */
270 uint32_t missing_counter_max;
271
272 uint32_t flow_id;
273 uint32_t flow_id_actual;
274 int dead;
275 struct rist_flow *next;
276 struct rist_peer **peer_lst;
277 size_t peer_lst_len;
278 uint32_t last_seq_output;
279 uint64_t last_seq_output_source_time;
280 uint32_t last_seq_found;
281 intptr_t receiver_id;
282 intptr_t sender_id;
283 uint64_t last_ipstats_time;
284 uint64_t last_output_time;
285 uint64_t max_source_time;
286 uint64_t too_late_ctr;
287
288 size_t offset_recalc_sample_count;
289 uint64_t offset_recalc_samples[2048];
290
291 int64_t time_offset;//Current offset between our clock and RTP packets.
292 int64_t time_offset_old;//Old offset between our clock and RTP packets.
293 uint64_t time_offset_changed_ts;//Timestamp the RTP counter last wrapped
294 uint64_t last_packet_ts;//Last packet time
295 bool rtc_timing_mode;
296
297 bool authenticated;
298
299 /* Receiver thread variables */
300 pthread_t receiver_thread;
301 bool receiver_thread_running;
302 /* data out thread signaling */
303 pthread_cond_t condition;
304 pthread_mutex_t mutex;
305
306 /* variable used for seq number length (16bit or 32bit) */
307 bool short_seq;
308
309 /* Scope-B merge auto-detection: set by keepalive L bit */
310 bool merge_auto_enabled;
311
312 /* Session timeouts variables */
313 uint64_t session_timeout;
314 uint64_t flow_timeout;
315 uint64_t last_recv_ts;
316
317 /* Receiver timed async data output */
318 struct rist_data_block **dataout_fifo_queue;
319 size_t dataout_fifo_queue_bytesize;
320 atomic_ulong dataout_fifo_queue_read_index;
321 atomic_ulong dataout_fifo_queue_write_index;
322 atomic_bool fifo_overflow;
323
324 /* Temporary buffer for grouping and sending nacks */
325 struct nacks nacks;
326 struct rist_logging_settings *logging_settings;
327 };
328
329 struct rist_retry {
330 struct rist_peer *peer;
331 uint64_t insert_time;
332 uint32_t seq;
333 bool active;//signal whether this retry has been consumed (false) or not
334 };
335
336 struct rist_common_ctx {
337 atomic_int shutdown;
338 atomic_bool startup_complete;
339
340 /* Used by logging */
341 intptr_t sender_id;
342 intptr_t receiver_id;
343 struct rist_logging_settings *logging_settings;
344
345
346 /* Flows */
347 struct rist_flow *FLOWS;
348 pthread_mutex_t flows_lock;
349
350 /* evsocket */
351 struct evsocket_ctx *evctx;
352
353 /* Timers */
354 int rist_max_jitter;
355
356 /* Recovery buffer RTT multiplier (default 7, per RIST spec) */
357 int recovery_rtt_multiplier;
358
359 /* Advanced-profile recovery-ring capacity in packets (power of two).
360 * Default RIST_SERVER_QUEUE_BUFFERS; tunable before rist_start() via
361 * rist_recovery_depth_set(). Simple/Main ignore this and use
362 * UINT16_SIZE. */
363 size_t recovery_queue_max;
364
365 /* Peer list sync - RW locks */
366 struct rist_peer *PEERS;
367 pthread_mutex_t peerlist_lock;
368
369 /* buffers */
370 /* these are pre-allocated buffers, not pre-allocated aligned stack */
371 struct {
372 uint8_t enc[RIST_MAX_PACKET_SIZE];
373 uint8_t dec[RIST_MAX_PACKET_SIZE];
374 uint8_t recv[RIST_MAX_PACKET_SIZE];
375 uint8_t recv_npd[RIST_MAX_PACKET_SIZE];
376 uint8_t rtcp[RIST_MAX_PACKET_SIZE];
377 } buf;
378 struct rist_buffer *rist_free_buffer;
379 pthread_mutex_t rist_free_buffer_mutex;
380 uint64_t rist_free_buffer_count;
381
382 /* timers */
383 uint64_t nacks_next_time;
384 uint64_t stats_report_time;
385
386 enum rist_profile profile;
387 /* Set by rist_*_start() and by rist_peer_create() once a peer with
388 * a definite profile has been inserted. When set, rist_peer_create
389 * refuses any peer whose ?profile= disagrees with cctx->profile. */
390 atomic_bool profile_locked;
391 uint8_t cname[RIST_MAX_HOSTNAME];
392
393 /* seq variables */
394 uint32_t seq;
395 uint16_t seq_rtp;
396
397 /* Advanced Profile (TR-06-3) state */
398 uint32_t adv_ssrc_base; /* Even SSRC for Protected flow */
399 uint32_t adv_seq_protected; /* 32-bit seq counter for even SSRC */
400 uint32_t adv_seq_unprotected; /* 32-bit seq counter for odd SSRC */
401
402 /* Peer counter (only the ones created by the API) */
403 uint32_t peer_counter;
404
405 /* Auth callback variables */
406 struct {
407 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);
408 int (*disconn_cb)(void *arg, struct rist_peer *peer);
409 void *arg;
410 } auth;
411
412 /* Out-of-band data variables */
413 int (*oob_data_callback)(void *arg, const struct rist_oob_block *oob_block);
414 void *oob_data_callback_argument;
415 bool oob_data_enabled;
416 struct rist_peer *oob_current_peer;
417
418 int (*stats_callback)(void *arg, const struct rist_stats *stats_container);
419 void *stats_callback_argument;
420 pthread_mutex_t stats_lock;
421
422 pthread_rwlock_t oob_queue_lock;
423 struct rist_buffer *oob_queue[RIST_OOB_QUEUE_BUFFERS]; /* oob queue */
424 size_t oob_queue_bytesize;
425 uint16_t oob_queue_read_index;
426 uint16_t oob_queue_write_index;
427
428 bool debug;
429 uint32_t birthtime_rtp_offset;
430
431 /* Pluggable transport (default: POSIX sockets) */
432 struct rist_transport_ops transport;
433 bool transport_active;
434
435 /* Connection status callback */
436 connection_status_callback_t connection_status_callback;
437 void *connection_status_callback_argument;
438
439 rist_thread_callback_func_t thread_callback;
440 void *thread_callback_arg;
441 };
442
443 struct rist_receiver {
444 /* data out thread signaling for fifo */
445 pthread_cond_t condition;
446 pthread_mutex_t mutex;
447
448 /* Receiver data callback */
449 receiver_data_callback2_t receiver_data_callback;
450 void *receiver_data_callback_argument;
451 int receiver_data_ready_notify_fd;
452
453 /* Data fd output (tunnel mode) — write received data directly to fd */
454 int receiver_data_fd;
455 uint32_t receiver_data_fd_flags;
456 atomic_uint_fast64_t data_fd_rx_packets;
457 atomic_uint_fast64_t data_fd_rx_bytes;
458
459 /* Receiver session timeout callback */
460 receiver_session_timeout_callback_t receiver_session_timeout_callback;
461 void *receiver_session_timeout_callback_argument;
462
463 /* Receiver flow attribute callback (Advanced Profile CI=0x8001) */
464 receiver_flow_attr_callback_t receiver_flow_attr_callback;
465 void *receiver_flow_attr_callback_argument;
466
467 /* Receiver thread variables */
468 bool protocol_running;
469 pthread_t receiver_thread;
470
471 /* Reporting id */
472 intptr_t id;
473
474 /* Common stuff */
475 struct rist_common_ctx common;
476
477 enum rist_nack_type nack_type;
478
479 bool simulate_loss;
480 uint16_t loss_percentage;
481 uint32_t fifo_queue_size;
482
483 uint32_t merge_mode;
484 uint64_t stats_pairs_merged;
485 uint64_t stats_orphan_first_delivered;
486 uint64_t stats_orphan_last_delivered;
487 };
488
489 struct rist_sender {
490 /* Advertised flow for this context */
491 uint32_t adv_flow_id;
492
493 /* max bitrate of all sender peers (sets the buffer size on sender queue) */
494 uint32_t recovery_maxbitrate_max;
495 uint32_t max_nacksperloop;
496 bool null_packet_suppression;
497
498 uint32_t split_mode;
499 uint64_t stats_pairs_emitted;
500 uint64_t stats_split_fallback_not_ts;
501
502 /* Sender thread variables */
503 bool protocol_running;
504 pthread_t sender_thread;
505 /* data/nacks out thread signaling */
506 pthread_cond_t condition;
507 pthread_mutex_t mutex;
508
509 bool sender_initialized;
510 uint32_t total_weight;
511 struct rist_buffer **sender_queue; /* input queue, heap-allocated to sender_queue_max */
512 size_t sender_queue_bytesize;
513 size_t sender_queue_size;
514 size_t sender_queue_timelength;
515 size_t sender_buffer_size; /* dynamic buffer size for retry window */
516 size_t sender_queue_delete_index;
517 atomic_ulong sender_queue_read_index;
518 atomic_ulong sender_queue_write_index;
519 size_t sender_queue_max;
520 int weight_counter;
521 uint64_t last_datagram_time;
522 bool simulate_loss;
523 uint16_t loss_percentage;
524 uint64_t stats_next_time;
525 uint64_t checks_next_time;
526 uint32_t session_timeout;
527
528 /* retry queue */
529 struct rist_retry *sender_retry_queue;
530 size_t sender_retry_queue_write_index;
531 size_t sender_retry_queue_read_index;
532 size_t sender_retry_queue_size;
533 uint64_t cooldown_time;
534 int cooldown_mode;
535
536 /* Recovery - heap-allocated to sender_queue_max so Advanced Profile
537 * can index with the full 32-bit seq space (seq & (queue_max - 1)). */
538 uint32_t *seq_index;
539 size_t sender_recover_min_time;
540 size_t sender_queue_buffer_size;
541
542 /* Reporting id */
543 intptr_t id;
544
545 /* flow_id time-related */
546 struct timeval time;
547
548 /* Common stuff */
549 struct rist_common_ctx common;
550
551 /* Peer tracking */
552 struct rist_peer **peer_lst;
553 size_t peer_lst_len;
554
555 /* Queue lock for fifo buffer */
556 pthread_mutex_t queue_lock;
557
558 /* sender stats callback */
559 int (*sender_stats_callback)(void *arg, uint16_t version, char *stats_json, uint32_t json_size);
560 void *sender_stats_callback_argument;
561 uint64_t stats_report_time; /* in ticks */
562
563 /* Data fd input (tunnel mode) — read from fd and send as RIST data */
564 int data_fd;
565 size_t data_fd_max_packet_size;
566 uint32_t data_fd_flags;
567 pthread_t data_fd_thread;
568 bool data_fd_thread_started;
569 atomic_uint_fast64_t data_fd_tx_packets;
570 atomic_uint_fast64_t data_fd_tx_bytes;
571 };
572
573 enum rist_ctx_mode {
574 RIST_SENDER_MODE,
575 RIST_RECEIVER_MODE
576 };
577
578 struct rist_ctx {
579 enum rist_ctx_mode mode;
580 struct rist_sender *sender_ctx;
581 struct rist_receiver *receiver_ctx;
582 };
583
584 struct rist_peer {
585 /* linked list */
586 pthread_mutex_t peer_lock;//Currently only used for setting password & in sending/receiving
587 struct rist_peer *next;
588 struct rist_peer *prev;
589
590 /* For simple profile authentication chain (data and rtcp on different ports) */
591 struct rist_peer *peer_rtcp;
592 struct rist_peer *peer_data;
593 bool handled_first;
594 bool is_rtcp;
595 bool is_data;
596 /* sender only: peer is known to respond to echo requests use those to calculate RTT instead */
597 bool echo_enabled;
598
599 /* For keeping track of the connection that initiated a peer */
600 struct rist_peer *parent;
601 struct rist_peer *sibling_prev;
602 struct rist_peer *sibling_next;
603 struct rist_peer *child;
604 uint32_t child_alive_count;
605
606 /* Flow for incoming traffic */
607 struct rist_flow *flow;
608
609 /* Advertised flow id to force peer selection */
610 uint32_t adv_flow_id;
611
612 /* Identifiers for multipeer links */
613 uint32_t adv_peer_id;
614 uint32_t peer_ssrc;
615
616 char receiver_name[RIST_MAX_HOSTNAME];
617
618 /* Config */
619 struct rist_peer_config config;
620 uint64_t sender_max_buffer_ticks;
621 uint64_t recovery_buffer_ticks;
622
623 bool buffer_bloat_active;
624
625 bool receiver_mode;
626
627 int sd;
628
629 /* State */
630 bool authenticated;
631
632 /* Data sending */
633 uint32_t seq;
634 uint64_t eight_times_rtt;
635 uint32_t w_count; /* Counter for weight in distributed send */
636
637 /* RTT statistics */
638 uint64_t last_rtt;
639
640 /* Missing queue max size */
641 uint32_t missing_counter_max;
642
643 /* Encryption */
644 bool supports_otf_passphrase_change;
645 struct rist_key key_tx; // used for transmitted packets
646 struct rist_key key_rx; // used for received packets
647 bool key_rx_odd_active;
648 bool key_tx_odd_active;
649 struct rist_key key_tx_odd; // used for transmitted packets
650 struct rist_key key_rx_odd; // used for received packets
651 bool rolling_over_passphrase;
652 struct eapsrp_ctx *eap_ctx;
653 int eap_authentication_state;
654 uint8_t rist_gre_version;
655
656 /* Advanced Profile (TR-06-3) peer state */
657 bool is_advanced; /* Peer operating in Advanced Profile mode */
658 bool remote_supports_advanced; /* Remote advertised I=1 in keep-alive */
659
660 /* compression flag (sender only) */
661 bool compression;
662
663 /* Addressing */
664 uint16_t local_port;
665 uint16_t remote_port;
666 union {
667 struct sockaddr address;
668 struct sockaddr_in inaddr;
669 struct sockaddr_in6 inaddr6;
670 struct sockaddr_storage storage;
671 } u;
672 socklen_t address_len;
673 uint16_t address_family;
674 uint16_t state;
675 char miface[128];
676
677 /* Events */
678 struct timeval expire;
679 bool send_keepalive;
680 struct evsocket_event *event_recv;
681
682 /* listening mode with @ */
683 bool listening;
684 /* multicast */
685 bool multicast_sender;
686 bool multicast_receiver;
687
688 /* rist ctx */
689 struct rist_sender *sender_ctx;
690 struct rist_receiver *receiver_ctx;
691
692 /* Reflector role flags */
693 bool is_reflector_publisher;
694
695 /* rist buffer bloating counteract */
696 uint64_t cooldown_time;
697
698 /* Statistics Sender */
699 struct rist_peer_sender_stats stats_sender_instant;
700 struct rist_peer_sender_stats stats_sender_total;
701
702
703 /* Statistics Receiver */
704 struct rist_peer_receiver_stats stats_receiver_instant;
705 struct rist_peer_receiver_stats stats_receiver_total;
706
707 int dead;
708 int timed_out;
709 uint64_t dead_since;
710 /* Caller-side socket rebind bookkeeping. */
711 uint64_t last_rebind_time;
712 uint32_t rebind_attempts;
713 uint64_t birthtime_peer;
714 uint64_t birthtime_local;
715
716 /* bw estimation */
717 struct rist_bandwidth_estimation bw;
718 struct rist_bandwidth_estimation retry_bw;
719 struct rist_bandwidth_estimation ts_nulls_bw;
720
721 /* shutting down flag */
722 atomic_bool shutdown;
723
724 /* Timers */
725 uint32_t rtcp_keepalive_interval;
726 uint64_t next_periodic_rtcp;
727 uint64_t next_keepalive_packet;
728 uint64_t next_flow_attr;
729 uint64_t session_timeout;
730 uint64_t last_pkt_received;
731 uint64_t last_sender_report_time;
732 uint64_t last_sender_report_ts;
733 uint64_t ts_null_bytes;
734
735 char *url;
736 char cname[RIST_MAX_HOSTNAME];
737 uint8_t mac_addr[6];
738 bool send_first_connection_event;
739
740 /* Rate-limit for EMSGSIZE/PMTU-too-large send errors so a flow of
741 * oversized packets doesn't drown the log. Holds the last log time
742 * in NTP ticks. */
743 uint64_t last_pmtu_error_log;
744
745 uint64_t log_repeat_timer;
746
747 uint8_t data[SIZEOF_GRE_KEEPALIVE];
748
749 };
750
751 static inline struct rist_common_ctx *rist_struct_get_common(struct rist_ctx *ctx) {
752 if (RIST_UNLIKELY(!ctx))
753 return NULL;
754 if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx)
755 return &ctx->sender_ctx->common;
756 else if (ctx->mode == RIST_RECEIVER_MODE && ctx->receiver_ctx)
757 return &ctx->receiver_ctx->common;
758 else
759 return NULL;
760 }
761
762 /* defined in flow.c */
763 RIST_PRIV void rist_receiver_flow_statistics(struct rist_receiver *ctx, struct rist_flow *flow);
764 RIST_PRIV struct cJSON *rist_sender_peer_statistics(struct rist_peer *peer);
765 RIST_PRIV void rist_sender_flow_statistics(struct rist_sender *ctx);
766 RIST_PRIV void rist_delete_flow(struct rist_receiver *ctx, struct rist_flow *f);
767 RIST_PRIV void rist_receiver_missing(struct rist_flow *f, struct rist_peer *peer,uint64_t nack_time, uint32_t seq, uint64_t rtt);
768 RIST_PRIV int rist_receiver_associate_flow(struct rist_peer *p, uint32_t flow_id);
769 RIST_PRIV size_t rist_best_rtt_index(struct rist_flow *f);
770 RIST_PRIV struct rist_buffer *rist_new_buffer(struct rist_common_ctx *ctx, const void *buf, size_t len, uint8_t type, uint32_t seq, uint64_t source_time, uint16_t src_port, uint16_t dst_port);
771 RIST_PRIV void free_rist_buffer(struct rist_common_ctx *ctx, struct rist_buffer *b);
772 RIST_PRIV void rist_calculate_bitrate(size_t len, struct rist_bandwidth_estimation *bw);
773 RIST_PRIV void rist_refresh_flow_bitrate(struct rist_bandwidth_estimation *bw);
774 RIST_PRIV void empty_receiver_queue(struct rist_flow *f, struct rist_common_ctx *ctx);
775 RIST_PRIV void rist_flush_missing_flow_queue(struct rist_flow *flow);
776
777 /* defined in rist-common.c */
778 RIST_PRIV void rist_peer_authenticate(struct rist_peer *peer);
779 RIST_PRIV void rist_shutdown_peer(struct rist_peer *peer);
780 RIST_PRIV void rist_print_inet_info(char *prefix, struct rist_peer *peer);
781 RIST_PRIV void rist_peer_rtcp(struct evsocket_ctx *ctx, void *arg);
782 RIST_PRIV void rist_populate_cname(struct rist_peer *peer);
783 RIST_PRIV void free_data_block(struct rist_data_block **const block);
784
785 /* needed after splitting up */
786 RIST_PRIV PTHREAD_START_FUNC(sender_pthread_protocol, arg);
787 RIST_PRIV PTHREAD_START_FUNC(receiver_pthread_protocol, arg);
788 RIST_PRIV int rist_max_jitter_set(struct rist_common_ctx *ctx, int t);
789 RIST_PRIV int rist_recovery_rtt_multiplier_set_internal(struct rist_common_ctx *ctx, int multiplier);
790 RIST_PRIV int parse_url_options(const char *url, struct rist_peer_config *output_peer_config);
791 RIST_PRIV int parse_url_udp_options(const char *url, struct rist_udp_config *output_udp_config);
792 RIST_PRIV struct rist_peer *rist_receiver_peer_insert_local(struct rist_receiver *ctx,
793 const struct rist_peer_config *config);
794 RIST_PRIV void rist_sender_destroy_local(struct rist_sender *ctx);
795 RIST_PRIV void rist_receiver_destroy_local(struct rist_receiver *ctx);
796 RIST_PRIV struct rist_peer *rist_sender_peer_insert_local(struct rist_sender *ctx,
797 const struct rist_peer_config *config, bool b_rtcp);
798 RIST_PRIV void rist_fsm_init_comm(struct rist_peer *peer);
799 RIST_PRIV int rist_oob_enqueue(struct rist_common_ctx *ctx, struct rist_peer *peer, const void *buf, size_t len);
800 RIST_PRIV int init_common_ctx(struct rist_common_ctx *ctx, enum rist_profile profile);
801 RIST_PRIV void init_advanced_state(struct rist_common_ctx *ctx);
802 RIST_PRIV int rist_peer_remove(struct rist_common_ctx *ctx, struct rist_peer *peer, struct rist_peer **next);
803 RIST_PRIV int rist_auth_handler(struct rist_common_ctx *ctx,
804 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),
805 int (*disconn_cb)(void *arg, struct rist_peer *peer),
806 void *arg);
807 RIST_PRIV void sender_peer_append(struct rist_sender *ctx, struct rist_peer *peer);
808
809 /* Get common context */
810 RIST_PRIV struct rist_common_ctx *get_cctx(struct rist_peer *peer);
811
812 /*static inline in header file */
813 static inline void peer_append(struct rist_peer *p)
814 {
815 struct rist_common_ctx *cctx = get_cctx(p);
816 struct rist_peer **PEERS = &cctx->PEERS;
817 struct rist_peer *plist = *PEERS;
818 if (!plist)
819 {
820 *PEERS = p;
821 return;
822 }
823 if (p->parent)
824 {
825 struct rist_peer *peer = p->parent;
826 if (!peer->child) {
827 peer->child = p;
828 if (peer->sender_ctx)
829 peer->sender_ctx->total_weight += p->config.weight;
830 }
831 else
832 {
833 struct rist_peer *child = peer->child;
834 while (child)
835 {
836 if (!child->sibling_next)
837 {
838 child->sibling_next = p;
839 p->sibling_prev = child;
840 break;
841 }
842 child = child->sibling_next;
843 }
844 }
845 ++peer->child_alive_count;
846 }
847 while (plist)
848 {
849 if (!plist->next)
850 {
851 p->prev = plist;
852 plist->next = p;
853 return;
854 }
855 plist = plist->next;
856 }
857 }
858
859 #endif