@@ -620,27 +620,42 @@ bool add_to_list(
620620 Node_format * nodes_list , uint32_t length , const uint8_t pk [CRYPTO_PUBLIC_KEY_SIZE ],
621621 const IP_Port * ip_port , const uint8_t cmp_pk [CRYPTO_PUBLIC_KEY_SIZE ])
622622{
623- for (uint32_t i = 0 ; i < length ; ++ i ) {
624- Node_format * node = & nodes_list [i ];
623+ uint8_t pk_cur [CRYPTO_PUBLIC_KEY_SIZE ];
624+ memcpy (pk_cur , pk , CRYPTO_PUBLIC_KEY_SIZE );
625+ IP_Port ip_port_cur = * ip_port ;
625626
626- if (id_closest (cmp_pk , node -> public_key , pk ) == 2 ) {
627- uint8_t pk_bak [CRYPTO_PUBLIC_KEY_SIZE ];
628- memcpy (pk_bak , node -> public_key , CRYPTO_PUBLIC_KEY_SIZE );
627+ bool inserted = false;
628+ bool done = false;
629629
630- const IP_Port ip_port_bak = node -> ip_port ;
631- memcpy ( node -> public_key , pk , CRYPTO_PUBLIC_KEY_SIZE ) ;
630+ while (! done ) {
631+ done = true ;
632632
633- node -> ip_port = * ip_port ;
633+ for (uint32_t i = 0 ; i < length ; ++ i ) {
634+ Node_format * node = & nodes_list [i ];
634635
635- if (i != length - 1 ) {
636- add_to_list ( nodes_list , length , pk_bak , & ip_port_bak , cmp_pk ) ;
637- }
636+ if (id_closest ( cmp_pk , node -> public_key , pk_cur ) == 2 ) {
637+ uint8_t pk_bak [ CRYPTO_PUBLIC_KEY_SIZE ] ;
638+ memcpy ( pk_bak , node -> public_key , CRYPTO_PUBLIC_KEY_SIZE );
638639
639- return true;
640+ const IP_Port ip_port_bak = node -> ip_port ;
641+ memcpy (node -> public_key , pk_cur , CRYPTO_PUBLIC_KEY_SIZE );
642+
643+ node -> ip_port = ip_port_cur ;
644+
645+ if (i == length - 1 ) {
646+ return true;
647+ }
648+
649+ memcpy (pk_cur , pk_bak , CRYPTO_PUBLIC_KEY_SIZE );
650+ ip_port_cur = ip_port_bak ;
651+ done = false;
652+ inserted = true;
653+ break ;
654+ }
640655 }
641656 }
642657
643- return false ;
658+ return inserted ;
644659}
645660
646661/**
0 commit comments