Skip to content

Commit c2c61f5

Browse files
committed
feat: add source pubkey and ipport to dht nodes response cb/event
1 parent 1d79022 commit c2c61f5

6 files changed

Lines changed: 113 additions & 11 deletions

File tree

other/event_tooling/generate_event_c.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,9 @@ int main(int argc, char** argv) {
722722
{
723723
"Dht_Nodes_Response",
724724
{
725+
EventTypeByteArray{"src_public_key", "TOX_PUBLIC_KEY_SIZE"},
726+
EventTypeByteRange{"src_ip", "src_ip_length", "src_ip_length", "char", "uint32_t", true},
727+
EventTypeTrivial{"uint16_t", "src_port"},
725728
EventTypeByteArray{"public_key", "TOX_PUBLIC_KEY_SIZE"},
726729
EventTypeByteRange{"ip", "ip_length", "ip_length", "char", "uint32_t", true},
727730
EventTypeTrivial{"uint16_t", "port"},

toxcore/DHT.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1519,13 +1519,17 @@ static int handle_nodes_response(void *_Nonnull object, const IP_Port *_Nonnull
15191519
return 0;
15201520
}
15211521

1522+
Node_format src_node;
1523+
memcpy(src_node.public_key, packet+1, CRYPTO_PUBLIC_KEY_SIZE);
1524+
src_node.ip_port = *source;
1525+
15221526
for (uint32_t i = 0; i < num_nodes; ++i) {
15231527
if (ipport_isset(&plain_nodes[i].ip_port)) {
15241528
ping_node_from_nodes_response_ok(dht, plain_nodes[i].public_key, &plain_nodes[i].ip_port);
15251529
returnedip_ports(dht, &plain_nodes[i].ip_port, plain_nodes[i].public_key, packet + 1);
15261530

15271531
if (dht->nodes_response_callback != nullptr) {
1528-
dht->nodes_response_callback(dht, &plain_nodes[i], userdata);
1532+
dht->nodes_response_callback(dht, &src_node, &plain_nodes[i], userdata);
15291533
}
15301534
}
15311535
}

toxcore/DHT.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ bool dht_send_nodes_request(DHT *_Nonnull dht, const IP_Port *_Nonnull ip_port,
244244

245245
typedef void dht_ip_cb(void *_Nullable object, int32_t number, const IP_Port *_Nonnull ip_port);
246246

247-
typedef void dht_nodes_response_cb(const DHT *_Nonnull dht, const Node_format *_Nonnull node, void *_Nullable user_data);
247+
typedef void dht_nodes_response_cb(const DHT *_Nonnull dht, const Node_format *_Nonnull src_node, const Node_format *_Nonnull node, void *_Nullable user_data);
248248

249249
/** Sets the callback to be triggered on a nodes response. */
250250
void dht_callback_nodes_response(DHT *_Nonnull dht, dht_nodes_response_cb *_Nullable function);

toxcore/events/dht_nodes_response.c

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,81 @@
2424
*****************************************************/
2525

2626
struct Tox_Event_Dht_Nodes_Response {
27+
uint8_t src_public_key[TOX_PUBLIC_KEY_SIZE];
28+
char *_Nullable src_ip;
29+
uint32_t src_ip_length;
30+
uint16_t src_port;
2731
uint8_t public_key[TOX_PUBLIC_KEY_SIZE];
2832
char *_Nullable ip;
2933
uint32_t ip_length;
3034
uint16_t port;
3135
};
3236

37+
static bool tox_event_dht_nodes_response_set_src_public_key(Tox_Event_Dht_Nodes_Response *_Nonnull dht_nodes_response, const uint8_t src_public_key[TOX_PUBLIC_KEY_SIZE])
38+
{
39+
assert(dht_nodes_response != nullptr);
40+
memcpy(dht_nodes_response->src_public_key, src_public_key, TOX_PUBLIC_KEY_SIZE);
41+
return true;
42+
}
43+
const uint8_t *tox_event_dht_nodes_response_get_src_public_key(const Tox_Event_Dht_Nodes_Response *dht_nodes_response)
44+
{
45+
assert(dht_nodes_response != nullptr);
46+
return dht_nodes_response->src_public_key;
47+
}
48+
49+
static bool tox_event_dht_nodes_response_set_src_ip(Tox_Event_Dht_Nodes_Response *_Nonnull dht_nodes_response,
50+
const Memory *_Nonnull mem, const char *_Nullable src_ip, uint32_t src_ip_length)
51+
{
52+
assert(dht_nodes_response != nullptr);
53+
if (dht_nodes_response->src_ip != nullptr) {
54+
mem_delete(mem, dht_nodes_response->src_ip);
55+
dht_nodes_response->src_ip = nullptr;
56+
dht_nodes_response->src_ip_length = 0;
57+
}
58+
59+
if (src_ip == nullptr) {
60+
assert(src_ip_length == 0);
61+
return true;
62+
}
63+
64+
if (src_ip_length == UINT32_MAX) {
65+
return false;
66+
}
67+
68+
char *src_ip_copy = (char *)mem_balloc(mem, src_ip_length + 1);
69+
70+
if (src_ip_copy == nullptr) {
71+
return false;
72+
}
73+
74+
memcpy(src_ip_copy, src_ip, src_ip_length);
75+
src_ip_copy[src_ip_length] = 0;
76+
dht_nodes_response->src_ip = src_ip_copy;
77+
dht_nodes_response->src_ip_length = src_ip_length;
78+
return true;
79+
}
80+
uint32_t tox_event_dht_nodes_response_get_src_ip_length(const Tox_Event_Dht_Nodes_Response *dht_nodes_response)
81+
{
82+
assert(dht_nodes_response != nullptr);
83+
return dht_nodes_response->src_ip_length;
84+
}
85+
const char *tox_event_dht_nodes_response_get_src_ip(const Tox_Event_Dht_Nodes_Response *dht_nodes_response)
86+
{
87+
assert(dht_nodes_response != nullptr);
88+
return dht_nodes_response->src_ip;
89+
}
90+
91+
static void tox_event_dht_nodes_response_set_src_port(Tox_Event_Dht_Nodes_Response *_Nonnull dht_nodes_response, uint16_t src_port)
92+
{
93+
assert(dht_nodes_response != nullptr);
94+
dht_nodes_response->src_port = src_port;
95+
}
96+
uint16_t tox_event_dht_nodes_response_get_src_port(const Tox_Event_Dht_Nodes_Response *dht_nodes_response)
97+
{
98+
assert(dht_nodes_response != nullptr);
99+
return dht_nodes_response->src_port;
100+
}
101+
33102
static bool tox_event_dht_nodes_response_set_public_key(Tox_Event_Dht_Nodes_Response *_Nonnull dht_nodes_response, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE])
34103
{
35104
assert(dht_nodes_response != nullptr);
@@ -105,13 +174,17 @@ static void tox_event_dht_nodes_response_construct(Tox_Event_Dht_Nodes_Response
105174
}
106175
static void tox_event_dht_nodes_response_destruct(Tox_Event_Dht_Nodes_Response *_Nonnull dht_nodes_response, const Memory *_Nonnull mem)
107176
{
177+
mem_delete(mem, dht_nodes_response->src_ip);
108178
mem_delete(mem, dht_nodes_response->ip);
109179
}
110180

111181
bool tox_event_dht_nodes_response_pack(
112182
const Tox_Event_Dht_Nodes_Response *event, Bin_Pack *bp)
113183
{
114-
return bin_pack_array(bp, 3)
184+
return bin_pack_array(bp, 6)
185+
&& bin_pack_bin(bp, event->src_public_key, TOX_PUBLIC_KEY_SIZE)
186+
&& bin_pack_str(bp, event->src_ip, event->src_ip_length)
187+
&& bin_pack_u16(bp, event->src_port)
115188
&& bin_pack_bin(bp, event->public_key, TOX_PUBLIC_KEY_SIZE)
116189
&& bin_pack_str(bp, event->ip, event->ip_length)
117190
&& bin_pack_u16(bp, event->port);
@@ -120,11 +193,14 @@ bool tox_event_dht_nodes_response_pack(
120193
static bool tox_event_dht_nodes_response_unpack_into(Tox_Event_Dht_Nodes_Response *_Nonnull event, Bin_Unpack *_Nonnull bu)
121194
{
122195
assert(event != nullptr);
123-
if (!bin_unpack_array_fixed(bu, 3, nullptr)) {
196+
if (!bin_unpack_array_fixed(bu, 6, nullptr)) {
124197
return false;
125198
}
126199

127-
return bin_unpack_bin_fixed(bu, event->public_key, TOX_PUBLIC_KEY_SIZE)
200+
return bin_unpack_bin_fixed(bu, event->src_public_key, TOX_PUBLIC_KEY_SIZE)
201+
&& bin_unpack_str(bu, &event->src_ip, &event->src_ip_length)
202+
&& bin_unpack_u16(bu, &event->src_port)
203+
&& bin_unpack_bin_fixed(bu, event->public_key, TOX_PUBLIC_KEY_SIZE)
128204
&& bin_unpack_str(bu, &event->ip, &event->ip_length)
129205
&& bin_unpack_u16(bu, &event->port);
130206
}
@@ -218,6 +294,9 @@ static Tox_Event_Dht_Nodes_Response *_Nullable tox_event_dht_nodes_response_allo
218294

219295
void tox_events_handle_dht_nodes_response(
220296
Tox *tox,
297+
const uint8_t *src_public_key,
298+
const char *src_ip, uint32_t src_ip_length,
299+
uint16_t src_port,
221300
const uint8_t *public_key,
222301
const char *ip, uint32_t ip_length,
223302
uint16_t port,
@@ -230,6 +309,13 @@ void tox_events_handle_dht_nodes_response(
230309
return;
231310
}
232311

312+
tox_event_dht_nodes_response_set_src_public_key(dht_nodes_response, src_public_key);
313+
if (!tox_event_dht_nodes_response_set_src_ip(dht_nodes_response, state->mem, src_ip, src_ip_length)) {
314+
tox_event_dht_nodes_response_free(dht_nodes_response, state->mem);
315+
state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;
316+
return;
317+
}
318+
tox_event_dht_nodes_response_set_src_port(dht_nodes_response, src_port);
233319
tox_event_dht_nodes_response_set_public_key(dht_nodes_response, public_key);
234320
if (!tox_event_dht_nodes_response_set_ip(dht_nodes_response, state->mem, ip, ip_length)) {
235321
tox_event_dht_nodes_response_free(dht_nodes_response, state->mem);
@@ -246,6 +332,6 @@ void tox_events_handle_dht_nodes_response_dispatch(Tox *tox, const Tox_Event_Dht
246332
}
247333

248334
tox_unlock(tox);
249-
tox->dht_nodes_response_callback(tox, event->public_key, (const char *)event->ip, event->ip_length, event->port, user_data);
335+
tox->dht_nodes_response_callback(tox, event->src_public_key, (const char *)event->src_ip, event->src_ip_length, event->src_port, event->public_key, (const char *)event->ip, event->ip_length, event->port, user_data);
250336
tox_lock(tox);
251337
}

toxcore/tox.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,20 +326,26 @@ static void tox_conference_peer_list_changed_handler(Messenger *m, uint32_t conf
326326
}
327327

328328
static dht_nodes_response_cb tox_dht_nodes_response_handler;
329-
static void tox_dht_nodes_response_handler(const DHT *dht, const Node_format *node, void *user_data)
329+
static void tox_dht_nodes_response_handler(const DHT *dht, const Node_format *_Nonnull src_node,
330+
const Node_format *node, void *user_data)
330331
{
331332
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
332333
if (tox_data->tox->dht_nodes_response_callback == nullptr) {
333334
return;
334335
}
335336

337+
Ip_Ntoa src_ip_str;
338+
net_ip_ntoa(&src_node->ip_port.ip, &src_ip_str);
339+
336340
Ip_Ntoa ip_str;
337341
net_ip_ntoa(&node->ip_port.ip, &ip_str);
338342

339343
tox_unlock(tox_data->tox);
340344
tox_data->tox->dht_nodes_response_callback(
341-
tox_data->tox, node->public_key, ip_str.buf, ip_str.length, net_ntohs(node->ip_port.port),
342-
tox_data->user_data);
345+
tox_data->tox,
346+
src_node->public_key, src_ip_str.buf, src_ip_str.length, net_ntohs(src_node->ip_port.port),
347+
node->public_key, ip_str.buf, ip_str.length, net_ntohs(node->ip_port.port),
348+
tox_data->user_data);
343349
tox_lock(tox_data->tox);
344350
}
345351

toxcore/tox_private.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,16 @@ uint32_t tox_dht_node_ip_string_size(void);
138138
uint32_t tox_dht_node_public_key_size(void);
139139

140140
/**
141+
* @param src_public_key The source node's public key.
142+
* @param src_ip The source node's IP address, represented as a NUL-terminated C string.
143+
* @param src_port The source node's port.
141144
* @param public_key The node's public key.
142145
* @param ip The node's IP address, represented as a NUL-terminated C string.
143146
* @param port The node's port.
144147
*/
145148
typedef void tox_dht_nodes_response_cb(
146-
Tox *_Nonnull tox, const uint8_t *_Nonnull public_key, const char *_Nonnull ip, uint32_t ip_length,
147-
uint16_t port, void *_Nullable user_data);
149+
Tox *_Nonnull tox, const uint8_t *_Nonnull src_public_key, const char *_Nonnull src_ip, uint32_t src_ip_length, uint16_t src_port,
150+
const uint8_t *_Nonnull public_key, const char *_Nonnull ip, uint32_t ip_length, uint16_t port, void *_Nullable user_data);
148151

149152
/**
150153
* Set the callback for the `dht_nodes_response` event. Pass NULL to unset.

0 commit comments

Comments
 (0)