summaryrefslogtreecommitdiff
path: root/toxav/msi.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxav/msi.c')
-rw-r--r--toxav/msi.c583
1 files changed, 314 insertions, 269 deletions
diff --git a/toxav/msi.c b/toxav/msi.c
index 84ab973f..8f69d942 100644
--- a/toxav/msi.c
+++ b/toxav/msi.c
@@ -1,5 +1,5 @@
1/** toxmsi.c 1/** toxmsi.c
2 * 2 *
3 * Copyright (C) 2013 Tox project All Rights Reserved. 3 * Copyright (C) 2013 Tox project All Rights Reserved.
4 * 4 *
5 * This file is part of Tox. 5 * This file is part of Tox.
@@ -17,7 +17,7 @@
17 * You should have received a copy of the GNU General Public License 17 * You should have received a copy of the GNU General Public License
18 * along with Tox. If not, see <http://www.gnu.org/licenses/>. 18 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
19 * 19 *
20 * 20 *
21 * Report bugs/suggestions at #tox-dev @ freenode.net:6667 21 * Report bugs/suggestions at #tox-dev @ freenode.net:6667
22 */ 22 */
23 23
@@ -46,7 +46,7 @@
46#define TYPE_REQUEST 1 46#define TYPE_REQUEST 1
47#define TYPE_RESPONSE 2 47#define TYPE_RESPONSE 2
48 48
49unsigned char* VERSION_STRING = (unsigned char*)"0.3.1"; 49unsigned char *VERSION_STRING = (unsigned char *)"0.3.1";
50#define VERSION_STRLEN 5 50#define VERSION_STRLEN 5
51 51
52#define CT_AUDIO_HEADER_VALUE "AUDIO" 52#define CT_AUDIO_HEADER_VALUE "AUDIO"
@@ -95,10 +95,10 @@ GENERIC_HEADER ( Nonce )
95/** 95/**
96 * @brief This is the message structure. It contains all of the headers and 96 * @brief This is the message structure. It contains all of the headers and
97 * destination/source of the message stored in friend_id. 97 * destination/source of the message stored in friend_id.
98 * 98 *
99 */ 99 */
100typedef struct _MSIMessage { 100typedef struct _MSIMessage {
101 101
102 MSIHeaderVersion version; 102 MSIHeaderVersion version;
103 MSIHeaderRequest request; 103 MSIHeaderRequest request;
104 MSIHeaderResponse response; 104 MSIHeaderResponse response;
@@ -109,11 +109,11 @@ typedef struct _MSIMessage {
109 MSIHeaderCallId callid; 109 MSIHeaderCallId callid;
110 MSIHeaderCryptoKey cryptokey; 110 MSIHeaderCryptoKey cryptokey;
111 MSIHeaderNonce nonce; 111 MSIHeaderNonce nonce;
112 112
113 struct _MSIMessage* next; 113 struct _MSIMessage *next;
114 114
115 int friend_id; 115 int friend_id;
116 116
117} MSIMessage; 117} MSIMessage;
118 118
119 119
@@ -145,23 +145,24 @@ typedef enum {
145 cancel, 145 cancel,
146 reject, 146 reject,
147 end, 147 end,
148 148
149} MSIRequest; 149} MSIRequest;
150 150
151 151
152/** 152/**
153 * @brief Get string value for request. 153 * @brief Get string value for request.
154 * 154 *
155 * @param request The request. 155 * @param request The request.
156 * @return const uint8_t* The string 156 * @return const uint8_t* The string
157 */ 157 */
158static inline const uint8_t *stringify_request ( MSIRequest request ) { 158static inline const uint8_t *stringify_request ( MSIRequest request )
159 static const uint8_t* strings[] = { 159{
160 ( uint8_t* ) "INVITE", 160 static const uint8_t *strings[] = {
161 ( uint8_t* ) "START", 161 ( uint8_t * ) "INVITE",
162 ( uint8_t* ) "CANCEL", 162 ( uint8_t * ) "START",
163 ( uint8_t* ) "REJECT", 163 ( uint8_t * ) "CANCEL",
164 ( uint8_t* ) "END" 164 ( uint8_t * ) "REJECT",
165 ( uint8_t * ) "END"
165 }; 166 };
166 167
167 return strings[request]; 168 return strings[request];
@@ -179,16 +180,17 @@ typedef enum {
179 180
180/** 181/**
181 * @brief Get string value for response. 182 * @brief Get string value for response.
182 * 183 *
183 * @param response The response. 184 * @param response The response.
184 * @return const uint8_t* The string 185 * @return const uint8_t* The string
185 */ 186 */
186static inline const uint8_t *stringify_response ( MSIResponse response ) { 187static inline const uint8_t *stringify_response ( MSIResponse response )
187 static const uint8_t* strings[] = { 188{
188 ( uint8_t* ) "ringing", 189 static const uint8_t *strings[] = {
189 ( uint8_t* ) "starting", 190 ( uint8_t * ) "ringing",
190 ( uint8_t* ) "ending", 191 ( uint8_t * ) "starting",
191 ( uint8_t* ) "error" 192 ( uint8_t * ) "ending",
193 ( uint8_t * ) "error"
192 }; 194 };
193 195
194 return strings[response]; 196 return strings[response];
@@ -212,78 +214,80 @@ static inline const uint8_t *stringify_response ( MSIResponse response ) {
212 * @brief Parse raw 'data' received from socket into MSIMessage struct. 214 * @brief Parse raw 'data' received from socket into MSIMessage struct.
213 * Every message has to have end value of 'end_byte' or _undefined_ behavior 215 * Every message has to have end value of 'end_byte' or _undefined_ behavior
214 * occures. The best practice is to check the end of the message at the handle_packet. 216 * occures. The best practice is to check the end of the message at the handle_packet.
215 * 217 *
216 * @param msg Container. 218 * @param msg Container.
217 * @param data The data. 219 * @param data The data.
218 * @return int 220 * @return int
219 * @retval -1 Error occured. 221 * @retval -1 Error occured.
220 * @retval 0 Success. 222 * @retval 0 Success.
221 */ 223 */
222int parse_raw_data ( MSIMessage* msg, const uint8_t* data, uint16_t length ) { 224int parse_raw_data ( MSIMessage *msg, const uint8_t *data, uint16_t length )
225{
223 assert ( msg ); 226 assert ( msg );
224 227
225 if ( data[length - 1] ) /* End byte must have value 0 */ 228 if ( data[length - 1] ) /* End byte must have value 0 */
226 return -1; 229 return -1;
227 230
228 const uint8_t* _it = data; 231 const uint8_t *_it = data;
229 232
230 while ( *_it ) {/* until end_byte is hit */ 233 while ( *_it ) {/* until end_byte is hit */
231 234
232 uint16_t itedlen = (_it - data) + 2; 235 uint16_t itedlen = (_it - data) + 2;
233 236
234 if ( *_it == field_byte && itedlen < length ) { 237 if ( *_it == field_byte && itedlen < length ) {
235 238
236 uint16_t _size = ( uint16_t ) * ( _it + 1 ) << 8 | 239 uint16_t _size = ( uint16_t ) * ( _it + 1 ) << 8 |
237 ( uint16_t ) * ( _it + 2 ); 240 ( uint16_t ) * ( _it + 2 );
238 241
239 if ( itedlen + _size > length ) return -1; 242 if ( itedlen + _size > length ) return -1;
240 243
241 _it += 3; /* place it at the field value beginning */ 244 _it += 3; /* place it at the field value beginning */
242 245
243 switch ( _size ) { /* Compare the size of the hardcoded values ( vary fast and convenient ) */ 246 switch ( _size ) { /* Compare the size of the hardcoded values ( vary fast and convenient ) */
244 247
245 case 4: { /* INFO header */ 248 case 4: { /* INFO header */
246 if ON_HEADER ( _it, msg->info, INFO_FIELD, 4 ) 249 if ON_HEADER ( _it, msg->info, INFO_FIELD, 4 )
247 } 250 }
248 break; 251 break;
249
250 case 5: { /* NONCE header */
251 if ON_HEADER ( _it, msg->nonce, NONCE_FIELD, 5 )
252 }
253 break;
254 252
255 case 6: { /* Reason header */ 253 case 5: { /* NONCE header */
256 if ON_HEADER ( _it, msg->reason, REASON_FIELD, 6 ) 254 if ON_HEADER ( _it, msg->nonce, NONCE_FIELD, 5 )
257 } 255 }
258 break; 256 break;
259 257
260 case 7: { /* Version, Request, Call-id headers */ 258 case 6: { /* Reason header */
261 if ON_HEADER ( _it, msg->version, VERSION_FIELD, 7 ) 259 if ON_HEADER ( _it, msg->reason, REASON_FIELD, 6 )
262 else if ON_HEADER ( _it, msg->request, REQUEST_FIELD, 7 ) 260 }
263 else if ON_HEADER ( _it, msg->callid, CALLID_FIELD, 7 ) 261 break;
264 }
265 break;
266 262
267 case 8: { /* Response header */ 263 case 7: { /* Version, Request, Call-id headers */
268 if ON_HEADER ( _it, msg->response, RESPONSE_FIELD, 8 ) 264 if ON_HEADER ( _it, msg->version, VERSION_FIELD, 7 )
269 } 265 else if ON_HEADER ( _it, msg->request, REQUEST_FIELD, 7 )
270 break; 266 else if ON_HEADER ( _it, msg->callid, CALLID_FIELD, 7 )
267 }
268 break;
271 269
272 case 9: { /* Call-type header */ 270 case 8: { /* Response header */
273 if ON_HEADER ( _it, msg->calltype, CALLTYPE_FIELD, 9 ) 271 if ON_HEADER ( _it, msg->response, RESPONSE_FIELD, 8 )
274 } 272 }
275 break; 273 break;
276 274
277 case 10: { /* User-agent, Crypto-key headers */ 275 case 9: { /* Call-type header */
278 if ON_HEADER ( _it, msg->useragent, USERAGENT_FIELD, 10 ) 276 if ON_HEADER ( _it, msg->calltype, CALLTYPE_FIELD, 9 )
279 else if ON_HEADER ( _it, msg->cryptokey, CRYPTOKEY_FIELD, 10 )
280 } 277 }
281 break; 278 break;
279
280 case 10: { /* User-agent, Crypto-key headers */
281 if ON_HEADER ( _it, msg->useragent, USERAGENT_FIELD, 10 )
282 else if ON_HEADER ( _it, msg->cryptokey, CRYPTOKEY_FIELD, 10 )
283 }
284 break;
282 285
283 default: 286 default:
284 return -1; 287 return -1;
285 } 288 }
286 } else return -1; 289 } else return -1;
290
287 /* If it's anything else return failure as the message is invalid */ 291 /* If it's anything else return failure as the message is invalid */
288 292
289 } 293 }
@@ -300,11 +304,12 @@ var.size = t_size;
300 304
301/** 305/**
302 * @brief Speaks for it self. 306 * @brief Speaks for it self.
303 * 307 *
304 * @param msg The message. 308 * @param msg The message.
305 * @return void 309 * @return void
306 */ 310 */
307void free_message ( MSIMessage* msg ) { 311void free_message ( MSIMessage *msg )
312{
308 assert ( msg ); 313 assert ( msg );
309 314
310 free ( msg->calltype.header_value ); 315 free ( msg->calltype.header_value );
@@ -323,29 +328,30 @@ void free_message ( MSIMessage* msg ) {
323 328
324 329
325/** 330/**
326 * @brief Create the message. 331 * @brief Create the message.
327 * 332 *
328 * @param type Request or response. 333 * @param type Request or response.
329 * @param type_id Type of request/response. 334 * @param type_id Type of request/response.
330 * @return MSIMessage* Created message. 335 * @return MSIMessage* Created message.
331 * @retval NULL Error occured. 336 * @retval NULL Error occured.
332 */ 337 */
333MSIMessage* msi_new_message ( uint8_t type, const uint8_t* type_id ) { 338MSIMessage *msi_new_message ( uint8_t type, const uint8_t *type_id )
334 MSIMessage* _retu = calloc ( sizeof ( MSIMessage ), 1 ); 339{
340 MSIMessage *_retu = calloc ( sizeof ( MSIMessage ), 1 );
335 assert ( _retu ); 341 assert ( _retu );
336 342
337 if ( type == TYPE_REQUEST ) { 343 if ( type == TYPE_REQUEST ) {
338 ALLOCATE_HEADER ( _retu->request, type_id, strlen ( (const char*)type_id ) ) 344 ALLOCATE_HEADER ( _retu->request, type_id, strlen ( (const char *)type_id ) )
339 345
340 } else if ( type == TYPE_RESPONSE ) { 346 } else if ( type == TYPE_RESPONSE ) {
341 ALLOCATE_HEADER ( _retu->response, type_id, strlen ( (const char*)type_id ) ) 347 ALLOCATE_HEADER ( _retu->response, type_id, strlen ( (const char *)type_id ) )
342 348
343 } else { 349 } else {
344 free_message ( _retu ); 350 free_message ( _retu );
345 return NULL; 351 return NULL;
346 } 352 }
347 353
348 ALLOCATE_HEADER ( _retu->version, VERSION_STRING, strlen ( (const char*)VERSION_STRING ) ) 354 ALLOCATE_HEADER ( _retu->version, VERSION_STRING, strlen ( (const char *)VERSION_STRING ) )
349 355
350 return _retu; 356 return _retu;
351} 357}
@@ -353,15 +359,16 @@ MSIMessage* msi_new_message ( uint8_t type, const uint8_t* type_id ) {
353 359
354/** 360/**
355 * @brief Parse data from handle_packet. 361 * @brief Parse data from handle_packet.
356 * 362 *
357 * @param data The data. 363 * @param data The data.
358 * @return MSIMessage* Parsed message. 364 * @return MSIMessage* Parsed message.
359 * @retval NULL Error occured. 365 * @retval NULL Error occured.
360 */ 366 */
361MSIMessage* parse_message ( const uint8_t* data, uint16_t length ) { 367MSIMessage *parse_message ( const uint8_t *data, uint16_t length )
368{
362 assert ( data ); 369 assert ( data );
363 370
364 MSIMessage* _retu = calloc ( sizeof ( MSIMessage ), 1 ); 371 MSIMessage *_retu = calloc ( sizeof ( MSIMessage ), 1 );
365 assert ( _retu ); 372 assert ( _retu );
366 373
367 memset ( _retu, 0, sizeof ( MSIMessage ) ); 374 memset ( _retu, 0, sizeof ( MSIMessage ) );
@@ -386,7 +393,7 @@ MSIMessage* parse_message ( const uint8_t* data, uint16_t length ) {
386 393
387/** 394/**
388 * @brief Speaks for it self. 395 * @brief Speaks for it self.
389 * 396 *
390 * @param dest Container. 397 * @param dest Container.
391 * @param header_field Field. 398 * @param header_field Field.
392 * @param header_value Field value. 399 * @param header_value Field value.
@@ -394,45 +401,53 @@ MSIMessage* parse_message ( const uint8_t* data, uint16_t length ) {
394 * @param length Pointer to container length. 401 * @param length Pointer to container length.
395 * @return uint8_t* Iterated container. 402 * @return uint8_t* Iterated container.
396 */ 403 */
397uint8_t* append_header_to_string ( 404uint8_t *append_header_to_string (
398 uint8_t* dest, 405 uint8_t *dest,
399 const uint8_t* header_field, 406 const uint8_t *header_field,
400 const uint8_t* header_value, 407 const uint8_t *header_value,
401 uint16_t value_len, 408 uint16_t value_len,
402 uint16_t* length ) 409 uint16_t *length )
403{ 410{
404 assert ( dest ); 411 assert ( dest );
405 assert ( header_value ); 412 assert ( header_value );
406 assert ( header_field ); 413 assert ( header_field );
407 414
408 const uint8_t* _hvit = header_value; 415 const uint8_t *_hvit = header_value;
409 uint16_t _total = 6 + value_len; /* 6 is known plus header value len + field len*/ 416 uint16_t _total = 6 + value_len; /* 6 is known plus header value len + field len*/
410 417
411 *dest = field_byte; /* Set the first byte */ 418 *dest = field_byte; /* Set the first byte */
412 419
413 uint8_t* _getback_byte = dest + 1; /* remeber the byte we were on */ 420 uint8_t *_getback_byte = dest + 1; /* remeber the byte we were on */
414 dest += 3; /* swith to 4th byte where field value starts */ 421 dest += 3; /* swith to 4th byte where field value starts */
415 422
416 /* Now set the field value and calculate it's length */ 423 /* Now set the field value and calculate it's length */
417 uint16_t _i = 0; 424 uint16_t _i = 0;
425
418 for ( ; header_field[_i]; ++_i ) { 426 for ( ; header_field[_i]; ++_i ) {
419 *dest = header_field[_i]; 427 *dest = header_field[_i];
420 ++dest; 428 ++dest;
421 }; 429 };
430
422 _total += _i; 431 _total += _i;
423 432
424 /* Now set the length of the field byte */ 433 /* Now set the length of the field byte */
425 *_getback_byte = ( uint8_t ) _i >> 8; 434 *_getback_byte = ( uint8_t ) _i >> 8;
435
426 _getback_byte++; 436 _getback_byte++;
437
427 *_getback_byte = ( uint8_t ) _i; 438 *_getback_byte = ( uint8_t ) _i;
428 439
429 /* for value part do it regulary */ 440 /* for value part do it regulary */
430 *dest = value_byte; 441 *dest = value_byte;
442
431 dest++; 443 dest++;
432 444
433 *dest = ( uint8_t ) value_len >> 8; 445 *dest = ( uint8_t ) value_len >> 8;
446
434 dest++; 447 dest++;
448
435 *dest = ( uint8_t ) value_len; 449 *dest = ( uint8_t ) value_len;
450
436 dest++; 451 dest++;
437 452
438 for ( _i = value_len; _i; --_i ) { 453 for ( _i = value_len; _i; --_i ) {
@@ -452,16 +467,17 @@ if ( header.header_value ) { var = append_header_to_string(var, (const uint8_t*)
452 467
453/** 468/**
454 * @brief Convert MSIMessage struct to _sendable_ string. 469 * @brief Convert MSIMessage struct to _sendable_ string.
455 * 470 *
456 * @param msg The message. 471 * @param msg The message.
457 * @param dest Destination. 472 * @param dest Destination.
458 * @return uint16_t It's final size. 473 * @return uint16_t It's final size.
459 */ 474 */
460uint16_t message_to_string ( MSIMessage* msg, uint8_t* dest ) { 475uint16_t message_to_string ( MSIMessage *msg, uint8_t *dest )
476{
461 assert ( msg ); 477 assert ( msg );
462 assert ( dest ); 478 assert ( dest );
463 479
464 uint8_t* _iterated = dest; 480 uint8_t *_iterated = dest;
465 uint16_t _size = 0; 481 uint16_t _size = 0;
466 482
467 CLEAN_ASSIGN ( _size, _iterated, VERSION_FIELD, msg->version ); 483 CLEAN_ASSIGN ( _size, _iterated, VERSION_FIELD, msg->version );
@@ -499,12 +515,13 @@ GENERIC_SETTER_DEFINITION ( nonce )
499 515
500/** 516/**
501 * @brief Generate _random_ alphanumerical string. 517 * @brief Generate _random_ alphanumerical string.
502 * 518 *
503 * @param str Destination. 519 * @param str Destination.
504 * @param size Size of string. 520 * @param size Size of string.
505 * @return void 521 * @return void
506 */ 522 */
507void t_randomstr ( uint8_t* str, size_t size ) { 523void t_randomstr ( uint8_t *str, size_t size )
524{
508 assert ( str ); 525 assert ( str );
509 526
510 static const uint8_t _bytes[] = 527 static const uint8_t _bytes[] =
@@ -535,19 +552,20 @@ typedef enum {
535 552
536/** 553/**
537 * @brief Stringify error code. 554 * @brief Stringify error code.
538 * 555 *
539 * @param error_code The code. 556 * @param error_code The code.
540 * @return const uint8_t* The string. 557 * @return const uint8_t* The string.
541 */ 558 */
542static inline const uint8_t *stringify_error ( MSICallError error_code ) { 559static inline const uint8_t *stringify_error ( MSICallError error_code )
543 static const uint8_t* strings[] = { 560{
544 ( uint8_t* ) "", 561 static const uint8_t *strings[] = {
545 ( uint8_t* ) "Using dead call", 562 ( uint8_t * ) "",
546 ( uint8_t* ) "Call id not set to any call", 563 ( uint8_t * ) "Using dead call",
547 ( uint8_t* ) "Call id not available", 564 ( uint8_t * ) "Call id not set to any call",
548 ( uint8_t* ) "No active call in session", 565 ( uint8_t * ) "Call id not available",
549 ( uint8_t* ) "No Crypto-key set", 566 ( uint8_t * ) "No active call in session",
550 ( uint8_t* ) "Callee busy" 567 ( uint8_t * ) "No Crypto-key set",
568 ( uint8_t * ) "Callee busy"
551 }; 569 };
552 570
553 return strings[error_code]; 571 return strings[error_code];
@@ -556,19 +574,20 @@ static inline const uint8_t *stringify_error ( MSICallError error_code ) {
556 574
557/** 575/**
558 * @brief Convert error_code into string. 576 * @brief Convert error_code into string.
559 * 577 *
560 * @param error_code The code. 578 * @param error_code The code.
561 * @return const uint8_t* The string. 579 * @return const uint8_t* The string.
562 */ 580 */
563static inline const uint8_t *stringify_error_code ( MSICallError error_code ) { 581static inline const uint8_t *stringify_error_code ( MSICallError error_code )
564 static const uint8_t* strings[] = { 582{
565 ( uint8_t* ) "", 583 static const uint8_t *strings[] = {
566 ( uint8_t* ) "1", 584 ( uint8_t * ) "",
567 ( uint8_t* ) "2", 585 ( uint8_t * ) "1",
568 ( uint8_t* ) "3", 586 ( uint8_t * ) "2",
569 ( uint8_t* ) "4", 587 ( uint8_t * ) "3",
570 ( uint8_t* ) "5", 588 ( uint8_t * ) "4",
571 ( uint8_t* ) "6" 589 ( uint8_t * ) "5",
590 ( uint8_t * ) "6"
572 }; 591 };
573 592
574 return strings[error_code]; 593 return strings[error_code];
@@ -577,7 +596,7 @@ static inline const uint8_t *stringify_error_code ( MSICallError error_code ) {
577 596
578/** 597/**
579 * @brief Speaks for it self. 598 * @brief Speaks for it self.
580 * 599 *
581 * @param session Control session. 600 * @param session Control session.
582 * @param msg The message. 601 * @param msg The message.
583 * @param to Where to. 602 * @param to Where to.
@@ -585,73 +604,76 @@ static inline const uint8_t *stringify_error_code ( MSICallError error_code ) {
585 * @retval -1 Error occured. 604 * @retval -1 Error occured.
586 * @retval 0 Success. 605 * @retval 0 Success.
587 */ 606 */
588int send_message ( MSISession* session, MSIMessage* msg, uint32_t to ) 607int send_message ( MSISession *session, MSIMessage *msg, uint32_t to )
589{ 608{
590 msi_msg_set_callid ( msg, session->call->id, CALL_ID_LEN ); 609 msi_msg_set_callid ( msg, session->call->id, CALL_ID_LEN );
591 610
592 uint8_t _msg_string_final [MSI_MAXMSG_SIZE]; 611 uint8_t _msg_string_final [MSI_MAXMSG_SIZE];
593 uint16_t _length = message_to_string ( msg, _msg_string_final ); 612 uint16_t _length = message_to_string ( msg, _msg_string_final );
594 613
595 return m_msi_packet(session->messenger_handle, to, _msg_string_final, _length) ? 0 : -1; 614 return m_msi_packet(session->messenger_handle, to, _msg_string_final, _length) ? 0 : -1;
596} 615}
597 616
598 617
599/** 618/**
600 * @brief Speaks for it self. 619 * @brief Speaks for it self.
601 * 620 *
602 * @param session Control session. 621 * @param session Control session.
603 * @param msg The message. 622 * @param msg The message.
604 * @param peer_id The peer. 623 * @param peer_id The peer.
605 * @return void 624 * @return void
606 */ 625 */
607void flush_peer_type ( MSISession* session, MSIMessage* msg, int peer_id ) { 626void flush_peer_type ( MSISession *session, MSIMessage *msg, int peer_id )
627{
608 if ( msg->calltype.header_value ) { 628 if ( msg->calltype.header_value ) {
609 if ( strcmp ( ( const char* ) msg->calltype.header_value, CT_AUDIO_HEADER_VALUE ) == 0 ) { 629 if ( strcmp ( ( const char * ) msg->calltype.header_value, CT_AUDIO_HEADER_VALUE ) == 0 ) {
610 session->call->type_peer[peer_id] = type_audio; 630 session->call->type_peer[peer_id] = type_audio;
611 631
612 } else if ( strcmp ( ( const char* ) msg->calltype.header_value, CT_VIDEO_HEADER_VALUE ) == 0 ) { 632 } else if ( strcmp ( ( const char * ) msg->calltype.header_value, CT_VIDEO_HEADER_VALUE ) == 0 ) {
613 session->call->type_peer[peer_id] = type_video; 633 session->call->type_peer[peer_id] = type_video;
614 } else {} /* Error */ 634 } else {} /* Error */
615 } else {} /* Error */ 635 } else {} /* Error */
616} 636}
617 637
618void handle_remote_connection_change(Messenger* messenger, int friend_num, uint8_t status, void* session_p) 638void handle_remote_connection_change(Messenger *messenger, int friend_num, uint8_t status, void *session_p)
619{ 639{
620 MSISession* session = session_p; 640 MSISession *session = session_p;
621 641
622 switch ( status ) 642 switch ( status ) {
623 { 643 case 0: { /* Went offline */
624 case 0: /* Went offline */
625 {
626 if ( session->call ) { 644 if ( session->call ) {
627 int i = 0; 645 int i = 0;
628 for ( ; i < session->call->peer_count; i ++ ) 646
647 for ( ; i < session->call->peer_count; i ++ )
629 if ( session->call->peers[i] == friend_num ) { 648 if ( session->call->peers[i] == friend_num ) {
630 msi_stopcall(session); /* Stop the call for now */ 649 msi_stopcall(session); /* Stop the call for now */
631 return; 650 return;
632 } 651 }
633 } 652 }
634 } break; 653 }
635 654 break;
636 default: break; 655
656 default:
657 break;
637 } 658 }
638} 659}
639 660
640/** 661/**
641 * @brief Sends error response to peer. 662 * @brief Sends error response to peer.
642 * 663 *
643 * @param session The session. 664 * @param session The session.
644 * @param errid The id. 665 * @param errid The id.
645 * @param to Where to? 666 * @param to Where to?
646 * @return int 667 * @return int
647 * @retval 0 It's always success. 668 * @retval 0 It's always success.
648 */ 669 */
649int handle_error ( MSISession* session, MSICallError errid, uint32_t to ) { 670int handle_error ( MSISession *session, MSICallError errid, uint32_t to )
650 MSIMessage* _msg_error = msi_new_message ( TYPE_RESPONSE, stringify_response ( error ) ); 671{
672 MSIMessage *_msg_error = msi_new_message ( TYPE_RESPONSE, stringify_response ( error ) );
651 673
652 const uint8_t* _error_code_str = stringify_error_code ( errid ); 674 const uint8_t *_error_code_str = stringify_error_code ( errid );
653 675
654 msi_msg_set_reason ( _msg_error, _error_code_str, strlen ( ( const char* ) _error_code_str ) ); 676 msi_msg_set_reason ( _msg_error, _error_code_str, strlen ( ( const char * ) _error_code_str ) );
655 send_message ( session, _msg_error, to ); 677 send_message ( session, _msg_error, to );
656 free_message ( _msg_error ); 678 free_message ( _msg_error );
657 679
@@ -666,14 +688,15 @@ int handle_error ( MSISession* session, MSICallError errid, uint32_t to ) {
666 688
667/** 689/**
668 * @brief Determine the error if any. 690 * @brief Determine the error if any.
669 * 691 *
670 * @param session Control session. 692 * @param session Control session.
671 * @param msg The message. 693 * @param msg The message.
672 * @return int 694 * @return int
673 * @retval -1 No error. 695 * @retval -1 No error.
674 * @retval 0 Error occured and response sent. 696 * @retval 0 Error occured and response sent.
675 */ 697 */
676int has_call_error ( MSISession* session, MSIMessage* msg ) { 698int has_call_error ( MSISession *session, MSIMessage *msg )
699{
677 if ( !msg->callid.header_value ) { 700 if ( !msg->callid.header_value ) {
678 return handle_error ( session, error_no_callid, msg->friend_id ); 701 return handle_error ( session, error_no_callid, msg->friend_id );
679 702
@@ -691,28 +714,29 @@ int has_call_error ( MSISession* session, MSIMessage* msg ) {
691 714
692/** 715/**
693 * @brief Function called at request timeout. 716 * @brief Function called at request timeout.
694 * 717 *
695 * @param arg Control session 718 * @param arg Control session
696 * @return void* 719 * @return void*
697 */ 720 */
698void* handle_timeout ( void* arg ) 721void *handle_timeout ( void *arg )
699{ 722{
700 /* Send hangup either way */ 723 /* Send hangup either way */
701 MSISession* _session = arg; 724 MSISession *_session = arg;
702 725
703 if ( _session && _session->call ) { 726 if ( _session && _session->call ) {
704 727
705 uint32_t* _peers = _session->call->peers; 728 uint32_t *_peers = _session->call->peers;
706 uint16_t _peer_count = _session->call->peer_count; 729 uint16_t _peer_count = _session->call->peer_count;
707 730
708 731
709 /* Cancel all? */ 732 /* Cancel all? */
710 uint16_t _it = 0; 733 uint16_t _it = 0;
734
711 for ( ; _it < _peer_count; _it++ ) 735 for ( ; _it < _peer_count; _it++ )
712 msi_cancel ( arg, _peers[_it], (const uint8_t*)"Timeout" ); 736 msi_cancel ( arg, _peers[_it], (const uint8_t *)"Timeout" );
713 737
714 } 738 }
715 739
716 ( *callbacks[MSI_OnRequestTimeout] ) ( _session->agent_handler ); 740 ( *callbacks[MSI_OnRequestTimeout] ) ( _session->agent_handler );
717 ( *callbacks[MSI_OnEnding ] ) ( _session->agent_handler ); 741 ( *callbacks[MSI_OnEnding ] ) ( _session->agent_handler );
718 742
@@ -722,38 +746,39 @@ void* handle_timeout ( void* arg )
722 746
723/** 747/**
724 * @brief Add peer to peer list. 748 * @brief Add peer to peer list.
725 * 749 *
726 * @param call What call. 750 * @param call What call.
727 * @param peer_id Its id. 751 * @param peer_id Its id.
728 * @return void 752 * @return void
729 */ 753 */
730void add_peer( MSICall* call, int peer_id ) 754void add_peer( MSICall *call, int peer_id )
731{ 755{
732 if ( !call->peers ) { 756 if ( !call->peers ) {
733 call->peers = calloc(sizeof(int), 1); 757 call->peers = calloc(sizeof(int), 1);
734 call->peer_count = 1; 758 call->peer_count = 1;
735 } else{ 759 } else {
736 call->peer_count ++; 760 call->peer_count ++;
737 call->peers = realloc( call->peers, sizeof(int) * call->peer_count); 761 call->peers = realloc( call->peers, sizeof(int) * call->peer_count);
738 } 762 }
739 763
740 call->peers[call->peer_count - 1] = peer_id; 764 call->peers[call->peer_count - 1] = peer_id;
741} 765}
742 766
743 767
744/** 768/**
745 * @brief Speaks for it self. 769 * @brief Speaks for it self.
746 * 770 *
747 * @param session Control session. 771 * @param session Control session.
748 * @param peers Amount of peers. (Currently it only supports 1) 772 * @param peers Amount of peers. (Currently it only supports 1)
749 * @param ringing_timeout Ringing timeout. 773 * @param ringing_timeout Ringing timeout.
750 * @return MSICall* The created call. 774 * @return MSICall* The created call.
751 */ 775 */
752MSICall* init_call ( MSISession* session, int peers, int ringing_timeout ) { 776MSICall *init_call ( MSISession *session, int peers, int ringing_timeout )
777{
753 assert ( session ); 778 assert ( session );
754 assert ( peers ); 779 assert ( peers );
755 780
756 MSICall* _call = calloc ( sizeof ( MSICall ), 1 ); 781 MSICall *_call = calloc ( sizeof ( MSICall ), 1 );
757 _call->type_peer = calloc ( sizeof ( MSICallType ), peers ); 782 _call->type_peer = calloc ( sizeof ( MSICallType ), peers );
758 783
759 assert ( _call ); 784 assert ( _call );
@@ -779,13 +804,14 @@ MSICall* init_call ( MSISession* session, int peers, int ringing_timeout ) {
779 804
780/** 805/**
781 * @brief Terminate the call. 806 * @brief Terminate the call.
782 * 807 *
783 * @param session Control session. 808 * @param session Control session.
784 * @return int 809 * @return int
785 * @retval -1 Error occured. 810 * @retval -1 Error occured.
786 * @retval 0 Success. 811 * @retval 0 Success.
787 */ 812 */
788int terminate_call ( MSISession* session ) { 813int terminate_call ( MSISession *session )
814{
789 assert ( session ); 815 assert ( session );
790 816
791 if ( !session->call ) 817 if ( !session->call )
@@ -802,7 +828,7 @@ int terminate_call ( MSISession* session ) {
802 /* Get a handle */ 828 /* Get a handle */
803 pthread_mutex_lock ( &session->call->mutex ); 829 pthread_mutex_lock ( &session->call->mutex );
804 830
805 MSICall* _call = session->call; 831 MSICall *_call = session->call;
806 session->call = NULL; 832 session->call = NULL;
807 833
808 free ( _call->type_peer ); 834 free ( _call->type_peer );
@@ -816,19 +842,21 @@ int terminate_call ( MSISession* session ) {
816 pthread_mutex_destroy ( &_call->mutex ); 842 pthread_mutex_destroy ( &_call->mutex );
817 843
818 free ( _call ); 844 free ( _call );
819 845
820 return 0; 846 return 0;
821} 847}
822 848
823 849
824/********** Request handlers **********/ 850/********** Request handlers **********/
825int handle_recv_invite ( MSISession* session, MSIMessage* msg ) { 851int handle_recv_invite ( MSISession *session, MSIMessage *msg )
852{
826 assert ( session ); 853 assert ( session );
827 854
828 if ( session->call ) { 855 if ( session->call ) {
829 handle_error ( session, error_busy, msg->friend_id ); 856 handle_error ( session, error_busy, msg->friend_id );
830 return 0; 857 return 0;
831 } 858 }
859
832 if ( !msg->callid.header_value ) { 860 if ( !msg->callid.header_value ) {
833 handle_error ( session, error_no_callid, msg->friend_id ); 861 handle_error ( session, error_no_callid, msg->friend_id );
834 return 0; 862 return 0;
@@ -837,12 +865,12 @@ int handle_recv_invite ( MSISession* session, MSIMessage* msg ) {
837 session->call = init_call ( session, 1, 0 ); 865 session->call = init_call ( session, 1, 0 );
838 memcpy ( session->call->id, msg->callid.header_value, CALL_ID_LEN ); 866 memcpy ( session->call->id, msg->callid.header_value, CALL_ID_LEN );
839 session->call->state = call_starting; 867 session->call->state = call_starting;
840 868
841 add_peer( session->call, msg->friend_id); 869 add_peer( session->call, msg->friend_id);
842 870
843 flush_peer_type ( session, msg, 0 ); 871 flush_peer_type ( session, msg, 0 );
844 872
845 MSIMessage* _msg_ringing = msi_new_message ( TYPE_RESPONSE, stringify_response ( ringing ) ); 873 MSIMessage *_msg_ringing = msi_new_message ( TYPE_RESPONSE, stringify_response ( ringing ) );
846 send_message ( session, _msg_ringing, msg->friend_id ); 874 send_message ( session, _msg_ringing, msg->friend_id );
847 free_message ( _msg_ringing ); 875 free_message ( _msg_ringing );
848 876
@@ -850,7 +878,8 @@ int handle_recv_invite ( MSISession* session, MSIMessage* msg ) {
850 878
851 return 1; 879 return 1;
852} 880}
853int handle_recv_start ( MSISession* session, MSIMessage* msg ) { 881int handle_recv_start ( MSISession *session, MSIMessage *msg )
882{
854 assert ( session ); 883 assert ( session );
855 884
856 if ( has_call_error ( session, msg ) == 0 ) 885 if ( has_call_error ( session, msg ) == 0 )
@@ -873,14 +902,15 @@ int handle_recv_start ( MSISession* session, MSIMessage* msg ) {
873 902
874 return 1; 903 return 1;
875} 904}
876int handle_recv_reject ( MSISession* session, MSIMessage* msg ) { 905int handle_recv_reject ( MSISession *session, MSIMessage *msg )
906{
877 assert ( session ); 907 assert ( session );
878 908
879 if ( has_call_error ( session, msg ) == 0 ) 909 if ( has_call_error ( session, msg ) == 0 )
880 return 0; 910 return 0;
881 911
882 912
883 MSIMessage* _msg_end = msi_new_message ( TYPE_REQUEST, stringify_request ( end ) ); 913 MSIMessage *_msg_end = msi_new_message ( TYPE_REQUEST, stringify_request ( end ) );
884 send_message ( session, _msg_end, msg->friend_id ); 914 send_message ( session, _msg_end, msg->friend_id );
885 free_message ( _msg_end ); 915 free_message ( _msg_end );
886 916
@@ -890,7 +920,8 @@ int handle_recv_reject ( MSISession* session, MSIMessage* msg ) {
890 920
891 return 1; 921 return 1;
892} 922}
893int handle_recv_cancel ( MSISession* session, MSIMessage* msg ) { 923int handle_recv_cancel ( MSISession *session, MSIMessage *msg )
924{
894 assert ( session ); 925 assert ( session );
895 926
896 if ( has_call_error ( session, msg ) == 0 ) 927 if ( has_call_error ( session, msg ) == 0 )
@@ -903,14 +934,15 @@ int handle_recv_cancel ( MSISession* session, MSIMessage* msg ) {
903 934
904 return 1; 935 return 1;
905} 936}
906int handle_recv_end ( MSISession* session, MSIMessage* msg ) { 937int handle_recv_end ( MSISession *session, MSIMessage *msg )
938{
907 assert ( session ); 939 assert ( session );
908 940
909 if ( has_call_error ( session, msg ) == 0 ) 941 if ( has_call_error ( session, msg ) == 0 )
910 return 0; 942 return 0;
911 943
912 944
913 MSIMessage* _msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) ); 945 MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) );
914 send_message ( session, _msg_ending, msg->friend_id ); 946 send_message ( session, _msg_ending, msg->friend_id );
915 free_message ( _msg_ending ); 947 free_message ( _msg_ending );
916 948
@@ -922,7 +954,8 @@ int handle_recv_end ( MSISession* session, MSIMessage* msg ) {
922} 954}
923 955
924/********** Response handlers **********/ 956/********** Response handlers **********/
925int handle_recv_ringing ( MSISession* session, MSIMessage* msg ) { 957int handle_recv_ringing ( MSISession *session, MSIMessage *msg )
958{
926 assert ( session ); 959 assert ( session );
927 960
928 if ( has_call_error ( session, msg ) == 0 ) 961 if ( has_call_error ( session, msg ) == 0 )
@@ -933,7 +966,8 @@ int handle_recv_ringing ( MSISession* session, MSIMessage* msg ) {
933 966
934 return 1; 967 return 1;
935} 968}
936int handle_recv_starting ( MSISession* session, MSIMessage* msg ) { 969int handle_recv_starting ( MSISession *session, MSIMessage *msg )
970{
937 assert ( session ); 971 assert ( session );
938 972
939 if ( has_call_error ( session, msg ) == 0 ) 973 if ( has_call_error ( session, msg ) == 0 )
@@ -959,7 +993,7 @@ int handle_recv_starting ( MSISession* session, MSIMessage* msg ) {
959 993
960 session->call->state = call_active; 994 session->call->state = call_active;
961 995
962 MSIMessage* _msg_start = msi_new_message ( TYPE_REQUEST, stringify_request ( start ) ); 996 MSIMessage *_msg_start = msi_new_message ( TYPE_REQUEST, stringify_request ( start ) );
963 msi_msg_set_cryptokey ( _msg_start, session->call->key_local, crypto_secretbox_KEYBYTES ); 997 msi_msg_set_cryptokey ( _msg_start, session->call->key_local, crypto_secretbox_KEYBYTES );
964 msi_msg_set_nonce ( _msg_start, session->call->nonce_local, crypto_box_NONCEBYTES ); 998 msi_msg_set_nonce ( _msg_start, session->call->nonce_local, crypto_box_NONCEBYTES );
965 send_message ( session, _msg_start, msg->friend_id ); 999 send_message ( session, _msg_start, msg->friend_id );
@@ -972,7 +1006,8 @@ int handle_recv_starting ( MSISession* session, MSIMessage* msg ) {
972 1006
973 return 1; 1007 return 1;
974} 1008}
975int handle_recv_ending ( MSISession* session, MSIMessage* msg ) { 1009int handle_recv_ending ( MSISession *session, MSIMessage *msg )
1010{
976 assert ( session ); 1011 assert ( session );
977 1012
978 if ( has_call_error ( session, msg ) == 0 ) 1013 if ( has_call_error ( session, msg ) == 0 )
@@ -985,13 +1020,14 @@ int handle_recv_ending ( MSISession* session, MSIMessage* msg ) {
985 1020
986 return 1; 1021 return 1;
987} 1022}
988int handle_recv_error ( MSISession* session, MSIMessage* msg ) { 1023int handle_recv_error ( MSISession *session, MSIMessage *msg )
1024{
989 assert ( session ); 1025 assert ( session );
990 assert ( session->call ); 1026 assert ( session->call );
991 1027
992 /* Handle error accordingly */ 1028 /* Handle error accordingly */
993 if ( msg->reason.header_value ) { 1029 if ( msg->reason.header_value ) {
994 session->last_error_id = atoi ( ( const char* ) msg->reason.header_value ); 1030 session->last_error_id = atoi ( ( const char * ) msg->reason.header_value );
995 session->last_error_str = stringify_error ( session->last_error_id ); 1031 session->last_error_str = stringify_error ( session->last_error_id );
996 } 1032 }
997 1033
@@ -1035,92 +1071,92 @@ int handle_recv_error ( MSISession* session, MSIMessage* msg ) {
1035 * 1071 *
1036 * 1072 *
1037 */ 1073 */
1038void msi_handle_packet ( Messenger* messenger, int source, uint8_t* data, uint16_t length, void* object ) 1074void msi_handle_packet ( Messenger *messenger, int source, uint8_t *data, uint16_t length, void *object )
1039{ 1075{
1040 /* Unused */ 1076 /* Unused */
1041 (void)messenger; 1077 (void)messenger;
1042 1078
1043 MSISession* _session = object; 1079 MSISession *_session = object;
1044 MSIMessage* _msg; 1080 MSIMessage *_msg;
1045 1081
1046 if ( !length ) return; 1082 if ( !length ) return;
1047 1083
1048 _msg = parse_message ( data, length ); 1084 _msg = parse_message ( data, length );
1049 1085
1050 if ( !_msg ) return; 1086 if ( !_msg ) return;
1051 1087
1052 _msg->friend_id = source; 1088 _msg->friend_id = source;
1053 1089
1054 1090
1055 /* Now handle message */ 1091 /* Now handle message */
1056 1092
1057 if ( _msg->request.header_value ) { /* Handle request */ 1093 if ( _msg->request.header_value ) { /* Handle request */
1058 1094
1059 const uint8_t* _request_value = _msg->request.header_value; 1095 const uint8_t *_request_value = _msg->request.header_value;
1060 1096
1061 if ( same ( _request_value, stringify_request ( invite ) ) ) { 1097 if ( same ( _request_value, stringify_request ( invite ) ) ) {
1062 handle_recv_invite ( _session, _msg ); 1098 handle_recv_invite ( _session, _msg );
1063 1099
1064 } else if ( same ( _request_value, stringify_request ( start ) ) ) { 1100 } else if ( same ( _request_value, stringify_request ( start ) ) ) {
1065 handle_recv_start ( _session, _msg ); 1101 handle_recv_start ( _session, _msg );
1066 1102
1067 } else if ( same ( _request_value, stringify_request ( cancel ) ) ) { 1103 } else if ( same ( _request_value, stringify_request ( cancel ) ) ) {
1068 handle_recv_cancel ( _session, _msg ); 1104 handle_recv_cancel ( _session, _msg );
1069 1105
1070 } else if ( same ( _request_value, stringify_request ( reject ) ) ) { 1106 } else if ( same ( _request_value, stringify_request ( reject ) ) ) {
1071 handle_recv_reject ( _session, _msg ); 1107 handle_recv_reject ( _session, _msg );
1072 1108
1073 } else if ( same ( _request_value, stringify_request ( end ) ) ) { 1109 } else if ( same ( _request_value, stringify_request ( end ) ) ) {
1074 handle_recv_end ( _session, _msg ); 1110 handle_recv_end ( _session, _msg );
1075 } 1111 }
1076 1112
1077 else { 1113 else {
1078 free_message ( _msg ); 1114 free_message ( _msg );
1079 return; 1115 return;
1080 } 1116 }
1081 1117
1082 } else if ( _msg->response.header_value ) { /* Handle response */ 1118 } else if ( _msg->response.header_value ) { /* Handle response */
1083 1119
1084 const uint8_t* _response_value = _msg->response.header_value; 1120 const uint8_t *_response_value = _msg->response.header_value;
1085 1121
1086 if ( same ( _response_value, stringify_response ( ringing ) ) ) { 1122 if ( same ( _response_value, stringify_response ( ringing ) ) ) {
1087 handle_recv_ringing ( _session, _msg ); 1123 handle_recv_ringing ( _session, _msg );
1088 1124
1089 } else if ( same ( _response_value, stringify_response ( starting ) ) ) { 1125 } else if ( same ( _response_value, stringify_response ( starting ) ) ) {
1090 handle_recv_starting ( _session, _msg ); 1126 handle_recv_starting ( _session, _msg );
1091 1127
1092 } else if ( same ( _response_value, stringify_response ( ending ) ) ) { 1128 } else if ( same ( _response_value, stringify_response ( ending ) ) ) {
1093 handle_recv_ending ( _session, _msg ); 1129 handle_recv_ending ( _session, _msg );
1094 1130
1095 } else if ( same ( _response_value, stringify_response ( error ) ) ) { 1131 } else if ( same ( _response_value, stringify_response ( error ) ) ) {
1096 handle_recv_error ( _session, _msg ); 1132 handle_recv_error ( _session, _msg );
1097 } else { 1133 } else {
1098 free_message ( _msg ); 1134 free_message ( _msg );
1099 return; 1135 return;
1100 } 1136 }
1101 1137
1102 /* Got response so cancel timer */ 1138 /* Got response so cancel timer */
1103 if ( _session->call ) 1139 if ( _session->call )
1104 event.timer_release ( _session->call->request_timer_id ); 1140 event.timer_release ( _session->call->request_timer_id );
1105 1141
1106 } 1142 }
1107 1143
1108 free_message ( _msg ); 1144 free_message ( _msg );
1109} 1145}
1110 1146
1111 1147
1112/******************************************************************************************************************** 1148/********************************************************************************************************************
1113 * ******************************************************************************************************************* 1149 * *******************************************************************************************************************
1114 ******************************************************************************************************************** 1150 ********************************************************************************************************************
1115 ******************************************************************************************************************** 1151 ********************************************************************************************************************
1116 ******************************************************************************************************************** 1152 ********************************************************************************************************************
1117 * 1153 *
1118 * 1154 *
1119 * 1155 *
1120 * PUBLIC API FUNCTIONS IMPLEMENTATIONS 1156 * PUBLIC API FUNCTIONS IMPLEMENTATIONS
1121 * 1157 *
1122 * 1158 *
1123 * 1159 *
1124 ******************************************************************************************************************** 1160 ********************************************************************************************************************
1125 ******************************************************************************************************************** 1161 ********************************************************************************************************************
1126 ******************************************************************************************************************** 1162 ********************************************************************************************************************
@@ -1136,12 +1172,12 @@ void msi_handle_packet ( Messenger* messenger, int source, uint8_t* data, uint16
1136 1172
1137/** 1173/**
1138 * @brief Callback setter. 1174 * @brief Callback setter.
1139 * 1175 *
1140 * @param callback The callback. 1176 * @param callback The callback.
1141 * @param id The id. 1177 * @param id The id.
1142 * @return void 1178 * @return void
1143 */ 1179 */
1144void msi_register_callback ( MSICallback callback, MSICallbackID id ) 1180void msi_register_callback ( MSICallback callback, MSICallbackID id )
1145{ 1181{
1146 callbacks[id] = callback; 1182 callbacks[id] = callback;
1147} 1183}
@@ -1149,54 +1185,56 @@ void msi_register_callback ( MSICallback callback, MSICallbackID id )
1149 1185
1150/** 1186/**
1151 * @brief Start the control session. 1187 * @brief Start the control session.
1152 * 1188 *
1153 * @param messenger Tox* object. 1189 * @param messenger Tox* object.
1154 * @param user_agent User agent, i.e. 'Venom'; 'QT-gui' 1190 * @param user_agent User agent, i.e. 'Venom'; 'QT-gui'
1155 * @return MSISession* The created session. 1191 * @return MSISession* The created session.
1156 * @retval NULL Error occured. 1192 * @retval NULL Error occured.
1157 */ 1193 */
1158MSISession* msi_init_session ( Messenger* messenger, const uint8_t* ua_name ) { 1194MSISession *msi_init_session ( Messenger *messenger, const uint8_t *ua_name )
1195{
1159 assert ( messenger ); 1196 assert ( messenger );
1160 1197
1161 MSISession* _retu = calloc ( sizeof ( MSISession ), 1 ); 1198 MSISession *_retu = calloc ( sizeof ( MSISession ), 1 );
1162 assert ( _retu ); 1199 assert ( _retu );
1163 1200
1164 _retu->ua_name = ua_name; 1201 _retu->ua_name = ua_name;
1165 _retu->messenger_handle = messenger; 1202 _retu->messenger_handle = messenger;
1166 _retu->agent_handler = NULL; 1203 _retu->agent_handler = NULL;
1167 1204
1168 _retu->call = NULL; 1205 _retu->call = NULL;
1169 1206
1170 _retu->frequ = 10000; /* default value? */ 1207 _retu->frequ = 10000; /* default value? */
1171 _retu->call_timeout = 30000; /* default value? */ 1208 _retu->call_timeout = 30000; /* default value? */
1172 1209
1173 1210
1174 m_callback_msi_packet(messenger, msi_handle_packet, _retu ); 1211 m_callback_msi_packet(messenger, msi_handle_packet, _retu );
1175 1212
1176 /* This is called when remote terminates session */ 1213 /* This is called when remote terminates session */
1177 m_callback_connectionstatus_internal_av(messenger, handle_remote_connection_change, _retu); 1214 m_callback_connectionstatus_internal_av(messenger, handle_remote_connection_change, _retu);
1178 1215
1179 return _retu; 1216 return _retu;
1180} 1217}
1181 1218
1182 1219
1183/** 1220/**
1184 * @brief Terminate control session. 1221 * @brief Terminate control session.
1185 * 1222 *
1186 * @param session The session 1223 * @param session The session
1187 * @return int 1224 * @return int
1188 */ 1225 */
1189int msi_terminate_session ( MSISession* session ) { 1226int msi_terminate_session ( MSISession *session )
1227{
1190 assert ( session ); 1228 assert ( session );
1191 1229
1192 int _status = 0; 1230 int _status = 0;
1193 1231
1194 terminate_call ( session ); 1232 terminate_call ( session );
1195 m_callback_msi_packet((struct Messenger*) session->messenger_handle, NULL, NULL); 1233 m_callback_msi_packet((struct Messenger *) session->messenger_handle, NULL, NULL);
1196 1234
1197 1235
1198 /* TODO: Clean it up more? */ 1236 /* TODO: Clean it up more? */
1199 1237
1200 free ( session ); 1238 free ( session );
1201 return _status; 1239 return _status;
1202} 1240}
@@ -1204,32 +1242,33 @@ int msi_terminate_session ( MSISession* session ) {
1204 1242
1205/** 1243/**
1206 * @brief Send invite request to friend_id. 1244 * @brief Send invite request to friend_id.
1207 * 1245 *
1208 * @param session Control session. 1246 * @param session Control session.
1209 * @param call_type Type of the call. Audio or Video(both audio and video) 1247 * @param call_type Type of the call. Audio or Video(both audio and video)
1210 * @param rngsec Ringing timeout. 1248 * @param rngsec Ringing timeout.
1211 * @param friend_id The friend. 1249 * @param friend_id The friend.
1212 * @return int 1250 * @return int
1213 */ 1251 */
1214int msi_invite ( MSISession* session, MSICallType call_type, uint32_t rngsec, uint32_t friend_id ) { 1252int msi_invite ( MSISession *session, MSICallType call_type, uint32_t rngsec, uint32_t friend_id )
1253{
1215 assert ( session ); 1254 assert ( session );
1216 1255
1217 MSIMessage* _msg_invite = msi_new_message ( TYPE_REQUEST, stringify_request ( invite ) ); 1256 MSIMessage *_msg_invite = msi_new_message ( TYPE_REQUEST, stringify_request ( invite ) );
1218 1257
1219 session->call = init_call ( session, 1, rngsec ); /* Just one for now */ 1258 session->call = init_call ( session, 1, rngsec ); /* Just one for now */
1220 t_randomstr ( session->call->id, CALL_ID_LEN ); 1259 t_randomstr ( session->call->id, CALL_ID_LEN );
1221 1260
1222 add_peer(session->call, friend_id ); 1261 add_peer(session->call, friend_id );
1223 1262
1224 session->call->type_local = call_type; 1263 session->call->type_local = call_type;
1225 /* Do whatever with message */ 1264 /* Do whatever with message */
1226 1265
1227 if ( call_type == type_audio ) { 1266 if ( call_type == type_audio ) {
1228 msi_msg_set_calltype 1267 msi_msg_set_calltype
1229 ( _msg_invite, ( const uint8_t* ) CT_AUDIO_HEADER_VALUE, strlen ( CT_AUDIO_HEADER_VALUE ) ); 1268 ( _msg_invite, ( const uint8_t * ) CT_AUDIO_HEADER_VALUE, strlen ( CT_AUDIO_HEADER_VALUE ) );
1230 } else { 1269 } else {
1231 msi_msg_set_calltype 1270 msi_msg_set_calltype
1232 ( _msg_invite, ( const uint8_t* ) CT_VIDEO_HEADER_VALUE, strlen ( CT_VIDEO_HEADER_VALUE ) ); 1271 ( _msg_invite, ( const uint8_t * ) CT_VIDEO_HEADER_VALUE, strlen ( CT_VIDEO_HEADER_VALUE ) );
1233 } 1272 }
1234 1273
1235 send_message ( session, _msg_invite, friend_id ); 1274 send_message ( session, _msg_invite, friend_id );
@@ -1245,26 +1284,28 @@ int msi_invite ( MSISession* session, MSICallType call_type, uint32_t rngsec, ui
1245 1284
1246/** 1285/**
1247 * @brief Hangup active call. 1286 * @brief Hangup active call.
1248 * 1287 *
1249 * @param session Control session. 1288 * @param session Control session.
1250 * @return int 1289 * @return int
1251 * @retval -1 Error occured. 1290 * @retval -1 Error occured.
1252 * @retval 0 Success. 1291 * @retval 0 Success.
1253 */ 1292 */
1254int msi_hangup ( MSISession* session ) { 1293int msi_hangup ( MSISession *session )
1294{
1255 assert ( session ); 1295 assert ( session );
1256 1296
1257 if ( !session->call || session->call->state != call_active ) 1297 if ( !session->call || session->call->state != call_active )
1258 return -1; 1298 return -1;
1259 1299
1260 MSIMessage* _msg_ending = msi_new_message ( TYPE_REQUEST, stringify_request ( end ) ); 1300 MSIMessage *_msg_ending = msi_new_message ( TYPE_REQUEST, stringify_request ( end ) );
1261 1301
1262 /* hangup for each peer */ 1302 /* hangup for each peer */
1263 int _it = 0; 1303 int _it = 0;
1304
1264 for ( ; _it < session->call->peer_count; _it ++ ) 1305 for ( ; _it < session->call->peer_count; _it ++ )
1265 send_message ( session, _msg_ending, session->call->peers[_it] ); 1306 send_message ( session, _msg_ending, session->call->peers[_it] );
1266 1307
1267 1308
1268 free_message ( _msg_ending ); 1309 free_message ( _msg_ending );
1269 1310
1270 session->call->request_timer_id = event.timer_alloc ( handle_timeout, session, m_deftout ); 1311 session->call->request_timer_id = event.timer_alloc ( handle_timeout, session, m_deftout );
@@ -1275,23 +1316,24 @@ int msi_hangup ( MSISession* session ) {
1275 1316
1276/** 1317/**
1277 * @brief Answer active call request. 1318 * @brief Answer active call request.
1278 * 1319 *
1279 * @param session Control session. 1320 * @param session Control session.
1280 * @param call_type Answer with Audio or Video(both). 1321 * @param call_type Answer with Audio or Video(both).
1281 * @return int 1322 * @return int
1282 */ 1323 */
1283int msi_answer ( MSISession* session, MSICallType call_type ) { 1324int msi_answer ( MSISession *session, MSICallType call_type )
1325{
1284 assert ( session ); 1326 assert ( session );
1285 1327
1286 MSIMessage* _msg_starting = msi_new_message ( TYPE_RESPONSE, stringify_response ( starting ) ); 1328 MSIMessage *_msg_starting = msi_new_message ( TYPE_RESPONSE, stringify_response ( starting ) );
1287 session->call->type_local = call_type; 1329 session->call->type_local = call_type;
1288 1330
1289 if ( call_type == type_audio ) { 1331 if ( call_type == type_audio ) {
1290 msi_msg_set_calltype 1332 msi_msg_set_calltype
1291 ( _msg_starting, ( const uint8_t* ) CT_AUDIO_HEADER_VALUE, strlen ( CT_AUDIO_HEADER_VALUE ) ); 1333 ( _msg_starting, ( const uint8_t * ) CT_AUDIO_HEADER_VALUE, strlen ( CT_AUDIO_HEADER_VALUE ) );
1292 } else { 1334 } else {
1293 msi_msg_set_calltype 1335 msi_msg_set_calltype
1294 ( _msg_starting, ( const uint8_t* ) CT_VIDEO_HEADER_VALUE, strlen ( CT_VIDEO_HEADER_VALUE ) ); 1336 ( _msg_starting, ( const uint8_t * ) CT_VIDEO_HEADER_VALUE, strlen ( CT_VIDEO_HEADER_VALUE ) );
1295 } 1337 }
1296 1338
1297 /* Now set the local encryption key and pass it with STARTING message */ 1339 /* Now set the local encryption key and pass it with STARTING message */
@@ -1316,18 +1358,19 @@ int msi_answer ( MSISession* session, MSICallType call_type ) {
1316 1358
1317/** 1359/**
1318 * @brief Cancel request. 1360 * @brief Cancel request.
1319 * 1361 *
1320 * @param session Control session. 1362 * @param session Control session.
1321 * @param reason Set optional reason header. Pass NULL if none. 1363 * @param reason Set optional reason header. Pass NULL if none.
1322 * @return int 1364 * @return int
1323 */ 1365 */
1324int msi_cancel ( MSISession* session, uint32_t peer, const uint8_t* reason ) { 1366int msi_cancel ( MSISession *session, uint32_t peer, const uint8_t *reason )
1367{
1325 assert ( session ); 1368 assert ( session );
1326 1369
1327 MSIMessage* _msg_cancel = msi_new_message ( TYPE_REQUEST, stringify_request ( cancel ) ); 1370 MSIMessage *_msg_cancel = msi_new_message ( TYPE_REQUEST, stringify_request ( cancel ) );
1328 1371
1329 if ( reason ) msi_msg_set_reason(_msg_cancel, reason, strlen((const char*)reason)); 1372 if ( reason ) msi_msg_set_reason(_msg_cancel, reason, strlen((const char *)reason));
1330 1373
1331 send_message ( session, _msg_cancel, peer ); 1374 send_message ( session, _msg_cancel, peer );
1332 free_message ( _msg_cancel ); 1375 free_message ( _msg_cancel );
1333 1376
@@ -1339,17 +1382,18 @@ int msi_cancel ( MSISession* session, uint32_t peer, const uint8_t* reason ) {
1339 1382
1340/** 1383/**
1341 * @brief Reject request. 1384 * @brief Reject request.
1342 * 1385 *
1343 * @param session Control session. 1386 * @param session Control session.
1344 * @return int 1387 * @return int
1345 */ 1388 */
1346int msi_reject ( MSISession* session, const uint8_t* reason ) { 1389int msi_reject ( MSISession *session, const uint8_t *reason )
1390{
1347 assert ( session ); 1391 assert ( session );
1348 1392
1349 MSIMessage* _msg_reject = msi_new_message ( TYPE_REQUEST, stringify_request ( reject ) ); 1393 MSIMessage *_msg_reject = msi_new_message ( TYPE_REQUEST, stringify_request ( reject ) );
1350 1394
1351 if ( reason ) msi_msg_set_reason(_msg_reject, reason, strlen((const char*)reason) + 1); 1395 if ( reason ) msi_msg_set_reason(_msg_reject, reason, strlen((const char *)reason) + 1);
1352 1396
1353 send_message ( session, _msg_reject, session->call->peers[session->call->peer_count - 1] ); 1397 send_message ( session, _msg_reject, session->call->peers[session->call->peer_count - 1] );
1354 free_message ( _msg_reject ); 1398 free_message ( _msg_reject );
1355 1399
@@ -1361,18 +1405,19 @@ int msi_reject ( MSISession* session, const uint8_t* reason ) {
1361 1405
1362/** 1406/**
1363 * @brief Terminate the current call. 1407 * @brief Terminate the current call.
1364 * 1408 *
1365 * @param session Control session. 1409 * @param session Control session.
1366 * @return int 1410 * @return int
1367 */ 1411 */
1368int msi_stopcall ( MSISession* session ) { 1412int msi_stopcall ( MSISession *session )
1413{
1369 assert ( session ); 1414 assert ( session );
1370 1415
1371 if ( !session->call ) 1416 if ( !session->call )
1372 return -1; 1417 return -1;
1373 1418
1374 /* just terminate it */ 1419 /* just terminate it */
1375 1420
1376 terminate_call ( session ); 1421 terminate_call ( session );
1377 1422
1378 return 0; 1423 return 0;