summaryrefslogtreecommitdiff
path: root/toxcore/TCP_connection.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-08-01 23:37:48 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-08-10 14:53:27 +0000
commit463cbcb19a68bc4109285872258d494332bdeaf6 (patch)
treebca1abb46cc91d3dbe462eed773ed54a0ccba164 /toxcore/TCP_connection.c
parentafab28f0ff0bd71efcf39bd633770053da99e04a (diff)
Use the crypto random functions instead of `rand()`.
Presumably the uses of `rand()` were fine because they were not used in security-sensitive places, but having to think about whether a crappy RNG is acceptable in each situation requires effort that could better be spent elsewhere. Also, this means that once we have a custom deterministic RNG for testing, that RNG is used everywhere, so all the code is deterministic. It also allowed us to delete a system-specific function that wasn't used anywhere except in a call to `srand()`.
Diffstat (limited to 'toxcore/TCP_connection.c')
-rw-r--r--toxcore/TCP_connection.c13
1 files changed, 7 insertions, 6 deletions
diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c
index 2d2dd470..6763328d 100644
--- a/toxcore/TCP_connection.c
+++ b/toxcore/TCP_connection.c
@@ -371,10 +371,10 @@ int send_packet_tcp_connection(TCP_Connections *tcp_c, int connections_number, c
371 */ 371 */
372int get_random_tcp_onion_conn_number(TCP_Connections *tcp_c) 372int get_random_tcp_onion_conn_number(TCP_Connections *tcp_c)
373{ 373{
374 unsigned int i, r = rand(); 374 const uint32_t r = random_u32();
375 375
376 for (i = 0; i < tcp_c->tcp_connections_length; ++i) { 376 for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) {
377 unsigned int index = ((i + r) % tcp_c->tcp_connections_length); 377 uint32_t index = ((i + r) % tcp_c->tcp_connections_length);
378 378
379 if (tcp_c->tcp_connections[index].onion && tcp_c->tcp_connections[index].status == TCP_CONN_CONNECTED) { 379 if (tcp_c->tcp_connections[index].onion && tcp_c->tcp_connections[index].status == TCP_CONN_CONNECTED) {
380 return index; 380 return index;
@@ -1283,11 +1283,12 @@ unsigned int tcp_connection_to_online_tcp_relays(TCP_Connections *tcp_c, int con
1283 * return number of relays copied to tcp_relays on success. 1283 * return number of relays copied to tcp_relays on success.
1284 * return 0 on failure. 1284 * return 0 on failure.
1285 */ 1285 */
1286unsigned int tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_relays, uint16_t max_num) 1286uint32_t tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_relays, uint16_t max_num)
1287{ 1287{
1288 unsigned int i, copied = 0, r = rand(); 1288 const uint32_t r = random_u32();
1289 uint32_t copied = 0;
1289 1290
1290 for (i = 0; (i < tcp_c->tcp_connections_length) && (copied < max_num); ++i) { 1291 for (uint32_t i = 0; (i < tcp_c->tcp_connections_length) && (copied < max_num); ++i) {
1291 TCP_con *tcp_con = get_tcp_connection(tcp_c, (i + r) % tcp_c->tcp_connections_length); 1292 TCP_con *tcp_con = get_tcp_connection(tcp_c, (i + r) % tcp_c->tcp_connections_length);
1292 1293
1293 if (!tcp_con) { 1294 if (!tcp_con) {