Commit 87cf71d7 authored by jehan's avatar jehan
Browse files

update ice to be compatible with rtp transport modifiers

parent 3138f02d
......@@ -127,7 +127,7 @@ typedef struct _IceStunServerCheckTransaction {
} IceStunServerCheckTransaction;
typedef struct _IceStunServerCheck {
ortp_socket_t sock;
RtpTransport *rtptp;
int srcport;
MSList *transactions; /**< List of IceStunServerCheckTransaction structures. */
MSTimeSpec next_transmission_time;
......
......@@ -389,19 +389,12 @@ static void ms_dtls_srtp_set_transport(MSDtlsSrtpContext *userData, RtpSession *
rtp_session_get_transports(s,&rtpt,&rtcpt);
ms_dtls_srtp_transport_modifier_new(userData, &rtp_modifier,&rtcp_modifier);
/*if transports are set, we assume they are meta transporters, otherwise create them*/
if (rtpt==NULL&&rtcpt==NULL){
meta_rtp_transport_new(&rtpt, TRUE, NULL, 0);
meta_rtp_transport_new(&rtcpt, FALSE, NULL, 0);
}
meta_rtp_transport_append_modifier(rtpt, rtp_modifier);
meta_rtp_transport_append_modifier(rtcpt, rtcp_modifier);
/* save transport modifier into context, needed to inject packets generated by DTLS */
userData->rtp_modifier = rtp_modifier;
rtp_session_set_transports(s, rtpt, rtcpt);
}
MSDtlsSrtpContext* ms_dtls_srtp_context_new(MediaStream *stream, RtpSession *s, MSDtlsSrtpParams *params){
......
......@@ -123,7 +123,7 @@ static MSTimeSpec ice_add_ms(MSTimeSpec orig, uint32_t ms);
static int32_t ice_compare_time(MSTimeSpec ts1, MSTimeSpec ts2);
static char * ice_inet_ntoa(struct sockaddr *addr, int addrlen, char *dest, int destlen);
static void transactionID2string(const UInt96 *tr_id, char *tr_id_str);
static void ice_send_stun_server_binding_request(ortp_socket_t sock, const struct sockaddr *server, socklen_t addrlen, IceStunServerCheck *check);
static void ice_send_stun_server_binding_request(RtpTransport *rtptp, const struct sockaddr *server, socklen_t addrlen, IceStunServerCheck *check);
static int ice_compare_transport_addresses(const IceTransportAddress *ta1, const IceTransportAddress *ta2);
static int ice_compare_pair_priorities(const IceCandidatePair *p1, const IceCandidatePair *p2);
static int ice_compare_pairs(const IceCandidatePair *p1, const IceCandidatePair *p2);
......@@ -895,20 +895,20 @@ static bool_t ice_check_list_gathering_needed(const IceCheckList *cl)
static void ice_check_list_gather_candidates(IceCheckList *cl, Session_Index *si)
{
IceStunServerCheck *check;
ortp_socket_t sock = -1;
RtpTransport *rtptp=NULL;
MSTimeSpec curtime = ice_current_time();
if ((cl->rtp_session != NULL) && (cl->gathering_candidates == FALSE) && (cl->state != ICL_Completed) && (ice_check_list_candidates_gathered(cl) == FALSE)) {
cl->gathering_candidates = TRUE;
cl->gathering_start_time = curtime;
sock = rtp_session_get_rtp_socket(cl->rtp_session);
if (sock > 0) {
rtp_session_get_transports(cl->rtp_session,&rtptp,NULL);
if (rtptp) {
check = (IceStunServerCheck *)ms_new0(IceStunServerCheck, 1);
check->sock = sock;
check->rtptp = rtptp;
check->srcport = rtp_session_get_local_port(cl->rtp_session);
if (si->index == 0) {
check->next_transmission_time = ice_add_ms(curtime, ICE_DEFAULT_RTO_DURATION);
ice_send_stun_server_binding_request(sock, (struct sockaddr *)&cl->session->ss, cl->session->ss_len, check);
ice_send_stun_server_binding_request(rtptp, (struct sockaddr *)&cl->session->ss, cl->session->ss_len, check);
} else {
check->next_transmission_time = ice_add_ms(curtime, 2 * si->index * ICE_DEFAULT_TA_DURATION);
}
......@@ -916,10 +916,11 @@ static void ice_check_list_gather_candidates(IceCheckList *cl, Session_Index *si
} else {
ms_error("ice: no rtp socket found for session [%p]",cl->rtp_session);
}
sock = rtp_session_get_rtcp_socket(cl->rtp_session);
if (sock > 0) {
rtptp=NULL;
rtp_session_get_transports(cl->rtp_session,NULL,&rtptp);
if (rtptp) {
check = (IceStunServerCheck *)ms_new0(IceStunServerCheck, 1);
check->sock = sock;
check->rtptp = rtptp;
check->srcport = rtp_session_get_local_rtcp_port(cl->rtp_session);
check->next_transmission_time = ice_add_ms(curtime, 2 * si->index * ICE_DEFAULT_TA_DURATION + ICE_DEFAULT_TA_DURATION);
cl->stun_server_checks = ms_list_append(cl->stun_server_checks, check);
......@@ -1082,20 +1083,38 @@ static IceTransaction * ice_find_transaction(const IceCheckList *cl, const IceCa
* STUN PACKETS HANDLING *
*****************************************************************************/
static void ice_send_stun_server_binding_request(ortp_socket_t sock, const struct sockaddr *server, socklen_t addrlen, IceStunServerCheck *check)
static int ice_send_message_to_socket(const RtpTransport * rtpt,char* buf,size_t len, const struct sockaddr *to, socklen_t tolen) {
/*rtp_session_get_transports(rtp_session,&rtpt,NULL);*/
return meta_rtp_transport_modifier_inject_packet_to(rtpt
, NULL
,rtp_session_create_packet_raw((const uint8_t *)buf, len)
, 0
,to
,tolen);
}
static int ice_send_message_to_stun_addr(const RtpTransport * rtpt,char* buff,size_t len, StunAddress4 *dest) {
struct sockaddr_in dest_addr;
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.sin_addr.s_addr = htonl(dest->addr);
dest_addr.sin_port = htons(dest->port);
dest_addr.sin_family = AF_INET;
return ice_send_message_to_socket(rtpt, buff, len,(struct sockaddr *)&dest_addr, sizeof(dest_addr));
}
static void ice_send_stun_server_binding_request(RtpTransport *rtptp, const struct sockaddr *server, socklen_t addrlen, IceStunServerCheck *check)
{
StunMessage msg;
StunAtrString username;
StunAtrString password;
char buf[STUN_MAX_MESSAGE_SIZE];
int len = STUN_MAX_MESSAGE_SIZE;
const struct sockaddr_in *servaddr = (const struct sockaddr_in *)server;
char tr_id_str[25];
memset(&msg, 0, sizeof(StunMessage));
memset(&username,0,sizeof(username));
memset(&password,0,sizeof(password));
stunBuildReqSimple(&msg, &username, FALSE, FALSE, check->sock);
stunBuildReqSimple(&msg, &username, FALSE, FALSE, 0);
len = stunEncodeMessage(&msg, buf, len, &password);
if (len > 0) {
IceStunServerCheckTransaction *transaction = ms_new0(IceStunServerCheckTransaction, 1);
......@@ -1104,7 +1123,7 @@ static void ice_send_stun_server_binding_request(ortp_socket_t sock, const struc
check->transactions = ms_list_append(check->transactions, transaction);
transactionID2string(&msg.msgHdr.tr_id, tr_id_str);
ms_message("ice: Send STUN binding request from port %u [%s]", check->srcport, tr_id_str);
sendMessage(sock, buf, len, htonl(servaddr->sin_addr.s_addr), htons(servaddr->sin_port));
ice_send_message_to_socket(rtptp, buf, len, server, addrlen);
} else {
ms_error("ice: encoding stun binding request from port %u [%s] failed", check->srcport, tr_id_str);
}
......@@ -1113,6 +1132,7 @@ static void ice_send_stun_server_binding_request(ortp_socket_t sock, const struc
static int ice_parse_stun_server_binding_response(const StunMessage *msg, char *addr, int addr_len, int *port)
{
struct sockaddr_in addr_in;
memset(&addr_in,0,sizeof(addr_in));
if (msg->hasXorMappedAddress) {
*port = msg->xorMappedAddress.ipv4.port;
......@@ -1138,7 +1158,7 @@ static void ice_send_binding_request(IceCheckList *cl, IceCandidatePair *pair, c
IceTransaction *transaction;
char buf[STUN_MAX_MESSAGE_SIZE];
int len = STUN_MAX_MESSAGE_SIZE;
int socket = 0;
RtpTransport *rtptp;
char tr_id_str[25];
transaction = ice_find_transaction(cl, pair);
......@@ -1170,9 +1190,9 @@ static void ice_send_binding_request(IceCheckList *cl, IceCandidatePair *pair, c
pair->transmission_time = ice_current_time();
if (pair->local->componentID == 1) {
socket = rtp_session_get_rtp_socket(rtp_session);
rtp_session_get_transports(rtp_session,&rtptp,NULL);
} else if (pair->local->componentID == 2) {
socket = rtp_session_get_rtcp_socket(rtp_session);
rtp_session_get_transports(rtp_session,NULL,&rtptp);
} else return;
snprintf(username.value, sizeof(username.value) - 1, "%s:%s", ice_check_list_remote_ufrag(cl), ice_check_list_local_ufrag(cl));
......@@ -1226,7 +1246,8 @@ static void ice_send_binding_request(IceCheckList *cl, IceCandidatePair *pair, c
pair->local->taddr.ip, pair->local->taddr.port, candidate_type_values[pair->local->type],
pair->remote->taddr.ip, pair->remote->taddr.port, candidate_type_values[pair->remote->type], tr_id_str);
}
sendMessage(socket, buf, len, dest.addr, dest.port);
ice_send_message_to_stun_addr(rtptp, buf, len, &dest);
if (pair->state != ICP_InProgress) {
/* First transmission of the request, initialize the retransmission timer. */
......@@ -1251,12 +1272,13 @@ static int ice_get_componentID_from_rtp_session(const OrtpEventData *evt_data)
}
static int ice_get_socket_from_rtp_session(const RtpSession *rtp_session, const OrtpEventData *evt_data)
{
static int ice_get_transport_from_rtp_session(const RtpSession *rtp_session, const OrtpEventData *evt_data, RtpTransport **rtptp) {
if (evt_data->info.socket_type == OrtpRTPSocket) {
return rtp_session_get_rtp_socket(rtp_session);
rtp_session_get_transports(rtp_session, rtptp, NULL);
return 0;
} else if (evt_data->info.socket_type == OrtpRTCPSocket) {
return rtp_session_get_rtcp_socket(rtp_session);
rtp_session_get_transports(rtp_session, NULL,rtptp);
return 0;
}
return -1;
}
......@@ -1276,7 +1298,7 @@ static void ice_send_binding_response(IceCheckList *cl, const RtpSession *rtp_se
StunAtrString password;
char buf[STUN_MAX_MESSAGE_SIZE];
int len = STUN_MAX_MESSAGE_SIZE;
int socket = ice_get_socket_from_rtp_session(rtp_session, evt_data);
RtpTransport *rtptp;
int recvport = ice_get_recv_port_from_rtp_session(rtp_session, evt_data);
struct sockaddr_in dest_addr;
struct sockaddr_in source_addr;
......@@ -1284,9 +1306,13 @@ static void ice_send_binding_response(IceCheckList *cl, const RtpSession *rtp_se
char source_addr_str[256];
char tr_id_str[25];
if (socket < 0) return;
ice_get_transport_from_rtp_session(rtp_session, evt_data,&rtptp);
if (!rtptp) return;
memset(&response, 0, sizeof(response));
memset(&password, 0, sizeof(password));
memset(&dest_addr,0,sizeof(dest_addr));
memset(&source_addr,0,sizeof(source_addr));
/* Copy magic cookie and transaction ID from the request. */
response.msgHdr.magic_cookie = ntohl(msg->msgHdr.magic_cookie);
......@@ -1325,7 +1351,7 @@ static void ice_send_binding_response(IceCheckList *cl, const RtpSession *rtp_se
source_addr.sin_family = AF_INET;
ice_inet_ntoa((struct sockaddr *)&source_addr, sizeof(source_addr), source_addr_str, sizeof(source_addr_str));
ms_message("ice: Send binding response: %s:%u --> %s:%u [%s]", source_addr_str, recvport, dest_addr_str, dest->port, tr_id_str);
sendMessage(socket, buf, len, dest->addr, dest->port);
ice_send_message_to_socket(rtptp, buf, len, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
}
}
......@@ -1335,7 +1361,7 @@ static void ice_send_error_response(const RtpSession *rtp_session, const OrtpEve
StunAtrString password;
char buf[STUN_MAX_MESSAGE_SIZE];
int len = STUN_MAX_MESSAGE_SIZE;
int socket = ice_get_socket_from_rtp_session(rtp_session, evt_data);
RtpTransport* rtptp;
int recvport = ice_get_recv_port_from_rtp_session(rtp_session, evt_data);
struct sockaddr_in dest_addr;
struct sockaddr_in source_addr;
......@@ -1345,6 +1371,9 @@ static void ice_send_error_response(const RtpSession *rtp_session, const OrtpEve
if (socket < 0) return;
memset(&response, 0, sizeof(response));
memset(&dest_addr, 0, sizeof(dest_addr));
memset(&source_addr, 0, sizeof(source_addr));
ice_get_transport_from_rtp_session(rtp_session, evt_data,&rtptp);
/* Copy magic cookie and transaction ID from the request. */
response.msgHdr.magic_cookie = ntohl(msg->msgHdr.magic_cookie);
......@@ -1361,6 +1390,7 @@ static void ice_send_error_response(const RtpSession *rtp_session, const OrtpEve
len = stunEncodeMessage(&response, buf, len, &password);
if (len > 0) {
RtpTransport *rtpt=NULL;
transactionID2string(&response.msgHdr.tr_id, tr_id_str);
dest_addr.sin_addr.s_addr = htonl(dest->addr);
dest_addr.sin_port = htons(dest->port);
......@@ -1371,7 +1401,12 @@ static void ice_send_error_response(const RtpSession *rtp_session, const OrtpEve
source_addr.sin_family = AF_INET;
ice_inet_ntoa((struct sockaddr *)&source_addr, sizeof(source_addr), source_addr_str, sizeof(source_addr_str));
ms_message("ice: Send error response: %s:%u --> %s:%u [%s]", source_addr_str, recvport, dest_addr_str, dest->port, tr_id_str);
sendMessage(socket, buf, len, dest->addr, dest->port);
rtp_session_get_transports(rtp_session,&rtpt,NULL);
ice_send_message_to_socket( rtpt
, buf
, len
, (struct sockaddr *)&dest_addr
, sizeof(dest_addr));
}
}
......@@ -1381,12 +1416,12 @@ static void ice_send_indication(const IceCandidatePair *pair, const RtpSession *
StunAddress4 dest;
char buf[STUN_MAX_MESSAGE_SIZE];
int len = STUN_MAX_MESSAGE_SIZE;
int socket;
RtpTransport *rtptp;
if (pair->local->componentID == 1) {
socket = rtp_session_get_rtp_socket(rtp_session);
rtp_session_get_transports(rtp_session,&rtptp,NULL);
} else if (pair->local->componentID == 2) {
socket = rtp_session_get_rtcp_socket(rtp_session);
rtp_session_get_transports(rtp_session,NULL,&rtptp);
} else return;
stunParseHostName(pair->remote->taddr.ip, &dest.addr, &dest.port, pair->remote->taddr.port);
......@@ -1400,7 +1435,7 @@ static void ice_send_indication(const IceCandidatePair *pair, const RtpSession *
ms_message("ice: Send indication for pair %p: %s:%u:%s --> %s:%u:%s", pair,
pair->local->taddr.ip, pair->local->taddr.port, candidate_type_values[pair->local->type],
pair->remote->taddr.ip, pair->remote->taddr.port, candidate_type_values[pair->remote->type]);
sendMessage(socket, buf, len, dest.addr, dest.port);
ice_send_message_to_stun_addr(rtptp, buf, len, &dest);
}
}
......@@ -1527,7 +1562,6 @@ static int ice_check_received_binding_request_role_conflict(const IceCheckList *
static void ice_fill_transport_address(IceTransportAddress *taddr, const char *ip, int port)
{
memset(taddr, 0, sizeof(IceTransportAddress));
strncpy(taddr->ip, ip, sizeof(taddr->ip));
taddr->port = port;
}
......@@ -1585,6 +1619,7 @@ static IceCandidatePair * ice_trigger_connectivity_check_on_binding_request(IceC
struct sockaddr_in source_addr;
char source_addr_str[256];
int recvport = ice_get_recv_port_from_rtp_session(rtp_session, evt_data);
memset(&source_addr,0,sizeof(source_addr));
if (recvport < 0) return NULL;
......@@ -1687,9 +1722,9 @@ static void ice_handle_received_binding_request(IceCheckList *cl, RtpSession *rt
ice_conclude_processing(cl, rtp_session);
}
static int ice_find_stun_server_check(const IceStunServerCheck *check, const ortp_socket_t *sock)
static int ice_find_stun_server_check(const IceStunServerCheck *check, const RtpTransport *rtptp)
{
return !(check->sock == *sock);
return !(check->rtptp == rtptp);
}
static IceCheckList * ice_find_check_list_gathering_candidates(const IceSession *session)
......@@ -1717,7 +1752,10 @@ static int ice_check_received_binding_response_addresses(const RtpSession *rtp_s
stunParseHostName(pair->remote->taddr.ip, &dest.addr, &dest.port, pair->remote->taddr.port);
stunParseHostName(pair->local->taddr.ip, &local.addr, &local.port, recvport);
// TODO: Handle IPv6 for ipi_addr
if ((remote_addr->addr != dest.addr) || (remote_addr->port != dest.port) || (ntohl(evt_data->packet->recv_addr.addr.ipi_addr.s_addr) != local.addr) || (local.port != pair->local->taddr.port)) {
if ( (remote_addr->addr != dest.addr)
|| (remote_addr->port != dest.port)
|| (ntohl(evt_data->packet->recv_addr.addr.ipi_addr.s_addr) != local.addr)
|| (local.port != pair->local->taddr.port)) {
/* Non-symmetric addresses, set the state of the pair to Failed as defined in 7.1.3.1. */
ms_warning("ice: Non symmetric addresses, set state of pair %p to Failed", pair);
ice_pair_set_state(pair, ICP_Failed);
......@@ -1751,6 +1789,8 @@ static IceCandidate * ice_discover_peer_reflexive_candidate(IceCheckList *cl, co
MSList *elem;
memset(&taddr, 0, sizeof(taddr));
memset(&addr_in,0,sizeof(addr_in));
addr_in.sin_addr.s_addr = htonl(msg->xorMappedAddress.ipv4.addr);
addr_in.sin_port = htons(msg->xorMappedAddress.ipv4.port);
addr_in.sin_family = AF_INET;
......@@ -1897,15 +1937,15 @@ static void ice_handle_received_binding_response(IceCheckList *cl, RtpSession *r
OrtpEvent *ev;
char addr[64];
int port;
ortp_socket_t sock;
RtpTransport *rtptp=NULL;
int componentID;
const struct sockaddr_in *servaddr = (const struct sockaddr_in *)&cl->session->ss;
bool_t stun_server_response = FALSE;
if (cl->gathering_candidates == TRUE) {
if ((htonl(remote_addr->addr) == servaddr->sin_addr.s_addr) && (htons(remote_addr->port) == servaddr->sin_port)) {
sock = ice_get_socket_from_rtp_session(rtp_session, evt_data);
elem = ms_list_find_custom(cl->stun_server_checks, (MSCompareFunc)ice_find_stun_server_check, &sock);
ice_get_transport_from_rtp_session(rtp_session, evt_data,&rtptp);
elem = ms_list_find_custom(cl->stun_server_checks, (MSCompareFunc)ice_find_stun_server_check, rtptp);
if (elem != NULL) {
IceStunServerCheckTransaction *transaction;
IceStunServerCheck *check = (IceStunServerCheck *)elem->data;
......@@ -2020,6 +2060,8 @@ void ice_handle_stun_packet(IceCheckList *cl, RtpSession *rtp_session, const Ort
if (cl->session == NULL) return;
memset(&msg, 0, sizeof(msg));
memset(&source_addr, 0, sizeof(source_addr));
res = stunParseMessage((char *) mp->b_rptr, mp->b_wptr - mp->b_rptr, &msg);
if (res == FALSE) {
ms_warning("ice: Received invalid STUN packet");
......@@ -3062,7 +3104,7 @@ static void ice_send_stun_server_checks(IceStunServerCheck *check, IceCheckList
if (ice_compare_time(curtime, check->next_transmission_time) >= 0) {
if (ms_list_size(check->transactions) < ICE_MAX_STUN_REQUEST_RETRANSMISSIONS) {
check->next_transmission_time = ice_add_ms(curtime, ICE_DEFAULT_RTO_DURATION);
ice_send_stun_server_binding_request(check->sock, (struct sockaddr *)&cl->session->ss, cl->session->ss_len, check);
ice_send_stun_server_binding_request(check->rtptp, (struct sockaddr *)&cl->session->ss, cl->session->ss_len, check);
}
}
}
......
......@@ -267,14 +267,8 @@ static int check_srtp_session_created(MediaStream *stream){
stream->sessions.srtp_session=session;
srtp_transport_modifier_new(session,&rtp_modifier,&rtcp_modifier);
rtp_session_get_transports(stream->sessions.rtp_session,&rtp,&rtcp);
/*if transports are set, we assume they are meta transporters, otherwise create them*/
if (rtp==NULL&&rtcp==NULL){
meta_rtp_transport_new(&rtp, TRUE, NULL, 0);
meta_rtp_transport_new(&rtcp, FALSE, NULL, 0);
}
meta_rtp_transport_append_modifier(rtp, rtp_modifier);
meta_rtp_transport_append_modifier(rtcp, rtcp_modifier);
rtp_session_set_transports(stream->sessions.rtp_session,rtp,rtcp);
stream->sessions.is_secured=TRUE;
}
return 0;
......
......@@ -355,19 +355,11 @@ static void ms_zrtp_set_transport(MSZrtpContext *userData, RtpSession *s) {
rtp_session_get_transports(s,&rtpt,&rtcpt);
ms_zrtp_transport_modifier_new(userData, &rtp_modifier,&rtcp_modifier);
/*if transports are set, we assume they are meta transporters, otherwise create them*/
if (rtpt==NULL&&rtcpt==NULL){
meta_rtp_transport_new(&rtpt, TRUE, NULL, 0);
meta_rtp_transport_new(&rtcpt, FALSE, NULL, 0);
}
meta_rtp_transport_append_modifier(rtpt, rtp_modifier);
meta_rtp_transport_append_modifier(rtcpt, rtcp_modifier);
/* save transport modifier into context, needed to inject packets generated by ZRTP */
userData->rtp_modifier = rtp_modifier;
rtp_session_set_transports(s, rtpt, rtcpt);
}
static MSZrtpContext* ms_zrtp_configure_context(MSZrtpContext *userData, RtpSession *s) {
bzrtpContext_t *context=userData->zrtpContext;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment