44 #include <openssl/opensslconf.h> 45 #include <openssl/opensslv.h> 46 #if !defined(OPENSSL_NO_SRTP) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) 47 #include <openssl/ssl.h> 48 #include <openssl/err.h> 49 #include <openssl/bio.h> 50 #if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L) 51 #include <openssl/bn.h> 54 #include <openssl/dh.h> 61 #include <pjlib-util.h> 92 #define MAX_TIMESTAMP_SKEW 640 94 #define RTP_SEQ_MOD (1<<16) 95 #define RTCP_DEFAULT_INTERVALMS 5000 96 #define RTCP_MIN_INTERVALMS 500 97 #define RTCP_MAX_INTERVALMS 60000 99 #define DEFAULT_RTP_START 5000 100 #define DEFAULT_RTP_END 31000 102 #define MINIMUM_RTP_PORT 1024 103 #define MAXIMUM_RTP_PORT 65535 105 #define DEFAULT_TURN_PORT 3478 107 #define TURN_STATE_WAIT_TIME 2000 109 #define DEFAULT_RTP_SEND_BUFFER_SIZE 250 110 #define MAXIMUM_RTP_SEND_BUFFER_SIZE (DEFAULT_RTP_SEND_BUFFER_SIZE + 200) 111 #define DEFAULT_RTP_RECV_BUFFER_SIZE 20 112 #define MAXIMUM_RTP_RECV_BUFFER_SIZE (DEFAULT_RTP_RECV_BUFFER_SIZE + 20) 113 #define OLD_PACKET_COUNT 1000 114 #define MISSING_SEQNOS_ADDED_TRIGGER 2 116 #define SEQNO_CYCLE_OVER 65536 119 #define RTCP_PT_FUR 192 121 #define RTCP_PT_SR AST_RTP_RTCP_SR 123 #define RTCP_PT_RR AST_RTP_RTCP_RR 125 #define RTCP_PT_SDES 202 127 #define RTCP_PT_BYE 203 129 #define RTCP_PT_APP 204 132 #define RTCP_PT_PSFB AST_RTP_RTCP_PSFB 135 #define DTMF_SAMPLE_RATE_MS 8 137 #define DEFAULT_DTMF_TIMEOUT (150 * (8000 / 1000)) 139 #define ZFONE_PROFILE_ID 0x505a 141 #define DEFAULT_LEARNING_MIN_SEQUENTIAL 4 154 #define CALC_LEARNING_MIN_DURATION(count) (((count) - 1) * 9 - 5) 155 #define DEFAULT_LEARNING_MIN_DURATION CALC_LEARNING_MIN_DURATION(DEFAULT_LEARNING_MIN_SEQUENTIAL) 157 #define SRTP_MASTER_KEY_LEN 16 158 #define SRTP_MASTER_SALT_LEN 14 159 #define SRTP_MASTER_LEN (SRTP_MASTER_KEY_LEN + SRTP_MASTER_SALT_LEN) 161 #define RTP_DTLS_ESTABLISHED -37 182 #define STRICT_RTP_LEARN_TIMEOUT 5000 184 #define DEFAULT_STRICT_RTP STRICT_RTP_YES 185 #define DEFAULT_SRTP_REPLAY_PROTECTION 1 186 #define DEFAULT_ICESUPPORT 1 187 #define DEFAULT_STUN_SOFTWARE_ATTRIBUTE 1 188 #define DEFAULT_DTLS_MTU 1200 204 static int nochecksums;
210 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 213 #ifdef HAVE_PJPROJECT 283 #define FLAG_3389_WARNING (1 << 0) 284 #define FLAG_NAT_ACTIVE (3 << 1) 285 #define FLAG_NAT_INACTIVE (0 << 1) 286 #define FLAG_NAT_INACTIVE_NOWARN (1 << 1) 287 #define FLAG_NEED_MARKER_BIT (1 << 3) 288 #define FLAG_DTMF_COMPENSATE (1 << 4) 289 #define FLAG_REQ_LOCAL_BRIDGE_BIT (1 << 5) 291 #define TRANSPORT_SOCKET_RTP 0 292 #define TRANSPORT_SOCKET_RTCP 1 293 #define TRANSPORT_TURN_RTP 2 294 #define TRANSPORT_TURN_RTCP 3 299 struct timeval start;
300 struct timeval received;
307 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 308 struct dtls_details {
318 #ifdef HAVE_PJPROJECT 340 struct timeval received;
350 unsigned int last_seqno;
352 unsigned int last_extended_seqno;
354 unsigned int feedback_count;
387 unsigned short seedrxseqno;
388 unsigned int seedrxts;
389 unsigned int rxcount;
390 unsigned int rxoctetcount;
391 unsigned int txcount;
392 unsigned int txoctetcount;
401 unsigned int last_seqno;
403 unsigned int dtmf_duration;
404 unsigned int dtmf_timeout;
405 unsigned int dtmfsamples;
408 unsigned int lastdigitts;
414 struct timeval rxcore;
415 struct timeval txcore;
417 struct timeval dtmfmute;
419 unsigned short seqno;
422 unsigned int asymmetric_codec;
445 #ifdef HAVE_PJPROJECT 450 pj_turn_sock *turn_rtp;
451 pj_turn_sock *turn_rtcp;
452 pj_turn_state_t turn_state;
453 unsigned int passthrough:1;
454 unsigned int rtp_passthrough:1;
455 unsigned int rtcp_passthrough:1;
456 unsigned int ice_port;
462 char remote_ufrag[256];
463 char remote_passwd[256];
465 char local_ufrag[256];
466 char local_passwd[256];
472 unsigned int ice_num_components;
473 unsigned int ice_media_started:1;
476 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 481 char local_fingerprint[160];
483 unsigned char remote_fingerprint[EVP_MAX_MD_SIZE];
486 struct dtls_details dtls;
508 struct timeval rxlsr;
509 struct timeval txlsr;
555 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 556 struct dtls_details dtls;
578 unsigned char t140red_data[64000];
579 unsigned char buf_data[64000];
623 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 627 static int dtls_bio_write(BIO *bio,
const char *
buf,
int len);
628 static long dtls_bio_ctrl(BIO *bio,
int cmd,
long arg1,
void *arg2);
629 static int dtls_bio_new(BIO *bio);
630 static int dtls_bio_free(BIO *bio);
632 #ifndef HAVE_OPENSSL_BIO_METHOD 633 static BIO_METHOD dtls_bio_methods = {
634 .type = BIO_TYPE_BIO,
636 .bwrite = dtls_bio_write,
637 .ctrl = dtls_bio_ctrl,
638 .create = dtls_bio_new,
639 .destroy = dtls_bio_free,
642 static BIO_METHOD *dtls_bio_methods;
648 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 649 static int dtls_bio_new(BIO *bio)
651 #ifdef HAVE_OPENSSL_BIO_METHOD 652 BIO_set_init(bio, 1);
653 BIO_set_data(bio,
NULL);
654 BIO_set_shutdown(bio, 0);
663 static int dtls_bio_free(BIO *bio)
669 #ifdef HAVE_OPENSSL_BIO_METHOD 670 BIO_set_data(bio,
NULL);
677 static int dtls_bio_write(BIO *bio,
const char *
buf,
int len)
679 #ifdef HAVE_OPENSSL_BIO_METHOD 694 if (rtp->rtcp && rtp->rtcp->dtls.write_bio == bio) {
705 bytes_sent =
__rtp_sendto(instance, (
char *)buf, len, 0, &remote_address, rtcp, &ice, 0);
708 ast_debug(0,
"(%p) DTLS - sent %s packet to %s%s (len %-6.6d)\n",
710 ice ?
" (via ICE)" :
"", bytes_sent);
716 static long dtls_bio_ctrl(BIO *bio,
int cmd,
long arg1,
void *arg2)
721 case BIO_CTRL_DGRAM_QUERY_MTU:
723 case BIO_CTRL_WPENDING:
724 case BIO_CTRL_PENDING:
733 #ifdef HAVE_PJPROJECT 752 char address[PJ_INET6_ADDRSTRLEN];
754 if (component < 1 || !ice->comp[component - 1].valid_check) {
759 pj_sockaddr_print(&ice->comp[component - 1].valid_check->rcand->addr, address,
760 sizeof(address), 0), 0);
762 pj_sockaddr_get_port(&ice->comp[component - 1].valid_check->rcand->addr));
783 int ice_attrb_reset = 0;
786 if (!
ast_strlen_zero(rtp->remote_ufrag) && strcmp(ufrag, rtp->remote_ufrag)) {
793 if (!
ast_strlen_zero(rtp->remote_passwd) && strcmp(password, rtp->remote_passwd)) {
796 ast_copy_string(rtp->remote_passwd, password,
sizeof(rtp->remote_passwd));
800 if (ice_attrb_reset) {
810 if (strcmp(candidate1->
foundation, candidate2->foundation) ||
811 candidate1->
id != candidate2->id ||
812 candidate1->
type != candidate2->type ||
827 if (strcasecmp(candidate->
transport,
"udp")) {
831 if (!rtp->ice_proposed_remote_candidates) {
834 if (!rtp->ice_proposed_remote_candidates) {
849 remote_candidate->
id = candidate->
id;
854 remote_candidate->
type = candidate->
type;
856 ast_debug_ice(2,
"(%p) ICE add remote candidate\n", instance);
858 ao2_link(rtp->ice_proposed_remote_candidates, remote_candidate);
867 pj_thread_desc *
desc;
870 if (pj_thread_is_registered() == PJ_TRUE) {
876 ast_log(
LOG_ERROR,
"Could not get thread desc from thread-local storage. Expect awful things to occur\n");
879 pj_bzero(*desc,
sizeof(*desc));
881 if (pj_thread_register(
"Asterisk Thread", *desc, &thread) != PJ_SUCCESS) {
931 *pj_role = PJ_ICE_SESS_ROLE_CONTROLLED;
934 *pj_role = PJ_ICE_SESS_ROLE_CONTROLLING;
942 case PJ_ICE_SESS_ROLE_CONTROLLED:
945 case PJ_ICE_SESS_ROLE_CONTROLLING:
948 case PJ_ICE_SESS_ROLE_UNKNOWN:
965 if (!rtp->ice->real_ice->is_nominating && !rtp->ice->real_ice->is_complete) {
966 ast_debug_ice(3,
" (%p) ICE nevermind, not ready for a reset\n", instance);
970 ast_debug_ice(3,
"(%p) ICE recreating ICE session %s (%d)\n",
972 res =
ice_create(instance, &rtp->ice_original_rtp_addr, rtp->ice_port, 1);
975 enum pj_ice_sess_role role = PJ_ICE_SESS_ROLE_UNKNOWN;
977 pj_ice_sess_change_role(rtp->ice->real_ice, role);
983 if (rtp->ice_num_components == 1 && rtp->turn_rtcp) {
985 struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, };
987 rtp->turn_state = PJ_TURN_STATE_NULL;
991 pj_turn_sock_destroy(rtp->turn_rtcp);
993 while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) {
998 rtp->ice_media_started = 0;
1016 if (!left_candidate) {
1034 pj_str_t ufrag = pj_str(rtp->remote_ufrag), passwd = pj_str(rtp->remote_passwd);
1035 pj_ice_sess_cand candidates[PJ_ICE_MAX_CAND];
1038 int cand_cnt = 0, has_rtp = 0, has_rtcp = 0;
1040 if (!rtp->ice || !rtp->ice_proposed_remote_candidates) {
1045 if (rtp->ice_active_remote_candidates &&
1047 ast_debug_ice(2,
"(%p) ICE proposed equals active candidates\n", instance);
1049 rtp->ice_proposed_remote_candidates =
NULL;
1057 rtp->ice_active_remote_candidates = rtp->ice_proposed_remote_candidates;
1058 rtp->ice_proposed_remote_candidates =
NULL;
1064 ast_log(
LOG_NOTICE,
"(%p) ICE failed to create replacement session\n", instance);
1079 pj_strdup2(rtp->ice->real_ice->pool, &candidates[cand_cnt].foundation,
1081 candidates[cand_cnt].comp_id = candidate->
id;
1082 candidates[cand_cnt].prio = candidate->
priority;
1091 candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_HOST;
1093 candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_SRFLX;
1095 candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_RELAYED;
1102 pj_turn_sock_set_perm(rtp->turn_rtp, 1, &candidates[cand_cnt].addr, 1);
1108 pj_turn_sock_set_perm(rtp->turn_rtcp, 1, &candidates[cand_cnt].addr, 1);
1119 ast_log(
LOG_WARNING,
"(%p) ICE lost %d candidates. Consider increasing PJ_ICE_MAX_CAND in PJSIP\n",
1128 if (!has_rtcp && rtp->ice_num_components > 1) {
1132 if (rtp->ice && has_rtp && (has_rtcp || rtp->ice_num_components == 1)) {
1141 res = pj_ice_sess_create_check_list(ice->
real_ice, &ufrag, &passwd, cand_cnt, &candidates[0]);
1142 if (res == PJ_SUCCESS) {
1143 ast_debug_ice(2,
"(%p) ICE successfully created checklist\n", instance);
1145 pj_ice_sess_start_check(ice->
real_ice);
1155 pj_strerror(res, reason,
sizeof(reason));
1156 ast_log(
LOG_WARNING,
"(%p) ICE failed to create session check list: %s\n", instance, reason);
1165 ao2_ref(rtp->ice_active_remote_candidates, -1);
1166 rtp->ice_active_remote_candidates =
NULL;
1168 rtp->ice->real_ice->rcand_cnt = rtp->ice->real_ice->clist.count = 0;
1177 return rtp->local_ufrag;
1185 return rtp->local_passwd;
1193 if (rtp->ice_local_candidates) {
1194 ao2_ref(rtp->ice_local_candidates, +1);
1197 return rtp->ice_local_candidates;
1211 pj_ice_sess_change_role(rtp->ice->real_ice, PJ_ICE_SESS_ROLE_CONTROLLING);
1220 ast_debug_ice(3,
"(%p) ICE set role failed; no ice instance\n", instance);
1226 if (!rtp->ice->real_ice->is_nominating && !rtp->ice->real_ice->is_complete) {
1231 PJ_ICE_SESS_ROLE_CONTROLLED : PJ_ICE_SESS_ROLE_CONTROLLING);
1233 ast_debug_ice(2,
"(%p) ICE not setting role because state is %s\n",
1234 instance, rtp->ice->real_ice->is_nominating ?
"nominating" :
"complete");
1240 unsigned comp_id,
unsigned transport_id, pj_ice_cand_type
type, pj_uint16_t local_pref,
1241 const pj_sockaddr_t *addr,
const pj_sockaddr_t *base_addr,
const pj_sockaddr_t *rel_addr,
1244 pj_str_t foundation;
1247 char address[PJ_INET6_ADDRSTRLEN];
1256 pj_ice_calc_foundation(rtp->ice->real_ice->pool, &foundation, type, addr);
1258 if (!rtp->ice_local_candidates) {
1261 if (!rtp->ice_local_candidates) {
1271 candidate->
id = comp_id;
1282 if (type == PJ_ICE_CAND_TYPE_HOST) {
1284 }
else if (type == PJ_ICE_CAND_TYPE_SRFLX) {
1286 }
else if (type == PJ_ICE_CAND_TYPE_RELAYED) {
1300 status = pj_ice_sess_add_cand(ice->
real_ice, comp_id, transport_id, type, local_pref,
1301 &foundation, addr, base_addr, rel_addr, addr_len,
NULL);
1304 if (!rtp->ice || status != PJ_SUCCESS) {
1312 candidate->
priority = rtp->ice->real_ice->lcand[rtp->ice->real_ice->lcand_cnt - 1].prio;
1317 ao2_link(rtp->ice_local_candidates, candidate);
1337 if (status != PJ_SUCCESS) {
1340 pj_strerror(status, buf,
sizeof(buf));
1342 instance, (
int)status, buf);
1345 if (!rtp->rtp_passthrough) {
1348 rtp->rtp_passthrough = 0;
1351 ast_sendto(rtp->
s, pkt, pkt_len, 0, &rtp->rtp_loop);
1370 rtp->turn_state = new_state;
1373 if (new_state == PJ_TURN_STATE_DESTROYING) {
1374 pj_turn_sock_set_user_data(rtp->turn_rtp,
NULL);
1375 rtp->turn_rtp =
NULL;
1403 if (status != PJ_SUCCESS) {
1406 pj_strerror(status, buf,
sizeof(buf));
1411 if (!rtp->rtcp_passthrough) {
1414 rtp->rtcp_passthrough = 0;
1417 ast_sendto(rtp->rtcp->
s, pkt, pkt_len, 0, &rtp->rtcp_loop);
1436 rtp->turn_state = new_state;
1439 if (new_state == PJ_TURN_STATE_DESTROYING) {
1440 pj_turn_sock_set_user_data(rtp->turn_rtcp,
NULL);
1441 rtp->turn_rtcp =
NULL;
1459 const pj_time_val delay = {0, 10};
1461 pj_ioqueue_poll(ioqueue->
ioqueue, &delay);
1474 pj_thread_join(ioqueue->
thread);
1475 pj_thread_destroy(ioqueue->
thread);
1478 if (ioqueue->
pool) {
1482 pj_pool_t *temp_pool = ioqueue->
pool;
1485 pj_pool_release(temp_pool);
1498 if ((ioqueue->
count - 2) == 0) {
1521 if ((ioqueue->
count + 2) < PJ_IOQUEUE_MAX_HANDLES) {
1528 ioqueue->
count += 2;
1542 if (pj_timer_heap_create(ioqueue->
pool, 4, &ioqueue->
timerheap) != PJ_SUCCESS) {
1546 if (pj_lock_create_recursive_mutex(ioqueue->
pool,
"rtp%p", &lock) != PJ_SUCCESS) {
1550 pj_timer_heap_set_lock(ioqueue->
timerheap, lock, PJ_TRUE);
1552 if (pj_ioqueue_create(ioqueue->
pool, PJ_IOQUEUE_MAX_HANDLES, &ioqueue->
ioqueue) != PJ_SUCCESS) {
1578 enum ast_transport transport,
const char *server,
unsigned int port,
const char *username,
const char *
password)
1581 pj_turn_sock **turn_sock;
1582 const pj_turn_sock_cb *turn_cb;
1583 pj_turn_tp_type conn_type;
1585 pj_stun_auth_cred cred = { 0, };
1588 pj_stun_config stun_config;
1590 struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, };
1591 pj_turn_session_info
info;
1594 pj_turn_sock_cfg turn_sock_cfg;
1606 turn_sock = &rtp->turn_rtp;
1611 turn_sock = &rtp->turn_rtcp;
1620 conn_type = PJ_TURN_TP_UDP;
1622 conn_type = PJ_TURN_TP_TCP;
1631 rtp->turn_state = PJ_TURN_STATE_NULL;
1635 pj_turn_sock_destroy(*turn_sock);
1637 while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) {
1651 if (!rtp->ioqueue) {
1656 pj_stun_config_init(&stun_config, &
cachingpool.factory, 0, rtp->ioqueue->ioqueue, rtp->ioqueue->timerheap);
1658 stun_config.software_name = pj_str(
NULL);
1662 pj_turn_sock_cfg_default(&turn_sock_cfg);
1665 turn_sock_cfg.grp_lock = ice->
real_ice->grp_lock;
1671 status = pj_turn_sock_create(&stun_config,
1673 turn_cb, &turn_sock_cfg, instance, turn_sock);
1675 if (status != PJ_SUCCESS) {
1681 cred.type = PJ_STUN_AUTH_CRED_STATIC;
1682 pj_strset2(&cred.data.static_cred.username, (
char*)username);
1683 cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
1684 pj_strset2(&cred.data.static_cred.data, (
char*)password);
1686 pj_turn_sock_alloc(*turn_sock, pj_cstr(&turn_addr, server), port,
NULL, &cred,
NULL);
1688 ast_debug_ice(2,
"(%p) ICE request TURN %s %s candidate\n", instance,
1698 while (rtp->turn_state < PJ_TURN_STATE_READY) {
1703 if (rtp->turn_state != PJ_TURN_STATE_READY) {
1707 pj_turn_sock_get_info(*turn_sock, &info);
1710 PJ_ICE_CAND_TYPE_RELAYED, 65535, &info.relay_addr, &info.relay_addr,
1711 &info.mapped_addr, pj_sockaddr_get_len(&info.relay_addr));
1725 for (x=0; x<4; x++) {
1728 snprintf(buf, size,
"%08lx%08lx%08lx%08lx", (
long unsigned)val[0], (
long unsigned)val[1], (
long unsigned)val[2], (
long unsigned)val[3]);
1741 if (!
icesupport || !rtp->ice || rtp->ice_num_components == num_components) {
1745 ast_debug_ice(2,
"(%p) ICE change number of components %u -> %u\n", instance,
1746 rtp->ice_num_components, num_components);
1748 rtp->ice_num_components = num_components;
1768 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 1769 static int dtls_verify_callback(
int preverify_ok, X509_STORE_CTX *ctx)
1775 static int dtls_details_initialize(
struct dtls_details *dtls,
SSL_CTX *ssl_ctx,
1778 dtls->dtls_setup = setup;
1780 if (!(dtls->ssl = SSL_new(ssl_ctx))) {
1785 if (!(dtls->read_bio = BIO_new(BIO_s_mem()))) {
1789 BIO_set_mem_eof_return(dtls->read_bio, -1);
1791 #ifdef HAVE_OPENSSL_BIO_METHOD 1792 if (!(dtls->write_bio = BIO_new(dtls_bio_methods))) {
1793 ast_log(
LOG_ERROR,
"Failed to allocate memory for outbound SSL traffic\n");
1797 BIO_set_data(dtls->write_bio, instance);
1799 if (!(dtls->write_bio = BIO_new(&dtls_bio_methods))) {
1800 ast_log(
LOG_ERROR,
"Failed to allocate memory for outbound SSL traffic\n");
1803 dtls->write_bio->ptr = instance;
1805 SSL_set_bio(dtls->ssl, dtls->read_bio, dtls->write_bio);
1808 SSL_set_accept_state(dtls->ssl);
1810 SSL_set_connect_state(dtls->ssl);
1817 if (dtls->read_bio) {
1818 BIO_free(dtls->read_bio);
1819 dtls->read_bio =
NULL;
1822 if (dtls->write_bio) {
1823 BIO_free(dtls->write_bio);
1824 dtls->write_bio =
NULL;
1828 SSL_free(dtls->ssl);
1838 if (!rtp->ssl_ctx || !rtp->rtcp) {
1843 return dtls_details_initialize(&rtp->rtcp->dtls, rtp->ssl_ctx, rtp->dtls.dtls_setup, instance);
1846 static const SSL_METHOD *get_dtls_method(
void)
1848 #if OPENSSL_VERSION_NUMBER < 0x10002000L || defined(LIBRESSL_VERSION_NUMBER) 1849 return DTLSv1_method();
1851 return DTLS_method();
1855 struct dtls_cert_info {
1856 EVP_PKEY *private_key;
1862 #if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L) && (OPENSSL_VERSION_NUMBER < 0x10100000L) 1866 #ifndef OPENSSL_NO_DH 1868 BIO *bio = BIO_new_file(dtls_cfg->
pvtfile,
"r");
1872 if (SSL_CTX_set_tmp_dh(rtp->ssl_ctx, dh)) {
1873 long options = SSL_OP_CIPHER_SERVER_PREFERENCE |
1874 SSL_OP_SINGLE_DH_USE | SSL_OP_SINGLE_ECDH_USE;
1875 options = SSL_CTX_set_options(rtp->ssl_ctx, options);
1876 ast_verb(2,
"DTLS DH initialized, PFS enabled\n");
1885 #if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L) && (OPENSSL_VERSION_NUMBER < 0x10100000L) 1887 ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
1889 if (SSL_CTX_set_tmp_ecdh(rtp->ssl_ctx, ecdh)) {
1890 #ifndef SSL_CTRL_SET_ECDH_AUTO 1891 #define SSL_CTRL_SET_ECDH_AUTO 94 1895 ast_verb(2,
"DTLS ECDH initialized (automatic), faster PFS enabled\n");
1897 ast_verb(2,
"DTLS ECDH initialized (secp256r1), faster PFS enabled\n");
1905 #if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L) 1907 static int create_ephemeral_ec_keypair(EVP_PKEY **keypair)
1909 EC_KEY *eckey =
NULL;
1912 group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
1917 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
1918 EC_GROUP_set_point_conversion_form(group, POINT_CONVERSION_UNCOMPRESSED);
1920 eckey = EC_KEY_new();
1925 if (!EC_KEY_set_group(eckey, group)) {
1929 if (!EC_KEY_generate_key(eckey)) {
1933 *keypair = EVP_PKEY_new();
1938 EVP_PKEY_assign_EC_KEY(*keypair, eckey);
1939 EC_GROUP_free(group);
1945 EC_GROUP_free(group);
1951 #define SERIAL_RAND_BITS 159 1953 static int create_ephemeral_certificate(EVP_PKEY *keypair, X509 **certificate)
1956 BIGNUM *serial =
NULL;
1964 if (!X509_set_version(cert, 2)) {
1969 X509_set_pubkey(cert, keypair);
1972 if (!(serial = BN_new())
1973 || !BN_rand(serial, SERIAL_RAND_BITS, -1, 0)
1974 || !BN_to_ASN1_INTEGER(serial, X509_get_serialNumber(cert))) {
1982 #if OPENSSL_VERSION_NUMBER < 0x10100000L 1983 if (!X509_time_adj_ex(X509_get_notBefore(cert), -1, 0,
NULL)
1984 || !X509_time_adj_ex(X509_get_notAfter(cert), 30, 0,
NULL)) {
1988 if (!X509_time_adj_ex(X509_getm_notBefore(cert), -1, 0,
NULL)
1989 || !X509_time_adj_ex(X509_getm_notAfter(cert), 30, 0,
NULL)) {
1995 if (!(name = X509_get_subject_name(cert))
1996 || !X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_ASC,
1997 (
unsigned char *)
"asterisk", -1, -1, 0)
1998 || !X509_set_issuer_name(cert, name)) {
2003 if (!X509_sign(cert, keypair, EVP_sha256())) {
2007 *certificate = cert;
2020 struct dtls_cert_info *cert_info)
2023 cert_info->private_key =
NULL;
2024 cert_info->certificate =
NULL;
2026 if (create_ephemeral_ec_keypair(&cert_info->private_key)) {
2031 if (create_ephemeral_certificate(cert_info->private_key, &cert_info->certificate)) {
2039 X509_free(cert_info->certificate);
2040 EVP_PKEY_free(cert_info->private_key);
2049 struct dtls_cert_info *cert_info)
2051 ast_log(
LOG_ERROR,
"Your version of OpenSSL does not support ECDSA keys\n");
2059 struct dtls_cert_info *cert_info)
2062 BIO *certbio =
NULL;
2063 EVP_PKEY *private_key =
NULL;
2067 fp = fopen(private_key_file,
"r");
2069 ast_log(
LOG_ERROR,
"Failed to read private key from file '%s': %s\n", private_key_file, strerror(
errno));
2073 if (!PEM_read_PrivateKey(fp, &private_key,
NULL,
NULL)) {
2074 ast_log(
LOG_ERROR,
"Failed to read private key from PEM file '%s'\n", private_key_file);
2080 ast_log(
LOG_ERROR,
"Failed to close private key file '%s': %s\n", private_key_file, strerror(
errno));
2084 certbio = BIO_new(BIO_s_file());
2086 ast_log(
LOG_ERROR,
"Failed to allocate memory for certificate fingerprinting on RTP instance '%p'\n",
2091 if (!BIO_read_filename(certbio, dtls_cfg->
certfile)
2092 || !(cert = PEM_read_bio_X509(certbio,
NULL, 0,
NULL))) {
2097 cert_info->private_key = private_key;
2098 cert_info->certificate = cert;
2100 BIO_free_all(certbio);
2106 BIO_free_all(certbio);
2107 EVP_PKEY_free(private_key);
2114 struct dtls_cert_info *cert_info)
2117 return create_certificate_ephemeral(instance, dtls_cfg, cert_info);
2119 return create_certificate_from_file(instance, dtls_cfg, cert_info);
2129 struct dtls_cert_info cert_info = { 0 };
2139 ast_log(
LOG_ERROR,
"SRTP support module is not loaded or available. Try loading res_srtp.so.\n");
2147 rtp->ssl_ctx = SSL_CTX_new(get_dtls_method());
2148 if (!rtp->ssl_ctx) {
2152 SSL_CTX_set_read_ahead(rtp->ssl_ctx, 1);
2154 configure_dhparams(rtp, dtls_cfg);
2156 rtp->dtls_verify = dtls_cfg->
verify;
2160 dtls_verify_callback :
NULL);
2163 SSL_CTX_set_tlsext_use_srtp(rtp->ssl_ctx,
"SRTP_AES128_CM_SHA1_80");
2165 SSL_CTX_set_tlsext_use_srtp(rtp->ssl_ctx,
"SRTP_AES128_CM_SHA1_32");
2167 ast_log(
LOG_ERROR,
"Unsupported suite specified for DTLS-SRTP on RTP instance '%p'\n", instance);
2171 rtp->local_hash = dtls_cfg->
hash;
2173 if (!load_dtls_certificate(instance, dtls_cfg, &cert_info)) {
2175 unsigned int size, i;
2176 unsigned char fingerprint[EVP_MAX_MD_SIZE];
2177 char *local_fingerprint = rtp->local_fingerprint;
2179 if (!SSL_CTX_use_certificate(rtp->ssl_ctx, cert_info.certificate)) {
2180 ast_log(
LOG_ERROR,
"Specified certificate for RTP instance '%p' could not be used\n",
2185 if (!SSL_CTX_use_PrivateKey(rtp->ssl_ctx, cert_info.private_key)
2186 || !SSL_CTX_check_private_key(rtp->ssl_ctx)) {
2187 ast_log(
LOG_ERROR,
"Specified private key for RTP instance '%p' could not be used\n",
2195 type = EVP_sha256();
2197 ast_log(
LOG_ERROR,
"Unsupported fingerprint hash type on RTP instance '%p'\n",
2202 if (!X509_digest(cert_info.certificate, type, fingerprint, &size) || !size) {
2203 ast_log(
LOG_ERROR,
"Could not produce fingerprint from certificate for RTP instance '%p'\n",
2208 for (i = 0; i < size; i++) {
2209 sprintf(local_fingerprint,
"%02hhX:", fingerprint[i]);
2210 local_fingerprint += 3;
2213 *(local_fingerprint - 1) = 0;
2215 EVP_PKEY_free(cert_info.private_key);
2216 X509_free(cert_info.certificate);
2220 if (!SSL_CTX_set_cipher_list(rtp->ssl_ctx, dtls_cfg->
cipher)) {
2221 ast_log(
LOG_ERROR,
"Invalid cipher specified in cipher list '%s' for RTP instance '%p'\n",
2222 dtls_cfg->
cipher, instance);
2228 if (!SSL_CTX_load_verify_locations(rtp->ssl_ctx,
S_OR(dtls_cfg->
cafile, NULL),
S_OR(dtls_cfg->
capath, NULL))) {
2229 ast_log(
LOG_ERROR,
"Invalid certificate authority file '%s' or path '%s' specified for RTP instance '%p'\n",
2235 rtp->rekey = dtls_cfg->
rekey;
2236 rtp->suite = dtls_cfg->
suite;
2238 res = dtls_details_initialize(&rtp->dtls, rtp->ssl_ctx, dtls_cfg->
default_setup, instance);
2240 dtls_setup_rtcp(instance);
2251 return !rtp->ssl_ctx ? 0 : 1;
2258 SSL *ssl = rtp->dtls.ssl;
2262 dtls_srtp_stop_timeout_timer(instance, rtp, 0);
2266 SSL_CTX_free(rtp->ssl_ctx);
2267 rtp->ssl_ctx =
NULL;
2270 if (rtp->dtls.ssl) {
2271 SSL_free(rtp->dtls.ssl);
2272 rtp->dtls.ssl =
NULL;
2277 dtls_srtp_stop_timeout_timer(instance, rtp, 1);
2280 if (rtp->rtcp->dtls.ssl) {
2281 if (rtp->rtcp->dtls.ssl != ssl) {
2282 SSL_free(rtp->rtcp->dtls.ssl);
2284 rtp->rtcp->dtls.ssl =
NULL;
2294 if (SSL_is_init_finished(rtp->dtls.ssl)) {
2295 SSL_shutdown(rtp->dtls.ssl);
2299 if (rtp->rtcp && SSL_is_init_finished(rtp->rtcp->dtls.ssl)) {
2300 SSL_shutdown(rtp->rtcp->dtls.ssl);
2310 return rtp->dtls.connection;
2318 return rtp->dtls.dtls_setup;
2347 if (old == *dtls_setup) {
2357 SSL_set_connect_state(ssl);
2359 SSL_set_accept_state(ssl);
2370 if (rtp->dtls.ssl) {
2371 dtls_set_setup(&rtp->dtls.dtls_setup, setup, rtp->dtls.ssl);
2374 if (rtp->rtcp && rtp->rtcp->dtls.ssl) {
2375 dtls_set_setup(&rtp->rtcp->dtls.dtls_setup, setup, rtp->rtcp->dtls.ssl);
2390 rtp->remote_hash = hash;
2392 while ((
value =
strsep(&tmp,
":")) && (pos != (EVP_MAX_MD_SIZE - 1))) {
2393 sscanf(
value,
"%02hhx", &rtp->remote_fingerprint[pos++]);
2402 return rtp->local_hash;
2406 static const char *ast_rtp_dtls_get_fingerprint(
struct ast_rtp_instance *instance)
2410 return rtp->local_fingerprint;
2416 .active = ast_rtp_dtls_active,
2417 .stop = ast_rtp_dtls_stop,
2418 .reset = ast_rtp_dtls_reset,
2419 .get_connection = ast_rtp_dtls_get_connection,
2420 .get_setup = ast_rtp_dtls_get_setup,
2421 .set_setup = ast_rtp_dtls_set_setup,
2422 .set_fingerprint = ast_rtp_dtls_set_fingerprint,
2423 .get_fingerprint_hash = ast_rtp_dtls_get_fingerprint_hash,
2424 .get_fingerprint = ast_rtp_dtls_get_fingerprint,
2429 #ifdef TEST_FRAMEWORK 2434 if (rtp && rtp->recv_buffer) {
2445 if (rtp && rtp->recv_buffer) {
2456 if (rtp && rtp->send_buffer) {
2467 if (rtp && rtp->rtcp) {
2468 rtp->rtcp->schedid =
id;
2509 #ifdef HAVE_PJPROJECT 2512 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 2513 .dtls = &ast_rtp_dtls,
2514 .activate = ast_rtp_activate,
2522 #ifdef TEST_FRAMEWORK 2527 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 2529 static void dtls_perform_handshake(
struct ast_rtp_instance *instance,
struct dtls_details *dtls,
int rtcp)
2533 ast_debug_dtls(3,
"(%p) DTLS perform handshake - ssl = %p, setup = %d\n",
2534 rtp, dtls->ssl, dtls->dtls_setup);
2544 SSL_do_handshake(dtls->ssl);
2554 dtls_srtp_start_timeout_timer(instance, rtp, rtcp);
2558 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 2559 static void dtls_perform_setup(
struct dtls_details *dtls)
2561 if (!dtls->ssl || !SSL_is_init_finished(dtls->ssl)) {
2565 SSL_clear(dtls->ssl);
2567 SSL_set_accept_state(dtls->ssl);
2569 SSL_set_connect_state(dtls->ssl);
2577 #ifdef HAVE_PJPROJECT 2588 if (status == PJ_SUCCESS) {
2605 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 2607 if (rtp->ice_media_started) {
2613 "(%p) ICE starting media - perform DTLS - (%p)\n", instance, rtp);
2627 dtls_perform_handshake(instance, &rtp->dtls, 0);
2630 dtls_perform_handshake(instance, &rtp->rtcp->dtls, 1);
2634 rtp->ice_media_started = 1;
2641 ast_verb(4,
"%p -- Strict RTP learning after ICE completion\n", rtp);
2646 #ifdef HAVE_PJPROJECT_ON_VALID_ICE_PAIR_CALLBACK 2650 ast_debug_ice(2,
"(%p) ICE valid pair, start media\n", ice->user_data);
2658 ast_debug_ice(2,
"(%p) ICE complete, start media\n", ice->user_data);
2663 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)
2671 rtp->passthrough = 1;
2673 rtp->rtp_passthrough = 1;
2675 rtp->rtcp_passthrough = 1;
2680 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)
2684 pj_status_t
status = PJ_EINVALIDOP;
2685 pj_ssize_t _size = (pj_ssize_t)size;
2689 status = pj_sock_sendto(rtp->
s, pkt, &_size, 0, dst_addr, dst_addr_len);
2691 ast_assert(_size == size || status != PJ_SUCCESS);
2695 status = pj_sock_sendto(rtp->rtcp->
s, pkt, &_size, 0, dst_addr, dst_addr_len);
2697 ast_assert(_size == size || status != PJ_SUCCESS);
2699 status = PJ_SUCCESS;
2703 if (rtp->turn_rtp) {
2704 status = pj_turn_sock_sendto(rtp->turn_rtp, pkt, size, dst_addr, dst_addr_len);
2708 if (rtp->turn_rtcp) {
2709 status = pj_turn_sock_sendto(rtp->turn_rtcp, pkt, size, dst_addr, dst_addr_len);
2718 #ifdef HAVE_PJPROJECT_ON_VALID_ICE_PAIR_CALLBACK 2731 if (pj_ioqueue_create(
pool, 1, &ioqueue) != PJ_SUCCESS) {
2736 const pj_time_val delay = {0, 10};
2739 pj_ioqueue_poll(ioqueue, &delay);
2778 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 2780 static int dtls_srtp_handle_timeout(
struct ast_rtp_instance *instance,
int rtcp)
2783 struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
2784 struct timeval dtls_timeout;
2786 ast_debug_dtls(3,
"(%p) DTLS srtp - handle timeout - rtcp=%d\n", instance, rtcp);
2787 DTLSv1_handle_timeout(dtls->ssl);
2790 if (!DTLSv1_get_timeout(dtls->ssl, &dtls_timeout)) {
2791 dtls->timeout_timer = -1;
2795 return dtls_timeout.tv_sec * 1000 + dtls_timeout.tv_usec / 1000;
2799 static int dtls_srtp_handle_rtp_timeout(
const void *data)
2805 reschedule = dtls_srtp_handle_timeout(instance, 0);
2815 static int dtls_srtp_handle_rtcp_timeout(
const void *data)
2821 reschedule = dtls_srtp_handle_timeout(instance, 1);
2832 struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
2833 struct timeval dtls_timeout;
2835 if (DTLSv1_get_timeout(dtls->ssl, &dtls_timeout)) {
2836 int timeout = dtls_timeout.tv_sec * 1000 + dtls_timeout.tv_usec / 1000;
2841 if ((dtls->timeout_timer =
ast_sched_add(rtp->sched, timeout,
2842 !rtcp ? dtls_srtp_handle_rtp_timeout : dtls_srtp_handle_rtcp_timeout, instance)) < 0) {
2844 ast_log(
LOG_WARNING,
"Scheduling '%s' DTLS retransmission for RTP instance [%p] failed.\n",
2845 !rtcp ?
"RTP" :
"RTCP", instance);
2847 ast_debug_dtls(3,
"(%p) DTLS srtp - scheduled timeout timer for '%d'\n", instance, timeout);
2855 struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
2858 ast_debug_dtls(3,
"(%p) DTLS srtp - stopped timeout timer'\n", instance);
2862 static int dtls_srtp_renegotiate(
const void *data)
2870 SSL_renegotiate(rtp->dtls.ssl);
2871 SSL_do_handshake(rtp->dtls.ssl);
2873 if (rtp->rtcp && rtp->rtcp->dtls.ssl && rtp->rtcp->dtls.ssl != rtp->dtls.ssl) {
2874 SSL_renegotiate(rtp->rtcp->dtls.ssl);
2875 SSL_do_handshake(rtp->rtcp->dtls.ssl);
2886 static int dtls_srtp_add_local_ssrc(
struct ast_rtp *rtp,
struct ast_rtp_instance *instance,
int rtcp,
unsigned int ssrc,
int set_remote_policy)
2889 unsigned char *local_key, *local_salt, *remote_key, *remote_salt;
2892 struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
2894 ast_debug_dtls(3,
"(%p) DTLS srtp - add local ssrc - rtcp=%d, set_remote_policy=%d'\n",
2895 instance, rtcp, set_remote_policy);
2898 if (!SSL_export_keying_material(dtls->ssl, material,
SRTP_MASTER_LEN * 2,
"EXTRACTOR-dtls_srtp", 19,
NULL, 0, 0)) {
2899 ast_log(
LOG_WARNING,
"Unable to extract SRTP keying material from DTLS-SRTP negotiation on RTP instance '%p'\n",
2906 local_key = material;
2911 remote_key = material;
2917 if (!(local_policy = res_srtp_policy->
alloc())) {
2922 ast_log(
LOG_WARNING,
"Could not set key/salt information on local policy of '%p' when setting up DTLS-SRTP\n", rtp);
2926 if (res_srtp_policy->
set_suite(local_policy, rtp->suite)) {
2927 ast_log(
LOG_WARNING,
"Could not set suite to '%u' on local policy of '%p' when setting up DTLS-SRTP\n", rtp->suite, rtp);
2931 res_srtp_policy->
set_ssrc(local_policy, ssrc, 0);
2933 if (set_remote_policy) {
2934 if (!(remote_policy = res_srtp_policy->
alloc())) {
2939 ast_log(
LOG_WARNING,
"Could not set key/salt information on remote policy of '%p' when setting up DTLS-SRTP\n", rtp);
2943 if (res_srtp_policy->
set_suite(remote_policy, rtp->suite)) {
2944 ast_log(
LOG_WARNING,
"Could not set suite to '%u' on remote policy of '%p' when setting up DTLS-SRTP\n", rtp->suite, rtp);
2948 res_srtp_policy->
set_ssrc(remote_policy, 0, 1);
2952 ast_log(
LOG_WARNING,
"Could not set policies when setting up DTLS-SRTP on '%p'\n", rtp);
2960 res_srtp_policy->
destroy(local_policy);
2962 if (remote_policy) {
2963 res_srtp_policy->
destroy(remote_policy);
2971 struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
2974 ast_debug_dtls(3,
"(%p) DTLS setup SRTP rtp=%p'\n", instance, rtp);
2980 if (!(certificate = SSL_get_peer_certificate(dtls->ssl))) {
2981 ast_log(
LOG_WARNING,
"No certificate was provided by the peer on RTP instance '%p'\n", instance);
2986 if (rtp->remote_fingerprint[0]) {
2988 unsigned char fingerprint[EVP_MAX_MD_SIZE];
2994 type = EVP_sha256();
2996 ast_log(
LOG_WARNING,
"Unsupported fingerprint hash type on RTP instance '%p'\n", instance);
3000 if (!X509_digest(certificate, type, fingerprint, &size) ||
3002 memcmp(fingerprint, rtp->remote_fingerprint, size)) {
3003 X509_free(certificate);
3004 ast_log(
LOG_WARNING,
"Fingerprint provided by remote party does not match that of peer certificate on RTP instance '%p'\n",
3010 X509_free(certificate);
3018 for (index = 0; index <
AST_VECTOR_SIZE(&rtp->ssrc_mapping); ++index) {
3028 if ((rtp->rekeyid =
ast_sched_add(rtp->sched, rtp->rekey * 1000, dtls_srtp_renegotiate, instance)) < 0) {
3041 return elem -
value;
3047 return elem ==
value;
3060 version = (packet[0] & 0XC0) >> 6;
3075 m = packet[1] & 0x80;
3076 pt = packet[1] & 0x7F;
3077 if (m && pt >= 64 && pt <= 95) {
3088 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 3091 #ifdef HAVE_PJPROJECT 3092 struct ast_sockaddr *loop = rtcp ? &rtp->rtcp_loop : &rtp->rtp_loop;
3094 #ifdef TEST_FRAMEWORK 3098 if ((len =
ast_recvfrom(rtcp ? rtp->rtcp->
s : rtp->
s, buf, size, flags, sa)) < 0) {
3102 #ifdef TEST_FRAMEWORK 3109 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 3112 if ((*in >= 20) && (*in <= 63)) {
3113 struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
3118 ast_log(
LOG_ERROR,
"Received SSL traffic on RTP instance '%p' without an SSL session\n",
3123 ast_debug_dtls(3,
"(%p) DTLS - __rtp_recvfrom rtp=%p - Got SSL packet '%d'\n", instance, rtp, *in);
3135 dtls_srtp_stop_timeout_timer(instance, rtp, rtcp);
3141 SSL_set_accept_state(dtls->ssl);
3144 BIO_write(dtls->read_bio, buf, len);
3146 len = SSL_read(dtls->ssl, buf, len);
3148 if ((len < 0) && (SSL_get_error(dtls->ssl, len) == SSL_ERROR_SSL)) {
3149 unsigned long error = ERR_get_error();
3150 ast_log(
LOG_ERROR,
"DTLS failure occurred on RTP instance '%p' due to reason '%s', terminating\n",
3151 instance, ERR_reason_error_string(error));
3155 if (SSL_is_init_finished(dtls->ssl)) {
3159 if ((res = dtls_srtp_setup(rtp, instance, rtcp))) {
3165 ast_debug_dtls(3,
"(%p) DTLS - __rtp_recvfrom rtp=%p - established'\n", instance, rtp);
3168 dtls_srtp_start_timeout_timer(instance, rtp, rtcp);
3175 #ifdef HAVE_PJPROJECT 3185 }
else if (rtp->ice) {
3193 pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &combined, &address);
3199 status = pj_ice_sess_on_rx_pkt(ice->
real_ice,
3202 pj_sockaddr_get_len(&address));
3205 if (status != PJ_SUCCESS) {
3208 pj_strerror(status, err_buf,
sizeof(err_buf));
3210 (
int)status, err_buf);
3213 if (!rtp->passthrough) {
3218 if (!rtp->ice_active_remote_candidates && !rtp->ice_proposed_remote_candidates) {
3227 rtp->passthrough = 0;
3252 struct ast_rtp_instance *transport = rtp->bundled ? rtp->bundled : instance;
3259 if (use_srtp && res_srtp && srtp && res_srtp->
protect(srtp, &temp, &len, rtcp) < 0) {
3263 #ifdef HAVE_PJPROJECT 3264 if (transport_rtp->ice) {
3270 if (rtcp && rtp->rtcp->
s == rtp->
s) {
3277 ice = transport_rtp->ice;
3279 if (instance == transport) {
3282 status = pj_ice_sess_send_data(ice->
real_ice, component, temp, len);
3284 if (instance == transport) {
3287 if (status == PJ_SUCCESS) {
3294 res =
ast_sendto(rtcp ? transport_rtp->rtcp->
s : transport_rtp->
s, temp, len, flags, sa);
3305 return __rtp_sendto(instance, buf, size, flags, sa, 1, ice, 1);
3315 if ((res =
__rtp_sendto(instance, buf, size, flags, sa, 0, ice, 1)) > 0) {
3317 rtp->txoctetcount += (res - hdrlen);
3325 unsigned int interval;
3338 double last_sum_of_squares = (*std_dev) * (*std_dev) * (*count ?: 1);
3340 if (++(*count) == 0) {
3352 delta1 = new_sample - *mean;
3353 *mean += (delta1 / *
count);
3354 delta2 = new_sample - *mean;
3357 *std_dev = sqrt((last_sum_of_squares + (delta1 * delta2)) / *count);
3371 setsockopt(sock, SOL_SOCKET, SO_NO_CHECK, &nochecksums,
sizeof(nochecksums));
3405 if (seq == (uint16_t) (info->
max_seq + 1)) {
3456 memset(&rtp->rtp_source_learn.proposed_address, 0,
3457 sizeof(rtp->rtp_source_learn.proposed_address));
3458 rtp->rtp_source_learn.start =
ast_tvnow();
3462 #ifdef HAVE_PJPROJECT 3525 unsigned int count = 0;
3526 struct ifaddrs *ifa, *ia;
3530 int af_inet_ok = 0, af_inet6_ok = 0;
3535 af_inet_ok = af_inet6_ok = 1;
3540 if (getifaddrs(&ifa) < 0) {
3542 ast_log(
LOG_ERROR,
"(%p) ICE Error obtaining list of local addresses: %s\n",
3543 instance, strerror(
errno));
3545 ast_debug_ice(2,
"(%p) ICE add system candidates\n", instance);
3549 for (ia = ifa; ia && count < PJ_ICE_MAX_CAND; ia = ia->ifa_next) {
3552 if (!ia->ifa_addr || (ia->ifa_flags & IFF_UP) == 0) {
3557 if (ia->ifa_addr->sa_family != AF_INET && ia->ifa_addr->sa_family != AF_INET6) {
3563 if (ia->ifa_addr->sa_family == AF_INET) {
3564 const struct sockaddr_in *sa_in = (
struct sockaddr_in*)ia->ifa_addr;
3572 if ((sa_in->sin_addr.s_addr & htonl(0xFF000000)) == htonl(0x7F000000)) {
3577 if ((sa_in->sin_addr.s_addr & htonl(0xFF000000)) == 0) {
3602 pj_sockaddr_set_port(&pjtmp, port);
3604 PJ_ICE_CAND_TYPE_HOST, 65535, &pjtmp, &pjtmp,
NULL,
3605 pj_sockaddr_get_len(&pjtmp));
3631 pj_sockaddr_set_port(&pjtmp, port);
3633 PJ_ICE_CAND_TYPE_HOST, 65535, &pjtmp, &pjtmp,
NULL,
3634 pj_sockaddr_get_len(&pjtmp));
3643 count < PJ_ICE_MAX_CAND) {
3644 struct sockaddr_in answer;
3648 "(%p) ICE request STUN %s %s candidate\n", instance,
3662 pj_sockaddr
ext, base;
3664 int srflx = 1, baseset = 0;
3667 pj_sockaddr_init(pj_AF_INET(), &ext, &mapped, ntohs(answer.sin_port));
3681 if (!pj_sockaddr_cmp(&candidate->
address, &ext)) {
3689 if (srflx && baseset) {
3690 pj_sockaddr_set_port(&base, port);
3692 PJ_ICE_CAND_TYPE_SRFLX, 65535, &ext, &base, &base,
3693 pj_sockaddr_get_len(&ext));
3723 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
3732 return (
unsigned int) ms;
3735 #ifdef HAVE_PJPROJECT 3751 int port,
int replace)
3753 pj_stun_config stun_config;
3754 pj_str_t ufrag, passwd;
3762 rtp->ice_local_candidates =
NULL;
3764 ast_debug_ice(2,
"(%p) ICE create%s\n", instance, replace ?
" and replace" :
"");
3776 stun_config.software_name = pj_str(
NULL);
3779 ufrag = pj_str(rtp->local_ufrag);
3780 passwd = pj_str(rtp->local_passwd);
3785 status = pj_ice_sess_create(&stun_config,
NULL, PJ_ICE_SESS_ROLE_UNKNOWN,
3786 rtp->ice_num_components, &ast_rtp_ice_sess_cb, &ufrag, &passwd,
NULL, &real_ice);
3788 if (status == PJ_SUCCESS) {
3790 real_ice->user_data = instance;
3807 if (replace && rtp->rtcp && rtp->ice_num_components > 1) {
3839 ast_log(
LOG_WARNING,
"Failed to create a new socket for RTP instance '%p'\n", instance);
3851 if (!
ast_bind(rtp->
s, &rtp->bind_address)) {
3852 ast_debug_rtp(1,
"(%p) RTP allocated port %d\n", instance, x);
3864 if (x == startplace || (
errno != EADDRINUSE &&
errno != EACCES)) {
3865 ast_log(
LOG_ERROR,
"Oh dear... we couldn't allocate a port for RTP instance '%p'\n", instance);
3872 #ifdef HAVE_PJPROJECT 3881 rtp->ice_num_components = 2;
3882 ast_debug_ice(2,
"(%p) ICE creating session %s (%d)\n", instance,
3884 if (
ice_create(instance, &rtp->bind_address, x, 0)) {
3893 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 3895 rtp->dtls.timeout_timer = -1;
3903 int saved_rtp_s = rtp->
s;
3904 #ifdef HAVE_PJPROJECT 3906 struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, };
3909 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 3910 ast_rtp_dtls_stop(instance);
3920 if (rtp->rtcp && rtp->rtcp->
s > -1) {
3921 if (saved_rtp_s != rtp->rtcp->
s) {
3922 close(rtp->rtcp->
s);
3927 #ifdef HAVE_PJPROJECT 3935 if (rtp->turn_rtp) {
3936 rtp->turn_state = PJ_TURN_STATE_NULL;
3940 pj_turn_sock_destroy(rtp->turn_rtp);
3942 while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) {
3945 rtp->turn_rtp =
NULL;
3949 if (rtp->turn_rtcp) {
3950 rtp->turn_state = PJ_TURN_STATE_NULL;
3954 pj_turn_sock_destroy(rtp->turn_rtcp);
3956 while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) {
3959 rtp->turn_rtcp =
NULL;
3962 ast_debug_ice(2,
"(%p) ICE RTP transport deallocating\n", instance);
3967 if (rtp->ice_local_candidates) {
3968 ao2_ref(rtp->ice_local_candidates, -1);
3969 rtp->ice_local_candidates =
NULL;
3972 if (rtp->ice_active_remote_candidates) {
3973 ao2_ref(rtp->ice_active_remote_candidates, -1);
3974 rtp->ice_active_remote_candidates =
NULL;
3986 rtp->ioqueue =
NULL;
4008 rtp->expectedseqno = -1;
4026 rtp->transport_wide_cc.schedid = -1;
4031 rtp->stream_num = -1;
4045 #define SSRC_MAPPING_ELEM_CMP(elem, value) ((elem).instance == (value)) 4070 if (rtp->smoother) {
4081 ast_free(rtp->rtcp->local_addr_str);
4095 if (rtp->send_buffer) {
4100 if (rtp->recv_buffer) {
4122 rtp->dtmfmode = dtmf_mode;
4130 return rtp->dtmfmode;
4138 int hdrlen = 12, res = 0, i = 0, payload = 101;
4140 unsigned int *rtpheader = (
unsigned int*)data;
4150 if ((digit <=
'9') && (digit >=
'0')) {
4152 }
else if (digit ==
'*') {
4154 }
else if (digit ==
'#') {
4156 }
else if ((digit >=
'A') && (digit <=
'D')) {
4157 digit = digit -
'A' + 12;
4158 }
else if ((digit >=
'a') && (digit <=
'd')) {
4159 digit = digit -
'a' + 12;
4169 rtp->send_duration = 160;
4171 rtp->lastdigitts = rtp->
lastts + rtp->send_duration;
4174 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno));
4175 rtpheader[1] = htonl(rtp->lastdigitts);
4176 rtpheader[2] = htonl(rtp->
ssrc);
4179 for (i = 0; i < 2; i++) {
4182 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
4183 res =
rtp_sendto(instance, (
void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
4190 ast_verbose(
"Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
4192 ice ?
" (via ICE)" :
"",
4193 payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
4196 rtp->send_duration += 160;
4197 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno));
4201 rtp->sending_digit = 1;
4202 rtp->send_digit =
digit;
4203 rtp->send_payload = payload;
4213 int hdrlen = 12, res = 0;
4215 unsigned int *rtpheader = (
unsigned int*)data;
4226 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
4227 rtpheader[1] = htonl(rtp->lastdigitts);
4228 rtpheader[2] = htonl(rtp->
ssrc);
4229 rtpheader[3] = htonl((rtp->send_digit << 24) | (0xa << 16) | (rtp->send_duration));
4232 res =
rtp_sendto(instance, (
void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
4240 ast_verbose(
"Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
4242 ice ?
" (via ICE)" :
"",
4243 rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
4248 rtp->send_duration += 160;
4259 int hdrlen = 12, res = -1, i = 0;
4261 unsigned int *rtpheader = (
unsigned int*)data;
4262 unsigned int measured_samples;
4272 if ((digit <=
'9') && (digit >=
'0')) {
4274 }
else if (digit ==
'*') {
4276 }
else if (digit ==
'#') {
4278 }
else if ((digit >=
'A') && (digit <=
'D')) {
4279 digit = digit -
'A' + 12;
4280 }
else if ((digit >=
'a') && (digit <=
'd')) {
4281 digit = digit -
'a' + 12;
4290 ast_debug_rtp(2,
"(%p) RTP adjusting final end duration from %d to %u\n",
4291 instance, rtp->send_duration, measured_samples);
4292 rtp->send_duration = measured_samples;
4296 rtpheader[1] = htonl(rtp->lastdigitts);
4297 rtpheader[2] = htonl(rtp->
ssrc);
4298 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
4299 rtpheader[3] |= htonl((1 << 23));
4302 for (i = 0; i < 3; i++) {
4305 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
4307 res =
rtp_sendto(instance, (
void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
4316 ast_verbose(
"Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
4318 ice ?
" (via ICE)" :
"",
4319 rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
4330 if (rtp->smoother) {
4332 rtp->smoother =
NULL;
4335 rtp->sending_digit = 0;
4336 rtp->send_digit = 0;
4354 ast_debug_rtp(3,
"(%p) RTP setting the marker bit due to a source update\n", instance);
4372 ast_debug_rtp(3,
"(%p) RTP changing ssrc from %u to %u due to a source change\n",
4373 instance, rtp->
ssrc, ssrc);
4376 ast_debug_rtp(3,
"(%p) RTP changing ssrc for SRTP from %u to %u\n",
4377 instance, rtp->
ssrc, ssrc);
4379 if (rtcp_srtp != srtp) {
4392 static void timeval2ntp(
struct timeval tv,
unsigned int *msw,
unsigned int *lsw)
4394 unsigned int sec, usec, frac;
4395 sec = tv.tv_sec + 2208988800u;
4416 frac = ((((usec << 12) / 125) << 7) / 125) << 7;
4421 static void ntp2timeval(
unsigned int msw,
unsigned int lsw,
struct timeval *tv)
4423 tv->tv_sec = msw - 2208988800u;
4425 tv->tv_usec = ((((lsw >> 7) * 125) >> 7) * 125) >> 12;
4429 unsigned int *lost_packets,
4432 unsigned int extended_seq_no;
4433 unsigned int expected_packets;
4434 unsigned int expected_interval;
4435 unsigned int received_interval;
4440 expected_packets = extended_seq_no - rtp->seedrxseqno + 1;
4441 if (rtp->rxcount > expected_packets) {
4442 expected_packets += rtp->rxcount - expected_packets;
4444 *lost_packets = expected_packets - rtp->rxcount;
4445 expected_interval = expected_packets - rtp->rtcp->expected_prior;
4446 received_interval = rtp->rxcount - rtp->rtcp->received_prior;
4447 if (received_interval > expected_interval) {
4453 expected_interval = received_interval;
4455 lost_interval = expected_interval - received_interval;
4456 if (expected_interval == 0 || lost_interval <= 0) {
4459 *fraction_lost = (lost_interval << 8) / expected_interval;
4463 rtp->rtcp->received_prior = rtp->rxcount;
4464 rtp->rtcp->expected_prior = expected_packets;
4472 if (lost_interval <= 0) {
4473 rtp->rtcp->rxlost = 0;
4475 rtp->rtcp->rxlost = lost_interval;
4477 if (rtp->rtcp->rxlost_count == 0) {
4478 rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
4480 if (lost_interval && lost_interval < rtp->rtcp->minrxlost) {
4481 rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
4483 if (lost_interval > rtp->rtcp->maxrxlost) {
4484 rtp->rtcp->maxrxlost = rtp->rtcp->rxlost;
4488 &rtp->rtcp->stdev_rxlost, &rtp->rtcp->rxlost_count);
4497 unsigned int now_lsw;
4498 unsigned int now_msw;
4499 unsigned int lost_packets;
4501 struct timeval dlsr = { 0, };
4504 if (!rtp || !rtp->rtcp) {
4517 *sr = rtp->txcount > rtp->rtcp->lastsrtxcount ? 1 : 0;
4522 gettimeofday(&now,
NULL);
4534 report_block =
ast_calloc(1,
sizeof(*report_block));
4535 if (!report_block) {
4545 report_block->
lsr = rtp->rtcp->themrxlsr;
4548 timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
4549 report_block->
dlsr = (((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000;
4586 if (!rtp || !rtp->rtcp) {
4602 rtp->rtcp->sr_count++;
4603 rtp->rtcp->lastsrtxcount = rtp->txcount;
4605 rtp->rtcp->rr_count++;
4609 ast_verbose(
"* Sent RTCP %s to %s%s\n", sr ?
"SR" :
"RR",
4628 ast_verbose(
" DLSR: %4.4f (sec)\n\n", (
double)(report_block->
dlsr / 65536.0));
4634 "from", rtp->rtcp->local_addr_str);
4636 rtcp_report, message_blob);
4646 uint16_t sdes_packet_len_bytes;
4647 uint16_t sdes_packet_len_rounded;
4649 if (!rtp || !rtp->rtcp) {
4661 sdes_packet_len_bytes =
4670 sdes_packet_len_rounded = (sdes_packet_len_bytes + 3) & ~0x3;
4674 rtcpheader[8] = 0x01;
4689 len += (sdes_packet_len_rounded - sdes_packet_len_bytes);
4712 if (res == 0 || res == 1) {
4713 ast_debug_rtcp(1,
"(%p) RTCP failed to generate %s report!\n", instance, sr ?
"SR" :
"RR");
4721 if (res == 0 || res == 1) {
4722 ast_debug_rtcp(1,
"(%p) RTCP failed to generate SDES!\n", instance);
4726 return packet_len + res;
4735 unsigned int fci = 0;
4736 size_t remaining_missing_seqno;
4738 if (!rtp || !rtp->rtcp) {
4751 if (!remaining_missing_seqno) {
4758 while (remaining_missing_seqno) {
4768 if (missing_seqno) {
4770 if (blp_index >= 17) {
4777 if (blp_index == 0) {
4778 fci |= (current_seqno << 16);
4780 fci |= (1 << (blp_index - 1));
4784 remaining_missing_seqno--;
4823 unsigned char *rtcpheader;
4829 if (!rtp || !rtp->rtcp || rtp->rtcp->schedid == -1) {
4839 if (res == 0 || res == 1) {
4851 res =
rtcp_sendto(instance, (
unsigned int *)rtcpheader, packet_len, 0, &remote_address, &ice);
4869 rtp->rtcp->schedid = -1;
4878 unsigned char *cp = p;
4882 datum = (time_msw << 18) & 0x00fc0000;
4883 datum |= (time_lsw >> 14) & 0x0003ffff;
4885 cp[0] = datum >> 16;
4899 #ifdef TEST_FRAMEWORK 4907 if (rtp->sending_digit) {
4911 #ifdef TEST_FRAMEWORK 4930 ast_debug_rtp(3,
"(%p) RTP audio difference is %d, ms is %u\n",
4931 instance,
abs((
int)rtp->
lastts - pred), ms);
4942 if (
abs((
int)rtp->
lastts - pred) < 7200) {
4946 ast_debug_rtp(3,
"(%p) RTP video difference is %d, ms is %u (%u), pred/ts/samples %u/%d/%d\n",
4957 if (
abs((
int)rtp->
lastts - pred) < 7200) {
4961 ast_debug_rtp(3,
"(%p) RTP other difference is %d, ms is %u, pred/ts/samples %u/%d/%d\n",
4975 if (rtp->
lastts > rtp->lastdigitts) {
4976 rtp->lastdigitts = rtp->
lastts;
4984 if (rtp->expectedseqno != -1) {
4986 int difference = frame->
seqno - rtp->expectedseqno;
4993 if (
abs(difference) > 100) {
4998 seqno += difference;
5000 if (difference >= 0) {
5002 rtp->expectedseqno = frame->
seqno + 1;
5003 rtp->seqno += difference;
5007 rtp->expectedseqno = frame->
seqno + 1;
5010 rtp->expectedseqno = -1;
5025 int abs_send_time_id;
5027 unsigned char *rtpheader;
5031 if (abs_send_time_id != -1) {
5037 packet_len = frame->
datalen + hdrlen;
5038 rtpheader = (
unsigned char *)(frame->
data.
ptr - hdrlen);
5040 put_unaligned_uint32(rtpheader, htonl((2 << 30) | (ext << 28) | (codec << 16) | (seqno) | (mark << 23)));
5047 if (abs_send_time_id != -1) {
5048 unsigned int now_msw;
5049 unsigned int now_lsw;
5055 rtpheader[16] = (abs_send_time_id << 4) | 2;
5062 if (rtp->send_buffer) {
5065 payload =
ast_malloc(
sizeof(*payload) + packet_len);
5067 payload->
size = packet_len;
5068 memcpy(payload->
buf, rtpheader, packet_len);
5075 res =
rtp_sendto(instance, (
void *)rtpheader, packet_len, 0, &remote_address, &ice);
5078 ast_debug_rtp(1,
"(%p) RTP transmission error of packet %d to %s: %s\n",
5079 instance, rtp->seqno,
5085 ast_debug(0,
"(%p) RTP NAT: Can't write RTP to private address %s, waiting for other end to send audio...\n",
5090 if (rtp->rtcp && rtp->rtcp->schedid < 0) {
5091 ast_debug_rtcp(1,
"(%p) RTCP starting transmission\n", instance);
5094 if (rtp->rtcp->schedid < 0) {
5102 ast_verbose(
"Sent RTP packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
5104 ice ?
" (via ICE)" :
"",
5105 codec, rtp->seqno, rtp->
lastts, res - hdrlen);
5113 if (seqno == rtp->seqno) {
5128 for (i = 1; i < red->
num_gen+1; i++)
5135 for (i = 0; i < red->
num_gen; i++)
5136 red->
len[i] = red->
len[i+1];
5141 for (i = 0; i < red->
num_gen; i++) {
5142 len += data[i*4+3] = red->
len[i];
5162 unsigned char *rtcpheader;
5163 unsigned char bdata[1024];
5173 if (!rtp || !rtp->rtcp) {
5190 rtp->rtcp->firseq++;
5191 if(rtp->rtcp->firseq == 256) {
5192 rtp->rtcp->firseq = 0;
5200 if (res == 0 || res == 1) {
5212 res =
rtcp_sendto(instance, (
unsigned int *)rtcpheader, packet_len + fir_len, 0, rtp->bundled ? remote_address : &rtp->rtcp->them, &ice);
5225 unsigned char *rtcpheader;
5226 unsigned char bdata[1024];
5237 ast_debug_rtcp(1,
"(%p) RTCP provided feedback frame of format %d to write, but only REMB is supported\n",
5238 instance, feedback->
fmt);
5242 if (!rtp || !rtp->rtcp) {
5248 ast_debug_rtcp(1,
"(%p) RTCP provided feedback REMB report to write, but REMB support not enabled\n",
5265 if (res == 0 || res == 1) {
5275 put_unaligned_uint32(rtcpheader + packet_len + 12, htonl((
'R' << 24) | (
'E' << 16) | (
'M' << 8) | (
'B')));
5278 res =
rtcp_sendto(instance, (
unsigned int *)rtcpheader, packet_len + remb_len, 0, rtp->bundled ? remote_address : &rtp->rtcp->them, &ice);
5300 ast_debug_rtp(1,
"(%p) RTP no remote address on instance, so dropping frame\n", instance);
5317 ast_debug_rtp(1,
"(%p) RTP received frame with no data for instance, so dropping frame\n", instance);
5349 ast_debug_rtp(1,
"(%p) RTP ooh, format changed from %s to %s\n",
5353 if (rtp->smoother) {
5355 rtp->smoother =
NULL;
5370 if (!rtp->smoother) {
5380 if (rtp->smoother) {
5396 if (frame->
offset < hdrlen) {
5418 double current_time;
5424 if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
5425 gettimeofday(&rtp->rxcore,
NULL);
5426 rtp->drxcore = (double) rtp->rxcore.tv_sec + (
double) rtp->rxcore.tv_usec / 1000000;
5428 rtp->seedrxts = timestamp;
5430 rtp->rxcore =
ast_tvsub(rtp->rxcore, tmp);
5432 rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 100;
5435 gettimeofday(&now,
NULL);
5440 prog = (double)((timestamp-rtp->seedrxts)/(float)(rate));
5441 dtv = (double)rtp->drxcore + (
double)(prog);
5442 current_time = (double)now.tv_sec + (
double)now.tv_usec/1000000;
5443 transit = current_time - dtv;
5444 d = transit - rtp->rxtransit;
5445 rtp->rxtransit = transit;
5449 rtp->rxjitter += (1./16.) * (d - rtp->rxjitter);
5451 if (rtp->rxjitter > rtp->rtcp->maxrxjitter)
5452 rtp->rtcp->maxrxjitter = rtp->rxjitter;
5453 if (rtp->rtcp->rxjitter_count == 1)
5454 rtp->rtcp->minrxjitter = rtp->rxjitter;
5455 if (rtp->rtcp && rtp->rxjitter < rtp->rtcp->minrxjitter)
5456 rtp->rtcp->minrxjitter = rtp->rxjitter;
5459 &rtp->rtcp->stdev_rxjitter, &rtp->rtcp->rxjitter_count);
5471 ast_debug_rtp(1,
"(%p) RTP ignore potential DTMF echo from '%s'\n",
5474 rtp->dtmfsamples = 0;
5480 rtp->dtmfsamples = 0;
5484 if (rtp->resp ==
'X') {
5490 ast_debug_rtp(1,
"(%p) RTP creating %s DTMF Frame: %d (%c), at %s\n",
5492 rtp->resp, rtp->resp,