Skip to content

Commit b4a0e61

Browse files
committed
refactor: Use IP string length from ip_ntoa instead of strlen.
Saves us a strlen and is slightly safer. Also, changed the event length to not include the terminating NUL byte. Adding it is just confusing.
1 parent b85b91f commit b4a0e61

File tree

11 files changed

+18
-20
lines changed

11 files changed

+18
-20
lines changed

.cirrus.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# test_all_script:
2121
# - |
2222
# # TODO(iphydf): Investigate FreeBSD failures on these tests.
23-
# sed -Ei -e '/\(dht_getnodes_api\)/s/^/#/' auto_tests/CMakeLists.txt
23+
# sed -Ei -e '/\(dht_nodes_request_api\)/s/^/#/' auto_tests/CMakeLists.txt
2424
# cmake . \
2525
# -DMIN_LOGGER_LEVEL=TRACE \
2626
# -DMUST_BUILD_TOXAV=ON \

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ jobs:
121121

122122
run: |
123123
# TODO(iphydf): Investigate NetBSD failures on these tests.
124-
sed -Ei -e '/\((TCP|dht_getnodes_api)\)/s/^/#/' auto_tests/CMakeLists.txt
124+
sed -Ei -e '/\((TCP|dht_nodes_request_api)\)/s/^/#/' auto_tests/CMakeLists.txt
125125
cmake . \
126126
-DMIN_LOGGER_LEVEL=TRACE \
127127
-DMUST_BUILD_TOXAV=ON \
@@ -160,7 +160,7 @@ jobs:
160160

161161
run: |
162162
# TODO(iphydf): Investigate FreeBSD failures on these tests.
163-
sed -Ei -e '/\(dht_getnodes_api\)/s/^/#/' auto_tests/CMakeLists.txt
163+
sed -Ei -e '/\(dht_nodes_request_api\)/s/^/#/' auto_tests/CMakeLists.txt
164164
cmake . \
165165
-DMIN_LOGGER_LEVEL=TRACE \
166166
-DMUST_BUILD_TOXAV=ON \

auto_tests/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ auto_test(conference_simple)
4848
auto_test(conference_two)
4949
auto_test(crypto)
5050
#auto_test(dht) # Doesn't work with UNITY_BUILD.
51-
auto_test(dht_getnodes_api)
51+
auto_test(dht_nodes_response_api)
5252
auto_test(encryptsave)
5353
auto_test(file_saving)
5454
auto_test(file_streaming)

auto_tests/dht_getnodes_api_test.c renamed to auto_tests/dht_nodes_response_api_test.c

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <stdio.h>
77
#include <stdlib.h>
88
#include <string.h>
9-
#include <time.h>
109

1110
#include "../toxcore/tox.h"
1211
#include "../toxcore/tox_private.h"

toxav/toxav.c

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "rtp.h"
1414
#include "toxav_hacks.h"
1515

16+
#include "../toxcore/Messenger.h"
1617
#include "../toxcore/ccompat.h"
1718
#include "../toxcore/logger.h"
1819
#include "../toxcore/mono_time.h"

toxcore/events/dht_nodes_response.c

+4-9
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ static bool tox_event_dht_nodes_response_set_ip(Tox_Event_Dht_Nodes_Response *dh
5151
dht_nodes_response->ip_length = 0;
5252
}
5353

54-
uint8_t *ip_tmp = (uint8_t *)mem_balloc(mem, ip_length);
54+
uint8_t *ip_tmp = (uint8_t *)mem_balloc(mem, ip_length + 1);
5555

5656
if (ip_tmp == nullptr) {
5757
return false;
5858
}
5959

60-
memcpy(ip_tmp, ip, ip_length);
60+
memcpy(ip_tmp, ip, ip_length + 1);
6161
dht_nodes_response->ip = ip_tmp;
6262
dht_nodes_response->ip_length = ip_length;
6363
return true;
@@ -206,22 +206,17 @@ static Tox_Event_Dht_Nodes_Response *tox_event_dht_nodes_response_alloc(void *us
206206

207207
void tox_events_handle_dht_nodes_response(
208208
Tox *tox, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE],
209-
const char *ip, uint16_t port, void *user_data)
209+
const char *ip, uint32_t ip_length, uint16_t port, void *user_data)
210210
{
211211
Tox_Event_Dht_Nodes_Response *dht_nodes_response = tox_event_dht_nodes_response_alloc(user_data);
212212

213213
if (dht_nodes_response == nullptr) {
214214
return;
215215
}
216216

217-
const size_t ip_length = strlen(ip);
218-
if (ip_length >= UINT32_MAX) {
219-
return;
220-
}
221-
222217
const Tox_System *sys = tox_get_system(tox);
223218

224219
tox_event_dht_nodes_response_set_public_key(dht_nodes_response, public_key);
225-
tox_event_dht_nodes_response_set_ip(dht_nodes_response, ip, ip_length + 1, sys->mem);
220+
tox_event_dht_nodes_response_set_ip(dht_nodes_response, ip, ip_length, sys->mem);
226221
tox_event_dht_nodes_response_set_port(dht_nodes_response, port);
227222
}

toxcore/tox.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,11 @@ static void tox_dht_nodes_response_handler(const DHT *dht, const Node_format *no
373373
}
374374

375375
Ip_Ntoa ip_str;
376+
net_ip_ntoa(&node->ip_port.ip, &ip_str);
377+
376378
tox_unlock(tox_data->tox);
377379
tox_data->tox->dht_nodes_response_callback(
378-
tox_data->tox, node->public_key, net_ip_ntoa(&node->ip_port.ip, &ip_str), net_ntohs(node->ip_port.port),
380+
tox_data->tox, node->public_key, ip_str.buf, ip_str.length, net_ntohs(node->ip_port.port),
379381
tox_data->user_data);
380382
tox_lock(tox_data->tox);
381383
}

toxcore/tox_events.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "tox.h"
1818
#include "tox_event.h"
1919
#include "tox_private.h"
20-
#include "tox_struct.h"
20+
#include "tox_struct.h" // IWYU pragma: keep
2121

2222
/*****************************************************
2323
*

toxcore/tox_private.c

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <assert.h>
1212

1313
#include "DHT.h"
14+
#include "Messenger.h"
1415
#include "TCP_server.h"
1516
#include "ccompat.h"
1617
#include "crypto_core.h"

toxcore/tox_private.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ uint32_t tox_dht_node_public_key_size(void);
9393
* @param ip The node's IP address, represented as a NUL-terminated C string.
9494
* @param port The node's port.
9595
*/
96-
typedef void tox_dht_nodes_response_cb(Tox *tox, const uint8_t *public_key, const char *ip, uint16_t port,
97-
void *user_data);
96+
typedef void tox_dht_nodes_response_cb(
97+
Tox *tox, const uint8_t *public_key, const char *ip, uint32_t ip_length,
98+
uint16_t port, void *user_data);
9899

99100
/**
100101
* Set the callback for the `dht_nodes_response` event. Pass NULL to unset.

toxcore/tox_struct.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include <pthread.h>
1010

11-
#include "Messenger.h"
1211
#include "mono_time.h"
1312
#include "tox.h"
1413
#include "tox_options.h" // tox_log_cb
@@ -19,7 +18,7 @@ extern "C" {
1918
#endif
2019

2120
struct Tox {
22-
Messenger *m;
21+
struct Messenger *m;
2322
Mono_Time *mono_time;
2423
Tox_System sys;
2524
pthread_mutex_t *mutex;

0 commit comments

Comments
 (0)