summaryrefslogtreecommitdiff
path: root/toxav/toxmsi.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxav/toxmsi.c')
-rwxr-xr-xtoxav/toxmsi.c31
1 files changed, 20 insertions, 11 deletions
diff --git a/toxav/toxmsi.c b/toxav/toxmsi.c
index db9ae2d2..d5c35730 100755
--- a/toxav/toxmsi.c
+++ b/toxav/toxmsi.c
@@ -47,7 +47,7 @@
47#define TYPE_REQUEST 1 47#define TYPE_REQUEST 1
48#define TYPE_RESPONSE 2 48#define TYPE_RESPONSE 2
49 49
50#define VERSION_STRING "0.3.1" 50unsigned char* VERSION_STRING = (unsigned char*)"0.3.1";
51#define VERSION_STRLEN 5 51#define VERSION_STRLEN 5
52 52
53#define CT_AUDIO_HEADER_VALUE "AUDIO" 53#define CT_AUDIO_HEADER_VALUE "AUDIO"
@@ -220,18 +220,26 @@ static inline const uint8_t *stringify_response ( MSIResponse response ) {
220 * @retval -1 Error occured. 220 * @retval -1 Error occured.
221 * @retval 0 Success. 221 * @retval 0 Success.
222 */ 222 */
223int parse_raw_data ( MSIMessage* msg, const uint8_t* data ) { 223int parse_raw_data ( MSIMessage* msg, const uint8_t* data, uint16_t length ) {
224 assert ( msg ); 224 assert ( msg );
225 225
226 if ( data[length - 1] ) /* End byte must have value 0 */
227 return -1;
228
226 const uint8_t* _it = data; 229 const uint8_t* _it = data;
227 230
228 while ( *_it ) {/* until end_byte is hit */ 231 while ( *_it ) {/* until end_byte is hit */
229 232
230 if ( *_it == field_byte ) { 233 uint16_t itedlen = (_it - data) + 2;
234
235 if ( *_it == field_byte && itedlen < length ) {
236
231 uint16_t _size = ( uint16_t ) * ( _it + 1 ) << 8 | 237 uint16_t _size = ( uint16_t ) * ( _it + 1 ) << 8 |
232 ( uint16_t ) * ( _it + 2 ); 238 ( uint16_t ) * ( _it + 2 );
233 239
234 _it += 3; /*place it at the field value beginning*/ 240 if ( itedlen + _size > length ) return -1;
241
242 _it += 3; /* place it at the field value beginning */
235 243
236 switch ( _size ) { /* Compare the size of the hardcoded values ( vary fast and convenient ) */ 244 switch ( _size ) { /* Compare the size of the hardcoded values ( vary fast and convenient ) */
237 245
@@ -340,7 +348,7 @@ MSIMessage* msi_new_message ( uint8_t type, const uint8_t* type_id ) {
340 return NULL; 348 return NULL;
341 } 349 }
342 350
343 ALLOCATE_HEADER ( _retu->version, VERSION_STRING, strlen ( VERSION_STRING ) ) 351 ALLOCATE_HEADER ( _retu->version, VERSION_STRING, strlen ( (const char*)VERSION_STRING ) )
344 352
345 return _retu; 353 return _retu;
346} 354}
@@ -353,7 +361,7 @@ MSIMessage* msi_new_message ( uint8_t type, const uint8_t* type_id ) {
353 * @return MSIMessage* Parsed message. 361 * @return MSIMessage* Parsed message.
354 * @retval NULL Error occured. 362 * @retval NULL Error occured.
355 */ 363 */
356MSIMessage* parse_message ( const uint8_t* data ) { 364MSIMessage* parse_message ( const uint8_t* data, uint16_t length ) {
357 assert ( data ); 365 assert ( data );
358 366
359 MSIMessage* _retu = calloc ( sizeof ( MSIMessage ), 1 ); 367 MSIMessage* _retu = calloc ( sizeof ( MSIMessage ), 1 );
@@ -361,7 +369,7 @@ MSIMessage* parse_message ( const uint8_t* data ) {
361 369
362 memset ( _retu, 0, sizeof ( MSIMessage ) ); 370 memset ( _retu, 0, sizeof ( MSIMessage ) );
363 371
364 if ( parse_raw_data ( _retu, data ) == -1 ) { 372 if ( parse_raw_data ( _retu, data, length ) == -1 ) {
365 373
366 free_message ( _retu ); 374 free_message ( _retu );
367 return NULL; 375 return NULL;
@@ -1014,12 +1022,13 @@ void msi_handle_packet ( Messenger* messenger, int source, uint8_t* data, uint16
1014{ 1022{
1015 /* Unused */ 1023 /* Unused */
1016 (void)messenger; 1024 (void)messenger;
1017 (void)&length;
1018 1025
1019 MSISession* _session = object; 1026 MSISession* _session = object;
1020 MSIMessage* _msg; 1027 MSIMessage* _msg;
1021 1028
1022 _msg = parse_message ( data ); 1029 if ( !length ) return;
1030
1031 _msg = parse_message ( data, length );
1023 1032
1024 if ( !_msg ) return; 1033 if ( !_msg ) return;
1025 1034
@@ -1227,7 +1236,7 @@ int msi_invite ( MSISession* session, MSICallType call_type, uint32_t rngsec, ui
1227int msi_hangup ( MSISession* session ) { 1236int msi_hangup ( MSISession* session ) {
1228 assert ( session ); 1237 assert ( session );
1229 1238
1230 if ( !session->call && session->call->state != call_active ) 1239 if ( !session->call || session->call->state != call_active )
1231 return -1; 1240 return -1;
1232 1241
1233 MSIMessage* _msg_ending = msi_new_message ( TYPE_REQUEST, stringify_request ( end ) ); 1242 MSIMessage* _msg_ending = msi_new_message ( TYPE_REQUEST, stringify_request ( end ) );