summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxav/msi.c193
-rw-r--r--toxav/msi.h6
-rw-r--r--toxav/rtp.c226
-rw-r--r--toxav/rtp.h24
-rw-r--r--toxav/toxav.c18
5 files changed, 96 insertions, 371 deletions
diff --git a/toxav/msi.c b/toxav/msi.c
index a5ba9628..136daf93 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,16 +783,18 @@ 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);
868} 799}
869 800
@@ -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 );
diff --git a/toxav/msi.h b/toxav/msi.h
index 156c1895..02432daf 100644
--- a/toxav/msi.h
+++ b/toxav/msi.h
@@ -71,12 +71,6 @@ typedef struct _MSICall { /* Call info structure */
71 71
72 uint8_t id[CALL_ID_LEN]; /* Random value identifying the call */ 72 uint8_t id[CALL_ID_LEN]; /* Random value identifying the call */
73 73
74 uint8_t *key_local; /* The key for encryption */
75 uint8_t *key_peer; /* The key for decryption */
76
77 uint8_t *nonce_local; /* Local nonce */
78 uint8_t *nonce_peer; /* Peer nonce */
79
80 int ringing_tout_ms; /* Ringing timeout in ms */ 74 int ringing_tout_ms; /* Ringing timeout in ms */
81 75
82 int request_timer_id; /* Timer id for outgoing request/action */ 76 int request_timer_id; /* Timer id for outgoing request/action */
diff --git a/toxav/rtp.c b/toxav/rtp.c
index 1b18777f..ca37bf47 100644
--- a/toxav/rtp.c
+++ b/toxav/rtp.c
@@ -27,7 +27,6 @@
27#include "../toxcore/util.h" 27#include "../toxcore/util.h"
28 28
29#include "rtp.h" 29#include "rtp.h"
30#include <assert.h>
31#include <stdlib.h> 30#include <stdlib.h>
32 31
33 32
@@ -50,7 +49,6 @@
50#define GET_SETTING_MARKER(_h) (( _h->marker_payloadt ) >> 7) 49#define GET_SETTING_MARKER(_h) (( _h->marker_payloadt ) >> 7)
51#define GET_SETTING_PAYLOAD(_h) ((_h->marker_payloadt) & 0x7f) 50#define GET_SETTING_PAYLOAD(_h) ((_h->marker_payloadt) & 0x7f)
52 51
53
54/** 52/**
55 * @brief Checks if message came in late. 53 * @brief Checks if message came in late.
56 * 54 *
@@ -70,46 +68,6 @@ inline__ int check_late_message (RTPSession *session, RTPMessage *msg)
70 return ( msg->header->sequnum < session->rsequnum && msg->header->timestamp < session->timestamp ) ? 0 : -1; 68 return ( msg->header->sequnum < session->rsequnum && msg->header->timestamp < session->timestamp ) ? 0 : -1;
71} 69}
72 70
73
74/**
75 * @brief Increases nonce value by 'target'
76 *
77 * @param nonce The nonce
78 * @param target The target
79 * @return void
80 */
81inline__ void increase_nonce(uint8_t *nonce, uint16_t target)
82{
83 uint16_t _nonce_counter;
84
85 uint8_t _reverse_bytes[2];
86 _reverse_bytes[0] = nonce[crypto_box_NONCEBYTES - 1];
87 _reverse_bytes[1] = nonce[crypto_box_NONCEBYTES - 2];
88
89 bytes_to_U16(&_nonce_counter, _reverse_bytes );
90
91 /* Check overflow */
92 if (_nonce_counter > UINT16_MAX - target ) { /* 2 bytes are not long enough */
93 uint8_t _it = 3;
94
95 while ( _it <= crypto_box_NONCEBYTES ) _it += ++nonce[crypto_box_NONCEBYTES - _it] ?
96 crypto_box_NONCEBYTES : 1;
97
98 _nonce_counter = _nonce_counter - (UINT16_MAX - target ); /* Assign the rest of it */
99 } else { /* Increase nonce */
100
101 _nonce_counter += target;
102 }
103
104 /* Assign the last bytes */
105
106 U16_to_bytes( _reverse_bytes, _nonce_counter);
107 nonce [crypto_box_NONCEBYTES - 1] = _reverse_bytes[0];
108 nonce [crypto_box_NONCEBYTES - 2] = _reverse_bytes[1];
109
110}
111
112
113/** 71/**
114 * @brief Speaks for it self. 72 * @brief Speaks for it self.
115 * 73 *
@@ -146,11 +104,17 @@ RTPHeader *extract_header ( const uint8_t *payload, int length )
146 return NULL; 104 return NULL;
147 } 105 }
148 106
149 const uint8_t *_it = payload;
150
151 RTPHeader *_retu = calloc(1, sizeof (RTPHeader)); 107 RTPHeader *_retu = calloc(1, sizeof (RTPHeader));
152 assert(_retu); 108
109 if ( !_retu ) {
110 LOGGER_WARNING("Alloc failed! Program might misbehave!");
111 return NULL;
112 }
113
114 bytes_to_U16(&_retu->sequnum, payload);
153 115
116 const uint8_t *_it = payload + 2;
117
154 _retu->flags = *_it; 118 _retu->flags = *_it;
155 ++_it; 119 ++_it;
156 120
@@ -214,7 +178,11 @@ RTPExtHeader *extract_ext_header ( const uint8_t *payload, uint16_t length )
214 const uint8_t *_it = payload; 178 const uint8_t *_it = payload;
215 179
216 RTPExtHeader *_retu = calloc(1, sizeof (RTPExtHeader)); 180 RTPExtHeader *_retu = calloc(1, sizeof (RTPExtHeader));
217 assert(_retu); 181
182 if ( !_retu ) {
183 LOGGER_WARNING("Alloc failed! Program might misbehave!");
184 return NULL;
185 }
218 186
219 uint16_t _ext_length; 187 uint16_t _ext_length;
220 bytes_to_U16(&_ext_length, _it); 188 bytes_to_U16(&_ext_length, _it);
@@ -231,8 +199,11 @@ RTPExtHeader *extract_ext_header ( const uint8_t *payload, uint16_t length )
231 bytes_to_U16(&_retu->type, _it); 199 bytes_to_U16(&_retu->type, _it);
232 _it += 2; 200 _it += 2;
233 201
234 _retu->table = calloc(_ext_length, sizeof (uint32_t)); 202 if ( !(_retu->table = calloc(_ext_length, sizeof (uint32_t))) ) {
235 assert(_retu->table); 203 LOGGER_WARNING("Alloc failed! Program might misbehave!");
204 free(_retu);
205 return NULL;
206 }
236 207
237 uint16_t _x; 208 uint16_t _x;
238 209
@@ -319,7 +290,11 @@ uint8_t *add_ext_header ( RTPExtHeader *header, uint8_t *payload )
319RTPHeader *build_header ( RTPSession *session ) 290RTPHeader *build_header ( RTPSession *session )
320{ 291{
321 RTPHeader *_retu = calloc ( 1, sizeof (RTPHeader) ); 292 RTPHeader *_retu = calloc ( 1, sizeof (RTPHeader) );
322 assert(_retu); 293
294 if ( !_retu ) {
295 LOGGER_WARNING("Alloc failed! Program might misbehave!");
296 return NULL;
297 }
323 298
324 ADD_FLAG_VERSION ( _retu, session->version ); 299 ADD_FLAG_VERSION ( _retu, session->version );
325 ADD_FLAG_PADDING ( _retu, session->padding ); 300 ADD_FLAG_PADDING ( _retu, session->padding );
@@ -355,7 +330,7 @@ RTPHeader *build_header ( RTPSession *session )
355 * @return RTPMessage* 330 * @return RTPMessage*
356 * @retval NULL Error occurred. 331 * @retval NULL Error occurred.
357 */ 332 */
358RTPMessage *msg_parse ( uint16_t sequnum, const uint8_t *data, int length ) 333RTPMessage *msg_parse ( const uint8_t *data, int length )
359{ 334{
360 RTPMessage *_retu = calloc(1, sizeof (RTPMessage)); 335 RTPMessage *_retu = calloc(1, sizeof (RTPMessage));
361 336
@@ -367,11 +342,9 @@ RTPMessage *msg_parse ( uint16_t sequnum, const uint8_t *data, int length )
367 return NULL; 342 return NULL;
368 } 343 }
369 344
370 _retu->header->sequnum = sequnum; 345 uint16_t _from_pos = _retu->header->length;
346 _retu->length = length - _from_pos;
371 347
372 _retu->length = length - _retu->header->length;
373
374 uint16_t _from_pos = _retu->header->length - 2 /* Since sequ num is excluded */ ;
375 348
376 349
377 if ( GET_FLAG_EXTENSION ( _retu->header ) ) { 350 if ( GET_FLAG_EXTENSION ( _retu->header ) ) {
@@ -418,7 +391,7 @@ int rtp_handle_packet ( void *object, uint8_t *data, uint32_t length )
418 RTPSession *_session = object; 391 RTPSession *_session = object;
419 RTPMessage *_msg; 392 RTPMessage *_msg;
420 393
421 if ( !_session || length < 13 + crypto_box_MACBYTES) { /* 12 is the minimum length for rtp + desc. byte */ 394 if ( !_session || length < 13 ) { /* 12 is the minimum length for rtp + desc. byte */
422 LOGGER_WARNING("No session or invalid length of received buffer!"); 395 LOGGER_WARNING("No session or invalid length of received buffer!");
423 return -1; 396 return -1;
424 } 397 }
@@ -428,55 +401,7 @@ int rtp_handle_packet ( void *object, uint8_t *data, uint32_t length )
428 return -1; 401 return -1;
429 } 402 }
430 403
431 uint8_t _plain[MAX_UDP_PACKET_SIZE]; 404 _msg = msg_parse ( data + 1, length - 1 );
432
433 uint16_t _sequnum;
434 bytes_to_U16(&_sequnum, data + 1);
435
436 /* Clculate the right nonce */
437 uint8_t _calculated[crypto_box_NONCEBYTES];
438 memcpy(_calculated, _session->decrypt_nonce, crypto_box_NONCEBYTES);
439 increase_nonce ( _calculated, _sequnum );
440
441 /* Decrypt message */
442 int _decrypted_length = decrypt_data_symmetric(
443 (uint8_t *)_session->decrypt_key, _calculated, data + 3, length - 3, _plain );
444
445 /* This packet is either not encrypted properly or late
446 */
447 if ( -1 == _decrypted_length ) {
448
449 /* If this is the case, then the packet is most likely late.
450 * Try with old nonce cycle.
451 */
452 if ( _session->rsequnum < _sequnum ) {
453 _decrypted_length = decrypt_data_symmetric(
454 (uint8_t *)_session->decrypt_key, _session->nonce_cycle, data + 3, length - 3, _plain );
455
456 if ( _decrypted_length == -1 ) {
457 LOGGER_WARNING("Packet not ecrypted properly!");
458 return -1; /* This packet is not encrypted properly */
459 }
460
461 /* Otherwise, if decryption is ok with new cycle, set new cycle
462 */
463 } else {
464 increase_nonce ( _calculated, MAX_SEQU_NUM );
465 _decrypted_length = decrypt_data_symmetric(
466 (uint8_t *)_session->decrypt_key, _calculated, data + 3, length - 3, _plain );
467
468 if ( _decrypted_length == -1 ) {
469 LOGGER_WARNING("Error decrypting!");
470 return -1; /* This is just an error */
471 }
472
473 /* A new cycle setting. */
474 memcpy(_session->nonce_cycle, _session->decrypt_nonce, crypto_box_NONCEBYTES);
475 memcpy(_session->decrypt_nonce, _calculated, crypto_box_NONCEBYTES);
476 }
477 }
478
479 _msg = msg_parse ( _sequnum, _plain, _decrypted_length );
480 405
481 if ( !_msg ) { 406 if ( !_msg ) {
482 LOGGER_WARNING("Could not parse message!"); 407 LOGGER_WARNING("Could not parse message!");
@@ -526,22 +451,28 @@ RTPMessage *rtp_new_message ( RTPSession *session, const uint8_t *data, uint32_t
526 451
527 uint8_t *_from_pos; 452 uint8_t *_from_pos;
528 RTPMessage *_retu = calloc(1, sizeof (RTPMessage)); 453 RTPMessage *_retu = calloc(1, sizeof (RTPMessage));
529 assert(_retu); 454
455 if ( !_retu ) {
456 LOGGER_WARNING("Alloc failed! Program might misbehave!");
457 return NULL;
458 }
530 459
531 /* Sets header values and copies the extension header in _retu */ 460 /* Sets header values and copies the extension header in _retu */
532 _retu->header = build_header ( session ); /* It allocates memory and all */ 461 _retu->header = build_header ( session ); /* It allocates memory and all */
533 _retu->ext_header = session->ext_header; 462 _retu->ext_header = session->ext_header;
534 463
535 464
536 uint32_t _total_length = length + _retu->header->length; 465 uint32_t _total_length = length + _retu->header->length + 1;
537 466
467 _retu->data[0] = session->prefix;
468
538 if ( _retu->ext_header ) { 469 if ( _retu->ext_header ) {
539 _total_length += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 ); 470 _total_length += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 );
540 471
541 _from_pos = add_header ( _retu->header, _retu->data ); 472 _from_pos = add_header ( _retu->header, _retu->data + 1 );
542 _from_pos = add_ext_header ( _retu->ext_header, _from_pos + 1 ); 473 _from_pos = add_ext_header ( _retu->ext_header, _from_pos + 1 );
543 } else { 474 } else {
544 _from_pos = add_header ( _retu->header, _retu->data ); 475 _from_pos = add_header ( _retu->header, _retu->data + 1 );
545 } 476 }
546 477
547 /* 478 /*
@@ -679,41 +610,17 @@ int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *dat
679 return -1; 610 return -1;
680 } 611 }
681 612
682 uint8_t _send_data [ MAX_UDP_PACKET_SIZE ]; 613 if ( -1 == send_custom_lossy_packet(messenger, session->dest, msg->data, msg->length) ) {
683
684 _send_data[0] = session->prefix;
685
686 /* Generate the right nonce */
687 uint8_t _calculated[crypto_box_NONCEBYTES];
688 memcpy(_calculated, session->encrypt_nonce, crypto_box_NONCEBYTES);
689 increase_nonce ( _calculated, msg->header->sequnum );
690
691 /* Need to skip 2 bytes that are for sequnum */
692 int encrypted_length = encrypt_data_symmetric( /* TODO: msg->length - 2 (fix this properly)*/
693 (uint8_t *) session->encrypt_key, _calculated, msg->data + 2, msg->length, _send_data + 3 );
694
695
696 _send_data[1] = msg->data[0];
697 _send_data[2] = msg->data[1];
698
699
700 if ( -1 == send_custom_lossy_packet(messenger, session->dest, _send_data, encrypted_length + 3) ) {
701 LOGGER_WARNING("Failed to send full packet! std error: %s", strerror(errno)); 614 LOGGER_WARNING("Failed to send full packet! std error: %s", strerror(errno));
702 rtp_free_msg ( session, msg ); 615 rtp_free_msg ( session, msg );
703 return -1; 616 return -1;
704 } 617 }
705 618
706 619
707 /* Set sequ number */ 620 /* Set sequ number */
708 if ( session->sequnum >= MAX_SEQU_NUM ) { 621 session->sequnum = session->sequnum >= MAX_SEQU_NUM ? 0 : session->sequnum + 1;
709 session->sequnum = 0;
710 memcpy(session->encrypt_nonce, _calculated, crypto_box_NONCEBYTES);
711 } else {
712 session->sequnum++;
713 }
714
715 rtp_free_msg ( session, msg ); 622 rtp_free_msg ( session, msg );
716 623
717 return 0; 624 return 0;
718} 625}
719 626
@@ -752,26 +659,19 @@ void rtp_free_msg ( RTPSession *session, RTPMessage *msg )
752 * @param payload_type Type of payload used to send. You can use values in toxmsi.h::MSICallType 659 * @param payload_type Type of payload used to send. You can use values in toxmsi.h::MSICallType
753 * @param messenger Tox* object. 660 * @param messenger Tox* object.
754 * @param friend_num Friend id. 661 * @param friend_num Friend id.
755 * @param encrypt_key Speaks for it self.
756 * @param decrypt_key Speaks for it self.
757 * @param encrypt_nonce Speaks for it self.
758 * @param decrypt_nonce Speaks for it self.
759 * @return RTPSession* Created control session. 662 * @return RTPSession* Created control session.
760 * @retval NULL Error occurred. 663 * @retval NULL Error occurred.
761 */ 664 */
762RTPSession *rtp_init_session ( int payload_type, 665RTPSession *rtp_init_session ( int payload_type, Messenger *messenger, int friend_num )
763 Messenger *messenger,
764 int friend_num,
765 const uint8_t *encrypt_key,
766 const uint8_t *decrypt_key,
767 const uint8_t *encrypt_nonce,
768 const uint8_t *decrypt_nonce )
769{ 666{
770 RTPSession *_retu = calloc(1, sizeof(RTPSession)); 667 RTPSession *_retu = calloc(1, sizeof(RTPSession));
771 assert(_retu); 668
669 if ( !_retu ) {
670 LOGGER_WARNING("Alloc failed! Program might misbehave!");
671 return NULL;
672 }
772 673
773 if ( -1 == custom_lossy_packet_registerhandler(messenger, friend_num, payload_type, rtp_handle_packet, _retu) || 674 if ( -1 == custom_lossy_packet_registerhandler(messenger, friend_num, payload_type, rtp_handle_packet, _retu)) {
774 !encrypt_key || !decrypt_key || !encrypt_nonce || !decrypt_nonce) {
775 LOGGER_ERROR("Error setting custom register handler for rtp session"); 675 LOGGER_ERROR("Error setting custom register handler for rtp session");
776 free(_retu); 676 free(_retu);
777 return NULL; 677 return NULL;
@@ -794,23 +694,12 @@ RTPSession *rtp_init_session ( int payload_type,
794 694
795 _retu->ext_header = NULL; /* When needed allocate */ 695 _retu->ext_header = NULL; /* When needed allocate */
796 696
797 _retu->encrypt_key = encrypt_key;
798 _retu->decrypt_key = decrypt_key;
799
800 /* Need to allocate new memory */
801 _retu->encrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) );
802 assert(_retu->encrypt_nonce);
803 _retu->decrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) );
804 assert(_retu->decrypt_nonce);
805 _retu->nonce_cycle = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) );
806 assert(_retu->nonce_cycle);
807 697
808 memcpy(_retu->encrypt_nonce, encrypt_nonce, crypto_box_NONCEBYTES); 698 if ( !(_retu->csrc = calloc(1, sizeof (uint32_t))) ) {
809 memcpy(_retu->decrypt_nonce, decrypt_nonce, crypto_box_NONCEBYTES); 699 LOGGER_WARNING("Alloc failed! Program might misbehave!");
810 memcpy(_retu->nonce_cycle , decrypt_nonce, crypto_box_NONCEBYTES); 700 free(_retu);
811 701 return NULL;
812 _retu->csrc = calloc(1, sizeof (uint32_t)); 702 }
813 assert(_retu->csrc);
814 703
815 _retu->csrc[0] = _retu->ssrc; /* Set my ssrc to the list receive */ 704 _retu->csrc[0] = _retu->ssrc; /* Set my ssrc to the list receive */
816 705
@@ -853,9 +742,6 @@ int rtp_terminate_session ( RTPSession *session, Messenger *messenger )
853 742
854 free ( session->ext_header ); 743 free ( session->ext_header );
855 free ( session->csrc ); 744 free ( session->csrc );
856 free ( session->decrypt_nonce );
857 free ( session->encrypt_nonce );
858 free ( session->nonce_cycle );
859 745
860 pthread_mutex_unlock(&session->mutex); 746 pthread_mutex_unlock(&session->mutex);
861 747
@@ -865,4 +751,4 @@ int rtp_terminate_session ( RTPSession *session, Messenger *messenger )
865 free ( session ); 751 free ( session );
866 752
867 return 0; 753 return 0;
868} 754} \ No newline at end of file
diff --git a/toxav/rtp.h b/toxav/rtp.h
index f4f3944d..63c8ce3d 100644
--- a/toxav/rtp.h
+++ b/toxav/rtp.h
@@ -105,18 +105,6 @@ typedef struct _RTPSession {
105 */ 105 */
106 RTPExtHeader *ext_header; 106 RTPExtHeader *ext_header;
107 107
108
109 /* Since these are only references of the
110 * call structure don't allocate or free
111 */
112
113 const uint8_t *encrypt_key;
114 const uint8_t *decrypt_key;
115 uint8_t *encrypt_nonce;
116 uint8_t *decrypt_nonce;
117
118 uint8_t *nonce_cycle;
119
120 RTPMessage *oldest_msg; 108 RTPMessage *oldest_msg;
121 RTPMessage *last_msg; /* tail */ 109 RTPMessage *last_msg; /* tail */
122 110
@@ -192,20 +180,10 @@ void rtp_free_msg ( RTPSession *session, RTPMessage *msg );
192 * @param payload_type Type of payload used to send. You can use values in toxmsi.h::MSICallType 180 * @param payload_type Type of payload used to send. You can use values in toxmsi.h::MSICallType
193 * @param messenger Tox* object. 181 * @param messenger Tox* object.
194 * @param friend_num Friend id. 182 * @param friend_num Friend id.
195 * @param encrypt_key Speaks for it self.
196 * @param decrypt_key Speaks for it self.
197 * @param encrypt_nonce Speaks for it self.
198 * @param decrypt_nonce Speaks for it self.
199 * @return RTPSession* Created control session. 183 * @return RTPSession* Created control session.
200 * @retval NULL Error occurred. 184 * @retval NULL Error occurred.
201 */ 185 */
202RTPSession *rtp_init_session ( int payload_type, 186RTPSession *rtp_init_session ( int payload_type, Messenger *messenger, int friend_num );
203 Messenger *messenger,
204 int friend_num,
205 const uint8_t *encrypt_key,
206 const uint8_t *decrypt_key,
207 const uint8_t *encrypt_nonce,
208 const uint8_t *decrypt_nonce );
209 187
210 188
211/** 189/**
diff --git a/toxav/toxav.c b/toxav/toxav.c
index ab5fe611..ad8e651f 100644
--- a/toxav/toxav.c
+++ b/toxav/toxav.c
@@ -304,14 +304,7 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
304 CallSpecific *call = &av->calls[call_index]; 304 CallSpecific *call = &av->calls[call_index];
305 305
306 call->crtps[audio_index] = 306 call->crtps[audio_index] =
307 rtp_init_session( 307 rtp_init_session(type_audio, av->messenger, av->msi_session->calls[call_index]->peers[0]);
308 type_audio,
309 av->messenger,
310 av->msi_session->calls[call_index]->peers[0],
311 av->msi_session->calls[call_index]->key_peer,
312 av->msi_session->calls[call_index]->key_local,
313 av->msi_session->calls[call_index]->nonce_peer,
314 av->msi_session->calls[call_index]->nonce_local);
315 308
316 309
317 if ( !call->crtps[audio_index] ) { 310 if ( !call->crtps[audio_index] ) {
@@ -322,14 +315,7 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
322 315
323 if ( support_video ) { 316 if ( support_video ) {
324 call->crtps[video_index] = 317 call->crtps[video_index] =
325 rtp_init_session ( 318 rtp_init_session(type_video, av->messenger, av->msi_session->calls[call_index]->peers[0]);
326 type_video,
327 av->messenger,
328 av->msi_session->calls[call_index]->peers[0],
329 av->msi_session->calls[call_index]->key_peer,
330 av->msi_session->calls[call_index]->key_local,
331 av->msi_session->calls[call_index]->nonce_peer,
332 av->msi_session->calls[call_index]->nonce_local);
333 319
334 320
335 if ( !call->crtps[video_index] ) { 321 if ( !call->crtps[video_index] ) {