@ -1,5 +1,6 @@
# include "route_lib.h"
# include "etcp.h"
# include "etcp_debug.h"
# include "../lib/debug_config.h"
# include "../lib/mem.h"
# include "../lib/platform_compat.h"
@ -15,14 +16,6 @@
// Вспомогательные функции (без изменений)
// ============================================================================
static char * ip_to_string ( uint32_t ip , char * buffer ) {
if ( ! buffer ) return NULL ;
struct in_addr addr ;
addr . s_addr = htonl ( ip ) ;
snprintf ( buffer , 16 , " %s " , inet_ntoa ( addr ) ) ;
return buffer ;
}
static uint32_t prefix_to_mask ( uint8_t prefix ) {
if ( prefix = = 0 ) return 0 ;
return ~ ( ( 1U < < ( 32 - prefix ) ) - 1 ) ;
@ -372,16 +365,14 @@ bool route_insert(struct ROUTE_TABLE *table,
DEBUG_ERROR ( DEBUG_CATEGORY_ROUTING , " route_insert: entry is NULL " ) ;
return false ;
}
DEBUG_TRACE ( DEBUG_CATEGORY_ROUTING , " route_insert: network=%08X prefix=%d conn=%p node_id=%llu " ,
entry - > network , entry - > prefix_length , ( void * ) conn , ( unsigned long long ) node_id ) ;
DEBUG_INFO ( DEBUG_CATEGORY_ROUTING , " route_insert: network=%s prefix=%d conn=%p node_id=%016llx " ,
ip_to_string ( entry - > network ) . a , entry - > prefix_length , ( void * ) conn , ( unsigned long long ) node_id ) ;
// === 0. Loop prevention — ВСЕГДА (исправление проблемы #3) ===
if ( conn & & my_node_id & & hop_list & & hop_count > 0 ) {
for ( uint8_t i = 0 ; i < hop_count ; i + + ) {
if ( hop_list [ i ] = = my_node_id ) {
char ip_str [ 16 ] ;
ip_to_string ( entry - > network , ip_str ) ;
DEBUG_WARN ( DEBUG_CATEGORY_ROUTING , " Loop detected: %s/%d " , ip_str , entry - > prefix_length ) ;
DEBUG_WARN ( DEBUG_CATEGORY_ROUTING , " Loop detected: %s/%d " , ip_to_string ( entry - > network ) . a , entry - > prefix_length ) ;
DEBUG_TRACE ( DEBUG_CATEGORY_ROUTING , " route_insert: REJECTED (loop detected) " ) ;
return false ;
}
@ -397,9 +388,7 @@ bool route_insert(struct ROUTE_TABLE *table,
if ( existing_local ! = new_local | |
( ! existing_local & & existing - > conn_list - > node_id ! = node_id ) ) {
char ip_str [ 16 ] ;
ip_to_string ( entry - > network , ip_str ) ;
DEBUG_WARN ( DEBUG_CATEGORY_ROUTING , " Route conflict (different owner): %s/%d " , ip_str , entry - > prefix_length ) ;
DEBUG_WARN ( DEBUG_CATEGORY_ROUTING , " Route conflict (different owner): %s/%d " , ip_to_string ( entry - > network ) . a , entry - > prefix_length ) ;
DEBUG_TRACE ( DEBUG_CATEGORY_ROUTING , " route_insert: REJECTED (conflict) " ) ;
return false ;
}
@ -409,7 +398,7 @@ bool route_insert(struct ROUTE_TABLE *table,
existing - > last_update = get_time_tb ( ) ;
if ( table - > change_callback )
table - > change_callback ( table , existing , 1 , table - > change_callback_arg ) ;
DEBUG_TRACE ( DEBUG_CATEGORY_ROUTING , " route_insert: UPDATED local route %08X /%d " , entry - > network , entry - > prefix_length ) ;
DEBUG_TRACE ( DEBUG_CATEGORY_ROUTING , " route_insert: UPDATED local route %s /%d " , ip_to_string ( entry - > network ) . a , entry - > prefix_length ) ;
return true ;
}
@ -428,17 +417,15 @@ bool route_insert(struct ROUTE_TABLE *table,
if ( table - > change_callback )
table - > change_callback ( table , existing , 1 , table - > change_callback_arg ) ;
DEBUG_TRACE ( DEBUG_CATEGORY_ROUTING , " route_insert: UPDATED learned route %08X/%d node_id=%llu " ,
entry - > network , entry - > prefix_length , ( unsigned long long ) node_id ) ;
DEBUG_INFO ( DEBUG_CATEGORY_ROUTING , " route_insert: UPDATED learned route %s/%d node_id=%016llx " ,
ip_to_string ( entry - > network ) . a , entry - > prefix_length , ( unsigned long long ) node_id ) ;
return true ;
}
// === 2. Новый префикс ===
if ( check_route_overlap_in_table ( entry - > network , entry - > prefix_length ,
table - > entries , table - > count ) ) {
char ip_str [ 16 ] ;
ip_to_string ( entry - > network , ip_str ) ;
DEBUG_WARN ( DEBUG_CATEGORY_ROUTING , " Route overlap rejected: %s/%d " , ip_str , entry - > prefix_length ) ;
DEBUG_WARN ( DEBUG_CATEGORY_ROUTING , " Route overlap rejected: %s/%d " , ip_to_string ( entry - > network ) . a , entry - > prefix_length ) ;
DEBUG_TRACE ( DEBUG_CATEGORY_ROUTING , " route_insert: REJECTED (overlap) " ) ;
return false ;
}
@ -503,14 +490,12 @@ bool route_insert(struct ROUTE_TABLE *table,
}
// === 8. Отладочное сообщение ===
char ip_str [ 16 ] ;
ip_to_string ( entry - > network , ip_str ) ;
DEBUG_INFO ( DEBUG_CATEGORY_ROUTING , " Route %s: %s/%d (node %016llx) " ,
conn ? " learned " : " local " , ip_str , entry - > prefix_length ,
conn ? " learned " : " local " , ip_to_string ( entry - > network ) . a , entry - > prefix_length ,
( unsigned long long ) node_id ) ;
DEBUG_TRACE ( DEBUG_CATEGORY_ROUTING , " route_insert: INSERTED %s %08X /%d " ,
conn ? " learned " : " local " , entry - > network , entry - > prefix_length ) ;
DEBUG_TRACE ( DEBUG_CATEGORY_ROUTING , " route_insert: INSERTED %s %s /%d " ,
conn ? " learned " : " local " , ip_to_string ( entry - > network ) . a , entry - > prefix_length ) ;
return true ;
}
@ -603,7 +588,7 @@ bool route_remove_path(struct ROUTE_TABLE *table,
DEBUG_ERROR ( DEBUG_CATEGORY_ROUTING , " route_remove_path: conn is NULL " ) ;
return false ;
}
DEBUG_TRACE ( DEBUG_CATEGORY_ROUTING , " route_remove_path: node_id=%llu conn=%p " ,
DEBUG_INFO ( DEBUG_CATEGORY_ROUTING , " route_remove_path: node_id=%016llx conn=%p " ,
( unsigned long long ) node_id , ( void * ) conn ) ;
struct NODE_CONNS_INFO * info = find_node_conns ( table , node_id ) ;
@ -619,7 +604,7 @@ bool route_remove_path(struct ROUTE_TABLE *table,
if ( info - > conninfo_count = = 0 ) {
// Последний путь к узлу — полный withdraw
route_delete ( table , node_id ) ; // уже вызывает callback(2) и очищает память
DEBUG_TRACE ( DEBUG_CATEGORY_ROUTING , " route_remove_path: WITHDRAW node_id=%llu " , ( unsigned long long ) node_id ) ;
DEBUG_INFO ( DEBUG_CATEGORY_ROUTING , " route_remove_path: WITHDRAW node_id=%016llx " , ( unsigned long long ) node_id ) ;
return true ;
} else if ( was_preferred ) {
// Изменился preferred_conn — уведомляем о reroute для всех префиксов этого узла
@ -631,7 +616,7 @@ bool route_remove_path(struct ROUTE_TABLE *table,
table - > change_callback ( table , e , 1 , table - > change_callback_arg ) ;
}
}
DEBUG_TRACE ( DEBUG_CATEGORY_ROUTING , " route_remove_path: REROUTE node_id=%llu " , ( unsigned long long ) node_id ) ;
DEBUG_INFO ( DEBUG_CATEGORY_ROUTING , " route_remove_path: REROUTE node_id=%016llx " , ( unsigned long long ) node_id ) ;
}
return false ;
}
@ -641,7 +626,7 @@ void route_delete(struct ROUTE_TABLE *table, uint64_t node_id) {
DEBUG_ERROR ( DEBUG_CATEGORY_ROUTING , " route_delete: table is NULL " ) ;
return ;
}
DEBUG_TRACE ( DEBUG_CATEGORY_ROUTING , " route_delete: node_id=%llu " , ( unsigned long long ) node_id ) ;
DEBUG_INFO ( DEBUG_CATEGORY_ROUTING , " route_delete: node_id=%016llx " , ( unsigned long long ) node_id ) ;
size_t i = 0 ;
while ( i < table - > count ) {
@ -679,7 +664,7 @@ void route_delete(struct ROUTE_TABLE *table, uint64_t node_id) {
}
}
DEBUG_INFO ( DEBUG_CATEGORY_ROUTING , " Routes deleted for node %llu " , ( unsigned long long ) node_id ) ;
DEBUG_INFO ( DEBUG_CATEGORY_ROUTING , " Routes deleted for node %016llx " , ( unsigned long long ) node_id ) ;
}
struct ROUTE_ENTRY * route_lookup ( struct ROUTE_TABLE * table , uint32_t dest_ip ) {
@ -687,14 +672,14 @@ struct ROUTE_ENTRY* route_lookup(struct ROUTE_TABLE *table, uint32_t dest_ip) {
DEBUG_ERROR ( DEBUG_CATEGORY_ROUTING , " route_lookup: table is NULL " ) ;
return NULL ;
}
DEBUG_TRACE ( DEBUG_CATEGORY_ROUTING , " route_lookup: dest_ip=%08X " , dest_ip ) ;
DEBUG_TRACE ( DEBUG_CATEGORY_ROUTING , " route_lookup: dest_ip=%s " , ip_to_string ( dest_ip ) . a ) ;
struct ROUTE_ENTRY * result = binary_search_lpm ( table , dest_ip ) ;
if ( result ) {
table - > stats . routes_lookup_hits + + ;
DEBUG_TRACE ( DEBUG_CATEGORY_ROUTING , " route_lookup: FOUND %08X/%d node_id=%llu " ,
result - > network , result - > prefix_length ,
DEBUG_INFO ( DEBUG_CATEGORY_ROUTING , " route_lookup: FOUND %s/%d node_id=%016llx " ,
ip_to_string ( result - > network ) . a , result - > prefix_length ,
result - > conn_list ? ( unsigned long long ) result - > conn_list - > node_id : 0 ) ;
} else {
table - > stats . routes_lookup_misses + + ;
@ -716,13 +701,11 @@ void route_table_print(const struct ROUTE_TABLE *table) {
for ( size_t i = 0 ; i < table - > count ; i + + ) {
const struct ROUTE_ENTRY * entry = & table - > entries [ i ] ;
char network_str [ 16 ] ;
ip_to_string ( entry - > network , network_str ) ;
DEBUG_INFO ( DEBUG_CATEGORY_ROUTING , " %zu: %s/%d " , i + 1 , network_str , entry - > prefix_length ) ;
DEBUG_INFO ( DEBUG_CATEGORY_ROUTING , " %zu: %s/%d " , i + 1 , ip_to_string ( entry - > network ) . a , entry - > prefix_length ) ;
if ( entry - > conn_list ) {
DEBUG_INFO ( DEBUG_CATEGORY_ROUTING , " node_id=%llu , conns=%d, ref=%d " ,
DEBUG_INFO ( DEBUG_CATEGORY_ROUTING , " node_id=%016llx , conns=%d, ref=%d " ,
( unsigned long long ) entry - > conn_list - > node_id ,
entry - > conn_list - > conninfo_count ,
entry - > conn_list - > ref_count ) ;