TFLAG_RTP = (1 << 7),
TFLAG_BYE = (1 << 8),
TFLAG_ANS = (1 << 9),
- TFLAG_EARLY_MEDIA = (1 << 10)
+ TFLAG_EARLY_MEDIA = (1 << 10),
+ TFLAG_SECURE = (1 << 11)
} TFLAGS;
char *codec_order[SWITCH_MAX_CODECS];
int codec_order_last;
switch_hash *call_hash;
+ switch_hash *srtp_hash;
int running;
int codec_ms;
int dtmf_duration;
unsigned int flags;
- char *crypto_key;
} globals;
struct private_object {
unsigned int out_digit_sofar;
unsigned int out_digit_dur;
uint16_t out_digit_seq;
+ char *realm;
};
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_extip, globals.extip)
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_ip, globals.ip)
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_codec_string, globals.codec_string)
-SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_crypto_key, globals.crypto_key)
static switch_status exosip_on_init(switch_core_session *session);
static switch_status exosip_on_hangup(switch_core_session *session);
ip = globals.extip;
}
}
- snprintf(from_uri, sizeof(from_uri), "%s <sip:%s@%s>", tech_pvt->caller_profile->caller_id_name, tech_pvt->caller_profile->caller_id_number, ip);
+ snprintf(from_uri, sizeof(from_uri), "%s <sip:%s@%s>",
+ tech_pvt->caller_profile->caller_id_name,
+ tech_pvt->caller_profile->caller_id_number,
+ ip);
/* Setup codec negotiation stuffs */
osip_rfc3264_init(&tech_pvt->sdp_config);
int bw, ms;
switch_channel *channel;
const char *err;
+ char *key = NULL;
assert(tech_pvt != NULL);
tech_pvt->remote_sdp_audio_port, tech_pvt->read_codec.codec_interface->ianacode, ms);
-
+ if (tech_pvt->realm) {
+ if (!(key = (char *) switch_core_hash_find(globals.srtp_hash, tech_pvt->realm))) {
+ switch_console_printf(SWITCH_CHANNEL_CONSOLE, "undefined Realm %s\n", tech_pvt->realm);
+ switch_channel_hangup(channel);
+ switch_set_flag(tech_pvt, TFLAG_BYE);
+ switch_clear_flag(tech_pvt, TFLAG_IO);
+ return SWITCH_STATUS_FALSE;
+ } else {
+ switch_console_printf(SWITCH_CHANNEL_CONSOLE, "using Realm %s\n", tech_pvt->realm);
+ }
+ }
+
tech_pvt->rtp_session = switch_rtp_new(tech_pvt->local_sdp_audio_ip,
tech_pvt->local_sdp_audio_port,
tech_pvt->remote_sdp_audio_ip,
tech_pvt->read_codec.implementation->encoded_bytes_per_frame,
ms,
0,
- globals.crypto_key,
+ key,
&err, switch_core_session_get_pool(tech_pvt->session));
if (tech_pvt->rtp_session) {
char name[128];
switch_caller_profile *caller_profile = NULL;
+
+ if (*outbound_profile->destination_number == '!') {
+ char *p;
+
+ outbound_profile->destination_number++;
+
+ if ((p=strchr(outbound_profile->destination_number, '!'))) {
+ *p = '\0';
+ p++;
+ tech_pvt->realm = switch_core_session_strdup(*new_session, outbound_profile->destination_number);
+ outbound_profile->destination_number = p;
+ if ((p = strchr(tech_pvt->realm, '!'))) {
+ *p = '\0';
+ }
+ }
+
+ }
snprintf(name, sizeof(name), "Exosip/%s-%04x", outbound_profile->destination_number, rand() & 0xffff);
switch_channel_set_name(channel, name);
}
switch_core_hash_init(&globals.call_hash, module_pool);
+ switch_core_hash_init(&globals.srtp_hash, module_pool);
/* connect my internal structure to the blank pointer passed to me */
*interface = &exosip_module_interface;
set_global_ip(val);
} else if (!strcmp(var, "dialplan")) {
set_global_dialplan(val);
- } else if (!strcmp(var, "crypto_key")) {
- set_global_crypto_key(val);
+ } else if (!strncasecmp(var, "srtp:", 5)) {
+ char *name = var + 5;
+ switch_core_hash_insert_dup(globals.srtp_hash, name, val);
} else if (!strcmp(var, "codec_prefs")) {
set_global_codec_string(val);
globals.codec_order_last = switch_separate_string(globals.codec_string, ',', globals.codec_order, SWITCH_MAX_CODECS);
switch_set_flag(rtp_session, SWITCH_RTP_FLAG_SECURE);
crypto_policy_set_rtp_default(&policy.rtp);
crypto_policy_set_rtcp_default(&policy.rtcp);
- policy.ssrc.type = ssrc_specific;
+ policy.ssrc.type = ssrc_any_inbound;
policy.ssrc.value = ssrc;
policy.key = (uint8_t *) key;
policy.next = NULL;
rtp_session->next_read = switch_time_now() + rtp_session->ms_per_packet;
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE)) {
- srtp_create(&rtp_session->recv_ctx, &policy);
- srtp_create(&rtp_session->send_ctx, &policy);
+ err_status_t stat;
+
+ if ((stat = srtp_create(&rtp_session->recv_ctx, &policy))) {
+ switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Error allocating srtp [%d]\n", stat);
+ *err = "Error";
+ return SWITCH_STATUS_FALSE;
+ }
+ if ((stat = srtp_create(&rtp_session->send_ctx, &policy))) {
+ switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Error allocating srtp [%d]\n", stat);
+ *err = "Error";
+ return SWITCH_STATUS_FALSE;
+ }
}
*new_rtp_session = rtp_session;
bytes = sizeof(rtp_msg_t);
status = switch_socket_recvfrom(rtp_session->from_addr, rtp_session->sock, 0, (void *)&rtp_session->recv_msg, &bytes);
+ if (!switch_test_flag(rtp_session, SWITCH_RTP_FLAG_IO)) {
+ return -1;
+ }
+
if (bytes < 0) {
return bytes;
- }
-
- if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE)) {
+ } else if (bytes > 0 && switch_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE)) {
int sbytes = (int)bytes;
err_status_t stat;
- stat = srtp_unprotect(rtp_session->recv_ctx, &rtp_session->recv_msg, &sbytes);
+ stat = srtp_unprotect(rtp_session->recv_ctx, &rtp_session->recv_msg.header, &sbytes);
if (stat) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE,
"error: srtp unprotection failed with code %d%s\n", stat,
stat == err_status_replay_fail ? " (replay check failed)" :
stat == err_status_auth_fail ? " (auth check failed)" : "");
- return -1;
+ switch_yield(1000);
+ continue;
}
bytes = sbytes;
}
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER)) {
- if (!switch_test_flag(rtp_session, SWITCH_RTP_FLAG_IO)) {
- return -1;
- }
-
+
if ((switch_time_now() - rtp_session->next_read) > 1000) {
/* We're late! We're Late!*/
memset(&rtp_session->recv_msg, 0, 13);
bytes = datalen + rtp_header_len;
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE)) {
int sbytes = (int)bytes;
- srtp_protect(rtp_session->send_ctx, &rtp_session->send_msg, &sbytes);
+ err_status_t stat;
+
+ stat = srtp_protect(rtp_session->send_ctx, &rtp_session->send_msg.header, &sbytes);
+ if (stat) {
+ switch_console_printf(SWITCH_CHANNEL_CONSOLE, "error: srtp unprotection failed with code %d\n", stat);
+ }
+
bytes = sbytes;
}