@@ -615,27 +615,42 @@ bool add_to_list(
615615 Node_format * nodes_list , uint32_t length , const uint8_t pk [CRYPTO_PUBLIC_KEY_SIZE ],
616616 const IP_Port * ip_port , const uint8_t cmp_pk [CRYPTO_PUBLIC_KEY_SIZE ])
617617{
618- for (uint32_t i = 0 ; i < length ; ++ i ) {
619- Node_format * node = & nodes_list [i ];
618+ uint8_t pk_cur [CRYPTO_PUBLIC_KEY_SIZE ];
619+ memcpy (pk_cur , pk , CRYPTO_PUBLIC_KEY_SIZE );
620+ IP_Port ip_port_cur = * ip_port ;
620621
621- if (id_closest (cmp_pk , node -> public_key , pk ) == 2 ) {
622- uint8_t pk_bak [CRYPTO_PUBLIC_KEY_SIZE ];
623- memcpy (pk_bak , node -> public_key , CRYPTO_PUBLIC_KEY_SIZE );
622+ bool inserted = false;
623+ bool done = false;
624624
625- const IP_Port ip_port_bak = node -> ip_port ;
626- memcpy ( node -> public_key , pk , CRYPTO_PUBLIC_KEY_SIZE ) ;
625+ while (! done ) {
626+ done = true ;
627627
628- node -> ip_port = * ip_port ;
628+ for (uint32_t i = 0 ; i < length ; ++ i ) {
629+ Node_format * node = & nodes_list [i ];
629630
630- if (i != length - 1 ) {
631- add_to_list ( nodes_list , length , pk_bak , & ip_port_bak , cmp_pk ) ;
632- }
631+ if (id_closest ( cmp_pk , node -> public_key , pk_cur ) == 2 ) {
632+ uint8_t pk_bak [ CRYPTO_PUBLIC_KEY_SIZE ] ;
633+ memcpy ( pk_bak , node -> public_key , CRYPTO_PUBLIC_KEY_SIZE );
633634
634- return true;
635+ const IP_Port ip_port_bak = node -> ip_port ;
636+ memcpy (node -> public_key , pk_cur , CRYPTO_PUBLIC_KEY_SIZE );
637+
638+ node -> ip_port = ip_port_cur ;
639+
640+ if (i == length - 1 ) {
641+ return true;
642+ }
643+
644+ memcpy (pk_cur , pk_bak , CRYPTO_PUBLIC_KEY_SIZE );
645+ ip_port_cur = ip_port_bak ;
646+ done = false;
647+ inserted = true;
648+ break ;
649+ }
635650 }
636651 }
637652
638- return false ;
653+ return inserted ;
639654}
640655
641656/**
0 commit comments