2424 *****************************************************/
2525
2626struct 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+
33102static 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}
106175static 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
111181bool 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(
120193static 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
219295void 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}
0 commit comments