@@ -422,7 +422,7 @@ int packed_node_size(Family ip_family)
422422 * Returns size of packed IP_Port data on success
423423 * Return -1 on failure.
424424 */
425- int pack_ip_port (uint8_t * data , uint16_t length , const IP_Port * ip_port )
425+ int pack_ip_port (const Logger * logger , uint8_t * data , uint16_t length , const IP_Port * ip_port )
426426{
427427 if (data == nullptr ) {
428428 return -1 ;
@@ -445,6 +445,10 @@ int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port)
445445 is_ipv4 = false;
446446 net_family = TOX_TCP_INET6 ;
447447 } else {
448+ char ip_str [IP_NTOA_LEN ];
449+ // TODO(iphydf): Find out why we're trying to pack invalid IPs, stop
450+ // doing that, and turn this into an error.
451+ LOGGER_TRACE (logger , "cannot pack invalid IP: %s" , ip_ntoa (& ip_port -> ip , ip_str , sizeof (ip_str )));
448452 return -1 ;
449453 }
450454
@@ -576,12 +580,12 @@ int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool
576580 * return length of packed nodes on success.
577581 * return -1 on failure.
578582 */
579- int pack_nodes (uint8_t * data , uint16_t length , const Node_format * nodes , uint16_t number )
583+ int pack_nodes (const Logger * logger , uint8_t * data , uint16_t length , const Node_format * nodes , uint16_t number )
580584{
581585 uint32_t packed_length = 0 ;
582586
583587 for (uint32_t i = 0 ; i < number && packed_length < length ; ++ i ) {
584- const int ipp_size = pack_ip_port (data + packed_length , length - packed_length , & nodes [i ].ip_port );
588+ const int ipp_size = pack_ip_port (logger , data + packed_length , length - packed_length , & nodes [i ].ip_port );
585589
586590 if (ipp_size == -1 ) {
587591 return -1 ;
@@ -1325,7 +1329,7 @@ bool dht_getnodes(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, c
13251329 memcpy (receiver .public_key , public_key , CRYPTO_PUBLIC_KEY_SIZE );
13261330 receiver .ip_port = * ip_port ;
13271331
1328- if (pack_nodes (plain_message , sizeof (plain_message ), & receiver , 1 ) == -1 ) {
1332+ if (pack_nodes (dht -> log , plain_message , sizeof (plain_message ), & receiver , 1 ) == -1 ) {
13291333 return false;
13301334 }
13311335
@@ -1334,6 +1338,7 @@ bool dht_getnodes(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, c
13341338 ping_id = ping_array_add (dht -> dht_ping_array , dht -> mono_time , plain_message , sizeof (receiver ));
13351339
13361340 if (ping_id == 0 ) {
1341+ LOGGER_ERROR (dht -> log , "adding ping id failed" );
13371342 return false;
13381343 }
13391344
@@ -1352,6 +1357,7 @@ bool dht_getnodes(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, c
13521357 crypto_memzero (shared_key , sizeof (shared_key ));
13531358
13541359 if (len != sizeof (data )) {
1360+ LOGGER_ERROR (dht -> log , "getnodes packet encryption failed" );
13551361 return false;
13561362 }
13571363
@@ -1383,7 +1389,7 @@ static int sendnodes_ipv6(const DHT *dht, const IP_Port *ip_port, const uint8_t
13831389 int nodes_length = 0 ;
13841390
13851391 if (num_nodes > 0 ) {
1386- nodes_length = pack_nodes (plain + 1 , node_format_size * MAX_SENT_NODES , nodes_list , num_nodes );
1392+ nodes_length = pack_nodes (dht -> log , plain + 1 , node_format_size * MAX_SENT_NODES , nodes_list , num_nodes );
13871393
13881394 if (nodes_length <= 0 ) {
13891395 return -1 ;
@@ -1841,9 +1847,14 @@ static void do_Close(DHT *dht)
18411847 }
18421848}
18431849
1844- void dht_bootstrap (DHT * dht , const IP_Port * ip_port , const uint8_t * public_key )
1850+ bool dht_bootstrap (DHT * dht , const IP_Port * ip_port , const uint8_t * public_key )
18451851{
1846- dht_getnodes (dht , ip_port , public_key , dht -> self_public_key );
1852+ if (id_equal (public_key , dht -> self_public_key )) {
1853+ // Bootstrapping off ourselves is ok (onion paths are still set up).
1854+ return true;
1855+ }
1856+
1857+ return dht_getnodes (dht , ip_port , public_key , dht -> self_public_key );
18471858}
18481859
18491860int dht_bootstrap_from_address (DHT * dht , const char * address , bool ipv6enabled ,
@@ -2682,8 +2693,7 @@ void dht_save(const DHT *dht, uint8_t *data)
26822693 }
26832694 }
26842695
2685- state_write_section_header (old_data , DHT_STATE_COOKIE_TYPE , pack_nodes (data , sizeof (Node_format ) * num , clients , num ),
2686- DHT_STATE_TYPE_NODES );
2696+ state_write_section_header (old_data , DHT_STATE_COOKIE_TYPE , pack_nodes (dht -> log , data , sizeof (Node_format ) * num , clients , num ), DHT_STATE_TYPE_NODES );
26872697
26882698 free (clients );
26892699}
0 commit comments