25fd3d52384d894d88316b4800ccc8749de5e39d
[asterisk/asterisk.git] / res / res_rtp_asterisk.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2008, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*!
20  * \file
21  *
22  * \brief Supports RTP and RTCP with Symmetric RTP support for NAT traversal.
23  *
24  * \author Mark Spencer <markster@digium.com>
25  *
26  * \note RTP is defined in RFC 3550.
27  *
28  * \ingroup rtp_engines
29  */
30
31 /*** MODULEINFO
32         <support_level>core</support_level>
33  ***/
34
35 #include "asterisk.h"
36
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38
39 #include <sys/time.h>
40 #include <signal.h>
41 #include <fcntl.h>
42
43 #ifdef HAVE_OPENSSL_SRTP
44 #include <openssl/ssl.h>
45 #include <openssl/err.h>
46 #include <openssl/bio.h>
47 #endif
48
49 /* Asterisk discourages the use of bzero in favor of memset, in fact if you try to use bzero it will tell you to use memset. As a result bzero has to be undefined
50  * here since it is used internally by pjlib. The only other option would be to modify pjlib... which won't happen. */
51 #undef bzero
52 #define bzero bzero
53 #include "pjlib.h"
54 #include "pjlib-util.h"
55 #include "pjnath.h"
56
57 #include "asterisk/stun.h"
58 #include "asterisk/pbx.h"
59 #include "asterisk/frame.h"
60 #include "asterisk/channel.h"
61 #include "asterisk/acl.h"
62 #include "asterisk/config.h"
63 #include "asterisk/lock.h"
64 #include "asterisk/utils.h"
65 #include "asterisk/cli.h"
66 #include "asterisk/manager.h"
67 #include "asterisk/unaligned.h"
68 #include "asterisk/module.h"
69 #include "asterisk/rtp_engine.h"
70
71 #define MAX_TIMESTAMP_SKEW      640
72
73 #define RTP_SEQ_MOD     (1<<16) /*!< A sequence number can't be more than 16 bits */
74 #define RTCP_DEFAULT_INTERVALMS   5000  /*!< Default milli-seconds between RTCP reports we send */
75 #define RTCP_MIN_INTERVALMS       500   /*!< Min milli-seconds between RTCP reports we send */
76 #define RTCP_MAX_INTERVALMS       60000 /*!< Max milli-seconds between RTCP reports we send */
77
78 #define DEFAULT_RTP_START 5000 /*!< Default port number to start allocating RTP ports from */
79 #define DEFAULT_RTP_END 31000  /*!< Default maximum port number to end allocating RTP ports at */
80
81 #define MINIMUM_RTP_PORT 1024 /*!< Minimum port number to accept */
82 #define MAXIMUM_RTP_PORT 65535 /*!< Maximum port number to accept */
83
84 #define DEFAULT_TURN_PORT 34780
85
86 #define TURN_ALLOCATION_WAIT_TIME 2000
87
88 #define RTCP_PT_FUR     192
89 #define RTCP_PT_SR      200
90 #define RTCP_PT_RR      201
91 #define RTCP_PT_SDES    202
92 #define RTCP_PT_BYE     203
93 #define RTCP_PT_APP     204
94
95 #define RTP_MTU         1200
96
97 #define DEFAULT_DTMF_TIMEOUT (150 * (8000 / 1000))      /*!< samples */
98
99 #define ZFONE_PROFILE_ID 0x505a
100
101 #define DEFAULT_LEARNING_MIN_SEQUENTIAL 4
102
103 #define SRTP_MASTER_KEY_LEN 16
104 #define SRTP_MASTER_SALT_LEN 14
105 #define SRTP_MASTER_LEN (SRTP_MASTER_KEY_LEN + SRTP_MASTER_SALT_LEN)
106
107 enum strict_rtp_state {
108         STRICT_RTP_OPEN = 0, /*! No RTP packets should be dropped, all sources accepted */
109         STRICT_RTP_LEARN,    /*! Accept next packet as source */
110         STRICT_RTP_CLOSED,   /*! Drop all RTP packets not coming from source that was learned */
111 };
112
113 #define DEFAULT_STRICT_RTP STRICT_RTP_CLOSED
114 #define DEFAULT_ICESUPPORT 1
115
116 extern struct ast_srtp_res *res_srtp;
117 extern struct ast_srtp_policy_res *res_srtp_policy;
118
119 static int dtmftimeout = DEFAULT_DTMF_TIMEOUT;
120
121 static int rtpstart = DEFAULT_RTP_START;                        /*!< First port for RTP sessions (set in rtp.conf) */
122 static int rtpend = DEFAULT_RTP_END;                    /*!< Last port for RTP sessions (set in rtp.conf) */
123 static int rtpdebug;                    /*!< Are we debugging? */
124 static int rtcpdebug;                   /*!< Are we debugging RTCP? */
125 static int rtcpstats;                   /*!< Are we debugging RTCP? */
126 static int rtcpinterval = RTCP_DEFAULT_INTERVALMS; /*!< Time between rtcp reports in millisecs */
127 static struct ast_sockaddr rtpdebugaddr;        /*!< Debug packets to/from this host */
128 static struct ast_sockaddr rtcpdebugaddr;       /*!< Debug RTCP packets to/from this host */
129 static int rtpdebugport;                /*< Debug only RTP packets from IP or IP+Port if port is > 0 */
130 static int rtcpdebugport;               /*< Debug only RTCP packets from IP or IP+Port if port is > 0 */
131 #ifdef SO_NO_CHECK
132 static int nochecksums;
133 #endif
134 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. */
135 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. */
136 static int icesupport = DEFAULT_ICESUPPORT;
137 static struct sockaddr_in stunaddr;
138 static pj_str_t turnaddr;
139 static int turnport = DEFAULT_TURN_PORT;
140 static pj_str_t turnusername;
141 static pj_str_t turnpassword;
142
143 /*! \brief Pool factory used by pjlib to allocate memory. */
144 static pj_caching_pool cachingpool;
145
146 /*! \brief Pool used by pjlib functions which require memory allocation. */
147 static pj_pool_t *pool;
148
149 /*! \brief I/O queue for TURN relay traffic */
150 static pj_ioqueue_t *ioqueue;
151
152 /*! \brief Timer heap for ICE and TURN stuff */
153 static pj_timer_heap_t *timerheap;
154
155 /*! \brief Worker thread for ICE/TURN */
156 static pj_thread_t *thread;
157
158 /*! \brief Notification that the ICE/TURN worker thread should stop */
159 static int worker_terminate;
160
161 #define FLAG_3389_WARNING               (1 << 0)
162 #define FLAG_NAT_ACTIVE                 (3 << 1)
163 #define FLAG_NAT_INACTIVE               (0 << 1)
164 #define FLAG_NAT_INACTIVE_NOWARN        (1 << 1)
165 #define FLAG_NEED_MARKER_BIT            (1 << 3)
166 #define FLAG_DTMF_COMPENSATE            (1 << 4)
167
168 #define TRANSPORT_SOCKET_RTP 1
169 #define TRANSPORT_SOCKET_RTCP 2
170 #define TRANSPORT_TURN_RTP 3
171 #define TRANSPORT_TURN_RTCP 4
172
173 #define COMPONENT_RTP 1
174 #define COMPONENT_RTCP 2
175
176 /*! \brief RTP session description */
177 struct ast_rtp {
178         int s;
179         struct ast_frame f;
180         unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
181         unsigned int ssrc;              /*!< Synchronization source, RFC 3550, page 10. */
182         unsigned int themssrc;          /*!< Their SSRC */
183         unsigned int rxssrc;
184         unsigned int lastts;
185         unsigned int lastrxts;
186         unsigned int lastividtimestamp;
187         unsigned int lastovidtimestamp;
188         unsigned int lastitexttimestamp;
189         unsigned int lastotexttimestamp;
190         unsigned int lasteventseqn;
191         int lastrxseqno;                /*!< Last received sequence number */
192         unsigned short seedrxseqno;     /*!< What sequence number did they start with?*/
193         unsigned int seedrxts;          /*!< What RTP timestamp did they start with? */
194         unsigned int rxcount;           /*!< How many packets have we received? */
195         unsigned int rxoctetcount;      /*!< How many octets have we received? should be rxcount *160*/
196         unsigned int txcount;           /*!< How many packets have we sent? */
197         unsigned int txoctetcount;      /*!< How many octets have we sent? (txcount*160)*/
198         unsigned int cycles;            /*!< Shifted count of sequence number cycles */
199         double rxjitter;                /*!< Interarrival jitter at the moment in seconds */
200         double rxtransit;               /*!< Relative transit time for previous packet */
201         struct ast_format lasttxformat;
202         struct ast_format lastrxformat;
203
204         int rtptimeout;                 /*!< RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
205         int rtpholdtimeout;             /*!< RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
206         int rtpkeepalive;               /*!< Send RTP comfort noice packets for keepalive */
207
208         /* DTMF Reception Variables */
209         char resp;                        /*!< The current digit being processed */
210         unsigned int last_seqno;          /*!< The last known sequence number for any DTMF packet */
211         unsigned int last_end_timestamp;  /*!< The last known timestamp received from an END packet */
212         unsigned int dtmf_duration;       /*!< Total duration in samples since the digit start event */
213         unsigned int dtmf_timeout;        /*!< When this timestamp is reached we consider END frame lost and forcibly abort digit */
214         unsigned int dtmfsamples;
215         enum ast_rtp_dtmf_mode dtmfmode;  /*!< The current DTMF mode of the RTP stream */
216         /* DTMF Transmission Variables */
217         unsigned int lastdigitts;
218         char sending_digit;     /*!< boolean - are we sending digits */
219         char send_digit;        /*!< digit we are sending */
220         int send_payload;
221         int send_duration;
222         unsigned int flags;
223         struct timeval rxcore;
224         struct timeval txcore;
225         double drxcore;                 /*!< The double representation of the first received packet */
226         struct timeval lastrx;          /*!< timeval when we last received a packet */
227         struct timeval dtmfmute;
228         struct ast_smoother *smoother;
229         int *ioid;
230         unsigned short seqno;           /*!< Sequence number, RFC 3550, page 13. */
231         unsigned short rxseqno;
232         struct ast_sched_context *sched;
233         struct io_context *io;
234         void *data;
235         struct ast_rtcp *rtcp;
236         struct ast_rtp *bridged;        /*!< Who we are Packet bridged to */
237
238         enum strict_rtp_state strict_rtp_state; /*!< Current state that strict RTP protection is in */
239         struct ast_sockaddr strict_rtp_address;  /*!< Remote address information for strict RTP purposes */
240         struct ast_sockaddr alt_rtp_address; /*!<Alternate remote address information */
241
242         /*
243          * Learning mode values based on pjmedia's probation mode.  Many of these values are redundant to the above,
244          * but these are in place to keep learning mode sequence values sealed from their normal counterparts.
245          */
246         uint16_t learning_max_seq;              /*!< Highest sequence number heard */
247         int learning_probation;         /*!< Sequential packets untill source is valid */
248
249         struct rtp_red *red;
250
251         pj_ice_sess *ice;           /*!< ICE session */
252         pj_turn_sock *turn_rtp;     /*!< RTP TURN relay */
253         pj_turn_sock *turn_rtcp;    /*!< RTCP TURN relay */
254         ast_mutex_t lock;           /*!< Lock for synchronization purposes */
255         pj_turn_state_t turn_state; /*!< Current state of the TURN relay session */
256         ast_cond_t cond;            /*!< Condition for signaling */
257         unsigned int passthrough:1; /*!< Bit to indicate that the received packet should be passed through */
258         unsigned int ice_started:1; /*!< Bit to indicate ICE connectivity checks have started */
259
260         char remote_ufrag[256];  /*!< The remote ICE username */
261         char remote_passwd[256]; /*!< The remote ICE password */
262
263         char local_ufrag[256];  /*!< The local ICE username */
264         char local_passwd[256]; /*!< The local ICE password */
265
266         struct ao2_container *local_candidates;   /*!< The local ICE candidates */
267         struct ao2_container *remote_candidates;  /*!< The remote ICE candidates */
268
269 #ifdef HAVE_OPENSSL_SRTP
270         SSL_CTX *ssl_ctx; /*!< SSL context */
271         SSL *ssl;         /*!< SSL session */
272         BIO *read_bio;    /*!< Memory buffer for reading */
273         BIO *write_bio;   /*!< Memory buffer for writing */
274         enum ast_rtp_dtls_setup dtls_setup; /*!< Current setup state */
275         enum ast_srtp_suite suite;   /*!< SRTP crypto suite */
276         char local_fingerprint[160]; /*!< Fingerprint of our certificate */
277         unsigned char remote_fingerprint[EVP_MAX_MD_SIZE]; /*!< Fingerprint of the peer certificate */
278         enum ast_rtp_dtls_connection connection; /*!< Whether this is a new or existing connection */
279         unsigned int dtls_failure:1; /*!< Failure occurred during DTLS negotiation */
280         unsigned int rekey; /*!< Interval at which to renegotiate and rekey */
281         int rekeyid; /*!< Scheduled item id for rekeying */
282 #endif
283 };
284
285 /*!
286  * \brief Structure defining an RTCP session.
287  *
288  * The concept "RTCP session" is not defined in RFC 3550, but since
289  * this structure is analogous to ast_rtp, which tracks a RTP session,
290  * it is logical to think of this as a RTCP session.
291  *
292  * RTCP packet is defined on page 9 of RFC 3550.
293  *
294  */
295 struct ast_rtcp {
296         int rtcp_info;
297         int s;                          /*!< Socket */
298         struct ast_sockaddr us;         /*!< Socket representation of the local endpoint. */
299         struct ast_sockaddr them;       /*!< Socket representation of the remote endpoint. */
300         unsigned int soc;               /*!< What they told us */
301         unsigned int spc;               /*!< What they told us */
302         unsigned int themrxlsr;         /*!< The middle 32 bits of the NTP timestamp in the last received SR*/
303         struct timeval rxlsr;           /*!< Time when we got their last SR */
304         struct timeval txlsr;           /*!< Time when we sent or last SR*/
305         unsigned int expected_prior;    /*!< no. packets in previous interval */
306         unsigned int received_prior;    /*!< no. packets received in previous interval */
307         int schedid;                    /*!< Schedid returned from ast_sched_add() to schedule RTCP-transmissions*/
308         unsigned int rr_count;          /*!< number of RRs we've sent, not including report blocks in SR's */
309         unsigned int sr_count;          /*!< number of SRs we've sent */
310         unsigned int lastsrtxcount;     /*!< Transmit packet count when last SR sent */
311         double accumulated_transit;     /*!< accumulated a-dlsr-lsr */
312         double rtt;                     /*!< Last reported rtt */
313         unsigned int reported_jitter;   /*!< The contents of their last jitter entry in the RR */
314         unsigned int reported_lost;     /*!< Reported lost packets in their RR */
315
316         double reported_maxjitter;
317         double reported_minjitter;
318         double reported_normdev_jitter;
319         double reported_stdev_jitter;
320         unsigned int reported_jitter_count;
321
322         double reported_maxlost;
323         double reported_minlost;
324         double reported_normdev_lost;
325         double reported_stdev_lost;
326
327         double rxlost;
328         double maxrxlost;
329         double minrxlost;
330         double normdev_rxlost;
331         double stdev_rxlost;
332         unsigned int rxlost_count;
333
334         double maxrxjitter;
335         double minrxjitter;
336         double normdev_rxjitter;
337         double stdev_rxjitter;
338         unsigned int rxjitter_count;
339         double maxrtt;
340         double minrtt;
341         double normdevrtt;
342         double stdevrtt;
343         unsigned int rtt_count;
344 };
345
346 struct rtp_red {
347         struct ast_frame t140;  /*!< Primary data  */
348         struct ast_frame t140red;   /*!< Redundant t140*/
349         unsigned char pt[AST_RED_MAX_GENERATION];  /*!< Payload types for redundancy data */
350         unsigned char ts[AST_RED_MAX_GENERATION]; /*!< Time stamps */
351         unsigned char len[AST_RED_MAX_GENERATION]; /*!< length of each generation */
352         int num_gen; /*!< Number of generations */
353         int schedid; /*!< Timer id */
354         int ti; /*!< How long to buffer data before send */
355         unsigned char t140red_data[64000];
356         unsigned char buf_data[64000]; /*!< buffered primary data */
357         int hdrlen;
358         long int prev_ts;
359 };
360
361 AST_LIST_HEAD_NOLOCK(frame_list, ast_frame);
362
363 /* Forward Declarations */
364 static int ast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data);
365 static int ast_rtp_destroy(struct ast_rtp_instance *instance);
366 static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit);
367 static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit);
368 static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration);
369 static int ast_rtp_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode);
370 static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get(struct ast_rtp_instance *instance);
371 static void ast_rtp_update_source(struct ast_rtp_instance *instance);
372 static void ast_rtp_change_source(struct ast_rtp_instance *instance);
373 static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame);
374 static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtcp);
375 static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value);
376 static int ast_rtp_fd(struct ast_rtp_instance *instance, int rtcp);
377 static void ast_rtp_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr);
378 static void ast_rtp_alt_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr);
379 static int rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations);
380 static int rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame);
381 static int ast_rtp_local_bridge(struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1);
382 static int ast_rtp_get_stat(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat);
383 static int ast_rtp_dtmf_compatible(struct ast_channel *chan0, struct ast_rtp_instance *instance0, struct ast_channel *chan1, struct ast_rtp_instance *instance1);
384 static void ast_rtp_stun_request(struct ast_rtp_instance *instance, struct ast_sockaddr *suggestion, const char *username);
385 static void ast_rtp_stop(struct ast_rtp_instance *instance);
386 static int ast_rtp_qos_set(struct ast_rtp_instance *instance, int tos, int cos, const char* desc);
387 static int ast_rtp_sendcng(struct ast_rtp_instance *instance, int level);
388
389 #ifdef HAVE_OPENSSL_SRTP
390 static int ast_rtp_activate(struct ast_rtp_instance *instance);
391 #endif
392
393 static int __rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp, int *ice, int use_srtp);
394
395 /*! \brief Destructor for locally created ICE candidates */
396 static void ast_rtp_ice_candidate_destroy(void *obj)
397 {
398         struct ast_rtp_engine_ice_candidate *candidate = obj;
399
400         if (candidate->foundation) {
401                 ast_free(candidate->foundation);
402         }
403
404         if (candidate->transport) {
405                 ast_free(candidate->transport);
406         }
407 }
408
409 static void ast_rtp_ice_set_authentication(struct ast_rtp_instance *instance, const char *ufrag, const char *password)
410 {
411         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
412
413         if (!ast_strlen_zero(ufrag)) {
414                 ast_copy_string(rtp->remote_ufrag, ufrag, sizeof(rtp->remote_ufrag));
415         }
416
417         if (!ast_strlen_zero(password)) {
418                 ast_copy_string(rtp->remote_passwd, password, sizeof(rtp->remote_passwd));
419         }
420 }
421
422 static void ast_rtp_ice_add_remote_candidate(struct ast_rtp_instance *instance, const struct ast_rtp_engine_ice_candidate *candidate)
423 {
424         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
425         struct ast_rtp_engine_ice_candidate *remote_candidate;
426
427         if (!rtp->remote_candidates && !(rtp->remote_candidates = ao2_container_alloc(1, NULL, NULL))) {
428                 return;
429         }
430
431         /* If this is going to exceed the maximum number of ICE candidates don't even add it */
432         if (ao2_container_count(rtp->remote_candidates) == PJ_ICE_MAX_CAND) {
433                 return;
434         }
435
436         if (!(remote_candidate = ao2_alloc(sizeof(*remote_candidate), ast_rtp_ice_candidate_destroy))) {
437                 return;
438         }
439
440         remote_candidate->foundation = ast_strdup(candidate->foundation);
441         remote_candidate->id = candidate->id;
442         remote_candidate->transport = ast_strdup(candidate->transport);
443         remote_candidate->priority = candidate->priority;
444         ast_sockaddr_copy(&remote_candidate->address, &candidate->address);
445         ast_sockaddr_copy(&remote_candidate->relay_address, &candidate->relay_address);
446         remote_candidate->type = candidate->type;
447
448         ao2_link(rtp->remote_candidates, remote_candidate);
449         ao2_ref(remote_candidate, -1);
450 }
451
452 AST_THREADSTORAGE(pj_thread_storage);
453
454 /*! \brief Function used to check if the calling thread is registered with pjlib. If it is not it will be registered. */
455 static void pj_thread_register_check(void)
456 {
457         pj_thread_desc *desc;
458         pj_thread_t *thread;
459
460         if (pj_thread_is_registered() == PJ_TRUE) {
461                 return;
462         }
463
464         desc = ast_threadstorage_get(&pj_thread_storage, sizeof(pj_thread_desc));
465         if (!desc) {
466                 ast_log(LOG_ERROR, "Could not get thread desc from thread-local storage. Expect awful things to occur\n");
467                 return;
468         }
469         pj_bzero(*desc, sizeof(*desc));
470
471         if (pj_thread_register("Asterisk Thread", *desc, &thread) != PJ_SUCCESS) {
472                 ast_log(LOG_ERROR, "Coudln't register thread with PJLIB.\n");
473         }
474         return;
475 }
476
477 /*! \brief Helper function which updates an ast_sockaddr with the candidate used for the component */
478 static void update_address_with_ice_candidate(struct ast_rtp *rtp, int component, struct ast_sockaddr *cand_address)
479 {
480         char address[PJ_INET6_ADDRSTRLEN];
481
482         if (!rtp->ice || (component < 1) || !rtp->ice->comp[component - 1].valid_check) {
483                 return;
484         }
485
486         ast_sockaddr_parse(cand_address, pj_sockaddr_print(&rtp->ice->comp[component - 1].valid_check->rcand->addr, address, sizeof(address), 0), 0);
487         ast_sockaddr_set_port(cand_address, pj_sockaddr_get_port(&rtp->ice->comp[component - 1].valid_check->rcand->addr));
488 }
489
490 static void ast_rtp_ice_start(struct ast_rtp_instance *instance)
491 {
492         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
493         pj_str_t ufrag = pj_str(rtp->remote_ufrag), passwd = pj_str(rtp->remote_passwd);
494         pj_ice_sess_cand candidates[PJ_ICE_MAX_CAND];
495         struct ao2_iterator i;
496         struct ast_rtp_engine_ice_candidate *candidate;
497         int cand_cnt = 0;
498
499         if (!rtp->ice || !rtp->remote_candidates || rtp->ice_started) {
500                 return;
501         }
502
503         pj_thread_register_check();
504
505         i = ao2_iterator_init(rtp->remote_candidates, 0);
506
507         while ((candidate = ao2_iterator_next(&i)) && (cand_cnt < PJ_ICE_MAX_CAND)) {
508                 pj_str_t address;
509
510                 pj_strdup2(rtp->ice->pool, &candidates[cand_cnt].foundation, candidate->foundation);
511                 candidates[cand_cnt].comp_id = candidate->id;
512                 candidates[cand_cnt].prio = candidate->priority;
513
514                 pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&address, ast_sockaddr_stringify(&candidate->address)), &candidates[cand_cnt].addr);
515
516                 if (!ast_sockaddr_isnull(&candidate->relay_address)) {
517                         pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&address, ast_sockaddr_stringify(&candidate->relay_address)), &candidates[cand_cnt].rel_addr);
518                 }
519
520                 if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_HOST) {
521                         candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_HOST;
522                 } else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_SRFLX) {
523                         candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_SRFLX;
524                 } else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_RELAYED) {
525                         candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_RELAYED;
526                 }
527
528                 if (candidate->id == COMPONENT_RTP && rtp->turn_rtp) {
529                         pj_turn_sock_set_perm(rtp->turn_rtp, 1, &candidates[cand_cnt].addr, 1);
530                 } else if (candidate->id == COMPONENT_RTCP && rtp->turn_rtcp) {
531                         pj_turn_sock_set_perm(rtp->turn_rtcp, 1, &candidates[cand_cnt].addr, 1);
532                 }
533
534                 cand_cnt++;
535         }
536
537         ao2_iterator_destroy(&i);
538
539         if (pj_ice_sess_create_check_list(rtp->ice, &ufrag, &passwd, ao2_container_count(rtp->remote_candidates), &candidates[0]) == PJ_SUCCESS) {
540                 pj_ice_sess_start_check(rtp->ice);
541                 pj_timer_heap_poll(timerheap, NULL);
542                 rtp->ice_started = 1;
543         }
544 }
545
546 static void ast_rtp_ice_stop(struct ast_rtp_instance *instance)
547 {
548         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
549
550         if (!rtp->ice) {
551                 return;
552         }
553
554         pj_thread_register_check();
555
556         pj_ice_sess_destroy(rtp->ice);
557         rtp->ice = NULL;
558 }
559
560 static const char *ast_rtp_ice_get_ufrag(struct ast_rtp_instance *instance)
561 {
562         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
563
564         return rtp->local_ufrag;
565 }
566
567 static const char *ast_rtp_ice_get_password(struct ast_rtp_instance *instance)
568 {
569         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
570
571         return rtp->local_passwd;
572 }
573
574 static struct ao2_container *ast_rtp_ice_get_local_candidates(struct ast_rtp_instance *instance)
575 {
576         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
577
578         if (rtp->local_candidates) {
579                 ao2_ref(rtp->local_candidates, +1);
580         }
581
582         return rtp->local_candidates;
583 }
584
585 static void ast_rtp_ice_lite(struct ast_rtp_instance *instance)
586 {
587         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
588
589         if (!rtp->ice) {
590                 return;
591         }
592
593         pj_thread_register_check();
594
595         pj_ice_sess_change_role(rtp->ice, PJ_ICE_SESS_ROLE_CONTROLLING);
596 }
597
598 static int ice_candidate_cmp(void *obj, void *arg, int flags)
599 {
600         struct ast_rtp_engine_ice_candidate *candidate1 = obj, *candidate2 = arg;
601
602         if ((strcmp(candidate1->foundation, candidate2->foundation)) ||
603             (candidate1->id != candidate2->id) ||
604             (ast_sockaddr_cmp(&candidate1->address, &candidate2->address)) ||
605             (candidate1->type != candidate1->type)) {
606                 return 0;
607         }
608
609         return CMP_MATCH | CMP_STOP;
610 }
611
612 static void ast_rtp_ice_add_cand(struct ast_rtp *rtp, unsigned comp_id, unsigned transport_id, pj_ice_cand_type type, pj_uint16_t local_pref,
613                                         const pj_sockaddr_t *addr, const pj_sockaddr_t *base_addr, const pj_sockaddr_t *rel_addr, int addr_len)
614 {
615         pj_str_t foundation;
616         struct ast_rtp_engine_ice_candidate *candidate, *existing;
617         char address[PJ_INET6_ADDRSTRLEN];
618
619         pj_thread_register_check();
620
621         pj_ice_calc_foundation(rtp->ice->pool, &foundation, type, addr);
622
623         if (!rtp->local_candidates && !(rtp->local_candidates = ao2_container_alloc(1, NULL, ice_candidate_cmp))) {
624                 return;
625         }
626
627         if (!(candidate = ao2_alloc(sizeof(*candidate), ast_rtp_ice_candidate_destroy))) {
628                 return;
629         }
630
631         candidate->foundation = ast_strndup(pj_strbuf(&foundation), pj_strlen(&foundation));
632         candidate->id = comp_id;
633         candidate->transport = ast_strdup("UDP");
634
635         ast_sockaddr_parse(&candidate->address, pj_sockaddr_print(addr, address, sizeof(address), 0), 0);
636         ast_sockaddr_set_port(&candidate->address, pj_sockaddr_get_port(addr));
637
638         if (rel_addr) {
639                 ast_sockaddr_parse(&candidate->relay_address, pj_sockaddr_print(rel_addr, address, sizeof(address), 0), 0);
640                 ast_sockaddr_set_port(&candidate->relay_address, pj_sockaddr_get_port(rel_addr));
641         }
642
643         if (type == PJ_ICE_CAND_TYPE_HOST) {
644                 candidate->type = AST_RTP_ICE_CANDIDATE_TYPE_HOST;
645         } else if (type == PJ_ICE_CAND_TYPE_SRFLX) {
646                 candidate->type = AST_RTP_ICE_CANDIDATE_TYPE_SRFLX;
647         } else if (type == PJ_ICE_CAND_TYPE_RELAYED) {
648                 candidate->type = AST_RTP_ICE_CANDIDATE_TYPE_RELAYED;
649         }
650
651         if ((existing = ao2_find(rtp->local_candidates, candidate, OBJ_POINTER))) {
652                 ao2_ref(existing, -1);
653                 ao2_ref(candidate, -1);
654                 return;
655         }
656
657         if (pj_ice_sess_add_cand(rtp->ice, comp_id, transport_id, type, local_pref, &foundation, addr, addr, rel_addr, addr_len, NULL) != PJ_SUCCESS) {
658                 ao2_ref(candidate, -1);
659                 return;
660         }
661
662         /* By placing the candidate into the ICE session it will have produced the priority, so update the local candidate with it */
663         candidate->priority = rtp->ice->lcand[rtp->ice->lcand_cnt - 1].prio;
664
665         ao2_link(rtp->local_candidates, candidate);
666         ao2_ref(candidate, -1);
667 }
668
669 static char *generate_random_string(char *buf, size_t size)
670 {
671         long val[4];
672         int x;
673
674         for (x=0; x<4; x++)
675                 val[x] = ast_random();
676         snprintf(buf, size, "%08lx%08lx%08lx%08lx", val[0], val[1], val[2], val[3]);
677
678         return buf;
679 }
680
681 /* ICE RTP Engine interface declaration */
682 static struct ast_rtp_engine_ice ast_rtp_ice = {
683         .set_authentication = ast_rtp_ice_set_authentication,
684         .add_remote_candidate = ast_rtp_ice_add_remote_candidate,
685         .start = ast_rtp_ice_start,
686         .stop = ast_rtp_ice_stop,
687         .get_ufrag = ast_rtp_ice_get_ufrag,
688         .get_password = ast_rtp_ice_get_password,
689         .get_local_candidates = ast_rtp_ice_get_local_candidates,
690         .ice_lite = ast_rtp_ice_lite,
691 };
692
693 #ifdef HAVE_OPENSSL_SRTP
694 static void dtls_info_callback(const SSL *ssl, int where, int ret)
695 {
696         struct ast_rtp *rtp = SSL_get_ex_data(ssl, 0);
697
698         /* We only care about alerts */
699         if (!(where & SSL_CB_ALERT)) {
700                 return;
701         }
702
703         rtp->dtls_failure = 1;
704 }
705
706 static int ast_rtp_dtls_set_configuration(struct ast_rtp_instance *instance, const struct ast_rtp_dtls_cfg *dtls_cfg)
707 {
708         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
709
710         if (!dtls_cfg->enabled) {
711                 return 0;
712         }
713
714         if (!ast_rtp_engine_srtp_is_registered()) {
715                 return -1;
716         }
717
718         if (!(rtp->ssl_ctx = SSL_CTX_new(DTLSv1_method()))) {
719                 return -1;
720         }
721
722         SSL_CTX_set_verify(rtp->ssl_ctx, dtls_cfg->verify ? SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT : SSL_VERIFY_NONE, NULL);
723
724         if (dtls_cfg->suite == AST_AES_CM_128_HMAC_SHA1_80) {
725                 SSL_CTX_set_tlsext_use_srtp(rtp->ssl_ctx, "SRTP_AES128_CM_SHA1_80");
726         } else if (dtls_cfg->suite == AST_AES_CM_128_HMAC_SHA1_32) {
727                 SSL_CTX_set_tlsext_use_srtp(rtp->ssl_ctx, "SRTP_AES128_CM_SHA1_32");
728         } else {
729                 ast_log(LOG_ERROR, "Unsupported suite specified for DTLS-SRTP on RTP instance '%p'\n", instance);
730                 goto error;
731         }
732
733         if (!ast_strlen_zero(dtls_cfg->certfile)) {
734                 char *private = ast_strlen_zero(dtls_cfg->pvtfile) ? dtls_cfg->certfile : dtls_cfg->pvtfile;
735                 BIO *certbio;
736                 X509 *cert;
737                 unsigned int size, i;
738                 unsigned char fingerprint[EVP_MAX_MD_SIZE];
739                 char *local_fingerprint = rtp->local_fingerprint;
740
741                 if (!SSL_CTX_use_certificate_file(rtp->ssl_ctx, dtls_cfg->certfile, SSL_FILETYPE_PEM)) {
742                         ast_log(LOG_ERROR, "Specified certificate file '%s' for RTP instance '%p' could not be used\n",
743                                 dtls_cfg->certfile, instance);
744                         goto error;
745                 }
746
747                 if (!SSL_CTX_use_PrivateKey_file(rtp->ssl_ctx, private, SSL_FILETYPE_PEM) ||
748                     !SSL_CTX_check_private_key(rtp->ssl_ctx)) {
749                         ast_log(LOG_ERROR, "Specified private key file '%s' for RTP instance '%p' could not be used\n",
750                                 private, instance);
751                         goto error;
752                 }
753
754                 if (!(certbio = BIO_new(BIO_s_file()))) {
755                         ast_log(LOG_ERROR, "Failed to allocate memory for certificate fingerprinting on RTP instance '%p'\n",
756                                 instance);
757                         goto error;
758                 }
759
760                 if (!BIO_read_filename(certbio, dtls_cfg->certfile) ||
761                     !(cert = PEM_read_bio_X509(certbio, NULL, 0, NULL)) ||
762                     !X509_digest(cert, EVP_sha1(), fingerprint, &size) ||
763                     !size) {
764                         ast_log(LOG_ERROR, "Could not produce fingerprint from certificate '%s' for RTP instance '%p'\n",
765                                 dtls_cfg->certfile, instance);
766                         BIO_free_all(certbio);
767                         goto error;
768                 }
769
770                 for (i = 0; i < size; i++) {
771                         sprintf(local_fingerprint, "%.2X:", fingerprint[i]);
772                         local_fingerprint += 3;
773                 }
774
775                 *(local_fingerprint-1) = 0;
776
777                 BIO_free_all(certbio);
778         }
779
780         if (!ast_strlen_zero(dtls_cfg->cipher)) {
781                 if (!SSL_CTX_set_cipher_list(rtp->ssl_ctx, dtls_cfg->cipher)) {
782                         ast_log(LOG_ERROR, "Invalid cipher specified in cipher list '%s' for RTP instance '%p'\n",
783                                 dtls_cfg->cipher, instance);
784                         goto error;
785                 }
786         }
787
788         if (!ast_strlen_zero(dtls_cfg->cafile) || !ast_strlen_zero(dtls_cfg->capath)) {
789                 if (!SSL_CTX_load_verify_locations(rtp->ssl_ctx, S_OR(dtls_cfg->cafile, NULL), S_OR(dtls_cfg->capath, NULL))) {
790                         ast_log(LOG_ERROR, "Invalid certificate authority file '%s' or path '%s' specified for RTP instance '%p'\n",
791                                 S_OR(dtls_cfg->cafile, ""), S_OR(dtls_cfg->capath, ""), instance);
792                         goto error;
793                 }
794         }
795
796         rtp->rekey = dtls_cfg->rekey;
797         rtp->dtls_setup = dtls_cfg->default_setup;
798         rtp->suite = dtls_cfg->suite;
799
800         if (!(rtp->ssl = SSL_new(rtp->ssl_ctx))) {
801                 ast_log(LOG_ERROR, "Failed to allocate memory for SSL context on RTP instance '%p'\n",
802                         instance);
803                 goto error;
804         }
805
806         SSL_set_ex_data(rtp->ssl, 0, rtp);
807         SSL_set_info_callback(rtp->ssl, dtls_info_callback);
808
809         if (!(rtp->read_bio = BIO_new(BIO_s_mem()))) {
810                 ast_log(LOG_ERROR, "Failed to allocate memory for inbound SSL traffic on RTP instance '%p'\n",
811                         instance);
812                 goto error;
813         }
814         BIO_set_mem_eof_return(rtp->read_bio, -1);
815
816         if (!(rtp->write_bio = BIO_new(BIO_s_mem()))) {
817                 ast_log(LOG_ERROR, "Failed to allocate memory for outbound SSL traffic on RTP instance '%p'\n",
818                         instance);
819                 goto error;
820         }
821         BIO_set_mem_eof_return(rtp->write_bio, -1);
822
823         SSL_set_bio(rtp->ssl, rtp->read_bio, rtp->write_bio);
824
825         if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) {
826                 SSL_set_accept_state(rtp->ssl);
827         } else {
828                 SSL_set_connect_state(rtp->ssl);
829         }
830
831         rtp->connection = AST_RTP_DTLS_CONNECTION_NEW;
832
833         return 0;
834
835 error:
836         if (rtp->read_bio) {
837                 BIO_free(rtp->read_bio);
838                 rtp->read_bio = NULL;
839         }
840
841         if (rtp->write_bio) {
842                 BIO_free(rtp->write_bio);
843                 rtp->write_bio = NULL;
844         }
845
846         if (rtp->ssl) {
847                 SSL_free(rtp->ssl);
848                 rtp->ssl = NULL;
849         }
850
851         SSL_CTX_free(rtp->ssl_ctx);
852         rtp->ssl_ctx = NULL;
853
854         return -1;
855 }
856
857 static int ast_rtp_dtls_active(struct ast_rtp_instance *instance)
858 {
859         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
860
861         return !rtp->ssl_ctx ? 0 : 1;
862 }
863
864 static void ast_rtp_dtls_stop(struct ast_rtp_instance *instance)
865 {
866         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
867
868         if (rtp->ssl_ctx) {
869                 SSL_CTX_free(rtp->ssl_ctx);
870                 rtp->ssl_ctx = NULL;
871         }
872
873         if (rtp->ssl) {
874                 SSL_free(rtp->ssl);
875                 rtp->ssl = NULL;
876         }
877 }
878
879 static void ast_rtp_dtls_reset(struct ast_rtp_instance *instance)
880 {
881         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
882
883         /* If the SSL session is not yet finalized don't bother resetting */
884         if (!SSL_is_init_finished(rtp->ssl)) {
885                 return;
886         }
887
888         SSL_shutdown(rtp->ssl);
889         rtp->connection = AST_RTP_DTLS_CONNECTION_NEW;
890 }
891
892 static enum ast_rtp_dtls_connection ast_rtp_dtls_get_connection(struct ast_rtp_instance *instance)
893 {
894         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
895
896         return rtp->connection;
897 }
898
899 static enum ast_rtp_dtls_setup ast_rtp_dtls_get_setup(struct ast_rtp_instance *instance)
900 {
901         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
902
903         return rtp->dtls_setup;
904 }
905
906 static void ast_rtp_dtls_set_setup(struct ast_rtp_instance *instance, enum ast_rtp_dtls_setup setup)
907 {
908         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
909         enum ast_rtp_dtls_setup old = rtp->dtls_setup;
910
911         switch (setup) {
912         case AST_RTP_DTLS_SETUP_ACTIVE:
913                 rtp->dtls_setup = AST_RTP_DTLS_SETUP_PASSIVE;
914                 break;
915         case AST_RTP_DTLS_SETUP_PASSIVE:
916                 rtp->dtls_setup = AST_RTP_DTLS_SETUP_ACTIVE;
917                 break;
918         case AST_RTP_DTLS_SETUP_ACTPASS:
919                 /* We can't respond to an actpass setup with actpass ourselves... so respond with active, as we can initiate connections */
920                 if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_ACTPASS) {
921                         rtp->dtls_setup = AST_RTP_DTLS_SETUP_ACTIVE;
922                 }
923                 break;
924         case AST_RTP_DTLS_SETUP_HOLDCONN:
925                 rtp->dtls_setup = AST_RTP_DTLS_SETUP_HOLDCONN;
926                 break;
927         default:
928                 /* This should never occur... if it does exit early as we don't know what state things are in */
929                 return;
930         }
931
932         /* If the setup state did not change we go on as if nothing happened */
933         if (old == rtp->dtls_setup) {
934                 return;
935         }
936
937         /* If they don't want us to establish a connection wait until later */
938         if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_HOLDCONN) {
939                 return;
940         }
941
942         if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_ACTIVE) {
943                 SSL_set_connect_state(rtp->ssl);
944         } else if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) {
945                 SSL_set_accept_state(rtp->ssl);
946         } else {
947                 return;
948         }
949 }
950
951 static void ast_rtp_dtls_set_fingerprint(struct ast_rtp_instance *instance, enum ast_rtp_dtls_hash hash, const char *fingerprint)
952 {
953         char *tmp = ast_strdupa(fingerprint), *value;
954         int pos = 0;
955         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
956
957         if (hash != AST_RTP_DTLS_HASH_SHA1) {
958                 return;
959         }
960
961         while ((value = strsep(&tmp, ":")) && (pos != (EVP_MAX_MD_SIZE - 1))) {
962                 sscanf(value, "%02x", (unsigned int*)&rtp->remote_fingerprint[pos++]);
963         }
964 }
965
966 static const char *ast_rtp_dtls_get_fingerprint(struct ast_rtp_instance *instance, enum ast_rtp_dtls_hash hash)
967 {
968         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
969
970         if (hash != AST_RTP_DTLS_HASH_SHA1) {
971                 return NULL;
972         }
973
974         return rtp->local_fingerprint;
975 }
976
977 /* DTLS RTP Engine interface declaration */
978 static struct ast_rtp_engine_dtls ast_rtp_dtls = {
979         .set_configuration = ast_rtp_dtls_set_configuration,
980         .active = ast_rtp_dtls_active,
981         .stop = ast_rtp_dtls_stop,
982         .reset = ast_rtp_dtls_reset,
983         .get_connection = ast_rtp_dtls_get_connection,
984         .get_setup = ast_rtp_dtls_get_setup,
985         .set_setup = ast_rtp_dtls_set_setup,
986         .set_fingerprint = ast_rtp_dtls_set_fingerprint,
987         .get_fingerprint = ast_rtp_dtls_get_fingerprint,
988 };
989
990 #endif
991
992 /* RTP Engine Declaration */
993 static struct ast_rtp_engine asterisk_rtp_engine = {
994         .name = "asterisk",
995         .new = ast_rtp_new,
996         .destroy = ast_rtp_destroy,
997         .dtmf_begin = ast_rtp_dtmf_begin,
998         .dtmf_end = ast_rtp_dtmf_end,
999         .dtmf_end_with_duration = ast_rtp_dtmf_end_with_duration,
1000         .dtmf_mode_set = ast_rtp_dtmf_mode_set,
1001         .dtmf_mode_get = ast_rtp_dtmf_mode_get,
1002         .update_source = ast_rtp_update_source,
1003         .change_source = ast_rtp_change_source,
1004         .write = ast_rtp_write,
1005         .read = ast_rtp_read,
1006         .prop_set = ast_rtp_prop_set,
1007         .fd = ast_rtp_fd,
1008         .remote_address_set = ast_rtp_remote_address_set,
1009         .alt_remote_address_set = ast_rtp_alt_remote_address_set,
1010         .red_init = rtp_red_init,
1011         .red_buffer = rtp_red_buffer,
1012         .local_bridge = ast_rtp_local_bridge,
1013         .get_stat = ast_rtp_get_stat,
1014         .dtmf_compatible = ast_rtp_dtmf_compatible,
1015         .stun_request = ast_rtp_stun_request,
1016         .stop = ast_rtp_stop,
1017         .qos = ast_rtp_qos_set,
1018         .sendcng = ast_rtp_sendcng,
1019         .ice = &ast_rtp_ice,
1020 #ifdef HAVE_OPENSSL_SRTP
1021         .dtls = &ast_rtp_dtls,
1022         .activate = ast_rtp_activate,
1023 #endif
1024 };
1025
1026 static void ast_rtp_on_ice_rx_data(pj_ice_sess *ice, unsigned comp_id, unsigned transport_id, void *pkt, pj_size_t size, const pj_sockaddr_t *src_addr, unsigned src_addr_len)
1027 {
1028         struct ast_rtp *rtp = ice->user_data;
1029
1030         /* Instead of handling the packet here (which really doesn't work with our architecture) we set a bit to indicate that it should be handled after pj_ice_sess_on_rx_pkt
1031          * returns */
1032         rtp->passthrough = 1;
1033 }
1034
1035 static pj_status_t ast_rtp_on_ice_tx_pkt(pj_ice_sess *ice, unsigned comp_id, unsigned transport_id, const void *pkt, pj_size_t size, const pj_sockaddr_t *dst_addr, unsigned dst_addr_len)
1036 {
1037         struct ast_rtp *rtp = ice->user_data;
1038         pj_status_t status = PJ_EINVALIDOP;
1039         pj_ssize_t _size = (pj_ssize_t)size;
1040
1041         if (transport_id == TRANSPORT_SOCKET_RTP) {
1042                 /* Traffic is destined to go right out the RTP socket we already have */
1043                 status = pj_sock_sendto(rtp->s, pkt, &_size, 0, dst_addr, dst_addr_len);
1044                 /* sendto on a connectionless socket should send all the data, or none at all */
1045                 ast_assert(_size == size || status != PJ_SUCCESS);
1046         } else if (transport_id == TRANSPORT_SOCKET_RTCP) {
1047                 /* Traffic is destined to go right out the RTCP socket we already have */
1048                 if (rtp->rtcp) {
1049                         status = pj_sock_sendto(rtp->rtcp->s, pkt, &_size, 0, dst_addr, dst_addr_len);
1050                         /* sendto on a connectionless socket should send all the data, or none at all */
1051                         ast_assert(_size == size || status != PJ_SUCCESS);
1052                 } else {
1053                         status = PJ_SUCCESS;
1054                 }
1055         } else if (transport_id == TRANSPORT_TURN_RTP) {
1056                 /* Traffic is going through the RTP TURN relay */
1057                 if (rtp->turn_rtp) {
1058                         status = pj_turn_sock_sendto(rtp->turn_rtp, pkt, size, dst_addr, dst_addr_len);
1059                 }
1060         } else if (transport_id == TRANSPORT_TURN_RTCP) {
1061                 /* Traffic is going through the RTCP TURN relay */
1062                 if (rtp->turn_rtcp) {
1063                         status = pj_turn_sock_sendto(rtp->turn_rtcp, pkt, size, dst_addr, dst_addr_len);
1064                 }
1065         }
1066
1067         return status;
1068 }
1069
1070 /* ICE Session interface declaration */
1071 static pj_ice_sess_cb ast_rtp_ice_sess_cb = {
1072         .on_rx_data = ast_rtp_on_ice_rx_data,
1073         .on_tx_pkt = ast_rtp_on_ice_tx_pkt,
1074 };
1075
1076 static void ast_rtp_on_turn_rx_rtp_data(pj_turn_sock *turn_sock, void *pkt, unsigned pkt_len, const pj_sockaddr_t *peer_addr, unsigned addr_len)
1077 {
1078         struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
1079         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1080         struct ast_sockaddr dest = { { 0, }, };
1081
1082         ast_rtp_instance_get_local_address(instance, &dest);
1083
1084         ast_sendto(rtp->s, pkt, pkt_len, 0, &dest);
1085 }
1086
1087 static void ast_rtp_on_turn_rtp_state(pj_turn_sock *turn_sock, pj_turn_state_t old_state, pj_turn_state_t new_state)
1088 {
1089         struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
1090         struct ast_rtp *rtp = NULL;
1091
1092         /* If this is a leftover from an already destroyed RTP instance just ignore the state change */
1093         if (!instance) {
1094                 return;
1095         }
1096
1097         rtp = ast_rtp_instance_get_data(instance);
1098
1099         /* If the TURN session is being destroyed we need to remove it from the RTP instance */
1100         if (new_state == PJ_TURN_STATE_DESTROYING) {
1101                 rtp->turn_rtp = NULL;
1102                 return;
1103         }
1104
1105         /* We store the new state so the other thread can actually handle it */
1106         ast_mutex_lock(&rtp->lock);
1107         rtp->turn_state = new_state;
1108
1109         /* If this is a state that the main thread should be notified about do so */
1110         if (new_state == PJ_TURN_STATE_READY || new_state == PJ_TURN_STATE_DEALLOCATING || new_state == PJ_TURN_STATE_DEALLOCATED) {
1111                 ast_cond_signal(&rtp->cond);
1112         }
1113
1114         ast_mutex_unlock(&rtp->lock);
1115 }
1116
1117 /* RTP TURN Socket interface declaration */
1118 static pj_turn_sock_cb ast_rtp_turn_rtp_sock_cb = {
1119         .on_rx_data = ast_rtp_on_turn_rx_rtp_data,
1120         .on_state = ast_rtp_on_turn_rtp_state,
1121 };
1122
1123 static void ast_rtp_on_turn_rx_rtcp_data(pj_turn_sock *turn_sock, void *pkt, unsigned pkt_len, const pj_sockaddr_t *peer_addr, unsigned addr_len)
1124 {
1125         struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
1126         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1127
1128         ast_sendto(rtp->rtcp->s, pkt, pkt_len, 0, &rtp->rtcp->us);
1129 }
1130
1131 static void ast_rtp_on_turn_rtcp_state(pj_turn_sock *turn_sock, pj_turn_state_t old_state, pj_turn_state_t new_state)
1132 {
1133         struct ast_rtp_instance *instance = pj_turn_sock_get_user_data(turn_sock);
1134         struct ast_rtp *rtp = NULL;
1135
1136         /* If this is a leftover from an already destroyed RTP instance just ignore the state change */
1137         if (!instance) {
1138                 return;
1139         }
1140
1141         rtp = ast_rtp_instance_get_data(instance);
1142
1143         /* If the TURN session is being destroyed we need to remove it from the RTP instance */
1144         if (new_state == PJ_TURN_STATE_DESTROYING) {
1145                 rtp->turn_rtcp = NULL;
1146                 return;
1147         }
1148
1149         /* We store the new state so the other thread can actually handle it */
1150         ast_mutex_lock(&rtp->lock);
1151         rtp->turn_state = new_state;
1152
1153         /* If this is a state that the main thread should be notified about do so */
1154         if (new_state == PJ_TURN_STATE_READY || new_state == PJ_TURN_STATE_DEALLOCATING || new_state == PJ_TURN_STATE_DEALLOCATED) {
1155                 ast_cond_signal(&rtp->cond);
1156         }
1157
1158        ast_mutex_unlock(&rtp->lock);
1159 }
1160
1161 /* RTCP TURN Socket interface declaration */
1162 static pj_turn_sock_cb ast_rtp_turn_rtcp_sock_cb = {
1163         .on_rx_data = ast_rtp_on_turn_rx_rtcp_data,
1164         .on_state = ast_rtp_on_turn_rtcp_state,
1165 };
1166
1167 /*! \brief Worker thread for I/O queue and timerheap */
1168 static int ice_worker_thread(void *data)
1169 {
1170         while (!worker_terminate) {
1171                 const pj_time_val delay = {0, 10};
1172
1173                 pj_ioqueue_poll(ioqueue, &delay);
1174
1175                 pj_timer_heap_poll(timerheap, NULL);
1176         }
1177
1178         return 0;
1179 }
1180
1181 static inline int rtp_debug_test_addr(struct ast_sockaddr *addr)
1182 {
1183         if (!rtpdebug) {
1184                 return 0;
1185         }
1186         if (!ast_sockaddr_isnull(&rtpdebugaddr)) {
1187                 if (rtpdebugport) {
1188                         return (ast_sockaddr_cmp(&rtpdebugaddr, addr) == 0); /* look for RTP packets from IP+Port */
1189                 } else {
1190                         return (ast_sockaddr_cmp_addr(&rtpdebugaddr, addr) == 0); /* only look for RTP packets from IP */
1191                 }
1192         }
1193
1194         return 1;
1195 }
1196
1197 static inline int rtcp_debug_test_addr(struct ast_sockaddr *addr)
1198 {
1199         if (!rtcpdebug) {
1200                 return 0;
1201         }
1202         if (!ast_sockaddr_isnull(&rtcpdebugaddr)) {
1203                 if (rtcpdebugport) {
1204                         return (ast_sockaddr_cmp(&rtcpdebugaddr, addr) == 0); /* look for RTCP packets from IP+Port */
1205                 } else {
1206                         return (ast_sockaddr_cmp_addr(&rtcpdebugaddr, addr) == 0); /* only look for RTCP packets from IP */
1207                 }
1208         }
1209
1210         return 1;
1211 }
1212
1213 #ifdef HAVE_OPENSSL_SRTP
1214 static void dtls_srtp_check_pending(struct ast_rtp_instance *instance, struct ast_rtp *rtp)
1215 {
1216         size_t pending = BIO_ctrl_pending(rtp->write_bio);
1217
1218         if (pending > 0) {
1219                 char outgoing[pending];
1220                 size_t out;
1221                 struct ast_sockaddr remote_address = { {0, } };
1222                 int ice;
1223
1224                 ast_rtp_instance_get_remote_address(instance, &remote_address);
1225
1226                 /* If we do not yet know an address to send this to defer it until we do */
1227                 if (ast_sockaddr_isnull(&remote_address)) {
1228                         return;
1229                 }
1230
1231                 out = BIO_read(rtp->write_bio, outgoing, sizeof(outgoing));
1232
1233                 __rtp_sendto(instance, outgoing, out, 0, &remote_address, 0, &ice, 0);
1234         }
1235 }
1236
1237 static int dtls_srtp_renegotiate(const void *data)
1238 {
1239         struct ast_rtp_instance *instance = (struct ast_rtp_instance *)data;
1240         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1241
1242         SSL_renegotiate(rtp->ssl);
1243         SSL_do_handshake(rtp->ssl);
1244         dtls_srtp_check_pending(instance, rtp);
1245
1246         rtp->rekeyid = -1;
1247         ao2_ref(instance, -1);
1248
1249         return 0;
1250 }
1251
1252 static int dtls_srtp_setup(struct ast_rtp *rtp, struct ast_srtp *srtp, struct ast_rtp_instance *instance)
1253 {
1254         unsigned char material[SRTP_MASTER_LEN * 2];
1255         unsigned char *local_key, *local_salt, *remote_key, *remote_salt;
1256         struct ast_srtp_policy *local_policy, *remote_policy = NULL;
1257         struct ast_rtp_instance_stats stats = { 0, };
1258
1259         /* If a fingerprint is present in the SDP make sure that the peer certificate matches it */
1260         if (SSL_CTX_get_verify_mode(rtp->ssl_ctx) != SSL_VERIFY_NONE) {
1261                 X509 *certificate;
1262
1263                 if (!(certificate = SSL_get_peer_certificate(rtp->ssl))) {
1264                         ast_log(LOG_WARNING, "No certificate was provided by the peer on RTP instance '%p'\n", instance);
1265                         return -1;
1266                 }
1267
1268                 /* If a fingerprint is present in the SDP make sure that the peer certificate matches it */
1269                 if (rtp->remote_fingerprint[0]) {
1270                         unsigned char fingerprint[EVP_MAX_MD_SIZE];
1271                         unsigned int size;
1272
1273                         if (!X509_digest(certificate, EVP_sha1(), fingerprint, &size) ||
1274                             !size ||
1275                             memcmp(fingerprint, rtp->remote_fingerprint, size)) {
1276                                 X509_free(certificate);
1277                                 ast_log(LOG_WARNING, "Fingerprint provided by remote party does not match that of peer certificate on RTP instance '%p'\n",
1278                                         instance);
1279                                 return -1;
1280                         }
1281                 }
1282
1283                 X509_free(certificate);
1284         }
1285
1286         /* Ensure that certificate verification was successful */
1287         if (SSL_get_verify_result(rtp->ssl) != X509_V_OK) {
1288                 ast_log(LOG_WARNING, "Peer certificate on RTP instance '%p' failed verification test\n",
1289                         instance);
1290                 return -1;
1291         }
1292
1293         /* Produce key information and set up SRTP */
1294         if (!SSL_export_keying_material(rtp->ssl, material, SRTP_MASTER_LEN * 2, "EXTRACTOR-dtls_srtp", 19, NULL, 0, 0)) {
1295                 ast_log(LOG_WARNING, "Unable to extract SRTP keying material from DTLS-SRTP negotiation on RTP instance '%p'\n",
1296                         instance);
1297                 return -1;
1298         }
1299
1300         /* Whether we are acting as a server or client determines where the keys/salts are */
1301         if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_ACTIVE) {
1302                 local_key = material;
1303                 remote_key = local_key + SRTP_MASTER_KEY_LEN;
1304                 local_salt = remote_key + SRTP_MASTER_KEY_LEN;
1305                 remote_salt = local_salt + SRTP_MASTER_SALT_LEN;
1306         } else {
1307                 remote_key = material;
1308                 local_key = remote_key + SRTP_MASTER_KEY_LEN;
1309                 remote_salt = local_key + SRTP_MASTER_KEY_LEN;
1310                 local_salt = remote_salt + SRTP_MASTER_SALT_LEN;
1311         }
1312
1313         if (!(local_policy = res_srtp_policy->alloc())) {
1314                 return -1;
1315         }
1316
1317         if (res_srtp_policy->set_master_key(local_policy, local_key, SRTP_MASTER_KEY_LEN, local_salt, SRTP_MASTER_SALT_LEN) < 0) {
1318                 ast_log(LOG_WARNING, "Could not set key/salt information on local policy of '%p' when setting up DTLS-SRTP\n", rtp);
1319                 goto error;
1320         }
1321
1322         if (res_srtp_policy->set_suite(local_policy, rtp->suite)) {
1323                 ast_log(LOG_WARNING, "Could not set suite to '%d' on local policy of '%p' when setting up DTLS-SRTP\n", rtp->suite, rtp);
1324                 goto error;
1325         }
1326
1327         if (ast_rtp_instance_get_stats(instance, &stats, AST_RTP_INSTANCE_STAT_LOCAL_SSRC)) {
1328                 goto error;
1329         }
1330
1331         res_srtp_policy->set_ssrc(local_policy, stats.local_ssrc, 0);
1332
1333         if (!(remote_policy = res_srtp_policy->alloc())) {
1334                 goto error;
1335         }
1336
1337         if (res_srtp_policy->set_master_key(remote_policy, remote_key, SRTP_MASTER_KEY_LEN, remote_salt, SRTP_MASTER_SALT_LEN) < 0) {
1338                 ast_log(LOG_WARNING, "Could not set key/salt information on remote policy of '%p' when setting up DTLS-SRTP\n", rtp);
1339                 goto error;
1340         }
1341
1342         if (res_srtp_policy->set_suite(remote_policy, rtp->suite)) {
1343                 ast_log(LOG_WARNING, "Could not set suite to '%d' on remote policy of '%p' when setting up DTLS-SRTP\n", rtp->suite, rtp);
1344                 goto error;
1345         }
1346
1347         res_srtp_policy->set_ssrc(remote_policy, 0, 1);
1348
1349         if (ast_rtp_instance_add_srtp_policy(instance, remote_policy, local_policy)) {
1350                 ast_log(LOG_WARNING, "Could not set policies when setting up DTLS-SRTP on '%p'\n", rtp);
1351                 goto error;
1352         }
1353
1354         if (rtp->rekey) {
1355                 ao2_ref(instance, +1);
1356                 if ((rtp->rekeyid = ast_sched_add(rtp->sched, rtp->rekey * 1000, dtls_srtp_renegotiate, instance)) < 0) {
1357                         ao2_ref(instance, -1);
1358                         goto error;
1359                 }
1360         }
1361
1362         return 0;
1363
1364 error:
1365         res_srtp_policy->destroy(local_policy);
1366
1367         if (remote_policy) {
1368                 res_srtp_policy->destroy(remote_policy);
1369         }
1370
1371         return -1;
1372 }
1373 #endif
1374
1375 static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp)
1376 {
1377         int len;
1378         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1379         struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance);
1380
1381         if ((len = ast_recvfrom(rtcp ? rtp->rtcp->s : rtp->s, buf, size, flags, sa)) < 0) {
1382            return len;
1383         }
1384
1385 #ifdef HAVE_OPENSSL_SRTP
1386         if (!rtcp) {
1387                 char *in = buf;
1388
1389                 dtls_srtp_check_pending(instance, rtp);
1390
1391                 /* If this is an SSL packet pass it to OpenSSL for processing */
1392                 if ((*in >= 20) && (*in <= 64)) {
1393                         int res = 0;
1394
1395                         /* If no SSL session actually exists terminate things */
1396                         if (!rtp->ssl) {
1397                                 ast_log(LOG_ERROR, "Received SSL traffic on RTP instance '%p' without an SSL session\n",
1398                                         instance);
1399                                 return -1;
1400                         }
1401
1402                         /* If we don't yet know if we are active or passive and we receive a packet... we are obviously passive */
1403                         if (rtp->dtls_setup == AST_RTP_DTLS_SETUP_ACTPASS) {
1404                                 rtp->dtls_setup = AST_RTP_DTLS_SETUP_PASSIVE;
1405                                 SSL_set_accept_state(rtp->ssl);
1406                         }
1407
1408                         dtls_srtp_check_pending(instance, rtp);
1409
1410                         BIO_write(rtp->read_bio, buf, len);
1411
1412                         len = SSL_read(rtp->ssl, buf, len);
1413
1414                         dtls_srtp_check_pending(instance, rtp);
1415
1416                         if (rtp->dtls_failure) {
1417                                 ast_log(LOG_ERROR, "DTLS failure occurred on RTP instance '%p', terminating\n",
1418                                         instance);
1419                                 return -1;
1420                         }
1421
1422                         if (SSL_is_init_finished(rtp->ssl)) {
1423                                 /* Any further connections will be existing since this is now established */
1424                                 rtp->connection = AST_RTP_DTLS_CONNECTION_EXISTING;
1425
1426                                 /* Use the keying material to set up key/salt information */
1427                                 res = dtls_srtp_setup(rtp, srtp, instance);
1428                         }
1429
1430                         return res;
1431                 }
1432         }
1433 #endif
1434
1435         if (rtp->ice) {
1436                 pj_str_t combined = pj_str(ast_sockaddr_stringify(sa));
1437                 pj_sockaddr address;
1438                 pj_status_t status;
1439
1440                 pj_thread_register_check();
1441
1442                 pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &combined, &address);
1443
1444                 status = pj_ice_sess_on_rx_pkt(rtp->ice, rtcp ? COMPONENT_RTCP : COMPONENT_RTP,
1445                         rtcp ? TRANSPORT_SOCKET_RTCP : TRANSPORT_SOCKET_RTP, buf, len, &address,
1446                         pj_sockaddr_get_len(&address));
1447                 if (status != PJ_SUCCESS) {
1448                         char buf[100];
1449
1450                         pj_strerror(status, buf, sizeof(buf));
1451                         ast_log(LOG_WARNING, "PJ ICE Rx error status code: %d '%s'.\n",
1452                                 (int) status, buf);
1453                         return -1;
1454                 }
1455                 if (!rtp->passthrough) {
1456                         return 0;
1457                 }
1458                 rtp->passthrough = 0;
1459         }
1460
1461         if (res_srtp && srtp && res_srtp->unprotect(srtp, buf, &len, rtcp) < 0) {
1462            return -1;
1463         }
1464
1465         return len;
1466 }
1467
1468 static int rtcp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
1469 {
1470         return __rtp_recvfrom(instance, buf, size, flags, sa, 1);
1471 }
1472
1473 static int rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
1474 {
1475         return __rtp_recvfrom(instance, buf, size, flags, sa, 0);
1476 }
1477
1478 static int __rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp, int *ice, int use_srtp)
1479 {
1480         int len = size;
1481         void *temp = buf;
1482         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1483         struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance);
1484
1485         *ice = 0;
1486
1487         if (use_srtp && res_srtp && srtp && res_srtp->protect(srtp, &temp, &len, rtcp) < 0) {
1488                 return -1;
1489         }
1490
1491         if (rtp->ice) {
1492                 pj_thread_register_check();
1493
1494                 if (pj_ice_sess_send_data(rtp->ice, rtcp ? COMPONENT_RTCP : COMPONENT_RTP, temp, len) == PJ_SUCCESS) {
1495                         *ice = 1;
1496                         return 0;
1497                 }
1498         }
1499
1500         return ast_sendto(rtcp ? rtp->rtcp->s : rtp->s, temp, len, flags, sa);
1501 }
1502
1503 static int rtcp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice)
1504 {
1505         return __rtp_sendto(instance, buf, size, flags, sa, 1, ice, 1);
1506 }
1507
1508 static int rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice)
1509 {
1510         return __rtp_sendto(instance, buf, size, flags, sa, 0, ice, 1);
1511 }
1512
1513 static int rtp_get_rate(struct ast_format *format)
1514 {
1515         return (format->id == AST_FORMAT_G722) ? 8000 : ast_format_rate(format);
1516 }
1517
1518 static unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp)
1519 {
1520         unsigned int interval;
1521         /*! \todo XXX Do a more reasonable calculation on this one
1522          * Look in RFC 3550 Section A.7 for an example*/
1523         interval = rtcpinterval;
1524         return interval;
1525 }
1526
1527 /*! \brief Calculate normal deviation */
1528 static double normdev_compute(double normdev, double sample, unsigned int sample_count)
1529 {
1530         normdev = normdev * sample_count + sample;
1531         sample_count++;
1532
1533         return normdev / sample_count;
1534 }
1535
1536 static double stddev_compute(double stddev, double sample, double normdev, double normdev_curent, unsigned int sample_count)
1537 {
1538 /*
1539                 for the formula check http://www.cs.umd.edu/~austinjp/constSD.pdf
1540                 return sqrt( (sample_count*pow(stddev,2) + sample_count*pow((sample-normdev)/(sample_count+1),2) + pow(sample-normdev_curent,2)) / (sample_count+1));
1541                 we can compute the sigma^2 and that way we would have to do the sqrt only 1 time at the end and would save another pow 2 compute
1542                 optimized formula
1543 */
1544 #define SQUARE(x) ((x) * (x))
1545
1546         stddev = sample_count * stddev;
1547         sample_count++;
1548
1549         return stddev +
1550                 ( sample_count * SQUARE( (sample - normdev) / sample_count ) ) +
1551                 ( SQUARE(sample - normdev_curent) / sample_count );
1552
1553 #undef SQUARE
1554 }
1555
1556 static int create_new_socket(const char *type, int af)
1557 {
1558         int sock = socket(af, SOCK_DGRAM, 0);
1559
1560         if (sock < 0) {
1561                 if (!type) {
1562                         type = "RTP/RTCP";
1563                 }
1564                 ast_log(LOG_WARNING, "Unable to allocate %s socket: %s\n", type, strerror(errno));
1565         } else {
1566                 long flags = fcntl(sock, F_GETFL);
1567                 fcntl(sock, F_SETFL, flags | O_NONBLOCK);
1568 #ifdef SO_NO_CHECK
1569                 if (nochecksums) {
1570                         setsockopt(sock, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
1571                 }
1572 #endif
1573         }
1574
1575         return sock;
1576 }
1577
1578 /*!
1579  * \internal
1580  * \brief Initializes sequence values and probation for learning mode.
1581  * \note This is an adaptation of pjmedia's pjmedia_rtp_seq_init function.
1582  *
1583  * \param rtp pointer to rtp struct used with the received rtp packet.
1584  * \param seq sequence number read from the rtp header
1585  */
1586 static void rtp_learning_seq_init(struct ast_rtp *rtp, uint16_t seq)
1587 {
1588         rtp->learning_max_seq = seq - 1;
1589         rtp->learning_probation = learning_min_sequential;
1590 }
1591
1592 /*!
1593  * \internal
1594  * \brief Updates sequence information for learning mode and determines if probation/learning mode should remain in effect.
1595  * \note This function was adapted from pjmedia's pjmedia_rtp_seq_update function.
1596  *
1597  * \param rtp pointer to rtp struct used with the received rtp packet.
1598  * \param seq sequence number read from the rtp header
1599  * \return boolean value indicating if probation mode is active at the end of the function
1600  */
1601 static int rtp_learning_rtp_seq_update(struct ast_rtp *rtp, uint16_t seq)
1602 {
1603         int probation = 1;
1604
1605         ast_debug(1, "%p -- probation = %d, seq = %d\n", rtp, rtp->learning_probation, seq);
1606
1607         if (seq == rtp->learning_max_seq + 1) {
1608                 /* packet is in sequence */
1609                 rtp->learning_probation--;
1610                 rtp->learning_max_seq = seq;
1611                 if (rtp->learning_probation == 0) {
1612                         probation = 0;
1613                 }
1614         } else {
1615                 rtp->learning_probation = learning_min_sequential - 1;
1616                 rtp->learning_max_seq = seq;
1617         }
1618
1619         return probation;
1620 }
1621
1622 static void rtp_add_candidates_to_ice(struct ast_rtp_instance *instance, struct ast_rtp *rtp, struct ast_sockaddr *addr, int port, int component,
1623                                       int transport, const pj_turn_sock_cb *turn_cb, pj_turn_sock **turn_sock)
1624 {
1625         pj_sockaddr address[16];
1626         unsigned int count = PJ_ARRAY_SIZE(address), pos = 0;
1627
1628         /* Add all the local interface IP addresses */
1629         pj_enum_ip_interface(ast_sockaddr_is_ipv4(addr) ? pj_AF_INET() : pj_AF_INET6(), &count, address);
1630
1631         for (pos = 0; pos < count; pos++) {
1632                 pj_sockaddr_set_port(&address[pos], port);
1633                 ast_rtp_ice_add_cand(rtp, component, transport, PJ_ICE_CAND_TYPE_HOST, 65535, &address[pos], &address[pos], NULL,
1634                                      pj_sockaddr_get_len(&address[pos]));
1635         }
1636
1637         /* If configured to use a STUN server to get our external mapped address do so */
1638         if (stunaddr.sin_addr.s_addr && ast_sockaddr_is_ipv4(addr)) {
1639                 struct sockaddr_in answer;
1640
1641                 if (!ast_stun_request(rtp->s, &stunaddr, NULL, &answer)) {
1642                         pj_str_t mapped = pj_str(ast_strdupa(ast_inet_ntoa(answer.sin_addr)));
1643
1644                         pj_sockaddr_init(pj_AF_INET(), &address[0], &mapped, ntohs(answer.sin_port));
1645
1646                         ast_rtp_ice_add_cand(rtp, component, transport, PJ_ICE_CAND_TYPE_SRFLX, 65535, &address[0], &address[0],
1647                                              NULL, pj_sockaddr_get_len(&address[0]));
1648                 }
1649         }
1650
1651         /* If configured to use a TURN relay create a session and allocate */
1652         if (pj_strlen(&turnaddr) && pj_turn_sock_create(&rtp->ice->stun_cfg, ast_sockaddr_is_ipv4(addr) ? pj_AF_INET() : pj_AF_INET6(), PJ_TURN_TP_TCP,
1653                                                         turn_cb, NULL, instance, turn_sock) == PJ_SUCCESS) {
1654                 pj_stun_auth_cred cred = { 0, };
1655                 struct timeval wait = ast_tvadd(ast_tvnow(), ast_samp2tv(TURN_ALLOCATION_WAIT_TIME, 1000));
1656                 struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, };
1657
1658                 cred.type = PJ_STUN_AUTH_CRED_STATIC;
1659                 cred.data.static_cred.username = turnusername;
1660                 cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
1661                 cred.data.static_cred.data = turnpassword;
1662
1663                 /* Because the TURN socket is asynchronous but we are synchronous we need to wait until it is done */
1664                 ast_mutex_lock(&rtp->lock);
1665                 pj_turn_sock_alloc(*turn_sock, &turnaddr, turnport, NULL, &cred, NULL);
1666                 ast_cond_timedwait(&rtp->cond, &rtp->lock, &ts);
1667                 ast_mutex_unlock(&rtp->lock);
1668
1669                 /* If a TURN session was allocated add it as a candidate */
1670                 if (rtp->turn_state == PJ_TURN_STATE_READY) {
1671                         pj_turn_session_info info;
1672
1673                         pj_turn_sock_get_info(*turn_sock, &info);
1674
1675                         if (transport == TRANSPORT_SOCKET_RTP) {
1676                                 transport = TRANSPORT_TURN_RTP;
1677                         } else if (transport == TRANSPORT_SOCKET_RTCP) {
1678                                 transport = TRANSPORT_TURN_RTCP;
1679                         }
1680
1681                         ast_rtp_ice_add_cand(rtp, component, transport, PJ_ICE_CAND_TYPE_RELAYED, 65535, &info.relay_addr, &info.relay_addr,
1682                                              NULL, pj_sockaddr_get_len(&info.relay_addr));
1683                 }
1684         }
1685 }
1686
1687 static int ast_rtp_new(struct ast_rtp_instance *instance,
1688                        struct ast_sched_context *sched, struct ast_sockaddr *addr,
1689                        void *data)
1690 {
1691         struct ast_rtp *rtp = NULL;
1692         int x, startplace;
1693         pj_stun_config stun_config;
1694         pj_str_t ufrag, passwd;
1695
1696         /* Create a new RTP structure to hold all of our data */
1697         if (!(rtp = ast_calloc(1, sizeof(*rtp)))) {
1698                 return -1;
1699         }
1700
1701         /* Initialize synchronization aspects */
1702         ast_mutex_init(&rtp->lock);
1703         ast_cond_init(&rtp->cond, NULL);
1704
1705         /* Set default parameters on the newly created RTP structure */
1706         rtp->ssrc = ast_random();
1707         rtp->seqno = ast_random() & 0xffff;
1708         rtp->strict_rtp_state = (strictrtp ? STRICT_RTP_LEARN : STRICT_RTP_OPEN);
1709         if (strictrtp) {
1710                 rtp_learning_seq_init(rtp, (uint16_t)rtp->seqno);
1711         }
1712
1713         /* Create a new socket for us to listen on and use */
1714         if ((rtp->s =
1715              create_new_socket("RTP",
1716                                ast_sockaddr_is_ipv4(addr) ? AF_INET  :
1717                                ast_sockaddr_is_ipv6(addr) ? AF_INET6 : -1)) < 0) {
1718                 ast_debug(1, "Failed to create a new socket for RTP instance '%p'\n", instance);
1719                 ast_free(rtp);
1720                 return -1;
1721         }
1722
1723         /* Now actually find a free RTP port to use */
1724         x = (rtpend == rtpstart) ? rtpstart : (ast_random() % (rtpend - rtpstart)) + rtpstart;
1725         x = x & ~1;
1726         startplace = x;
1727
1728         for (;;) {
1729                 ast_sockaddr_set_port(addr, x);
1730                 /* Try to bind, this will tell us whether the port is available or not */
1731                 if (!ast_bind(rtp->s, addr)) {
1732                         ast_debug(1, "Allocated port %d for RTP instance '%p'\n", x, instance);
1733                         ast_rtp_instance_set_local_address(instance, addr);
1734                         break;
1735                 }
1736
1737                 x += 2;
1738                 if (x > rtpend) {
1739                         x = (rtpstart + 1) & ~1;
1740                 }
1741
1742                 /* See if we ran out of ports or if the bind actually failed because of something other than the address being in use */
1743                 if (x == startplace || errno != EADDRINUSE) {
1744                         ast_log(LOG_ERROR, "Oh dear... we couldn't allocate a port for RTP instance '%p'\n", instance);
1745                         close(rtp->s);
1746                         ast_free(rtp);
1747                         return -1;
1748                 }
1749         }
1750
1751         pj_thread_register_check();
1752
1753         pj_stun_config_init(&stun_config, &cachingpool.factory, 0, ioqueue, timerheap);
1754
1755         generate_random_string(rtp->local_ufrag, sizeof(rtp->local_ufrag));
1756         ufrag = pj_str(rtp->local_ufrag);
1757         generate_random_string(rtp->local_passwd, sizeof(rtp->local_passwd));
1758         passwd = pj_str(rtp->local_passwd);
1759
1760         ast_rtp_instance_set_data(instance, rtp);
1761
1762         /* Create an ICE session for ICE negotiation */
1763         if (icesupport && pj_ice_sess_create(&stun_config, NULL, PJ_ICE_SESS_ROLE_UNKNOWN, 2, &ast_rtp_ice_sess_cb, &ufrag, &passwd, &rtp->ice) == PJ_SUCCESS) {
1764                 /* Make this available for the callbacks */
1765                 rtp->ice->user_data = rtp;
1766
1767                 /* Add all of the available candidates to the ICE session */
1768                 rtp_add_candidates_to_ice(instance, rtp, addr, x, COMPONENT_RTP, TRANSPORT_SOCKET_RTP, &ast_rtp_turn_rtp_sock_cb, &rtp->turn_rtp);
1769         }
1770
1771         /* Record any information we may need */
1772         rtp->sched = sched;
1773
1774 #ifdef HAVE_OPENSSL_SRTP
1775         rtp->rekeyid = -1;
1776 #endif
1777
1778         return 0;
1779 }
1780
1781 static int ast_rtp_destroy(struct ast_rtp_instance *instance)
1782 {
1783         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1784
1785         /* Destroy the smoother that was smoothing out audio if present */
1786         if (rtp->smoother) {
1787                 ast_smoother_free(rtp->smoother);
1788         }
1789
1790         /* Close our own socket so we no longer get packets */
1791         if (rtp->s > -1) {
1792                 close(rtp->s);
1793         }
1794
1795         /* Destroy RTCP if it was being used */
1796         if (rtp->rtcp) {
1797                 /*
1798                  * It is not possible for there to be an active RTCP scheduler
1799                  * entry at this point since it holds a reference to the
1800                  * RTP instance while it's active.
1801                  */
1802                 close(rtp->rtcp->s);
1803                 ast_free(rtp->rtcp);
1804         }
1805
1806         /* Destroy RED if it was being used */
1807         if (rtp->red) {
1808                 AST_SCHED_DEL(rtp->sched, rtp->red->schedid);
1809                 ast_free(rtp->red);
1810         }
1811
1812         pj_thread_register_check();
1813
1814         /* Destroy the ICE session if being used */
1815         if (rtp->ice) {
1816                 pj_ice_sess_destroy(rtp->ice);
1817         }
1818
1819         /* Destroy the RTP TURN relay if being used */
1820         if (rtp->turn_rtp) {
1821                 pj_turn_sock_set_user_data(rtp->turn_rtp, NULL);
1822                 pj_turn_sock_destroy(rtp->turn_rtp);
1823         }
1824
1825         /* Destroy the RTCP TURN relay if being used */
1826         if (rtp->turn_rtcp) {
1827                 pj_turn_sock_set_user_data(rtp->turn_rtcp, NULL);
1828                 pj_turn_sock_destroy(rtp->turn_rtcp);
1829         }
1830
1831         /* Destroy any candidates */
1832         if (rtp->local_candidates) {
1833                 ao2_ref(rtp->local_candidates, -1);
1834         }
1835
1836         if (rtp->remote_candidates) {
1837                 ao2_ref(rtp->remote_candidates, -1);
1838         }
1839
1840 #ifdef HAVE_OPENSSL_SRTP
1841         /* Destroy the SSL context if present */
1842         if (rtp->ssl_ctx) {
1843                 SSL_CTX_free(rtp->ssl_ctx);
1844         }
1845
1846         /* Destroy the SSL session if present */
1847         if (rtp->ssl) {
1848                 SSL_free(rtp->ssl);
1849         }
1850 #endif
1851
1852         /* Destroy synchronization items */
1853         ast_mutex_destroy(&rtp->lock);
1854         ast_cond_destroy(&rtp->cond);
1855
1856         /* Finally destroy ourselves */
1857         ast_free(rtp);
1858
1859         return 0;
1860 }
1861
1862 static int ast_rtp_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
1863 {
1864         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1865         rtp->dtmfmode = dtmf_mode;
1866         return 0;
1867 }
1868
1869 static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get(struct ast_rtp_instance *instance)
1870 {
1871         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1872         return rtp->dtmfmode;
1873 }
1874
1875 static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit)
1876 {
1877         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1878         struct ast_sockaddr remote_address = { {0,} };
1879         int hdrlen = 12, res = 0, i = 0, payload = 101;
1880         char data[256];
1881         unsigned int *rtpheader = (unsigned int*)data;
1882
1883         ast_rtp_instance_get_remote_address(instance, &remote_address);
1884
1885         /* If we have no remote address information bail out now */
1886         if (ast_sockaddr_isnull(&remote_address)) {
1887                 return -1;
1888         }
1889
1890         /* Convert given digit into what we want to transmit */
1891         if ((digit <= '9') && (digit >= '0')) {
1892                 digit -= '0';
1893         } else if (digit == '*') {
1894                 digit = 10;
1895         } else if (digit == '#') {
1896                 digit = 11;
1897         } else if ((digit >= 'A') && (digit <= 'D')) {
1898                 digit = digit - 'A' + 12;
1899         } else if ((digit >= 'a') && (digit <= 'd')) {
1900                 digit = digit - 'a' + 12;
1901         } else {
1902                 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
1903                 return -1;
1904         }
1905
1906         /* Grab the payload that they expect the RFC2833 packet to be received in */
1907         payload = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance), 0, NULL, AST_RTP_DTMF);
1908
1909         rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
1910         rtp->send_duration = 160;
1911         rtp->lastdigitts = rtp->lastts + rtp->send_duration;
1912
1913         /* Create the actual packet that we will be sending */
1914         rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno));
1915         rtpheader[1] = htonl(rtp->lastdigitts);
1916         rtpheader[2] = htonl(rtp->ssrc);
1917
1918         /* Actually send the packet */
1919         for (i = 0; i < 2; i++) {
1920                 int ice;
1921
1922                 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
1923                 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
1924                 if (res < 0) {
1925                         ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
1926                                 ast_sockaddr_stringify(&remote_address),
1927                                 strerror(errno));
1928                 }
1929                 update_address_with_ice_candidate(rtp, COMPONENT_RTP, &remote_address);
1930                 if (rtp_debug_test_addr(&remote_address)) {
1931                         ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
1932                                     ast_sockaddr_stringify(&remote_address),
1933                                     ice ? " (via ICE)" : "",
1934                                     payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
1935                 }
1936                 rtp->seqno++;
1937                 rtp->send_duration += 160;
1938                 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno));
1939         }
1940
1941         /* Record that we are in the process of sending a digit and information needed to continue doing so */
1942         rtp->sending_digit = 1;
1943         rtp->send_digit = digit;
1944         rtp->send_payload = payload;
1945
1946         return 0;
1947 }
1948
1949 static int ast_rtp_dtmf_continuation(struct ast_rtp_instance *instance)
1950 {
1951         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1952         struct ast_sockaddr remote_address = { {0,} };
1953         int hdrlen = 12, res = 0;
1954         char data[256];
1955         unsigned int *rtpheader = (unsigned int*)data;
1956         int ice;
1957
1958         ast_rtp_instance_get_remote_address(instance, &remote_address);
1959
1960         /* Make sure we know where the other side is so we can send them the packet */
1961         if (ast_sockaddr_isnull(&remote_address)) {
1962                 return -1;
1963         }
1964
1965         /* Actually create the packet we will be sending */
1966         rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
1967         rtpheader[1] = htonl(rtp->lastdigitts);
1968         rtpheader[2] = htonl(rtp->ssrc);
1969         rtpheader[3] = htonl((rtp->send_digit << 24) | (0xa << 16) | (rtp->send_duration));
1970
1971         /* Boom, send it on out */
1972         res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
1973         if (res < 0) {
1974                 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
1975                         ast_sockaddr_stringify(&remote_address),
1976                         strerror(errno));
1977         }
1978
1979         update_address_with_ice_candidate(rtp, COMPONENT_RTP, &remote_address);
1980
1981         if (rtp_debug_test_addr(&remote_address)) {
1982                 ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
1983                             ast_sockaddr_stringify(&remote_address),
1984                             ice ? " (via ICE)" : "",
1985                             rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
1986         }
1987
1988         /* And now we increment some values for the next time we swing by */
1989         rtp->seqno++;
1990         rtp->send_duration += 160;
1991
1992         return 0;
1993 }
1994
1995 static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
1996 {
1997         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
1998         struct ast_sockaddr remote_address = { {0,} };
1999         int hdrlen = 12, res = 0, i = 0;
2000         char data[256];
2001         unsigned int *rtpheader = (unsigned int*)data;
2002         unsigned int measured_samples;
2003
2004         ast_rtp_instance_get_remote_address(instance, &remote_address);
2005
2006         /* Make sure we know where the remote side is so we can send them the packet we construct */
2007         if (ast_sockaddr_isnull(&remote_address)) {
2008                 return -1;
2009         }
2010
2011         /* Convert the given digit to the one we are going to send */
2012         if ((digit <= '9') && (digit >= '0')) {
2013                 digit -= '0';
2014         } else if (digit == '*') {
2015                 digit = 10;
2016         } else if (digit == '#') {
2017                 digit = 11;
2018         } else if ((digit >= 'A') && (digit <= 'D')) {
2019                 digit = digit - 'A' + 12;
2020         } else if ((digit >= 'a') && (digit <= 'd')) {
2021                 digit = digit - 'a' + 12;
2022         } else {
2023                 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
2024                 return -1;
2025         }
2026
2027         rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
2028
2029         if (duration > 0 && (measured_samples = duration * rtp_get_rate(&rtp->f.subclass.format) / 1000) > rtp->send_duration) {
2030                 ast_debug(2, "Adjusting final end duration from %u to %u\n", rtp->send_duration, measured_samples);
2031                 rtp->send_duration = measured_samples;
2032         }
2033
2034         /* Construct the packet we are going to send */
2035         rtpheader[1] = htonl(rtp->lastdigitts);
2036         rtpheader[2] = htonl(rtp->ssrc);
2037         rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
2038         rtpheader[3] |= htonl((1 << 23));
2039
2040         /* Send it 3 times, that's the magical number */
2041         for (i = 0; i < 3; i++) {
2042                 int ice;
2043
2044                 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
2045
2046                 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
2047
2048                 if (res < 0) {
2049                         ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
2050                                 ast_sockaddr_stringify(&remote_address),
2051                                 strerror(errno));
2052                 }
2053
2054                 update_address_with_ice_candidate(rtp, COMPONENT_RTP, &remote_address);
2055
2056                 if (rtp_debug_test_addr(&remote_address)) {
2057                         ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2058                                     ast_sockaddr_stringify(&remote_address),
2059                                     ice ? " (via ICE)" : "",
2060                                     rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
2061                 }
2062
2063                 rtp->seqno++;
2064         }
2065
2066         /* Oh and we can't forget to turn off the stuff that says we are sending DTMF */
2067         rtp->lastts += rtp->send_duration;
2068         rtp->sending_digit = 0;
2069         rtp->send_digit = 0;
2070
2071         return 0;
2072 }
2073
2074 static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit)
2075 {
2076         return ast_rtp_dtmf_end_with_duration(instance, digit, 0);
2077 }
2078
2079 static void ast_rtp_update_source(struct ast_rtp_instance *instance)
2080 {
2081         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2082
2083         /* We simply set this bit so that the next packet sent will have the marker bit turned on */
2084         ast_set_flag(rtp, FLAG_NEED_MARKER_BIT);
2085         ast_debug(3, "Setting the marker bit due to a source update\n");
2086
2087         return;
2088 }
2089
2090 static void ast_rtp_change_source(struct ast_rtp_instance *instance)
2091 {
2092         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2093         struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance);
2094         unsigned int ssrc = ast_random();
2095
2096         if (!rtp->lastts) {
2097                 ast_debug(3, "Not changing SSRC since we haven't sent any RTP yet\n");
2098                 return;
2099         }
2100
2101         /* We simply set this bit so that the next packet sent will have the marker bit turned on */
2102         ast_set_flag(rtp, FLAG_NEED_MARKER_BIT);
2103
2104         ast_debug(3, "Changing ssrc from %u to %u due to a source change\n", rtp->ssrc, ssrc);
2105
2106         if (srtp) {
2107                 ast_debug(3, "Changing ssrc for SRTP from %u to %u\n", rtp->ssrc, ssrc);
2108                 res_srtp->change_source(srtp, rtp->ssrc, ssrc);
2109         }
2110
2111         rtp->ssrc = ssrc;
2112
2113         return;
2114 }
2115
2116 static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
2117 {
2118         struct timeval t;
2119         long ms;
2120
2121         if (ast_tvzero(rtp->txcore)) {
2122                 rtp->txcore = ast_tvnow();
2123                 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
2124         }
2125
2126         t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow();
2127         if ((ms = ast_tvdiff_ms(t, rtp->txcore)) < 0) {
2128                 ms = 0;
2129         }
2130         rtp->txcore = t;
2131
2132         return (unsigned int) ms;
2133 }
2134
2135 static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw)
2136 {
2137         unsigned int sec, usec, frac;
2138         sec = tv.tv_sec + 2208988800u; /* Sec between 1900 and 1970 */
2139         usec = tv.tv_usec;
2140         frac = (usec << 12) + (usec << 8) - ((usec * 3650) >> 6);
2141         *msw = sec;
2142         *lsw = frac;
2143 }
2144
2145 /*! \brief Send RTCP recipient's report */
2146 static int ast_rtcp_write_rr(struct ast_rtp_instance *instance)
2147 {
2148         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2149         int res;
2150         int len = 32;
2151         unsigned int lost;
2152         unsigned int extended;
2153         unsigned int expected;
2154         unsigned int expected_interval;
2155         unsigned int received_interval;
2156         int lost_interval;
2157         struct timeval now;
2158         unsigned int *rtcpheader;
2159         char bdata[1024];
2160         struct timeval dlsr;
2161         int fraction;
2162         int rate = rtp_get_rate(&rtp->f.subclass.format);
2163         int ice;
2164         double rxlost_current;
2165         struct ast_sockaddr remote_address = { {0,} };
2166
2167         if (!rtp || !rtp->rtcp)
2168                 return 0;
2169
2170         if (ast_sockaddr_isnull(&rtp->rtcp->them)) {
2171                 /*
2172                  * RTCP was stopped.
2173                  */
2174                 return 0;
2175         }
2176
2177         extended = rtp->cycles + rtp->lastrxseqno;
2178         expected = extended - rtp->seedrxseqno + 1;
2179         lost = expected - rtp->rxcount;
2180         expected_interval = expected - rtp->rtcp->expected_prior;
2181         rtp->rtcp->expected_prior = expected;
2182         received_interval = rtp->rxcount - rtp->rtcp->received_prior;
2183         rtp->rtcp->received_prior = rtp->rxcount;
2184         lost_interval = expected_interval - received_interval;
2185
2186         if (lost_interval <= 0)
2187                 rtp->rtcp->rxlost = 0;
2188         else rtp->rtcp->rxlost = rtp->rtcp->rxlost;
2189         if (rtp->rtcp->rxlost_count == 0)
2190                 rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
2191         if (lost_interval < rtp->rtcp->minrxlost)
2192                 rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
2193         if (lost_interval > rtp->rtcp->maxrxlost)
2194                 rtp->rtcp->maxrxlost = rtp->rtcp->rxlost;
2195
2196         rxlost_current = normdev_compute(rtp->rtcp->normdev_rxlost, rtp->rtcp->rxlost, rtp->rtcp->rxlost_count);
2197         rtp->rtcp->stdev_rxlost = stddev_compute(rtp->rtcp->stdev_rxlost, rtp->rtcp->rxlost, rtp->rtcp->normdev_rxlost, rxlost_current, rtp->rtcp->rxlost_count);
2198         rtp->rtcp->normdev_rxlost = rxlost_current;
2199         rtp->rtcp->rxlost_count++;
2200
2201         if (expected_interval == 0 || lost_interval <= 0)
2202                 fraction = 0;
2203         else
2204                 fraction = (lost_interval << 8) / expected_interval;
2205         gettimeofday(&now, NULL);
2206         timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
2207         rtcpheader = (unsigned int *)bdata;
2208         rtcpheader[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_RR << 16) | ((len/4)-1));
2209         rtcpheader[1] = htonl(rtp->ssrc);
2210         rtcpheader[2] = htonl(rtp->themssrc);
2211         rtcpheader[3] = htonl(((fraction & 0xff) << 24) | (lost & 0xffffff));
2212         rtcpheader[4] = htonl((rtp->cycles) | ((rtp->lastrxseqno & 0xffff)));
2213         rtcpheader[5] = htonl((unsigned int)(rtp->rxjitter * rate));
2214         rtcpheader[6] = htonl(rtp->rtcp->themrxlsr);
2215         rtcpheader[7] = htonl((((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000);
2216
2217         /*! \note Insert SDES here. Probably should make SDES text equal to mimetypes[code].type (not subtype 'cos
2218           it can change mid call, and SDES can't) */
2219         rtcpheader[len/4]     = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
2220         rtcpheader[(len/4)+1] = htonl(rtp->ssrc);               /* Our SSRC */
2221         rtcpheader[(len/4)+2] = htonl(0x01 << 24);              /* Empty for the moment */
2222         len += 12;
2223
2224         ast_sockaddr_copy(&remote_address, &rtp->rtcp->them);
2225
2226         res = rtcp_sendto(instance, (unsigned int *)rtcpheader, len, 0, &remote_address, &ice);
2227
2228         if (res < 0) {
2229                 ast_log(LOG_ERROR, "RTCP RR transmission error, rtcp halted: %s\n",strerror(errno));
2230                 return 0;
2231         }
2232
2233         rtp->rtcp->rr_count++;
2234
2235         update_address_with_ice_candidate(rtp, COMPONENT_RTCP, &remote_address);
2236
2237         if (rtcp_debug_test_addr(&remote_address)) {
2238                 ast_verbose("\n* Sending RTCP RR to %s%s\n"
2239                         "  Our SSRC: %u\nTheir SSRC: %u\niFraction lost: %d\nCumulative loss: %u\n"
2240                         "  IA jitter: %.4f\n"
2241                         "  Their last SR: %u\n"
2242                             "  DLSR: %4.4f (sec)\n\n",
2243                             ast_sockaddr_stringify(&remote_address),
2244                             ice ? " (via ICE)" : "",
2245                             rtp->ssrc, rtp->themssrc, fraction, lost,
2246                             rtp->rxjitter,
2247                             rtp->rtcp->themrxlsr,
2248                             (double)(ntohl(rtcpheader[7])/65536.0));
2249         }
2250
2251         return res;
2252 }
2253
2254 /*! \brief Send RTCP sender's report */
2255 static int ast_rtcp_write_sr(struct ast_rtp_instance *instance)
2256 {
2257         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2258         int res;
2259         int len = 0;
2260         struct timeval now;
2261         unsigned int now_lsw;
2262         unsigned int now_msw;
2263         unsigned int *rtcpheader;
2264         unsigned int lost;
2265         unsigned int extended;
2266         unsigned int expected;
2267         unsigned int expected_interval;
2268         unsigned int received_interval;
2269         int lost_interval;
2270         int fraction;
2271         struct timeval dlsr;
2272         char bdata[512];
2273         int rate = rtp_get_rate(&rtp->f.subclass.format);
2274         int ice;
2275         struct ast_sockaddr remote_address = { {0,} };
2276
2277         if (!rtp || !rtp->rtcp)
2278                 return 0;
2279
2280         if (ast_sockaddr_isnull(&rtp->rtcp->them)) {  /* This'll stop rtcp for this rtp session */
2281                 /*
2282                  * RTCP was stopped.
2283                  */
2284                 return 0;
2285         }
2286
2287         gettimeofday(&now, NULL);
2288         timeval2ntp(now, &now_msw, &now_lsw); /* fill thses ones in from utils.c*/
2289         rtcpheader = (unsigned int *)bdata;
2290         rtcpheader[1] = htonl(rtp->ssrc);               /* Our SSRC */
2291         rtcpheader[2] = htonl(now_msw);                 /* now, MSW. gettimeofday() + SEC_BETWEEN_1900_AND_1970*/
2292         rtcpheader[3] = htonl(now_lsw);                 /* now, LSW */
2293         rtcpheader[4] = htonl(rtp->lastts);             /* FIXME shouldn't be that, it should be now */
2294         rtcpheader[5] = htonl(rtp->txcount);            /* No. packets sent */
2295         rtcpheader[6] = htonl(rtp->txoctetcount);       /* No. bytes sent */
2296         len += 28;
2297
2298         extended = rtp->cycles + rtp->lastrxseqno;
2299         expected = extended - rtp->seedrxseqno + 1;
2300         if (rtp->rxcount > expected)
2301                 expected += rtp->rxcount - expected;
2302         lost = expected - rtp->rxcount;
2303         expected_interval = expected - rtp->rtcp->expected_prior;
2304         rtp->rtcp->expected_prior = expected;
2305         received_interval = rtp->rxcount - rtp->rtcp->received_prior;
2306         rtp->rtcp->received_prior = rtp->rxcount;
2307         lost_interval = expected_interval - received_interval;
2308         if (expected_interval == 0 || lost_interval <= 0)
2309                 fraction = 0;
2310         else
2311                 fraction = (lost_interval << 8) / expected_interval;
2312         timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
2313         rtcpheader[7] = htonl(rtp->themssrc);
2314         rtcpheader[8] = htonl(((fraction & 0xff) << 24) | (lost & 0xffffff));
2315         rtcpheader[9] = htonl((rtp->cycles) | ((rtp->lastrxseqno & 0xffff)));
2316         rtcpheader[10] = htonl((unsigned int)(rtp->rxjitter * rate));
2317         rtcpheader[11] = htonl(rtp->rtcp->themrxlsr);
2318         rtcpheader[12] = htonl((((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000);
2319         len += 24;
2320
2321         rtcpheader[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SR << 16) | ((len/4)-1));
2322
2323         /* Insert SDES here. Probably should make SDES text equal to mimetypes[code].type (not subtype 'cos */
2324         /* it can change mid call, and SDES can't) */
2325         rtcpheader[len/4]     = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
2326         rtcpheader[(len/4)+1] = htonl(rtp->ssrc);               /* Our SSRC */
2327         rtcpheader[(len/4)+2] = htonl(0x01 << 24);                    /* Empty for the moment */
2328         len += 12;
2329
2330         ast_sockaddr_copy(&remote_address, &rtp->rtcp->them);
2331
2332         res = rtcp_sendto(instance, (unsigned int *)rtcpheader, len, 0, &remote_address, &ice);
2333         if (res < 0) {
2334                 ast_log(LOG_ERROR, "RTCP SR transmission error to %s, rtcp halted %s\n",
2335                         ast_sockaddr_stringify(&rtp->rtcp->them),
2336                         strerror(errno));
2337                 return 0;
2338         }
2339
2340         /* FIXME Don't need to get a new one */
2341         gettimeofday(&rtp->rtcp->txlsr, NULL);
2342         rtp->rtcp->sr_count++;
2343
2344         rtp->rtcp->lastsrtxcount = rtp->txcount;
2345
2346         update_address_with_ice_candidate(rtp, COMPONENT_RTCP, &remote_address);
2347
2348         if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
2349                 ast_verbose("* Sent RTCP SR to %s%s\n", ast_sockaddr_stringify(&remote_address), ice ? " (via ICE)" : "");
2350                 ast_verbose("  Our SSRC: %u\n", rtp->ssrc);
2351                 ast_verbose("  Sent(NTP): %u.%010u\n", (unsigned int)now.tv_sec, (unsigned int)now.tv_usec*4096);
2352                 ast_verbose("  Sent(RTP): %u\n", rtp->lastts);
2353                 ast_verbose("  Sent packets: %u\n", rtp->txcount);
2354                 ast_verbose("  Sent octets: %u\n", rtp->txoctetcount);
2355                 ast_verbose("  Report block:\n");
2356                 ast_verbose("  Fraction lost: %u\n", fraction);
2357                 ast_verbose("  Cumulative loss: %u\n", lost);
2358                 ast_verbose("  IA jitter: %.4f\n", rtp->rxjitter);
2359                 ast_verbose("  Their last SR: %u\n", rtp->rtcp->themrxlsr);
2360                 ast_verbose("  DLSR: %4.4f (sec)\n\n", (double)(ntohl(rtcpheader[12])/65536.0));
2361         }
2362         manager_event(EVENT_FLAG_REPORTING, "RTCPSent", "To: %s\r\n"
2363                                             "OurSSRC: %u\r\n"
2364                                             "SentNTP: %u.%010u\r\n"
2365                                             "SentRTP: %u\r\n"
2366                                             "SentPackets: %u\r\n"
2367                                             "SentOctets: %u\r\n"
2368                                             "ReportBlock:\r\n"
2369                                             "FractionLost: %u\r\n"
2370                                             "CumulativeLoss: %u\r\n"
2371                                             "IAJitter: %.4f\r\n"
2372                                             "TheirLastSR: %u\r\n"
2373                       "DLSR: %4.4f (sec)\r\n",
2374                       ast_sockaddr_stringify(&remote_address),
2375                       rtp->ssrc,
2376                       (unsigned int)now.tv_sec, (unsigned int)now.tv_usec*4096,
2377                       rtp->lastts,
2378                       rtp->txcount,
2379                       rtp->txoctetcount,
2380                       fraction,
2381                       lost,
2382                       rtp->rxjitter,
2383                       rtp->rtcp->themrxlsr,
2384                       (double)(ntohl(rtcpheader[12])/65536.0));
2385         return res;
2386 }
2387
2388 /*! \brief Write and RTCP packet to the far end
2389  * \note Decide if we are going to send an SR (with Reception Block) or RR
2390  * RR is sent if we have not sent any rtp packets in the previous interval */
2391 static int ast_rtcp_write(const void *data)
2392 {
2393         struct ast_rtp_instance *instance = (struct ast_rtp_instance *) data;
2394         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2395         int res;
2396
2397         if (!rtp || !rtp->rtcp || rtp->rtcp->schedid == -1) {
2398                 ao2_ref(instance, -1);
2399                 return 0;
2400         }
2401
2402         if (rtp->txcount > rtp->rtcp->lastsrtxcount) {
2403                 res = ast_rtcp_write_sr(instance);
2404         } else {
2405                 res = ast_rtcp_write_rr(instance);
2406         }
2407
2408         if (!res) {
2409                 /* 
2410                  * Not being rescheduled.
2411                  */
2412                 ao2_ref(instance, -1);
2413                 rtp->rtcp->schedid = -1;
2414         }
2415
2416         return res;
2417 }
2418
2419 static int ast_rtp_raw_write(struct ast_rtp_instance *instance, struct ast_frame *frame, int codec)
2420 {
2421         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2422         int pred, mark = 0;
2423         unsigned int ms = calc_txstamp(rtp, &frame->delivery);
2424         struct ast_sockaddr remote_address = { {0,} };
2425         int rate = rtp_get_rate(&frame->subclass.format) / 1000;
2426
2427         if (frame->subclass.format.id == AST_FORMAT_G722) {
2428                 frame->samples /= 2;
2429         }
2430
2431         if (rtp->sending_digit) {
2432                 return 0;
2433         }
2434
2435         if (frame->frametype == AST_FRAME_VOICE) {
2436                 pred = rtp->lastts + frame->samples;
2437
2438                 /* Re-calculate last TS */
2439                 rtp->lastts = rtp->lastts + ms * rate;
2440                 if (ast_tvzero(frame->delivery)) {
2441                         /* If this isn't an absolute delivery time, Check if it is close to our prediction,
2442                            and if so, go with our prediction */
2443                         if (abs(rtp->lastts - pred) < MAX_TIMESTAMP_SKEW) {
2444                                 rtp->lastts = pred;
2445                         } else {
2446                                 ast_debug(3, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
2447                                 mark = 1;
2448                         }
2449                 }
2450         } else if (frame->frametype == AST_FRAME_VIDEO) {
2451                 mark = ast_format_get_video_mark(&frame->subclass.format);
2452                 pred = rtp->lastovidtimestamp + frame->samples;
2453                 /* Re-calculate last TS */
2454                 rtp->lastts = rtp->lastts + ms * 90;
2455                 /* If it's close to our prediction, go for it */
2456                 if (ast_tvzero(frame->delivery)) {
2457                         if (abs(rtp->lastts - pred) < 7200) {
2458                                 rtp->lastts = pred;
2459                                 rtp->lastovidtimestamp += frame->samples;
2460                         } else {
2461                                 ast_debug(3, "Difference is %d, ms is %d (%d), pred/ts/samples %d/%d/%d\n", abs(rtp->lastts - pred), ms, ms * 90, rtp->lastts, pred, frame->samples);
2462                                 rtp->lastovidtimestamp = rtp->lastts;
2463                         }
2464                 }
2465         } else {
2466                 pred = rtp->lastotexttimestamp + frame->samples;
2467                 /* Re-calculate last TS */
2468                 rtp->lastts = rtp->lastts + ms;
2469                 /* If it's close to our prediction, go for it */
2470                 if (ast_tvzero(frame->delivery)) {
2471                         if (abs(rtp->lastts - pred) < 7200) {
2472                                 rtp->lastts = pred;
2473                                 rtp->lastotexttimestamp += frame->samples;
2474                         } else {
2475                                 ast_debug(3, "Difference is %d, ms is %d, pred/ts/samples %d/%d/%d\n", abs(rtp->lastts - pred), ms, rtp->lastts, pred, frame->samples);
2476                                 rtp->lastotexttimestamp = rtp->lastts;
2477                         }
2478                 }
2479         }
2480
2481         /* If we have been explicitly told to set the marker bit then do so */
2482         if (ast_test_flag(rtp, FLAG_NEED_MARKER_BIT)) {
2483                 mark = 1;
2484                 ast_clear_flag(rtp, FLAG_NEED_MARKER_BIT);
2485         }
2486
2487         /* If the timestamp for non-digt packets has moved beyond the timestamp for digits, update the digit timestamp */
2488         if (rtp->lastts > rtp->lastdigitts) {
2489                 rtp->lastdigitts = rtp->lastts;
2490         }
2491
2492         if (ast_test_flag(frame, AST_FRFLAG_HAS_TIMING_INFO)) {
2493                 rtp->lastts = frame->ts * rate;
2494         }
2495
2496         ast_rtp_instance_get_remote_address(instance, &remote_address);
2497
2498         /* If we know the remote address construct a packet and send it out */
2499         if (!ast_sockaddr_isnull(&remote_address)) {
2500                 int hdrlen = 12, res, ice;
2501                 unsigned char *rtpheader = (unsigned char *)(frame->data.ptr - hdrlen);
2502
2503                 put_unaligned_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (rtp->seqno) | (mark << 23)));
2504                 put_unaligned_uint32(rtpheader + 4, htonl(rtp->lastts));
2505                 put_unaligned_uint32(rtpheader + 8, htonl(rtp->ssrc));
2506
2507                 if ((res = rtp_sendto(instance, (void *)rtpheader, frame->datalen + hdrlen, 0, &remote_address, &ice)) < 0) {
2508                         if (!ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_NAT) || (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_NAT) && (ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_ACTIVE))) {
2509                                 ast_debug(1, "RTP Transmission error of packet %d to %s: %s\n",
2510                                           rtp->seqno,
2511                                           ast_sockaddr_stringify(&remote_address),
2512                                           strerror(errno));
2513                         } else if (((ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(rtp, FLAG_NAT_INACTIVE_NOWARN)) {
2514                                 /* Only give this error message once if we are not RTP debugging */
2515                                 if (rtpdebug)
2516                                         ast_debug(0, "RTP NAT: Can't write RTP to private address %s, waiting for other end to send audio...\n",
2517                                                   ast_sockaddr_stringify(&remote_address));
2518                                 ast_set_flag(rtp, FLAG_NAT_INACTIVE_NOWARN);
2519                         }
2520                 } else {
2521                         rtp->txcount++;
2522                         rtp->txoctetcount += (res - hdrlen);
2523
2524                         if (rtp->rtcp && rtp->rtcp->schedid < 1) {
2525                                 ast_debug(1, "Starting RTCP transmission on RTP instance '%p'\n", instance);
2526                                 ao2_ref(instance, +1);
2527                                 rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, instance);
2528                                 if (rtp->rtcp->schedid < 0) {
2529                                         ao2_ref(instance, -1);
2530                                         ast_log(LOG_WARNING, "scheduling RTCP transmission failed.\n");
2531                                 }
2532                         }
2533                 }
2534
2535                 update_address_with_ice_candidate(rtp, COMPONENT_RTP, &remote_address);
2536
2537                 if (rtp_debug_test_addr(&remote_address)) {
2538                         ast_verbose("Sent RTP packet to      %s%s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2539                                     ast_sockaddr_stringify(&remote_address),
2540                                     ice ? " (via ICE)" : "",
2541                                     codec, rtp->seqno, rtp->lastts, res - hdrlen);
2542                 }
2543         }
2544
2545         rtp->seqno++;
2546
2547         return 0;
2548 }
2549
2550 static struct ast_frame *red_t140_to_red(struct rtp_red *red) {
2551         unsigned char *data = red->t140red.data.ptr;
2552         int len = 0;
2553         int i;
2554
2555         /* replace most aged generation */
2556         if (red->len[0]) {
2557                 for (i = 1; i < red->num_gen+1; i++)
2558                         len += red->len[i];
2559
2560                 memmove(&data[red->hdrlen], &data[red->hdrlen+red->len[0]], len);
2561         }
2562
2563         /* Store length of each generation and primary data length*/
2564         for (i = 0; i < red->num_gen; i++)
2565                 red->len[i] = red->len[i+1];
2566         red->len[i] = red->t140.datalen;
2567
2568         /* write each generation length in red header */
2569         len = red->hdrlen;
2570         for (i = 0; i < red->num_gen; i++)
2571                 len += data[i*4+3] = red->len[i];
2572
2573         /* add primary data to buffer */
2574         memcpy(&data[len], red->t140.data.ptr, red->t140.datalen);
2575         red->t140red.datalen = len + red->t140.datalen;
2576
2577         /* no primary data and no generations to send */
2578         if (len == red->hdrlen && !red->t140.datalen)
2579                 return NULL;
2580
2581         /* reset t.140 buffer */
2582         red->t140.datalen = 0;
2583
2584         return &red->t140red;
2585 }
2586
2587 static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
2588 {
2589         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2590         struct ast_sockaddr remote_address = { {0,} };
2591         struct ast_format subclass;
2592         int codec;
2593
2594         ast_rtp_instance_get_remote_address(instance, &remote_address);
2595
2596         /* If we don't actually know the remote address don't even bother doing anything */
2597         if (ast_sockaddr_isnull(&remote_address)) {
2598                 ast_debug(1, "No remote address on RTP instance '%p' so dropping frame\n", instance);
2599                 return 0;
2600         }
2601
2602         /* If there is no data length we can't very well send the packet */
2603         if (!frame->datalen) {
2604                 ast_debug(1, "Received frame with no data for RTP instance '%p' so dropping frame\n", instance);
2605                 return 0;
2606         }
2607
2608         /* If the packet is not one our RTP stack supports bail out */
2609         if (frame->frametype != AST_FRAME_VOICE && frame->frametype != AST_FRAME_VIDEO && frame->frametype != AST_FRAME_TEXT) {
2610                 ast_log(LOG_WARNING, "RTP can only send voice, video, and text\n");
2611                 return -1;
2612         }
2613
2614         if (rtp->red) {
2615                 /* return 0; */
2616                 /* no primary data or generations to send */
2617                 if ((frame = red_t140_to_red(rtp->red)) == NULL)
2618                         return 0;
2619         }
2620
2621         /* Grab the subclass and look up the payload we are going to use */
2622         ast_format_copy(&subclass, &frame->subclass.format);
2623         if ((codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance), 1, &subclass, 0)) < 0) {
2624                 ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(&frame->subclass.format));
2625                 return -1;
2626         }
2627
2628         /* Oh dear, if the format changed we will have to set up a new smoother */
2629         if (ast_format_cmp(&rtp->lasttxformat, &subclass) == AST_FORMAT_CMP_NOT_EQUAL) {
2630                 ast_debug(1, "Ooh, format changed from %s to %s\n", ast_getformatname(&rtp->lasttxformat), ast_getformatname(&subclass));
2631                 rtp->lasttxformat = subclass;
2632                 ast_format_copy(&rtp->lasttxformat, &subclass);
2633                 if (rtp->smoother) {
2634                         ast_smoother_free(rtp->smoother);
2635                         rtp->smoother = NULL;
2636                 }
2637         }
2638
2639         /* If no smoother is present see if we have to set one up */
2640         if (!rtp->smoother) {
2641                 struct ast_format_list fmt = ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance)->pref, &subclass);
2642
2643                 switch (subclass.id) {
2644                 case AST_FORMAT_SPEEX:
2645                 case AST_FORMAT_SPEEX16:
2646                 case AST_FORMAT_SPEEX32:
2647                 case AST_FORMAT_SILK:
2648                 case AST_FORMAT_CELT:
2649                 case AST_FORMAT_G723_1:
2650                 case AST_FORMAT_SIREN7:
2651                 case AST_FORMAT_SIREN14:
2652                 case AST_FORMAT_G719:
2653                         /* these are all frame-based codecs and cannot be safely run through
2654                            a smoother */
2655                         break;
2656                 default:
2657                         if (fmt.inc_ms) {
2658                                 if (!(rtp->smoother = ast_smoother_new((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms))) {
2659                                         ast_log(LOG_WARNING, "Unable to create smoother: format %s ms: %d len: %d\n", ast_getformatname(&subclass), fmt.cur_ms, ((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms));
2660                                         return -1;
2661                                 }
2662                                 if (fmt.flags) {
2663                                         ast_smoother_set_flags(rtp->smoother, fmt.flags);
2664                                 }
2665                                 ast_debug(1, "Created smoother: format: %s ms: %d len: %d\n", ast_getformatname(&subclass), fmt.cur_ms, ((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms));
2666                         }
2667                 }
2668         }
2669
2670         /* Feed audio frames into the actual function that will create a frame and send it */
2671         if (rtp->smoother) {
2672                 struct ast_frame *f;
2673
2674                 if (ast_smoother_test_flag(rtp->smoother, AST_SMOOTHER_FLAG_BE)) {
2675                         ast_smoother_feed_be(rtp->smoother, frame);
2676                 } else {
2677                         ast_smoother_feed(rtp->smoother, frame);
2678                 }
2679
2680                 while ((f = ast_smoother_read(rtp->smoother)) && (f->data.ptr)) {
2681                                 ast_rtp_raw_write(instance, f, codec);
2682                 }
2683         } else {
2684                 int hdrlen = 12;
2685                 struct ast_frame *f = NULL;
2686
2687                 if (frame->offset < hdrlen) {
2688                         f = ast_frdup(frame);
2689                 } else {
2690                         f = frame;
2691                 }
2692                 if (f->data.ptr) {
2693                         ast_rtp_raw_write(instance, f, codec);
2694                 }
2695                 if (f != frame) {
2696                         ast_frfree(f);
2697                 }
2698
2699         }
2700
2701         return 0;
2702 }
2703
2704 static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp, int mark)
2705 {
2706         struct timeval now;
2707         struct timeval tmp;
2708         double transit;
2709         double current_time;
2710         double d;
2711         double dtv;
2712         double prog;
2713         int rate = rtp_get_rate(&rtp->f.subclass.format);
2714
2715         double normdev_rxjitter_current;
2716         if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
2717                 gettimeofday(&rtp->rxcore, NULL);
2718                 rtp->drxcore = (double) rtp->rxcore.tv_sec + (double) rtp->rxcore.tv_usec / 1000000;
2719                 /* map timestamp to a real time */
2720                 rtp->seedrxts = timestamp; /* Their RTP timestamp started with this */
2721                 tmp = ast_samp2tv(timestamp, rate);
2722                 rtp->rxcore = ast_tvsub(rtp->rxcore, tmp);
2723                 /* Round to 0.1ms for nice, pretty timestamps */
2724                 rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 100;
2725         }
2726
2727         gettimeofday(&now,NULL);
2728         /* rxcore is the mapping between the RTP timestamp and _our_ real time from gettimeofday() */
2729         tmp = ast_samp2tv(timestamp, rate);
2730         *tv = ast_tvadd(rtp->rxcore, tmp);
2731
2732         prog = (double)((timestamp-rtp->seedrxts)/(float)(rate));
2733         dtv = (double)rtp->drxcore + (double)(prog);
2734         current_time = (double)now.tv_sec + (double)now.tv_usec/1000000;
2735         transit = current_time - dtv;
2736         d = transit - rtp->rxtransit;
2737         rtp->rxtransit = transit;
2738         if (d<0)
2739                 d=-d;
2740         rtp->rxjitter += (1./16.) * (d - rtp->rxjitter);
2741
2742         if (rtp->rtcp) {
2743                 if (rtp->rxjitter > rtp->rtcp->maxrxjitter)
2744                         rtp->rtcp->maxrxjitter = rtp->rxjitter;
2745                 if (rtp->rtcp->rxjitter_count == 1)
2746                         rtp->rtcp->minrxjitter = rtp->rxjitter;
2747                 if (rtp->rtcp && rtp->rxjitter < rtp->rtcp->minrxjitter)
2748                         rtp->rtcp->minrxjitter = rtp->rxjitter;
2749
2750                 normdev_rxjitter_current = normdev_compute(rtp->rtcp->normdev_rxjitter,rtp->rxjitter,rtp->rtcp->rxjitter_count);
2751                 rtp->rtcp->stdev_rxjitter = stddev_compute(rtp->rtcp->stdev_rxjitter,rtp->rxjitter,rtp->rtcp->normdev_rxjitter,normdev_rxjitter_current,rtp->rtcp->rxjitter_count);
2752
2753                 rtp->rtcp->normdev_rxjitter = normdev_rxjitter_current;
2754                 rtp->rtcp->rxjitter_count++;
2755         }
2756 }
2757
2758 static struct ast_frame *create_dtmf_frame(struct ast_rtp_instance *instance, enum ast_frame_type type, int compensate)
2759 {
2760         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2761         struct ast_sockaddr remote_address = { {0,} };
2762
2763         ast_rtp_instance_get_remote_address(instance, &remote_address);
2764
2765         if (((compensate && type == AST_FRAME_DTMF_END) || (type == AST_FRAME_DTMF_BEGIN)) && ast_tvcmp(ast_tvnow(), rtp->dtmfmute) < 0) {
2766                 ast_debug(1, "Ignore potential DTMF echo from '%s'\n",
2767                           ast_sockaddr_stringify(&remote_address));
2768                 rtp->resp = 0;
2769                 rtp->dtmfsamples = 0;
2770                 return &ast_null_frame;
2771         }
2772         ast_debug(1, "Creating %s DTMF Frame: %d (%c), at %s\n",
2773                 type == AST_FRAME_DTMF_END ? "END" : "BEGIN",
2774                 rtp->resp, rtp->resp,
2775                 ast_sockaddr_stringify(&remote_address));
2776         if (rtp->resp == 'X') {
2777                 rtp->f.frametype = AST_FRAME_CONTROL;
2778                 rtp->f.subclass.integer = AST_CONTROL_FLASH;
2779         } else {
2780                 rtp->f.frametype = type;
2781                 rtp->f.subclass.integer = rtp->resp;
2782         }
2783         rtp->f.datalen = 0;
2784         rtp->f.samples = 0;
2785         rtp->f.mallocd = 0;
2786         rtp->f.src = "RTP";
2787         AST_LIST_NEXT(&rtp->f, frame_list) = NULL;
2788
2789         return &rtp->f;
2790 }
2791
2792 static void process_dtmf_rfc2833(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, struct ast_sockaddr *addr, int payloadtype, int mark, struct frame_list *frames)
2793 {
2794         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2795         struct ast_sockaddr remote_address = { {0,} };
2796         unsigned int event, event_end, samples;
2797         char resp = 0;
2798         struct ast_frame *f = NULL;
2799
2800         ast_rtp_instance_get_remote_address(instance, &remote_address);
2801
2802         /* Figure out event, event end, and samples */
2803         event = ntohl(*((unsigned int *)(data)));
2804         event >>= 24;
2805         event_end = ntohl(*((unsigned int *)(data)));
2806         event_end <<= 8;
2807         event_end >>= 24;
2808         samples = ntohl(*((unsigned int *)(data)));
2809         samples &= 0xFFFF;
2810
2811         if (rtp_debug_test_addr(&remote_address)) {
2812                 ast_verbose("Got  RTP RFC2833 from   %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u, mark %d, event %08x, end %d, duration %-5.5d) \n",
2813                             ast_sockaddr_stringify(&remote_address),
2814                             payloadtype, seqno, timestamp, len, (mark?1:0), event, ((event_end & 0x80)?1:0), samples);
2815         }
2816
2817         /* Print out debug if turned on */
2818         if (rtpdebug)
2819                 ast_debug(0, "- RTP 2833 Event: %08x (len = %d)\n", event, len);
2820
2821         /* Figure out what digit was pressed */
2822         if (event < 10) {
2823                 resp = '0' + event;
2824         } else if (event < 11) {
2825                 resp = '*';
2826         } else if (event < 12) {
2827                 resp = '#';
2828         } else if (event < 16) {
2829                 resp = 'A' + (event - 12);
2830         } else if (event < 17) {        /* Event 16: Hook flash */
2831                 resp = 'X';
2832         } else {
2833                 /* Not a supported event */
2834                 ast_debug(1, "Ignoring RTP 2833 Event: %08x. Not a DTMF Digit.\n", event);
2835                 return;
2836         }
2837
2838         if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)) {
2839                 if ((rtp->last_end_timestamp != timestamp) || (rtp->resp && rtp->resp != resp)) {
2840                         rtp->resp = resp;
2841                         rtp->dtmf_timeout = 0;
2842                         f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)));
2843                         f->len = 0;
2844                         rtp->last_end_timestamp = timestamp;
2845                         AST_LIST_INSERT_TAIL(frames, f, frame_list);
2846                 }
2847         } else {
2848                 /*  The duration parameter measures the complete
2849                     duration of the event (from the beginning) - RFC2833.
2850                     Account for the fact that duration is only 16 bits long
2851                     (about 8 seconds at 8000 Hz) and can wrap is digit
2852                     is hold for too long. */
2853                 unsigned int new_duration = rtp->dtmf_duration;
2854                 unsigned int last_duration = new_duration & 0xFFFF;
2855
2856                 if (last_duration > 64000 && samples < last_duration) {
2857                         new_duration += 0xFFFF + 1;
2858                 }
2859                 new_duration = (new_duration & ~0xFFFF) | samples;
2860
2861                 if (event_end & 0x80) {
2862                         /* End event */
2863                         if ((rtp->last_seqno != seqno) && (timestamp > rtp->last_end_timestamp)) {
2864                                 rtp->last_end_timestamp = timestamp;
2865                                 rtp->dtmf_duration = new_duration;
2866                                 rtp->resp = resp;
2867                                 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0));
2868                                 f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(&f->subclass.format)), ast_tv(0, 0));
2869                                 rtp->resp = 0;
2870                                 rtp->dtmf_duration = rtp->dtmf_timeout = 0;
2871                                 AST_LIST_INSERT_TAIL(frames, f, frame_list);
2872                         } else if (rtpdebug) {
2873                                 ast_debug(1, "Dropping duplicate or out of order DTMF END frame (seqno: %d, ts %d, digit %c)\n",
2874                                         seqno, timestamp, resp);
2875                         }
2876                 } else {
2877                         /* Begin/continuation */
2878
2879                         /* The second portion of the seqno check is to not mistakenly
2880                          * stop accepting DTMF if the seqno rolls over beyond
2881                          * 65535.
2882                          */
2883                         if ((rtp->last_seqno > seqno && rtp->last_seqno - seqno < 50)
2884                                 || timestamp <= rtp->last_end_timestamp) {
2885                                 /* Out of order frame. Processing this can cause us to
2886                                  * improperly duplicate incoming DTMF, so just drop
2887                                  * this.
2888                                  */
2889                                 if (rtpdebug) {
2890                                         ast_debug(1, "Dropping out of order DTMF frame (seqno %d, ts %d, digit %c)\n",
2891                                                 seqno, timestamp, resp);
2892                                 }
2893                                 return;
2894                         }
2895
2896                         if (rtp->resp && rtp->resp != resp) {
2897                                 /* Another digit already began. End it */
2898                                 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0));
2899                                 f->len = ast_tvdiff_ms(ast_samp2tv(rtp->dtmf_duration, rtp_get_rate(&f->subclass.format)), ast_tv(0, 0));
2900                                 rtp->resp = 0;
2901                                 rtp->dtmf_duration = rtp->dtmf_timeout = 0;
2902                                 AST_LIST_INSERT_TAIL(frames, f, frame_list);
2903                         }
2904
2905                         if (rtp->resp) {
2906                                 /* Digit continues */
2907                                 rtp->dtmf_duration = new_duration;
2908                         } else {
2909                                 /* New digit began */
2910                                 rtp->resp = resp;
2911                                 f = ast_frdup(create_dtmf_frame(instance, AST_FRAME_DTMF_BEGIN, 0));
2912                                 rtp->dtmf_duration = samples;
2913                                 AST_LIST_INSERT_TAIL(frames, f, frame_list);
2914                         }
2915
2916                         rtp->dtmf_timeout = timestamp + rtp->dtmf_duration + dtmftimeout;
2917                 }
2918
2919                 rtp->last_seqno = seqno;
2920         }
2921
2922         rtp->dtmfsamples = samples;
2923
2924         return;
2925 }
2926
2927 static struct ast_frame *process_dtmf_cisco(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, struct ast_sockaddr *addr, int payloadtype, int mark)
2928 {
2929         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
2930         unsigned int event, flags, power;
2931         char resp = 0;
2932         unsigned char seq;
2933         struct ast_frame *f = NULL;
2934
2935         if (len < 4) {
2936                 return NULL;
2937         }
2938
2939         /*      The format of Cisco RTP DTMF packet looks like next:
2940                 +0                              - sequence number of DTMF RTP packet (begins from 1,
2941                                                   wrapped to 0)
2942                 +1                              - set of flags
2943                 +1 (bit 0)              - flaps by different DTMF digits delimited by audio
2944                                                   or repeated digit without audio???
2945                 +2 (+4,+6,...)  - power level? (rises from 0 to 32 at begin of tone
2946                                                   then falls to 0 at its end)
2947                 +3 (+5,+7,...)  - detected DTMF digit (0..9,*,#,A-D,...)
2948                 Repeated DTMF information (bytes 4/5, 6/7) is history shifted right
2949                 by each new packet and thus provides some redudancy.
2950
2951                 Sample of Cisco RTP DTMF packet is (all data in hex):
2952                         19 07 00 02 12 02 20 02
2953                 showing end of DTMF digit '2'.
2954
2955                 The packets
2956                         27 07 00 02 0A 02 20 02
2957                         28 06 20 02 00 02 0A 02
2958                 shows begin of new digit '2' with very short pause (20 ms) after
2959                 previous digit '2'. Bit +1.0 flips at begin of new digit.
2960
2961                 Cisco RTP DTMF packets comes as replacement of audio RTP packets
2962                 so its uses the same sequencing and timestamping rules as replaced
2963                 audio packets. Repeat interval of DTMF packets is 20 ms and not rely
2964                 on audio framing parameters. Marker bit isn't used within stream of
2965                 DTMFs nor audio stream coming immediately after DTMF stream. Timestamps
2966                 are not sequential at borders between DTMF and audio streams,
2967         */
2968
2969         seq = data[0];
2970         flags = data[1];
2971         power = data[2];
2972         event = data[3] & 0x1f;
2973
2974         if (rtpdebug)
2975                 ast_debug(0, "Cisco DTMF Digit: %02x (len=%d, seq=%d, flags=%02x, power=%d, history count=%d)\n", event, len, seq, flags, power, (len - 4) / 2);
2976         if (event < 10) {
2977                 resp = '0' + event;
2978         } else if (event < 11) {
2979                 resp = '*';
2980         } else if (event < 12) {
2981                 resp = '#';
2982         } else if (event < 16) {
2983                 resp = 'A' + (event - 12);
2984         } else if (event < 17) {
2985                 resp = 'X';
2986         }
2987         if ((!rtp->resp && power) || (rtp->resp && (rtp->resp != resp))) {
2988                 rtp->resp = resp;
2989                 /* Why we should care on DTMF compensation at reception? */
2990                 if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE)) {
2991                         f = create_dtmf_frame(instance, AST_FRAME_DTMF_BEGIN, 0);
2992                         rtp->dtmfsamples = 0;
2993                 }
2994         } else if ((rtp->resp == resp) && !power) {
2995                 f = create_dtmf_frame(instance, AST_FRAME_DTMF_END, ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_DTMF_COMPENSATE));
2996                 f->samples = rtp->dtmfsamples * (rtp->lastrxformat.id ? (rtp_get_rate(&rtp->lastrxformat) / 1000) : 8);
2997                 rtp->resp = 0;
2998         } else if (rtp->resp == resp)
2999                 rtp->dtmfsamples += 20 * (rtp->lastrxformat.id ? (rtp_get_rate(&rtp->lastrxformat) / 1000) : 8);
3000
3001         rtp->dtmf_timeout = 0;
3002
3003         return f;
3004 }
3005
3006 static struct ast_frame *process_cn_rfc3389(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, struct ast_sockaddr *addr, int payloadtype, int mark)
3007 {
3008         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3009
3010         /* Convert comfort noise into audio with various codecs.  Unfortunately this doesn't
3011            totally help us out becuase we don't have an engine to keep it going and we are not
3012            guaranteed to have it every 20ms or anything */
3013         if (rtpdebug)
3014                 ast_debug(0, "- RTP 3389 Comfort noise event: Level %d (len = %d)\n", (int) rtp->lastrxformat.id, len);
3015
3016         if (ast_test_flag(rtp, FLAG_3389_WARNING)) {
3017                 struct ast_sockaddr remote_address = { {0,} };
3018
3019                 ast_rtp_instance_get_remote_address(instance, &remote_address);
3020
3021                 ast_log(LOG_NOTICE, "Comfort noise support incomplete in Asterisk (RFC 3389). Please turn off on client if possible. Client address: %s\n",
3022                         ast_sockaddr_stringify(&remote_address));
3023                 ast_set_flag(rtp, FLAG_3389_WARNING);
3024         }
3025
3026         /* Must have at least one byte */
3027         if (!len)
3028                 return NULL;
3029         if (len < 24) {
3030                 rtp->f.data.ptr = rtp->rawdata + AST_FRIENDLY_OFFSET;
3031                 rtp->f.datalen = len - 1;
3032                 rtp->f.offset = AST_FRIENDLY_OFFSET;
3033                 memcpy(rtp->f.data.ptr, data + 1, len - 1);
3034         } else {
3035                 rtp->f.data.ptr = NULL;
3036                 rtp->f.offset = 0;
3037                 rtp->f.datalen = 0;
3038         }
3039         rtp->f.frametype = AST_FRAME_CNG;
3040         rtp->f.subclass.integer = data[0] & 0x7f;
3041         rtp->f.samples = 0;
3042         rtp->f.delivery.tv_usec = rtp->f.delivery.tv_sec = 0;
3043
3044         return &rtp->f;
3045 }
3046
3047 static struct ast_frame *ast_rtcp_read(struct ast_rtp_instance *instance)
3048 {
3049         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3050         struct ast_sockaddr addr;
3051         unsigned char rtcpdata[8192 + AST_FRIENDLY_OFFSET];
3052         unsigned int *rtcpheader = (unsigned int *)(rtcpdata + AST_FRIENDLY_OFFSET);
3053         int res, packetwords, position = 0;
3054         struct ast_frame *f = &ast_null_frame;
3055
3056         /* Read in RTCP data from the socket */
3057         if ((res = rtcp_recvfrom(instance, rtcpdata + AST_FRIENDLY_OFFSET,
3058                                 sizeof(rtcpdata) - AST_FRIENDLY_OFFSET,
3059                                 0, &addr)) < 0) {
3060                 ast_assert(errno != EBADF);
3061                 if (errno != EAGAIN) {
3062                         ast_log(LOG_WARNING, "RTCP Read error: %s.  Hanging up.\n",
3063                                 (errno) ? strerror(errno) : "Unspecified");
3064                         return NULL;
3065                 }
3066                 return &ast_null_frame;
3067         }
3068
3069         /* If this was handled by the ICE session don't do anything further */
3070         if (!res) {
3071                 return &ast_null_frame;
3072         }
3073
3074         if (!*(rtcpdata + AST_FRIENDLY_OFFSET)) {
3075                 struct sockaddr_in addr_tmp;
3076                 struct ast_sockaddr addr_v4;
3077
3078                 if (ast_sockaddr_is_ipv4(&addr)) {
3079                         ast_sockaddr_to_sin(&addr, &addr_tmp);
3080                 } else if (ast_sockaddr_ipv4_mapped(&addr, &addr_v4)) {
3081                         ast_debug(1, "Using IPv6 mapped address %s for STUN\n",
3082                                   ast_sockaddr_stringify(&addr));
3083                         ast_sockaddr_to_sin(&addr_v4, &addr_tmp);
3084                 } else {
3085                         ast_debug(1, "Cannot do STUN for non IPv4 address %s\n",
3086                                   ast_sockaddr_stringify(&addr));
3087                         return &ast_null_frame;
3088                 }
3089                 if ((ast_stun_handle_packet(rtp->rtcp->s, &addr_tmp, rtcpdata + AST_FRIENDLY_OFFSET, res, NULL, NULL) == AST_STUN_ACCEPT)) {
3090                         ast_sockaddr_from_sin(&addr, &addr_tmp);
3091                         ast_sockaddr_copy(&rtp->rtcp->them, &addr);
3092                 }
3093                 return &ast_null_frame;
3094         }
3095
3096         packetwords = res / 4;
3097
3098         if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_NAT)) {
3099                 /* Send to whoever sent to us */
3100                 if (ast_sockaddr_cmp(&rtp->rtcp->them, &addr)) {
3101                         ast_sockaddr_copy(&rtp->rtcp->them, &addr);
3102                         if (rtpdebug)
3103                                 ast_debug(0, "RTCP NAT: Got RTCP from other end. Now sending to address %s\n",
3104                                           ast_sockaddr_stringify(&rtp->rtcp->them));
3105                 }
3106         }
3107
3108         ast_debug(1, "Got RTCP report of %d bytes\n", res);
3109
3110         while (position < packetwords) {
3111                 int i, pt, rc;
3112                 unsigned int length, dlsr, lsr, msw, lsw, comp;
3113                 struct timeval now;
3114                 double rttsec, reported_jitter, reported_normdev_jitter_current, normdevrtt_current, reported_lost, reported_normdev_lost_current;
3115                 uint64_t rtt = 0;
3116
3117                 i = position;
3118                 length = ntohl(rtcpheader[i]);
3119                 pt = (length & 0xff0000) >> 16;
3120                 rc = (length & 0x1f000000) >> 24;
3121                 length &= 0xffff;
3122
3123                 if ((i + length) > packetwords) {
3124                         if (rtpdebug)
3125                                 ast_debug(1, "RTCP Read too short\n");
3126                         return &ast_null_frame;
3127                 }
3128
3129                 if (rtcp_debug_test_addr(&addr)) {
3130                         ast_verbose("\n\nGot RTCP from %s\n",
3131                                     ast_sockaddr_stringify(&addr));
3132                         ast_verbose("PT: %d(%s)\n", pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown");
3133                         ast_verbose("Reception reports: %d\n", rc);
3134                         ast_verbose("SSRC of sender: %u\n", rtcpheader[i + 1]);
3135                 }
3136
3137                 i += 2; /* Advance past header and ssrc */
3138                 if (rc == 0 && pt == RTCP_PT_RR) {      /* We're receiving a receiver report with no reports, which is ok */
3139                         position += (length + 1);
3140                         continue;
3141                 }
3142
3143                 switch (pt) {
3144                 case RTCP_PT_SR:
3145                         gettimeofday(&rtp->rtcp->rxlsr,NULL); /* To be able to populate the dlsr */
3146                         rtp->rtcp->spc = ntohl(rtcpheader[i+3]);
3147                         rtp->rtcp->soc = ntohl(rtcpheader[i + 4]);
3148                         rtp->rtcp->themrxlsr = ((ntohl(rtcpheader[i]) & 0x0000ffff) << 16) | ((ntohl(rtcpheader[i + 1]) & 0xffff0000) >> 16); /* Going to LSR in RR*/
3149
3150                         if (rtcp_debug_test_addr(&addr)) {
3151                                 ast_verbose("NTP timestamp: %lu.%010lu\n", (unsigned long) ntohl(rtcpheader[i]), (unsigned long) ntohl(rtcpheader[i + 1]) * 4096);
3152                                 ast_verbose("RTP timestamp: %lu\n", (unsigned long) ntohl(rtcpheader[i + 2]));
3153                                 ast_verbose("SPC: %lu\tSOC: %lu\n", (unsigned long) ntohl(rtcpheader[i + 3]), (unsigned long) ntohl(rtcpheader[i + 4]));
3154                         }
3155                         i += 5;
3156                         if (rc < 1)
3157                                 break;
3158                         /* Intentional fall through */
3159                 case RTCP_PT_RR:
3160                         /* Don't handle multiple reception reports (rc > 1) yet */
3161                         /* Calculate RTT per RFC */
3162                         gettimeofday(&now, NULL);
3163                         timeval2ntp(now, &msw, &lsw);
3164                         if (ntohl(rtcpheader[i + 4]) && ntohl(rtcpheader[i + 5])) { /* We must have the LSR && DLSR */
3165                                 comp = ((msw & 0xffff) << 16) | ((lsw & 0xffff0000) >> 16);
3166                                 lsr = ntohl(rtcpheader[i + 4]);
3167                                 dlsr = ntohl(rtcpheader[i + 5]);
3168                                 rtt = comp - lsr - dlsr;
3169
3170                                 /* Convert end to end delay to usec (keeping the calculation in 64bit space)
3171                                    sess->ee_delay = (eedelay * 1000) / 65536; */
3172                                 if (rtt < 4294) {
3173                                         rtt = (rtt * 1000000) >> 16;
3174                                 } else {
3175                                         rtt = (rtt * 1000) >> 16;
3176                                         rtt *= 1000;
3177                                 }
3178                                 rtt = rtt / 1000.;
3179                                 rttsec = rtt / 1000.;
3180                                 rtp->rtcp->rtt = rttsec;
3181
3182                                 if (comp - dlsr >= lsr) {
3183                                         rtp->rtcp->accumulated_transit += rttsec;
3184
3185                                         if (rtp->rtcp->rtt_count == 0)
3186                                                 rtp->rtcp->minrtt = rttsec;
3187
3188                                         if (rtp->rtcp->maxrtt<rttsec)
3189                                                 rtp->rtcp->maxrtt = rttsec;
3190                                         if (rtp->rtcp->minrtt>rttsec)
3191                                                 rtp->rtcp->minrtt = rttsec;
3192
3193                                         normdevrtt_current = normdev_compute(rtp->rtcp->normdevrtt, rttsec, rtp->rtcp->rtt_count);
3194
3195                                         rtp->rtcp->stdevrtt = stddev_compute(rtp->rtcp->stdevrtt, rttsec, rtp->rtcp->normdevrtt, normdevrtt_current, rtp->rtcp->rtt_count);
3196
3197                                         rtp->rtcp->normdevrtt = normdevrtt_current;
3198
3199                                         rtp->rtcp->rtt_count++;
3200                                 } else if (rtcp_debug_test_addr(&addr)) {
3201                                         ast_verbose("Internal RTCP NTP clock skew detected: "
3202                                                            "lsr=%u, now=%u, dlsr=%u (%d:%03dms), "
3203                                                     "diff=%d\n",
3204                                                     lsr, comp, dlsr, dlsr / 65536,
3205                                                     (dlsr % 65536) * 1000 / 65536,
3206                                                     dlsr - (comp - lsr));
3207                                 }
3208                         }
3209
3210                         rtp->rtcp->reported_jitter = ntohl(rtcpheader[i + 3]);
3211                         reported_jitter = (double) rtp->rtcp->reported_jitter;
3212
3213                         if (rtp->rtcp->reported_jitter_count == 0)
3214                                 rtp->rtcp->reported_minjitter = reported_jitter;
3215
3216                         if (reported_jitter < rtp->rtcp->reported_minjitter)
3217                                 rtp->rtcp->reported_minjitter = reported_jitter;
3218
3219                         if (reported_jitter > rtp->rtcp->reported_maxjitter)
3220                                 rtp->rtcp->reported_maxjitter = reported_jitter;
3221
3222                         reported_normdev_jitter_current = normdev_compute(rtp->rtcp->reported_normdev_jitter, reported_jitter, rtp->rtcp->reported_jitter_count);
3223
3224                         rtp->rtcp->reported_stdev_jitter = stddev_compute(rtp->rtcp->reported_stdev_jitter, reported_jitter, rtp->rtcp->reported_normdev_jitter, reported_normdev_jitter_current, rtp->rtcp->reported_jitter_count);
3225
3226                         rtp->rtcp->reported_normdev_jitter = reported_normdev_jitter_current;
3227
3228                         rtp->rtcp->reported_lost = ntohl(rtcpheader[i + 1]) & 0xffffff;
3229
3230                         reported_lost = (double) rtp->rtcp->reported_lost;
3231
3232                         /* using same counter as for jitter */
3233                         if (rtp->rtcp->reported_jitter_count == 0)
3234                                 rtp->rtcp->reported_minlost = reported_lost;
3235
3236                         if (reported_lost < rtp->rtcp->reported_minlost)
3237                                 rtp->rtcp->reported_minlost = reported_lost;
3238
3239                         if (reported_lost > rtp->rtcp->reported_maxlost)
3240                                 rtp->rtcp->reported_maxlost = reported_lost;
3241                         reported_normdev_lost_current = normdev_compute(rtp->rtcp->reported_normdev_lost, reported_lost, rtp->rtcp->reported_jitter_count);
3242
3243                         rtp->rtcp->reported_stdev_lost = stddev_compute(rtp->rtcp->reported_stdev_lost, reported_lost, rtp->rtcp->reported_normdev_lost, reported_normdev_lost_current, rtp->rtcp->reported_jitter_count);
3244
3245                         rtp->rtcp->reported_normdev_lost = reported_normdev_lost_current;
3246
3247                         rtp->rtcp->reported_jitter_count++;
3248
3249                         if (rtcp_debug_test_addr(&addr)) {
3250                                 ast_verbose("  Fraction lost: %ld\n", (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24));
3251                                 ast_verbose("  Packets lost so far: %d\n", rtp->rtcp->reported_lost);
3252                                 ast_verbose("  Highest sequence number: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff));
3253                                 ast_verbose("  Sequence number cycles: %ld\n", (long) (ntohl(rtcpheader[i + 2])) >> 16);
3254                                 ast_verbose("  Interarrival jitter: %u\n", rtp->rtcp->reported_jitter);
3255                                 ast_verbose("  Last SR(our NTP): %lu.%010lu\n",(unsigned long) ntohl(rtcpheader[i + 4]) >> 16,((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096);
3256                                 ast_verbose("  DLSR: %4.4f (sec)\n",ntohl(rtcpheader[i + 5])/65536.0);
3257                                 if (rtt)
3258                                         ast_verbose("  RTT: %lu(sec)\n", (unsigned long) rtt);
3259                         }
3260                         if (rtt) {
3261                                 manager_event(EVENT_FLAG_REPORTING, "RTCPReceived", "From: %s\r\n"
3262                                                                     "PT: %d(%s)\r\n"
3263                                                                     "ReceptionReports: %d\r\n"
3264                                                                     "SenderSSRC: %u\r\n"
3265                                                                     "FractionLost: %ld\r\n"
3266                                                                     "PacketsLost: %d\r\n"
3267                                                                     "HighestSequence: %ld\r\n"
3268                                                                     "SequenceNumberCycles: %ld\r\n"
3269                                                                     "IAJitter: %u\r\n"
3270                                                                     "LastSR: %lu.%010lu\r\n"
3271                                                                     "DLSR: %4.4f(sec)\r\n"
3272                                               "RTT: %llu(sec)\r\n",
3273                                               ast_sockaddr_stringify(&addr),
3274                                               pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown",
3275                                               rc,
3276                                               rtcpheader[i + 1],
3277                                               (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24),
3278                                               rtp->rtcp->reported_lost,
3279                                               (long) (ntohl(rtcpheader[i + 2]) & 0xffff),
3280                                               (long) (ntohl(rtcpheader[i + 2])) >> 16,
3281                                               rtp->rtcp->reported_jitter,
3282                                               (unsigned long) ntohl(rtcpheader[i + 4]) >> 16, ((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096,
3283                                               ntohl(rtcpheader[i + 5])/65536.0,
3284                                               (unsigned long long)rtt);
3285                         } else {
3286                                 manager_event(EVENT_FLAG_REPORTING, "RTCPReceived", "From: %s\r\n"
3287                                                                     "PT: %d(%s)\r\n"
3288                                                                     "ReceptionReports: %d\r\n"
3289                                                                     "SenderSSRC: %u\r\n"
3290                                                                     "FractionLost: %ld\r\n"
3291                                                                     "PacketsLost: %d\r\n"
3292                                                                     "HighestSequence: %ld\r\n"
3293                                                                     "SequenceNumberCycles: %ld\r\n"
3294                                                                     "IAJitter: %u\r\n"
3295                                                                     "LastSR: %lu.%010lu\r\n"
3296                                               "DLSR: %4.4f(sec)\r\n",
3297                                               ast_sockaddr_stringify(&addr),
3298                                               pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown",
3299                                               rc,
3300                                               rtcpheader[i + 1],
3301                                               (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24),
3302                                               rtp->rtcp->reported_lost,
3303                                               (long) (ntohl(rtcpheader[i + 2]) & 0xffff),
3304                                               (long) (ntohl(rtcpheader[i + 2])) >> 16,
3305                                               rtp->rtcp->reported_jitter,
3306                                               (unsigned long) ntohl(rtcpheader[i + 4]) >> 16,
3307                                               ((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096,
3308                                               ntohl(rtcpheader[i + 5])/65536.0);
3309                         }
3310                         break;
3311                 case RTCP_PT_FUR:
3312                         if (rtcp_debug_test_addr(&addr))
3313                                 ast_verbose("Received an RTCP Fast Update Request\n");
3314                         rtp->f.frametype = AST_FRAME_CONTROL;
3315                         rtp->f.subclass.integer = AST_CONTROL_VIDUPDATE;
3316                         rtp->f.datalen = 0;
3317                         rtp->f.samples = 0;
3318                         rtp->f.mallocd = 0;
3319                         rtp->f.src = "RTP";
3320                         f = &rtp->f;
3321                         break;
3322                 case RTCP_PT_SDES:
3323                         if (rtcp_debug_test_addr(&addr))
3324                                 ast_verbose("Received an SDES from %s\n",
3325                                             ast_sockaddr_stringify(&rtp->rtcp->them));
3326                         break;
3327                 case RTCP_PT_BYE:
3328                         if (rtcp_debug_test_addr(&addr))
3329                                 ast_verbose("Received a BYE from %s\n",
3330                                             ast_sockaddr_stringify(&rtp->rtcp->them));
3331                         break;
3332                 default:
3333                         ast_debug(1, "Unknown RTCP packet (pt=%d) received from %s\n",
3334                                   pt, ast_sockaddr_stringify(&rtp->rtcp->them));
3335                         break;
3336                 }
3337                 position += (length + 1);
3338         }
3339
3340         rtp->rtcp->rtcp_info = 1;
3341
3342         return f;
3343 }
3344
3345 static int bridge_p2p_rtp_write(struct ast_rtp_instance *instance, unsigned int *rtpheader, int len, int hdrlen)
3346 {
3347         struct ast_rtp_instance *instance1 = ast_rtp_instance_get_bridged(instance);
3348         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance), *bridged = ast_rtp_instance_get_data(instance1);
3349         int res = 0, payload = 0, bridged_payload = 0, mark;
3350         struct ast_rtp_payload_type payload_type;
3351         int reconstruct = ntohl(rtpheader[0]);
3352         struct ast_sockaddr remote_address = { {0,} };
3353         int ice;
3354
3355         /* Get fields from packet */
3356         payload = (reconstruct & 0x7f0000) >> 16;
3357         mark = (((reconstruct & 0x800000) >> 23) != 0);
3358
3359         /* Check what the payload value should be */
3360         payload_type = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs(instance), payload);
3361
3362         /* Otherwise adjust bridged payload to match */
3363         bridged_payload = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance1), payload_type.asterisk_format, &payload_type.format, payload_type.rtp_code);
3364
3365         /* If no codec could be matched between instance and instance1, then somehow things were made incompatible while we were still bridged.  Bail. */
3366         if (bridged_payload < 0) {
3367                 return -1;
3368         }
3369
3370         /* If the payload coming in is not one of the negotiated ones then send it to the core, this will cause formats to change and the bridge to break */
3371         if (ast_rtp_codecs_find_payload_code(ast_rtp_instance_get_codecs(instance1), bridged_payload) == -1) {
3372                 ast_debug(1, "Unsupported payload type received \n");
3373                 return -1;
3374         }
3375
3376         /* If the marker bit has been explicitly set turn it on */
3377         if (ast_test_flag(rtp, FLAG_NEED_MARKER_BIT)) {
3378                 mark = 1;
3379                 ast_clear_flag(rtp, FLAG_NEED_MARKER_BIT);
3380         }
3381
3382         /* Reconstruct part of the packet */
3383         reconstruct &= 0xFF80FFFF;
3384         reconstruct |= (bridged_payload << 16);
3385         reconstruct |= (mark << 23);
3386         rtpheader[0] = htonl(reconstruct);
3387
3388         ast_rtp_instance_get_remote_address(instance1, &remote_address);
3389
3390         if (ast_sockaddr_isnull(&remote_address)) {
3391                 ast_debug(1, "Remote address is null, most likely RTP has been stopped\n");
3392                 return 0;
3393         }
3394
3395         /* Send the packet back out */
3396         res = rtp_sendto(instance1, (void *)rtpheader, len, 0, &remote_address, &ice);
3397         if (res < 0) {
3398                 if (!ast_rtp_instance_get_prop(instance1, AST_RTP_PROPERTY_NAT) || (ast_rtp_instance_get_prop(instance1, AST_RTP_PROPERTY_NAT) && (ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_ACTIVE))) {
3399                         ast_log(LOG_WARNING,
3400                                 "RTP Transmission error of packet to %s: %s\n",
3401                                 ast_sockaddr_stringify(&remote_address),
3402                                 strerror(errno));
3403                 } else if (((ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(bridged, FLAG_NAT_INACTIVE_NOWARN)) {
3404                         if (option_debug || rtpdebug)
3405                                 ast_log(LOG_WARNING,
3406                                         "RTP NAT: Can't write RTP to private "
3407                                         "address %s, waiting for other end to "
3408                                         "send audio...\n",
3409                                         ast_sockaddr_stringify(&remote_address));
3410                         ast_set_flag(bridged, FLAG_NAT_INACTIVE_NOWARN);
3411                 }
3412                 return 0;
3413         }
3414
3415         update_address_with_ice_candidate(rtp, COMPONENT_RTP, &remote_address);
3416
3417         if (rtp_debug_test_addr(&remote_address)) {
3418                 ast_verbose("Sent RTP P2P packet to %s%s (type %-2.2d, len %-6.6u)\n",
3419                             ast_sockaddr_stringify(&remote_address),
3420                             ice ? " (via ICE)" : "",
3421                             bridged_payload, len - hdrlen);
3422         }
3423
3424         return 0;
3425 }
3426
3427 static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtcp)
3428 {
3429         struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3430         struct ast_sockaddr addr;
3431         int res, hdrlen = 12, version, payloadtype, padding, mark, ext, cc, prev_seqno;
3432         unsigned int *rtpheader = (unsigned int*)(rtp->rawdata + AST_FRIENDLY_OFFSET), seqno, ssrc, timestamp;
3433         struct ast_rtp_payload_type payload;
3434         struct ast_sockaddr remote_address = { {0,} };
3435         struct frame_list frames;
3436
3437         /* If this is actually RTCP let's hop on over and handle it */
3438         if (rtcp) {
3439                 if (rtp->rtcp) {
3440                         return ast_rtcp_read(instance);
3441                 }
3442                 return &ast_null_frame;
3443         }
3444
3445         /* If we are currently sending DTMF to the remote party send a continuation packet */
3446         if (rtp->sending_digit) {
3447                 ast_rtp_dtmf_continuation(instance);
3448         }
3449
3450         /* Actually read in the data from the socket */
3451         if ((res = rtp_recvfrom(instance, rtp->rawdata + AST_FRIENDLY_OFFSET,
3452                                 sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET, 0,
3453                                 &addr)) < 0) {
3454                 ast_assert(errno != EBADF);
3455                 if (errno != EAGAIN) {
3456                         ast_log(LOG_WARNING, "RTP Read error: %s.  Hanging up.\n",
3457                                 (errno) ? strerror(errno) : "Unspecified");
3458                         return NULL;
3459                 }
3460                 return &ast_null_frame;
3461         }
3462
3463         /* If this was handled by the ICE session don't do anything */
3464         if (!res) {
3465                 return &ast_null_frame;
3466         }
3467
3468         /* Make sure the data that was read in is actually enough to make up an RTP packet */
3469         if (res < hdrlen) {
3470                 ast_log(LOG_WARNING, "RTP Read too short\n");
3471                 return &ast_null_frame;
3472         }
3473
3474         /* Get fields and verify this is an RTP packet */
3475         seqno = ntohl(rtpheader[0]);
3476
3477         ast_rtp_instance_get_remote_address(instance, &remote_address);
3478
3479         if (!(version = (seqno & 0xC0000000) >> 30)) {
3480                 struct sockaddr_in addr_tmp;
3481                 struct ast_sockaddr addr_v4;
3482                 if (ast_sockaddr_is_ipv4(&addr)) {
3483                         ast_sockaddr_to_sin(&addr, &addr_tmp);
3484                 } else if (ast_sockaddr_ipv4_mapped(&addr, &addr_v4)) {
3485                         ast_debug(1, "Using IPv6 mapped address %s for STUN\n",
3486                                   ast_sockaddr_stringify(&addr));
3487                         ast_sockaddr_to_sin(&addr_v4, &addr_tmp);
3488                 } else {
3489                         ast_debug(1, "Cannot do STUN for non IPv4 address %s\n",
3490                                   ast_sockaddr_stringify(&addr));
3491                         return &ast_null_frame;
3492                 }
3493                 if ((ast_stun_handle_packet(rtp->s, &addr_tmp, rtp->rawdata + AST_FRIENDLY_OFFSET, res, NULL, NULL) == AST_STUN_ACCEPT) &&
3494                     ast_sockaddr_isnull(&remote_address)) {
3495                         ast_sockaddr_from_sin(&addr, &addr_tmp);
3496                         ast_rtp_instance_set_remote_address(instance, &addr);
3497                 }
3498                 return &ast_null_frame;
3499         }
3500
3501         /* If strict RTP protection is enabled see if we need to learn the remote address or if we need to drop the packet */
3502         if (rtp->strict_rtp_state == STRICT_RTP_LEARN) {
3503                 ast_debug(1, "%p -- start learning mode pass with addr = %s\n", rtp, ast_sockaddr_stringify(&addr));
3504                 /* For now, we always copy the address. */
3505                 ast_sockaddr_copy(&rtp->strict_rtp_address, &addr);
3506
3507                 /* Send the rtp and the seqno from header to rtp_learning_rtp_seq_upda