-
Notifications
You must be signed in to change notification settings - Fork 933
/
res_rtp_asterisk.c
9876 lines (8451 loc) · 326 KB
/
res_rtp_asterisk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2008, Digium, Inc.
*
* Mark Spencer <markster@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*!
* \file
*
* \brief Supports RTP and RTCP with Symmetric RTP support for NAT traversal.
*
* \author Mark Spencer <markster@digium.com>
*
* \note RTP is defined in RFC 3550.
*
* \ingroup rtp_engines
*/
/*** MODULEINFO
<use type="external">openssl</use>
<use type="external">pjproject</use>
<support_level>core</support_level>
***/
#include "asterisk.h"
#include <arpa/nameser.h>
#include "asterisk/dns_core.h"
#include "asterisk/dns_internal.h"
#include "asterisk/dns_recurring.h"
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>
#include <math.h>
#ifdef HAVE_OPENSSL
#include <openssl/opensslconf.h>
#include <openssl/opensslv.h>
#if !defined(OPENSSL_NO_SRTP) && (OPENSSL_VERSION_NUMBER >= 0x10001000L)
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L)
#include <openssl/bn.h>
#endif
#ifndef OPENSSL_NO_DH
#include <openssl/dh.h>
#endif
#endif
#endif
#ifdef HAVE_PJPROJECT
#include <pjlib.h>
#include <pjlib-util.h>
#include <pjnath.h>
#include <ifaddrs.h>
#endif
#include "asterisk/conversions.h"
#include "asterisk/options.h"
#include "asterisk/logger_category.h"
#include "asterisk/stun.h"
#include "asterisk/pbx.h"
#include "asterisk/frame.h"
#include "asterisk/format_cache.h"
#include "asterisk/channel.h"
#include "asterisk/acl.h"
#include "asterisk/config.h"
#include "asterisk/lock.h"
#include "asterisk/utils.h"
#include "asterisk/cli.h"
#include "asterisk/manager.h"
#include "asterisk/unaligned.h"
#include "asterisk/module.h"
#include "asterisk/rtp_engine.h"
#include "asterisk/smoother.h"
#include "asterisk/uuid.h"
#include "asterisk/test.h"
#include "asterisk/data_buffer.h"
#ifdef HAVE_PJPROJECT
#include "asterisk/res_pjproject.h"
#include "asterisk/security_events.h"
#endif
#define MAX_TIMESTAMP_SKEW 640
#define RTP_SEQ_MOD (1<<16) /*!< A sequence number can't be more than 16 bits */
#define RTCP_DEFAULT_INTERVALMS 5000 /*!< Default milli-seconds between RTCP reports we send */
#define RTCP_MIN_INTERVALMS 500 /*!< Min milli-seconds between RTCP reports we send */
#define RTCP_MAX_INTERVALMS 60000 /*!< Max milli-seconds between RTCP reports we send */
#define DEFAULT_RTP_START 5000 /*!< Default port number to start allocating RTP ports from */
#define DEFAULT_RTP_END 31000 /*!< Default maximum port number to end allocating RTP ports at */
#define MINIMUM_RTP_PORT 1024 /*!< Minimum port number to accept */
#define MAXIMUM_RTP_PORT 65535 /*!< Maximum port number to accept */
#define DEFAULT_TURN_PORT 3478
#define TURN_STATE_WAIT_TIME 2000
#define DEFAULT_RTP_SEND_BUFFER_SIZE 250 /*!< The initial size of the RTP send buffer */
#define MAXIMUM_RTP_SEND_BUFFER_SIZE (DEFAULT_RTP_SEND_BUFFER_SIZE + 200) /*!< Maximum RTP send buffer size */
#define DEFAULT_RTP_RECV_BUFFER_SIZE 20 /*!< The initial size of the RTP receiver buffer */
#define MAXIMUM_RTP_RECV_BUFFER_SIZE (DEFAULT_RTP_RECV_BUFFER_SIZE + 20) /*!< Maximum RTP receive buffer size */
#define OLD_PACKET_COUNT 1000 /*!< The number of previous packets that are considered old */
#define MISSING_SEQNOS_ADDED_TRIGGER 2 /*!< The number of immediate missing packets that will trigger an immediate NACK */
#define SEQNO_CYCLE_OVER 65536 /*!< The number after the maximum allowed sequence number */
/*! Full INTRA-frame Request / Fast Update Request (From RFC2032) */
#define RTCP_PT_FUR 192
/*! Sender Report (From RFC3550) */
#define RTCP_PT_SR AST_RTP_RTCP_SR
/*! Receiver Report (From RFC3550) */
#define RTCP_PT_RR AST_RTP_RTCP_RR
/*! Source Description (From RFC3550) */
#define RTCP_PT_SDES 202
/*! Goodbye (To remove SSRC's from tables) (From RFC3550) */
#define RTCP_PT_BYE 203
/*! Application defined (From RFC3550) */
#define RTCP_PT_APP 204
/* VP8: RTCP Feedback */
/*! Payload Specific Feed Back (From RFC4585 also RFC5104) */
#define RTCP_PT_PSFB AST_RTP_RTCP_PSFB
#define RTP_MTU 1200
#define DTMF_SAMPLE_RATE_MS 8 /*!< DTMF samples per millisecond */
#define DEFAULT_DTMF_TIMEOUT (150 * (8000 / 1000)) /*!< samples */
#define ZFONE_PROFILE_ID 0x505a
#define DEFAULT_LEARNING_MIN_SEQUENTIAL 4
/*!
* \brief Calculate the min learning duration in ms.
*
* \details
* The min supported packet size represents 10 ms and we need to account
* for some jitter and fast clocks while learning. Some messed up devices
* have very bad jitter for a small packet sample size. Jitter can also
* be introduced by the network itself.
*
* So we'll allow packets to come in every 9ms on average for fast clocking
* with the last one coming in 5ms early for jitter.
*/
#define CALC_LEARNING_MIN_DURATION(count) (((count) - 1) * 9 - 5)
#define DEFAULT_LEARNING_MIN_DURATION CALC_LEARNING_MIN_DURATION(DEFAULT_LEARNING_MIN_SEQUENTIAL)
#define SRTP_MASTER_KEY_LEN 16
#define SRTP_MASTER_SALT_LEN 14
#define SRTP_MASTER_LEN (SRTP_MASTER_KEY_LEN + SRTP_MASTER_SALT_LEN)
#define RTP_DTLS_ESTABLISHED -37
enum strict_rtp_state {
STRICT_RTP_OPEN = 0, /*! No RTP packets should be dropped, all sources accepted */
STRICT_RTP_LEARN, /*! Accept next packet as source */
STRICT_RTP_CLOSED, /*! Drop all RTP packets not coming from source that was learned */
};
enum strict_rtp_mode {
STRICT_RTP_NO = 0, /*! Don't adhere to any strict RTP rules */
STRICT_RTP_YES, /*! Strict RTP that restricts packets based on time and sequence number */
STRICT_RTP_SEQNO, /*! Strict RTP that restricts packets based on sequence number */
};
/*!
* \brief Strict RTP learning timeout time in milliseconds
*
* \note Set to 5 seconds to allow reinvite chains for direct media
* to settle before media actually starts to arrive. There may be a
* reinvite collision involved on the other leg.
*/
#define STRICT_RTP_LEARN_TIMEOUT 5000
#define DEFAULT_STRICT_RTP STRICT_RTP_YES /*!< Enabled by default */
#define DEFAULT_SRTP_REPLAY_PROTECTION 1
#define DEFAULT_ICESUPPORT 1
#define DEFAULT_STUN_SOFTWARE_ATTRIBUTE 1
#define DEFAULT_DTLS_MTU 1200
extern struct ast_srtp_res *res_srtp;
extern struct ast_srtp_policy_res *res_srtp_policy;
static int dtmftimeout = DEFAULT_DTMF_TIMEOUT;
static int rtpstart = DEFAULT_RTP_START; /*!< First port for RTP sessions (set in rtp.conf) */
static int rtpend = DEFAULT_RTP_END; /*!< Last port for RTP sessions (set in rtp.conf) */
static int rtcpstats; /*!< Are we debugging RTCP? */
static int rtcpinterval = RTCP_DEFAULT_INTERVALMS; /*!< Time between rtcp reports in millisecs */
static struct ast_sockaddr rtpdebugaddr; /*!< Debug packets to/from this host */
static struct ast_sockaddr rtcpdebugaddr; /*!< Debug RTCP packets to/from this host */
static int rtpdebugport; /*!< Debug only RTP packets from IP or IP+Port if port is > 0 */
static int rtcpdebugport; /*!< Debug only RTCP packets from IP or IP+Port if port is > 0 */
#ifdef SO_NO_CHECK
static int nochecksums;
#endif
static int strictrtp = DEFAULT_STRICT_RTP; /*!< Only accept RTP frames from a defined source. If we receive an indication of a changing source, enter learning mode. */
static int learning_min_sequential = DEFAULT_LEARNING_MIN_SEQUENTIAL; /*!< Number of sequential RTP frames needed from a single source during learning mode to accept new source. */
static int learning_min_duration = DEFAULT_LEARNING_MIN_DURATION; /*!< Lowest acceptable timeout between the first and the last sequential RTP frame. */
static int srtp_replay_protection = DEFAULT_SRTP_REPLAY_PROTECTION;
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
static int dtls_mtu = DEFAULT_DTLS_MTU;
#endif
#ifdef HAVE_PJPROJECT
static int icesupport = DEFAULT_ICESUPPORT;
static int stun_software_attribute = DEFAULT_STUN_SOFTWARE_ATTRIBUTE;
static struct sockaddr_in stunaddr;
static pj_str_t turnaddr;
static int turnport = DEFAULT_TURN_PORT;
static pj_str_t turnusername;
static pj_str_t turnpassword;
static struct stasis_subscription *acl_change_sub = NULL;
static struct ast_sockaddr lo6 = { .len = 0 };
/*! ACL for ICE addresses */
static struct ast_acl_list *ice_acl = NULL;
static ast_rwlock_t ice_acl_lock = AST_RWLOCK_INIT_VALUE;
/*! ACL for STUN requests */
static struct ast_acl_list *stun_acl = NULL;
static ast_rwlock_t stun_acl_lock = AST_RWLOCK_INIT_VALUE;
/*! stunaddr recurring resolution */
static ast_rwlock_t stunaddr_lock = AST_RWLOCK_INIT_VALUE;
static struct ast_dns_query_recurring *stunaddr_resolver = NULL;
/*! \brief Pool factory used by pjlib to allocate memory. */
static pj_caching_pool cachingpool;
/*! \brief Global memory pool for configuration and timers */
static pj_pool_t *pool;
/*! \brief Global timer heap */
static pj_timer_heap_t *timer_heap;
/*! \brief Thread executing the timer heap */
static pj_thread_t *timer_thread;
/*! \brief Used to tell the timer thread to terminate */
static int timer_terminate;
/*! \brief Structure which contains ioqueue thread information */
struct ast_rtp_ioqueue_thread {
/*! \brief Pool used by the thread */
pj_pool_t *pool;
/*! \brief The thread handling the queue and timer heap */
pj_thread_t *thread;
/*! \brief Ioqueue which polls on sockets */
pj_ioqueue_t *ioqueue;
/*! \brief Timer heap for scheduled items */
pj_timer_heap_t *timerheap;
/*! \brief Termination request */
int terminate;
/*! \brief Current number of descriptors being waited on */
unsigned int count;
/*! \brief Linked list information */
AST_LIST_ENTRY(ast_rtp_ioqueue_thread) next;
};
/*! \brief List of ioqueue threads */
static AST_LIST_HEAD_STATIC(ioqueues, ast_rtp_ioqueue_thread);
/*! \brief Structure which contains ICE host candidate mapping information */
struct ast_ice_host_candidate {
struct ast_sockaddr local;
struct ast_sockaddr advertised;
unsigned int include_local;
AST_RWLIST_ENTRY(ast_ice_host_candidate) next;
};
/*! \brief List of ICE host candidate mappings */
static AST_RWLIST_HEAD_STATIC(host_candidates, ast_ice_host_candidate);
static char *generate_random_string(char *buf, size_t size);
#endif
#define FLAG_3389_WARNING (1 << 0)
#define FLAG_NAT_ACTIVE (3 << 1)
#define FLAG_NAT_INACTIVE (0 << 1)
#define FLAG_NAT_INACTIVE_NOWARN (1 << 1)
#define FLAG_NEED_MARKER_BIT (1 << 3)
#define FLAG_DTMF_COMPENSATE (1 << 4)
#define FLAG_REQ_LOCAL_BRIDGE_BIT (1 << 5)
#define TRANSPORT_SOCKET_RTP 0
#define TRANSPORT_SOCKET_RTCP 1
#define TRANSPORT_TURN_RTP 2
#define TRANSPORT_TURN_RTCP 3
/*! \brief RTP learning mode tracking information */
struct rtp_learning_info {
struct ast_sockaddr proposed_address; /*!< Proposed remote address for strict RTP */
struct timeval start; /*!< The time learning mode was started */
struct timeval received; /*!< The time of the first received packet */
int max_seq; /*!< The highest sequence number received */
int packets; /*!< The number of remaining packets before the source is accepted */
/*! Type of media stream carried by the RTP instance */
enum ast_media_type stream_type;
};
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
struct dtls_details {
SSL *ssl; /*!< SSL session */
BIO *read_bio; /*!< Memory buffer for reading */
BIO *write_bio; /*!< Memory buffer for writing */
enum ast_rtp_dtls_setup dtls_setup; /*!< Current setup state */
enum ast_rtp_dtls_connection connection; /*!< Whether this is a new or existing connection */
int timeout_timer; /*!< Scheduler id for timeout timer */
};
#endif
#ifdef HAVE_PJPROJECT
/*! An ao2 wrapper protecting the PJPROJECT ice structure with ref counting. */
struct ice_wrap {
pj_ice_sess *real_ice; /*!< ICE session */
};
#endif
/*! \brief Structure used for mapping an incoming SSRC to an RTP instance */
struct rtp_ssrc_mapping {
/*! \brief The received SSRC */
unsigned int ssrc;
/*! True if the SSRC is available. Otherwise, this is a placeholder mapping until the SSRC is set. */
unsigned int ssrc_valid;
/*! \brief The RTP instance this SSRC belongs to*/
struct ast_rtp_instance *instance;
};
/*! \brief Packet statistics (used for transport-cc) */
struct rtp_transport_wide_cc_packet_statistics {
/*! The transport specific sequence number */
unsigned int seqno;
/*! The time at which the packet was received */
struct timeval received;
/*! The delta between this packet and the previous */
int delta;
};
/*! \brief Statistics information (used for transport-cc) */
struct rtp_transport_wide_cc_statistics {
/*! A vector of packet statistics */
AST_VECTOR(, struct rtp_transport_wide_cc_packet_statistics) packet_statistics; /*!< Packet statistics, used for transport-cc */
/*! The last sequence number received */
unsigned int last_seqno;
/*! The last extended sequence number */
unsigned int last_extended_seqno;
/*! How many feedback packets have gone out */
unsigned int feedback_count;
/*! How many cycles have occurred for the sequence numbers */
unsigned int cycles;
/*! Scheduler id for periodic feedback transmission */
int schedid;
};
typedef struct {
unsigned int ts;
unsigned char is_set;
} optional_ts;
/*! \brief RTP session description */
struct ast_rtp {
int s;
/*! \note The f.subclass.format holds a ref. */
struct ast_frame f;
unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
unsigned int ssrc; /*!< Synchronization source, RFC 3550, page 10. */
unsigned int ssrc_orig; /*!< SSRC used before native bridge activated */
unsigned char ssrc_saved; /*!< indicates if ssrc_orig has a value */
char cname[AST_UUID_STR_LEN]; /*!< Our local CNAME */
unsigned int themssrc; /*!< Their SSRC */
unsigned int themssrc_valid; /*!< True if their SSRC is available. */
unsigned int lastts;
unsigned int lastividtimestamp;
unsigned int lastovidtimestamp;
unsigned int lastitexttimestamp;
unsigned int lastotexttimestamp;
int lastrxseqno; /*!< Last received sequence number, from the network */
int expectedrxseqno; /*!< Next expected sequence number, from the network */
AST_VECTOR(, int) missing_seqno; /*!< A vector of sequence numbers we never received */
int expectedseqno; /*!< Next expected sequence number, from the core */
unsigned short seedrxseqno; /*!< What sequence number did they start with?*/
unsigned int seedrxts; /*!< What RTP timestamp did they start with? */
unsigned int rxcount; /*!< How many packets have we received? */
unsigned int rxoctetcount; /*!< How many octets have we received? should be rxcount *160*/
unsigned int txcount; /*!< How many packets have we sent? */
unsigned int txoctetcount; /*!< How many octets have we sent? (txcount*160)*/
unsigned int cycles; /*!< Shifted count of sequence number cycles */
double rxjitter; /*!< Interarrival jitter at the moment in seconds to be reported */
double rxtransit; /*!< Relative transit time for previous packet */
struct ast_format *lasttxformat;
struct ast_format *lastrxformat;
/* DTMF Reception Variables */
char resp; /*!< The current digit being processed */
unsigned int last_seqno; /*!< The last known sequence number for any DTMF packet */
optional_ts last_end_timestamp; /*!< The last known timestamp received from an END packet */
unsigned int dtmf_duration; /*!< Total duration in samples since the digit start event */
unsigned int dtmf_timeout; /*!< When this timestamp is reached we consider END frame lost and forcibly abort digit */
unsigned int dtmfsamples;
enum ast_rtp_dtmf_mode dtmfmode; /*!< The current DTMF mode of the RTP stream */
/* DTMF Transmission Variables */
unsigned int lastdigitts;
char sending_digit; /*!< boolean - are we sending digits */
char send_digit; /*!< digit we are sending */
int send_payload;
int send_duration;
unsigned int flags;
struct timeval rxcore;
struct timeval txcore;
double drxcore; /*!< The double representation of the first received packet */
struct timeval dtmfmute;
struct ast_smoother *smoother;
unsigned short seqno; /*!< Sequence number, RFC 3550, page 13. */
struct ast_sched_context *sched;
struct ast_rtcp *rtcp;
unsigned int asymmetric_codec; /*!< Indicate if asymmetric send/receive codecs are allowed */
struct ast_rtp_instance *bundled; /*!< The RTP instance we are bundled to */
int stream_num; /*!< Stream num for this RTP instance */
AST_VECTOR(, struct rtp_ssrc_mapping) ssrc_mapping; /*!< Mappings of SSRC to RTP instances */
struct ast_sockaddr bind_address; /*!< Requested bind address for the sockets */
enum strict_rtp_state strict_rtp_state; /*!< Current state that strict RTP protection is in */
struct ast_sockaddr strict_rtp_address; /*!< Remote address information for strict RTP purposes */
/*
* Learning mode values based on pjmedia's probation mode. Many of these values are redundant to the above,
* but these are in place to keep learning mode sequence values sealed from their normal counterparts.
*/
struct rtp_learning_info rtp_source_learn; /* Learning mode track for the expected RTP source */
struct rtp_red *red;
struct ast_data_buffer *send_buffer; /*!< Buffer for storing sent packets for retransmission */
struct ast_data_buffer *recv_buffer; /*!< Buffer for storing received packets for retransmission */
struct rtp_transport_wide_cc_statistics transport_wide_cc; /*!< Transport-cc statistics information */
#ifdef HAVE_PJPROJECT
ast_cond_t cond; /*!< ICE/TURN condition for signaling */
struct ice_wrap *ice; /*!< ao2 wrapped ICE session */
enum ast_rtp_ice_role role; /*!< Our role in ICE negotiation */
pj_turn_sock *turn_rtp; /*!< RTP TURN relay */
pj_turn_sock *turn_rtcp; /*!< RTCP TURN relay */
pj_turn_state_t turn_state; /*!< Current state of the TURN relay session */
unsigned int passthrough:1; /*!< Bit to indicate that the received packet should be passed through */
unsigned int rtp_passthrough:1; /*!< Bit to indicate that TURN RTP should be passed through */
unsigned int rtcp_passthrough:1; /*!< Bit to indicate that TURN RTCP should be passed through */
unsigned int ice_port; /*!< Port that ICE was started with if it was previously started */
struct ast_sockaddr rtp_loop; /*!< Loopback address for forwarding RTP from TURN */
struct ast_sockaddr rtcp_loop; /*!< Loopback address for forwarding RTCP from TURN */
struct ast_rtp_ioqueue_thread *ioqueue; /*!< The ioqueue thread handling us */
char remote_ufrag[256]; /*!< The remote ICE username */
char remote_passwd[256]; /*!< The remote ICE password */
char local_ufrag[256]; /*!< The local ICE username */
char local_passwd[256]; /*!< The local ICE password */
struct ao2_container *ice_local_candidates; /*!< The local ICE candidates */
struct ao2_container *ice_active_remote_candidates; /*!< The remote ICE candidates */
struct ao2_container *ice_proposed_remote_candidates; /*!< Incoming remote ICE candidates for new session */
struct ast_sockaddr ice_original_rtp_addr; /*!< rtp address that ICE started on first session */
unsigned int ice_num_components; /*!< The number of ICE components */
unsigned int ice_media_started:1; /*!< ICE media has started, either on a valid pair or on ICE completion */
#endif
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
SSL_CTX *ssl_ctx; /*!< SSL context */
enum ast_rtp_dtls_verify dtls_verify; /*!< What to verify */
enum ast_srtp_suite suite; /*!< SRTP crypto suite */
enum ast_rtp_dtls_hash local_hash; /*!< Local hash used for the fingerprint */
char local_fingerprint[160]; /*!< Fingerprint of our certificate */
enum ast_rtp_dtls_hash remote_hash; /*!< Remote hash used for the fingerprint */
unsigned char remote_fingerprint[EVP_MAX_MD_SIZE]; /*!< Fingerprint of the peer certificate */
unsigned int rekey; /*!< Interval at which to renegotiate and rekey */
int rekeyid; /*!< Scheduled item id for rekeying */
struct dtls_details dtls; /*!< DTLS state information */
#endif
};
/*!
* \brief Structure defining an RTCP session.
*
* The concept "RTCP session" is not defined in RFC 3550, but since
* this structure is analogous to ast_rtp, which tracks a RTP session,
* it is logical to think of this as a RTCP session.
*
* RTCP packet is defined on page 9 of RFC 3550.
*
*/
struct ast_rtcp {
int rtcp_info;
int s; /*!< Socket */
struct ast_sockaddr us; /*!< Socket representation of the local endpoint. */
struct ast_sockaddr them; /*!< Socket representation of the remote endpoint. */
unsigned int soc; /*!< What they told us */
unsigned int spc; /*!< What they told us */
unsigned int themrxlsr; /*!< The middle 32 bits of the NTP timestamp in the last received SR*/
struct timeval rxlsr; /*!< Time when we got their last SR */
struct timeval txlsr; /*!< Time when we sent or last SR*/
unsigned int expected_prior; /*!< no. packets in previous interval */
unsigned int received_prior; /*!< no. packets received in previous interval */
int schedid; /*!< Schedid returned from ast_sched_add() to schedule RTCP-transmissions*/
unsigned int rr_count; /*!< number of RRs we've sent, not including report blocks in SR's */
unsigned int sr_count; /*!< number of SRs we've sent */
unsigned int lastsrtxcount; /*!< Transmit packet count when last SR sent */
double accumulated_transit; /*!< accumulated a-dlsr-lsr */
double rtt; /*!< Last reported rtt */
unsigned int reported_jitter; /*!< The contents of their last jitter entry in the RR */
unsigned int reported_lost; /*!< Reported lost packets in their RR */
double reported_maxjitter; /*!< Maximum reported interarrival jitter */
double reported_minjitter; /*!< Minimum reported interarrival jitter */
double reported_normdev_jitter; /*!< Mean of reported interarrival jitter */
double reported_stdev_jitter; /*!< Standard deviation of reported interarrival jitter */
unsigned int reported_jitter_count; /*!< Reported interarrival jitter count */
double reported_maxlost; /*!< Maximum reported packets lost */
double reported_minlost; /*!< Minimum reported packets lost */
double reported_normdev_lost; /*!< Mean of reported packets lost */
double reported_stdev_lost; /*!< Standard deviation of reported packets lost */
unsigned int reported_lost_count; /*!< Reported packets lost count */
double rxlost; /*!< Calculated number of lost packets since last report */
double maxrxlost; /*!< Maximum calculated lost number of packets between reports */
double minrxlost; /*!< Minimum calculated lost number of packets between reports */
double normdev_rxlost; /*!< Mean of calculated lost packets between reports */
double stdev_rxlost; /*!< Standard deviation of calculated lost packets between reports */
unsigned int rxlost_count; /*!< Calculated lost packets sample count */
double maxrxjitter; /*!< Maximum of calculated interarrival jitter */
double minrxjitter; /*!< Minimum of calculated interarrival jitter */
double normdev_rxjitter; /*!< Mean of calculated interarrival jitter */
double stdev_rxjitter; /*!< Standard deviation of calculated interarrival jitter */
unsigned int rxjitter_count; /*!< Calculated interarrival jitter count */
double maxrtt; /*!< Maximum of calculated round trip time */
double minrtt; /*!< Minimum of calculated round trip time */
double normdevrtt; /*!< Mean of calculated round trip time */
double stdevrtt; /*!< Standard deviation of calculated round trip time */
unsigned int rtt_count; /*!< Calculated round trip time count */
/* VP8: sequence number for the RTCP FIR FCI */
int firseq;
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
struct dtls_details dtls; /*!< DTLS state information */
#endif
/* Cached local address string allows us to generate
* RTCP stasis messages without having to look up our
* own address every time
*/
char *local_addr_str;
enum ast_rtp_instance_rtcp type;
/* Buffer for frames created during RTCP interpretation */
unsigned char frame_buf[512 + AST_FRIENDLY_OFFSET];
};
struct rtp_red {
struct ast_frame t140; /*!< Primary data */
struct ast_frame t140red; /*!< Redundant t140*/
unsigned char pt[AST_RED_MAX_GENERATION]; /*!< Payload types for redundancy data */
unsigned char ts[AST_RED_MAX_GENERATION]; /*!< Time stamps */
unsigned char len[AST_RED_MAX_GENERATION]; /*!< length of each generation */
int num_gen; /*!< Number of generations */
int schedid; /*!< Timer id */
int ti; /*!< How long to buffer data before send */
unsigned char t140red_data[64000];
unsigned char buf_data[64000]; /*!< buffered primary data */
int hdrlen;
long int prev_ts;
};
/*! \brief Structure for storing RTP packets for retransmission */
struct ast_rtp_rtcp_nack_payload {
size_t size; /*!< The size of the payload */
unsigned char buf[0]; /*!< The payload data */
};
AST_LIST_HEAD_NOLOCK(frame_list, ast_frame);
/* Forward Declarations */
static int ast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data);
static int ast_rtp_destroy(struct ast_rtp_instance *instance);
static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit);
static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit);
static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration);
static int ast_rtp_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode);
static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get(struct ast_rtp_instance *instance);
static void ast_rtp_update_source(struct ast_rtp_instance *instance);
static void ast_rtp_change_source(struct ast_rtp_instance *instance);
static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame);
static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtcp);
static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value);
static int ast_rtp_fd(struct ast_rtp_instance *instance, int rtcp);
static void ast_rtp_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr);
static int rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations);
static int rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame);
static int ast_rtp_local_bridge(struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1);
static int ast_rtp_get_stat(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat);
static int ast_rtp_dtmf_compatible(struct ast_channel *chan0, struct ast_rtp_instance *instance0, struct ast_channel *chan1, struct ast_rtp_instance *instance1);
static void ast_rtp_stun_request(struct ast_rtp_instance *instance, struct ast_sockaddr *suggestion, const char *username);
static void ast_rtp_stop(struct ast_rtp_instance *instance);
static int ast_rtp_qos_set(struct ast_rtp_instance *instance, int tos, int cos, const char* desc);
static int ast_rtp_sendcng(struct ast_rtp_instance *instance, int level);
static unsigned int ast_rtp_get_ssrc(struct ast_rtp_instance *instance);
static const char *ast_rtp_get_cname(struct ast_rtp_instance *instance);
static void ast_rtp_set_remote_ssrc(struct ast_rtp_instance *instance, unsigned int ssrc);
static void ast_rtp_set_stream_num(struct ast_rtp_instance *instance, int stream_num);
static int ast_rtp_extension_enable(struct ast_rtp_instance *instance, enum ast_rtp_extension extension);
static int ast_rtp_bundle(struct ast_rtp_instance *child, struct ast_rtp_instance *parent);
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
static int ast_rtp_activate(struct ast_rtp_instance *instance);
static void dtls_srtp_start_timeout_timer(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp);
static void dtls_srtp_stop_timeout_timer(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp);
static int dtls_bio_write(BIO *bio, const char *buf, int len);
static long dtls_bio_ctrl(BIO *bio, int cmd, long arg1, void *arg2);
static int dtls_bio_new(BIO *bio);
static int dtls_bio_free(BIO *bio);
#ifndef HAVE_OPENSSL_BIO_METHOD
static BIO_METHOD dtls_bio_methods = {
.type = BIO_TYPE_BIO,
.name = "rtp write",
.bwrite = dtls_bio_write,
.ctrl = dtls_bio_ctrl,
.create = dtls_bio_new,
.destroy = dtls_bio_free,
};
#else
static BIO_METHOD *dtls_bio_methods;
#endif
#endif
static int __rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp, int *via_ice, int use_srtp);
#ifdef HAVE_PJPROJECT
static void stunaddr_resolve_callback(const struct ast_dns_query *query);
static int store_stunaddr_resolved(const struct ast_dns_query *query);
#endif
#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
static int dtls_bio_new(BIO *bio)
{
#ifdef HAVE_OPENSSL_BIO_METHOD
BIO_set_init(bio, 1);
BIO_set_data(bio, NULL);
BIO_set_shutdown(bio, 0);
#else
bio->init = 1;
bio->ptr = NULL;
bio->flags = 0;
#endif
return 1;
}
static int dtls_bio_free(BIO *bio)
{
/* The pointer on the BIO is that of the RTP instance. It is not reference counted as the BIO
* lifetime is tied to the instance, and actions on the BIO are taken by the thread handling
* the RTP instance - not another thread.
*/
#ifdef HAVE_OPENSSL_BIO_METHOD
BIO_set_data(bio, NULL);
#else
bio->ptr = NULL;
#endif
return 1;
}
static int dtls_bio_write(BIO *bio, const char *buf, int len)
{
#ifdef HAVE_OPENSSL_BIO_METHOD
struct ast_rtp_instance *instance = BIO_get_data(bio);
#else
struct ast_rtp_instance *instance = bio->ptr;
#endif
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
int rtcp = 0;
struct ast_sockaddr remote_address = { {0, } };
int ice;
int bytes_sent;
/* OpenSSL can't tolerate a packet not being sent, so we always state that
* we sent the packet. If it isn't then retransmission will occur.
*/
if (rtp->rtcp && rtp->rtcp->dtls.write_bio == bio) {
rtcp = 1;
ast_sockaddr_copy(&remote_address, &rtp->rtcp->them);
} else {
ast_rtp_instance_get_remote_address(instance, &remote_address);
}
if (ast_sockaddr_isnull(&remote_address)) {
return len;
}
bytes_sent = __rtp_sendto(instance, (char *)buf, len, 0, &remote_address, rtcp, &ice, 0);
if (bytes_sent > 0 && ast_debug_dtls_packet_is_allowed) {
ast_debug(0, "(%p) DTLS - sent %s packet to %s%s (len %-6.6d)\n",
instance, rtcp ? "RTCP" : "RTP", ast_sockaddr_stringify(&remote_address),
ice ? " (via ICE)" : "", bytes_sent);
}
return len;
}
static long dtls_bio_ctrl(BIO *bio, int cmd, long arg1, void *arg2)
{
switch (cmd) {
case BIO_CTRL_FLUSH:
return 1;
case BIO_CTRL_DGRAM_QUERY_MTU:
return dtls_mtu;
case BIO_CTRL_WPENDING:
case BIO_CTRL_PENDING:
return 0L;
default:
return 0;
}
}
#endif
#ifdef HAVE_PJPROJECT
/*! \brief Helper function which clears the ICE host candidate mapping */
static void host_candidate_overrides_clear(void)
{
struct ast_ice_host_candidate *candidate;
AST_RWLIST_WRLOCK(&host_candidates);
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&host_candidates, candidate, next) {
AST_RWLIST_REMOVE_CURRENT(next);
ast_free(candidate);
}
AST_RWLIST_TRAVERSE_SAFE_END;
AST_RWLIST_UNLOCK(&host_candidates);
}
/*! \brief Helper function which updates an ast_sockaddr with the candidate used for the component */
static void update_address_with_ice_candidate(pj_ice_sess *ice, enum ast_rtp_ice_component_type component,
struct ast_sockaddr *cand_address)
{
char address[PJ_INET6_ADDRSTRLEN];
if (component < 1 || !ice->comp[component - 1].valid_check) {
return;
}
ast_sockaddr_parse(cand_address,
pj_sockaddr_print(&ice->comp[component - 1].valid_check->rcand->addr, address,
sizeof(address), 0), 0);
ast_sockaddr_set_port(cand_address,
pj_sockaddr_get_port(&ice->comp[component - 1].valid_check->rcand->addr));
}
/*! \brief Destructor for locally created ICE candidates */
static void ast_rtp_ice_candidate_destroy(void *obj)
{
struct ast_rtp_engine_ice_candidate *candidate = obj;
if (candidate->foundation) {
ast_free(candidate->foundation);
}
if (candidate->transport) {
ast_free(candidate->transport);
}
}
/*! \pre instance is locked */
static void ast_rtp_ice_set_authentication(struct ast_rtp_instance *instance, const char *ufrag, const char *password)
{
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
int ice_attrb_reset = 0;
if (!ast_strlen_zero(ufrag)) {
if (!ast_strlen_zero(rtp->remote_ufrag) && strcmp(ufrag, rtp->remote_ufrag)) {
ice_attrb_reset = 1;
}
ast_copy_string(rtp->remote_ufrag, ufrag, sizeof(rtp->remote_ufrag));
}
if (!ast_strlen_zero(password)) {
if (!ast_strlen_zero(rtp->remote_passwd) && strcmp(password, rtp->remote_passwd)) {
ice_attrb_reset = 1;
}
ast_copy_string(rtp->remote_passwd, password, sizeof(rtp->remote_passwd));
}
/* If the remote ufrag or passwd changed, local ufrag and passwd need to regenerate */
if (ice_attrb_reset) {
generate_random_string(rtp->local_ufrag, sizeof(rtp->local_ufrag));
generate_random_string(rtp->local_passwd, sizeof(rtp->local_passwd));
}
}
static int ice_candidate_cmp(void *obj, void *arg, int flags)
{
struct ast_rtp_engine_ice_candidate *candidate1 = obj, *candidate2 = arg;
if (strcmp(candidate1->foundation, candidate2->foundation) ||
candidate1->id != candidate2->id ||
candidate1->type != candidate2->type ||
ast_sockaddr_cmp(&candidate1->address, &candidate2->address)) {
return 0;
}
return CMP_MATCH | CMP_STOP;
}
/*! \pre instance is locked */
static void ast_rtp_ice_add_remote_candidate(struct ast_rtp_instance *instance, const struct ast_rtp_engine_ice_candidate *candidate)
{
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
struct ast_rtp_engine_ice_candidate *remote_candidate;
/* ICE sessions only support UDP candidates */
if (strcasecmp(candidate->transport, "udp")) {
return;
}
if (!rtp->ice_proposed_remote_candidates) {
rtp->ice_proposed_remote_candidates = ao2_container_alloc_list(
AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, ice_candidate_cmp);
if (!rtp->ice_proposed_remote_candidates) {
return;
}
}
/* If this is going to exceed the maximum number of ICE candidates don't even add it */
if (ao2_container_count(rtp->ice_proposed_remote_candidates) == PJ_ICE_MAX_CAND) {
return;
}
if (!(remote_candidate = ao2_alloc(sizeof(*remote_candidate), ast_rtp_ice_candidate_destroy))) {
return;
}
remote_candidate->foundation = ast_strdup(candidate->foundation);
remote_candidate->id = candidate->id;
remote_candidate->transport = ast_strdup(candidate->transport);
remote_candidate->priority = candidate->priority;
ast_sockaddr_copy(&remote_candidate->address, &candidate->address);
ast_sockaddr_copy(&remote_candidate->relay_address, &candidate->relay_address);
remote_candidate->type = candidate->type;
ast_debug_ice(2, "(%p) ICE add remote candidate\n", instance);
ao2_link(rtp->ice_proposed_remote_candidates, remote_candidate);
ao2_ref(remote_candidate, -1);
}
AST_THREADSTORAGE(pj_thread_storage);
/*! \brief Function used to check if the calling thread is registered with pjlib. If it is not it will be registered. */
static void pj_thread_register_check(void)
{
pj_thread_desc *desc;
pj_thread_t *thread;
if (pj_thread_is_registered() == PJ_TRUE) {
return;
}
desc = ast_threadstorage_get(&pj_thread_storage, sizeof(pj_thread_desc));
if (!desc) {
ast_log(LOG_ERROR, "Could not get thread desc from thread-local storage. Expect awful things to occur\n");
return;
}
pj_bzero(*desc, sizeof(*desc));
if (pj_thread_register("Asterisk Thread", *desc, &thread) != PJ_SUCCESS) {
ast_log(LOG_ERROR, "Coudln't register thread with PJLIB.\n");
}
return;
}
static int ice_create(struct ast_rtp_instance *instance, struct ast_sockaddr *addr,
int port, int replace);
/*! \pre instance is locked */
static void ast_rtp_ice_stop(struct ast_rtp_instance *instance)
{
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
struct ice_wrap *ice;
ice = rtp->ice;
rtp->ice = NULL;
if (ice) {
/* Release the instance lock to avoid deadlock with PJPROJECT group lock */
ao2_unlock(instance);
ao2_ref(ice, -1);
ao2_lock(instance);
ast_debug_ice(2, "(%p) ICE stopped\n", instance);
}
}
/*!
* \brief ao2 ICE wrapper object destructor.
*
* \param vdoomed Object being destroyed.
*
* \note The associated struct ast_rtp_instance object must not
* be locked when unreffing the object. Otherwise we could
* deadlock trying to destroy the PJPROJECT ICE structure.
*/
static void ice_wrap_dtor(void *vdoomed)
{
struct ice_wrap *ice = vdoomed;
if (ice->real_ice) {
pj_thread_register_check();
pj_ice_sess_destroy(ice->real_ice);
}
}
static void ast2pj_rtp_ice_role(enum ast_rtp_ice_role ast_role, enum pj_ice_sess_role *pj_role)
{
switch (ast_role) {
case AST_RTP_ICE_ROLE_CONTROLLED:
*pj_role = PJ_ICE_SESS_ROLE_CONTROLLED;
break;
case AST_RTP_ICE_ROLE_CONTROLLING:
*pj_role = PJ_ICE_SESS_ROLE_CONTROLLING;
break;
}
}
static void pj2ast_rtp_ice_role(enum pj_ice_sess_role pj_role, enum ast_rtp_ice_role *ast_role)
{
switch (pj_role) {
case PJ_ICE_SESS_ROLE_CONTROLLED:
*ast_role = AST_RTP_ICE_ROLE_CONTROLLED;
return;
case PJ_ICE_SESS_ROLE_CONTROLLING:
*ast_role = AST_RTP_ICE_ROLE_CONTROLLING;
return;
case PJ_ICE_SESS_ROLE_UNKNOWN:
/* Don't change anything */
return;
default:
/* If we aren't explicitly handling something, it's a bug */
ast_assert(0);
return;
}
}
/*! \pre instance is locked */
static int ice_reset_session(struct ast_rtp_instance *instance)
{
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
int res;
ast_debug_ice(3, "(%p) ICE resetting\n", instance);
if (!rtp->ice->real_ice->is_nominating && !rtp->ice->real_ice->is_complete) {
ast_debug_ice(3, " (%p) ICE nevermind, not ready for a reset\n", instance);
return 0;
}
ast_debug_ice(3, "(%p) ICE recreating ICE session %s (%d)\n",
instance, ast_sockaddr_stringify(&rtp->ice_original_rtp_addr), rtp->ice_port);
res = ice_create(instance, &rtp->ice_original_rtp_addr, rtp->ice_port, 1);
if (!res) {
/* Use the current expected role for the ICE session */
enum pj_ice_sess_role role = PJ_ICE_SESS_ROLE_UNKNOWN;
ast2pj_rtp_ice_role(rtp->role, &role);
pj_ice_sess_change_role(rtp->ice->real_ice, role);
}
/* If we only have one component now, and we previously set up TURN for RTCP,
* we need to destroy that TURN socket.
*/
if (rtp->ice_num_components == 1 && rtp->turn_rtcp) {
struct timeval wait = ast_tvadd(ast_tvnow(), ast_samp2tv(TURN_STATE_WAIT_TIME, 1000));
struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, };