diff options
author | mannol <eniz_vukovic@hotmail.com> | 2014-05-31 00:29:43 +0200 |
---|---|---|
committer | mannol <eniz_vukovic@hotmail.com> | 2014-05-31 00:29:43 +0200 |
commit | 2ebefb85b7f5ae28db1e8c11196467efd3c914cf (patch) | |
tree | c062506bfc8e5e7ba779d30ece29d6e7f4c05e57 /toxav/msi.c | |
parent | e0ed51b2dc8cf9d14c5978f717bacef001df3aa4 (diff) |
Fixed potential memleaks
Diffstat (limited to 'toxav/msi.c')
-rw-r--r-- | toxav/msi.c | 65 |
1 files changed, 43 insertions, 22 deletions
diff --git a/toxav/msi.c b/toxav/msi.c index e40d16c1..031f1e54 100644 --- a/toxav/msi.c +++ b/toxav/msi.c | |||
@@ -328,7 +328,8 @@ var.size = t_size; } | |||
328 | void free_message ( MSIMessage *msg ) | 328 | void free_message ( MSIMessage *msg ) |
329 | { | 329 | { |
330 | if ( msg == NULL ) { | 330 | if ( msg == NULL ) { |
331 | LOGGER_ERROR("Tried to free empty message"); | 331 | LOGGER_WARNING("Tried to free empty message"); |
332 | return; | ||
332 | } | 333 | } |
333 | 334 | ||
334 | free ( msg->calltype.header_value ); | 335 | free ( msg->calltype.header_value ); |
@@ -818,6 +819,11 @@ MSICall *find_call ( MSISession *session, uint8_t *call_id ) | |||
818 | */ | 819 | */ |
819 | int handle_error ( MSISession *session, MSICall *call, MSICallError errid, uint32_t to ) | 820 | int handle_error ( MSISession *session, MSICall *call, MSICallError errid, uint32_t to ) |
820 | { | 821 | { |
822 | if (!call) { | ||
823 | LOGGER_WARNING("Cannot handle error on 'null' call"); | ||
824 | return -1; | ||
825 | } | ||
826 | |||
821 | LOGGER_DEBUG("Sending error: %d on call: %s", errid, call->id); | 827 | LOGGER_DEBUG("Sending error: %d on call: %s", errid, call->id); |
822 | 828 | ||
823 | MSIMessage *_msg_error = msi_new_message ( TYPE_RESPONSE, stringify_response ( error ) ); | 829 | MSIMessage *_msg_error = msi_new_message ( TYPE_RESPONSE, stringify_response ( error ) ); |
@@ -1041,6 +1047,7 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg ) | |||
1041 | 1047 | ||
1042 | pthread_mutex_lock(&session->mutex); | 1048 | pthread_mutex_lock(&session->mutex); |
1043 | 1049 | ||
1050 | |||
1044 | if ( call ) { | 1051 | if ( call ) { |
1045 | if ( call->peers[0] == msg->friend_id ) { | 1052 | if ( call->peers[0] == msg->friend_id ) { |
1046 | /* The glare case. A calls B when at the same time | 1053 | /* The glare case. A calls B when at the same time |
@@ -1051,7 +1058,18 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg ) | |||
1051 | */ | 1058 | */ |
1052 | 1059 | ||
1053 | if ( call_id_bigger (call->id, msg->callid.header_value) == 1 ) { /* Peer has advantage */ | 1060 | if ( call_id_bigger (call->id, msg->callid.header_value) == 1 ) { /* Peer has advantage */ |
1061 | |||
1062 | /* Terminate call; peer will timeout(call) if call initialization (magically) fails */ | ||
1054 | terminate_call(session, call); | 1063 | terminate_call(session, call); |
1064 | |||
1065 | call = init_call ( session, 1, 0 ); | ||
1066 | |||
1067 | if ( !call ) { | ||
1068 | pthread_mutex_unlock(&session->mutex); | ||
1069 | LOGGER_ERROR("Starting call"); | ||
1070 | return 0; | ||
1071 | } | ||
1072 | |||
1055 | } else { | 1073 | } else { |
1056 | pthread_mutex_unlock(&session->mutex); | 1074 | pthread_mutex_unlock(&session->mutex); |
1057 | return 0; /* Wait for ringing from peer */ | 1075 | return 0; /* Wait for ringing from peer */ |
@@ -1063,6 +1081,15 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg ) | |||
1063 | return 0; | 1081 | return 0; |
1064 | } | 1082 | } |
1065 | } | 1083 | } |
1084 | else { | ||
1085 | call = init_call ( session, 1, 0 ); | ||
1086 | |||
1087 | if ( !call ) { | ||
1088 | pthread_mutex_unlock(&session->mutex); | ||
1089 | LOGGER_ERROR("Starting call"); | ||
1090 | return 0; | ||
1091 | } | ||
1092 | } | ||
1066 | 1093 | ||
1067 | if ( !msg->callid.header_value ) { | 1094 | if ( !msg->callid.header_value ) { |
1068 | handle_error ( session, call, error_no_callid, msg->friend_id ); | 1095 | handle_error ( session, call, error_no_callid, msg->friend_id ); |
@@ -1070,28 +1097,20 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg ) | |||
1070 | return 0; | 1097 | return 0; |
1071 | } | 1098 | } |
1072 | 1099 | ||
1073 | MSICall *new_call = init_call ( session, 1, 0 ); | 1100 | memcpy ( call->id, msg->callid.header_value, CALL_ID_LEN ); |
1101 | call->state = call_starting; | ||
1074 | 1102 | ||
1075 | if ( !new_call ) { | 1103 | add_peer( call, msg->friend_id); |
1076 | handle_error ( session, call, error_busy, msg->friend_id ); | ||
1077 | pthread_mutex_unlock(&session->mutex); | ||
1078 | return 0; | ||
1079 | } | ||
1080 | 1104 | ||
1081 | memcpy ( new_call->id, msg->callid.header_value, CALL_ID_LEN ); | 1105 | flush_peer_type ( call, msg, 0 ); |
1082 | new_call->state = call_starting; | ||
1083 | |||
1084 | add_peer( new_call, msg->friend_id); | ||
1085 | |||
1086 | flush_peer_type ( new_call, msg, 0 ); | ||
1087 | 1106 | ||
1088 | MSIMessage *_msg_ringing = msi_new_message ( TYPE_RESPONSE, stringify_response ( ringing ) ); | 1107 | MSIMessage *_msg_ringing = msi_new_message ( TYPE_RESPONSE, stringify_response ( ringing ) ); |
1089 | send_message ( session, new_call, _msg_ringing, msg->friend_id ); | 1108 | send_message ( session, call, _msg_ringing, msg->friend_id ); |
1090 | free_message ( _msg_ringing ); | 1109 | free_message ( _msg_ringing ); |
1091 | 1110 | ||
1092 | pthread_mutex_unlock(&session->mutex); | 1111 | pthread_mutex_unlock(&session->mutex); |
1093 | 1112 | ||
1094 | invoke_callback(new_call->call_idx, MSI_OnInvite); | 1113 | invoke_callback(call->call_idx, MSI_OnInvite); |
1095 | 1114 | ||
1096 | return 1; | 1115 | return 1; |
1097 | } | 1116 | } |
@@ -1418,6 +1437,10 @@ void msi_handle_packet ( Messenger *messenger, int source, uint8_t *data, uint16 | |||
1418 | goto free_end; | 1437 | goto free_end; |
1419 | } | 1438 | } |
1420 | 1439 | ||
1440 | /* Got response so cancel timer */ | ||
1441 | if ( _call ) | ||
1442 | event.timer_release ( _call->request_timer_id ); | ||
1443 | |||
1421 | uint8_t _response_value[32]; | 1444 | uint8_t _response_value[32]; |
1422 | 1445 | ||
1423 | memcpy(_response_value, _msg->response.header_value, _msg->response.size); | 1446 | memcpy(_response_value, _msg->response.header_value, _msg->response.size); |
@@ -1440,10 +1463,6 @@ void msi_handle_packet ( Messenger *messenger, int source, uint8_t *data, uint16 | |||
1440 | goto free_end; | 1463 | goto free_end; |
1441 | } | 1464 | } |
1442 | 1465 | ||
1443 | /* Got response so cancel timer */ | ||
1444 | if ( _call ) | ||
1445 | event.timer_release ( _call->request_timer_id ); | ||
1446 | |||
1447 | } else { | 1466 | } else { |
1448 | LOGGER_WARNING("Invalid message: no resp nor requ headers"); | 1467 | LOGGER_WARNING("Invalid message: no resp nor requ headers"); |
1449 | } | 1468 | } |
@@ -1568,13 +1587,13 @@ int msi_invite ( MSISession *session, int32_t *call_index, MSICallType call_type | |||
1568 | 1587 | ||
1569 | LOGGER_DEBUG("Session: %p Inviting friend: %u", session, friend_id); | 1588 | LOGGER_DEBUG("Session: %p Inviting friend: %u", session, friend_id); |
1570 | 1589 | ||
1571 | MSIMessage *_msg_invite = msi_new_message ( TYPE_REQUEST, stringify_request ( invite ) ); | ||
1572 | 1590 | ||
1573 | MSICall *_call = init_call ( session, 1, rngsec ); /* Just one peer for now */ | 1591 | MSICall *_call = init_call ( session, 1, rngsec ); /* Just one peer for now */ |
1574 | 1592 | ||
1575 | if ( !_call ) { | 1593 | if ( !_call ) { |
1576 | pthread_mutex_unlock(&session->mutex); | 1594 | pthread_mutex_unlock(&session->mutex); |
1577 | return -1; /* Cannot handle more calls */ | 1595 | LOGGER_ERROR("Cannot handle more calls"); |
1596 | return -1; | ||
1578 | } | 1597 | } |
1579 | 1598 | ||
1580 | *call_index = _call->call_idx; | 1599 | *call_index = _call->call_idx; |
@@ -1584,6 +1603,8 @@ int msi_invite ( MSISession *session, int32_t *call_index, MSICallType call_type | |||
1584 | add_peer(_call, friend_id ); | 1603 | add_peer(_call, friend_id ); |
1585 | 1604 | ||
1586 | _call->type_local = call_type; | 1605 | _call->type_local = call_type; |
1606 | |||
1607 | MSIMessage *_msg_invite = msi_new_message ( TYPE_REQUEST, stringify_request ( invite ) ); | ||
1587 | 1608 | ||
1588 | /* Do whatever with message */ | 1609 | /* Do whatever with message */ |
1589 | if ( call_type == type_audio ) { | 1610 | if ( call_type == type_audio ) { |