Commit 2b1415bf authored by Ghislain MARY's avatar Ghislain MARY

Fix memory leaks.

parent ea7df952
......@@ -186,6 +186,7 @@ typedef enum {
typedef struct {
RtpSession *rtp_session;
RtpTransport *endpoint;
char *realm;
char *nonce;
char *username;
......
......@@ -657,17 +657,21 @@ static void alsa_card_detect(MSSndCardManager *m){
if (snd_device_name_hint(-1, "pcm", &hints)==0){
for(i=0; hints[i]!=NULL; ++i){
char *hint = snd_device_name_get_hint(hints[i],"NAME");
char *device_name = strsep(&hint, ":");
if (device_name){
char *card_hint = strsep(&hint, ",");
if (card_hint){
if (strcmp(strsep(&card_hint, "="), "CARD")==0){
char *card_name = card_hint;
card_names[hint_device_count] = card_name;
device_names[hint_device_count] = device_name;
hint_device_count++;
char *hint_ptr = hint;
if (hint != NULL) {
char *device_name = strsep(&hint, ":");
if (device_name){
char *card_hint = strsep(&hint, ",");
if (card_hint){
if (strcmp(strsep(&card_hint, "="), "CARD")==0){
char *card_name = card_hint;
card_names[hint_device_count] = card_name;
device_names[hint_device_count] = device_name;
hint_device_count++;
}
}
}
free(hint_ptr);
}
}
snd_device_name_free_hint(hints);
......
......@@ -1067,14 +1067,12 @@ void ice_session_enable_forced_relay(IceSession *session, bool_t enable)
session->forced_relay = enable;
}
static void ice_check_list_create_turn_contexts(IceCheckList *cl)
{
static void ice_check_list_create_turn_contexts(IceCheckList *cl) {
cl->rtp_turn_context = ms_turn_context_new(MS_TURN_CONTEXT_TYPE_RTP, cl->rtp_session);
cl->rtcp_turn_context = ms_turn_context_new(MS_TURN_CONTEXT_TYPE_RTCP, cl->rtp_session);
}
void ice_session_enable_turn(IceSession *session, bool_t enable)
{
void ice_session_enable_turn(IceSession *session, bool_t enable) {
int i;
session->turn_enabled = enable;
for (i = 0; i < ICE_SESSION_MAX_CHECK_LISTS; i++) {
......@@ -1222,6 +1220,7 @@ static void ice_stun_server_request_transaction_free(IceStunServerRequestTransac
static void ice_stun_server_request_free(IceStunServerRequest *request) {
ms_list_for_each(request->transactions, (void (*)(void*))ice_stun_server_request_transaction_free);
ms_list_free(request->transactions);
if (request->source_ai != NULL) bctbx_freeaddrinfo(request->source_ai);
ms_free(request);
}
......@@ -1239,8 +1238,8 @@ static int ice_send_message_to_socket(const RtpTransport * rtpt, char* buf, size
static int ice_send_message_to_stun_addr(const RtpTransport * rtpt, char* buff, size_t len, MSStunAddress *source, MSStunAddress *dest) {
struct sockaddr_storage source_addr;
struct sockaddr_storage dest_addr;
socklen_t source_addrlen = 0;
socklen_t dest_addrlen = 0;
socklen_t source_addrlen = sizeof(source_addr);
socklen_t dest_addrlen = sizeof(dest_addr);
ms_stun_address_to_sockaddr(source, (struct sockaddr *)&source_addr, &source_addrlen);
ms_stun_address_to_sockaddr(dest, (struct sockaddr *)&dest_addr, &dest_addrlen);
return ice_send_message_to_socket(rtpt, buff, len, (struct sockaddr*)&source_addr, source_addrlen, (struct sockaddr *)&dest_addr, dest_addrlen);
......@@ -1328,6 +1327,7 @@ static IceStunServerRequestTransaction * ice_send_turn_server_create_permission_
stun_message_fill_authentication_from_turn_context(msg, request->turn_context);
request->stun_method = ms_stun_message_get_method(msg);
transaction = ice_send_stun_request(request->rtptp, request->source_ai->ai_addr, request->source_ai->ai_addrlen, server, addrlen, msg, "TURN create permission request");
ms_stun_message_destroy(msg);
return transaction;
}
......@@ -1337,6 +1337,7 @@ static IceStunServerRequestTransaction * ice_send_turn_server_channel_bind_reque
stun_message_fill_authentication_from_turn_context(msg, request->turn_context);
request->stun_method = ms_stun_message_get_method(msg);
transaction = ice_send_stun_request(request->rtptp, request->source_ai->ai_addr, request->source_ai->ai_addrlen, server, addrlen, msg, "TURN channel bind request");
ms_stun_message_destroy(msg);
return transaction;
}
......@@ -1482,7 +1483,7 @@ static void ice_send_binding_request(IceCheckList *cl, IceCandidatePair *pair, c
}
}
if (buf != NULL) ms_free(buf);
ms_free(msg);
ms_stun_message_destroy(msg);
}
static int ice_get_componentID_from_rtp_session(const OrtpEventData *evt_data)
......@@ -1551,9 +1552,9 @@ static void ice_send_binding_response(IceCheckList *cl, const RtpSession *rtp_se
int len;
RtpTransport *rtptp = NULL;
struct sockaddr_storage dest_addr;
socklen_t dest_addrlen = 0;
socklen_t dest_addrlen = sizeof(dest_addr);
struct sockaddr_storage source_addr;
socklen_t source_addrlen = 0;
socklen_t source_addrlen = sizeof(source_addr);
char dest_addr_str[256];
char source_addr_str[256];
char tr_id_str[25];
......@@ -1601,7 +1602,7 @@ static void ice_send_binding_response(IceCheckList *cl, const RtpSession *rtp_se
ice_send_message_to_socket(rtptp, buf, len, (struct sockaddr *)&source_addr, source_addrlen, (struct sockaddr *)&dest_addr, dest_addrlen);
}
if (buf != NULL) ms_free(buf);
ms_free(response);
ms_stun_message_destroy(response);
}
static void ice_send_error_response(const RtpSession *rtp_session, const OrtpEventData *evt_data, const MSStunMessage *msg, const MSStunAddress *dest, uint16_t error_num, const char *error_msg)
......@@ -1611,9 +1612,9 @@ static void ice_send_error_response(const RtpSession *rtp_session, const OrtpEve
int len;
RtpTransport* rtptp;
struct sockaddr_storage dest_addr;
socklen_t dest_addrlen = 0;
socklen_t dest_addrlen = sizeof(dest_addr);
struct sockaddr_storage source_addr;
socklen_t source_addrlen = 0;
socklen_t source_addrlen = sizeof(source_addr);
char dest_addr_str[256];
char source_addr_str[256];
char tr_id_str[25];
......@@ -1744,10 +1745,12 @@ static int ice_check_received_binding_request_integrity(const IceCheckList *cl,
{
char *hmac;
mblk_t *mp = evt_data->packet;
int ret = 0;
/* Check the message integrity: first remove length of fingerprint... */
char *lenpos = (char *)mp->b_rptr + sizeof(uint16_t);
uint16_t newlen = htons(ms_stun_message_get_length(msg) - 8);
memcpy(lenpos, &newlen, sizeof(uint16_t));
hmac = ms_stun_calculate_integrity_short_term((char *)mp->b_rptr, (size_t)(mp->b_wptr - mp->b_rptr - 24 - 8), ice_check_list_local_pwd(cl));
/* ... and then restore the length with fingerprint. */
......@@ -1759,10 +1762,11 @@ static int ice_check_received_binding_request_integrity(const IceCheckList *cl,
ms_message("ice: skipping message integrity check for cl [%p]",cl);
} else {
ice_send_error_response(rtp_session, evt_data, msg, remote_addr, MS_STUN_ERROR_CODE_UNAUTHORIZED, "Wrong MESSAGE-INTEGRITY attribute");
return -1;
ret = -1;
}
}
return 0;
ms_free(hmac);
return ret;
}
static int ice_check_received_binding_request_username(const IceCheckList *cl, const RtpSession *rtp_session, const OrtpEventData *evt_data, const MSStunMessage *msg, const MSStunAddress *remote_addr)
......@@ -1809,7 +1813,7 @@ static void ice_fill_transport_address_from_sockaddr(IceTransportAddress *taddr,
static void ice_fill_transport_address_from_stun_address(IceTransportAddress *taddr, const MSStunAddress *stun_addr) {
struct sockaddr_storage addr;
socklen_t addrlen = 0;
socklen_t addrlen = sizeof(addr);
ms_stun_address_to_sockaddr(stun_addr, (struct sockaddr *)&addr, &addrlen);
ice_fill_transport_address_from_sockaddr(taddr, (struct sockaddr *)&addr, addrlen);
}
......@@ -1821,6 +1825,7 @@ static MSStunAddress ice_transport_address_to_stun_address(IceTransportAddress *
static void ice_transport_address_to_printable_ip_address(const IceTransportAddress *taddr, char *printable_ip, size_t printable_ip_size) {
struct addrinfo *ai = bctbx_ip_address_to_addrinfo(taddr->family, SOCK_DGRAM, taddr->ip, taddr->port);
bctbx_addrinfo_to_printable_ip_address(ai, printable_ip, printable_ip_size);
bctbx_freeaddrinfo(ai);
}
static int ice_find_candidate_from_foundation(const IceCandidate *candidate, const char *foundation)
......@@ -2481,7 +2486,15 @@ static int ice_compare_stun_server_requests_to_remove(IceStunServerRequest *requ
}
static void ice_check_list_remove_stun_server_request(IceCheckList *cl, UInt96 *tr_id) {
cl->stun_server_requests = ms_list_remove_custom(cl->stun_server_requests, (MSCompareFunc)ice_find_stun_server_request_transaction, tr_id);
MSList *elem = cl->stun_server_requests;
while (elem != NULL) {
elem = ms_list_find_custom(cl->stun_server_requests, (MSCompareFunc)ice_find_stun_server_request_transaction, tr_id);
if (elem != NULL) {
IceStunServerRequest *request = (IceStunServerRequest *)elem->data;
ice_stun_server_request_free(request);
cl->stun_server_requests = ms_list_remove_link(cl->stun_server_requests, elem);
}
}
}
static IceStunServerRequest * ice_check_list_get_stun_server_request(IceCheckList *cl, UInt96 *tr_id) {
......@@ -2570,7 +2583,7 @@ void ice_handle_stun_packet(IceCheckList *cl, RtpSession *rtp_session, const Ort
ms_warning("ice: STUN message type not handled");
}
ms_free(msg);
ms_stun_message_destroy(msg);
}
......@@ -3609,7 +3622,15 @@ static int ice_find_gathering_stun_server_request(const IceStunServerRequest *re
}
static void ice_remove_gathering_stun_server_requests(IceCheckList *cl) {
cl->stun_server_requests = ms_list_remove_custom(cl->stun_server_requests, (MSCompareFunc)ice_find_gathering_stun_server_request, NULL);
MSList *elem = cl->stun_server_requests;
while (elem != NULL) {
elem = ms_list_find_custom(cl->stun_server_requests, (MSCompareFunc)ice_find_gathering_stun_server_request, NULL);
if (elem != NULL) {
IceStunServerRequest *request = (IceStunServerRequest *)elem->data;
ice_stun_server_request_free(request);
cl->stun_server_requests = ms_list_remove_link(cl->stun_server_requests, elem);
}
}
}
static void ice_check_list_stop_gathering(IceCheckList *cl) {
......
......@@ -554,6 +554,8 @@ void ms_stun_address_to_sockaddr(const MSStunAddress *stun_addr, struct sockaddr
addr_in6->sin6_port = htons(stun_addr->ip.v6.port);
memcpy(addr_in6->sin6_addr.s6_addr, &stun_addr->ip.v6.addr, sizeof(UInt128));
*addrlen = sizeof(struct sockaddr_in6);
} else {
memset(addr, 0, *addrlen);
}
}
......@@ -566,6 +568,8 @@ void ms_sockaddr_to_stun_address(const struct sockaddr *addr, MSStunAddress *stu
stun_addr->family = MS_STUN_ADDR_FAMILY_IPV6;
stun_addr->ip.v6.port = ntohs(((const struct sockaddr_in6 *)addr)->sin6_port);
memcpy(&stun_addr->ip.v6.addr, ((const struct sockaddr_in6 *)addr)->sin6_addr.s6_addr, sizeof(UInt128));
} else {
memset(stun_addr, 0, sizeof(MSStunAddress));
}
}
......@@ -573,19 +577,20 @@ MSStunAddress ms_ip_address_to_stun_address(int ai_family, int socktype, const c
MSStunAddress stun_addr = { 0 };
struct addrinfo *res = bctbx_ip_address_to_addrinfo(ai_family, socktype, hostname, port);
ms_sockaddr_to_stun_address(res->ai_addr, &stun_addr);
bctbx_freeaddrinfo(res);
return stun_addr;
}
void ms_stun_address_to_ip_address(const MSStunAddress *stun_address, char *ip, size_t ip_size, int *port) {
struct sockaddr_storage addr;
socklen_t addrlen = 0;
socklen_t addrlen = sizeof(addr);
ms_stun_address_to_sockaddr(stun_address, (struct sockaddr *)&addr, &addrlen);
bctbx_sockaddr_to_ip_address((struct sockaddr *)&addr, addrlen, ip, ip_size, port);
}
void ms_stun_address_to_printable_ip_address(const MSStunAddress *stun_address, char *printable_ip, size_t printable_ip_size) {
struct sockaddr_storage addr;
socklen_t addrlen = 0;
socklen_t addrlen = sizeof(addr);
ms_stun_address_to_sockaddr(stun_address, (struct sockaddr *)&addr, &addrlen);
bctbx_sockaddr_to_printable_ip_address((struct sockaddr *)&addr, addrlen, printable_ip, printable_ip_size);
}
......@@ -890,9 +895,12 @@ void ms_stun_message_destroy(MSStunMessage *msg) {
memset(msg->password, '\0', strlen(msg->password));
ms_free(msg->password);
}
if (msg->ha1) ms_free(msg->ha1);
if (msg->realm) ms_free(msg->realm);
if (msg->nonce) ms_free(msg->nonce);
if (msg->message_integrity) ms_free(msg->message_integrity);
if (msg->software) ms_free(msg->software);
if (msg->error_code.reason) ms_free(msg->error_code.reason);
if (msg->data) ms_free(msg->data);
ms_free(msg);
}
......@@ -1277,6 +1285,15 @@ MSTurnContext * ms_turn_context_new(MSTurnContextType type, RtpSession *rtp_sess
}
void ms_turn_context_destroy(MSTurnContext *context) {
if (context->realm != NULL) ms_free(context->realm);
if (context->nonce != NULL) ms_free(context->nonce);
if (context->username != NULL) ms_free(context->username);
if (context->password != NULL) {
memset(context->password, '\0', strlen(context->password));
ms_free(context->password);
}
if (context->ha1 != NULL) ms_free(context->ha1);
if (context->endpoint != NULL) context->endpoint->data = NULL;
ms_free(context);
}
......@@ -1361,7 +1378,7 @@ static int ms_turn_rtp_endpoint_recvfrom(RtpTransport *rtptp, mblk_t *msg, int f
MSTurnContext *context = (MSTurnContext *)rtptp->data;
int msgsize = 0;
if (context->rtp_session != NULL) {
if ((context != NULL) && (context->rtp_session != NULL)) {
msgsize = rtp_session_recvfrom(context->rtp_session, context->type == MS_TURN_CONTEXT_TYPE_RTP, msg, flags, from, fromlen);
if ((msgsize >= RTP_FIXED_HEADER_SIZE) && (rtp_get_version(msg) != 2)) {
/* This is not a RTP packet, try to see if it is a TURN ChannelData message */
......@@ -1386,7 +1403,7 @@ static int ms_turn_rtp_endpoint_recvfrom(RtpTransport *rtptp, mblk_t *msg, int f
if (stun_addr != NULL) {
struct sockaddr_storage relay_ss;
struct sockaddr *relay_sa = (struct sockaddr *)&relay_ss;
socklen_t relay_sa_len = 0;
socklen_t relay_sa_len = sizeof(relay_ss);
// TODO: check if permissions have been set for the source address
/* Copy the data of the TURN data indication in the mblk_t so that it contains the unpacked data */
msgsize = ms_stun_message_get_data_length(stun_msg);
......@@ -1421,7 +1438,7 @@ static int ms_turn_rtp_endpoint_recvfrom(RtpTransport *rtptp, mblk_t *msg, int f
static bool_t ms_turn_rtp_endpoint_send_via_turn_server(MSTurnContext *context, const struct sockaddr *from, socklen_t fromlen) {
struct sockaddr_storage relay_ss;
struct sockaddr *relay_sa = (struct sockaddr *)&relay_ss;
socklen_t relay_sa_len = 0;
socklen_t relay_sa_len = sizeof(relay_ss);
ms_stun_address_to_sockaddr(&context->relay_addr, relay_sa, &relay_sa_len);
if (relay_sa->sa_family != from->sa_family) return FALSE;
......@@ -1441,13 +1458,14 @@ static int ms_turn_rtp_endpoint_sendto(RtpTransport *rtptp, mblk_t *msg, int fla
MSStunMessage *stun_msg = NULL;
bool_t rtp_packet = FALSE;
int ret = 0;
mblk_t *new_msg = NULL;
if (context->rtp_session != NULL) {
if ((context != NULL) && (context->rtp_session != NULL)) {
if ((msgdsize(msg) >= RTP_FIXED_HEADER_SIZE) && (rtp_get_version(msg) == 2)) rtp_packet = TRUE;
if ((rtp_packet && context->force_rtp_sending_via_relay) || ms_turn_rtp_endpoint_send_via_turn_server(context, (struct sockaddr *)&msg->net_addr, msg->net_addrlen)) {
if (ms_turn_context_get_state(context) >= MS_TURN_CONTEXT_STATE_CHANNEL_BOUND) {
/* Use a TURN ChannelData message */
mblk_t *new_msg = allocb(4, 0);
new_msg = allocb(4, 0);
*((uint16_t *)new_msg->b_wptr) = htons(ms_turn_context_get_channel_number(context));
new_msg->b_wptr += 2;
*((uint16_t *)new_msg->b_wptr) = htons(msgdsize(msg));
......@@ -1482,12 +1500,16 @@ static int ms_turn_rtp_endpoint_sendto(RtpTransport *rtptp, mblk_t *msg, int fla
ret = rtp_session_sendto(context->rtp_session, context->type == MS_TURN_CONTEXT_TYPE_RTP, msg, flags, to, tolen);
}
if (stun_msg != NULL) ms_stun_message_destroy(stun_msg);
if (new_msg != NULL) {
new_msg->b_cont = NULL;
freemsg(new_msg);
}
return ret;
}
static void ms_turn_rtp_endpoint_close(RtpTransport *rtptp) {
MSTurnContext *context = (MSTurnContext *)rtptp->data;
context->rtp_session = NULL;
if (context != NULL) context->rtp_session = NULL;
}
static void ms_turn_rtp_endpoint_destroy(RtpTransport *rtptp) {
......@@ -1502,5 +1524,6 @@ RtpTransport * ms_turn_context_create_endpoint(MSTurnContext *context) {
rtptp->t_close = ms_turn_rtp_endpoint_close;
rtptp->t_destroy = ms_turn_rtp_endpoint_destroy;
rtptp->data = context;
context->endpoint = rtptp;
return rtptp;
}
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