summaryrefslogtreecommitdiff
path: root/toxav/msi.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxav/msi.c')
-rw-r--r--toxav/msi.c187
1 files changed, 34 insertions, 153 deletions
diff --git a/toxav/msi.c b/toxav/msi.c
index a5ba9628..072cb6fa 100644
--- a/toxav/msi.c
+++ b/toxav/msi.c
@@ -31,7 +31,6 @@
31#include "event.h" 31#include "event.h"
32 32
33 33
34#include <assert.h>
35#include <unistd.h> 34#include <unistd.h>
36#include <string.h> 35#include <string.h>
37#include <stdlib.h> 36#include <stdlib.h>
@@ -85,8 +84,6 @@ GENERIC_HEADER ( CallType )
85GENERIC_HEADER ( CallId ) 84GENERIC_HEADER ( CallId )
86GENERIC_HEADER ( Info ) 85GENERIC_HEADER ( Info )
87GENERIC_HEADER ( Reason ) 86GENERIC_HEADER ( Reason )
88GENERIC_HEADER ( CryptoKey )
89GENERIC_HEADER ( Nonce )
90 87
91 88
92/** 89/**
@@ -103,8 +100,6 @@ typedef struct _MSIMessage {
103 MSIHeaderInfo info; 100 MSIHeaderInfo info;
104 MSIHeaderReason reason; 101 MSIHeaderReason reason;
105 MSIHeaderCallId callid; 102 MSIHeaderCallId callid;
106 MSIHeaderCryptoKey cryptokey;
107 MSIHeaderNonce nonce;
108 103
109 struct _MSIMessage *next; 104 struct _MSIMessage *next;
110 105
@@ -138,8 +133,6 @@ inline__ void invoke_callback(int32_t call_index, MSICallbackID id)
138#define REASON_FIELD "Reason" 133#define REASON_FIELD "Reason"
139#define CALLTYPE_FIELD "Call-type" 134#define CALLTYPE_FIELD "Call-type"
140#define CALLID_FIELD "Call-id" 135#define CALLID_FIELD "Call-id"
141#define CRYPTOKEY_FIELD "Crypto-key"
142#define NONCE_FIELD "Nonce"
143 136
144/* protocol descriptors */ 137/* protocol descriptors */
145#define end_byte 0x0 138#define end_byte 0x0
@@ -227,7 +220,8 @@ if ( *iterator != value_byte || size_con <= type_size_const) { return -1; } size
227iterator ++; if(size_con <= 3) {return -1;} size_con -= 3; \ 220iterator ++; if(size_con <= 3) {return -1;} size_con -= 3; \
228uint16_t _value_size; memcpy(&_value_size, iterator, sizeof(_value_size)); _value_size = ntohs(_value_size);\ 221uint16_t _value_size; memcpy(&_value_size, iterator, sizeof(_value_size)); _value_size = ntohs(_value_size);\
229if(size_con < _value_size) { return -1; } size_con -= _value_size; \ 222if(size_con < _value_size) { return -1; } size_con -= _value_size; \
230header.header_value = calloc(sizeof(uint8_t), _value_size); \ 223if ( !(header.header_value = calloc(sizeof(uint8_t), _value_size)) ); \
224LOGGER_ERROR("Allocation failed! Program might misbehave!"); \
231header.size = _value_size; \ 225header.size = _value_size; \
232memcpy(header.header_value, iterator + 2, _value_size);\ 226memcpy(header.header_value, iterator + 2, _value_size);\
233iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte */ } 227iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte */ }
@@ -265,11 +259,6 @@ iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte
265 } 259 }
266 break; 260 break;
267 261
268 case 5: { /* NONCE header */
269 if ON_HEADER ( _it, size_max, msg->nonce, NONCE_FIELD, 5 )
270 }
271 break;
272
273 case 6: { /* Reason header */ 262 case 6: { /* Reason header */
274 if ON_HEADER ( _it, size_max, msg->reason, REASON_FIELD, 6 ) 263 if ON_HEADER ( _it, size_max, msg->reason, REASON_FIELD, 6 )
275 } 264 }
@@ -292,11 +281,6 @@ iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte
292 } 281 }
293 break; 282 break;
294 283
295 case 10: { /* Crypto-key headers */
296 if ON_HEADER ( _it, size_max, msg->cryptokey, CRYPTOKEY_FIELD, 10 )
297 }
298 break;
299
300 default: 284 default:
301 LOGGER_ERROR("Unkown field value"); 285 LOGGER_ERROR("Unkown field value");
302 return -1; 286 return -1;
@@ -315,8 +299,8 @@ iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte
315 299
316 300
317#define ALLOCATE_HEADER( var, mheader_value, t_size) \ 301#define ALLOCATE_HEADER( var, mheader_value, t_size) \
318var.header_value = calloc(sizeof *mheader_value, t_size); \ 302if (!(var.header_value = calloc(sizeof *mheader_value, t_size))) \
319if (var.header_value == NULL) { LOGGER_WARNING("Header allocation failed!"); } \ 303{ LOGGER_WARNING("Header allocation failed! Program might misbehave!"); } \
320else { memcpy(var.header_value, mheader_value, t_size); \ 304else { memcpy(var.header_value, mheader_value, t_size); \
321var.size = t_size; } 305var.size = t_size; }
322 306
@@ -339,8 +323,6 @@ void free_message ( MSIMessage *msg )
339 free ( msg->response.header_value ); 323 free ( msg->response.header_value );
340 free ( msg->version.header_value ); 324 free ( msg->version.header_value );
341 free ( msg->info.header_value ); 325 free ( msg->info.header_value );
342 free ( msg->cryptokey.header_value );
343 free ( msg->nonce.header_value );
344 free ( msg->reason.header_value ); 326 free ( msg->reason.header_value );
345 free ( msg->callid.header_value ); 327 free ( msg->callid.header_value );
346 328
@@ -361,7 +343,7 @@ MSIMessage *msi_new_message ( uint8_t type, const uint8_t *type_id )
361 MSIMessage *_retu = calloc ( sizeof ( MSIMessage ), 1 ); 343 MSIMessage *_retu = calloc ( sizeof ( MSIMessage ), 1 );
362 344
363 if ( _retu == NULL ) { 345 if ( _retu == NULL ) {
364 LOGGER_WARNING("Allocation failed!"); 346 LOGGER_WARNING("Allocation failed! Program might misbehave!");
365 return NULL; 347 return NULL;
366 } 348 }
367 349
@@ -399,7 +381,7 @@ MSIMessage *parse_message ( const uint8_t *data, uint16_t length )
399 MSIMessage *_retu = calloc ( sizeof ( MSIMessage ), 1 ); 381 MSIMessage *_retu = calloc ( sizeof ( MSIMessage ), 1 );
400 382
401 if ( _retu == NULL ) { 383 if ( _retu == NULL ) {
402 LOGGER_WARNING("Allocation failed!"); 384 LOGGER_WARNING("Allocation failed! Program might misbehave!");
403 return NULL; 385 return NULL;
404 } 386 }
405 387
@@ -422,47 +404,6 @@ MSIMessage *parse_message ( const uint8_t *data, uint16_t length )
422} 404}
423 405
424 406
425
426/**
427 * @brief Makes clear message presentation
428 *
429 * @param msg Message
430 * @param dest Dest string
431 * @return int
432 */
433int stringify_message(MSIMessage *msg, char *dest)
434{
435// THIS CODE HAS NO EFFECT, AND THE ARGUMENTS ARE NOT MODIFIED
436#if 0
437
438#define HDR_TO_STR(__dest, __hdr) if (__hdr.header_value) {\
439 char nltstr[MSI_MAXMSG_SIZE]; memset(nltstr+__hdr.size, '\0', MSI_MAXMSG_SIZE-__hdr.size); int i = 0; \
440 for ( ; i < __hdr.size; i ++) nltstr[i] = (char)__hdr.header_value[i]; \
441 }
442
443 if ( !msg || !dest )
444 return -1;
445
446 HDR_TO_STR(dest, msg->version);
447 HDR_TO_STR(dest, msg->request);
448 HDR_TO_STR(dest, msg->response);
449 HDR_TO_STR(dest, msg->reason);
450 HDR_TO_STR(dest, msg->callid);
451 HDR_TO_STR(dest, msg->calltype);
452 HDR_TO_STR(dest, msg->cryptokey);
453 HDR_TO_STR(dest, msg->nonce);
454
455// if (msg->version.header_value) {
456// U8_TO_NLTCHAR(msg->version.header_value, msg->version.size, nltstr, MSI_MAXMSG_SIZE);
457// sprintf(dest, "Version: %s\n", nltstr);
458// }
459#endif
460
461 return 0;
462}
463
464
465
466/** 407/**
467 * @brief Speaks for it self. 408 * @brief Speaks for it self.
468 * 409 *
@@ -482,7 +423,7 @@ uint8_t *append_header_to_string (
482{ 423{
483 if ( dest == NULL ) { 424 if ( dest == NULL ) {
484 LOGGER_ERROR("No destination space!"); 425 LOGGER_ERROR("No destination space!");
485 assert(dest); 426 return NULL;
486 } 427 }
487 428
488 if (header_value == NULL) { 429 if (header_value == NULL) {
@@ -577,8 +518,6 @@ uint16_t message_to_send ( MSIMessage *msg, uint8_t *dest )
577 CLEAN_ASSIGN ( _size, _iterated, INFO_FIELD, msg->info ); 518 CLEAN_ASSIGN ( _size, _iterated, INFO_FIELD, msg->info );
578 CLEAN_ASSIGN ( _size, _iterated, CALLID_FIELD, msg->callid ); 519 CLEAN_ASSIGN ( _size, _iterated, CALLID_FIELD, msg->callid );
579 CLEAN_ASSIGN ( _size, _iterated, REASON_FIELD, msg->reason ); 520 CLEAN_ASSIGN ( _size, _iterated, REASON_FIELD, msg->reason );
580 CLEAN_ASSIGN ( _size, _iterated, CRYPTOKEY_FIELD, msg->cryptokey );
581 CLEAN_ASSIGN ( _size, _iterated, NONCE_FIELD, msg->nonce );
582 521
583 *_iterated = end_byte; 522 *_iterated = end_byte;
584 _size ++; 523 _size ++;
@@ -589,7 +528,7 @@ uint16_t message_to_send ( MSIMessage *msg, uint8_t *dest )
589 528
590#define GENERIC_SETTER_DEFINITION(header) \ 529#define GENERIC_SETTER_DEFINITION(header) \
591void msi_msg_set_##header ( MSIMessage* _msg, const uint8_t* header_value, uint16_t _size ) \ 530void msi_msg_set_##header ( MSIMessage* _msg, const uint8_t* header_value, uint16_t _size ) \
592{ assert(_msg); assert(header_value); \ 531{ if ( !_msg || !header_value) { LOGGER_WARNING("No setter values!"); return; } \
593 free(_msg->header.header_value); \ 532 free(_msg->header.header_value); \
594 ALLOCATE_HEADER( _msg->header, header_value, _size )} 533 ALLOCATE_HEADER( _msg->header, header_value, _size )}
595 534
@@ -597,8 +536,6 @@ GENERIC_SETTER_DEFINITION ( calltype )
597GENERIC_SETTER_DEFINITION ( reason ) 536GENERIC_SETTER_DEFINITION ( reason )
598GENERIC_SETTER_DEFINITION ( info ) 537GENERIC_SETTER_DEFINITION ( info )
599GENERIC_SETTER_DEFINITION ( callid ) 538GENERIC_SETTER_DEFINITION ( callid )
600GENERIC_SETTER_DEFINITION ( cryptokey )
601GENERIC_SETTER_DEFINITION ( nonce )
602 539
603 540
604/** 541/**
@@ -608,7 +545,7 @@ GENERIC_SETTER_DEFINITION ( nonce )
608 * @param size Size of string. 545 * @param size Size of string.
609 * @return void 546 * @return void
610 */ 547 */
611void t_randomstr ( uint8_t *str, size_t size ) 548void t_randomstr ( uint8_t *str, uint32_t size )
612{ 549{
613 if (str == NULL) { 550 if (str == NULL) {
614 LOGGER_DEBUG("Empty destination!"); 551 LOGGER_DEBUG("Empty destination!");
@@ -620,7 +557,7 @@ void t_randomstr ( uint8_t *str, size_t size )
620 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 557 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
621 "abcdefghijklmnopqrstuvwxyz"; 558 "abcdefghijklmnopqrstuvwxyz";
622 559
623 size_t _it = 0; 560 uint32_t _it = 0;
624 561
625 for ( ; _it < size; _it++ ) { 562 for ( ; _it < size; _it++ ) {
626 str[_it] = _bytes[ random_int() % 61 ]; 563 str[_it] = _bytes[ random_int() % 61 ];
@@ -708,14 +645,6 @@ int send_message ( MSISession *session, MSICall *call, MSIMessage *msg, uint32_t
708 return -1; 645 return -1;
709 } 646 }
710 647
711 /*
712 LOGGER_SCOPE(
713 char cast[MSI_MAXMSG_SIZE];
714 stringify_message(msg, cast);
715 LOGGER_DEBUG("[Call: %s] [to: %u] Sending message: len: %d\n%s", call->id, to, _length, cast);
716 );*/
717
718
719 if ( m_msi_packet(session->messenger_handle, to, _msg_string_final, _length) ) { 648 if ( m_msi_packet(session->messenger_handle, to, _msg_string_final, _length) ) {
720 LOGGER_DEBUG("Sent message"); 649 LOGGER_DEBUG("Sent message");
721 return 0; 650 return 0;
@@ -854,14 +783,16 @@ int send_error ( MSISession *session, MSICall *call, MSICallError errid, uint32_
854 */ 783 */
855void add_peer( MSICall *call, int peer_id ) 784void add_peer( MSICall *call, int peer_id )
856{ 785{
857 if ( !call->peers ) { 786 uint32_t *peers = !call->peers ? peers = calloc(sizeof(uint32_t), 1) :
858 call->peers = calloc(sizeof(uint32_t), 1); 787 realloc( call->peers, sizeof(uint32_t) * call->peer_count);
859 call->peer_count = 1; 788
860 } else { 789 if (!peers) {
861 call->peer_count ++; 790 LOGGER_WARNING("Allocation failed! Program might misbehave!");
862 call->peers = realloc( call->peers, sizeof(uint32_t) * call->peer_count); 791 return;
863 } 792 }
864 793
794 call->peer_count ++;
795 call->peers = peers;
865 call->peers[call->peer_count - 1] = peer_id; 796 call->peers[call->peer_count - 1] = peer_id;
866 797
867 LOGGER_DEBUG("Added peer: %d", peer_id); 798 LOGGER_DEBUG("Added peer: %d", peer_id);
@@ -888,7 +819,12 @@ MSICall *init_call ( MSISession *session, int peers, int ringing_timeout )
888 819
889 for (; _call_idx < session->max_calls; _call_idx ++) { 820 for (; _call_idx < session->max_calls; _call_idx ++) {
890 if ( !session->calls[_call_idx] ) { 821 if ( !session->calls[_call_idx] ) {
891 session->calls[_call_idx] = calloc ( sizeof ( MSICall ), 1 ); 822
823 if (!(session->calls[_call_idx] = calloc ( sizeof ( MSICall ), 1 ))) {
824 LOGGER_WARNING("Allocation failed! Program might misbehave!");
825 return NULL;
826 }
827
892 break; 828 break;
893 } 829 }
894 } 830 }
@@ -901,16 +837,11 @@ MSICall *init_call ( MSISession *session, int peers, int ringing_timeout )
901 837
902 MSICall *_call = session->calls[_call_idx]; 838 MSICall *_call = session->calls[_call_idx];
903 839
904 if ( _call == NULL ) {
905 LOGGER_WARNING("Allocation failed!");
906 return NULL;
907 }
908
909 _call->call_idx = _call_idx; 840 _call->call_idx = _call_idx;
910 _call->type_peer = calloc ( sizeof ( MSICallType ), peers );
911 841
912 if ( _call->type_peer == NULL ) { 842 if ( !(_call->type_peer = calloc ( sizeof ( MSICallType ), peers )) ) {
913 LOGGER_WARNING("Allocation failed!"); 843 LOGGER_WARNING("Allocation failed! Program might misbehave!");
844 free(_call);
914 return NULL; 845 return NULL;
915 } 846 }
916 847
@@ -921,11 +852,6 @@ MSICall *init_call ( MSISession *session, int peers, int ringing_timeout )
921 _call->request_timer_id = 0; 852 _call->request_timer_id = 0;
922 _call->ringing_timer_id = 0; 853 _call->ringing_timer_id = 0;
923 854
924 _call->key_local = NULL;
925 _call->key_peer = NULL;
926 _call->nonce_local = NULL;
927 _call->nonce_peer = NULL;
928
929 _call->ringing_tout_ms = ringing_timeout; 855 _call->ringing_tout_ms = ringing_timeout;
930 856
931 pthread_mutex_init ( &_call->mutex, NULL ); 857 pthread_mutex_init ( &_call->mutex, NULL );
@@ -966,8 +892,6 @@ int terminate_call ( MSISession *session, MSICall *call )
966 session->calls[call->call_idx] = NULL; 892 session->calls[call->call_idx] = NULL;
967 893
968 free ( call->type_peer ); 894 free ( call->type_peer );
969 free ( call->key_local );
970 free ( call->key_peer );
971 free ( call->peers); 895 free ( call->peers);
972 896
973 /* Release handle */ 897 /* Release handle */
@@ -1103,21 +1027,8 @@ int handle_recv_start ( MSISession *session, MSICall *call, MSIMessage *msg )
1103 1027
1104 pthread_mutex_lock(&session->mutex); 1028 pthread_mutex_lock(&session->mutex);
1105 1029
1106 if ( !msg->cryptokey.header_value ) {
1107 int rc = send_error ( session, call, error_no_crypto_key, msg->friend_id );
1108 terminate_call(session, call);
1109 pthread_mutex_unlock(&session->mutex);
1110 return rc;
1111 }
1112
1113 call->state = call_active; 1030 call->state = call_active;
1114 1031
1115 call->key_peer = calloc ( sizeof ( uint8_t ), crypto_box_KEYBYTES );
1116 memcpy ( call->key_peer, msg->cryptokey.header_value, crypto_box_KEYBYTES );
1117
1118 call->nonce_peer = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES );
1119 memcpy ( call->nonce_peer, msg->nonce.header_value, crypto_box_NONCEBYTES );
1120
1121 flush_peer_type ( call, msg, 0 ); 1032 flush_peer_type ( call, msg, 0 );
1122 1033
1123 pthread_mutex_unlock(&session->mutex); 1034 pthread_mutex_unlock(&session->mutex);
@@ -1230,33 +1141,9 @@ int handle_recv_starting ( MSISession *session, MSICall *call, MSIMessage *msg )
1230 1141
1231 LOGGER_DEBUG("Session: %p Handling 'starting' on call: %s", session, call->id ); 1142 LOGGER_DEBUG("Session: %p Handling 'starting' on call: %s", session, call->id );
1232 1143
1233
1234 if ( !msg->cryptokey.header_value ) {
1235 int rc = send_error ( session, call, error_no_crypto_key, msg->friend_id );
1236 terminate_call(session, call);
1237 pthread_mutex_unlock(&session->mutex);
1238 return rc;
1239 }
1240
1241 /* Generate local key/nonce to send */
1242 call->key_local = calloc ( sizeof ( uint8_t ), crypto_box_KEYBYTES );
1243 new_symmetric_key ( call->key_local );
1244
1245 call->nonce_local = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES );
1246 new_nonce ( call->nonce_local );
1247
1248 /* Save peer key/nonce */
1249 call->key_peer = calloc ( sizeof ( uint8_t ), crypto_box_KEYBYTES );
1250 memcpy ( call->key_peer, msg->cryptokey.header_value, crypto_box_KEYBYTES );
1251
1252 call->nonce_peer = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES );
1253 memcpy ( call->nonce_peer, msg->nonce.header_value, crypto_box_NONCEBYTES );
1254
1255 call->state = call_active; 1144 call->state = call_active;
1256 1145
1257 MSIMessage *_msg_start = msi_new_message ( TYPE_REQUEST, stringify_request ( start ) ); 1146 MSIMessage *_msg_start = msi_new_message ( TYPE_REQUEST, stringify_request ( start ) );
1258 msi_msg_set_cryptokey ( _msg_start, call->key_local, crypto_box_KEYBYTES );
1259 msi_msg_set_nonce ( _msg_start, call->nonce_local, crypto_box_NONCEBYTES );
1260 send_message ( session, call, _msg_start, msg->friend_id ); 1147 send_message ( session, call, _msg_start, msg->friend_id );
1261 free_message ( _msg_start ); 1148 free_message ( _msg_start );
1262 1149
@@ -1491,14 +1378,19 @@ MSISession *msi_init_session ( Messenger *messenger, int32_t max_calls )
1491 MSISession *_retu = calloc ( sizeof ( MSISession ), 1 ); 1378 MSISession *_retu = calloc ( sizeof ( MSISession ), 1 );
1492 1379
1493 if (_retu == NULL) { 1380 if (_retu == NULL) {
1494 LOGGER_ERROR("Allocation failed!"); 1381 LOGGER_ERROR("Allocation failed! Program might misbehave!");
1495 return NULL; 1382 return NULL;
1496 } 1383 }
1497 1384
1498 _retu->messenger_handle = messenger; 1385 _retu->messenger_handle = messenger;
1499 _retu->agent_handler = NULL; 1386 _retu->agent_handler = NULL;
1500 1387
1501 _retu->calls = calloc( sizeof (MSICall *), max_calls ); 1388 if (!(_retu->calls = calloc( sizeof (MSICall *), max_calls ))) {
1389 LOGGER_ERROR("Allocation failed! Program might misbehave!");
1390 free(_retu);
1391 return NULL;
1392 }
1393
1502 _retu->max_calls = max_calls; 1394 _retu->max_calls = max_calls;
1503 1395
1504 _retu->frequ = 10000; /* default value? */ 1396 _retu->frequ = 10000; /* default value? */
@@ -1690,17 +1582,6 @@ int msi_answer ( MSISession *session, int32_t call_index, MSICallType call_type
1690 ( _msg_starting, ( const uint8_t *) CT_VIDEO_HEADER_VALUE, strlen ( CT_VIDEO_HEADER_VALUE ) ); 1582 ( _msg_starting, ( const uint8_t *) CT_VIDEO_HEADER_VALUE, strlen ( CT_VIDEO_HEADER_VALUE ) );
1691 } 1583 }
1692 1584
1693 /* Now set the local encryption key and pass it with STARTING message */
1694
1695 session->calls[call_index]->key_local = calloc ( sizeof ( uint8_t ), crypto_box_KEYBYTES );
1696 new_symmetric_key ( session->calls[call_index]->key_local );
1697
1698 session->calls[call_index]->nonce_local = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES );
1699 new_nonce ( session->calls[call_index]->nonce_local );
1700
1701 msi_msg_set_cryptokey ( _msg_starting, session->calls[call_index]->key_local, crypto_box_KEYBYTES );
1702 msi_msg_set_nonce ( _msg_starting, session->calls[call_index]->nonce_local, crypto_box_NONCEBYTES );
1703
1704 send_message ( session, session->calls[call_index], _msg_starting, 1585 send_message ( session, session->calls[call_index], _msg_starting,
1705 session->calls[call_index]->peers[session->calls[call_index]->peer_count - 1] ); 1586 session->calls[call_index]->peers[session->calls[call_index]->peer_count - 1] );
1706 free_message ( _msg_starting ); 1587 free_message ( _msg_starting );