summaryrefslogtreecommitdiff
path: root/toxcore/friend_connection.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2016-08-31 19:12:19 +0100
committeriphydf <iphydf@users.noreply.github.com>2016-08-31 20:04:16 +0100
commit633da98ae69866efb195e00d9a3a22ace6bada66 (patch)
tree875535f3d2257c4ea5bb97a553b2f1beab4a1590 /toxcore/friend_connection.c
parent6356eb4e4fe407fa7870f2a685d0d08b5c2ec5bb (diff)
Add braces to all if statements.
Diffstat (limited to 'toxcore/friend_connection.c')
-rw-r--r--toxcore/friend_connection.c153
1 files changed, 102 insertions, 51 deletions
diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c
index 858e54b3..31a7d187 100644
--- a/toxcore/friend_connection.c
+++ b/toxcore/friend_connection.c
@@ -33,14 +33,17 @@
33 */ 33 */
34static uint8_t friendconn_id_not_valid(const Friend_Connections *fr_c, int friendcon_id) 34static uint8_t friendconn_id_not_valid(const Friend_Connections *fr_c, int friendcon_id)
35{ 35{
36 if ((unsigned int)friendcon_id >= fr_c->num_cons) 36 if ((unsigned int)friendcon_id >= fr_c->num_cons) {
37 return 1; 37 return 1;
38 }
38 39
39 if (fr_c->conns == NULL) 40 if (fr_c->conns == NULL) {
40 return 1; 41 return 1;
42 }
41 43
42 if (fr_c->conns[friendcon_id].status == FRIENDCONN_STATUS_NONE) 44 if (fr_c->conns[friendcon_id].status == FRIENDCONN_STATUS_NONE) {
43 return 1; 45 return 1;
46 }
44 47
45 return 0; 48 return 0;
46} 49}
@@ -61,8 +64,9 @@ static int realloc_friendconns(Friend_Connections *fr_c, uint32_t num)
61 64
62 Friend_Conn *newgroup_cons = realloc(fr_c->conns, num * sizeof(Friend_Conn)); 65 Friend_Conn *newgroup_cons = realloc(fr_c->conns, num * sizeof(Friend_Conn));
63 66
64 if (newgroup_cons == NULL) 67 if (newgroup_cons == NULL) {
65 return -1; 68 return -1;
69 }
66 70
67 fr_c->conns = newgroup_cons; 71 fr_c->conns = newgroup_cons;
68 return 0; 72 return 0;
@@ -78,8 +82,9 @@ static int create_friend_conn(Friend_Connections *fr_c)
78 uint32_t i; 82 uint32_t i;
79 83
80 for (i = 0; i < fr_c->num_cons; ++i) { 84 for (i = 0; i < fr_c->num_cons; ++i) {
81 if (fr_c->conns[i].status == FRIENDCONN_STATUS_NONE) 85 if (fr_c->conns[i].status == FRIENDCONN_STATUS_NONE) {
82 return i; 86 return i;
87 }
83 } 88 }
84 89
85 int id = -1; 90 int id = -1;
@@ -100,15 +105,17 @@ static int create_friend_conn(Friend_Connections *fr_c)
100 */ 105 */
101static int wipe_friend_conn(Friend_Connections *fr_c, int friendcon_id) 106static int wipe_friend_conn(Friend_Connections *fr_c, int friendcon_id)
102{ 107{
103 if (friendconn_id_not_valid(fr_c, friendcon_id)) 108 if (friendconn_id_not_valid(fr_c, friendcon_id)) {
104 return -1; 109 return -1;
110 }
105 111
106 uint32_t i; 112 uint32_t i;
107 memset(&(fr_c->conns[friendcon_id]), 0 , sizeof(Friend_Conn)); 113 memset(&(fr_c->conns[friendcon_id]), 0 , sizeof(Friend_Conn));
108 114
109 for (i = fr_c->num_cons; i != 0; --i) { 115 for (i = fr_c->num_cons; i != 0; --i) {
110 if (fr_c->conns[i - 1].status != FRIENDCONN_STATUS_NONE) 116 if (fr_c->conns[i - 1].status != FRIENDCONN_STATUS_NONE) {
111 break; 117 break;
118 }
112 } 119 }
113 120
114 if (fr_c->num_cons != i) { 121 if (fr_c->num_cons != i) {
@@ -121,8 +128,9 @@ static int wipe_friend_conn(Friend_Connections *fr_c, int friendcon_id)
121 128
122static Friend_Conn *get_conn(const Friend_Connections *fr_c, int friendcon_id) 129static Friend_Conn *get_conn(const Friend_Connections *fr_c, int friendcon_id)
123{ 130{
124 if (friendconn_id_not_valid(fr_c, friendcon_id)) 131 if (friendconn_id_not_valid(fr_c, friendcon_id)) {
125 return 0; 132 return 0;
133 }
126 134
127 return &fr_c->conns[friendcon_id]; 135 return &fr_c->conns[friendcon_id];
128} 136}
@@ -138,8 +146,9 @@ int getfriend_conn_id_pk(Friend_Connections *fr_c, const uint8_t *real_pk)
138 Friend_Conn *friend_con = get_conn(fr_c, i); 146 Friend_Conn *friend_con = get_conn(fr_c, i);
139 147
140 if (friend_con) { 148 if (friend_con) {
141 if (public_key_cmp(friend_con->real_public_key, real_pk) == 0) 149 if (public_key_cmp(friend_con->real_public_key, real_pk) == 0) {
142 return i; 150 return i;
151 }
143 } 152 }
144 } 153 }
145 154
@@ -155,8 +164,9 @@ int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_
155{ 164{
156 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 165 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);
157 166
158 if (!friend_con) 167 if (!friend_con) {
159 return -1; 168 return -1;
169 }
160 170
161 /* Local ip and same pk means that they are hosting a TCP relay. */ 171 /* Local ip and same pk means that they are hosting a TCP relay. */
162 if (Local_ip(ip_port.ip) && public_key_cmp(friend_con->dht_temp_pk, public_key) == 0) { 172 if (Local_ip(ip_port.ip) && public_key_cmp(friend_con->dht_temp_pk, public_key) == 0) {
@@ -190,8 +200,9 @@ static void connect_to_saved_tcp_relays(Friend_Connections *fr_c, int friendcon_
190{ 200{
191 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 201 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);
192 202
193 if (!friend_con) 203 if (!friend_con) {
194 return; 204 return;
205 }
195 206
196 unsigned int i; 207 unsigned int i;
197 208
@@ -211,8 +222,9 @@ static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id)
211{ 222{
212 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 223 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);
213 224
214 if (!friend_con) 225 if (!friend_con) {
215 return 0; 226 return 0;
227 }
216 228
217 Node_format nodes[MAX_SHARED_RELAYS]; 229 Node_format nodes[MAX_SHARED_RELAYS];
218 uint8_t data[1024]; 230 uint8_t data[1024];
@@ -230,8 +242,9 @@ static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id)
230 242
231 length = pack_nodes(data + 1, sizeof(data) - 1, nodes, n); 243 length = pack_nodes(data + 1, sizeof(data) - 1, nodes, n);
232 244
233 if (length <= 0) 245 if (length <= 0) {
234 return 0; 246 return 0;
247 }
235 248
236 data[0] = PACKET_ID_SHARE_RELAYS; 249 data[0] = PACKET_ID_SHARE_RELAYS;
237 ++length; 250 ++length;
@@ -250,8 +263,9 @@ static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_por
250 Friend_Connections *fr_c = object; 263 Friend_Connections *fr_c = object;
251 Friend_Conn *friend_con = get_conn(fr_c, number); 264 Friend_Conn *friend_con = get_conn(fr_c, number);
252 265
253 if (!friend_con) 266 if (!friend_con) {
254 return -1; 267 return -1;
268 }
255 269
256 if (friend_con->crypt_connection_id != -1) { 270 if (friend_con->crypt_connection_id != -1) {
257 return friend_add_tcp_relay(fr_c, number, ip_port, public_key); 271 return friend_add_tcp_relay(fr_c, number, ip_port, public_key);
@@ -267,8 +281,9 @@ static void dht_ip_callback(void *object, int32_t number, IP_Port ip_port)
267 Friend_Connections *fr_c = object; 281 Friend_Connections *fr_c = object;
268 Friend_Conn *friend_con = get_conn(fr_c, number); 282 Friend_Conn *friend_con = get_conn(fr_c, number);
269 283
270 if (!friend_con) 284 if (!friend_con) {
271 return; 285 return;
286 }
272 287
273 if (friend_con->crypt_connection_id == -1) { 288 if (friend_con->crypt_connection_id == -1) {
274 friend_new_connection(fr_c, number); 289 friend_new_connection(fr_c, number);
@@ -288,8 +303,9 @@ static void change_dht_pk(Friend_Connections *fr_c, int friendcon_id, const uint
288{ 303{
289 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 304 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);
290 305
291 if (!friend_con) 306 if (!friend_con) {
292 return; 307 return;
308 }
293 309
294 friend_con->dht_pk_lastrecv = unix_time(); 310 friend_con->dht_pk_lastrecv = unix_time();
295 311
@@ -311,8 +327,9 @@ static int handle_status(void *object, int number, uint8_t status)
311 Friend_Connections *fr_c = object; 327 Friend_Connections *fr_c = object;
312 Friend_Conn *friend_con = get_conn(fr_c, number); 328 Friend_Conn *friend_con = get_conn(fr_c, number);
313 329
314 if (!friend_con) 330 if (!friend_con) {
315 return -1; 331 return -1;
332 }
316 333
317 _Bool call_cb = 0; 334 _Bool call_cb = 0;
318 335
@@ -338,9 +355,10 @@ static int handle_status(void *object, int number, uint8_t status)
338 unsigned int i; 355 unsigned int i;
339 356
340 for (i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) { 357 for (i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) {
341 if (friend_con->callbacks[i].status_callback) 358 if (friend_con->callbacks[i].status_callback) {
342 friend_con->callbacks[i].status_callback(friend_con->callbacks[i].status_callback_object, 359 friend_con->callbacks[i].status_callback(friend_con->callbacks[i].status_callback_object,
343 friend_con->callbacks[i].status_callback_id, status); 360 friend_con->callbacks[i].status_callback_id, status);
361 }
344 } 362 }
345 } 363 }
346 364
@@ -353,11 +371,13 @@ static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_pub
353 Friend_Connections *fr_c = object; 371 Friend_Connections *fr_c = object;
354 Friend_Conn *friend_con = get_conn(fr_c, number); 372 Friend_Conn *friend_con = get_conn(fr_c, number);
355 373
356 if (!friend_con) 374 if (!friend_con) {
357 return; 375 return;
376 }
358 377
359 if (public_key_cmp(friend_con->dht_temp_pk, dht_public_key) == 0) 378 if (public_key_cmp(friend_con->dht_temp_pk, dht_public_key) == 0) {
360 return; 379 return;
380 }
361 381
362 change_dht_pk(fr_c, number, dht_public_key); 382 change_dht_pk(fr_c, number, dht_public_key);
363 383
@@ -374,18 +394,21 @@ static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_pub
374 394
375static int handle_packet(void *object, int number, uint8_t *data, uint16_t length, void *userdata) 395static int handle_packet(void *object, int number, uint8_t *data, uint16_t length, void *userdata)
376{ 396{
377 if (length == 0) 397 if (length == 0) {
378 return -1; 398 return -1;
399 }
379 400
380 Friend_Connections *fr_c = object; 401 Friend_Connections *fr_c = object;
381 Friend_Conn *friend_con = get_conn(fr_c, number); 402 Friend_Conn *friend_con = get_conn(fr_c, number);
382 403
383 if (!friend_con) 404 if (!friend_con) {
384 return -1; 405 return -1;
406 }
385 407
386 if (data[0] == PACKET_ID_FRIEND_REQUESTS) { 408 if (data[0] == PACKET_ID_FRIEND_REQUESTS) {
387 if (fr_c->fr_request_callback) 409 if (fr_c->fr_request_callback) {
388 fr_c->fr_request_callback(fr_c->fr_request_object, friend_con->real_public_key, data, length); 410 fr_c->fr_request_callback(fr_c->fr_request_object, friend_con->real_public_key, data, length);
411 }
389 412
390 return 0; 413 return 0;
391 } else if (data[0] == PACKET_ID_ALIVE) { 414 } else if (data[0] == PACKET_ID_ALIVE) {
@@ -395,8 +418,9 @@ static int handle_packet(void *object, int number, uint8_t *data, uint16_t lengt
395 Node_format nodes[MAX_SHARED_RELAYS]; 418 Node_format nodes[MAX_SHARED_RELAYS];
396 int n; 419 int n;
397 420
398 if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data + 1, length - 1, 1)) == -1) 421 if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data + 1, length - 1, 1)) == -1) {
399 return -1; 422 return -1;
423 }
400 424
401 int j; 425 int j;
402 426
@@ -410,15 +434,17 @@ static int handle_packet(void *object, int number, uint8_t *data, uint16_t lengt
410 unsigned int i; 434 unsigned int i;
411 435
412 for (i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) { 436 for (i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) {
413 if (friend_con->callbacks[i].data_callback) 437 if (friend_con->callbacks[i].data_callback) {
414 friend_con->callbacks[i].data_callback( 438 friend_con->callbacks[i].data_callback(
415 friend_con->callbacks[i].data_callback_object, 439 friend_con->callbacks[i].data_callback_object,
416 friend_con->callbacks[i].data_callback_id, data, length, userdata); 440 friend_con->callbacks[i].data_callback_id, data, length, userdata);
441 }
417 442
418 friend_con = get_conn(fr_c, number); 443 friend_con = get_conn(fr_c, number);
419 444
420 if (!friend_con) 445 if (!friend_con) {
421 return -1; 446 return -1;
447 }
422 } 448 }
423 449
424 return 0; 450 return 0;
@@ -426,26 +452,30 @@ static int handle_packet(void *object, int number, uint8_t *data, uint16_t lengt
426 452
427static int handle_lossy_packet(void *object, int number, const uint8_t *data, uint16_t length) 453static int handle_lossy_packet(void *object, int number, const uint8_t *data, uint16_t length)
428{ 454{
429 if (length == 0) 455 if (length == 0) {
430 return -1; 456 return -1;
457 }
431 458
432 Friend_Connections *fr_c = object; 459 Friend_Connections *fr_c = object;
433 Friend_Conn *friend_con = get_conn(fr_c, number); 460 Friend_Conn *friend_con = get_conn(fr_c, number);
434 461
435 if (!friend_con) 462 if (!friend_con) {
436 return -1; 463 return -1;
464 }
437 465
438 unsigned int i; 466 unsigned int i;
439 467
440 for (i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) { 468 for (i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) {
441 if (friend_con->callbacks[i].lossy_data_callback) 469 if (friend_con->callbacks[i].lossy_data_callback) {
442 friend_con->callbacks[i].lossy_data_callback(friend_con->callbacks[i].lossy_data_callback_object, 470 friend_con->callbacks[i].lossy_data_callback(friend_con->callbacks[i].lossy_data_callback_object,
443 friend_con->callbacks[i].lossy_data_callback_id, data, length); 471 friend_con->callbacks[i].lossy_data_callback_id, data, length);
472 }
444 473
445 friend_con = get_conn(fr_c, number); 474 friend_con = get_conn(fr_c, number);
446 475
447 if (!friend_con) 476 if (!friend_con) {
448 return -1; 477 return -1;
478 }
449 } 479 }
450 480
451 return 0; 481 return 0;
@@ -459,8 +489,9 @@ static int handle_new_connections(void *object, New_Connection *n_c)
459 489
460 if (friend_con) { 490 if (friend_con) {
461 491
462 if (friend_con->crypt_connection_id != -1) 492 if (friend_con->crypt_connection_id != -1) {
463 return -1; 493 return -1;
494 }
464 495
465 int id = accept_crypto_connection(fr_c->net_crypto, n_c); 496 int id = accept_crypto_connection(fr_c->net_crypto, n_c);
466 497
@@ -495,8 +526,9 @@ static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id)
495{ 526{
496 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 527 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);
497 528
498 if (!friend_con) 529 if (!friend_con) {
499 return -1; 530 return -1;
531 }
500 532
501 if (friend_con->crypt_connection_id != -1) { 533 if (friend_con->crypt_connection_id != -1) {
502 return -1; 534 return -1;
@@ -509,8 +541,9 @@ static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id)
509 541
510 int id = new_crypto_connection(fr_c->net_crypto, friend_con->real_public_key, friend_con->dht_temp_pk); 542 int id = new_crypto_connection(fr_c->net_crypto, friend_con->real_public_key, friend_con->dht_temp_pk);
511 543
512 if (id == -1) 544 if (id == -1) {
513 return -1; 545 return -1;
546 }
514 547
515 friend_con->crypt_connection_id = id; 548 friend_con->crypt_connection_id = id;
516 connection_status_handler(fr_c->net_crypto, id, &handle_status, fr_c, friendcon_id); 549 connection_status_handler(fr_c->net_crypto, id, &handle_status, fr_c, friendcon_id);
@@ -525,8 +558,9 @@ static int send_ping(const Friend_Connections *fr_c, int friendcon_id)
525{ 558{
526 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 559 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);
527 560
528 if (!friend_con) 561 if (!friend_con) {
529 return -1; 562 return -1;
563 }
530 564
531 uint8_t ping = PACKET_ID_ALIVE; 565 uint8_t ping = PACKET_ID_ALIVE;
532 int64_t ret = write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, &ping, sizeof(ping), 0); 566 int64_t ret = write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, &ping, sizeof(ping), 0);
@@ -548,8 +582,9 @@ int friend_connection_lock(Friend_Connections *fr_c, int friendcon_id)
548{ 582{
549 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 583 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);
550 584
551 if (!friend_con) 585 if (!friend_con) {
552 return -1; 586 return -1;
587 }
553 588
554 ++friend_con->lock_count; 589 ++friend_con->lock_count;
555 return 0; 590 return 0;
@@ -563,8 +598,9 @@ unsigned int friend_con_connected(Friend_Connections *fr_c, int friendcon_id)
563{ 598{
564 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 599 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);
565 600
566 if (!friend_con) 601 if (!friend_con) {
567 return 0; 602 return 0;
603 }
568 604
569 return friend_con->status; 605 return friend_con->status;
570} 606}
@@ -578,14 +614,17 @@ int get_friendcon_public_keys(uint8_t *real_pk, uint8_t *dht_temp_pk, Friend_Con
578{ 614{
579 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 615 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);
580 616
581 if (!friend_con) 617 if (!friend_con) {
582 return -1; 618 return -1;
619 }
583 620
584 if (real_pk) 621 if (real_pk) {
585 memcpy(real_pk, friend_con->real_public_key, crypto_box_PUBLICKEYBYTES); 622 memcpy(real_pk, friend_con->real_public_key, crypto_box_PUBLICKEYBYTES);
623 }
586 624
587 if (dht_temp_pk) 625 if (dht_temp_pk) {
588 memcpy(dht_temp_pk, friend_con->dht_temp_pk, crypto_box_PUBLICKEYBYTES); 626 memcpy(dht_temp_pk, friend_con->dht_temp_pk, crypto_box_PUBLICKEYBYTES);
627 }
589 628
590 return 0; 629 return 0;
591} 630}
@@ -611,11 +650,13 @@ int friend_connection_callbacks(Friend_Connections *fr_c, int friendcon_id, unsi
611{ 650{
612 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 651 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);
613 652
614 if (!friend_con) 653 if (!friend_con) {
615 return -1; 654 return -1;
655 }
616 656
617 if (index >= MAX_FRIEND_CONNECTION_CALLBACKS) 657 if (index >= MAX_FRIEND_CONNECTION_CALLBACKS) {
618 return -1; 658 return -1;
659 }
619 660
620 friend_con->callbacks[index].status_callback = status_callback; 661 friend_con->callbacks[index].status_callback = status_callback;
621 friend_con->callbacks[index].data_callback = data_callback; 662 friend_con->callbacks[index].data_callback = data_callback;
@@ -640,8 +681,9 @@ int friend_connection_crypt_connection_id(Friend_Connections *fr_c, int friendco
640{ 681{
641 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 682 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);
642 683
643 if (!friend_con) 684 if (!friend_con) {
644 return -1; 685 return -1;
686 }
645 687
646 return friend_con->crypt_connection_id; 688 return friend_con->crypt_connection_id;
647} 689}
@@ -663,13 +705,15 @@ int new_friend_connection(Friend_Connections *fr_c, const uint8_t *real_public_k
663 705
664 friendcon_id = create_friend_conn(fr_c); 706 friendcon_id = create_friend_conn(fr_c);
665 707
666 if (friendcon_id == -1) 708 if (friendcon_id == -1) {
667 return -1; 709 return -1;
710 }
668 711
669 int32_t onion_friendnum = onion_addfriend(fr_c->onion_c, real_public_key); 712 int32_t onion_friendnum = onion_addfriend(fr_c->onion_c, real_public_key);
670 713
671 if (onion_friendnum == -1) 714 if (onion_friendnum == -1) {
672 return -1; 715 return -1;
716 }
673 717
674 Friend_Conn *friend_con = &fr_c->conns[friendcon_id]; 718 Friend_Conn *friend_con = &fr_c->conns[friendcon_id];
675 719
@@ -693,8 +737,9 @@ int kill_friend_connection(Friend_Connections *fr_c, int friendcon_id)
693{ 737{
694 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 738 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);
695 739
696 if (!friend_con) 740 if (!friend_con) {
697 return -1; 741 return -1;
742 }
698 743
699 if (friend_con->lock_count) { 744 if (friend_con->lock_count) {
700 --friend_con->lock_count; 745 --friend_con->lock_count;
@@ -733,13 +778,15 @@ void set_friend_request_callback(Friend_Connections *fr_c, int (*fr_request_call
733int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint32_t nospam_num, const uint8_t *data, 778int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint32_t nospam_num, const uint8_t *data,
734 uint16_t length) 779 uint16_t length)
735{ 780{
736 if (1 + sizeof(nospam_num) + length > ONION_CLIENT_MAX_DATA_SIZE || length == 0) 781 if (1 + sizeof(nospam_num) + length > ONION_CLIENT_MAX_DATA_SIZE || length == 0) {
737 return -1; 782 return -1;
783 }
738 784
739 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 785 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);
740 786
741 if (!friend_con) 787 if (!friend_con) {
742 return -1; 788 return -1;
789 }
743 790
744 uint8_t packet[1 + sizeof(nospam_num) + length]; 791 uint8_t packet[1 + sizeof(nospam_num) + length];
745 memcpy(packet + 1, &nospam_num, sizeof(nospam_num)); 792 memcpy(packet + 1, &nospam_num, sizeof(nospam_num));
@@ -752,8 +799,9 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3
752 packet[0] = CRYPTO_PACKET_FRIEND_REQ; 799 packet[0] = CRYPTO_PACKET_FRIEND_REQ;
753 int num = send_onion_data(fr_c->onion_c, friend_con->onion_friendnum, packet, sizeof(packet)); 800 int num = send_onion_data(fr_c->onion_c, friend_con->onion_friendnum, packet, sizeof(packet));
754 801
755 if (num <= 0) 802 if (num <= 0) {
756 return -1; 803 return -1;
804 }
757 805
758 return num; 806 return num;
759 } 807 }
@@ -762,13 +810,15 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3
762/* Create new friend_connections instance. */ 810/* Create new friend_connections instance. */
763Friend_Connections *new_friend_connections(Onion_Client *onion_c) 811Friend_Connections *new_friend_connections(Onion_Client *onion_c)
764{ 812{
765 if (!onion_c) 813 if (!onion_c) {
766 return NULL; 814 return NULL;
815 }
767 816
768 Friend_Connections *temp = calloc(1, sizeof(Friend_Connections)); 817 Friend_Connections *temp = calloc(1, sizeof(Friend_Connections));
769 818
770 if (temp == NULL) 819 if (temp == NULL) {
771 return NULL; 820 return NULL;
821 }
772 822
773 temp->dht = onion_c->dht; 823 temp->dht = onion_c->dht;
774 temp->net_crypto = onion_c->c; 824 temp->net_crypto = onion_c->c;
@@ -843,8 +893,9 @@ void do_friend_connections(Friend_Connections *fr_c)
843/* Free everything related with friend_connections. */ 893/* Free everything related with friend_connections. */
844void kill_friend_connections(Friend_Connections *fr_c) 894void kill_friend_connections(Friend_Connections *fr_c)
845{ 895{
846 if (!fr_c) 896 if (!fr_c) {
847 return; 897 return;
898 }
848 899
849 uint32_t i; 900 uint32_t i;
850 901