44 #include <apr_network_io.h>
47 #undef PACKAGE_TARNAME
48 #undef PACKAGE_VERSION
49 #undef PACKAGE_BUGREPORT
53 #include <datatypes.h>
55 #include <srtp_priv.h>
59 #define JITTER_LEAD_FRAMES 10
60 #define READ_INC(rtp_session) switch_mutex_lock(rtp_session->read_mutex); rtp_session->reading++
61 #define READ_DEC(rtp_session) switch_mutex_unlock(rtp_session->read_mutex); rtp_session->reading--
62 #define WRITE_INC(rtp_session) switch_mutex_lock(rtp_session->write_mutex); rtp_session->writing++
63 #define WRITE_DEC(rtp_session) switch_mutex_unlock(rtp_session->write_mutex); rtp_session->writing--
65 #define RTP_STUN_FREQ 1000000
66 #define rtp_header_len 12
67 #define RTP_START_PORT 16384
68 #define RTP_END_PORT 32768
69 #define MASTER_KEY_LEN 30
70 #define RTP_MAGIC_NUMBER 42
71 #define WARN_SRTP_ERRS 10
72 #define MAX_SRTP_ERRS 100
73 #define NTP_TIME_OFFSET 2208988800UL
74 #define ZRTP_MAGIC_COOKIE 0x5a525450
77 #define DTMF_SANITY (rtp_session->one_second * 30)
79 #define rtp_session_name(_rtp_session) _rtp_session->session ? switch_core_session_get_name(_rtp_session->session) : "-"
90 static zrtp_global_t *zrtp_global;
92 static zrtp_zid_t zid = {
"FreeSWITCH01" };
94 static zrtp_zid_t zid = {
"FreeSWITCH0" };
96 static int zrtp_on = 0;
97 #define ZRTP_MITM_TRIES 100
118 #define RTP_BODY(_s) (char *) (_s->recv_msg.ebody ? _s->recv_msg.ebody : _s->recv_msg.body)
129 #pragma pack(push, r1, 1)
143 #if SWITCH_BYTE_ORDER == __BIG_ENDIAN
170 #pragma pack(pop, r1)
254 char last_sent_id[13];
456 zrtp_session_t *zrtp_session;
457 zrtp_profile_t *zrtp_profile;
458 zrtp_stream_t *zrtp_stream;
510 static void do_2833(switch_rtp_t *rtp_session);
513 #define rtp_type(rtp_session) rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"
518 int is_rtcp = ice == &rtp_session->
rtcp_ice;
519 const char *err =
"";
576 unsigned char *packet = (
unsigned char *)
RTP_BODY(rtp_session);
580 uint16_t in_digit_seq;
585 if (!(packet[0] || packet[1] || packet[2] || packet[3]) && len >= 8) {
591 if (!(packet[0] || packet[1] || packet[2] || packet[3]) && rtp_session->
dtmf_data.
in_digit_ts) {
600 end = packet[1] & 0x80 ? 1 : 0;
601 duration = (packet[2] << 8) + packet[3];
603 in_digit_seq = ntohs((uint16_t) rtp_session->
last_rtp_hdr.seq);
617 if (in_digit_seq < rtp_session->dtmf_data.in_digit_seq) {
765 static int rtp_write_ready(switch_rtp_t *rtp_session, uint32_t bytes,
int line);
773 uint8_t
buf[256] = { 0 };
775 unsigned int elapsed;
804 if (elapsed > 30000) {
858 if (strchr(them,
':')) {
870 char username[34] = { 0 };
871 unsigned char buf[512] = { 0 };
875 uint32_t *pri = NULL;
876 int is_rtcp = ice == &rtp_session->
rtcp_ice;
897 if (cpylen >
sizeof(buf)) {
898 cpylen =
sizeof(
buf);
902 memcpy(buf, data, cpylen);
934 switch (attr->
type) {
943 uint32_t code = (err->
code * 100) + err->
number;
990 uint32_t priority = 0;
991 pri = (uint32_t *) attr->
value;
992 priority = ntohl(*pri);
1004 }
while (xlen <= packet->header.length);
1007 ok = !strcmp(ice->
user_ice, username);
1015 char foo1[13] =
"", foo2[13] =
"";
1034 memcpy(foo1, packet->
header.
id, 12);
1043 if (!
zstr(username)) {
1044 if (!
icecmp(username, ice)) {
1059 if (elapsed > 20000 && pri) {
1063 const char *old_host, *err = NULL;
1079 for (j = 0; j < 2; j++) {
1080 if (!icep[j] || !icep[j]->ice_params) {
1084 if (icep[j]->ice_params && icep[j]->ice_params->cands[i][icep[j]->
proto].priority == *pri) {
1105 if (!host || !port) {
1111 "%s ICE Auto Changing port from %s:%u to %s:%u\n",
rtp_type(rtp_session), old_host, old, host, port);
1160 const char *host = NULL, *host2 = NULL;
1166 uint8_t stunbuf[512];
1168 const char *remote_ip;
1186 memset(stunbuf, 0,
sizeof(stunbuf));
1211 "STUN from %s:%d %s\n", host, port, cmp ?
"EXPECTED" :
"IGNORED");
1222 }
else if (rtp_session->
wrong_addrs > 10 || elapsed >= 10000) {
1242 if (elapsed < 1000) {
1244 "Skiping RELAY stun/%s/dtls port change from %s:%u to %s:%u\n", is_rtcp ?
"rtcp" :
"rtp",
1257 "Auto Changing %s stun/%s/dtls port from %s:%u to %s:%u\n",
rtp_type(rtp_session), is_rtcp ?
"rtcp" :
"rtp",
1273 msg.
from = __FILE__;
1296 zrtp_status_t status = zrtp_status_ok;
1298 status = zrtp_def_cache_store(zrtp_global);
1303 static int zrtp_send_rtp_callback(
const zrtp_stream_t *stream,
char *rtp_packet,
unsigned int rtp_packet_length)
1305 switch_rtp_t *rtp_session = zrtp_stream_get_userdata(stream);
1307 zrtp_status_t status = zrtp_status_ok;
1313 static void zrtp_event_callback(zrtp_stream_t *stream,
unsigned event)
1315 switch_rtp_t *rtp_session = zrtp_stream_get_userdata(stream);
1316 zrtp_session_info_t zrtp_session_info;
1325 case ZRTP_EVENT_IS_SECURE:
1333 if (zrtp_status_ok == zrtp_session_get(stream->session, &zrtp_session_info)) {
1334 if (zrtp_session_info.sas_is_ready) {
1339 zrtp_verified_set(zrtp_global, &stream->session->zid, &stream->session->peer_zid, (uint8_t)1);
1350 if (!video_rtp_session) {
1354 if (video_rtp_session) {
1355 if (zrtp_status_ok != zrtp_stream_attach(stream->session, &video_rtp_session->zrtp_stream)) {
1358 zrtp_stream_set_userdata(video_rtp_session->zrtp_stream, video_rtp_session);
1360 zrtp_stream_registration_start(video_rtp_session->zrtp_stream, video_rtp_session->
ssrc);
1362 zrtp_stream_start(video_rtp_session->zrtp_stream, video_rtp_session->
ssrc);
1371 stream->session->sas2.buffer);
1378 case ZRTP_EVENT_NO_ZRTP_QUICK:
1380 if (stream != NULL) {
1381 zrtp_stream_stop(stream);
1386 case ZRTP_EVENT_IS_CLIENT_ENROLLMENT:
1393 case ZRTP_EVENT_USER_ALREADY_ENROLLED:
1400 case ZRTP_EVENT_NEW_USER_ENROLLED:
1407 case ZRTP_EVENT_USER_UNENROLLED:
1414 case ZRTP_EVENT_IS_PENDINGCLEAR:
1421 rtp_session->zrtp_mitm_tries = 0;
1425 case ZRTP_EVENT_NO_ZRTP:
1436 static void zrtp_logger(
int level,
const char *data,
int len,
int offset)
1447 zrtp_config_t zrtp_config;
1448 char zrtp_cache_path[256] =
"";
1449 zrtp_on = zrtp_enabled ?
switch_true(zrtp_enabled) : 0;
1458 zrtp_config_defaults(&zrtp_config);
1459 strcpy(zrtp_config.client_id,
"FreeSWITCH");
1460 zrtp_config.is_mitm = 1;
1461 zrtp_config.lic_mode = ZRTP_LICENSE_MODE_ACTIVE;
1463 cache_len=(uint32_t)strlen(zrtp_cache_path);
1464 ZSTR_SET_EMPTY(zrtp_config.def_cache_path);
1465 zrtp_config.def_cache_path.length = cache_len > zrtp_config.def_cache_path.max_length ? zrtp_config.def_cache_path.max_length : (uint16_t)cache_len;
1466 strncpy(zrtp_config.def_cache_path.buffer, zrtp_cache_path, zrtp_config.def_cache_path.max_length);
1467 zrtp_config.cb.event_cb.on_zrtp_protocol_event = (void (*)(zrtp_stream_t*,zrtp_protocol_event_t))zrtp_event_callback;
1468 zrtp_config.cb.misc_cb.on_send_packet = zrtp_send_rtp_callback;
1469 zrtp_config.cb.event_cb.on_zrtp_security_event = (void (*)(zrtp_stream_t*,zrtp_security_event_t))zrtp_event_callback;
1470 zrtp_log_set_log_engine((zrtp_log_engine *) zrtp_logger);
1471 zrtp_log_set_level(4);
1472 if (zrtp_status_ok == zrtp_init(&zrtp_config, &zrtp_global)) {
1473 memcpy(zid, zid_string, 12);
1499 }
else if (timestamp) {
1500 rtp_session->
ts = (uint32_t) timestamp;
1521 static void do_mos(switch_rtp_t *rtp_session,
int force) {
1536 if (diff < 1) diff = 1;
1549 if (R < 0 || R > 100) R = 100;
1552 rtp_session->
stats.
inbound.
mos = 1 + (0.035) * R + (.000007) * R * (R-60) * (100-R);
1577 lost += i * loss[i];
1580 if (received > 0 && bursts > 0) {
1581 *burstr = (double)((
double)lost / (double)bursts) / (double)(1.0 / ( 1.0 - (
double)lost / (double)received ));
1583 *burstr = - *burstr;
1589 *lossr = (double)((
double)lost / (double)received);
1607 int64_t diff_time = 0, cur_diff = 0;
1623 seq = (int)(uint16_t) ntohs((uint16_t) rtp_session->
last_rtp_hdr.seq);
1687 if ( diff_time < 0 ) {
1688 diff_time = -diff_time;
1734 uint32_t sec, ntp_sec, ntp_usec;
1737 sec = (uint32_t)(now/1000000);
1740 ntp_usec = (uint32_t)(now - (sec*1000000));
1741 sr->
ntp_lsw = htonl((u_long)(ntp_usec*(
double)(((uint64_t)1)<<32)*1.0e-6));
1750 (ntohl(sr->
ntp_lsw)&0xffff0000)>>16 | (ntohl(sr->
ntp_msw)&0x0000ffff)<<16,
1762 uint32_t expected_pkt, dlsr;
1764 uint32_t ntp_sec, ntp_usec, lsr_now, sec;
1766 sec = (uint32_t)(now/1000000);
1768 ntp_usec = (uint32_t)(now - (sec*1000000));
1779 if (expected_pkt > 0 && pkt_lost > 0) {
1780 rtcp_report_block->
fraction = (uint8_t) (pkt_lost * 256 / expected_pkt);
1784 #if SWITCH_BYTE_ORDER != __BIG_ENDIAN
1803 lsr_now = (uint32_t)(ntp_usec*0.065536) | (ntp_sec&0x0000ffff)<<16;
1811 rtcp_report_block->
dlsr = htonl(dlsr);
1821 stats->
ssrc = ntohl(hdr->ssrc);
1830 stats->
base_seq = ntohs((uint16_t)hdr->seq);
1854 uint32_t packet_spacing_diff = 0, pkt_tsdiff, pkt_extended_seq;
1855 uint16_t pkt_seq, seq_diff, max_seq;
1856 const int MAX_DROPOUT = 3000;
1857 const int MAX_MISORDER = 100;
1858 const int RTP_SEQ_MOD = (1<<16);
1863 pkt_seq = (uint16_t) ntohs((uint16_t) rtp_session->
last_rtp_hdr.seq);
1867 seq_diff = pkt_seq - max_seq;
1869 if (seq_diff < MAX_DROPOUT) {
1870 if (pkt_seq < max_seq) {
1875 pkt_extended_seq = stats->
cycle << 16 | pkt_seq;
1880 else if (seq_diff <= (RTP_SEQ_MOD - MAX_MISORDER)) {
1881 if (pkt_seq == stats->
bad_seq) {
1884 stats->
bad_seq = (pkt_seq + 1) & (RTP_SEQ_MOD-1);
1892 if (ntohl(hdr->ssrc) != stats->
ssrc || !stats->
init) {
1903 pkt_tsdiff = abs((
int)rtp_session->
timer.
samplecount - (
int)ntohl(hdr->ts));
1907 packet_spacing_diff = abs((
int)pkt_tsdiff - (
int)stats->
last_pkt_tsdiff);
1922 uint32_t mantissa_max, i = 0;
1924 uint32_t mantissa = 0;
1925 uint16_t overhead = 60;
1929 mantissa_max = (1 << bits) - 1;
1931 for (i = 0; i < 32; ++i) {
1932 if (bps <= (mantissa_max << i)) {
1938 mantissa = (bps >> exp);
1940 tmmbx->
parts[0] = (uint8_t) ((exp << 2) + ((mantissa >> 15) & 0x03));
1941 tmmbx->
parts[1] = (uint8_t) (mantissa >> 7);
1942 tmmbx->
parts[2] = (uint8_t) ((mantissa >> 1) + ((overhead >> 8) & 0x01));
1943 tmmbx->
parts[3] = (uint8_t) (overhead);
1959 int rtcp_ok = 0, rtcp_fb = 0;
1961 int rate = 0, nack_ttl = 0;
1962 uint32_t cur_nack[
MAX_NACK] = { 0 };
1969 uint8_t data[10] = { 0 };
1993 cur_nack[nack_ttl++] = nack;
2016 if (rtcp_ok &&
using_ice(rtp_session)) {
2055 rr->
ssrc = htonl(rtp_session->
ssrc);
2062 sr->
ssrc = htonl(rtp_session->
ssrc);
2106 for (n = 0; n < nack_ttl; n++) {
2118 ext_hdr->
length = htons(3);
2120 nack = (uint32_t *) p;
2121 *nack = cur_nack[n];
2124 ntohs(*nack & 0xFFFF));
2153 fir->
r1 = fir->
r2 = fir->
r3 = 0;
2168 while (rtp_session->
tmmbr || rtp_session->
tmmbn) {
2184 if (rtp_session->
tmmbr) {
2187 bps = rtp_session->
tmmbr;
2188 rtp_session->
tmmbr = 0;
2192 bps = rtp_session->
tmmbn;
2193 rtp_session->
tmmbn = 0;
2212 p = (uint8_t *) (sdes) + sdes_bytes;
2213 ssrc = (uint32_t *) p;
2214 *ssrc = htonl(rtp_session->
ssrc);
2215 sdes_bytes +=
sizeof(uint32_t);
2218 p = (uint8_t *) (sdes) + sdes_bytes;
2221 snprintf((
char *)unit->
value, 80,
"%x", rtp_session->
ssrc);
2229 snprintf((
char *)unit->
value, 80,
"FreeSWITCH.org -- Come to ClueCon.com");
2235 sdes_bytes += 4 - (sdes_bytes % 4);
2237 sdes->length = htons((uint16_t)(sdes_bytes / 4) - 1);
2238 rtcp_bytes += sdes_bytes;
2252 int sbytes = (int) rtcp_bytes;
2259 rtcp_bytes = sbytes;
2268 unsigned int sbytes = (int) rtcp_bytes;
2269 zrtp_status_t stat = zrtp_status_fail;
2271 stat = zrtp_process_rtcp(rtp_session->zrtp_stream, (
void *) &rtp_session->
rtcp_send_msg, &sbytes);
2274 case zrtp_status_ok:
2276 case zrtp_status_drop:
2278 ret = (int)rtcp_bytes;
2281 case zrtp_status_fail:
2288 rtcp_bytes = sbytes;
2294 const char *old_host;
2341 crypto_get_random(buf, len);
2374 zrtp_status_t status = zrtp_status_ok;
2376 status = zrtp_def_cache_store(zrtp_global);
2378 zrtp_down(zrtp_global);
2382 crypto_kernel_shutdown();
2459 rtp_session->pmaps = pmap;
2469 rtp_session->rtp_bugs = bugs;
2472 rtp_session->seq = 0;
2486 *err =
"RTCP Remote Address Error!";
2510 *err =
"RTCP Socket Error!";
2515 *err =
"RTCP NOT ACTIVE!";
2532 *err =
"RTCP Local Address Error!";
2537 *err =
"RTCP Socket Error!";
2542 *err =
"RTCP Socket Error!";
2547 *err =
"RTCP Bind Error!";
2553 *err =
"RTCP From Address Error!";
2559 rtcp_new_sock = NULL;
2571 if (rtcp_new_sock) {
2575 if (rtcp_old_sock) {
2591 char o[5] =
"TEST", i[5] =
"";
2596 if (rtp_session->ready != 1) {
2612 if (
zstr(host) || !port) {
2613 *err =
"Address Error";
2619 rtp_session->local_port = port;
2623 *err =
"Local Address Error!";
2628 if (rtp_session->sock_input) {
2633 *err =
"Socket Error!";
2638 *err =
"Socket Error!";
2654 if ((j = atoi(host)) && j > 223 && j < 240) {
2656 *err =
"Multicast Socket interface Error";
2661 *err =
"Multicast Error";
2665 if (rtp_session->session) {
2670 int ttl = atoi(var);
2672 if (ttl > 0 && ttl < 256) {
2674 *err =
"Mutlicast TTL set failed";
2712 old_sock = rtp_session->sock_input;
2713 rtp_session->sock_input = new_sock;
2745 if (rtp_session->ready != 1) {
2759 if (rtp_session->missed_count >= max) {
2762 "new max missed packets(%d->%d) greater than current missed packets(%d). RTP will timeout.\n",
2763 rtp_session->missed_count, max, rtp_session->missed_count);
2766 rtp_session->max_missed_packets = max;
2772 if (rtp_session->jb) {
2780 if (rtp_session->vb) {
2784 if (rtp_session->vbw) {
2795 rtp_session->seq = (uint16_t) rand();
2796 rtp_session->ts = 0;
2797 memset(&rtp_session->ts_norm, 0,
sizeof(rtp_session->ts_norm));
2799 rtp_session->last_stun = rtp_session->first_stun = 0;
2800 rtp_session->wrong_addrs = 0;
2801 rtp_session->rtcp_sent_packets = 0;
2802 rtp_session->rtcp_last_sent = 0;
2803 rtp_session->ice_adj = 0;
2810 if (rtp_session->ice.ready) {
2812 rtp_session->ice.ready = rtp_session->ice.rready = 0;
2819 rtp_session->missed_count = 0;
2824 return zstr(rtp_session->remote_host_str) ?
"0.0.0.0" : rtp_session->remote_host_str;
2829 return rtp_session->remote_port;
2834 uint32_t o = UINT_MAX;
2860 memset(&rtp_session->timer, 0,
sizeof(rtp_session->timer));
2864 rtp_session->missed_count = 0;
2865 rtp_session->max_missed_packets = 0;
2869 if (rtp_session->rtcp_sock_input) {
2874 if (rtp_session->rtcp_sock_output && rtp_session->rtcp_sock_output != rtp_session->rtcp_sock_input) {
2879 rtp_session->rtcp_sock_input = NULL;
2880 rtp_session->rtcp_sock_output = NULL;
2882 if ((sock = rtp_session->rtcp_sock_input)) {
2883 rtp_session->rtcp_sock_input = NULL;
2886 if (rtp_session->rtcp_sock_output && rtp_session->rtcp_sock_output != sock) {
2887 if ((sock = rtp_session->rtcp_sock_output)) {
2888 rtp_session->rtcp_sock_output = NULL;
2921 *err =
"Remote Address Error!";
2928 rtp_session->remote_addr = remote_addr;
2929 switch_cp_addr(rtp_session->rtp_from_addr, rtp_session->remote_addr);
2931 if (change_adv_addr) {
2933 rtp_session->remote_port = port;
2937 rtp_session->eff_remote_port = port;
2940 rtp_session->sock_output = rtp_session->sock_input;
2942 if (rtp_session->sock_output && rtp_session->sock_output != rtp_session->sock_input) {
2949 *err =
"Socket Error!";
2953 if (rtp_session->dtls) {
2954 rtp_session->dtls->sock_output = rtp_session->sock_output;
2964 rtp_session->rtcp_remote_addr = rtp_session->remote_addr;
2965 rtp_session->rtcp_sock_output = rtp_session->sock_output;
2967 if (remote_rtcp_port) {
2968 rtp_session->remote_rtcp_port = remote_rtcp_port;
2970 rtp_session->remote_rtcp_port = rtp_session->eff_remote_port + 1;
2974 if (rtp_session->rtcp_dtls) {
2976 rtp_session->rtcp_dtls->remote_addr = rtp_session->rtcp_remote_addr;
2977 rtp_session->rtcp_dtls->sock_output = rtp_session->rtcp_sock_output;
2995 return dtls_state_names_t[s];
2999 #define dtls_set_state(_dtls, _state) switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n", rtp_type(rtp_session), dtls_state_names(_dtls->state), dtls_state_names(_state)); _dtls->new_state = 1; _dtls->last_state = _dtls->state; _dtls->state = _state
3001 #define cr_keylen 16
3002 #define cr_saltlen 14
3012 }
else if ((cert = SSL_get_peer_certificate(dtls->
ssl))) {
3023 uint8_t raw_key_data[
cr_kslen*2] = { 0 };
3024 unsigned char *local_key, *remote_key, *local_salt, *remote_salt;
3025 unsigned char local_key_buf[
cr_kslen] = {0}, remote_key_buf[
cr_kslen] = {0};
3029 #ifdef HAVE_OPENSSL_DTLS_SRTP
3030 if (!SSL_export_keying_material(dtls->
ssl, raw_key_data,
sizeof(raw_key_data),
"EXTRACTOR-dtls_srtp", 19, NULL, 0, 0)) {
3040 local_key = raw_key_data;
3046 remote_key = raw_key_data;
3052 memcpy(local_key_buf, local_key,
cr_keylen);
3055 memcpy(remote_key_buf, remote_key,
cr_keylen);
3104 if ((ret = SSL_do_handshake(dtls->
ssl)) != 1){
3105 switch((ret = SSL_get_error(dtls->
ssl, ret))){
3106 case SSL_ERROR_WANT_READ:
3107 case SSL_ERROR_WANT_WRITE:
3108 case SSL_ERROR_NONE:
3117 if (SSL_is_init_finished(dtls->
ssl)) {
3136 SSL_free(dtls->
ssl);
3146 int r = 0, ret = 0, len;
3148 unsigned char buf[4096] =
"";
3152 if (!dtls->
bytes && !ready) {
3157 ret = SSL_get_error(dtls->
ssl, ret);
3161 if (dtls_states[dtls->
state]) {
3162 r = dtls_states[dtls->
state](rtp_session, dtls);
3165 if ((len = BIO_read(dtls->
write_bio, buf,
sizeof(buf))) > 0) {
3179 static int cb_verify_peer(
int preverify_ok, X509_STORE_CTX *ctx)
3186 ssl = X509_STORE_CTX_get_app_data(ctx);
3189 if (!(ssl && dtls)) {
3193 if ((cert = SSL_get_peer_certificate(dtls->
ssl))) {
3208 #ifdef HAVE_OPENSSL_DTLS_SRTP
3217 if (!rtp_session || (!rtp_session->dtls && !rtp_session->rtcp_dtls)) {
3222 return rtp_session->dtls->state;
3226 return rtp_session->rtcp_dtls->state;
3235 if (!rtp_session || (!rtp_session->dtls && !rtp_session->rtcp_dtls)) {
3240 if (rtp_session->dtls && rtp_session->dtls == rtp_session->rtcp_dtls) {
3241 rtp_session->rtcp_dtls = NULL;
3244 if (rtp_session->dtls) {
3248 if (rtp_session->jb) {
3252 if (rtp_session->vb) {
3256 if (rtp_session->vbw) {
3270 for(x = 0; x < 2; x++) {
3271 if (rtp_session->send_ctx[x]) {
3272 srtp_dealloc(rtp_session->send_ctx[x]);
3273 rtp_session->send_ctx[x] = NULL;
3281 for (x = 0; x < 2; x++) {
3282 if (rtp_session->recv_ctx[x]) {
3283 srtp_dealloc(rtp_session->recv_ctx[x]);
3284 rtp_session->recv_ctx[x] = NULL;
3298 const char *kind =
"";
3301 #ifndef OPENSSL_NO_EC
3305 #ifndef HAVE_OPENSSL_DTLS_SRTP
3319 if ((type & DTLS_TYPE_RTP) && (type & DTLS_TYPE_RTCP)) {
3321 }
else if ((type & DTLS_TYPE_RTP)) {
3328 "Activate %s %s DTLS %s\n", kind,
rtp_type(rtp_session), (type & DTLS_TYPE_SERVER) ?
"server" :
"client");
3330 if (((type & DTLS_TYPE_RTP) && rtp_session->dtls) || ((type & DTLS_TYPE_RTCP) && rtp_session->rtcp_dtls)) {
3348 dtls->
ssl_ctx = SSL_CTX_new((type & DTLS_TYPE_SERVER) ? DTLSv1_server_method() : DTLSv1_client_method());
3351 bio = BIO_new_file(dtls->
pem,
"r");
3352 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
3355 SSL_CTX_set_tmp_dh(dtls->
ssl_ctx, dh);
3359 SSL_CTX_set_mode(dtls->
ssl_ctx, SSL_MODE_AUTO_RETRY);
3362 SSL_CTX_set_verify(dtls->
ssl_ctx, SSL_VERIFY_NONE, NULL);
3364 SSL_CTX_set_cipher_list(dtls->
ssl_ctx,
"ALL");
3366 #ifdef HAVE_OPENSSL_DTLS_SRTP
3368 SSL_CTX_set_tlsext_use_srtp(dtls->
ssl_ctx,
"SRTP_AES128_CM_SHA1_80");
3372 dtls->
read_bio = BIO_new(BIO_s_mem());
3378 BIO_set_mem_eof_return(dtls->
read_bio, -1);
3379 BIO_set_mem_eof_return(dtls->
write_bio, -1);
3381 if ((ret=SSL_CTX_use_certificate_file(dtls->
ssl_ctx, dtls->
rsa, SSL_FILETYPE_PEM)) != 1) {
3386 if ((ret=SSL_CTX_use_PrivateKey_file(dtls->
ssl_ctx, dtls->
pvt, SSL_FILETYPE_PEM)) != 1) {
3391 if (SSL_CTX_check_private_key(dtls->
ssl_ctx) == 0) {
3397 && (ret = SSL_CTX_load_verify_locations(dtls->
ssl_ctx, dtls->
ca, NULL)) != 1) {
3400 SSL_get_error(dtls->
ssl, ret));
3407 SSL_set_mode(dtls->
ssl, SSL_MODE_AUTO_RETRY);
3408 SSL_set_read_ahead(dtls->
ssl, 1);
3411 #ifndef OPENSSL_NO_EC
3412 ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
3416 SSL_set_options(dtls->
ssl, SSL_OP_SINGLE_ECDH_USE);
3417 SSL_set_tmp_ecdh(dtls->
ssl, ecdh);
3421 SSL_set_verify(dtls->
ssl, SSL_VERIFY_NONE, NULL);
3422 SSL_set_app_data(dtls->
ssl, dtls);
3424 BIO_ctrl(dtls->
read_bio, BIO_CTRL_DGRAM_SET_MTU, 1400, NULL);
3425 BIO_ctrl(dtls->
write_bio, BIO_CTRL_DGRAM_SET_MTU, 1400, NULL);
3426 SSL_set_mtu(dtls->
ssl, 1400);
3427 BIO_ctrl(dtls->
write_bio, BIO_C_SET_BUFF_SIZE, 1400, NULL);
3428 BIO_ctrl(dtls->
read_bio, BIO_C_SET_BUFF_SIZE, 1400, NULL);
3438 if ((type & DTLS_TYPE_RTP)) {
3439 rtp_session->dtls = dtls;
3444 if ((type & DTLS_TYPE_RTCP)) {
3445 rtp_session->rtcp_dtls = dtls;
3446 if (!(type & DTLS_TYPE_RTP)) {
3447 dtls->
sock_output = rtp_session->rtcp_sock_output;
3448 dtls->
remote_addr = rtp_session->rtcp_remote_addr;
3452 if ((type & DTLS_TYPE_SERVER)) {
3453 SSL_set_accept_state(dtls->
ssl);
3455 SSL_set_connect_state(dtls->
ssl);
3477 srtp_policy_t *policy;
3485 unsigned char b64_key[512] =
"";
3516 rtp_session->srtp_idx_rtcp = idx = 1;
3519 rtp_session->srtp_idx_rtcp = idx = 1;
3523 policy = &rtp_session->recv_policy[idx];
3525 policy = &rtp_session->send_policy[idx];
3528 crypto_key->
type = type;
3529 crypto_key->
index = index;
3530 memcpy(crypto_key->
key, key, keylen);
3531 crypto_key->
next = rtp_session->crypto_keys[direction];
3532 rtp_session->crypto_keys[direction] = crypto_key;
3534 memset(policy, 0,
sizeof(*policy));
3544 switch (crypto_key->
type) {
3546 crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy->rtp);
3547 crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy->rtcp);
3554 crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy->rtp);
3555 crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy->rtcp);
3564 crypto_policy_set_aes_gcm_256_8_auth(&policy->rtp);
3565 crypto_policy_set_aes_gcm_256_8_auth(&policy->rtcp);
3573 crypto_policy_set_aes_gcm_128_8_auth(&policy->rtp);
3574 crypto_policy_set_aes_gcm_128_8_auth(&policy->rtcp);
3582 crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy->rtp);
3583 crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy->rtcp);
3589 crypto_policy_set_aes_cm_128_null_auth(&policy->rtp);
3590 crypto_policy_set_aes_cm_128_null_auth(&policy->rtcp);
3600 policy->key = (uint8_t *) crypto_key->
key;
3601 policy->next = NULL;
3603 policy->window_size = 1024;
3604 policy->allow_repeat_tx = 1;
3609 switch (direction) {
3611 policy->ssrc.type = ssrc_any_inbound;
3616 if ((stat = srtp_create(&rtp_session->recv_ctx[idx], policy)) || !rtp_session->recv_ctx[idx]) {
3631 policy->ssrc.type = ssrc_any_outbound;
3638 if ((stat = srtp_create(&rtp_session->send_ctx[idx], policy)) || !rtp_session->send_ctx[idx]) {
3659 if (rtp_session->dtls) {
3676 rtp_session->ms_per_packet = ms_per_packet;
3677 rtp_session->samples_per_interval = rtp_session->conf_samples_per_interval = samples_per_interval;
3678 rtp_session->missed_count = 0;
3679 rtp_session->samples_per_second =
3680 (uint32_t) ((
double) (1000.0f / (double) (rtp_session->ms_per_packet / 1000)) * (
double) rtp_session->samples_per_interval);
3682 rtp_session->one_second = (rtp_session->samples_per_second / rtp_session->samples_per_interval);
3690 int change_timer = 0;
3692 if (rtp_session->ms_per_packet && rtp_session->ms_per_packet != ms_per_packet) {
3698 if (change_timer && rtp_session->timer_name) {
3702 if (rtp_session->timer.timer_interface) {
3706 rtp_session->timer_name, ms_per_packet / 1000,
3710 "RE-Starting timer [%s] %d bytes per %dms\n", rtp_session->timer_name, samples_per_interval, ms_per_packet / 1000);
3713 memset(&rtp_session->timer, 0,
sizeof(rtp_session->timer));
3715 "Problem RE-Starting timer [%s] %d bytes per %dms\n", rtp_session->timer_name, samples_per_interval, ms_per_packet / 1000);
3727 rtp_session->ssrc = ssrc;
3728 rtp_session->send_msg.header.ssrc = htonl(rtp_session->ssrc);
3735 rtp_session->remote_ssrc = ssrc;
3742 uint32_t samples_per_interval,
3743 uint32_t ms_per_packet,
3746 switch_rtp_t *rtp_session = NULL;
3752 *new_rtp_session = NULL;
3755 *err =
"Packet Size Too Large!";
3760 *err =
"Memory Error!";
3768 rtp_session->
session = session;
3787 rtp_session->
seq = (uint16_t) rand();
3811 rtp_session->
payload = payload;
3818 timer_name =
"soft";
3821 if (!
zstr(timer_name) && !strcasecmp(timer_name,
"none")) {
3825 if (!
zstr(timer_name)) {
3832 "Starting timer [%s] %d bytes per %dms\n", timer_name, samples_per_interval, ms_per_packet / 1000);
3836 "Error Starting timer [%s] %d bytes per %dms, async RTP disabled\n", timer_name, samples_per_interval, ms_per_packet / 1000);
3863 switch_rtp_t *master_rtp_session = NULL;
3871 "You can not have ZRTP and SRTP enabled simultaneously, ZRTP will be disabled for this call!\n");
3873 zrtp_enabled = NULL;
3883 master_rtp_session = rtp_session;
3891 if (rtp_session == master_rtp_session) {
3892 rtp_session->zrtp_profile =
switch_core_alloc(rtp_session->
pool,
sizeof(*rtp_session->zrtp_profile));
3893 zrtp_profile_defaults(rtp_session->zrtp_profile, zrtp_global);
3895 rtp_session->zrtp_profile->allowclear = 0;
3896 rtp_session->zrtp_profile->disclose_bit = 0;
3897 rtp_session->zrtp_profile->cache_ttl = (uint32_t) -1;
3899 if (zrtp_status_ok != zrtp_session_init(zrtp_global, rtp_session->zrtp_profile, zid, initiator, &rtp_session->zrtp_session)) {
3901 zrtp_session_down(rtp_session->zrtp_session);
3902 rtp_session->zrtp_session = NULL;
3906 zrtp_session_set_userdata(rtp_session->zrtp_session, session);
3909 if (zrtp_status_ok != zrtp_stream_attach(master_rtp_session->zrtp_session, &rtp_session->zrtp_stream)) {
3913 zrtp_stream_set_userdata(rtp_session->zrtp_stream, rtp_session);
3916 zrtp_stream_registration_start(rtp_session->zrtp_stream, rtp_session->
ssrc);
3918 zrtp_stream_start(rtp_session->zrtp_stream, rtp_session->
ssrc);
3944 rtp_session->
ready = 1;
3945 *new_rtp_session = rtp_session;
3952 const
char *tx_host,
3955 uint32_t samples_per_interval,
3956 uint32_t ms_per_packet,
3959 switch_rtp_t *rtp_session = NULL;
3961 if (
zstr(rx_host)) {
3962 *err =
"Missing local host";
3967 *err =
"Missing local port";
3971 if (
zstr(tx_host)) {
3972 *err =
"Missing remote host";
3977 *err =
"Missing remote port";
4003 rtp_session->
ready = 2;
4005 rtp_session->
rx_port = rx_port;
4017 rtp_session->te = te;
4025 rtp_session->recv_te = te;
4032 rtp_session->cng_pt = pt;
4039 if (rtp_session && rtp_session->timer.timer_interface) {
4043 return &rtp_session->timer;
4056 return rtp_session->jb;
4066 if (!!pause == !!rtp_session->pause_jb) {
4070 if (rtp_session->pause_jb && !pause) {
4074 rtp_session->pause_jb = pause ? 1 : 0;
4094 if (rtp_session->vb) {
4095 return switch_jb_get_frames(rtp_session->vb, min_frame_len, max_frame_len, cur_frame_len, highest_frame_len);
4111 if (!rtp_session->vb) {
4134 if (name) x = atoi(name);
4137 if (rtp_session->jb) {
4139 }
else if (rtp_session->vb) {
4147 uint32_t queue_frames,
4148 uint32_t max_queue_frames,
4149 uint32_t samples_per_packet,
4150 uint32_t samples_per_second)
4158 if (queue_frames < 1) {
4162 if (max_queue_frames < queue_frames) {
4163 max_queue_frames = queue_frames * 3;
4168 if (rtp_session->jb) {
4188 const char *err = NULL;
4190 if (!rtp_session->ms_per_packet) {
4196 if (!(rtp_session->remote_rtcp_port = remote_port)) {
4197 rtp_session->remote_rtcp_port = rtp_session->remote_port + 1;
4205 if (send_rate == -1) {
4212 rtp_session->rtcp_interval = send_rate;
4218 rtp_session->remote_rtcp_port, 0, rtp_session->pool) !=
SWITCH_STATUS_SUCCESS || !rtp_session->rtcp_remote_addr) {
4223 rtp_session->rtcp_local_addr = rtp_session->local_addr;
4224 rtp_session->rtcp_from_addr = rtp_session->from_addr;
4225 rtp_session->rtcp_sock_input = rtp_session->sock_input;
4226 rtp_session->rtcp_sock_output = rtp_session->sock_output;
4228 rtp_session->rtcp_recv_msg_p = (
rtcp_msg_t *) &rtp_session->recv_msg;
4234 rtp_session->rtcp_recv_msg_p = (
rtcp_msg_t *) &rtp_session->rtcp_recv_msg;
4242 const
char *password, const
char *rpassword,
ice_proto_t proto,
4257 ice = &rtp_session->ice;
4261 ice = &rtp_session->rtcp_ice;
4304 ice->
addr = rtp_session->remote_addr;
4306 ice->
addr = rtp_session->rtcp_remote_addr;
4314 proto ==
IPR_RTP ?
"RTP" :
"RTCP",
rtp_type(rtp_session), ice_user, host, port);
4342 if (!
rtp_write_ready(rtp_session, 0, __LINE__) || rtp_session->tmmbr) {
4346 rtp_session->tmmbr = bps;
4353 if (!
rtp_write_ready(rtp_session, 0, __LINE__) || rtp_session->tmmbn) {
4357 rtp_session->tmmbn = bps;
4370 rtp_session->fir_count = 1;
4381 rtp_session->pli_count = 1;
4397 }
else if (rtp_session->session) {
4418 if (rtp_session->sock_input) {
4431 if (rtp_session->sock_input) {
4435 if (rtp_session->sock_output && rtp_session->sock_output != rtp_session->sock_input) {
4440 if (rtp_session->rtcp_sock_input) {
4444 if (rtp_session->rtcp_sock_output && rtp_session->rtcp_sock_output != rtp_session->rtcp_sock_input) {
4461 ret = (rtp_session->flags[
SWITCH_RTP_FLAG_IO] && rtp_session->sock_input && rtp_session->sock_output && rtp_session->remote_addr
4462 && rtp_session->ready == 2) ? 1 : 0;
4483 if (rtp_session->stats.inbound.error_log && !rtp_session->stats.inbound.error_log->stop) {
4499 if (!rtp_session || !*rtp_session || !(*rtp_session)->ready) {
4508 (*rtp_session)->ready = 0;
4529 if ((*rtp_session)->jb) {
4533 if ((*rtp_session)->vb) {
4537 if ((*rtp_session)->vbw) {
4541 if ((*rtp_session)->dtls && (*rtp_session)->dtls == (*rtp_session)->rtcp_dtls) {
4542 (*rtp_session)->rtcp_dtls = NULL;
4545 if ((*rtp_session)->dtls) {
4549 if ((*rtp_session)->rtcp_dtls) {
4554 sock = (*rtp_session)->sock_input;
4555 (*rtp_session)->sock_input = NULL;
4558 if ((*rtp_session)->sock_output != sock) {
4559 sock = (*rtp_session)->sock_output;
4560 (*rtp_session)->sock_output = NULL;
4564 if ((sock = (*rtp_session)->rtcp_sock_input)) {
4565 (*rtp_session)->rtcp_sock_input = NULL;
4568 if ((*rtp_session)->rtcp_sock_output && (*rtp_session)->rtcp_sock_output != sock) {
4569 if ((sock = (*rtp_session)->rtcp_sock_output)) {
4570 (*rtp_session)->rtcp_sock_output = NULL;
4578 for(x = 0; x < 2; x++) {
4579 if ((*rtp_session)->send_ctx[x]) {
4580 srtp_dealloc((*rtp_session)->send_ctx[x]);
4581 (*rtp_session)->send_ctx[x] = NULL;
4588 for (x = 0; x < 2; x++) {
4589 if ((*rtp_session)->recv_ctx[x]) {
4590 srtp_dealloc((*rtp_session)->recv_ctx[x]);
4591 (*rtp_session)->recv_ctx[x] = NULL;
4602 if ((*rtp_session)->zrtp_stream != NULL) {
4603 zrtp_stream_stop((*rtp_session)->zrtp_stream);
4614 if ((*rtp_session)->zrtp_session) {
4615 zrtp_session_down((*rtp_session)->zrtp_session);
4616 (*rtp_session)->zrtp_session = NULL;
4620 if ((*rtp_session)->timer.timer_interface) {
4632 rtp_session->interdigit_delay = delay;
4637 return rtp_session->sock_input;
4642 return rtp_session->samples_per_interval;
4647 rtp_session->payload = payload;
4652 return rtp_session->payload;
4657 rtp_session->invalid_handler = on_invalid;
4666 rtp_session->flags[i] = flags[i];
4669 rtp_session->autoadj_window = 20;
4670 rtp_session->autoadj_threshold = 10;
4671 rtp_session->autoadj_tally = 0;
4672 if (rtp_session->session) {
4677 if (xn > 0 && xn <= 65535) {
4678 rtp_session->autoadj_window = xn*2;
4679 rtp_session->autoadj_threshold = xn;
4697 rtp_session->flags[i] = 0;
4706 rtp_session->flags[flag] = 1;
4710 rtp_session->stats.inbound.last_processed_seq = 0;
4714 rtp_session->autoadj_window = 20;
4715 rtp_session->autoadj_threshold = 10;
4716 rtp_session->autoadj_tally = 0;
4717 if (rtp_session->session) {
4722 if (xn > 0 && xn <= 65535) {
4723 rtp_session->autoadj_window = xn*2;
4724 rtp_session->autoadj_threshold = xn;
4733 if (rtp_session->jb) {
4744 return (uint32_t) rtp_session->flags[flags];
4751 rtp_session->flags[flag] = 0;
4755 rtp_session->stats.inbound.last_processed_seq = 0;