summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2020-05-04 02:33:38 +0100
committeriphydf <iphydf@users.noreply.github.com>2020-05-05 00:58:33 +0100
commitd52bf0a94799560a15c3af5f0ff8467079d444ea (patch)
treec8c5c1c79d80a0839102897596566cc94a0eb8ed
parent669ea8797ba7040d3bfbe1dfbf49e3222575d3a0 (diff)
refactor: Minor cleanup: limit scope of loop iterators.
-rw-r--r--.github/settings.yml1
-rw-r--r--toxcore/TCP_connection.c94
2 files changed, 42 insertions, 53 deletions
diff --git a/.github/settings.yml b/.github/settings.yml
index 4dedd871..a724c8d2 100644
--- a/.github/settings.yml
+++ b/.github/settings.yml
@@ -6,7 +6,6 @@ repository:
6 description: The future of online communications. 6 description: The future of online communications.
7 homepage: https://tox.chat/ 7 homepage: https://tox.chat/
8 topics: toxcore, network, p2p, security, encryption, cryptography 8 topics: toxcore, network, p2p, security, encryption, cryptography
9 has_issues: true
10 9
11branches: 10branches:
12 - name: "master" 11 - name: "master"
diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c
index 411589bf..4256f786 100644
--- a/toxcore/TCP_connection.c
+++ b/toxcore/TCP_connection.c
@@ -125,7 +125,7 @@ static bool connections_number_is_valid(const TCP_Connections *tcp_c, int connec
125 */ 125 */
126static bool tcp_connections_number_is_valid(const TCP_Connections *tcp_c, int tcp_connections_number) 126static bool tcp_connections_number_is_valid(const TCP_Connections *tcp_c, int tcp_connections_number)
127{ 127{
128 if ((unsigned int)tcp_connections_number >= tcp_c->tcp_connections_length) { 128 if ((uint32_t)tcp_connections_number >= tcp_c->tcp_connections_length) {
129 return false; 129 return false;
130 } 130 }
131 131
@@ -173,9 +173,7 @@ static int create_connection(TCP_Connections *tcp_c)
173 */ 173 */
174static int create_tcp_connection(TCP_Connections *tcp_c) 174static int create_tcp_connection(TCP_Connections *tcp_c)
175{ 175{
176 uint32_t i; 176 for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) {
177
178 for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
179 if (tcp_c->tcp_connections[i].status == TCP_CONN_NONE) { 177 if (tcp_c->tcp_connections[i].status == TCP_CONN_NONE) {
180 return i; 178 return i;
181 } 179 }
@@ -231,9 +229,10 @@ static int wipe_tcp_connection(TCP_Connections *tcp_c, int tcp_connections_numbe
231 return -1; 229 return -1;
232 } 230 }
233 231
234 uint32_t i;
235 memset(&tcp_c->tcp_connections[tcp_connections_number], 0, sizeof(TCP_con)); 232 memset(&tcp_c->tcp_connections[tcp_connections_number], 0, sizeof(TCP_con));
236 233
234 uint32_t i;
235
237 for (i = tcp_c->tcp_connections_length; i != 0; --i) { 236 for (i = tcp_c->tcp_connections_length; i != 0; --i) {
238 if (tcp_c->tcp_connections[i - 1].status != TCP_CONN_NONE) { 237 if (tcp_c->tcp_connections[i - 1].status != TCP_CONN_NONE) {
239 break; 238 break;
@@ -375,7 +374,7 @@ int get_random_tcp_onion_conn_number(TCP_Connections *tcp_c)
375 * return 0 on success. 374 * return 0 on success.
376 * return -1 on failure. 375 * return -1 on failure.
377 */ 376 */
378int tcp_send_onion_request(TCP_Connections *tcp_c, unsigned int tcp_connections_number, const uint8_t *data, 377int tcp_send_onion_request(TCP_Connections *tcp_c, uint32_t tcp_connections_number, const uint8_t *data,
379 uint16_t length) 378 uint16_t length)
380{ 379{
381 if (tcp_connections_number >= tcp_c->tcp_connections_length) { 380 if (tcp_connections_number >= tcp_c->tcp_connections_length) {
@@ -474,9 +473,7 @@ static int find_tcp_connection_to(TCP_Connections *tcp_c, const uint8_t *public_
474 */ 473 */
475static int find_tcp_connection_relay(TCP_Connections *tcp_c, const uint8_t *relay_pk) 474static int find_tcp_connection_relay(TCP_Connections *tcp_c, const uint8_t *relay_pk)
476{ 475{
477 unsigned int i; 476 for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) {
478
479 for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
480 TCP_con *tcp_con = get_tcp_connection(tcp_c, i); 477 TCP_con *tcp_con = get_tcp_connection(tcp_c, i);
481 478
482 if (tcp_con) { 479 if (tcp_con) {
@@ -1314,9 +1311,7 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, bool status)
1314 } 1311 }
1315 1312
1316 if (status) { 1313 if (status) {
1317 unsigned int i; 1314 for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) {
1318
1319 for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
1320 TCP_con *tcp_con = get_tcp_connection(tcp_c, i); 1315 TCP_con *tcp_con = get_tcp_connection(tcp_c, i);
1321 1316
1322 if (tcp_con) { 1317 if (tcp_con) {
@@ -1334,7 +1329,7 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, bool status)
1334 if (tcp_c->onion_num_conns < NUM_ONION_TCP_CONNECTIONS) { 1329 if (tcp_c->onion_num_conns < NUM_ONION_TCP_CONNECTIONS) {
1335 unsigned int wakeup = NUM_ONION_TCP_CONNECTIONS - tcp_c->onion_num_conns; 1330 unsigned int wakeup = NUM_ONION_TCP_CONNECTIONS - tcp_c->onion_num_conns;
1336 1331
1337 for (i = 0; i < tcp_c->tcp_connections_length; ++i) { 1332 for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) {
1338 TCP_con *tcp_con = get_tcp_connection(tcp_c, i); 1333 TCP_con *tcp_con = get_tcp_connection(tcp_c, i);
1339 1334
1340 if (tcp_con) { 1335 if (tcp_con) {
@@ -1351,9 +1346,7 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, bool status)
1351 1346
1352 tcp_c->onion_status = 1; 1347 tcp_c->onion_status = 1;
1353 } else { 1348 } else {
1354 unsigned int i; 1349 for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) {
1355
1356 for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
1357 TCP_con *tcp_con = get_tcp_connection(tcp_c, i); 1350 TCP_con *tcp_con = get_tcp_connection(tcp_c, i);
1358 1351
1359 if (tcp_con) { 1352 if (tcp_con) {
@@ -1400,46 +1393,46 @@ TCP_Connections *new_tcp_connections(Mono_Time *mono_time, const uint8_t *secret
1400 1393
1401static void do_tcp_conns(const Logger *logger, TCP_Connections *tcp_c, void *userdata) 1394static void do_tcp_conns(const Logger *logger, TCP_Connections *tcp_c, void *userdata)
1402{ 1395{
1403 unsigned int i; 1396 for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) {
1404
1405 for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
1406 TCP_con *tcp_con = get_tcp_connection(tcp_c, i); 1397 TCP_con *tcp_con = get_tcp_connection(tcp_c, i);
1407 1398
1408 if (tcp_con) { 1399 if (tcp_con == nullptr) {
1409 if (tcp_con->status != TCP_CONN_SLEEPING) { 1400 continue;
1410 do_TCP_connection(logger, tcp_c->mono_time, tcp_con->connection, userdata); 1401 }
1411 1402
1412 /* callbacks can change TCP connection address. */ 1403 if (tcp_con->status != TCP_CONN_SLEEPING) {
1413 tcp_con = get_tcp_connection(tcp_c, i); 1404 do_TCP_connection(logger, tcp_c->mono_time, tcp_con->connection, userdata);
1414 1405
1415 // Make sure the TCP connection wasn't dropped in any of the callbacks. 1406 /* callbacks can change TCP connection address. */
1416 assert(tcp_con != nullptr); 1407 tcp_con = get_tcp_connection(tcp_c, i);
1417 1408
1418 if (tcp_con_status(tcp_con->connection) == TCP_CLIENT_DISCONNECTED) { 1409 // Make sure the TCP connection wasn't dropped in any of the callbacks.
1419 if (tcp_con->status == TCP_CONN_CONNECTED) { 1410 assert(tcp_con != nullptr);
1420 reconnect_tcp_relay_connection(tcp_c, i);
1421 } else {
1422 kill_tcp_relay_connection(tcp_c, i);
1423 }
1424 1411
1425 continue; 1412 if (tcp_con_status(tcp_con->connection) == TCP_CLIENT_DISCONNECTED) {
1413 if (tcp_con->status == TCP_CONN_CONNECTED) {
1414 reconnect_tcp_relay_connection(tcp_c, i);
1415 } else {
1416 kill_tcp_relay_connection(tcp_c, i);
1426 } 1417 }
1427 1418
1428 if (tcp_con->status == TCP_CONN_VALID && tcp_con_status(tcp_con->connection) == TCP_CLIENT_CONFIRMED) { 1419 continue;
1429 tcp_relay_on_online(tcp_c, i); 1420 }
1430 }
1431 1421
1432 if (tcp_con->status == TCP_CONN_CONNECTED && !tcp_con->onion && tcp_con->lock_count 1422 if (tcp_con->status == TCP_CONN_VALID && tcp_con_status(tcp_con->connection) == TCP_CLIENT_CONFIRMED) {
1433 && tcp_con->lock_count == tcp_con->sleep_count 1423 tcp_relay_on_online(tcp_c, i);
1434 && mono_time_is_timeout(tcp_c->mono_time, tcp_con->connected_time, TCP_CONNECTION_ANNOUNCE_TIMEOUT)) {
1435 sleep_tcp_relay_connection(tcp_c, i);
1436 }
1437 } 1424 }
1438 1425
1439 if (tcp_con->status == TCP_CONN_SLEEPING && tcp_con->unsleep) { 1426 if (tcp_con->status == TCP_CONN_CONNECTED && !tcp_con->onion && tcp_con->lock_count
1440 unsleep_tcp_relay_connection(tcp_c, i); 1427 && tcp_con->lock_count == tcp_con->sleep_count
1428 && mono_time_is_timeout(tcp_c->mono_time, tcp_con->connected_time, TCP_CONNECTION_ANNOUNCE_TIMEOUT)) {
1429 sleep_tcp_relay_connection(tcp_c, i);
1441 } 1430 }
1442 } 1431 }
1432
1433 if (tcp_con->status == TCP_CONN_SLEEPING && tcp_con->unsleep) {
1434 unsleep_tcp_relay_connection(tcp_c, i);
1435 }
1443 } 1436 }
1444} 1437}
1445 1438
@@ -1449,12 +1442,11 @@ static void kill_nonused_tcp(TCP_Connections *tcp_c)
1449 return; 1442 return;
1450 } 1443 }
1451 1444
1452 unsigned int i; 1445 uint32_t num_online = 0;
1453 unsigned int num_online = 0; 1446 uint32_t num_kill = 0;
1454 unsigned int num_kill = 0;
1455 VLA(unsigned int, to_kill, tcp_c->tcp_connections_length); 1447 VLA(unsigned int, to_kill, tcp_c->tcp_connections_length);
1456 1448
1457 for (i = 0; i < tcp_c->tcp_connections_length; ++i) { 1449 for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) {
1458 TCP_con *tcp_con = get_tcp_connection(tcp_c, i); 1450 TCP_con *tcp_con = get_tcp_connection(tcp_c, i);
1459 1451
1460 if (tcp_con) { 1452 if (tcp_con) {
@@ -1474,13 +1466,13 @@ static void kill_nonused_tcp(TCP_Connections *tcp_c)
1474 return; 1466 return;
1475 } 1467 }
1476 1468
1477 unsigned int n = num_online - RECOMMENDED_FRIEND_TCP_CONNECTIONS; 1469 uint32_t n = num_online - RECOMMENDED_FRIEND_TCP_CONNECTIONS;
1478 1470
1479 if (n < num_kill) { 1471 if (n < num_kill) {
1480 num_kill = n; 1472 num_kill = n;
1481 } 1473 }
1482 1474
1483 for (i = 0; i < num_kill; ++i) { 1475 for (uint32_t i = 0; i < num_kill; ++i) {
1484 kill_tcp_relay_connection(tcp_c, to_kill[i]); 1476 kill_tcp_relay_connection(tcp_c, to_kill[i]);
1485 } 1477 }
1486} 1478}
@@ -1493,9 +1485,7 @@ void do_tcp_connections(const Logger *logger, TCP_Connections *tcp_c, void *user
1493 1485
1494void kill_tcp_connections(TCP_Connections *tcp_c) 1486void kill_tcp_connections(TCP_Connections *tcp_c)
1495{ 1487{
1496 unsigned int i; 1488 for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) {
1497
1498 for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
1499 kill_TCP_connection(tcp_c->tcp_connections[i].connection); 1489 kill_TCP_connection(tcp_c->tcp_connections[i].connection);
1500 } 1490 }
1501 1491