summaryrefslogtreecommitdiff
path: root/toxcore/friend_connection.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-02-16 17:16:49 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-02-18 13:01:16 +0000
commit3f73dfa7f1a7fa1afef2492d1834e4c619f4afb3 (patch)
treea8fa188dc9929dbfbb5556b1e58d17d46596ec05 /toxcore/friend_connection.c
parent841b63434fcb949c48d42a2a8b2b4f69467f3f78 (diff)
Use `const` more in C code.
Diffstat (limited to 'toxcore/friend_connection.c')
-rw-r--r--toxcore/friend_connection.c226
1 files changed, 98 insertions, 128 deletions
diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c
index 9381262b..6f5685fb 100644
--- a/toxcore/friend_connection.c
+++ b/toxcore/friend_connection.c
@@ -88,48 +88,38 @@ Net_Crypto *friendconn_net_crypto(const Friend_Connections *fr_c)
88} 88}
89 89
90 90
91/* return 1 if the friendcon_id is not valid. 91/* return true if the friendcon_id is valid.
92 * return 0 if the friendcon_id is valid. 92 * return false if the friendcon_id is not valid.
93 */ 93 */
94static uint8_t friendconn_id_not_valid(const Friend_Connections *fr_c, int friendcon_id) 94static bool friendconn_id_valid(const Friend_Connections *fr_c, int friendcon_id)
95{ 95{
96 if ((unsigned int)friendcon_id >= fr_c->num_cons) { 96 return (unsigned int)friendcon_id < fr_c->num_cons &&
97 return 1; 97 fr_c->conns != nullptr &&
98 } 98 fr_c->conns[friendcon_id].status != FRIENDCONN_STATUS_NONE;
99
100 if (fr_c->conns == nullptr) {
101 return 1;
102 }
103
104 if (fr_c->conns[friendcon_id].status == FRIENDCONN_STATUS_NONE) {
105 return 1;
106 }
107
108 return 0;
109} 99}
110 100
111 101
112/* Set the size of the friend connections list to num. 102/* Set the size of the friend connections list to num.
113 * 103 *
114 * return -1 if realloc fails. 104 * return false if realloc fails.
115 * return 0 if it succeeds. 105 * return true if it succeeds.
116 */ 106 */
117static int realloc_friendconns(Friend_Connections *fr_c, uint32_t num) 107static bool realloc_friendconns(Friend_Connections *fr_c, uint32_t num)
118{ 108{
119 if (num == 0) { 109 if (num == 0) {
120 free(fr_c->conns); 110 free(fr_c->conns);
121 fr_c->conns = nullptr; 111 fr_c->conns = nullptr;
122 return 0; 112 return true;
123 } 113 }
124 114
125 Friend_Conn *newgroup_cons = (Friend_Conn *)realloc(fr_c->conns, num * sizeof(Friend_Conn)); 115 Friend_Conn *newgroup_cons = (Friend_Conn *)realloc(fr_c->conns, num * sizeof(Friend_Conn));
126 116
127 if (newgroup_cons == nullptr) { 117 if (newgroup_cons == nullptr) {
128 return -1; 118 return false;
129 } 119 }
130 120
131 fr_c->conns = newgroup_cons; 121 fr_c->conns = newgroup_cons;
132 return 0; 122 return true;
133} 123}
134 124
135/* Create a new empty friend connection. 125/* Create a new empty friend connection.
@@ -139,22 +129,20 @@ static int realloc_friendconns(Friend_Connections *fr_c, uint32_t num)
139 */ 129 */
140static int create_friend_conn(Friend_Connections *fr_c) 130static int create_friend_conn(Friend_Connections *fr_c)
141{ 131{
142 uint32_t i; 132 for (uint32_t i = 0; i < fr_c->num_cons; ++i) {
143
144 for (i = 0; i < fr_c->num_cons; ++i) {
145 if (fr_c->conns[i].status == FRIENDCONN_STATUS_NONE) { 133 if (fr_c->conns[i].status == FRIENDCONN_STATUS_NONE) {
146 return i; 134 return i;
147 } 135 }
148 } 136 }
149 137
150 int id = -1; 138 if (!realloc_friendconns(fr_c, fr_c->num_cons + 1)) {
151 139 return -1;
152 if (realloc_friendconns(fr_c, fr_c->num_cons + 1) == 0) {
153 id = fr_c->num_cons;
154 ++fr_c->num_cons;
155 memset(&fr_c->conns[id], 0, sizeof(Friend_Conn));
156 } 140 }
157 141
142 const int id = fr_c->num_cons;
143 ++fr_c->num_cons;
144 memset(&fr_c->conns[id], 0, sizeof(Friend_Conn));
145
158 return id; 146 return id;
159} 147}
160 148
@@ -165,13 +153,14 @@ static int create_friend_conn(Friend_Connections *fr_c)
165 */ 153 */
166static int wipe_friend_conn(Friend_Connections *fr_c, int friendcon_id) 154static int wipe_friend_conn(Friend_Connections *fr_c, int friendcon_id)
167{ 155{
168 if (friendconn_id_not_valid(fr_c, friendcon_id)) { 156 if (!friendconn_id_valid(fr_c, friendcon_id)) {
169 return -1; 157 return -1;
170 } 158 }
171 159
172 uint32_t i;
173 memset(&fr_c->conns[friendcon_id], 0, sizeof(Friend_Conn)); 160 memset(&fr_c->conns[friendcon_id], 0, sizeof(Friend_Conn));
174 161
162 uint32_t i;
163
175 for (i = fr_c->num_cons; i != 0; --i) { 164 for (i = fr_c->num_cons; i != 0; --i) {
176 if (fr_c->conns[i - 1].status != FRIENDCONN_STATUS_NONE) { 165 if (fr_c->conns[i - 1].status != FRIENDCONN_STATUS_NONE) {
177 break; 166 break;
@@ -188,7 +177,7 @@ static int wipe_friend_conn(Friend_Connections *fr_c, int friendcon_id)
188 177
189static Friend_Conn *get_conn(const Friend_Connections *fr_c, int friendcon_id) 178static Friend_Conn *get_conn(const Friend_Connections *fr_c, int friendcon_id)
190{ 179{
191 if (friendconn_id_not_valid(fr_c, friendcon_id)) { 180 if (!friendconn_id_valid(fr_c, friendcon_id)) {
192 return nullptr; 181 return nullptr;
193 } 182 }
194 183
@@ -200,9 +189,7 @@ static Friend_Conn *get_conn(const Friend_Connections *fr_c, int friendcon_id)
200 */ 189 */
201int getfriend_conn_id_pk(Friend_Connections *fr_c, const uint8_t *real_pk) 190int getfriend_conn_id_pk(Friend_Connections *fr_c, const uint8_t *real_pk)
202{ 191{
203 uint32_t i; 192 for (uint32_t i = 0; i < fr_c->num_cons; ++i) {
204
205 for (i = 0; i < fr_c->num_cons; ++i) {
206 Friend_Conn *friend_con = get_conn(fr_c, i); 193 Friend_Conn *friend_con = get_conn(fr_c, i);
207 194
208 if (friend_con) { 195 if (friend_con) {
@@ -222,7 +209,7 @@ int getfriend_conn_id_pk(Friend_Connections *fr_c, const uint8_t *real_pk)
222 */ 209 */
223int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_port, const uint8_t *public_key) 210int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_port, const uint8_t *public_key)
224{ 211{
225 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 212 Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);
226 213
227 if (!friend_con) { 214 if (!friend_con) {
228 return -1; 215 return -1;
@@ -237,11 +224,9 @@ int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_
237 } 224 }
238 } 225 }
239 226
240 unsigned int i; 227 const uint16_t index = friend_con->tcp_relay_counter % FRIEND_MAX_STORED_TCP_RELAYS;
241 228
242 uint16_t index = friend_con->tcp_relay_counter % FRIEND_MAX_STORED_TCP_RELAYS; 229 for (unsigned i = 0; i < FRIEND_MAX_STORED_TCP_RELAYS; ++i) {
243
244 for (i = 0; i < FRIEND_MAX_STORED_TCP_RELAYS; ++i) {
245 if (friend_con->tcp_relays[i].ip_port.ip.family != 0 230 if (friend_con->tcp_relays[i].ip_port.ip.family != 0
246 && public_key_cmp(friend_con->tcp_relays[i].public_key, public_key) == 0) { 231 && public_key_cmp(friend_con->tcp_relays[i].public_key, public_key) == 0) {
247 memset(&friend_con->tcp_relays[i], 0, sizeof(Node_format)); 232 memset(&friend_con->tcp_relays[i], 0, sizeof(Node_format));
@@ -258,16 +243,14 @@ int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_
258/* Connect to number saved relays for friend. */ 243/* Connect to number saved relays for friend. */
259static void connect_to_saved_tcp_relays(Friend_Connections *fr_c, int friendcon_id, unsigned int number) 244static void connect_to_saved_tcp_relays(Friend_Connections *fr_c, int friendcon_id, unsigned int number)
260{ 245{
261 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 246 const Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);
262 247
263 if (!friend_con) { 248 if (!friend_con) {
264 return; 249 return;
265 } 250 }
266 251
267 unsigned int i; 252 for (unsigned i = 0; (i < FRIEND_MAX_STORED_TCP_RELAYS) && (number != 0); ++i) {
268 253 const uint16_t index = (friend_con->tcp_relay_counter - (i + 1)) % FRIEND_MAX_STORED_TCP_RELAYS;
269 for (i = 0; (i < FRIEND_MAX_STORED_TCP_RELAYS) && (number != 0); ++i) {
270 uint16_t index = (friend_con->tcp_relay_counter - (i + 1)) % FRIEND_MAX_STORED_TCP_RELAYS;
271 254
272 if (friend_con->tcp_relays[index].ip_port.ip.family) { 255 if (friend_con->tcp_relays[index].ip_port.ip.family) {
273 if (add_tcp_relay_peer(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->tcp_relays[index].ip_port, 256 if (add_tcp_relay_peer(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->tcp_relays[index].ip_port,
@@ -280,7 +263,7 @@ static void connect_to_saved_tcp_relays(Friend_Connections *fr_c, int friendcon_
280 263
281static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id) 264static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id)
282{ 265{
283 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 266 Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);
284 267
285 if (!friend_con) { 268 if (!friend_con) {
286 return 0; 269 return 0;
@@ -288,19 +271,16 @@ static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id)
288 271
289 Node_format nodes[MAX_SHARED_RELAYS]; 272 Node_format nodes[MAX_SHARED_RELAYS];
290 uint8_t data[1024]; 273 uint8_t data[1024];
291 int n, length;
292 274
293 n = copy_connected_tcp_relays(fr_c->net_crypto, nodes, MAX_SHARED_RELAYS); 275 const int n = copy_connected_tcp_relays(fr_c->net_crypto, nodes, MAX_SHARED_RELAYS);
294 276
295 int i; 277 for (int i = 0; i < n; ++i) {
296
297 for (i = 0; i < n; ++i) {
298 /* Associated the relays being sent with this connection. 278 /* Associated the relays being sent with this connection.
299 On receiving the peer will do the same which will establish the connection. */ 279 On receiving the peer will do the same which will establish the connection. */
300 friend_add_tcp_relay(fr_c, friendcon_id, nodes[i].ip_port, nodes[i].public_key); 280 friend_add_tcp_relay(fr_c, friendcon_id, nodes[i].ip_port, nodes[i].public_key);
301 } 281 }
302 282
303 length = pack_nodes(data + 1, sizeof(data) - 1, nodes, n); 283 int length = pack_nodes(data + 1, sizeof(data) - 1, nodes, n);
304 284
305 if (length <= 0) { 285 if (length <= 0) {
306 return 0; 286 return 0;
@@ -321,7 +301,7 @@ static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id)
321static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key) 301static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key)
322{ 302{
323 Friend_Connections *fr_c = (Friend_Connections *)object; 303 Friend_Connections *fr_c = (Friend_Connections *)object;
324 Friend_Conn *friend_con = get_conn(fr_c, number); 304 const Friend_Conn *friend_con = get_conn(fr_c, number);
325 305
326 if (!friend_con) { 306 if (!friend_con) {
327 return -1; 307 return -1;
@@ -338,8 +318,8 @@ static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id);
338/* Callback for DHT ip_port changes. */ 318/* Callback for DHT ip_port changes. */
339static void dht_ip_callback(void *object, int32_t number, IP_Port ip_port) 319static void dht_ip_callback(void *object, int32_t number, IP_Port ip_port)
340{ 320{
341 Friend_Connections *fr_c = (Friend_Connections *)object; 321 Friend_Connections *const fr_c = (Friend_Connections *)object;
342 Friend_Conn *friend_con = get_conn(fr_c, number); 322 Friend_Conn *const friend_con = get_conn(fr_c, number);
343 323
344 if (!friend_con) { 324 if (!friend_con) {
345 return; 325 return;
@@ -361,7 +341,7 @@ static void dht_ip_callback(void *object, int32_t number, IP_Port ip_port)
361 341
362static void change_dht_pk(Friend_Connections *fr_c, int friendcon_id, const uint8_t *dht_public_key) 342static void change_dht_pk(Friend_Connections *fr_c, int friendcon_id, const uint8_t *dht_public_key)
363{ 343{
364 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 344 Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);
365 345
366 if (!friend_con) { 346 if (!friend_con) {
367 return; 347 return;
@@ -384,8 +364,8 @@ static void change_dht_pk(Friend_Connections *fr_c, int friendcon_id, const uint
384 364
385static int handle_status(void *object, int number, uint8_t status, void *userdata) 365static int handle_status(void *object, int number, uint8_t status, void *userdata)
386{ 366{
387 Friend_Connections *fr_c = (Friend_Connections *)object; 367 Friend_Connections *const fr_c = (Friend_Connections *)object;
388 Friend_Conn *friend_con = get_conn(fr_c, number); 368 Friend_Conn *const friend_con = get_conn(fr_c, number);
389 369
390 if (!friend_con) { 370 if (!friend_con) {
391 return -1; 371 return -1;
@@ -429,8 +409,8 @@ static int handle_status(void *object, int number, uint8_t status, void *userdat
429/* Callback for dht public key changes. */ 409/* Callback for dht public key changes. */
430static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_public_key, void *userdata) 410static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_public_key, void *userdata)
431{ 411{
432 Friend_Connections *fr_c = (Friend_Connections *)object; 412 Friend_Connections *const fr_c = (Friend_Connections *)object;
433 Friend_Conn *friend_con = get_conn(fr_c, number); 413 Friend_Conn *const friend_con = get_conn(fr_c, number);
434 414
435 if (!friend_con) { 415 if (!friend_con) {
436 return; 416 return;
@@ -459,7 +439,7 @@ static int handle_packet(void *object, int number, const uint8_t *data, uint16_t
459 return -1; 439 return -1;
460 } 440 }
461 441
462 Friend_Connections *fr_c = (Friend_Connections *)object; 442 Friend_Connections *const fr_c = (Friend_Connections *)object;
463 Friend_Conn *friend_con = get_conn(fr_c, number); 443 Friend_Conn *friend_con = get_conn(fr_c, number);
464 444
465 if (!friend_con) { 445 if (!friend_con) {
@@ -481,24 +461,20 @@ static int handle_packet(void *object, int number, const uint8_t *data, uint16_t
481 461
482 if (data[0] == PACKET_ID_SHARE_RELAYS) { 462 if (data[0] == PACKET_ID_SHARE_RELAYS) {
483 Node_format nodes[MAX_SHARED_RELAYS]; 463 Node_format nodes[MAX_SHARED_RELAYS];
484 int n; 464 const int n = unpack_nodes(nodes, MAX_SHARED_RELAYS, nullptr, data + 1, length - 1, 1);
485 465
486 if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, nullptr, data + 1, length - 1, 1)) == -1) { 466 if (n == -1) {
487 return -1; 467 return -1;
488 } 468 }
489 469
490 int j; 470 for (int j = 0; j < n; j++) {
491
492 for (j = 0; j < n; j++) {
493 friend_add_tcp_relay(fr_c, number, nodes[j].ip_port, nodes[j].public_key); 471 friend_add_tcp_relay(fr_c, number, nodes[j].ip_port, nodes[j].public_key);
494 } 472 }
495 473
496 return 0; 474 return 0;
497 } 475 }
498 476
499 unsigned int i; 477 for (unsigned i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) {
500
501 for (i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) {
502 if (friend_con->callbacks[i].data_callback) { 478 if (friend_con->callbacks[i].data_callback) {
503 friend_con->callbacks[i].data_callback( 479 friend_con->callbacks[i].data_callback(
504 friend_con->callbacks[i].callback_object, 480 friend_con->callbacks[i].callback_object,
@@ -521,16 +497,14 @@ static int handle_lossy_packet(void *object, int number, const uint8_t *data, ui
521 return -1; 497 return -1;
522 } 498 }
523 499
524 Friend_Connections *fr_c = (Friend_Connections *)object; 500 Friend_Connections *const fr_c = (Friend_Connections *)object;
525 Friend_Conn *friend_con = get_conn(fr_c, number); 501 const Friend_Conn *friend_con = get_conn(fr_c, number);
526 502
527 if (!friend_con) { 503 if (!friend_con) {
528 return -1; 504 return -1;
529 } 505 }
530 506
531 unsigned int i; 507 for (unsigned i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) {
532
533 for (i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) {
534 if (friend_con->callbacks[i].lossy_data_callback) { 508 if (friend_con->callbacks[i].lossy_data_callback) {
535 friend_con->callbacks[i].lossy_data_callback( 509 friend_con->callbacks[i].lossy_data_callback(
536 friend_con->callbacks[i].callback_object, 510 friend_con->callbacks[i].callback_object,
@@ -549,48 +523,47 @@ static int handle_lossy_packet(void *object, int number, const uint8_t *data, ui
549 523
550static int handle_new_connections(void *object, New_Connection *n_c) 524static int handle_new_connections(void *object, New_Connection *n_c)
551{ 525{
552 Friend_Connections *fr_c = (Friend_Connections *)object; 526 Friend_Connections *const fr_c = (Friend_Connections *)object;
553 int friendcon_id = getfriend_conn_id_pk(fr_c, n_c->public_key); 527 const int friendcon_id = getfriend_conn_id_pk(fr_c, n_c->public_key);
554 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 528 Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);
555 529
556 if (friend_con) { 530 if (!friend_con) {
557 531 return -1;
558 if (friend_con->crypt_connection_id != -1) { 532 }
559 return -1;
560 }
561 533
562 int id = accept_crypto_connection(fr_c->net_crypto, n_c); 534 if (friend_con->crypt_connection_id != -1) {
535 return -1;
536 }
563 537
564 if (id == -1) { 538 const int id = accept_crypto_connection(fr_c->net_crypto, n_c);
565 return -1;
566 }
567 539
568 connection_status_handler(fr_c->net_crypto, id, &handle_status, fr_c, friendcon_id); 540 if (id == -1) {
569 connection_data_handler(fr_c->net_crypto, id, &handle_packet, fr_c, friendcon_id); 541 return -1;
570 connection_lossy_data_handler(fr_c->net_crypto, id, &handle_lossy_packet, fr_c, friendcon_id); 542 }
571 friend_con->crypt_connection_id = id;
572 543
573 if (n_c->source.ip.family != TOX_AF_INET && n_c->source.ip.family != TOX_AF_INET6) { 544 connection_status_handler(fr_c->net_crypto, id, &handle_status, fr_c, friendcon_id);
574 set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->dht_ip_port, 0); 545 connection_data_handler(fr_c->net_crypto, id, &handle_packet, fr_c, friendcon_id);
575 } else { 546 connection_lossy_data_handler(fr_c->net_crypto, id, &handle_lossy_packet, fr_c, friendcon_id);
576 friend_con->dht_ip_port = n_c->source; 547 friend_con->crypt_connection_id = id;
577 friend_con->dht_ip_port_lastrecv = unix_time();
578 }
579 548
580 if (public_key_cmp(friend_con->dht_temp_pk, n_c->dht_public_key) != 0) { 549 if (n_c->source.ip.family != TOX_AF_INET && n_c->source.ip.family != TOX_AF_INET6) {
581 change_dht_pk(fr_c, friendcon_id, n_c->dht_public_key); 550 set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->dht_ip_port, 0);
582 } 551 } else {
552 friend_con->dht_ip_port = n_c->source;
553 friend_con->dht_ip_port_lastrecv = unix_time();
554 }
583 555
584 nc_dht_pk_callback(fr_c->net_crypto, id, &dht_pk_callback, fr_c, friendcon_id); 556 if (public_key_cmp(friend_con->dht_temp_pk, n_c->dht_public_key) != 0) {
585 return 0; 557 change_dht_pk(fr_c, friendcon_id, n_c->dht_public_key);
586 } 558 }
587 559
588 return -1; 560 nc_dht_pk_callback(fr_c->net_crypto, id, &dht_pk_callback, fr_c, friendcon_id);
561 return 0;
589} 562}
590 563
591static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id) 564static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id)
592{ 565{
593 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 566 Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);
594 567
595 if (!friend_con) { 568 if (!friend_con) {
596 return -1; 569 return -1;
@@ -605,7 +578,7 @@ static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id)
605 return -1; 578 return -1;
606 } 579 }
607 580
608 int id = new_crypto_connection(fr_c->net_crypto, friend_con->real_public_key, friend_con->dht_temp_pk); 581 const int id = new_crypto_connection(fr_c->net_crypto, friend_con->real_public_key, friend_con->dht_temp_pk);
609 582
610 if (id == -1) { 583 if (id == -1) {
611 return -1; 584 return -1;
@@ -622,14 +595,14 @@ static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id)
622 595
623static int send_ping(const Friend_Connections *fr_c, int friendcon_id) 596static int send_ping(const Friend_Connections *fr_c, int friendcon_id)
624{ 597{
625 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 598 Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);
626 599
627 if (!friend_con) { 600 if (!friend_con) {
628 return -1; 601 return -1;
629 } 602 }
630 603
631 uint8_t ping = PACKET_ID_ALIVE; 604 const uint8_t ping = PACKET_ID_ALIVE;
632 int64_t ret = write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, &ping, sizeof(ping), 0); 605 const int64_t ret = write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, &ping, sizeof(ping), 0);
633 606
634 if (ret != -1) { 607 if (ret != -1) {
635 friend_con->ping_lastsent = unix_time(); 608 friend_con->ping_lastsent = unix_time();
@@ -646,7 +619,7 @@ static int send_ping(const Friend_Connections *fr_c, int friendcon_id)
646 */ 619 */
647int friend_connection_lock(Friend_Connections *fr_c, int friendcon_id) 620int friend_connection_lock(Friend_Connections *fr_c, int friendcon_id)
648{ 621{
649 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 622 Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);
650 623
651 if (!friend_con) { 624 if (!friend_con) {
652 return -1; 625 return -1;
@@ -662,7 +635,7 @@ int friend_connection_lock(Friend_Connections *fr_c, int friendcon_id)
662 */ 635 */
663unsigned int friend_con_connected(Friend_Connections *fr_c, int friendcon_id) 636unsigned int friend_con_connected(Friend_Connections *fr_c, int friendcon_id)
664{ 637{
665 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 638 const Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);
666 639
667 if (!friend_con) { 640 if (!friend_con) {
668 return 0; 641 return 0;
@@ -678,7 +651,7 @@ unsigned int friend_con_connected(Friend_Connections *fr_c, int friendcon_id)
678 */ 651 */
679int get_friendcon_public_keys(uint8_t *real_pk, uint8_t *dht_temp_pk, Friend_Connections *fr_c, int friendcon_id) 652int get_friendcon_public_keys(uint8_t *real_pk, uint8_t *dht_temp_pk, Friend_Connections *fr_c, int friendcon_id)
680{ 653{
681 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 654 const Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);
682 655
683 if (!friend_con) { 656 if (!friend_con) {
684 return -1; 657 return -1;
@@ -714,7 +687,7 @@ int friend_connection_callbacks(Friend_Connections *fr_c, int friendcon_id, unsi
714 int (*lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata), 687 int (*lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata),
715 void *object, int number) 688 void *object, int number)
716{ 689{
717 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 690 Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);
718 691
719 if (!friend_con) { 692 if (!friend_con) {
720 return -1; 693 return -1;
@@ -741,7 +714,7 @@ int friend_connection_callbacks(Friend_Connections *fr_c, int friendcon_id, unsi
741 */ 714 */
742int friend_connection_crypt_connection_id(Friend_Connections *fr_c, int friendcon_id) 715int friend_connection_crypt_connection_id(Friend_Connections *fr_c, int friendcon_id)
743{ 716{
744 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 717 const Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);
745 718
746 if (!friend_con) { 719 if (!friend_con) {
747 return -1; 720 return -1;
@@ -771,13 +744,13 @@ int new_friend_connection(Friend_Connections *fr_c, const uint8_t *real_public_k
771 return -1; 744 return -1;
772 } 745 }
773 746
774 int32_t onion_friendnum = onion_addfriend(fr_c->onion_c, real_public_key); 747 const int32_t onion_friendnum = onion_addfriend(fr_c->onion_c, real_public_key);
775 748
776 if (onion_friendnum == -1) { 749 if (onion_friendnum == -1) {
777 return -1; 750 return -1;
778 } 751 }
779 752
780 Friend_Conn *friend_con = &fr_c->conns[friendcon_id]; 753 Friend_Conn *const friend_con = &fr_c->conns[friendcon_id];
781 754
782 friend_con->crypt_connection_id = -1; 755 friend_con->crypt_connection_id = -1;
783 friend_con->status = FRIENDCONN_STATUS_CONNECTING; 756 friend_con->status = FRIENDCONN_STATUS_CONNECTING;
@@ -797,7 +770,7 @@ int new_friend_connection(Friend_Connections *fr_c, const uint8_t *real_public_k
797 */ 770 */
798int kill_friend_connection(Friend_Connections *fr_c, int friendcon_id) 771int kill_friend_connection(Friend_Connections *fr_c, int friendcon_id)
799{ 772{
800 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 773 Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);
801 774
802 if (!friend_con) { 775 if (!friend_con) {
803 return -1; 776 return -1;
@@ -844,7 +817,7 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3
844 return -1; 817 return -1;
845 } 818 }
846 819
847 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); 820 const Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id);
848 821
849 if (!friend_con) { 822 if (!friend_con) {
850 return -1; 823 return -1;
@@ -860,7 +833,7 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3
860 } 833 }
861 834
862 packet[0] = CRYPTO_PACKET_FRIEND_REQ; 835 packet[0] = CRYPTO_PACKET_FRIEND_REQ;
863 int num = send_onion_data(fr_c->onion_c, friend_con->onion_friendnum, packet, SIZEOF_VLA(packet)); 836 const int num = send_onion_data(fr_c->onion_c, friend_con->onion_friendnum, packet, SIZEOF_VLA(packet));
864 837
865 if (num <= 0) { 838 if (num <= 0) {
866 return -1; 839 return -1;
@@ -872,11 +845,11 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3
872/* Create new friend_connections instance. */ 845/* Create new friend_connections instance. */
873Friend_Connections *new_friend_connections(Onion_Client *onion_c, bool local_discovery_enabled) 846Friend_Connections *new_friend_connections(Onion_Client *onion_c, bool local_discovery_enabled)
874{ 847{
875 if (!onion_c) { 848 if (onion_c == nullptr) {
876 return nullptr; 849 return nullptr;
877 } 850 }
878 851
879 Friend_Connections *temp = (Friend_Connections *)calloc(1, sizeof(Friend_Connections)); 852 Friend_Connections *const temp = (Friend_Connections *)calloc(1, sizeof(Friend_Connections));
880 853
881 if (temp == nullptr) { 854 if (temp == nullptr) {
882 return nullptr; 855 return nullptr;
@@ -923,11 +896,10 @@ static void LANdiscovery(Friend_Connections *fr_c)
923/* main friend_connections loop. */ 896/* main friend_connections loop. */
924void do_friend_connections(Friend_Connections *fr_c, void *userdata) 897void do_friend_connections(Friend_Connections *fr_c, void *userdata)
925{ 898{
926 uint32_t i; 899 const uint64_t temp_time = unix_time();
927 uint64_t temp_time = unix_time();
928 900
929 for (i = 0; i < fr_c->num_cons; ++i) { 901 for (uint32_t i = 0; i < fr_c->num_cons; ++i) {
930 Friend_Conn *friend_con = get_conn(fr_c, i); 902 Friend_Conn *const friend_con = get_conn(fr_c, i);
931 903
932 if (friend_con) { 904 if (friend_con) {
933 if (friend_con->status == FRIENDCONN_STATUS_CONNECTING) { 905 if (friend_con->status == FRIENDCONN_STATUS_CONNECTING) {
@@ -980,9 +952,7 @@ void kill_friend_connections(Friend_Connections *fr_c)
980 return; 952 return;
981 } 953 }
982 954
983 uint32_t i; 955 for (uint32_t i = 0; i < fr_c->num_cons; ++i) {
984
985 for (i = 0; i < fr_c->num_cons; ++i) {
986 kill_friend_connection(fr_c, i); 956 kill_friend_connection(fr_c, i);
987 } 957 }
988 958