39#include <arpa/nameser.h> 
   50#include <openssl/opensslconf.h> 
   51#include <openssl/opensslv.h> 
   52#if !defined(OPENSSL_NO_SRTP) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) 
   53#include <openssl/ssl.h> 
   54#include <openssl/err.h> 
   55#include <openssl/bio.h> 
   56#if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L) 
   57#include <openssl/bn.h> 
   60#include <openssl/dh.h> 
   67#include <pjlib-util.h> 
   98#define MAX_TIMESTAMP_SKEW  640 
  100#define RTP_SEQ_MOD     (1<<16)  
  101#define RTCP_DEFAULT_INTERVALMS   5000   
  102#define RTCP_MIN_INTERVALMS       500    
  103#define RTCP_MAX_INTERVALMS       60000  
  105#define DEFAULT_RTP_START 5000  
  106#define DEFAULT_RTP_END 31000   
  108#define MINIMUM_RTP_PORT 1024  
  109#define MAXIMUM_RTP_PORT 65535  
  111#define DEFAULT_TURN_PORT 3478 
  113#define TURN_STATE_WAIT_TIME 2000 
  115#define DEFAULT_RTP_SEND_BUFFER_SIZE    250  
  116#define MAXIMUM_RTP_SEND_BUFFER_SIZE    (DEFAULT_RTP_SEND_BUFFER_SIZE + 200)     
  117#define DEFAULT_RTP_RECV_BUFFER_SIZE    20   
  118#define MAXIMUM_RTP_RECV_BUFFER_SIZE    (DEFAULT_RTP_RECV_BUFFER_SIZE + 20)  
  119#define OLD_PACKET_COUNT        1000     
  120#define MISSING_SEQNOS_ADDED_TRIGGER    2    
  122#define SEQNO_CYCLE_OVER        65536    
  125#define RTCP_PT_FUR     192 
  127#define RTCP_PT_SR      AST_RTP_RTCP_SR 
  129#define RTCP_PT_RR      AST_RTP_RTCP_RR 
  131#define RTCP_PT_SDES    202 
  133#define RTCP_PT_BYE     203 
  135#define RTCP_PT_APP     204 
  138#define RTCP_PT_PSFB    AST_RTP_RTCP_PSFB 
  142#define DEFAULT_DTMF_TIMEOUT (150 * (8000 / 1000))   
  144#define ZFONE_PROFILE_ID 0x505a 
  146#define DEFAULT_LEARNING_MIN_SEQUENTIAL 4 
  159#define CALC_LEARNING_MIN_DURATION(count) (((count) - 1) * 9 - 5) 
  160#define DEFAULT_LEARNING_MIN_DURATION CALC_LEARNING_MIN_DURATION(DEFAULT_LEARNING_MIN_SEQUENTIAL) 
  162#define SRTP_MASTER_KEY_LEN 16 
  163#define SRTP_MASTER_SALT_LEN 14 
  164#define SRTP_MASTER_LEN (SRTP_MASTER_KEY_LEN + SRTP_MASTER_SALT_LEN) 
  166#define RTP_DTLS_ESTABLISHED -37 
  187#define STRICT_RTP_LEARN_TIMEOUT    5000 
  189#define DEFAULT_STRICT_RTP STRICT_RTP_YES    
  190#define DEFAULT_SRTP_REPLAY_PROTECTION 1 
  191#define DEFAULT_ICESUPPORT 1 
  192#define DEFAULT_STUN_SOFTWARE_ATTRIBUTE 1 
  193#define DEFAULT_DTLS_MTU 1200 
  203#define RTP_IGNORE_FIRST_PACKETS_COUNT 15 
  219static int nochecksums;
 
  225#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
  231static struct sockaddr_in stunaddr;
 
  232static pj_str_t turnaddr;
 
  234static pj_str_t turnusername;
 
  235static pj_str_t turnpassword;
 
  255static pj_pool_t *pool;
 
  258static pj_timer_heap_t *timer_heap;
 
  261static pj_thread_t *timer_thread;
 
  264static int timer_terminate;
 
  267struct ast_rtp_ioqueue_thread {
 
  273    pj_ioqueue_t *ioqueue;
 
  275    pj_timer_heap_t *timerheap;
 
  288struct ast_ice_host_candidate {
 
  291    unsigned int include_local;
 
  302#define FLAG_3389_WARNING               (1 << 0) 
  303#define FLAG_NAT_ACTIVE                 (3 << 1) 
  304#define FLAG_NAT_INACTIVE               (0 << 1) 
  305#define FLAG_NAT_INACTIVE_NOWARN        (1 << 1) 
  306#define FLAG_NEED_MARKER_BIT            (1 << 3) 
  307#define FLAG_DTMF_COMPENSATE            (1 << 4) 
  308#define FLAG_REQ_LOCAL_BRIDGE_BIT       (1 << 5) 
  310#define TRANSPORT_SOCKET_RTP 0 
  311#define TRANSPORT_SOCKET_RTCP 1 
  312#define TRANSPORT_TURN_RTP 2 
  313#define TRANSPORT_TURN_RTCP 3 
  326#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
  340    pj_ice_sess *real_ice;           
 
  484    struct ice_wrap *ice;       
 
  486    pj_turn_sock *turn_rtp;     
 
  487    pj_turn_sock *turn_rtcp;    
 
  488    pj_turn_state_t turn_state; 
 
  489    unsigned int passthrough:1; 
 
  490    unsigned int rtp_passthrough:1; 
 
  491    unsigned int rtcp_passthrough:1; 
 
  492    unsigned int ice_port;      
 
  496    struct ast_rtp_ioqueue_thread *ioqueue; 
 
  498    char remote_ufrag[257];  
 
  499    char remote_passwd[257]; 
 
  501    char local_ufrag[257];  
 
  502    char local_passwd[257]; 
 
  508    unsigned int ice_num_components; 
 
  509    unsigned int ice_media_started:1; 
 
  512#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
  517    char local_fingerprint[160]; 
 
  519    unsigned char remote_fingerprint[EVP_MAX_MD_SIZE]; 
 
  522    struct dtls_details dtls; 
 
 
  604#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
  605    struct dtls_details dtls; 
 
 
  673#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
  677static int dtls_bio_write(BIO *bio, 
const char *
buf, 
int len);
 
  678static long dtls_bio_ctrl(BIO *bio, 
int cmd, 
long arg1, 
void *arg2);
 
  679static int dtls_bio_new(BIO *bio);
 
  680static int dtls_bio_free(BIO *bio);
 
  682#ifndef HAVE_OPENSSL_BIO_METHOD 
  683static BIO_METHOD dtls_bio_methods = {
 
  684    .type = BIO_TYPE_BIO,
 
  686    .bwrite = dtls_bio_write,
 
  687    .ctrl = dtls_bio_ctrl,
 
  688    .create = dtls_bio_new,
 
  689    .destroy = dtls_bio_free,
 
  692static BIO_METHOD *dtls_bio_methods;
 
  699static void stunaddr_resolve_callback(
const struct ast_dns_query *query);
 
  700static int store_stunaddr_resolved(
const struct ast_dns_query *query);
 
  703#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
  704static int dtls_bio_new(BIO *bio)
 
  706#ifdef HAVE_OPENSSL_BIO_METHOD 
  707    BIO_set_init(bio, 1);
 
  708    BIO_set_data(bio, 
NULL);
 
  709    BIO_set_shutdown(bio, 0);
 
  718static int dtls_bio_free(BIO *bio)
 
  724#ifdef HAVE_OPENSSL_BIO_METHOD 
  725    BIO_set_data(bio, 
NULL);
 
  732static int dtls_bio_write(BIO *bio, 
const char *
buf, 
int len)
 
  734#ifdef HAVE_OPENSSL_BIO_METHOD 
  749    if (rtp->
rtcp && rtp->
rtcp->dtls.write_bio == bio) {
 
  760    bytes_sent = 
__rtp_sendto(instance, (
char *)
buf, 
len, 0, &remote_address, rtcp, &ice, 0);
 
  763        ast_debug(0, 
"(%p) DTLS - sent %s packet to %s%s (len %-6.6d)\n",
 
  765            ice ? 
" (via ICE)" : 
"", bytes_sent);
 
  771static long dtls_bio_ctrl(BIO *bio, 
int cmd, 
long arg1, 
void *arg2)
 
  776    case BIO_CTRL_DGRAM_QUERY_MTU:
 
  778    case BIO_CTRL_WPENDING:
 
  779    case BIO_CTRL_PENDING:
 
  790static void host_candidate_overrides_clear(
void)
 
  792    struct ast_ice_host_candidate *candidate;
 
  807    char address[PJ_INET6_ADDRSTRLEN];
 
  809    if (component < 1 || !ice->comp[component - 1].valid_check) {
 
  814        pj_sockaddr_print(&ice->comp[component - 1].valid_check->rcand->addr, 
address,
 
  817        pj_sockaddr_get_port(&ice->comp[component - 1].valid_check->rcand->addr));
 
  821static void ast_rtp_ice_candidate_destroy(
void *obj)
 
  835static void ast_rtp_ice_set_authentication(
struct ast_rtp_instance *instance, 
const char *ufrag, 
const char *password)
 
  838    int ice_attrb_reset = 0;
 
  841        if (!
ast_strlen_zero(rtp->remote_ufrag) && strcmp(ufrag, rtp->remote_ufrag)) {
 
  848        if (!
ast_strlen_zero(rtp->remote_passwd) && strcmp(password, rtp->remote_passwd)) {
 
  851        ast_copy_string(rtp->remote_passwd, password, 
sizeof(rtp->remote_passwd));
 
  855    if (ice_attrb_reset) {
 
  861static int ice_candidate_cmp(
void *obj, 
void *arg, 
int flags)
 
  865    if (strcmp(candidate1->
foundation, candidate2->foundation) ||
 
  866            candidate1->
id != candidate2->id ||
 
  867            candidate1->
type != candidate2->type ||
 
  882    if (strcasecmp(candidate->
transport, 
"udp")) {
 
  886    if (!rtp->ice_proposed_remote_candidates) {
 
  889        if (!rtp->ice_proposed_remote_candidates) {
 
  899    if (!(remote_candidate = 
ao2_alloc(
sizeof(*remote_candidate), ast_rtp_ice_candidate_destroy))) {
 
  904    remote_candidate->
id = candidate->
id;
 
  909    remote_candidate->
type = candidate->
type;
 
  911    ast_debug_ice(2, 
"(%p) ICE add remote candidate\n", instance);
 
  913    ao2_link(rtp->ice_proposed_remote_candidates, remote_candidate);
 
  920static void pj_thread_register_check(
void)
 
  922    pj_thread_desc *
desc;
 
  925    if (pj_thread_is_registered() == PJ_TRUE) {
 
  931        ast_log(
LOG_ERROR, 
"Could not get thread desc from thread-local storage. Expect awful things to occur\n");
 
  936    if (pj_thread_register(
"Asterisk Thread", *
desc, &
thread) != PJ_SUCCESS) {
 
  949    struct ice_wrap *ice;
 
  971static void ice_wrap_dtor(
void *vdoomed)
 
  973    struct ice_wrap *ice = vdoomed;
 
  976        pj_thread_register_check();
 
  978        pj_ice_sess_destroy(ice->real_ice);
 
  982static void ast2pj_rtp_ice_role(
enum ast_rtp_ice_role ast_role, 
enum pj_ice_sess_role *pj_role)
 
  986        *pj_role = PJ_ICE_SESS_ROLE_CONTROLLED;
 
  989        *pj_role = PJ_ICE_SESS_ROLE_CONTROLLING;
 
  994static void pj2ast_rtp_ice_role(
enum pj_ice_sess_role pj_role, 
enum ast_rtp_ice_role *ast_role)
 
  997    case PJ_ICE_SESS_ROLE_CONTROLLED:
 
 1000    case PJ_ICE_SESS_ROLE_CONTROLLING:
 
 1003    case PJ_ICE_SESS_ROLE_UNKNOWN:
 
 1020    if (!rtp->ice->real_ice->is_nominating && !rtp->ice->real_ice->is_complete) {
 
 1021        ast_debug_ice(3, 
" (%p) ICE nevermind, not ready for a reset\n", instance);
 
 1025    ast_debug_ice(3, 
"(%p) ICE recreating ICE session %s (%d)\n",
 
 1027    res = ice_create(instance, &rtp->ice_original_rtp_addr, rtp->ice_port, 1);
 
 1030        enum pj_ice_sess_role role = PJ_ICE_SESS_ROLE_UNKNOWN;
 
 1031        ast2pj_rtp_ice_role(rtp->role, &role);
 
 1032        pj_ice_sess_change_role(rtp->ice->real_ice, role);
 
 1038    if (rtp->ice_num_components == 1 && rtp->turn_rtcp) {
 
 1040        struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, };
 
 1042        rtp->turn_state = PJ_TURN_STATE_NULL;
 
 1046        pj_turn_sock_destroy(rtp->turn_rtcp);
 
 1048        while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) {
 
 1053    rtp->ice_media_started = 0;
 
 1071        if (!left_candidate) {
 
 1089    pj_str_t ufrag = pj_str(rtp->remote_ufrag), passwd = pj_str(rtp->remote_passwd);
 
 1090    pj_ice_sess_cand candidates[PJ_ICE_MAX_CAND];
 
 1093    int cand_cnt = 0, has_rtp = 0, has_rtcp = 0;
 
 1095    if (!rtp->ice || !rtp->ice_proposed_remote_candidates) {
 
 1100    if (rtp->ice_active_remote_candidates &&
 
 1101            !ice_candidates_compare(rtp->ice_proposed_remote_candidates, rtp->ice_active_remote_candidates)) {
 
 1102        ast_debug_ice(2, 
"(%p) ICE proposed equals active candidates\n", instance);
 
 1104        rtp->ice_proposed_remote_candidates = 
NULL;
 
 1106        pj2ast_rtp_ice_role(rtp->ice->real_ice->role, &rtp->role);
 
 1112    rtp->ice_active_remote_candidates = rtp->ice_proposed_remote_candidates;
 
 1113    rtp->ice_proposed_remote_candidates = 
NULL;
 
 1118    if (ice_reset_session(instance)) {
 
 1119        ast_log(
LOG_NOTICE, 
"(%p) ICE failed to create replacement session\n", instance);
 
 1123    pj_thread_register_check();
 
 1134        pj_strdup2(rtp->ice->real_ice->pool, &candidates[cand_cnt].foundation,
 
 1136        candidates[cand_cnt].comp_id = candidate->
id;
 
 1137        candidates[cand_cnt].prio = candidate->
priority;
 
 1146            candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_HOST;
 
 1148            candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_SRFLX;
 
 1150            candidates[cand_cnt].type = PJ_ICE_CAND_TYPE_RELAYED;
 
 1157            pj_turn_sock_set_perm(rtp->turn_rtp, 1, &candidates[cand_cnt].addr, 1);
 
 1163            pj_turn_sock_set_perm(rtp->turn_rtcp, 1, &candidates[cand_cnt].addr, 1);
 
 1174        ast_log(
LOG_WARNING, 
"(%p) ICE lost %d candidates. Consider increasing PJ_ICE_MAX_CAND in PJSIP\n",
 
 1183    if (!has_rtcp && rtp->ice_num_components > 1) {
 
 1187    if (rtp->ice && has_rtp && (has_rtcp || rtp->ice_num_components == 1)) {
 
 1190        struct ice_wrap *ice;
 
 1196        res = pj_ice_sess_create_check_list(ice->real_ice, &ufrag, &passwd, cand_cnt, &candidates[0]);
 
 1197        if (res == PJ_SUCCESS) {
 
 1198            ast_debug_ice(2, 
"(%p) ICE successfully created checklist\n", instance);
 
 1200            pj_ice_sess_start_check(ice->real_ice);
 
 1201            pj_timer_heap_poll(timer_heap, 
NULL);
 
 1210        pj_strerror(res, reason, 
sizeof(reason));
 
 1211        ast_log(
LOG_WARNING, 
"(%p) ICE failed to create session check list: %s\n", instance, reason);
 
 1220    ao2_ref(rtp->ice_active_remote_candidates, -1);
 
 1221    rtp->ice_active_remote_candidates = 
NULL;
 
 1223        rtp->ice->real_ice->rcand_cnt = rtp->ice->real_ice->clist.count = 0;
 
 1232    return rtp->local_ufrag;
 
 1236static const char *ast_rtp_ice_get_password(
struct ast_rtp_instance *instance)
 
 1240    return rtp->local_passwd;
 
 1248    if (rtp->ice_local_candidates) {
 
 1249        ao2_ref(rtp->ice_local_candidates, +1);
 
 1252    return rtp->ice_local_candidates;
 
 1264    pj_thread_register_check();
 
 1266    pj_ice_sess_change_role(rtp->ice->real_ice, PJ_ICE_SESS_ROLE_CONTROLLING);
 
 1275        ast_debug_ice(3, 
"(%p) ICE set role failed; no ice instance\n", instance);
 
 1281    if (!rtp->ice->real_ice->is_nominating && !rtp->ice->real_ice->is_complete) {
 
 1282        pj_thread_register_check();
 
 1286            PJ_ICE_SESS_ROLE_CONTROLLED : PJ_ICE_SESS_ROLE_CONTROLLING);
 
 1288        ast_debug_ice(2, 
"(%p) ICE not setting role because state is %s\n",
 
 1289            instance, rtp->ice->real_ice->is_nominating ? 
"nominating" : 
"complete");
 
 1295    unsigned comp_id, 
unsigned transport_id, pj_ice_cand_type 
type, pj_uint16_t local_pref,
 
 1296    const pj_sockaddr_t *addr, 
const pj_sockaddr_t *base_addr, 
const pj_sockaddr_t *rel_addr,
 
 1299    pj_str_t foundation;
 
 1301    struct ice_wrap *ice;
 
 1302    char address[PJ_INET6_ADDRSTRLEN];
 
 1309    pj_thread_register_check();
 
 1311    pj_ice_calc_foundation(rtp->ice->real_ice->pool, &foundation, 
type, addr);
 
 1313    if (!rtp->ice_local_candidates) {
 
 1315            NULL, ice_candidate_cmp);
 
 1316        if (!rtp->ice_local_candidates) {
 
 1321    if (!(candidate = 
ao2_alloc(
sizeof(*candidate), ast_rtp_ice_candidate_destroy))) {
 
 1326    candidate->
id = comp_id;
 
 1337    if (
type == PJ_ICE_CAND_TYPE_HOST) {
 
 1339    } 
else if (
type == PJ_ICE_CAND_TYPE_SRFLX) {
 
 1341    } 
else if (
type == PJ_ICE_CAND_TYPE_RELAYED) {
 
 1355    status = pj_ice_sess_add_cand(ice->real_ice, comp_id, transport_id, 
type, local_pref,
 
 1356        &foundation, addr, base_addr, rel_addr, addr_len, 
NULL);
 
 1359    if (!rtp->ice || 
status != PJ_SUCCESS) {
 
 1367    candidate->
priority = rtp->ice->real_ice->lcand[rtp->ice->real_ice->lcand_cnt - 1].prio;
 
 1372    ao2_link(rtp->ice_local_candidates, candidate);
 
 1377static 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)
 
 1381    struct ice_wrap *ice;
 
 1392        if (
status != PJ_SUCCESS) {
 
 1400        if (!rtp->rtp_passthrough) {
 
 1403        rtp->rtp_passthrough = 0;
 
 1406    ast_sendto(rtp->
s, pkt, pkt_len, 0, &rtp->rtp_loop);
 
 1410static void ast_rtp_on_turn_rtp_state(pj_turn_sock *turn_sock, pj_turn_state_t old_state, pj_turn_state_t new_state)
 
 1425    rtp->turn_state = new_state;
 
 1428    if (new_state == PJ_TURN_STATE_DESTROYING) {
 
 1429        pj_turn_sock_set_user_data(rtp->turn_rtp, 
NULL);
 
 1430        rtp->turn_rtp = 
NULL;
 
 1437static pj_turn_sock_cb ast_rtp_turn_rtp_sock_cb = {
 
 1438    .on_rx_data = ast_rtp_on_turn_rx_rtp_data,
 
 1439    .on_state = ast_rtp_on_turn_rtp_state,
 
 1443static 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)
 
 1447    struct ice_wrap *ice;
 
 1458        if (
status != PJ_SUCCESS) {
 
 1466        if (!rtp->rtcp_passthrough) {
 
 1469        rtp->rtcp_passthrough = 0;
 
 1476static void ast_rtp_on_turn_rtcp_state(pj_turn_sock *turn_sock, pj_turn_state_t old_state, pj_turn_state_t new_state)
 
 1491    rtp->turn_state = new_state;
 
 1494    if (new_state == PJ_TURN_STATE_DESTROYING) {
 
 1495        pj_turn_sock_set_user_data(rtp->turn_rtcp, 
NULL);
 
 1496        rtp->turn_rtcp = 
NULL;
 
 1503static pj_turn_sock_cb ast_rtp_turn_rtcp_sock_cb = {
 
 1504    .on_rx_data = ast_rtp_on_turn_rx_rtcp_data,
 
 1505    .on_state = ast_rtp_on_turn_rtcp_state,
 
 1509static int ioqueue_worker_thread(
void *data)
 
 1511    struct ast_rtp_ioqueue_thread *ioqueue = data;
 
 1513    while (!ioqueue->terminate) {
 
 1514        const pj_time_val delay = {0, 10};
 
 1516        pj_ioqueue_poll(ioqueue->ioqueue, &delay);
 
 1518        pj_timer_heap_poll(ioqueue->timerheap, 
NULL);
 
 1525static void rtp_ioqueue_thread_destroy(
struct ast_rtp_ioqueue_thread *ioqueue)
 
 1527    if (ioqueue->thread) {
 
 1528        ioqueue->terminate = 1;
 
 1529        pj_thread_join(ioqueue->thread);
 
 1530        pj_thread_destroy(ioqueue->thread);
 
 1533    if (ioqueue->pool) {
 
 1537        pj_pool_t *temp_pool = ioqueue->pool;
 
 1539        ioqueue->pool = 
NULL;
 
 1540        pj_pool_release(temp_pool);
 
 1547static void rtp_ioqueue_thread_remove(
struct ast_rtp_ioqueue_thread *ioqueue)
 
 1553    if ((ioqueue->count -= 2) == 0) {
 
 1563    rtp_ioqueue_thread_destroy(ioqueue);
 
 1567static struct ast_rtp_ioqueue_thread *rtp_ioqueue_thread_get_or_create(
void)
 
 1569    struct ast_rtp_ioqueue_thread *ioqueue;
 
 1576        if ((ioqueue->count + 2) < PJ_IOQUEUE_MAX_HANDLES) {
 
 1583        ioqueue->count += 2;
 
 1592    ioqueue->pool = pj_pool_create(&
cachingpool.factory, 
"rtp", 512, 512, 
NULL);
 
 1597    if (pj_timer_heap_create(ioqueue->pool, 4, &ioqueue->timerheap) != PJ_SUCCESS) {
 
 1601    if (pj_lock_create_recursive_mutex(ioqueue->pool, 
"rtp%p", &
lock) != PJ_SUCCESS) {
 
 1605    pj_timer_heap_set_lock(ioqueue->timerheap, 
lock, PJ_TRUE);
 
 1607    if (pj_ioqueue_create(ioqueue->pool, PJ_IOQUEUE_MAX_HANDLES, &ioqueue->ioqueue) != PJ_SUCCESS) {
 
 1611    if (pj_thread_create(ioqueue->pool, 
"ice", &ioqueue_worker_thread, ioqueue, 0, 0, &ioqueue->thread) != PJ_SUCCESS) {
 
 1623    rtp_ioqueue_thread_destroy(ioqueue);
 
 1633        enum ast_transport transport, 
const char *server, 
unsigned int port, 
const char *username, 
const char *password)
 
 1636    pj_turn_sock **turn_sock;
 
 1637    const pj_turn_sock_cb *turn_cb;
 
 1638    pj_turn_tp_type conn_type;
 
 1640    pj_stun_auth_cred cred = { 0, };
 
 1643    pj_stun_config stun_config;
 
 1645    struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, };
 
 1646    pj_turn_session_info 
info;
 
 1649    pj_turn_sock_cfg turn_sock_cfg;
 
 1650    struct ice_wrap *ice;
 
 1661        turn_sock = &rtp->turn_rtp;
 
 1662        turn_cb = &ast_rtp_turn_rtp_sock_cb;
 
 1666        turn_sock = &rtp->turn_rtcp;
 
 1667        turn_cb = &ast_rtp_turn_rtcp_sock_cb;
 
 1675        conn_type = PJ_TURN_TP_UDP;
 
 1677        conn_type = PJ_TURN_TP_TCP;
 
 1686        rtp->turn_state = PJ_TURN_STATE_NULL;
 
 1690        pj_turn_sock_destroy(*turn_sock);
 
 1692        while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) {
 
 1704        rtp->ioqueue = rtp_ioqueue_thread_get_or_create();
 
 1706        if (!rtp->ioqueue) {
 
 1711    pj_stun_config_init(&stun_config, &
cachingpool.factory, 0, rtp->ioqueue->ioqueue, rtp->ioqueue->timerheap);
 
 1712    if (!stun_software_attribute) {
 
 1713        stun_config.software_name = pj_str(
NULL);
 
 1717    pj_turn_sock_cfg_default(&turn_sock_cfg);
 
 1720        turn_sock_cfg.grp_lock = ice->real_ice->grp_lock;
 
 1726    status = pj_turn_sock_create(&stun_config,
 
 1728        turn_cb, &turn_sock_cfg, instance, turn_sock);
 
 1730    if (
status != PJ_SUCCESS) {
 
 1736    cred.type = PJ_STUN_AUTH_CRED_STATIC;
 
 1737    pj_strset2(&cred.data.static_cred.username, (
char*)username);
 
 1738    cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN;
 
 1739    pj_strset2(&cred.data.static_cred.data, (
char*)password);
 
 1741    pj_turn_sock_alloc(*turn_sock, pj_cstr(&turn_addr, server), port, 
NULL, &cred, 
NULL);
 
 1743    ast_debug_ice(2, 
"(%p) ICE request TURN %s %s candidate\n", instance,
 
 1753    while (rtp->turn_state < PJ_TURN_STATE_READY) {
 
 1758    if (rtp->turn_state != PJ_TURN_STATE_READY) {
 
 1762    pj_turn_sock_get_info(*turn_sock, &info);
 
 1764    ast_rtp_ice_add_cand(instance, rtp, component, conn_transport,
 
 1765        PJ_ICE_CAND_TYPE_RELAYED, 65535, &
info.relay_addr, &
info.relay_addr,
 
 1766        &
info.mapped_addr, pj_sockaddr_get_len(&
info.relay_addr));
 
 1780        for (x=0; x<4; x++) {
 
 1783        snprintf(
buf, size, 
"%08lx%08lx%08lx%08lx", (
long unsigned)
val[0], (
long unsigned)
val[1], (
long unsigned)
val[2], (
long unsigned)
val[3]);
 
 1789static void ast_rtp_ice_change_components(
struct ast_rtp_instance *instance, 
int num_components)
 
 1796    if (!icesupport || !rtp->ice || rtp->ice_num_components == num_components) {
 
 1800    ast_debug_ice(2, 
"(%p) ICE change number of components %u -> %u\n", instance,
 
 1801        rtp->ice_num_components, num_components);
 
 1803    rtp->ice_num_components = num_components;
 
 1804    ice_reset_session(instance);
 
 1823#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
 1833    dtls->dtls_setup = setup;
 
 1835    if (!(dtls->ssl = 
SSL_new(ssl_ctx))) {
 
 1846#ifdef HAVE_OPENSSL_BIO_METHOD 
 1848        ast_log(
LOG_ERROR, 
"Failed to allocate memory for outbound SSL traffic\n");
 
 1855        ast_log(
LOG_ERROR, 
"Failed to allocate memory for outbound SSL traffic\n");
 
 1858    dtls->write_bio->ptr = instance;
 
 1860    SSL_set_bio(dtls->ssl, dtls->read_bio, dtls->write_bio);
 
 1872    if (dtls->read_bio) {
 
 1874        dtls->read_bio = 
NULL;
 
 1877    if (dtls->write_bio) {
 
 1879        dtls->write_bio = 
NULL;
 
 1893    if (!rtp->ssl_ctx || !rtp->
rtcp) {
 
 1898    return dtls_details_initialize(&rtp->
rtcp->dtls, rtp->ssl_ctx, rtp->dtls.dtls_setup, instance);
 
 1901static const SSL_METHOD *get_dtls_method(
void)
 
 1903#if OPENSSL_VERSION_NUMBER < 0x10002000L 
 1904    return DTLSv1_method();
 
 1906    return DTLS_method();
 
 1910struct dtls_cert_info {
 
 1911    EVP_PKEY *private_key;
 
 1915static int apply_dh_params(
SSL_CTX *ctx, BIO *bio)
 
 1919#if OPENSSL_VERSION_NUMBER >= 0x30000000L 
 1920    EVP_PKEY *dhpkey = PEM_read_bio_Parameters(bio, 
NULL);
 
 1921    if (dhpkey && EVP_PKEY_is_a(dhpkey, 
"DH")) {
 
 1922        res = SSL_CTX_set0_tmp_dh_pkey(ctx, dhpkey);
 
 1928        EVP_PKEY_free(dhpkey);
 
 1933        res = SSL_CTX_set_tmp_dh(ctx, dh);
 
 1943#if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L) && (OPENSSL_VERSION_NUMBER < 0x10100000L) 
 1947#ifndef OPENSSL_NO_DH 
 1949        BIO *bio = BIO_new_file(dtls_cfg->
pvtfile, 
"r");
 
 1951            if (apply_dh_params(rtp->ssl_ctx, bio)) {
 
 1952                long options = SSL_OP_CIPHER_SERVER_PREFERENCE |
 
 1953                    SSL_OP_SINGLE_DH_USE | SSL_OP_SINGLE_ECDH_USE;
 
 1955                ast_verb(2, 
"DTLS DH initialized, PFS enabled\n");
 
 1962#if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L) && (OPENSSL_VERSION_NUMBER < 0x10100000L) 
 1964    ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
 
 1966        if (SSL_CTX_set_tmp_ecdh(rtp->ssl_ctx, ecdh)) {
 
 1967            #ifndef SSL_CTRL_SET_ECDH_AUTO 
 1968                #define SSL_CTRL_SET_ECDH_AUTO 94 
 1971            if (SSL_CTX_ctrl(rtp->ssl_ctx, SSL_CTRL_SET_ECDH_AUTO, 1, 
NULL)) {
 
 1972                ast_verb(2, 
"DTLS ECDH initialized (automatic), faster PFS enabled\n");
 
 1974                ast_verb(2, 
"DTLS ECDH initialized (secp256r1), faster PFS enabled\n");
 
 1982#if !defined(OPENSSL_NO_ECDH) && (OPENSSL_VERSION_NUMBER >= 0x10000000L) 
 1984static int create_ephemeral_ec_keypair(EVP_PKEY **keypair)
 
 1986#if OPENSSL_VERSION_NUMBER >= 0x30000000L 
 1987    *keypair = EVP_EC_gen(SN_X9_62_prime256v1);
 
 1988    return *keypair ? 0 : -1;
 
 1990    EC_KEY *eckey = 
NULL;
 
 1993    group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
 
 1998    EC_GROUP_set_asn1_flag(
group, OPENSSL_EC_NAMED_CURVE);
 
 1999    EC_GROUP_set_point_conversion_form(
group, POINT_CONVERSION_UNCOMPRESSED);
 
 2001    eckey = EC_KEY_new();
 
 2006    if (!EC_KEY_set_group(eckey, 
group)) {
 
 2010    if (!EC_KEY_generate_key(eckey)) {
 
 2014    *keypair = EVP_PKEY_new();
 
 2019    EVP_PKEY_assign_EC_KEY(*keypair, eckey);
 
 2020    EC_GROUP_free(
group);
 
 2026    EC_GROUP_free(
group);
 
 2033#define SERIAL_RAND_BITS 159 
 2035static int create_ephemeral_certificate(EVP_PKEY *keypair, X509 **certificate)
 
 2038    BIGNUM *serial = 
NULL;
 
 2046    if (!X509_set_version(cert, 2)) {
 
 2051    X509_set_pubkey(cert, keypair);
 
 2054    if (!(serial = BN_new())
 
 2055       || !BN_rand(serial, SERIAL_RAND_BITS, -1, 0)
 
 2056       || !BN_to_ASN1_INTEGER(serial, X509_get_serialNumber(cert))) {
 
 2067#if OPENSSL_VERSION_NUMBER < 0x10100000L 
 2068    if (!X509_time_adj_ex(X509_get_notBefore(cert), -1, 0, 
NULL)
 
 2069       || !X509_time_adj_ex(X509_get_notAfter(cert), 30, 0, 
NULL)) {
 
 2073    if (!X509_time_adj_ex(X509_getm_notBefore(cert), -1, 0, 
NULL)
 
 2074       || !X509_time_adj_ex(X509_getm_notAfter(cert), 30, 0, 
NULL)) {
 
 2080    if (!(
name = X509_get_subject_name(cert))
 
 2081       || !X509_NAME_add_entry_by_NID(
name, NID_commonName, MBSTRING_ASC,
 
 2082                                      (
unsigned char *) 
"asterisk", -1, -1, 0)
 
 2083       || !X509_set_issuer_name(cert, 
name)) {
 
 2088    if (!X509_sign(cert, keypair, EVP_sha256())) {
 
 2092    *certificate = cert;
 
 2104                                        struct dtls_cert_info *cert_info)
 
 2107    cert_info->private_key = 
NULL;
 
 2108    cert_info->certificate = 
NULL;
 
 2110    if (create_ephemeral_ec_keypair(&cert_info->private_key)) {
 
 2115    if (create_ephemeral_certificate(cert_info->private_key, &cert_info->certificate)) {
 
 2123    X509_free(cert_info->certificate);
 
 2124    EVP_PKEY_free(cert_info->private_key);
 
 2133                                        struct dtls_cert_info *cert_info)
 
 2135    ast_log(
LOG_ERROR, 
"Your version of OpenSSL does not support ECDSA keys\n");
 
 2143                                        struct dtls_cert_info *cert_info)
 
 2146    BIO *certbio = 
NULL;
 
 2147    EVP_PKEY *private_key = 
NULL;
 
 2151    fp = fopen(private_key_file, 
"r");
 
 2153        ast_log(
LOG_ERROR, 
"Failed to read private key from file '%s': %s\n", private_key_file, strerror(
errno));
 
 2157    if (!PEM_read_PrivateKey(fp, &private_key, 
NULL, 
NULL)) {
 
 2158        ast_log(
LOG_ERROR, 
"Failed to read private key from PEM file '%s'\n", private_key_file);
 
 2164        ast_log(
LOG_ERROR, 
"Failed to close private key file '%s': %s\n", private_key_file, strerror(
errno));
 
 2168    certbio = BIO_new(BIO_s_file());
 
 2170        ast_log(
LOG_ERROR, 
"Failed to allocate memory for certificate fingerprinting on RTP instance '%p'\n",
 
 2175    if (!BIO_read_filename(certbio, dtls_cfg->
certfile)
 
 2176       || !(cert = PEM_read_bio_X509(certbio, 
NULL, 0, 
NULL))) {
 
 2181    cert_info->private_key = private_key;
 
 2182    cert_info->certificate = cert;
 
 2184    BIO_free_all(certbio);
 
 2190    BIO_free_all(certbio);
 
 2191    EVP_PKEY_free(private_key);
 
 2198                                 struct dtls_cert_info *cert_info)
 
 2201        return create_certificate_ephemeral(instance, dtls_cfg, cert_info);
 
 2203        return create_certificate_from_file(instance, dtls_cfg, cert_info);
 
 2213    struct dtls_cert_info cert_info = { 0 };
 
 2223        ast_log(
LOG_ERROR, 
"SRTP support module is not loaded or available. Try loading res_srtp.so.\n");
 
 2231    rtp->ssl_ctx = SSL_CTX_new(get_dtls_method());
 
 2232    if (!rtp->ssl_ctx) {
 
 2236    SSL_CTX_set_read_ahead(rtp->ssl_ctx, 1);
 
 2238    configure_dhparams(rtp, dtls_cfg);
 
 2240    rtp->dtls_verify = dtls_cfg->
verify;
 
 2244        dtls_verify_callback : 
NULL);
 
 2247        SSL_CTX_set_tlsext_use_srtp(rtp->ssl_ctx, 
"SRTP_AES128_CM_SHA1_80");
 
 2249        SSL_CTX_set_tlsext_use_srtp(rtp->ssl_ctx, 
"SRTP_AES128_CM_SHA1_32");
 
 2251        ast_log(
LOG_ERROR, 
"Unsupported suite specified for DTLS-SRTP on RTP instance '%p'\n", instance);
 
 2255    rtp->local_hash = dtls_cfg->
hash;
 
 2257    if (!load_dtls_certificate(instance, dtls_cfg, &cert_info)) {
 
 2259        unsigned int size, i;
 
 2260        unsigned char fingerprint[EVP_MAX_MD_SIZE];
 
 2261        char *local_fingerprint = rtp->local_fingerprint;
 
 2263        if (!SSL_CTX_use_certificate(rtp->ssl_ctx, cert_info.certificate)) {
 
 2264            ast_log(
LOG_ERROR, 
"Specified certificate for RTP instance '%p' could not be used\n",
 
 2269        if (!SSL_CTX_use_PrivateKey(rtp->ssl_ctx, cert_info.private_key)
 
 2270            || !SSL_CTX_check_private_key(rtp->ssl_ctx)) {
 
 2271            ast_log(
LOG_ERROR, 
"Specified private key for RTP instance '%p' could not be used\n",
 
 2279            type = EVP_sha256();
 
 2281            ast_log(
LOG_ERROR, 
"Unsupported fingerprint hash type on RTP instance '%p'\n",
 
 2286        if (!X509_digest(cert_info.certificate, 
type, fingerprint, &size) || !size) {
 
 2287            ast_log(
LOG_ERROR, 
"Could not produce fingerprint from certificate for RTP instance '%p'\n",
 
 2292        for (i = 0; i < size; i++) {
 
 2293            sprintf(local_fingerprint, 
"%02hhX:", fingerprint[i]);
 
 2294            local_fingerprint += 3;
 
 2297        *(local_fingerprint - 1) = 0;
 
 2299        EVP_PKEY_free(cert_info.private_key);
 
 2300        X509_free(cert_info.certificate);
 
 2304        if (!SSL_CTX_set_cipher_list(rtp->ssl_ctx, dtls_cfg->
cipher)) {
 
 2305            ast_log(
LOG_ERROR, 
"Invalid cipher specified in cipher list '%s' for RTP instance '%p'\n",
 
 2306                dtls_cfg->
cipher, instance);
 
 2313            ast_log(
LOG_ERROR, 
"Invalid certificate authority file '%s' or path '%s' specified for RTP instance '%p'\n",
 
 2319    rtp->rekey = dtls_cfg->
rekey;
 
 2320    rtp->suite = dtls_cfg->
suite;
 
 2322    res = dtls_details_initialize(&rtp->dtls, rtp->ssl_ctx, dtls_cfg->
default_setup, instance);
 
 2324        dtls_setup_rtcp(instance);
 
 2335    return !rtp->ssl_ctx ? 0 : 1;
 
 2342    SSL *ssl = rtp->dtls.ssl;
 
 2346    dtls_srtp_stop_timeout_timer(instance, rtp, 0);
 
 2350        SSL_CTX_free(rtp->ssl_ctx);
 
 2351        rtp->ssl_ctx = 
NULL;
 
 2354    if (rtp->dtls.ssl) {
 
 2355        SSL_free(rtp->dtls.ssl);
 
 2356        rtp->dtls.ssl = 
NULL;
 
 2361        dtls_srtp_stop_timeout_timer(instance, rtp, 1);
 
 2364        if (rtp->
rtcp->dtls.ssl) {
 
 2365            if (rtp->
rtcp->dtls.ssl != ssl) {
 
 2366                SSL_free(rtp->
rtcp->dtls.ssl);
 
 2378    if (SSL_is_init_finished(rtp->dtls.ssl)) {
 
 2379        SSL_shutdown(rtp->dtls.ssl);
 
 2383    if (rtp->
rtcp && SSL_is_init_finished(rtp->
rtcp->dtls.ssl)) {
 
 2384        SSL_shutdown(rtp->
rtcp->dtls.ssl);
 
 2394    return rtp->dtls.connection;
 
 2402    return rtp->dtls.dtls_setup;
 
 2431    if (old == *dtls_setup) {
 
 2441        SSL_set_connect_state(ssl);
 
 2443        SSL_set_accept_state(ssl);
 
 2454    if (rtp->dtls.ssl) {
 
 2455        dtls_set_setup(&rtp->dtls.dtls_setup, setup, rtp->dtls.ssl);
 
 2458    if (rtp->
rtcp && rtp->
rtcp->dtls.ssl) {
 
 2459        dtls_set_setup(&rtp->
rtcp->dtls.dtls_setup, setup, rtp->
rtcp->dtls.ssl);
 
 2474    rtp->remote_hash = hash;
 
 2476    while ((
value = 
strsep(&tmp, 
":")) && (pos != (EVP_MAX_MD_SIZE - 1))) {
 
 2477        sscanf(
value, 
"%02hhx", &rtp->remote_fingerprint[pos++]);
 
 2486    return rtp->local_hash;
 
 2490static const char *ast_rtp_dtls_get_fingerprint(
struct ast_rtp_instance *instance)
 
 2494    return rtp->local_fingerprint;
 
 2500    .active = ast_rtp_dtls_active,
 
 2501    .stop = ast_rtp_dtls_stop,
 
 2502    .reset = ast_rtp_dtls_reset,
 
 2503    .get_connection = ast_rtp_dtls_get_connection,
 
 2504    .get_setup = ast_rtp_dtls_get_setup,
 
 2505    .set_setup = ast_rtp_dtls_set_setup,
 
 2506    .set_fingerprint = ast_rtp_dtls_set_fingerprint,
 
 2507    .get_fingerprint_hash = ast_rtp_dtls_get_fingerprint_hash,
 
 2508    .get_fingerprint = ast_rtp_dtls_get_fingerprint,
 
 2513#ifdef TEST_FRAMEWORK 
 2551    if (rtp && rtp->
rtcp) {
 
 2556static struct ast_rtp_engine_test ast_rtp_test = {
 
 2557    .packets_to_drop = 0,
 
 2560    .recv_buffer_count = get_recv_buffer_count,
 
 2561    .recv_buffer_max = get_recv_buffer_max,
 
 2562    .send_buffer_count = get_send_buffer_count,
 
 2563    .set_schedid = set_rtp_rtcp_schedid,
 
 2593#ifdef HAVE_PJPROJECT 
 2594    .ice = &ast_rtp_ice,
 
 2596#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
 2597    .dtls = &ast_rtp_dtls,
 
 2598    .activate = ast_rtp_activate,
 
 2606#ifdef TEST_FRAMEWORK 
 2607    .test = &ast_rtp_test,
 
 
 2611#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
 2613static void dtls_perform_handshake(
struct ast_rtp_instance *instance, 
struct dtls_details *
dtls, 
int rtcp)
 
 2617    ast_debug_dtls(3, 
"(%p) DTLS perform handshake - ssl = %p, setup = %d\n",
 
 2618        rtp, dtls->ssl, dtls->dtls_setup);
 
 2628    SSL_do_handshake(dtls->ssl);
 
 2638    dtls_srtp_start_timeout_timer(instance, rtp, 
rtcp);
 
 2642#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
 2643static void dtls_perform_setup(
struct dtls_details *dtls)
 
 2645    if (!dtls->ssl || !SSL_is_init_finished(dtls->ssl)) {
 
 2649    SSL_clear(dtls->ssl);
 
 2651        SSL_set_accept_state(dtls->ssl);
 
 2653        SSL_set_connect_state(dtls->ssl);
 
 2661#ifdef HAVE_PJPROJECT 
 2665static void ast_rtp_ice_start_media(pj_ice_sess *ice, pj_status_t 
status)
 
 2672    if (
status == PJ_SUCCESS) {
 
 2689#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
 2691    if (rtp->ice_media_started) {
 
 2697        "(%p) ICE starting media - perform DTLS - (%p)\n", instance, rtp);
 
 2711    dtls_perform_handshake(instance, &rtp->dtls, 0);
 
 2714        dtls_perform_handshake(instance, &rtp->
rtcp->dtls, 1);
 
 2718    rtp->ice_media_started = 1;
 
 2725    ast_verb(4, 
"%p -- Strict RTP learning after ICE completion\n", rtp);
 
 2730#ifdef HAVE_PJPROJECT_ON_VALID_ICE_PAIR_CALLBACK 
 2732static void ast_rtp_on_valid_pair(pj_ice_sess *ice)
 
 2734    ast_debug_ice(2, 
"(%p) ICE valid pair, start media\n", ice->user_data);
 
 2735    ast_rtp_ice_start_media(ice, PJ_SUCCESS);
 
 2740static void ast_rtp_on_ice_complete(pj_ice_sess *ice, pj_status_t 
status)
 
 2742    ast_debug_ice(2, 
"(%p) ICE complete, start media\n", ice->user_data);
 
 2743    ast_rtp_ice_start_media(ice, 
status);
 
 2747static 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)
 
 2755        rtp->passthrough = 1;
 
 2757        rtp->rtp_passthrough = 1;
 
 2759        rtp->rtcp_passthrough = 1;
 
 2764static 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)
 
 2768    pj_status_t 
status = PJ_EINVALIDOP;
 
 2769    pj_ssize_t _size = (pj_ssize_t)size;
 
 2773        status = pj_sock_sendto(rtp->
s, pkt, &_size, 0, dst_addr, dst_addr_len);
 
 2779            status = pj_sock_sendto(rtp->
rtcp->
s, pkt, &_size, 0, dst_addr, dst_addr_len);
 
 2787        if (rtp->turn_rtp) {
 
 2788            status = pj_turn_sock_sendto(rtp->turn_rtp, pkt, size, dst_addr, dst_addr_len);
 
 2792        if (rtp->turn_rtcp) {
 
 2793            status = pj_turn_sock_sendto(rtp->turn_rtcp, pkt, size, dst_addr, dst_addr_len);
 
 2801static pj_ice_sess_cb ast_rtp_ice_sess_cb = {
 
 2802#ifdef HAVE_PJPROJECT_ON_VALID_ICE_PAIR_CALLBACK 
 2803    .on_valid_pair = ast_rtp_on_valid_pair,
 
 2805    .on_ice_complete = ast_rtp_on_ice_complete,
 
 2806    .on_rx_data = ast_rtp_on_ice_rx_data,
 
 2807    .on_tx_pkt = ast_rtp_on_ice_tx_pkt,
 
 2811static int timer_worker_thread(
void *data)
 
 2813    pj_ioqueue_t *ioqueue;
 
 2815    if (pj_ioqueue_create(pool, 1, &ioqueue) != PJ_SUCCESS) {
 
 2819    while (!timer_terminate) {
 
 2820        const pj_time_val delay = {0, 10};
 
 2822        pj_timer_heap_poll(timer_heap, 
NULL);
 
 2823        pj_ioqueue_poll(ioqueue, &delay);
 
 2862#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
 2882    struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->
rtcp->dtls;
 
 2883    struct timeval dtls_timeout;
 
 2886    res = DTLSv1_handle_timeout(dtls->ssl);
 
 2887    ast_debug_dtls(3, 
"(%p) DTLS srtp - handle timeout - rtcp=%d result: %d\n", instance, rtcp, res);
 
 2890    res = DTLSv1_get_timeout(dtls->ssl, &dtls_timeout);
 
 2893        dtls->timeout_timer = -1;
 
 2896        ast_debug_dtls(3, 
"(%p) DTLS srtp - handle timeout - rtcp=%d get timeout failure\n", instance, rtcp);
 
 2899    *timeout = dtls_timeout.tv_sec * 1000 + dtls_timeout.tv_usec / 1000;
 
 2900    if (*timeout == 0) {
 
 2920    ast_debug_dtls(3, 
"(%p) DTLS srtp - handle timeout - rtcp=%d timeout=%d\n", instance, rtcp, *timeout);
 
 2926static int dtls_srtp_handle_rtp_timeout(
const void *data)
 
 2933    res = dtls_srtp_handle_timeout(instance, &
timeout, 0);
 
 2945static int dtls_srtp_handle_rtcp_timeout(
const void *
data)
 
 2952    res = dtls_srtp_handle_timeout(instance, &
timeout, 1);
 
 2965    struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->
rtcp->dtls;
 
 2966    ast_sched_cb cb = !rtcp ? dtls_srtp_handle_rtp_timeout : dtls_srtp_handle_rtcp_timeout;
 
 2967    struct timeval dtls_timeout;
 
 2973    res = DTLSv1_get_timeout(dtls->ssl, &dtls_timeout);
 
 2975        ast_debug_dtls(3, 
"(%p) DTLS srtp - DTLSv1_get_timeout return an error or there was no timeout set for %s\n",
 
 2976            instance, rtcp ? 
"RTCP" : 
"RTP");
 
 2980    timeout = dtls_timeout.tv_sec * 1000 + dtls_timeout.tv_usec / 1000;
 
 2989        ast_log(
LOG_WARNING, 
"Scheduling '%s' DTLS retransmission for RTP instance [%p] failed.\n",
 
 2990            !rtcp ? 
"RTP" : 
"RTCP", instance);
 
 2992        ast_debug_dtls(3, 
"(%p) DTLS srtp - scheduled timeout timer for '%d' %s\n",
 
 2993            instance, timeout, rtcp ? 
"RTCP" : 
"RTP");
 
 3000    struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->
rtcp->dtls;
 
 3003    ast_debug_dtls(3, 
"(%p) DTLS srtp - stopped timeout timer'\n", instance);
 
 3007static int dtls_srtp_renegotiate(
const void *data)
 
 3015    SSL_renegotiate(rtp->dtls.ssl);
 
 3016    SSL_do_handshake(rtp->dtls.ssl);
 
 3018    if (rtp->
rtcp && rtp->
rtcp->dtls.ssl && rtp->
rtcp->dtls.ssl != rtp->dtls.ssl) {
 
 3019        SSL_renegotiate(rtp->
rtcp->dtls.ssl);
 
 3020        SSL_do_handshake(rtp->
rtcp->dtls.ssl);
 
 3034    unsigned char *local_key, *local_salt, *remote_key, *remote_salt;
 
 3037    struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->
rtcp->dtls;
 
 3039    ast_debug_dtls(3, 
"(%p) DTLS srtp - add local ssrc - rtcp=%d, set_remote_policy=%d'\n",
 
 3040                   instance, rtcp, set_remote_policy);
 
 3043    if (!SSL_export_keying_material(dtls->ssl, material, 
SRTP_MASTER_LEN * 2, 
"EXTRACTOR-dtls_srtp", 19, 
NULL, 0, 0)) {
 
 3044        ast_log(
LOG_WARNING, 
"Unable to extract SRTP keying material from DTLS-SRTP negotiation on RTP instance '%p'\n",
 
 3051        local_key = material;
 
 3056        remote_key = material;
 
 3067        ast_log(
LOG_WARNING, 
"Could not set key/salt information on local policy of '%p' when setting up DTLS-SRTP\n", rtp);
 
 3072        ast_log(
LOG_WARNING, 
"Could not set suite to '%u' on local policy of '%p' when setting up DTLS-SRTP\n", rtp->suite, rtp);
 
 3078    if (set_remote_policy) {
 
 3084            ast_log(
LOG_WARNING, 
"Could not set key/salt information on remote policy of '%p' when setting up DTLS-SRTP\n", rtp);
 
 3089            ast_log(
LOG_WARNING, 
"Could not set suite to '%u' on remote policy of '%p' when setting up DTLS-SRTP\n", rtp->suite, rtp);
 
 3097        ast_log(
LOG_WARNING, 
"Could not set policies when setting up DTLS-SRTP on '%p'\n", rtp);
 
 3107    if (remote_policy) {
 
 3116    struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->
rtcp->dtls;
 
 3119    ast_debug_dtls(3, 
"(%p) DTLS setup SRTP rtp=%p'\n", instance, rtp);
 
 3125        if (!(certificate = SSL_get_peer_certificate(dtls->ssl))) {
 
 3126            ast_log(
LOG_WARNING, 
"No certificate was provided by the peer on RTP instance '%p'\n", instance);
 
 3131        if (rtp->remote_fingerprint[0]) {
 
 3133            unsigned char fingerprint[EVP_MAX_MD_SIZE];
 
 3139                type = EVP_sha256();
 
 3141                ast_log(
LOG_WARNING, 
"Unsupported fingerprint hash type on RTP instance '%p'\n", instance);
 
 3145            if (!X509_digest(certificate, 
type, fingerprint, &size) ||
 
 3147                memcmp(fingerprint, rtp->remote_fingerprint, size)) {
 
 3148                X509_free(certificate);
 
 3149                ast_log(
LOG_WARNING, 
"Fingerprint provided by remote party does not match that of peer certificate on RTP instance '%p'\n",
 
 3155        X509_free(certificate);
 
 3186    return elem - 
value;
 
 
 3192    return elem == 
value;
 
 
 3205    version = (packet[0] & 0XC0) >> 6;
 
 3220    m = packet[1] & 0x80;
 
 3221    pt = packet[1] & 0x7F;
 
 3222    if (m && pt >= 64 && pt <= 95) {
 
 
 3233#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
 3236#ifdef HAVE_PJPROJECT 
 3237    struct ast_sockaddr *loop = rtcp ? &rtp->rtcp_loop : &rtp->rtp_loop;
 
 3239#ifdef TEST_FRAMEWORK 
 3240    struct ast_rtp_engine_test *
test = ast_rtp_instance_get_test(instance);
 
 3247#ifdef TEST_FRAMEWORK 
 3248    if (
test && 
test->packets_to_drop > 0) {
 
 3249        test->packets_to_drop--;
 
 3254#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
 3257    if ((*
in >= 20) && (*
in <= 63)) {
 
 3258        struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->
rtcp->dtls;
 
 3263            ast_log(
LOG_ERROR, 
"Received SSL traffic on RTP instance '%p' without an SSL session\n",
 
 3268        ast_debug_dtls(3, 
"(%p) DTLS - __rtp_recvfrom rtp=%p - Got SSL packet '%d'\n", instance, rtp, *
in);
 
 3277#ifdef HAVE_PJPROJECT 
 3279            int pass_src_check = 0;
 
 3293            if (!rtp->ice_media_started) {
 
 3306            for (ix = 0; ix < rtp->ice->real_ice->rcand_cnt; ix++) {
 
 3307                pj_ice_sess_cand *rcand = &rtp->ice->real_ice->rcand[ix];
 
 3314            if (!pass_src_check) {
 
 3315                ast_log(
LOG_WARNING, 
"%s: DTLS packet from %s dropped. Source not in ICE active candidate list.\n",
 
 3333        dtls_srtp_stop_timeout_timer(instance, rtp, rtcp);
 
 3339            SSL_set_accept_state(dtls->ssl);
 
 3342        BIO_write(dtls->read_bio, 
buf, 
len);
 
 3346        if ((
len < 0) && (SSL_get_error(dtls->ssl, 
len) == SSL_ERROR_SSL)) {
 
 3347            unsigned long error = ERR_get_error();
 
 3348            ast_log(
LOG_ERROR, 
"DTLS failure occurred on RTP instance '%p' due to reason '%s', terminating\n",
 
 3349                instance, ERR_reason_error_string(
error));
 
 3353        if (SSL_is_init_finished(dtls->ssl)) {
 
 3357            if ((res = dtls_srtp_setup(rtp, instance, rtcp))) {
 
 3363            ast_debug_dtls(3, 
"(%p) DTLS - __rtp_recvfrom rtp=%p - established'\n", instance, rtp);
 
 3366            dtls_srtp_start_timeout_timer(instance, rtp, rtcp);
 
 3373#ifdef HAVE_PJPROJECT 
 3383    } 
else if (rtp->ice) {
 
 3387        struct ice_wrap *ice;
 
 3389        pj_thread_register_check();
 
 3391        pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &combined, &
address);
 
 3397        status = pj_ice_sess_on_rx_pkt(ice->real_ice,
 
 3400            pj_sockaddr_get_len(&
address));
 
 3403        if (
status != PJ_SUCCESS) {
 
 3406            pj_strerror(
status, err_buf, 
sizeof(err_buf));
 
 3411        if (!rtp->passthrough) {
 
 3416            if (!rtp->ice_active_remote_candidates && !rtp->ice_proposed_remote_candidates) {
 
 3425        rtp->passthrough = 0;
 
 
 3454#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
 3471#ifdef HAVE_PJPROJECT 
 3472    if (transport_rtp->ice) {
 
 3475        struct ice_wrap *ice;
 
 3478        if (rtcp && rtp->
rtcp->
s == rtp->
s) {
 
 3482        pj_thread_register_check();
 
 3485        ice = transport_rtp->ice;
 
 3487        if (instance == transport) {
 
 3490        status = pj_ice_sess_send_data(ice->real_ice, component, temp, 
len);
 
 3492        if (instance == transport) {
 
 3495        if (
status == PJ_SUCCESS) {
 
 
 3533    unsigned int interval;
 
 
 3546    double last_sum_of_squares = (*std_dev) * (*std_dev) * (*count ?: 1);
 
 3548    if (++(*count) == 0) {
 
 3560    delta1 = new_sample - *mean;
 
 3561    *mean += (delta1 / *count);
 
 3562    delta2 = new_sample - *mean;
 
 3565    *std_dev = sqrt((last_sum_of_squares + (delta1 * delta2)) / *count);
 
 
 3579        setsockopt(sock, SOL_SOCKET, SO_NO_CHECK, &nochecksums, 
sizeof(nochecksums));
 
 
 3596    info->max_seq = 
seq;
 
 3598    memset(&info->received, 0, 
sizeof(info->received));
 
 
 3613    if (
seq == (uint16_t) (info->max_seq + 1)) {
 
 3624        switch (info->stream_type) {
 
 3634            } 
else if (!info->packets
 
 3649    info->max_seq = 
seq;
 
 3651    return info->packets;
 
 
 3668#ifdef HAVE_PJPROJECT 
 3714static int stun_address_is_blacklisted(
const struct ast_sockaddr *addr)
 
 3729    unsigned int count = 0;
 
 3730    struct ifaddrs *ifa, *ia;
 
 3733    struct ast_ice_host_candidate *candidate;
 
 3734    int af_inet_ok = 0, af_inet6_ok = 0;
 
 3735    struct sockaddr_in stunaddr_copy;
 
 3740        af_inet_ok = af_inet6_ok = 1;
 
 3745    if (getifaddrs(&ifa) < 0) {
 
 3747        ast_log(
LOG_ERROR, 
"(%p) ICE Error obtaining list of local addresses: %s\n",
 
 3748                instance, strerror(
errno));
 
 3750        ast_debug_ice(2, 
"(%p) ICE add system candidates\n", instance);
 
 3754        for (ia = ifa; ia && count < PJ_ICE_MAX_CAND; ia = ia->ifa_next) {
 
 3757            if (!ia->ifa_addr || (ia->ifa_flags & IFF_UP) == 0) {
 
 3762            if (ia->ifa_addr->sa_family != AF_INET && ia->ifa_addr->sa_family != AF_INET6) {
 
 3768            if (ia->ifa_addr->sa_family == AF_INET) {
 
 3769                const struct sockaddr_in *sa_in = (
struct sockaddr_in*)ia->ifa_addr;
 
 3777                if ((sa_in->sin_addr.s_addr & htonl(0xFF000000)) == htonl(0x7F000000)) {
 
 3782                if ((sa_in->sin_addr.s_addr & htonl(0xFF000000)) == 0) {
 
 3805                    if (!rtp_address_is_ice_blacklisted(&candidate->advertised)) {
 
 3807                        pj_sockaddr_set_port(&pjtmp, port);
 
 3808                        ast_rtp_ice_add_cand(instance, rtp, component, transport,
 
 3809                                PJ_ICE_CAND_TYPE_HOST, 65535, &pjtmp, &pjtmp, 
NULL,
 
 3810                                pj_sockaddr_get_len(&pjtmp));
 
 3814                    if (!candidate->include_local) {
 
 3831            if (rtp_address_is_ice_blacklisted(&tmp)) {
 
 3836            pj_sockaddr_set_port(&pjtmp, port);
 
 3837            ast_rtp_ice_add_cand(instance, rtp, component, transport,
 
 3838                    PJ_ICE_CAND_TYPE_HOST, 65535, &pjtmp, &pjtmp, 
NULL,
 
 3839                    pj_sockaddr_get_len(&pjtmp));
 
 3846    memcpy(&stunaddr_copy, &stunaddr, 
sizeof(stunaddr));
 
 3850    if (stunaddr_copy.sin_addr.s_addr && !stun_address_is_blacklisted(addr) &&
 
 3852        count < PJ_ICE_MAX_CAND) {
 
 3853        struct sockaddr_in 
answer;
 
 3857            "(%p) ICE request STUN %s %s candidate\n", instance,
 
 3871            pj_sockaddr 
ext, base;
 
 3873            int srflx = 1, baseset = 0;
 
 3876            pj_sockaddr_init(pj_AF_INET(), &
ext, &mapped, ntohs(
answer.sin_port));
 
 3890                if (!pj_sockaddr_cmp(&candidate->
address, &
ext)) {
 
 3898            if (srflx && baseset) {
 
 3899                pj_sockaddr_set_port(&base, port);
 
 3900                ast_rtp_ice_add_cand(instance, rtp, component, transport,
 
 3901                    PJ_ICE_CAND_TYPE_SRFLX, 65535, &
ext, &base, &base,
 
 3902                    pj_sockaddr_get_len(&
ext));
 
 3908    if (pj_strlen(&turnaddr)) {
 
 3909        ast_rtp_ice_turn_request(instance, component, 
AST_TRANSPORT_TCP, pj_strbuf(&turnaddr), turnport,
 
 3910            pj_strbuf(&turnusername), pj_strbuf(&turnpassword));
 
 3941    return (
unsigned int) ms;
 
 
 3944#ifdef HAVE_PJPROJECT 
 3962    pj_stun_config stun_config;
 
 3963    pj_str_t ufrag, passwd;
 
 3965    struct ice_wrap *ice_old;
 
 3966    struct ice_wrap *ice;
 
 3967    pj_ice_sess *real_ice = 
NULL;
 
 3971    rtp->ice_local_candidates = 
NULL;
 
 3977        ast_rtp_ice_stop(instance);
 
 3981    pj_thread_register_check();
 
 3983    pj_stun_config_init(&stun_config, &
cachingpool.factory, 0, 
NULL, timer_heap);
 
 3984    if (!stun_software_attribute) {
 
 3985        stun_config.software_name = pj_str(
NULL);
 
 3988    ufrag = pj_str(rtp->local_ufrag);
 
 3989    passwd = pj_str(rtp->local_passwd);
 
 3994    status = pj_ice_sess_create(&stun_config, 
NULL, PJ_ICE_SESS_ROLE_UNKNOWN,
 
 3995        rtp->ice_num_components, &ast_rtp_ice_sess_cb, &ufrag, &passwd, 
NULL, &real_ice);
 
 3997    if (
status == PJ_SUCCESS) {
 
 3999        real_ice->user_data = instance;
 
 4000        ice->real_ice = real_ice;
 
 4016        if (
replace && rtp->
rtcp && rtp->ice_num_components > 1) {
 
 4017            rtp_add_candidates_to_ice(instance, rtp, &rtp->
rtcp->
us,
 
 4031    ast_rtp_ice_stop(instance);
 
 4039    int x, startplace, i, maxloops;
 
 4048        ast_log(
LOG_WARNING, 
"Failed to create a new socket for RTP instance '%p'\n", instance);
 
 4060    for (i = 0; i <= maxloops; i++) {
 
 4064            ast_debug_rtp(1, 
"(%p) RTP allocated port %d\n", instance, x);
 
 4076        if (x == startplace || (
errno != EADDRINUSE && 
errno != EACCES)) {
 
 4077            ast_log(
LOG_ERROR, 
"Oh dear... we couldn't allocate a port for RTP instance '%p'\n", instance);
 
 4084#ifdef HAVE_PJPROJECT 
 4093        rtp->ice_num_components = 2;
 
 4094        ast_debug_ice(2, 
"(%p) ICE creating session %s (%d)\n", instance,
 
 4105#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
 4107    rtp->dtls.timeout_timer = -1;
 
 
 4115    int saved_rtp_s = rtp->
s;
 
 4116#ifdef HAVE_PJPROJECT 
 4118    struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, };
 
 4121#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
 4122    ast_rtp_dtls_stop(instance);
 
 4133        if (saved_rtp_s != rtp->
rtcp->
s) {
 
 4134            close(rtp->
rtcp->
s);
 
 4139#ifdef HAVE_PJPROJECT 
 4140    pj_thread_register_check();
 
 4147    if (rtp->turn_rtp) {
 
 4148        rtp->turn_state = PJ_TURN_STATE_NULL;
 
 4152        pj_turn_sock_destroy(rtp->turn_rtp);
 
 4154        while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) {
 
 4157        rtp->turn_rtp = 
NULL;
 
 4161    if (rtp->turn_rtcp) {
 
 4162        rtp->turn_state = PJ_TURN_STATE_NULL;
 
 4166        pj_turn_sock_destroy(rtp->turn_rtcp);
 
 4168        while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) {
 
 4171        rtp->turn_rtcp = 
NULL;
 
 4174    ast_debug_ice(2, 
"(%p) ICE RTP transport deallocating\n", instance);
 
 4176    ast_rtp_ice_stop(instance);
 
 4179    if (rtp->ice_local_candidates) {
 
 4180        ao2_ref(rtp->ice_local_candidates, -1);
 
 4181        rtp->ice_local_candidates = 
NULL;
 
 4184    if (rtp->ice_active_remote_candidates) {
 
 4185        ao2_ref(rtp->ice_active_remote_candidates, -1);
 
 4186        rtp->ice_active_remote_candidates = 
NULL;
 
 4189    if (rtp->ice_proposed_remote_candidates) {
 
 4190        ao2_ref(rtp->ice_proposed_remote_candidates, -1);
 
 4191        rtp->ice_proposed_remote_candidates = 
NULL;
 
 4201        rtp_ioqueue_thread_remove(rtp->ioqueue);
 
 4203        rtp->ioqueue = 
NULL;
 
 
 4219    rtp->
owner = instance;
 
 
 4262#define SSRC_MAPPING_ELEM_CMP(elem, value) ((elem).instance == (value)) 
 4356    int hdrlen = 12, res = 0, i = 0, payload = -1, sample_rate = -1;
 
 4358    unsigned int *rtpheader = (
unsigned int*)data;
 
 4371    } 
else if (
digit == 
'*') {
 
 4373    } 
else if (
digit == 
'#') {
 
 4375    } 
else if ((
digit >= 
'A') && (
digit <= 
'D')) {
 
 4377    } 
else if ((
digit >= 
'a') && (
digit <= 
'd')) {
 
 4387        if (payload_format) {
 
 4395    if (sample_rate != -1) {
 
 4399    if (payload == -1 ||
 
 4410    if (payload == -1 || sample_rate == -1) {
 
 4415    if (payload == -1) {
 
 4420    ast_debug(1, 
"Sending digit '%d' at rate %d with payload %d\n", 
digit, sample_rate, payload);
 
 4429    rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->
seqno));
 
 4431    rtpheader[2] = htonl(rtp->
ssrc);
 
 4434    for (i = 0; i < 2; i++) {
 
 4438        res = 
rtp_sendto(instance, (
void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
 
 4445            ast_verbose(
"Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
 
 4447                    ice ? 
" (via ICE)" : 
"",
 
 4452        rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->
seqno));
 
 
 4468    int hdrlen = 12, res = 0;
 
 4470    unsigned int *rtpheader = (
unsigned int*)data;
 
 4483    rtpheader[2] = htonl(rtp->
ssrc);
 
 4487    res = 
rtp_sendto(instance, (
void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
 
 4495        ast_verbose(
"Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
 
 4497                ice ? 
" (via ICE)" : 
"",
 
 
 4514    int hdrlen = 12, res = -1, i = 0;
 
 4516    unsigned int *rtpheader = (
unsigned int*)data;
 
 4517    unsigned int measured_samples;
 
 4529    } 
else if (
digit == 
'*') {
 
 4531    } 
else if (
digit == 
'#') {
 
 4533    } 
else if ((
digit >= 
'A') && (
digit <= 
'D')) {
 
 4535    } 
else if ((
digit >= 
'a') && (
digit <= 
'd')) {
 
 4545        ast_debug_rtp(2, 
"(%p) RTP adjusting final end duration from %d to %u\n",
 
 4552    rtpheader[2] = htonl(rtp->
ssrc);
 
 4554    rtpheader[3] |= htonl((1 << 23));
 
 4557    for (i = 0; i < 3; i++) {
 
 4562        res = 
rtp_sendto(instance, (
void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
 
 4571            ast_verbose(
"Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
 
 4573                    ice ? 
" (via ICE)" : 
"",
 
 
 4612    ast_debug_rtp(3, 
"(%p) RTP setting the marker bit due to a source update\n", instance);
 
 
 4630    ast_debug_rtp(3, 
"(%p) RTP changing ssrc from %u to %u due to a source change\n",
 
 4631        instance, 
rtp->ssrc, ssrc);
 
 4634        ast_debug_rtp(3, 
"(%p) RTP changing ssrc for SRTP from %u to %u\n",
 
 4635            instance, 
rtp->ssrc, ssrc);
 
 4637        if (rtcp_srtp != srtp) {
 
 4645    rtp->expectedrxseqno = -1;
 
 
 4650static void timeval2ntp(
struct timeval tv, 
unsigned int *msw, 
unsigned int *lsw)
 
 4652    unsigned int sec, usec, frac;
 
 4653    sec = tv.tv_sec + 2208988800u; 
 
 4674    frac = ((((usec << 12) / 125) << 7) / 125) << 7;
 
 
 4679static void ntp2timeval(
unsigned int msw, 
unsigned int lsw, 
struct timeval *tv)
 
 4681    tv->tv_sec = msw - 2208988800u;
 
 4683    tv->tv_usec = ((((lsw >> 7) * 125) >> 7) * 125) >> 12;
 
 
 4687        unsigned int *lost_packets,
 
 4690    unsigned int extended_seq_no;
 
 4691    unsigned int expected_packets;
 
 4692    unsigned int expected_interval;
 
 4693    unsigned int received_interval;
 
 4697    extended_seq_no = 
rtp->cycles + 
rtp->lastrxseqno;
 
 4698    expected_packets = extended_seq_no - 
rtp->seedrxseqno + 1;
 
 4699    if (
rtp->rxcount > expected_packets) {
 
 4700        expected_packets += 
rtp->rxcount - expected_packets;
 
 4702    *lost_packets = expected_packets - 
rtp->rxcount;
 
 4703    expected_interval = expected_packets - 
rtp->rtcp->expected_prior;
 
 4704    received_interval = 
rtp->rxcount - 
rtp->rtcp->received_prior;
 
 4705    if (received_interval > expected_interval) {
 
 4711        expected_interval = received_interval;
 
 4713    lost_interval = expected_interval - received_interval;
 
 4714    if (expected_interval == 0 || lost_interval <= 0) {
 
 4717        *fraction_lost = (lost_interval << 8) / expected_interval;
 
 4721    rtp->rtcp->received_prior = 
rtp->rxcount;
 
 4722    rtp->rtcp->expected_prior = expected_packets;
 
 4730    if (lost_interval <= 0) {
 
 4731        rtp->rtcp->rxlost = 0;
 
 4733        rtp->rtcp->rxlost = lost_interval;
 
 4735    if (
rtp->rtcp->rxlost_count == 0) {
 
 4736        rtp->rtcp->minrxlost = 
rtp->rtcp->rxlost;
 
 4738    if (lost_interval && lost_interval < rtp->rtcp->minrxlost) {
 
 4739        rtp->rtcp->minrxlost = 
rtp->rtcp->rxlost;
 
 4741    if (lost_interval > 
rtp->rtcp->maxrxlost) {
 
 4742        rtp->rtcp->maxrxlost = 
rtp->rtcp->rxlost;
 
 4746        &
rtp->rtcp->stdev_rxlost, &
rtp->rtcp->rxlost_count);
 
 
 4755    unsigned int now_lsw;
 
 4756    unsigned int now_msw;
 
 4757    unsigned int lost_packets;
 
 4759    struct timeval dlsr = { 0, };
 
 4762    if (!rtp || !rtp->
rtcp) {
 
 4785    gettimeofday(&now, 
NULL);
 
 4797        report_block = 
ast_calloc(1, 
sizeof(*report_block));
 
 4798        if (!report_block) {
 
 4812            report_block->
dlsr = (((
dlsr.tv_sec * 1000) + (
dlsr.tv_usec / 1000)) * 65536) / 1000;
 
 
 4849    if (!rtp || !rtp->
rtcp) {
 
 4872        ast_verbose(
"* Sent RTCP %s to %s%s\n", sr ? 
"SR" : 
"RR",
 
 4893            ast_verbose(
"    DLSR: %4.4f (sec)\n\n", (
double)(report_block->
dlsr / 65536.0));
 
 4903            rtcp_report, message_blob);
 
 
 4913    uint16_t sdes_packet_len_bytes;
 
 4914    uint16_t sdes_packet_len_rounded;
 
 4916    if (!rtp || !rtp->
rtcp) {
 
 4928    sdes_packet_len_bytes =
 
 4937    sdes_packet_len_rounded = (sdes_packet_len_bytes + 3) & ~0x3;
 
 4941    rtcpheader[8] = 0x01; 
 
 4956    len += (sdes_packet_len_rounded - sdes_packet_len_bytes);
 
 
 4979    if (res == 0 || res == 1) {
 
 4980        ast_debug_rtcp(1, 
"(%p) RTCP failed to generate %s report!\n", instance, sr ? 
"SR" : 
"RR");
 
 4988    if (res == 0 || res == 1) {
 
 4989        ast_debug_rtcp(1, 
"(%p) RTCP failed to generate SDES!\n", instance);
 
 4993    return packet_len + res;
 
 
 5002    unsigned int fci = 0;
 
 5003    size_t remaining_missing_seqno;
 
 5005    if (!rtp || !rtp->
rtcp) {
 
 5018    if (!remaining_missing_seqno) {
 
 5025    while (remaining_missing_seqno) {
 
 5037            if (blp_index >= 17) {
 
 5044            if (blp_index == 0) {
 
 5045                fci |= (current_seqno << 16);
 
 5047                fci |= (1 << (blp_index - 1));
 
 5051            remaining_missing_seqno--;
 
 
 5090    unsigned char *rtcpheader;
 
 5104    if (res == 0 || res == 1) {
 
 5116    res = 
rtcp_sendto(instance, (
unsigned int *)rtcpheader, packet_len, 0, &remote_address, &ice);
 
 
 5143    unsigned char *cp = p;
 
 5147    datum = (time_msw << 18) & 0x00fc0000;
 
 5148    datum |= (time_lsw >> 14) & 0x0003ffff;
 
 5150    cp[0] = datum >> 16;
 
 
 5164#ifdef TEST_FRAMEWORK 
 5165    struct ast_rtp_engine_test *
test = ast_rtp_instance_get_test(instance);
 
 5176#ifdef TEST_FRAMEWORK 
 5178        test->send_report = 0;
 
 5195                ast_debug_rtp(3, 
"(%p) RTP audio difference is %d, ms is %u\n",
 
 5196                    instance, 
abs((
int)rtp->
lastts - pred), ms);
 
 5207            if (
abs((
int)rtp->
lastts - pred) < 7200) {
 
 5211                ast_debug_rtp(3, 
"(%p) RTP video difference is %d, ms is %u (%u), pred/ts/samples %u/%d/%d\n",
 
 5222            if (
abs((
int)rtp->
lastts - pred) < 7200) {
 
 5226                ast_debug_rtp(3, 
"(%p) RTP other difference is %d, ms is %u, pred/ts/samples %u/%d/%d\n",
 
 5258            if (
abs(difference) > 100) {
 
 5263            seqno += difference;
 
 5265            if (difference >= 0) {
 
 5268                rtp->
seqno += difference;
 
 5290        int abs_send_time_id;
 
 5292        unsigned char *rtpheader;
 
 5296        if (abs_send_time_id != -1) {
 
 5302        packet_len = frame->
datalen + hdrlen;
 
 5303        rtpheader = (
unsigned char *)(frame->
data.
ptr - hdrlen);
 
 5312        if (abs_send_time_id != -1) {
 
 5313            unsigned int now_msw;
 
 5314            unsigned int now_lsw;
 
 5320            rtpheader[16] = (abs_send_time_id << 4) | 2;
 
 5330            payload = 
ast_malloc(
sizeof(*payload) + packet_len);
 
 5332                payload->
size = packet_len;
 
 5333                memcpy(payload->
buf, rtpheader, packet_len);
 
 5340        res = 
rtp_sendto(instance, (
void *)rtpheader, packet_len, 0, &remote_address, &ice);
 
 5343                ast_debug_rtp(1, 
"(%p) RTP transmission error of packet %d to %s: %s\n",
 
 5344                      instance, rtp->
seqno,
 
 5350                    ast_debug(0, 
"(%p) RTP NAT: Can't write RTP to private address %s, waiting for other end to send audio...\n",
 
 5368            ast_verbose(
"Sent RTP packet to      %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
 
 5370                    ice ? 
" (via ICE)" : 
"",
 
 5379    if (seqno == rtp->
seqno) {
 
 
 5394        for (i = 1; i < red->
num_gen+1; i++)
 
 5401    for (i = 0; i < red->
num_gen; i++)
 
 5402        red->
len[i] = red->
len[i+1];
 
 5407    for (i = 0; i < red->
num_gen; i++) {
 
 
 5428    unsigned char *rtcpheader;
 
 5429    unsigned char bdata[1024];
 
 5437    if (!rtp || !rtp->
rtcp) {
 
 5465    if (res == 0 || res == 1) {
 
 5477    res = 
rtcp_sendto(instance, (
unsigned int *)rtcpheader, packet_len + fir_len, 0, rtp->
bundled ? remote_address : &rtp->
rtcp->
them, &ice);
 
 
 5490    unsigned char *rtcpheader;
 
 5491    unsigned char bdata[1024];
 
 5500        ast_debug_rtcp(1, 
"(%p) RTCP provided feedback frame of format %d to write, but only REMB is supported\n",
 
 5501            instance, feedback->
fmt);
 
 5505    if (!rtp || !rtp->
rtcp) {
 
 5511        ast_debug_rtcp(1, 
"(%p) RTCP provided feedback REMB report to write, but REMB support not enabled\n",
 
 5529    if (res == 0 || res == 1) {
 
 5539    put_unaligned_uint32(rtcpheader + packet_len + 12, htonl((
'R' << 24) | (
'E' << 16) | (
'M' << 8) | (
'B'))); 
 
 5542    res = 
rtcp_sendto(instance, (
unsigned int *)rtcpheader, packet_len + remb_len, 0, rtp->
bundled ? remote_address : &rtp->
rtcp->
them, &ice);
 
 
 5564        ast_debug_rtp(1, 
"(%p) RTP no remote address on instance, so dropping frame\n", instance);
 
 5581        ast_debug_rtp(1, 
"(%p) RTP received frame with no data for instance, so dropping frame\n", instance);
 
 5613        ast_debug_rtp(1, 
"(%s) RTP ooh, format changed from %s to %s\n",
 
 5661        if (frame->
offset < hdrlen) {
 
 
 5679    struct ast_rtp *rtp, 
unsigned int rx_rtp_ts,
 
 5684    double jitter = 0.0;
 
 5685    double prev_jitter = 0.0;
 
 5690    unsigned int arrival;
 
 5694    gettimeofday(&now,
NULL);
 
 5696    if (rtp->
rxcount == 1 || mark) {
 
 5718            "Seed ts: %u current time: %f\n",
 
 5760            "pkt: %5u Stable Seed ts: %u current time: %f\n",
 
 5776        ast_debug_rtcp(3, 
"%s: Current packet seq %d != last packet seq %d + 1.  Ignoring\n",
 
 5816    transit = arrival - rx_rtp_ts;
 
 5824    jitter = (1.0/16.0) * (((
double)
d) - prev_jitter);
 
 5833        "Arrival sec: %7.3f  Arrival ts: %10u  RX ts: %10u " 
 5834        "Transit samp: %6d Last transit samp: %6d d: %4d " 
 5835        "Curr jitter: %7.0f(%7.3f) Prev Jitter: %7.0f(%7.3f) New Jitter: %7.0f(%7.3f)\n",
 
 
 5881        ast_debug_rtp(1, 
"(%p) RTP ignore potential DTMF echo from '%s'\n",
 
 5894    if (rtp->
resp == 
'X') {
 
 5900        ast_debug_rtp(1, 
"(%p) RTP creating %s DTMF Frame: %d (%c), at %s\n",
 
 
 5920    unsigned int event, event_end, samples;
 
 5927    event = ntohl(*((
unsigned int *)(
data)));
 
 5929    event_end = ntohl(*((
unsigned int *)(
data)));
 
 5936        ast_verbose(
"Got  RTP RFC2833 from   %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6d, mark %d, event %08x, end %d, duration %-5.5u) \n",
 
 5948    } 
else if (
event < 11) {
 
 5950    } 
else if (
event < 12) {
 
 5952    } 
else if (
event < 16) {
 
 5953        resp = 
'A' + (
event - 12);
 
 5954    } 
else if (
event < 17) {        
 
 5958        ast_debug_rtp(1, 
"(%p) RTP ignoring RTP 2833 Event: %08x. Not a DTMF Digit.\n", instance, 
event);
 
 5979        unsigned int last_duration = new_duration & 0xFFFF;
 
 5981        if (last_duration > 64000 && 
samples < last_duration) {
 
 5982            new_duration += 0xFFFF + 1;
 
 5984        new_duration = (new_duration & ~0xFFFF) | 
samples;
 
 5986        if (event_end & 0x80) {
 
 5999                ast_debug_rtp(1, 
"(%p) RTP dropping duplicate or out of order DTMF END frame (seqno: %u, ts %u, digit %c)\n",
 
 6000                    instance, 
seqno, timestamp, resp);
 
 6011                  && timestamp <= rtp->last_end_timestamp.ts)) {
 
 6017                    ast_debug(0, 
"Dropping out of order DTMF frame (seqno %u, ts %u, digit %c)\n",
 
 6018                        seqno, timestamp, resp);
 
 6023            if (rtp->
resp && rtp->
resp != resp) {
 
 
 6099    event = 
data[3] & 0x1f;
 
 6102        ast_debug(0, 
"Cisco DTMF Digit: %02x (len=%d, seq=%d, flags=%02x, power=%u, history count=%d)\n", 
event, 
len, 
seq, 
flags, power, (
len - 4) / 2);
 
 6105    } 
else if (
event < 11) {
 
 6107    } 
else if (
event < 12) {
 
 6109    } 
else if (
event < 16) {
 
 6110        resp = 
'A' + (
event - 12);
 
 6111    } 
else if (
event < 17) {
 
 6114    if ((!rtp->
resp && power) || (rtp->
resp && (rtp->
resp != resp))) {
 
 6121    } 
else if ((rtp->
resp == resp) && !power) {
 
 6125    } 
else if (rtp->
resp == resp) {
 
 
 6142        ast_debug(0, 
"- RTP 3389 Comfort noise event: Format %s (len = %d)\n",
 
 6151        ast_log(
LOG_NOTICE, 
"Comfort noise support incomplete in Asterisk (RFC 3389). Please turn off on client if possible. Client address: %s\n",
 
 
 6181    struct timeval rtt_tv;
 
 6184    unsigned int rtt_msw;
 
 6185    unsigned int rtt_lsw;
 
 6189    gettimeofday(&now, 
NULL);
 
 6192    lsr_a = ((msw & 0x0000ffff) << 16) | ((lsw & 0xffff0000) >> 16);
 
 6193    rtt = lsr_a - lsr - dlsr;
 
 6194    rtt_msw = (rtt & 0xffff0000) >> 16;
 
 6195    rtt_lsw = (rtt & 0x0000ffff);
 
 6196    rtt_tv.tv_sec = rtt_msw;
 
 6210    rtt_tv.tv_usec = (rtt_lsw * 15625) >> 10;
 
 6211    rtp->
rtcp->
rtt = (double)rtt_tv.tv_sec + ((
double)rtt_tv.tv_usec / 1000000);
 
 6212    if (lsr_a - dlsr < lsr) {
 
 
 6261    double reported_lost;
 
 6268    if (reported_lost < rtp->rtcp->reported_minlost) {
 
 
 6279#define RESCALE(in, inmin, inmax, outmin, outmax) ((((in - inmin)/(inmax-inmin))*(outmax-outmin))+outmin) 
 6304    double normdevrtt, 
double normdev_rxjitter, 
double stdev_rxjitter,
 
 6305    double normdev_rxlost)
 
 6320    double effective_latency = (normdevrtt * 1000)
 
 6321        + ((normdev_rxjitter * 2) * (stdev_rxjitter / 3))
 
 6332    if (effective_latency < 160) {
 
 6333        r_value = 93.2 - (effective_latency / 40);
 
 6335        r_value = 93.2 - (effective_latency - 120) / 10;
 
 6339    r_value = r_value - (normdev_rxlost * 2.0);
 
 6347    } 
else if (r_value > 100) {
 
 6350        pseudo_mos = 1 + (0.035 * r_value) + (r_value * (r_value - 60) * (100 - r_value) * 0.000007);
 
 6358    mes = 
RESCALE(pseudo_mos, 0.0, 5.0, 0.0, 100.0);
 
 
 6380    if (mes < rtp->rtcp->reported_minmes) {
 
 6390    ast_debug_rtcp(2, 
"%s: rtt: %.9f j: %.9f sjh: %.9f lost: %.9f mes: %4.1f\n",
 
 
 6424    ast_debug_rtcp(2, 
"   %s: rtt: %.9f j: %.9f sjh: %.9f lost: %.9f mes: %4.1f\n",
 
 
 6434    struct ast_rtp *rtp, 
unsigned int ssrc, 
int source)
 
 
 6462    struct ast_rtp *rtp, 
unsigned int ssrc)
 
 
 6469    struct ast_rtp *rtp, 
unsigned int ssrc)
 
 
 6480        str = 
"Sender Report";
 
 6483        str = 
"Receiver Report";
 
 6494        str = 
"Source Description";
 
 
 6528    unsigned int length)
 
 6536    unsigned int current_word;
 
 6540    int abs_send_time_id;
 
 6541    unsigned int now_msw = 0;
 
 6542    unsigned int now_lsw = 0;
 
 6543    unsigned int packets_not_found = 0;
 
 6547            "but we don't have a RTP packet storage!\n", instance);
 
 6552    if (abs_send_time_id != -1) {
 
 6562    for (packet_index = 3; packet_index < length; packet_index++) {
 
 6563        current_word = ntohl(nackdata[position + packet_index]);
 
 6564        pid = current_word >> 16;
 
 6568            if (abs_send_time_id != -1) {
 
 6574            res += 
rtp_sendto(instance, payload->
buf, payload->
size, 0, &remote_address, &ice);
 
 6576            ast_debug_rtcp(1, 
"(%p) RTCP received NACK request for RTP packet with seqno %d, " 
 6577                "but we don't have it\n", instance, pid);
 
 6578            packets_not_found++;
 
 6586        blp = current_word & 0xffff;
 
 6591                unsigned int seqno = (pid + blp_index) % 65536;
 
 6594                    if (abs_send_time_id != -1) {
 
 6597                    res += 
rtp_sendto(instance, payload->
buf, payload->
size, 0, &remote_address, &ice);
 
 6599                    ast_debug_rtcp(1, 
"(%p) RTCP remote end also requested RTP packet with seqno %d, " 
 6600                        "but we don't have it\n", instance, seqno);
 
 6601                    packets_not_found++;
 
 6609    if (packets_not_found) {
 
 6615        ast_debug_rtcp(2, 
"(%p) RTCP send buffer on RTP instance is now at maximum of %zu\n",
 
 
 6625#define RTCP_LENGTH_MASK            0xFFFF 
 6626#define RTCP_PAYLOAD_TYPE_MASK      0xFF 
 6627#define RTCP_REPORT_COUNT_MASK      0x1F 
 6628#define RTCP_PADDING_MASK           0x01 
 6629#define RTCP_VERSION_MASK           0x03 
 6634#define RTCP_LENGTH_SHIFT           0 
 6635#define RTCP_PAYLOAD_TYPE_SHIFT     16 
 6636#define RTCP_REPORT_COUNT_SHIFT     24 
 6637#define RTCP_PADDING_SHIFT          29 
 6638#define RTCP_VERSION_SHIFT          30 
 6640#define RTCP_VERSION                2U 
 6641#define RTCP_VERSION_SHIFTED        (RTCP_VERSION << RTCP_VERSION_SHIFT) 
 6642#define RTCP_VERSION_MASK_SHIFTED   (RTCP_VERSION_MASK << RTCP_VERSION_SHIFT) 
 6655#define RTCP_VALID_MASK (RTCP_VERSION_MASK_SHIFTED | (((RTCP_PAYLOAD_TYPE_MASK & ~0x1)) << RTCP_PAYLOAD_TYPE_SHIFT)) 
 6656#define RTCP_VALID_VALUE (RTCP_VERSION_SHIFTED | (RTCP_PT_SR << RTCP_PAYLOAD_TYPE_SHIFT)) 
 6658#define RTCP_SR_BLOCK_WORD_LENGTH 5 
 6659#define RTCP_RR_BLOCK_WORD_LENGTH 6 
 6660#define RTCP_HEADER_SSRC_LENGTH   2 
 6661#define RTCP_FB_REMB_BLOCK_WORD_LENGTH 4 
 6662#define RTCP_FB_NACK_BLOCK_WORD_LENGTH 2 
 6665    const unsigned char *rtcpdata, 
size_t size, 
struct ast_sockaddr *addr)
 
 6670    unsigned int *rtcpheader = (
unsigned int *)(rtcpdata);
 
 6671    unsigned int packetwords;
 
 6672    unsigned int position;
 
 6673    unsigned int first_word;
 
 6675    unsigned int ssrc_seen;
 
 6678#ifdef TEST_FRAMEWORK 
 6679    struct ast_rtp_engine_test *test_engine;
 
 6688    packetwords = 
len / 4;
 
 6699        ast_debug_rtcp(2, 
"(%s) RTCP %p -- from %s: Frame size (%u words) is too short\n",
 
 6705    first_word = ntohl(rtcpheader[position]);
 
 6707        ast_debug_rtcp(2, 
"(%s) RTCP %p -- from %s: Failed first packet validity check\n",
 
 6714        if (packetwords <= position) {
 
 6717        first_word = ntohl(rtcpheader[position]);
 
 6719    if (position != packetwords) {
 
 6720        ast_debug_rtcp(2, 
"(%s) RTCP %p -- from %s: Failed packet version or length check\n",
 
 6745    while (position < packetwords) {
 
 6751        unsigned int ssrc_valid;
 
 6752        unsigned int length;
 
 6753        unsigned int min_length;
 
 6755        unsigned int use_packet_source = 1;
 
 6764        first_word = ntohl(rtcpheader[i]);
 
 6778            use_packet_source = 0;
 
 6790            use_packet_source = 0;
 
 6809            min_length = length;
 
 6812            ast_debug_rtcp(1, 
"(%p) RTCP %p -- from %s: %u(%s) skipping record\n",
 
 6816                ast_verbose(
"RTCP from %s: %u(%s) skipping record\n",
 
 6822        if (length < min_length) {
 
 6823            ast_debug_rtcp(1, 
"(%p) RTCP %p -- from %s: %u(%s) length field less than expected minimum.  Min:%u Got:%u\n",
 
 6825                min_length - 1, length - 1);
 
 6838            rtcp_report->reception_report_count = rc;
 
 6840            ssrc = ntohl(rtcpheader[i + 2]);
 
 6841            rtcp_report->ssrc = ssrc;
 
 6845            ssrc = ntohl(rtcpheader[i + 1]);
 
 6848            ssrc = ntohl(rtcpheader[i + 2]);
 
 6865                ast_verbose(
"Packet Subtype: %u (%s)\n", rc, subtype);
 
 6878            if (use_packet_source) {
 
 6883            if (child && child != transport) {
 
 6895                rtp = transport_rtp;
 
 6899            rtp = transport_rtp;
 
 6912            if ((ssrc != rtp->
themssrc && use_packet_source && ssrc != 1)
 
 6920                ast_debug_rtcp(1, 
"(%p) RTCP %p -- from %s: Skipping record, received SSRC '%u' != expected '%u'\n",
 
 6935                    ast_debug(0, 
"(%p) RTCP NAT: Got RTCP from other end. Now sending to address %s\n",
 
 6945            rtp->
rtcp->
themrxlsr = ((ntohl(rtcpheader[i]) & 0x0000ffff) << 16) | ((ntohl(rtcpheader[i + 1]) & 0xffff0000) >> 16);
 
 6946            rtp->
rtcp->
spc = ntohl(rtcpheader[i + 3]);
 
 6947            rtp->
rtcp->
soc = ntohl(rtcpheader[i + 4]);
 
 6950            rtcp_report->sender_information.packet_count = rtp->
rtcp->
spc;
 
 6951            rtcp_report->sender_information.octet_count = rtp->
rtcp->
soc;
 
 6953                    (
unsigned int)ntohl(rtcpheader[i + 1]),
 
 6954                    &rtcp_report->sender_information.ntp_timestamp);
 
 6955            rtcp_report->sender_information.rtp_timestamp = ntohl(rtcpheader[i + 2]);
 
 6958                        (
unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_sec,
 
 6959                        (
unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_usec);
 
 6960                ast_verbose(
"RTP timestamp: %u\n", rtcp_report->sender_information.rtp_timestamp);
 
 6962                        rtcp_report->sender_information.packet_count,
 
 6963                        rtcp_report->sender_information.octet_count);
 
 6974                report_block = 
ast_calloc(1, 
sizeof(*report_block));
 
 6975                if (!report_block) {
 
 6981                rtcp_report->report_block[0] = report_block;
 
 6986                report_block->
ia_jitter =  ntohl(rtcpheader[i + 3]);
 
 6987                report_block->
lsr = ntohl(rtcpheader[i + 4]);
 
 6988                report_block->
dlsr = ntohl(rtcpheader[i + 5]);
 
 6989                if (report_block->
lsr) {
 
 6993                        unsigned int lsr_now, lsw, msw;
 
 6994                        gettimeofday(&now, 
NULL);
 
 6996                        lsr_now = (((msw & 0xffff) << 16) | ((lsw & 0xffff0000) >> 16));
 
 6997                        ast_verbose(
"Internal RTCP NTP clock skew detected: " 
 6998                            "lsr=%u, now=%u, dlsr=%u (%u:%03ums), " 
 7000                            report_block->
lsr, lsr_now, report_block->
dlsr, report_block->
dlsr / 65536,
 
 7001                            (report_block->
dlsr % 65536) * 1000 / 65536,
 
 7002                            report_block->
dlsr - (lsr_now - report_block->
lsr));
 
 7023                    ast_verbose(
"  Last SR(our NTP): %lu.%010lu\n",(
unsigned long)(report_block->
lsr) >> 16,((
unsigned long)(report_block->
lsr) << 16) * 4096);
 
 7024                    ast_verbose(
"  DLSR: %4.4f (sec)\n",(
double)report_block->
dlsr / 65536.0);
 
 7064            transport_rtp->
f.
src = 
"RTP";
 
 7066            f = &transport_rtp->
f;
 
 7077                    ast_verbose(
"Received generic RTCP NACK message\n");
 
 7094                    ast_verbose(
"Received an RTCP Fast Update Request\n");
 
 7101                transport_rtp->
f.
src = 
"RTP";
 
 7102                f = &transport_rtp->
f;
 
 7117                feedback = transport_rtp->
f.
data.
ptr;
 
 7121                first_word = ntohl(rtcpheader[i + 2]);
 
 7122                feedback->
remb.
br_exp = (first_word >> 18) & ((1 << 6) - 1);
 
 7131                transport_rtp->
f.
src = 
"RTP";
 
 7132                f = &transport_rtp->
f;
 
 7143#ifdef TEST_FRAMEWORK 
 7144            if ((test_engine = ast_rtp_instance_get_test(instance))) {
 
 7145                test_engine->sdes_received = 1;
 
 
 7181    if ((res = 
rtcp_recvfrom(instance, read_area, read_area_size,
 
 7190        if (
errno != EAGAIN) {
 
 7204        struct sockaddr_in addr_tmp;
 
 7214            ast_debug_stun(2, 
"(%p) STUN cannot do for non IPv4 address %s\n",
 
 
 7234    int res = 0, payload = 0, bridged_payload = 0, mark;
 
 7239    unsigned int timestamp = ntohl(rtpheader[1]);
 
 7247    if (!payload_type) {
 
 7253        payload_type->asterisk_format, payload_type->format, payload_type->rtp_code, payload_type->sample_rate);
 
 7256    if (bridged_payload < 0) {
 
 7262        ast_debug_rtp(1, 
"(%p, %p) RTP unsupported payload type received\n", instance, instance1);
 
 7272        ast_debug_rtp(1, 
"(%p, %p) RTP feeding packet with duplicate timestamp to core\n", instance, instance1);
 
 7276    if (payload_type->asterisk_format) {
 
 7304        ast_debug_rtp(1, 
"(%p, %p) RTP Feeding packet to core until DTMF finishes\n", instance, instance1);
 
 7310    if (payload_type->asterisk_format) {
 
 7318            ast_debug_rtp(1, 
"(%p, %p) RTP asymmetric RTP codecs detected (TX: %s, RX: %s) sending frame to core\n",
 
 7332        ast_debug_rtp(5, 
"(%p, %p) RTP remote address is null, most likely RTP has been stopped\n",
 
 7333            instance, instance1);
 
 7359        bridged->
ssrc = ntohl(rtpheader[2]);
 
 7363    res = 
rtp_sendto(instance1, (
void *)rtpheader, 
len, 0, &remote_address, &ice);
 
 7367                "RTP Transmission error of packet to %s: %s\n",
 
 7373                    "RTP NAT: Can't write RTP to private " 
 7374                    "address %s, waiting for other end to " 
 7386        ast_verbose(
"Sent RTP P2P packet to %s%s (type %-2.2d, len %-6.6d)\n",
 
 7388                ice ? 
" (via ICE)" : 
"",
 
 7389                bridged_payload, 
len - hdrlen);
 
 
 7407    return a.seqno - 
b.seqno;
 
 
 7411    uint16_t *status_vector_chunk, 
int status)
 
 7414    *status_vector_chunk_bits -= 2;
 
 7420    *status_vector_chunk |= (
status << (16 - 2 - (14 - *status_vector_chunk_bits)));
 
 7423    if (*status_vector_chunk_bits) {
 
 7429    *status_vector_chunk_bits = 14;
 
 7435    *status_vector_chunk = (1 << 15) | (1 << 14);
 
 
 7440    uint16_t *status_vector_chunk, 
int *run_length_chunk_count, 
int *run_length_chunk_status, 
int status)
 
 7442    if (*run_length_chunk_status != 
status) {
 
 7443        while (*run_length_chunk_count > 0 && *run_length_chunk_count < 8) {
 
 7451                status_vector_chunk, *run_length_chunk_status);
 
 7452            *run_length_chunk_count -= 1;
 
 7455        if (*run_length_chunk_count) {
 
 7457            put_unaligned_uint16(rtcpheader + *packet_len, htons((0 << 15) | (*run_length_chunk_status << 13) | *run_length_chunk_count));
 
 7462        *run_length_chunk_count = 0;
 
 7463        *run_length_chunk_status = -1;
 
 7465        if (*status_vector_chunk_bits == 14) {
 
 7467            *run_length_chunk_status = 
status;
 
 7468            *run_length_chunk_count = 1;
 
 7472                status_vector_chunk, 
status);
 
 7476        *run_length_chunk_count += 1;
 
 
 7484    unsigned char *rtcpheader;
 
 7489    int status_vector_chunk_bits = 14;
 
 7490    uint16_t status_vector_chunk = (1 << 15) | (1 << 14);
 
 7491    int run_length_chunk_count = 0;
 
 7492    int run_length_chunk_status = -1;
 
 7493    int packet_len = 20;
 
 7495    int packet_count = 0;
 
 7496    unsigned int received_msw;
 
 7497    unsigned int received_lsw;
 
 7501    unsigned int large_delta_count = 0;
 
 7502    unsigned int small_delta_count = 0;
 
 7503    unsigned int lost_count = 0;
 
 7518    rtcpheader = (
unsigned char *)bdata;
 
 7522    previous_packet = first_packet;
 
 7549                &status_vector_chunk, &run_length_chunk_count, &run_length_chunk_status, 0);
 
 7555            if (packet_len + delta_len + 20 > 
sizeof(bdata)) {
 
 7575                &status_vector_chunk, &run_length_chunk_count, &run_length_chunk_status, 2);
 
 7577            large_delta_count++;
 
 7581                &status_vector_chunk, &run_length_chunk_count, &run_length_chunk_status, 1);
 
 7583            small_delta_count++;
 
 7589        if (packet_len + delta_len + 20 > 
sizeof(bdata)) {
 
 7594    if (status_vector_chunk_bits != 14) {
 
 7598    } 
else if (run_length_chunk_count) {
 
 7600        put_unaligned_uint16(rtcpheader + packet_len, htons((0 << 15) | (run_length_chunk_status << 13) | run_length_chunk_count));
 
 7629    while (packet_len % 4) {
 
 7630        rtcpheader[packet_len++] = 0;
 
 7649    ast_debug_rtcp(2, 
"(%p) RTCP sending transport-cc feedback packet of size '%d' on '%s' with packet count of %d (small = %d, large = %d, lost = %d)\n",
 
 7652    res = 
rtcp_sendto(instance, (
unsigned int *)rtcpheader, packet_len, 0, &remote_address, &ice);
 
 
 7668    unsigned char *data, 
int len)
 
 7670    uint16_t *
seqno = (uint16_t *)data;
 
 7710        ast_debug_rtcp(1, 
"(%p) RTCP starting transport-cc feedback transmission on RTP instance '%p'\n", instance, transport);
 
 7716            ast_log(
LOG_WARNING, 
"Scheduling RTCP transport-cc feedback transmission failed on RTP instance '%p'\n",
 
 
 7729    if (transport_wide_cc_id == -1) {
 
 7736        int extension_len = (
extension[pos] & 0xF) + 1;
 
 7753        } 
else if (
id == 15) {
 
 7763        } 
else if ((pos + extension_len) > 
len) {
 
 7771        if (
id == transport_wide_cc_id) {
 
 7776        pos += extension_len;
 
 
 7781    const struct ast_sockaddr *remote_address, 
unsigned char *read_area, 
int length, 
int prev_seqno,
 
 7782    unsigned int bundled)
 
 7784    unsigned int *rtpheader = (
unsigned int*)(read_area);
 
 7787    int res = length, hdrlen = 12, ssrc, seqno, payloadtype, padding, mark, 
ext, cc;
 
 7788    unsigned int timestamp;
 
 7804    ssrc = ntohl(rtpheader[2]);
 
 7805    seqno = ntohl(rtpheader[0]);
 
 7806    payloadtype = (seqno & 0x7f0000) >> 16;
 
 7807    padding = seqno & (1 << 29);
 
 7808    mark = seqno & (1 << 23);
 
 7809    ext = seqno & (1 << 28);
 
 7810    cc = (seqno & 0xF000000) >> 24;
 
 7812    timestamp = ntohl(rtpheader[1]);
 
 7818        res -= read_area[res - 1];
 
 7828        int extensions_size = (ntohl(rtpheader[hdrlen/4]) & 0xffff) << 2;
 
 7829        unsigned int profile;
 
 7830        profile = (ntohl(rtpheader[3]) & 0xffff0000) >> 16;
 
 7832        if (profile == 0xbede) {
 
 7836            if (profile == 0x505a) {
 
 7837                ast_log(
LOG_DEBUG, 
"Found Zfone extension in RTP stream - zrtp - not supported.\n");
 
 7844        hdrlen += extensions_size;
 
 7865                    ast_debug(0, 
"(%p) RTP forcing Marker bit, because SSRC has changed\n", instance);
 
 7907    if ((
int)prev_seqno - (
int)
seqno  > 100) 
 
 7917        struct timeval rxtime;
 
 7939    if (!payload->asterisk_format) {
 
 7949        } 
else if (payload->rtp_code == 
AST_RTP_CN) {
 
 8003    rtp->
f.
data.
ptr = read_area + hdrlen;
 
 8010        && ((
int)
seqno - (prev_seqno + 1) > 0)
 
 8011        && ((
int)
seqno - (prev_seqno + 1) < 10)) {
 
 8023        unsigned char *header_end;
 
 8024        int num_generations;
 
 8027        int diff =(int)
seqno - (prev_seqno+1); 
 
 8032        if (header_end == 
NULL) {
 
 8037        header_length = header_end - 
data;
 
 8038        num_generations = header_length / 4;
 
 8039        len = header_length;
 
 8042            for (x = 0; x < num_generations; x++)
 
 8050        } 
else if (diff > num_generations && diff < 10) {
 
 8060            for ( x = 0; x < num_generations - diff; x++)
 
 
 8111struct rtp_drop_packets_data {
 
 8113    unsigned int use_random_num;
 
 8115    unsigned int use_random_interval;
 
 8118    unsigned int num_to_drop;
 
 8120    unsigned int num_dropped;
 
 8123    struct timeval interval;
 
 8125    struct timeval next;
 
 8132static struct rtp_drop_packets_data drop_packets_data;
 
 8134static void drop_packets_data_update(
struct timeval tv)
 
 8143    drop_packets_data.num_dropped = drop_packets_data.use_random_num ?
 
 8144        ast_random() % drop_packets_data.num_to_drop : 0;
 
 8152    if (drop_packets_data.use_random_interval) {
 
 8155            &drop_packets_data.interval) * ((
double)(
ast_random() % 100 + 1) / 100),
 
 8158        drop_packets_data.next = 
ast_tvadd(tv, interval);
 
 8160        drop_packets_data.next = 
ast_tvadd(tv, drop_packets_data.interval);
 
 8164static int should_drop_packets(
struct ast_sockaddr *addr)
 
 8168    if (!drop_packets_data.num_to_drop) {
 
 8177        (drop_packets_data.port ?
 
 8185    if (drop_packets_data.num_dropped < drop_packets_data.num_to_drop) {
 
 8186        ++drop_packets_data.num_dropped;
 
 8195    if (
ast_tvzero(drop_packets_data.interval)) {
 
 8197        drop_packets_data.num_to_drop = 0;
 
 8202    if (
ast_tvcmp(tv, drop_packets_data.next) == -1) {
 
 8212    drop_packets_data_update(tv);
 
 8225    int res, hdrlen = 12, 
version, payloadtype;
 
 8228    unsigned int *rtpheader = (
unsigned int*)(read_area), seqno, ssrc, timestamp, prev_seqno;
 
 8232    unsigned int bundled;
 
 8243    if ((res = 
rtp_recvfrom(instance, read_area, read_area_size, 0,
 
 8252        if (
errno != EAGAIN) {
 
 8274        for (i = 0; i < res; ++i) {
 
 8275            if (read_area[i] != 
'\0') {
 
 8284    seqno = ntohl(rtpheader[0]);
 
 8289        struct sockaddr_in addr_tmp;
 
 8298            ast_debug_stun(1, 
"(%p) STUN cannot do for non IPv4 address %s\n",
 
 8316    ssrc = ntohl(rtpheader[2]);
 
 8327    if (child != instance) {
 
 8385            ast_verb(4, 
"%p -- Strict RTP learning complete - Locking on source address %s\n",
 
 8414                ast_verb(4, 
"%p -- Strict RTP switching to RTP target address %s as source\n",
 
 8433                    ast_verb(4, 
"%p -- Strict RTP qualifying stream type: %s\n",
 
 8438                    ast_verb(4, 
"%p -- Strict RTP switching source address to %s\n",
 
 8445                ast_debug_rtp(1, 
"(%p) RTP %p -- Received packet from %s, dropping due to strict RTP protection. Will switch to it in %d packets.\n",
 
 8454                ast_debug_rtp(1, 
"(%p) RTP %p -- Received packet from %s, dropping due to strict RTP protection. Qualifying new stream.\n",
 
 8473        ast_debug_rtp(1, 
"(%p) RTP %p -- Received packet from %s, dropping due to strict RTP protection.\n",
 
 8475#ifdef TEST_FRAMEWORK 
 8477        static int strict_rtp_test_event = 1;
 
 8478        if (strict_rtp_test_event) {
 
 8481            strict_rtp_test_event = 0; 
 
 8502                ast_debug(0, 
"(%p) RTP NAT: Got audio from other end. Now sending to address %s\n",
 
 8508    payloadtype = (seqno & 0x7f0000) >> 16;
 
 8510    timestamp = ntohl(rtpheader[1]);
 
 8513    if (should_drop_packets(&addr)) {
 
 8514        ast_debug(0, 
"(%p) RTP: drop received packet from %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6d)\n",
 
 8521        ast_verbose(
"Got  RTP packet from    %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6d)\n",
 
 8523                payloadtype, seqno, timestamp, res - hdrlen);
 
 8537        frame = 
ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno, bundled);
 
 8552            frame = 
ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno, bundled);
 
 8559            ast_debug_rtp(2, 
"(%p) RTP Packet with sequence number '%d' on instance is no longer missing\n",
 
 8567            frame = 
ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno, bundled);
 
 8602            ast_debug_rtp(2, 
"(%p) RTP pulled buffered packet with sequence number '%d' to additionally return\n",
 
 8603                instance, frame->
seqno);
 
 8624            ast_debug_rtp(2, 
"(%p) RTP source has wild gap or packet loss, sending FIR\n",
 
 8642                    ast_debug_rtp(2, 
"(%p) RTP inserted just received packet with sequence number '%d' in correct order\n",
 
 8667                    ast_debug_rtp(2, 
"(%p) RTP emptying queue and returning packet with sequence number '%d'\n",
 
 8668                        instance, frame->
seqno);
 
 8685            frame = 
ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno, bundled);
 
 8692            ast_debug_rtp(2, 
"(%p) RTP adding just received packet with sequence number '%d' to end of dumped queue\n",
 
 8727        ast_debug_rtp(2, 
"(%p) RTP received an old packet with sequence number '%d', dropping it\n",
 
 8732        ast_debug_rtp(2, 
"(%p) RTP received a duplicate transmission of packet with sequence number '%d', dropping it\n",
 
 8740        unsigned int missing_seqnos_added = 0;
 
 8742        ast_debug_rtp(2, 
"(%p) RTP received an out of order packet with sequence number '%d' while expecting '%d' from the future\n",
 
 8745        payload = 
ast_malloc(
sizeof(*payload) + res);
 
 8756        payload->
size = res;
 
 8757        memcpy(payload->
buf, rtpheader, res);
 
 8769        if (!remove_failed) {
 
 8770            ast_debug_rtp(2, 
"(%p) RTP packet with sequence number '%d' is no longer missing\n",
 
 8779        missing_seqno = seqno;
 
 8780        while (remove_failed) {
 
 8784            if (missing_seqno < 0) {
 
 8785                missing_seqno = 65535;
 
 8789            if (missing_seqno == prev_seqno) {
 
 8817            ast_debug_rtp(2, 
"(%p) RTP added missing sequence number '%d'\n",
 
 8818                instance, missing_seqno);
 
 8821            missing_seqnos_added++;
 
 8846            rtcpheader = 
ast_malloc(
sizeof(*rtcpheader) + data_size);
 
 8848                ast_debug_rtcp(1, 
"(%p) RTCP failed to allocate memory for NACK\n", instance);
 
 8852            memset(rtcpheader, 0, data_size);
 
 8856            if (res == 0 || res == 1) {
 
 8865                ast_debug_rtcp(1, 
"(%p) RTCP failed to construct NACK, stopping here\n", instance);
 
 8871            res = 
rtcp_sendto(instance, rtcpheader, packet_len, 0, &remote_address, &ice);
 
 8873                ast_debug_rtcp(1, 
"(%p) RTCP failed to send NACK request out\n", instance);
 
 8875                ast_debug_rtcp(2, 
"(%p) RTCP sending a NACK request to get missing packets\n", instance);
 
 
 8895                ast_debug_rtcp(1, 
"(%p) RTCP ignoring duplicate property\n", instance);
 
 8905#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
 8906                rtp->
rtcp->dtls.timeout_timer = -1;
 
 8946                               AF_INET6 : -1)) < 0) {
 
 8947                    ast_debug_rtcp(1, 
"(%p) RTCP failed to create a new socket\n", instance);
 
 8956                    ast_debug_rtcp(1, 
"(%p) RTCP failed to setup RTP instance\n", instance);
 
 8957                    close(rtp->
rtcp->
s);
 
 8963#ifdef HAVE_PJPROJECT 
 8968#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
 8969                dtls_setup_rtcp(instance);
 
 8982                if (rtp->
rtcp->
s > -1 && rtp->
rtcp->
s != rtp->
s) {
 
 8983                    close(rtp->
rtcp->
s);
 
 8988#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
 8989                if (rtp->
rtcp->dtls.ssl && rtp->
rtcp->dtls.ssl != rtp->dtls.ssl) {
 
 8990                    SSL_free(rtp->
rtcp->dtls.ssl);
 
 8992                rtp->
rtcp->dtls.ssl = rtp->dtls.ssl;
 
 9007                        ast_debug_rtcp(1, 
"(%p) RTCP failed to tear down RTCP\n", instance);
 
 9019                        ast_debug_rtcp(1, 
"(%p) RTCP failed to tear down transport-cc feedback\n", instance);
 
 9026                if (rtp->
rtcp->
s > -1 && rtp->
rtcp->
s != rtp->
s) {
 
 9027                    close(rtp->
rtcp->
s);
 
 9029#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
 9031                dtls_srtp_stop_timeout_timer(instance, rtp, 1);
 
 9034                if (rtp->
rtcp->dtls.ssl && rtp->
rtcp->dtls.ssl != rtp->dtls.ssl) {
 
 9035                    SSL_free(rtp->
rtcp->dtls.ssl);
 
 
 9101        ast_debug_rtcp(1, 
"(%p) RTCP setting address on RTP instance\n", instance);
 
 9133        ast_verb(4, 
"%p -- Strict RTP learning after remote address set to: %s\n",
 
 
 9181    for (x = 0; x < generations; x++) {
 
 9182        rtp->
red->
pt[x] = payloads[x];
 
 9183        rtp->
red->
pt[x] |= 1 << 7; 
 
 
 9204            const unsigned char *primary = red->
buf_data;
 
 9207            if (primary[0] == 0x08 || primary[0] == 0x0a || primary[0] == 0x0d) {
 
 9212                if (primary[0] == 0x08 || primary[0] == 0x0a || primary[0] == 0x0d) {
 
 
 9341    struct sockaddr_in suggestion_tmp;
 
 
 9358#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
 9362    dtls_srtp_stop_timeout_timer(instance, rtp, 0);
 
 9364        dtls_srtp_stop_timeout_timer(instance, rtp, 1);
 
 
 9418    unsigned int *rtpheader;
 
 9420    int res, payload = 0;
 
 9434    level = 127 - (level & 0x7f);
 
 9439    rtpheader = (
unsigned int *)data;
 
 9440    rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->
seqno));
 
 9441    rtpheader[1] = htonl(rtp->
lastts);
 
 9442    rtpheader[2] = htonl(rtp->
ssrc);
 
 9445    res = 
rtp_sendto(instance, (
void *) rtpheader, hdrlen + 1, 0, &remote_address, &ice);
 
 9453        ast_verbose(
"Sent Comfort Noise RTP packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
 
 9455                ice ? 
" (via ICE)" : 
"",
 
 
 9545    if (child_rtp->
bundled == parent) {
 
 9590#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
 9595        dtls_srtp_add_local_ssrc(parent_rtp, parent, 0, child_rtp->
ssrc, 0);
 
 
 9611#ifdef HAVE_PJPROJECT 
 9612static void stunaddr_resolve_callback(
const struct ast_dns_query *query)
 
 9616    const char *stunaddr_resolved_str;
 
 9618    if (!store_stunaddr_resolved(query)) {
 
 9619        ast_log(
LOG_WARNING, 
"Failed to resolve stunaddr '%s'. Cancelling recurring resolution.\n", stunaddr_name);
 
 9628        ast_debug_stun(2, 
"Resolved stunaddr '%s' to '%s'. Lowest TTL = %d.\n",
 
 9630            stunaddr_resolved_str,
 
 9639static int store_stunaddr_resolved(
const struct ast_dns_query *query)
 
 9649        if (
rr_type == ns_t_a && data_size == 4) {
 
 9651            memcpy(&stunaddr.sin_addr, 
data, data_size);
 
 9652            stunaddr.sin_family = AF_INET;
 
 9657            ast_debug_stun(3, 
"Unrecognized rr_type '%u' or data_size '%zu' from DNS query for stunaddr '%s'\n",
 
 9665static void clean_stunaddr(
void) {
 
 9666    if (stunaddr_resolver) {
 
 9668            ast_log(
LOG_ERROR, 
"Failed to cancel recurring DNS resolution of previous stunaddr.\n");
 
 9670        ao2_ref(stunaddr_resolver, -1);
 
 9671        stunaddr_resolver = 
NULL;
 
 9674    memset(&stunaddr, 0, 
sizeof(stunaddr));
 
 9679#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
 9686#ifdef HAVE_PJPROJECT 
 9692    ast_debug_dtls(3, 
"(%p) DTLS - ast_rtp_activate rtp=%p - setup and perform DTLS'\n", instance, rtp);
 
 9694    dtls_perform_setup(&rtp->dtls);
 
 9695    dtls_perform_handshake(instance, &rtp->dtls, 0);
 
 9698        dtls_perform_setup(&rtp->
rtcp->dtls);
 
 9699        dtls_perform_handshake(instance, &rtp->
rtcp->dtls, 1);
 
 9709    char *debughost = 
NULL;
 
 9710    char *debugport = 
NULL;
 
 9713        ast_cli(
a->fd, 
"Lookup failed for '%s'\n", arg);
 
 9717    ast_cli(
a->fd, 
"RTP Packet Debugging Enabled for address: %s\n",
 
 
 9726    char *debughost = 
NULL;
 
 9727    char *debugport = 
NULL;
 
 9730        ast_cli(
a->fd, 
"Lookup failed for '%s'\n", arg);
 
 9734    ast_cli(
a->fd, 
"RTCP Packet Debugging Enabled for address: %s\n",
 
 
 9744        e->
command = 
"rtp set debug {on|off|ip}";
 
 9746            "Usage: rtp set debug {on|off|ip host[:port]}\n" 
 9747            "       Enable/Disable dumping of all RTP packets. If 'ip' is\n" 
 9748            "       specified, limit the dumped packets to those to and from\n" 
 9749            "       the specified 'host' with optional port.\n";
 
 9755    if (
a->argc == e->
args) { 
 
 9756        if (!strncasecmp(
a->argv[e->
args-1], 
"on", 2)) {
 
 9759            ast_cli(
a->fd, 
"RTP Packet Debugging Enabled\n");
 
 9761        } 
else if (!strncasecmp(
a->argv[e->
args-1], 
"off", 3)) {
 
 9763            ast_cli(
a->fd, 
"RTP Packet Debugging Disabled\n");
 
 9766    } 
else if (
a->argc == e->
args +1) { 
 
 
 9776#ifdef HAVE_PJPROJECT 
 9777    struct sockaddr_in stunaddr_copy;
 
 9781        e->
command = 
"rtp show settings";
 
 9783            "Usage: rtp show settings\n" 
 9784            "       Display RTP configuration settings\n";
 
 9794    ast_cli(
a->fd, 
"\n\nGeneral Settings:\n");
 
 9795    ast_cli(
a->fd, 
"----------------\n");
 
 9809#ifdef HAVE_PJPROJECT 
 9813    memcpy(&stunaddr_copy, &stunaddr, 
sizeof(stunaddr));
 
 9815    ast_cli(
a->fd, 
"  STUN address:    %s:%d\n", 
ast_inet_ntoa(stunaddr_copy.sin_addr), htons(stunaddr_copy.sin_port));
 
 
 9825        e->
command = 
"rtcp set debug {on|off|ip}";
 
 9827            "Usage: rtcp set debug {on|off|ip host[:port]}\n" 
 9828            "       Enable/Disable dumping of all RTCP packets. If 'ip' is\n" 
 9829            "       specified, limit the dumped packets to those to and from\n" 
 9830            "       the specified 'host' with optional port.\n";
 
 9836    if (
a->argc == e->
args) { 
 
 9837        if (!strncasecmp(
a->argv[e->
args-1], 
"on", 2)) {
 
 9840            ast_cli(
a->fd, 
"RTCP Packet Debugging Enabled\n");
 
 9842        } 
else if (!strncasecmp(
a->argv[e->
args-1], 
"off", 3)) {
 
 9844            ast_cli(
a->fd, 
"RTCP Packet Debugging Disabled\n");
 
 9847    } 
else if (
a->argc == e->
args +1) { 
 
 
 9858        e->
command = 
"rtcp set stats {on|off}";
 
 9860            "Usage: rtcp set stats {on|off}\n" 
 9861            "       Enable/Disable dumping of RTCP stats.\n";
 
 9867    if (
a->argc != e->
args)
 
 9870    if (!strncasecmp(
a->argv[e->
args-1], 
"on", 2))
 
 9872    else if (!strncasecmp(
a->argv[e->
args-1], 
"off", 3))
 
 
 9883static unsigned int use_random(
struct ast_cli_args *
a, 
int pos, 
unsigned int index)
 
 9886        !strcasecmp(
a->argv[index - 1], 
"random");
 
 9891    static const char * 
const completions_2[] = { 
"stop", 
"<N>", 
NULL };
 
 9892    static const char * 
const completions_3[] = { 
"random", 
"incoming packets", 
NULL };
 
 9893    static const char * 
const completions_5[] = { 
"on", 
"every", 
NULL };
 
 9894    static const char * 
const completions_units[] = { 
"random", 
"usec", 
"msec", 
"sec", 
"min", 
NULL };
 
 9896    unsigned int use_random_num = 0;
 
 9897    unsigned int use_random_interval = 0;
 
 9898    unsigned int num_to_drop = 0;
 
 9899    unsigned int interval = 0;
 
 9900    const char *interval_s = 
NULL;
 
 9901    const char *unit_s = 
NULL;
 
 9903    const char *addr_s = 
NULL;
 
 9909            "Usage: rtp drop [stop|[<N> [random] incoming packets[ every <N> [random] {usec|msec|sec|min}][ on <ip[:port]>]]\n" 
 9910            "       Drop RTP incoming packets.\n";
 
 9913        use_random_num = use_random(
a, 
a->pos, 4);
 
 9914        use_random_interval = use_random(
a, 
a->pos, 8 + use_random_num) ||
 
 9915            use_random(
a, 
a->pos, 10 + use_random_num);
 
 9917        switch (
a->pos - use_random_num - use_random_interval) {
 
 9925            if (!strcasecmp(
a->argv[
a->pos - 2], 
"on")) {
 
 9931            if (!strcasecmp(
a->argv[
a->pos - 2 - use_random_interval], 
"every")) {
 
 9936            if (!strcasecmp(
a->argv[
a->pos - 3 - use_random_interval], 
"every")) {
 
 9949    use_random_num = use_random(
a, 
a->argc, 4);
 
 9950    use_random_interval = use_random(
a, 
a->argc, 8 + use_random_num) ||
 
 9951        use_random(
a, 
a->argc, 10 + use_random_num);
 
 9953    if (!strcasecmp(
a->argv[2], 
"stop")) {
 
 9955    } 
else if (
a->argc < 5) {
 
 9958        ast_cli(
a->fd, 
"%s is not a valid number of packets to drop\n", 
a->argv[2]);
 
 9960    } 
else if (
a->argc - use_random_num == 5) {
 
 9962    } 
else if (
a->argc - use_random_num >= 7 && !strcasecmp(
a->argv[5 + use_random_num], 
"on")) {
 
 9964        addr_s = 
a->argv[6 + use_random_num];
 
 9965        if (
a->argc - use_random_num - use_random_interval == 10 &&
 
 9966                !strcasecmp(
a->argv[7 + use_random_num], 
"every")) {
 
 9968            interval_s = 
a->argv[8 + use_random_num];
 
 9969            unit_s = 
a->argv[9 + use_random_num + use_random_interval];
 
 9971    } 
else if (
a->argc - use_random_num >= 8 && !strcasecmp(
a->argv[5 + use_random_num], 
"every")) {
 
 9973        interval_s = 
a->argv[6 + use_random_num];
 
 9974        unit_s = 
a->argv[7 + use_random_num + use_random_interval];
 
 9975        if (
a->argc == 10 + use_random_num + use_random_interval &&
 
 9976                !strcasecmp(
a->argv[8 + use_random_num + use_random_interval], 
"on")) {
 
 9978            addr_s = 
a->argv[9 + use_random_num + use_random_interval];
 
 9984    if (
a->argc - use_random_num >= 8 && !interval_s && !addr_s) {
 
 9989        ast_cli(
a->fd, 
"%s is not a valid interval number\n", interval_s);
 
 9993    memset(&addr, 0, 
sizeof(addr));
 
 9995        ast_cli(
a->fd, 
"%s is not a valid hostname[:port]\n", addr_s);
 
 9999    drop_packets_data.use_random_num = use_random_num;
 
10000    drop_packets_data.use_random_interval = use_random_interval;
 
10001    drop_packets_data.num_to_drop = num_to_drop;
 
10018    AST_CLI_DEFINE(handle_cli_rtp_drop_incoming_packets, 
"Drop RTP incoming packets"),
 
 
10028#ifdef HAVE_PJPROJECT 
10030    struct ast_ice_host_candidate *candidate;
 
10031    int acl_subscription_flag = 0;
 
10060#ifdef HAVE_PJPROJECT 
10065    turnaddr = pj_str(
NULL);
 
10066    turnusername = pj_str(
NULL);
 
10067    turnpassword = pj_str(
NULL);
 
10068    host_candidate_overrides_clear();
 
10071#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
10103            ast_log(
LOG_WARNING, 
"Disabling RTP checksums is not supported on this operating system!\n");
 
10109            ast_log(
LOG_WARNING, 
"DTMF timeout of '%d' outside range, using default of '%d' instead\n",
 
10117        } 
else if (!strcasecmp(s, 
"seqno")) {
 
10125            ast_log(
LOG_WARNING, 
"Value for 'probation' could not be read, using default of '%d' instead\n",
 
10134#ifdef HAVE_PJPROJECT 
10139        stun_software_attribute = 
ast_true(s);
 
10142        char *hostport, *host, *port;
 
10149            ast_debug_stun(3, 
"stunaddr = '%s' does not need name resolution\n",
 
10161            stunaddr.sin_port = htons(port_parsed);
 
10164                &stunaddr_resolve_callback, 
NULL);
 
10165            if (!stunaddr_resolver) {
 
10166                ast_log(
LOG_ERROR, 
"Failed to setup recurring DNS resolution of stunaddr '%s'",
 
10174        struct sockaddr_in addr;
 
10179            pj_strdup2_with_null(pool, &turnaddr, 
ast_inet_ntoa(addr.sin_addr));
 
10182            turnport = ntohs(addr.sin_port);
 
10186        pj_strdup2_with_null(pool, &turnusername, s);
 
10189        pj_strdup2_with_null(pool, &turnpassword, s);
 
10195        unsigned int include_local_address = 0;
 
10206        sep = strchr(
var->value,
',');
 
10211            include_local_address = strcmp(sep, 
"include_local_address") == 0;
 
10219        if (!(candidate = 
ast_calloc(1, 
sizeof(*candidate)))) {
 
10224        candidate->include_local = include_local_address;
 
10240        const char* sense = 
NULL;
 
10242        if (strncasecmp(
var->name, 
"ice_", 4) == 0) {
 
10243            sense = 
var->name + 4;
 
10245        } 
else if (strncasecmp(
var->name, 
"stun_", 5) == 0) {
 
10246            sense = 
var->name + 5;
 
10252        if (strcasecmp(sense, 
"blacklist") == 0) {
 
10256        if (strcasecmp(sense, 
"acl") && strcasecmp(sense, 
"permit") && strcasecmp(sense, 
"deny")) {
 
10273#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) 
10275        if ((sscanf(s, 
"%d", &dtls_mtu) != 1) || dtls_mtu < 256) {
 
10276            ast_log(
LOG_WARNING, 
"Value for 'dtls_mtu' could not be read, using default of '%d' instead\n",
 
 
10307#ifdef HAVE_PJPROJECT 
10308static void rtp_terminate_pjproject(
void)
 
10310    pj_thread_register_check();
 
10312    if (timer_thread) {
 
10313        timer_terminate = 1;
 
10314        pj_thread_join(timer_thread);
 
10315        pj_thread_destroy(timer_thread);
 
10335#ifdef HAVE_PJPROJECT 
10341    if (pj_init() != PJ_SUCCESS) {
 
10345    if (pjlib_util_init() != PJ_SUCCESS) {
 
10346        rtp_terminate_pjproject();
 
10350    if (pjnath_init() != PJ_SUCCESS) {
 
10351        rtp_terminate_pjproject();
 
10357    pool = pj_pool_create(&
cachingpool.factory, 
"timer", 512, 512, 
NULL);
 
10359    if (pj_timer_heap_create(pool, 100, &timer_heap) != PJ_SUCCESS) {
 
10360        rtp_terminate_pjproject();
 
10364    if (pj_lock_create_recursive_mutex(pool, 
"rtp%p", &
lock) != PJ_SUCCESS) {
 
10365        rtp_terminate_pjproject();
 
10369    pj_timer_heap_set_lock(timer_heap, 
lock, PJ_TRUE);
 
10371    if (pj_thread_create(pool, 
"timer", &timer_worker_thread, 
NULL, 0, 0, &timer_thread) != PJ_SUCCESS) {
 
10372        rtp_terminate_pjproject();
 
10378#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) && defined(HAVE_OPENSSL_BIO_METHOD) 
10379    dtls_bio_methods = BIO_meth_new(BIO_TYPE_BIO, 
"rtp write");
 
10380    if (!dtls_bio_methods) {
 
10381#ifdef HAVE_PJPROJECT 
10382        rtp_terminate_pjproject();
 
10386    BIO_meth_set_write(dtls_bio_methods, dtls_bio_write);
 
10387    BIO_meth_set_ctrl(dtls_bio_methods, dtls_bio_ctrl);
 
10388    BIO_meth_set_create(dtls_bio_methods, dtls_bio_new);
 
10389    BIO_meth_set_destroy(dtls_bio_methods, dtls_bio_free);
 
10393#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) && defined(HAVE_OPENSSL_BIO_METHOD) 
10394        BIO_meth_free(dtls_bio_methods);
 
10396#ifdef HAVE_PJPROJECT 
10397        rtp_terminate_pjproject();
 
10403#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) && defined(HAVE_OPENSSL_BIO_METHOD) 
10404        BIO_meth_free(dtls_bio_methods);
 
10406#ifdef HAVE_PJPROJECT 
10408        rtp_terminate_pjproject();
 
 
10423#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) && defined(HAVE_OPENSSL_BIO_METHOD) 
10424    if (dtls_bio_methods) {
 
10425        BIO_meth_free(dtls_bio_methods);
 
10429#ifdef HAVE_PJPROJECT 
10430    host_candidate_overrides_clear();
 
10431    pj_thread_register_check();
 
10432    rtp_terminate_pjproject();
 
10435    rtp_unload_acl(&ice_acl_lock, &ice_acl);
 
10436    rtp_unload_acl(&stun_acl_lock, &stun_acl);
 
 
10449#ifdef HAVE_PJPROJECT
 
10450    .
requires = 
"res_pjproject",
 
Access Control of various sorts.
struct stasis_message_type * ast_named_acl_change_type(void)
a stasis_message_type for changes against a named ACL or the set of all named ACLs
int ast_ouraddrfor(const struct ast_sockaddr *them, struct ast_sockaddr *us)
Get our local IP address when contacting a remote host.
void ast_append_acl(const char *sense, const char *stuff, struct ast_acl_list **path, int *error, int *named_acl_flag)
Add a rule to an ACL struct.
int ast_find_ourip(struct ast_sockaddr *ourip, const struct ast_sockaddr *bindaddr, int family)
Find our IP address.
enum ast_acl_sense ast_apply_acl_nolog(struct ast_acl_list *acl_list, const struct ast_sockaddr *addr)
Apply a set of rules to a given IP address, don't log failure.
struct ast_acl_list * ast_free_acl_list(struct ast_acl_list *acl)
Free a list of ACLs.
void ast_cli_unregister_multiple(void)
static volatile unsigned int seq
void timersub(struct timeval *tvend, struct timeval *tvstart, struct timeval *tvdiff)
char * strsep(char **str, const char *delims)
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strndup(str, len)
A wrapper for strndup()
#define ast_strdup(str)
A wrapper for strdup()
#define ast_strdupa(s)
duplicate a string in memory from the stack
void ast_free_ptr(void *ptr)
free() wrapper
#define ast_calloc(num, len)
A wrapper for calloc()
#define ast_malloc(len)
A wrapper for malloc()
#define ao2_iterator_next(iter)
#define ao2_link(container, obj)
Add an object to a container.
@ AO2_ALLOC_OPT_LOCK_NOLOCK
@ AO2_ALLOC_OPT_LOCK_MUTEX
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
#define ao2_find(container, arg, flags)
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
#define ao2_replace(dst, src)
Replace one object reference with another cleaning up the original.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
#define ao2_alloc_options(data_size, destructor_fn, options)
void * ao2_object_get_lockaddr(void *obj)
Return the mutex lock address of an object.
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
#define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn)
Allocate and initialize a list container.
#define ao2_alloc(data_size, destructor_fn)
static struct stasis_subscription * acl_change_sub
static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
static char version[AST_MAX_EXTENSION]
static int answer(void *data)
General Asterisk PBX channel definitions.
Standard Command Line Interface.
#define AST_CLI_YESNO(x)
Return Yes or No depending on the argument.
#define AST_CLI_DEFINE(fn, txt,...)
int ast_cli_completion_add(char *value)
Add a result to a request for completion options.
void ast_cli(int fd, const char *fmt,...)
char * ast_cli_complete(const char *word, const char *const choices[], int pos)
#define ast_cli_register_multiple(e, len)
Register multiple commands.
static struct ao2_container * codecs
Registered codecs.
ast_media_type
Types of media.
unsigned int ast_codec_samples_count(struct ast_frame *frame)
Get the number of samples contained within a frame.
const char * ast_codec_media_type2str(enum ast_media_type type)
Conversion function to take a media type and turn it into a string.
static int reconstruct(int sign, int dqln, int y)
Conversion utility functions.
int ast_str_to_uint(const char *str, unsigned int *res)
Convert the given string to an unsigned integer.
void * ast_data_buffer_get(const struct ast_data_buffer *buffer, size_t pos)
Retrieve a data payload from the data buffer.
struct ast_data_buffer * ast_data_buffer_alloc(ast_data_buffer_free_callback free_fn, size_t size)
Allocate a data buffer.
size_t ast_data_buffer_count(const struct ast_data_buffer *buffer)
Return the number of payloads in a data buffer.
void ast_data_buffer_resize(struct ast_data_buffer *buffer, size_t size)
Resize a data buffer.
int ast_data_buffer_put(struct ast_data_buffer *buffer, size_t pos, void *payload)
Place a data payload at a position in the data buffer.
size_t ast_data_buffer_max(const struct ast_data_buffer *buffer)
Return the maximum number of payloads a data buffer can hold.
void * ast_data_buffer_remove(struct ast_data_buffer *buffer, size_t pos)
Remove a data payload from the data buffer.
void ast_data_buffer_free(struct ast_data_buffer *buffer)
Free a data buffer (and all held data payloads)
const struct ast_dns_record * ast_dns_record_get_next(const struct ast_dns_record *record)
Get the next DNS record.
int ast_dns_result_get_lowest_ttl(const struct ast_dns_result *result)
Retrieve the lowest TTL from a result.
const char * ast_dns_record_get_data(const struct ast_dns_record *record)
Retrieve the raw DNS record.
const struct ast_dns_record * ast_dns_result_get_records(const struct ast_dns_result *result)
Get the first record of a DNS Result.
struct ast_dns_result * ast_dns_query_get_result(const struct ast_dns_query *query)
Get the result information for a DNS query.
int ast_dns_record_get_rr_type(const struct ast_dns_record *record)
Get the resource record type of a DNS record.
const char * ast_dns_query_get_name(const struct ast_dns_query *query)
Get the name queried in a DNS query.
size_t ast_dns_record_get_data_size(const struct ast_dns_record *record)
Retrieve the size of the raw DNS record.
Internal DNS structure definitions.
DNS Recurring Resolution API.
int ast_dns_resolve_recurring_cancel(struct ast_dns_query_recurring *recurring)
Cancel an asynchronous recurring DNS resolution.
struct ast_dns_query_recurring * ast_dns_resolve_recurring(const char *name, int rr_type, int rr_class, ast_dns_resolve_callback callback, void *data)
Asynchronously resolve a DNS query, and continue resolving it according to the lowest TTL available.
static int replace(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
struct stasis_message_type * ast_rtp_rtcp_sent_type(void)
Message type for an RTCP message sent from this Asterisk instance.
struct stasis_message_type * ast_rtp_rtcp_received_type(void)
Message type for an RTCP message received from some external source.
Configuration File Parser.
struct ast_config * ast_config_load2(const char *filename, const char *who_asked, struct ast_flags flags)
Load a config file.
#define CONFIG_STATUS_FILEUNCHANGED
#define CONFIG_STATUS_FILEINVALID
int ast_parse_arg(const char *arg, enum ast_parse_flags flags, void *p_result,...)
The argument parsing routine.
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
@ CONFIG_FLAG_FILEUNCHANGED
const char * ast_variable_retrieve(struct ast_config *config, const char *category, const char *variable)
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Asterisk internal frame definitions.
#define ast_frame_byteswap_be(fr)
#define ast_frisolate(fr)
Makes a frame independent of any static storage.
void ast_frame_free(struct ast_frame *frame, int cache)
Frees a frame or list of frames.
@ AST_FRFLAG_HAS_SEQUENCE_NUMBER
@ AST_FRFLAG_HAS_TIMING_INFO
#define ast_frdup(fr)
Copies a frame.
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
ast_frame_type
Frame types.
struct ast_frame ast_null_frame
#define DEBUG_ATLEAST(level)
#define ast_debug(level,...)
Log a DEBUG message.
#define ast_verb(level,...)
struct ssl_ctx_st SSL_CTX
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
#define AST_RWLIST_REMOVE_CURRENT
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
#define AST_LIST_HEAD_INIT_NOLOCK(head)
Initializes a list head structure.
#define AST_LIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a list of specified type, statically initialized.
#define AST_LIST_HEAD_NOLOCK(name, type)
Defines a structure to be used to hold a list of specified type (with no lock).
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized.
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
#define AST_RWLIST_TRAVERSE_SAFE_END
#define AST_LIST_LOCK(head)
Locks a list.
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
#define AST_LIST_REMOVE(head, elm, field)
Removes a specific entry from a list.
#define AST_RWLIST_INSERT_TAIL
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
#define AST_LIST_FIRST(head)
Returns the first entry contained in a list.
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Asterisk locking-related definitions:
#define ast_rwlock_wrlock(a)
#define AST_RWLOCK_INIT_VALUE
#define ast_cond_init(cond, attr)
#define ast_cond_timedwait(cond, mutex, time)
#define ast_rwlock_rdlock(a)
pthread_cond_t ast_cond_t
#define ast_rwlock_unlock(a)
#define ast_cond_signal(cond)
#define AST_LOG_CATEGORY_DISABLED
#define AST_LOG_CATEGORY_ENABLED
#define ast_debug_category(sublevel, ids,...)
Log for a debug category.
int ast_debug_category_set_sublevel(const char *name, int sublevel)
Set the debug category's sublevel.
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
Asterisk module definitions.
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
@ AST_MODPRI_CHANNEL_DEPEND
@ AST_MODULE_SUPPORT_CORE
#define ASTERISK_GPL_KEY
The text the key() function should return.
@ AST_MODULE_LOAD_SUCCESS
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
int ast_sockaddr_ipv4_mapped(const struct ast_sockaddr *addr, struct ast_sockaddr *ast_mapped)
Convert an IPv4-mapped IPv6 address into an IPv4 address.
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
#define ast_sockaddr_port(addr)
Get the port number of a socket address.
static void ast_sockaddr_copy(struct ast_sockaddr *dst, const struct ast_sockaddr *src)
Copies the data from one ast_sockaddr to another.
int ast_sockaddr_is_ipv6(const struct ast_sockaddr *addr)
Determine if this is an IPv6 address.
int ast_bind(int sockfd, const struct ast_sockaddr *addr)
Wrapper around bind(2) that uses struct ast_sockaddr.
#define ast_sockaddr_from_sockaddr(addr, sa)
Converts a struct sockaddr to a struct ast_sockaddr.
int ast_sockaddr_cmp_addr(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares the addresses of two ast_sockaddr structures.
static char * ast_sockaddr_stringify_host(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return an address only, suitable for a URL (with brack...
ssize_t ast_sendto(int sockfd, const void *buf, size_t len, int flags, const struct ast_sockaddr *dest_addr)
Wrapper around sendto(2) that uses ast_sockaddr.
int ast_sockaddr_is_any(const struct ast_sockaddr *addr)
Determine if the address type is unspecified, or "any" address.
int ast_sockaddr_split_hostport(char *str, char **host, char **port, int flags)
Splits a string into its host and port components.
int ast_set_qos(int sockfd, int tos, int cos, const char *desc)
Set type of service.
#define ast_sockaddr_to_sin(addr, sin)
Converts a struct ast_sockaddr to a struct sockaddr_in.
int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags)
Parse an IPv4 or IPv6 address string.
static int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
Checks if the ast_sockaddr is null. "null" in this sense essentially means uninitialized,...
ssize_t ast_recvfrom(int sockfd, void *buf, size_t len, int flags, struct ast_sockaddr *src_addr)
Wrapper around recvfrom(2) that uses struct ast_sockaddr.
int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares two ast_sockaddr structures.
#define ast_sockaddr_set_port(addr, port)
Sets the port number of a socket address.
#define ast_sockaddr_from_sin(addr, sin)
Converts a struct sockaddr_in to a struct ast_sockaddr.
static void ast_sockaddr_setnull(struct ast_sockaddr *addr)
Sets address addr to null.
int ast_sockaddr_is_ipv4(const struct ast_sockaddr *addr)
Determine if the address is an IPv4 address.
const char * ast_inet_ntoa(struct in_addr ia)
thread-safe replacement for inet_ntoa().
Options provided by main asterisk program.
#define AST_PJPROJECT_INIT_LOG_LEVEL()
Get maximum log level pjproject was compiled with.
Core PBX routines and definitions.
static char * generate_random_string(char *buf, size_t size)
Generate 32 byte random string (stolen from chan_sip.c)
static struct stasis_subscription * sub
Statsd channel stats. Exmaple of how to subscribe to Stasis events.
int ast_sockaddr_to_pj_sockaddr(const struct ast_sockaddr *addr, pj_sockaddr *pjaddr)
Fill a pj_sockaddr from an ast_sockaddr.
void ast_pjproject_caching_pool_destroy(pj_caching_pool *cp)
Destroy caching pool factory and all cached pools.
int ast_sockaddr_pj_sockaddr_cmp(const struct ast_sockaddr *addr, const pj_sockaddr *pjaddr)
Compare an ast_sockaddr to a pj_sockaddr.
void ast_pjproject_caching_pool_init(pj_caching_pool *cp, const pj_pool_factory_policy *policy, pj_size_t max_capacity)
Initialize the caching pool factory.
static pj_caching_pool cachingpool
Pool factory used by pjlib to allocate memory.
#define TURN_STATE_WAIT_TIME
static int ast_rtp_dtmf_compatible(struct ast_channel *chan0, struct ast_rtp_instance *instance0, struct ast_channel *chan1, struct ast_rtp_instance *instance1)
static void ast_rtp_update_source(struct ast_rtp_instance *instance)
#define TRANSPORT_TURN_RTCP
static int ast_rtp_destroy(struct ast_rtp_instance *instance)
#define RTCP_LENGTH_SHIFT
struct ast_srtp_res * res_srtp
static int ast_rtp_rtcp_handle_nack(struct ast_rtp_instance *instance, unsigned int *nackdata, unsigned int position, unsigned int length)
static int rtp_reload(int reload, int by_external_config)
#define RTCP_PAYLOAD_TYPE_SHIFT
#define DEFAULT_RTP_RECV_BUFFER_SIZE
#define MAX_TIMESTAMP_SKEW
#define DEFAULT_ICESUPPORT
static void ntp2timeval(unsigned int msw, unsigned int lsw, struct timeval *tv)
#define RTCP_FB_NACK_BLOCK_WORD_LENGTH
static struct ast_sockaddr rtpdebugaddr
#define DEFAULT_LEARNING_MIN_SEQUENTIAL
#define FLAG_3389_WARNING
static int rtp_learning_rtp_seq_update(struct rtp_learning_info *info, uint16_t seq)
static int rtp_transport_wide_cc_feedback_produce(const void *data)
#define RTCP_RR_BLOCK_WORD_LENGTH
static struct ast_frame * ast_rtcp_interpret(struct ast_rtp_instance *instance, struct ast_srtp *srtp, const unsigned char *rtcpdata, size_t size, struct ast_sockaddr *addr)
static const char * rtcp_payload_subtype2str(unsigned int pt, unsigned int subtype)
static int ast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data)
static char * handle_cli_rtp_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static struct ast_frame * ast_rtcp_read(struct ast_rtp_instance *instance)
#define RTP_IGNORE_FIRST_PACKETS_COUNT
static int rtp_raw_write(struct ast_rtp_instance *instance, struct ast_frame *frame, int codec)
#define RTCP_SR_BLOCK_WORD_LENGTH
static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
static int rtcp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
static struct ast_rtp_instance * rtp_find_instance_by_packet_source_ssrc(struct ast_rtp_instance *instance, struct ast_rtp *rtp, unsigned int ssrc)
#define RTCP_REPORT_COUNT_SHIFT
#define RTCP_DEFAULT_INTERVALMS
static void rtp_deallocate_transport(struct ast_rtp_instance *instance, struct ast_rtp *rtp)
#define RTP_DTLS_ESTABLISHED
static int bridge_p2p_rtp_write(struct ast_rtp_instance *instance, struct ast_rtp_instance *instance1, unsigned int *rtpheader, int len, int hdrlen)
#define DEFAULT_DTMF_TIMEOUT
static void put_unaligned_time24(void *p, uint32_t time_msw, uint32_t time_lsw)
#define RTCP_MAX_INTERVALMS
static int ast_rtcp_generate_nack(struct ast_rtp_instance *instance, unsigned char *rtcpheader)
static char * handle_cli_rtp_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static void rtp_transport_wide_cc_feedback_status_append(unsigned char *rtcpheader, int *packet_len, int *status_vector_chunk_bits, uint16_t *status_vector_chunk, int *run_length_chunk_count, int *run_length_chunk_status, int status)
static int ast_rtp_bundle(struct ast_rtp_instance *child, struct ast_rtp_instance *parent)
static struct ast_rtp_engine asterisk_rtp_engine
static const char * rtcp_payload_type2str(unsigned int pt)
#define TRANSPORT_SOCKET_RTP
static void rtp_instance_parse_transport_wide_cc(struct ast_rtp_instance *instance, struct ast_rtp *rtp, unsigned char *data, int len)
static void calc_rxstamp_and_jitter(struct timeval *tv, struct ast_rtp *rtp, unsigned int rx_rtp_ts, int mark)
static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
static int ast_rtp_dtmf_continuation(struct ast_rtp_instance *instance)
#define SRTP_MASTER_KEY_LEN
static int learning_min_sequential
static int rtp_allocate_transport(struct ast_rtp_instance *instance, struct ast_rtp *rtp)
static char * rtcp_do_debug_ip(struct ast_cli_args *a)
static int ast_rtcp_generate_report(struct ast_rtp_instance *instance, unsigned char *rtcpheader, struct ast_rtp_rtcp_report *rtcp_report, int *sr)
static struct ast_frame * ast_rtp_read(struct ast_rtp_instance *instance, int rtcp)
static char * handle_cli_rtcp_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
struct ast_srtp_policy_res * res_srtp_policy
static struct ast_rtp_instance * __rtp_find_instance_by_ssrc(struct ast_rtp_instance *instance, struct ast_rtp *rtp, unsigned int ssrc, int source)
static void calculate_lost_packet_statistics(struct ast_rtp *rtp, unsigned int *lost_packets, int *fraction_lost)
#define RTCP_HEADER_SSRC_LENGTH
static void process_dtmf_rfc2833(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, int payloadtype, int mark, struct frame_list *frames)
static int srtp_replay_protection
static void ast_rtp_set_stream_num(struct ast_rtp_instance *instance, int stream_num)
static int learning_min_duration
static int create_new_socket(const char *type, int af)
static int ast_rtp_fd(struct ast_rtp_instance *instance, int rtcp)
static int ast_rtcp_generate_compound_prefix(struct ast_rtp_instance *instance, unsigned char *rtcpheader, struct ast_rtp_rtcp_report *report, int *sr)
static void update_jitter_stats(struct ast_rtp *rtp, unsigned int ia_jitter)
#define RTCP_FB_REMB_BLOCK_WORD_LENGTH
static struct ast_frame * process_cn_rfc3389(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, int payloadtype, int mark)
#define DEFAULT_SRTP_REPLAY_PROTECTION
static int reload_module(void)
#define FLAG_NAT_INACTIVE
static int rtcp_debug_test_addr(struct ast_sockaddr *addr)
static int ast_rtp_qos_set(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
static void ast_rtp_change_source(struct ast_rtp_instance *instance)
static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit)
static void calc_mean_and_standard_deviation(double new_sample, double *mean, double *std_dev, unsigned int *count)
static int compare_by_value(int elem, int value)
Helper function to compare an elem in a vector by value.
static int update_rtt_stats(struct ast_rtp *rtp, unsigned int lsr, unsigned int dlsr)
static int rtcp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice)
static struct ast_sockaddr rtcpdebugaddr
static struct ast_rtp_instance * rtp_find_instance_by_media_source_ssrc(struct ast_rtp_instance *instance, struct ast_rtp *rtp, unsigned int ssrc)
#define MAXIMUM_RTP_RECV_BUFFER_SIZE
static int rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
#define STRICT_RTP_LEARN_TIMEOUT
Strict RTP learning timeout time in milliseconds.
#define RTCP_VERSION_SHIFTED
static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit)
static void rtp_instance_parse_extmap_extensions(struct ast_rtp_instance *instance, struct ast_rtp *rtp, unsigned char *extension, int len)
static int find_by_value(int elem, int value)
Helper function to find an elem in a vector by value.
#define RTCP_REPORT_COUNT_MASK
static void rtp_instance_unlock(struct ast_rtp_instance *instance)
#define DEFAULT_RTP_START
#define TRANSPORT_TURN_RTP
static int rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
static struct ast_cli_entry cli_rtp[]
static int ast_rtp_get_stat(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
#define RESCALE(in, inmin, inmax, outmin, outmax)
#define MISSING_SEQNOS_ADDED_TRIGGER
#define SRTP_MASTER_SALT_LEN
static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
#define RTCP_PAYLOAD_TYPE_MASK
#define DEFAULT_RTP_SEND_BUFFER_SIZE
#define FLAG_NEED_MARKER_BIT
static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp)
#define RTCP_MIN_INTERVALMS
static void ast_rtp_stop(struct ast_rtp_instance *instance)
#define FLAG_REQ_LOCAL_BRIDGE_BIT
static int __rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp, int *via_ice, int use_srtp)
static int load_module(void)
static int ast_rtcp_calculate_sr_rr_statistics(struct ast_rtp_instance *instance, struct ast_rtp_rtcp_report *rtcp_report, struct ast_sockaddr remote_address, int ice, int sr)
#define RTCP_VERSION_MASK_SHIFTED
#define CALC_LEARNING_MIN_DURATION(count)
Calculate the min learning duration in ms.
static int rtp_transport_wide_cc_packet_statistics_cmp(struct rtp_transport_wide_cc_packet_statistics a, struct rtp_transport_wide_cc_packet_statistics b)
#define SSRC_MAPPING_ELEM_CMP(elem, value)
SSRC mapping comparator for AST_VECTOR_REMOVE_CMP_UNORDERED()
static int rtp_debug_test_addr(struct ast_sockaddr *addr)
static int rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
static int unload_module(void)
static void ast_rtp_set_remote_ssrc(struct ast_rtp_instance *instance, unsigned int ssrc)
static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
#define FLAG_NAT_INACTIVE_NOWARN
static int rtcp_mux(struct ast_rtp *rtp, const unsigned char *packet)
static void rtp_learning_seq_init(struct rtp_learning_info *info, uint16_t seq)
#define TRANSPORT_SOCKET_RTCP
static void rtp_learning_start(struct ast_rtp *rtp)
Start the strictrtp learning mode.
static int ast_rtp_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
static unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp)
static char * rtp_do_debug_ip(struct ast_cli_args *a)
static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get(struct ast_rtp_instance *instance)
static int rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice)
static unsigned int ast_rtp_get_ssrc(struct ast_rtp_instance *instance)
static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw)
static void ast_rtp_remote_address_set(struct ast_rtp_instance *instance, struct ast_sockaddr *addr)
static struct ast_frame * ast_rtp_interpret(struct ast_rtp_instance *instance, struct ast_srtp *srtp, const struct ast_sockaddr *remote_address, unsigned char *read_area, int length, int prev_seqno, unsigned int bundled)
static int ast_rtp_local_bridge(struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1)
static struct ast_frame * red_t140_to_red(struct rtp_red *red)
static int ast_rtcp_generate_sdes(struct ast_rtp_instance *instance, unsigned char *rtcpheader, struct ast_rtp_rtcp_report *rtcp_report)
static double calc_media_experience_score(struct ast_rtp_instance *instance, double normdevrtt, double normdev_rxjitter, double stdev_rxjitter, double normdev_rxlost)
Calculate a "media experience score" based on given data.
static void update_reported_mes_stats(struct ast_rtp *rtp)
#define DEFAULT_STRICT_RTP
static int ast_rtp_sendcng(struct ast_rtp_instance *instance, int level)
generate comfort noice (CNG)
static char * handle_cli_rtcp_set_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static void update_lost_stats(struct ast_rtp *rtp, unsigned int lost_packets)
static void ast_rtp_stun_request(struct ast_rtp_instance *instance, struct ast_sockaddr *suggestion, const char *username)
static const char * ast_rtp_get_cname(struct ast_rtp_instance *instance)
static void rtp_write_rtcp_psfb(struct ast_rtp_instance *instance, struct ast_rtp *rtp, struct ast_frame *frame, struct ast_sockaddr *remote_address)
static struct ast_frame * process_dtmf_cisco(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, int payloadtype, int mark)
static struct ast_frame * create_dtmf_frame(struct ast_rtp_instance *instance, enum ast_frame_type type, int compensate)
static void rtp_write_rtcp_fir(struct ast_rtp_instance *instance, struct ast_rtp *rtp, struct ast_sockaddr *remote_address)
#define DEFAULT_TURN_PORT
#define MAXIMUM_RTP_SEND_BUFFER_SIZE
static int red_write(const void *data)
Write t140 redundancy frame.
#define DEFAULT_STUN_SOFTWARE_ATTRIBUTE
#define DEFAULT_LEARNING_MIN_DURATION
static void update_local_mes_stats(struct ast_rtp *rtp)
static void rtp_transport_wide_cc_feedback_status_vector_append(unsigned char *rtcpheader, int *packet_len, int *status_vector_chunk_bits, uint16_t *status_vector_chunk, int status)
static int ast_rtp_extension_enable(struct ast_rtp_instance *instance, enum ast_rtp_extension extension)
static int ast_rtcp_write(const void *data)
Write a RTCP packet to the far end.
@ AST_AES_CM_128_HMAC_SHA1_80
@ AST_AES_CM_128_HMAC_SHA1_32
static void cleanup(void)
Clean up any old apps that we don't need any more.
Pluggable RTP Architecture.
ast_rtp_dtls_setup
DTLS setup types.
@ AST_RTP_DTLS_SETUP_PASSIVE
@ AST_RTP_DTLS_SETUP_HOLDCONN
@ AST_RTP_DTLS_SETUP_ACTPASS
@ AST_RTP_DTLS_SETUP_ACTIVE
#define AST_RTP_RTCP_PSFB
#define AST_DEBUG_CATEGORY_DTLS
#define ast_debug_rtcp_packet_is_allowed
ast_rtp_ice_role
ICE role during negotiation.
@ AST_RTP_ICE_ROLE_CONTROLLING
@ AST_RTP_ICE_ROLE_CONTROLLED
#define AST_RTP_RTCP_FMT_FIR
#define ast_debug_rtcp(sublevel,...)
Log debug level RTCP information.
struct ast_format * ast_rtp_codecs_get_preferred_format(struct ast_rtp_codecs *codecs)
Retrieve rx preferred format.
ast_rtp_ice_component_type
ICE component types.
@ AST_RTP_ICE_COMPONENT_RTCP
@ AST_RTP_ICE_COMPONENT_RTP
struct ast_rtp_rtcp_report * ast_rtp_rtcp_report_alloc(unsigned int report_blocks)
Allocate an ao2 ref counted instance of ast_rtp_rtcp_report.
struct ast_rtp_payload_type * ast_rtp_codecs_get_payload(struct ast_rtp_codecs *codecs, int payload)
Retrieve rx payload mapped information by payload type.
int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
Get the value of an RTP instance property.
ast_rtp_dtls_hash
DTLS fingerprint hashes.
@ AST_RTP_DTLS_HASH_SHA256
int ast_rtp_engine_srtp_is_registered(void)
#define AST_RED_MAX_GENERATION
@ AST_RTP_INSTANCE_RTCP_MUX
@ AST_RTP_INSTANCE_RTCP_STANDARD
void ast_rtp_publish_rtcp_message(struct ast_rtp_instance *rtp, struct stasis_message_type *message_type, struct ast_rtp_rtcp_report *report, struct ast_json *blob)
Publish an RTCP message to Stasis Message Bus API.
struct ast_srtp * ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance, int rtcp)
Obtain the SRTP instance associated with an RTP instance.
#define AST_RTP_STAT_TERMINATOR(combined)
#define AST_RTP_RTCP_RTPFB
struct ast_rtp_instance * ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
Get the other RTP instance that an instance is bridged to.
ast_rtp_dtls_verify
DTLS verification settings.
@ AST_RTP_DTLS_VERIFY_FINGERPRINT
@ AST_RTP_DTLS_VERIFY_CERTIFICATE
#define ast_debug_rtp_packet_is_allowed
#define AST_LOG_CATEGORY_RTCP_PACKET
@ AST_RTP_INSTANCE_STAT_LOCAL_MAXRXPLOSS
@ AST_RTP_INSTANCE_STAT_REMOTE_NORMDEVRXPLOSS
@ AST_RTP_INSTANCE_STAT_REMOTE_MAXRXPLOSS
@ AST_RTP_INSTANCE_STAT_REMOTE_NORMDEVMES
@ AST_RTP_INSTANCE_STAT_REMOTE_MINJITTER
@ AST_RTP_INSTANCE_STAT_LOCAL_NORMDEVMES
@ AST_RTP_INSTANCE_STAT_MIN_RTT
@ AST_RTP_INSTANCE_STAT_TXMES
@ AST_RTP_INSTANCE_STAT_CHANNEL_UNIQUEID
@ AST_RTP_INSTANCE_STAT_TXPLOSS
@ AST_RTP_INSTANCE_STAT_MAX_RTT
@ AST_RTP_INSTANCE_STAT_RXPLOSS
@ AST_RTP_INSTANCE_STAT_REMOTE_MAXJITTER
@ AST_RTP_INSTANCE_STAT_LOCAL_MAXJITTER
@ AST_RTP_INSTANCE_STAT_REMOTE_MINMES
@ AST_RTP_INSTANCE_STAT_REMOTE_STDEVJITTER
@ AST_RTP_INSTANCE_STAT_REMOTE_MINRXPLOSS
@ AST_RTP_INSTANCE_STAT_LOCAL_MINMES
@ AST_RTP_INSTANCE_STAT_TXOCTETCOUNT
@ AST_RTP_INSTANCE_STAT_RXMES
@ AST_RTP_INSTANCE_STAT_REMOTE_STDEVMES
@ AST_RTP_INSTANCE_STAT_LOCAL_NORMDEVRXPLOSS
@ AST_RTP_INSTANCE_STAT_REMOTE_STDEVRXPLOSS
@ AST_RTP_INSTANCE_STAT_LOCAL_STDEVRXPLOSS
@ AST_RTP_INSTANCE_STAT_REMOTE_MAXMES
@ AST_RTP_INSTANCE_STAT_TXCOUNT
@ AST_RTP_INSTANCE_STAT_STDEVRTT
@ AST_RTP_INSTANCE_STAT_COMBINED_MES
@ AST_RTP_INSTANCE_STAT_LOCAL_MAXMES
@ AST_RTP_INSTANCE_STAT_RXJITTER
@ AST_RTP_INSTANCE_STAT_LOCAL_MINRXPLOSS
@ AST_RTP_INSTANCE_STAT_LOCAL_SSRC
@ AST_RTP_INSTANCE_STAT_REMOTE_NORMDEVJITTER
@ AST_RTP_INSTANCE_STAT_COMBINED_JITTER
@ AST_RTP_INSTANCE_STAT_TXJITTER
@ AST_RTP_INSTANCE_STAT_LOCAL_MINJITTER
@ AST_RTP_INSTANCE_STAT_COMBINED_LOSS
@ AST_RTP_INSTANCE_STAT_LOCAL_STDEVJITTER
@ AST_RTP_INSTANCE_STAT_COMBINED_RTT
@ AST_RTP_INSTANCE_STAT_NORMDEVRTT
@ AST_RTP_INSTANCE_STAT_RTT
@ AST_RTP_INSTANCE_STAT_RXOCTETCOUNT
@ AST_RTP_INSTANCE_STAT_LOCAL_STDEVMES
@ AST_RTP_INSTANCE_STAT_LOCAL_NORMDEVJITTER
@ AST_RTP_INSTANCE_STAT_RXCOUNT
@ AST_RTP_INSTANCE_STAT_REMOTE_SSRC
void * ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
Get the data portion of an RTP instance.
#define ast_rtp_instance_get_remote_address(instance, address)
Get the address of the remote endpoint that we are sending RTP to.
enum ast_media_type ast_rtp_codecs_get_stream_type(struct ast_rtp_codecs *codecs)
Determine the type of RTP stream media from the codecs mapped.
#define AST_RTP_RTCP_FMT_NACK
#define ast_debug_rtp(sublevel,...)
Log debug level RTP information.
void ast_rtp_instance_set_last_tx(struct ast_rtp_instance *rtp, time_t time)
Set the last RTP transmission time.
void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
Set the data portion of an RTP instance.
int ast_rtp_codecs_payload_code_tx_sample_rate(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code, unsigned int sample_rate)
Retrieve a tx mapped payload type based on whether it is an Asterisk format and the code.
void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
Set the value of an RTP instance property.
int ast_rtp_codecs_find_payload_code(struct ast_rtp_codecs *codecs, int payload)
Search for the tx payload type in the ast_rtp_codecs structure.
int ast_rtp_codecs_get_preferred_dtmf_format_rate(struct ast_rtp_codecs *codecs)
Retrieve rx preferred dtmf format sample rate.
void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance, struct ast_sockaddr *address)
Get the local address that we are expecting RTP on.
@ AST_RTP_ICE_CANDIDATE_TYPE_RELAYED
@ AST_RTP_ICE_CANDIDATE_TYPE_SRFLX
@ AST_RTP_ICE_CANDIDATE_TYPE_HOST
#define AST_DEBUG_CATEGORY_ICE
int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance, const struct ast_sockaddr *address)
Set the address that we are expecting to receive RTP on.
int ast_rtp_get_rate(const struct ast_format *format)
Retrieve the sample rate of a format according to RTP specifications.
int ast_rtp_codecs_payload_code_tx(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
Retrieve a tx mapped payload type based on whether it is an Asterisk format and the code.
ast_rtp_extension
Known RTP extensions.
@ AST_RTP_EXTENSION_TRANSPORT_WIDE_CC
@ AST_RTP_EXTENSION_ABS_SEND_TIME
int ast_rtp_payload_mapping_tx_is_present(struct ast_rtp_codecs *codecs, const struct ast_rtp_payload_type *to_match)
Determine if a type of payload is already present in mappings.
#define ast_rtp_instance_set_remote_address(instance, address)
Set the address of the remote endpoint that we are sending RTP to.
#define AST_RTP_RTCP_FMT_REMB
ast_rtp_dtls_connection
DTLS connection states.
@ AST_RTP_DTLS_CONNECTION_NEW
@ AST_RTP_DTLS_CONNECTION_EXISTING
#define ast_debug_dtls(sublevel,...)
Log debug level DTLS information.
@ AST_RTP_PROPERTY_RETRANS_RECV
@ AST_RTP_PROPERTY_RETRANS_SEND
@ AST_RTP_PROPERTY_ASYMMETRIC_CODEC
@ AST_RTP_PROPERTY_DTMF_COMPENSATE
#define ast_debug_dtls_packet_is_allowed
#define AST_LOG_CATEGORY_RTP_PACKET
#define AST_RTP_STAT_STRCPY(current_stat, combined, placement, value)
#define ast_debug_ice(sublevel,...)
Log debug level ICE information.
void ast_rtp_instance_get_requested_target_address(struct ast_rtp_instance *instance, struct ast_sockaddr *address)
Get the requested target address of the remote endpoint.
int ast_rtp_instance_set_incoming_source_address(struct ast_rtp_instance *instance, const struct ast_sockaddr *address)
Set the incoming source address of the remote endpoint that we are sending RTP to.
int ast_rtp_instance_extmap_get_id(struct ast_rtp_instance *instance, enum ast_rtp_extension extension)
Retrieve the id for an RTP extension.
int ast_rtp_codecs_get_preferred_dtmf_format_pt(struct ast_rtp_codecs *codecs)
Retrieve rx preferred dtmf format payload type.
int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
Unregister an RTP engine.
#define AST_RTP_RTCP_FMT_TRANSPORT_WIDE_CC
struct ast_rtp_codecs * ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
Get the codecs structure of an RTP instance.
const char * ast_rtp_instance_get_channel_id(struct ast_rtp_instance *instance)
Get the unique ID of the channel that owns this RTP instance.
unsigned int ast_rtp_codecs_get_framing(struct ast_rtp_codecs *codecs)
Get the framing used for a set of codecs.
unsigned int ast_rtp_instance_get_ssrc(struct ast_rtp_instance *rtp)
Retrieve the local SSRC value that we will be using.
#define AST_RTP_STAT_SET(current_stat, combined, placement, value)
#define DEFAULT_DTMF_SAMPLE_RATE_MS
int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy, int rtcp)
Add or replace the SRTP policies for the given RTP instance.
#define AST_RTP_RTCP_FMT_PLI
#define ast_rtp_engine_register(engine)
#define AST_RTP_CISCO_DTMF
#define AST_SCHED_DEL_UNREF(sched, id, refcall)
schedule task to get deleted and call unref function
#define AST_SCHED_DEL(sched, id)
Remove a scheduler entry.
int ast_sched_del(struct ast_sched_context *con, int id) attribute_warn_unused_result
Deletes a scheduled event.
int ast_sched_add(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data) attribute_warn_unused_result
Adds a scheduled event.
int ast_sched_add_variable(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data, int variable) attribute_warn_unused_result
Adds a scheduled event with rescheduling support.
int(* ast_sched_cb)(const void *data)
scheduler callback
Security Event Reporting API.
struct stasis_topic * ast_security_topic(void)
A stasis_topic which publishes messages for security related issues.
Asterisk internal frame definitions.
void ast_smoother_set_flags(struct ast_smoother *smoother, int flags)
#define ast_smoother_feed_be(s, f)
int ast_smoother_test_flag(struct ast_smoother *s, int flag)
void ast_smoother_free(struct ast_smoother *s)
#define AST_SMOOTHER_FLAG_FORCED
struct ast_frame * ast_smoother_read(struct ast_smoother *s)
#define ast_smoother_feed(s, f)
struct ast_smoother * ast_smoother_new(int bytes)
#define AST_SMOOTHER_FLAG_BE
@ STASIS_SUBSCRIPTION_FILTER_SELECTIVE
int stasis_subscription_accept_message_type(struct stasis_subscription *subscription, const struct stasis_message_type *type)
Indicate to a subscription that we are interested in a message type.
int stasis_subscription_set_filter(struct stasis_subscription *subscription, enum stasis_subscription_message_filter filter)
Set the message type filtering level on a subscription.
struct stasis_subscription * stasis_unsubscribe_and_join(struct stasis_subscription *subscription)
Cancel a subscription, blocking until the last message is processed.
#define stasis_subscribe(topic, callback, data)
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
static force_inline int attribute_pure ast_strlen_zero(const char *s)
int attribute_pure ast_false(const char *val)
Make sure something is false. Determine if a string containing a boolean value is "false"....
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
char *attribute_pure ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Wrapper for an ast_acl linked list.
Structure to describe a channel "technology", ie a channel driver See for examples:
Main Channel structure associated with a channel.
descriptor for a cli entry.
int args
This gets set in ast_cli_register()
Data buffer containing fixed number of data payloads.
char data[0]
The raw DNS record.
int rr_type
Resource record type.
The result of a DNS query.
Structure used to handle boolean flags.
unsigned int frame_ending
struct ast_format * format
Data structure associated with a single frame of data.
struct ast_frame_subclass subclass
enum ast_frame_type frametype
union ast_frame::@239 data
Abstract JSON element (object, array, string, int, ...).
Structure defining an RTCP session.
unsigned int received_prior
unsigned char frame_buf[512+AST_FRIENDLY_OFFSET]
double reported_maxjitter
unsigned int reported_mes_count
double reported_normdev_lost
double reported_normdev_mes
unsigned int lastsrtxcount
unsigned int reported_lost
double reported_stdev_jitter
unsigned int reported_jitter_count
double accumulated_transit
double reported_stdev_lost
enum ast_rtp_instance_rtcp type
unsigned int rxjitter_count
unsigned int reported_lost_count
double reported_stdev_mes
double reported_normdev_jitter
unsigned int rxlost_count
double reported_minjitter
unsigned int expected_prior
DTLS configuration structure.
enum ast_rtp_dtls_setup default_setup
enum ast_rtp_dtls_verify verify
enum ast_rtp_dtls_hash hash
unsigned int ephemeral_cert
enum ast_srtp_suite suite
Structure that represents the optional DTLS SRTP support within an RTP engine.
int(* set_configuration)(struct ast_rtp_instance *instance, const struct ast_rtp_dtls_cfg *dtls_cfg)
Structure for an ICE candidate.
struct ast_sockaddr address
enum ast_rtp_ice_component_type id
struct ast_sockaddr relay_address
enum ast_rtp_ice_candidate_type type
Structure that represents the optional ICE support within an RTP engine.
void(* set_authentication)(struct ast_rtp_instance *instance, const char *ufrag, const char *password)
void(* start)(struct ast_rtp_instance *instance)
struct ast_rtp_engine_dtls * dtls
double remote_stdevrxploss
double local_normdevjitter
double local_normdevrxploss
unsigned int rxoctetcount
double remote_normdevrxploss
double local_stdevrxploss
double remote_normdevjitter
unsigned int txoctetcount
char channel_uniqueid[MAX_CHANNEL_ID]
double remote_stdevjitter
An object that represents data received in a feedback report.
struct ast_rtp_rtcp_feedback_remb remb
Structure for storing RTP packets for retransmission.
A report block within a SR/RR report.
unsigned int highest_seq_no
struct ast_rtp_rtcp_report_block::@291 lost_count
An object that represents data sent during a SR/RR RTCP report.
struct ast_rtp_rtcp_report::@292 sender_information
unsigned short reception_report_count
unsigned int rtp_timestamp
struct ast_rtp_rtcp_report_block * report_block[0]
struct timeval ntp_timestamp
unsigned int packet_count
unsigned int lastividtimestamp
unsigned int dtmf_duration
struct ast_format * lasttxformat
struct rtp_transport_wide_cc_statistics transport_wide_cc
struct ast_rtp::@509 missing_seqno
struct ast_smoother * smoother
struct ast_sched_context * sched
unsigned short seedrxseqno
unsigned int remote_seed_rx_rtp_ts_stable
enum ast_rtp_dtmf_mode dtmfmode
struct ast_sockaddr strict_rtp_address
enum strict_rtp_state strict_rtp_state
unsigned int rxoctetcount
unsigned int themssrc_valid
unsigned int dtmf_timeout
char cname[AST_UUID_STR_LEN]
unsigned char rawdata[8192+AST_FRIENDLY_OFFSET]
unsigned int last_transit_time_samples
unsigned int lastovidtimestamp
unsigned int asymmetric_codec
struct ast_rtp::@510 ssrc_mapping
struct ast_data_buffer * recv_buffer
optional_ts last_end_timestamp
unsigned int lastotexttimestamp
struct ast_sockaddr bind_address
struct ast_data_buffer * send_buffer
struct rtp_learning_info rtp_source_learn
struct ast_rtp_instance * owner
The RTP instance owning us (used for debugging purposes) We don't hold a reference to the instance be...
unsigned int remote_seed_rx_rtp_ts
unsigned int lastitexttimestamp
unsigned int dtmf_samplerate_ms
unsigned int txoctetcount
struct ast_rtp_instance * bundled
struct ast_format * lastrxformat
Structure for rwlock and tracking information.
Socket address structure.
void(* destroy)(struct ast_srtp_policy *policy)
int(* set_master_key)(struct ast_srtp_policy *policy, const unsigned char *key, size_t key_len, const unsigned char *salt, size_t salt_len)
void(* set_ssrc)(struct ast_srtp_policy *policy, unsigned long ssrc, int inbound)
int(* set_suite)(struct ast_srtp_policy *policy, enum ast_srtp_suite suite)
struct ast_srtp_policy *(* alloc)(void)
int(* unprotect)(struct ast_srtp *srtp, void *buf, int *size, int rtcp)
int(* change_source)(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc)
int(* protect)(struct ast_srtp *srtp, void **buf, int *size, int rtcp)
struct ast_rtp_instance * rtp
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
structure to hold extensions
RTP learning mode tracking information.
enum ast_media_type stream_type
struct ast_sockaddr proposed_address
unsigned char t140red_data[64000]
unsigned char ts[AST_RED_MAX_GENERATION]
unsigned char len[AST_RED_MAX_GENERATION]
unsigned char buf_data[64000]
unsigned char pt[AST_RED_MAX_GENERATION]
Structure used for mapping an incoming SSRC to an RTP instance.
unsigned int ssrc
The received SSRC.
struct ast_rtp_instance * instance
The RTP instance this SSRC belongs to.
Packet statistics (used for transport-cc)
Statistics information (used for transport-cc)
struct rtp_transport_wide_cc_statistics::@508 packet_statistics
unsigned int last_extended_seqno
unsigned int feedback_count
int ast_stun_request(int s, struct sockaddr_in *dst, const char *username, struct sockaddr_in *answer)
Generic STUN request.
int ast_stun_handle_packet(int s, struct sockaddr_in *src, unsigned char *data, size_t len, stun_cb_f *stun_cb, void *arg)
handle an incoming STUN message.
#define ast_debug_stun(sublevel,...)
Log debug level STUN information.
#define AST_DEBUG_CATEGORY_STUN
static const int STANDARD_STUN_PORT
#define ast_test_suite_event_notify(s, f,...)
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
int64_t ast_tvdiff_us(struct timeval end, struct timeval start)
Computes the difference (in microseconds) between two struct timeval instances.
struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate)
Returns a timeval corresponding to the duration of n samples at rate r. Useful to convert samples to ...
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
int ast_tvcmp(struct timeval _a, struct timeval _b)
Compress two struct timeval instances returning -1, 0, 1 if the first arg is smaller,...
struct timeval ast_time_create_by_unit(unsigned long val, enum TIME_UNIT unit)
Convert the given unit value, and create a timeval object from it.
double ast_samp2sec(unsigned int _nsamp, unsigned int _rate)
Returns the duration in seconds of _nsamp samples at rate _rate.
unsigned int ast_sec2samp(double _seconds, int _rate)
Returns the number of samples at _rate in the duration in _seconds.
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
struct timeval ast_time_create_by_unit_str(unsigned long val, const char *unit)
Convert the given unit value, and create a timeval object from it.
struct timeval ast_tvsub(struct timeval a, struct timeval b)
Returns the difference of two timevals a - b.
double ast_tv2double(const struct timeval *tv)
Returns a double corresponding to the number of seconds in the timeval tv.
ast_suseconds_t ast_time_tv_to_usec(const struct timeval *tv)
Convert a timeval structure to microseconds.
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
static void destroy(struct ast_trans_pvt *pvt)
Handle unaligned data access.
static void put_unaligned_uint16(void *p, unsigned short datum)
static void put_unaligned_uint32(void *p, unsigned int datum)
int error(const char *format,...)
static void statistics(void)
#define ast_test_flag(p, flag)
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
#define ast_socket_nonblock(domain, type, protocol)
Create a non-blocking socket.
#define ast_clear_flag(p, flag)
long int ast_random(void)
#define ast_set_flag(p, flag)
Universally unique identifier support.
char * ast_uuid_generate_str(char *buf, size_t size)
Generate a UUID string.
#define AST_VECTOR_RESET(vec, cleanup)
Reset vector.
#define AST_VECTOR_ELEM_CLEANUP_NOOP(elem)
Vector element cleanup that does nothing.
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
#define AST_VECTOR_REMOVE_CMP_ORDERED(vec, value, cmp, cleanup)
Remove an element from a vector that matches the given comparison while maintaining order.
#define AST_VECTOR_FREE(vec)
Deallocates this vector.
#define AST_VECTOR_REMOVE_CMP_UNORDERED(vec, value, cmp, cleanup)
Remove an element from a vector that matches the given comparison.
#define AST_VECTOR_GET_CMP(vec, value, cmp)
Get an element from a vector that matches the given comparison.
#define AST_VECTOR_ADD_SORTED(vec, elem, cmp)
Add an element into a sorted vector.
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
#define AST_VECTOR(name, type)
Define a vector structure.
#define AST_VECTOR_GET_ADDR(vec, idx)
Get an address of element in a vector.