diff --git a/src/route_bgp.c b/src/route_bgp.c index cb94de7..fc2f287 100644 --- a/src/route_bgp.c +++ b/src/route_bgp.c @@ -88,9 +88,9 @@ static void route_bgp_broadcast_route(struct ROUTE_BGP* bgp, const struct ROUTE_ } char net_buf[16]; - format_ip(route->network, net_buf); + format_ip(route->peer_info.network, net_buf); DEBUG_INFO(DEBUG_CATEGORY_BGP, "Broadcasted route %s/%d to %d connections", - net_buf, route->prefix_length, sent_count); + net_buf, route->peer_info.prefix_length, sent_count); } /** @@ -128,11 +128,11 @@ static void route_bgp_send_route(struct ROUTE_BGP* bgp, struct ETCP_CONN* conn, pkt->cmd = ETCP_ID_ROUTE_ENTRY; pkt->subcmd = ROUTE_SUBCMD_ENTRY; - pkt->peer_info.node_id = htobe64(route->node_id); - pkt->peer_info.network = htonl(route->network); - pkt->peer_info.prefix_length = route->prefix_length; - pkt->peer_info.hop_count = route->hop_count; - pkt->peer_info.latency = htons(route->latency); + pkt->peer_info.node_id = htobe64(route->peer_info.node_id); + pkt->peer_info.network = htonl(route->peer_info.network); + pkt->peer_info.prefix_length = route->peer_info.prefix_length; + pkt->peer_info.hop_count = route->peer_info.hop_count; + pkt->peer_info.latency = htons(route->peer_info.latency); // Add other fields if present, e.g., bandwidth struct ll_entry *send_entry = queue_entry_new(0); @@ -163,11 +163,11 @@ static void route_bgp_send_reroute(struct ROUTE_BGP* bgp, struct ETCP_CONN* conn pkt->cmd = ETCP_ID_ROUTE_ENTRY; pkt->subcmd = ROUTE_SUBCMD_ENTRY_REROUTE; pkt->old_node_id = htobe64(old_node_id); - pkt->peer_info.node_id = htobe64(route->node_id); - pkt->peer_info.network = htonl(route->network); - pkt->peer_info.prefix_length = route->prefix_length; - pkt->peer_info.hop_count = route->hop_count; - pkt->peer_info.latency = htons(route->latency); + pkt->peer_info.node_id = htobe64(route->peer_info.node_id); + pkt->peer_info.network = htonl(route->peer_info.network); + pkt->peer_info.prefix_length = route->peer_info.prefix_length; + pkt->peer_info.hop_count = route->peer_info.hop_count; + pkt->peer_info.latency = htons(route->peer_info.latency); struct ll_entry *send_entry = queue_entry_new(0); if (!send_entry) { @@ -227,7 +227,7 @@ static void route_bgp_send_withdraw_for_conn(struct ROUTE_BGP* bgp, struct ETCP_ for (size_t i = 0; i < table->count; i++) { if (table->entries[i].next_hop == conn) { - uint64_t nid = table->entries[i].node_id; + uint64_t nid = table->entries[i].peer_info.node_id; found = false; for (size_t j = 0; j < unique_count; j++) { if (unique_nodes[j] == nid) { @@ -344,15 +344,15 @@ static void route_bgp_receive_cbk(struct ETCP_CONN* from_conn, struct ll_entry* struct ROUTE_ENTRY new_route; memset(&new_route, 0, sizeof(new_route)); - new_route.node_id = node_id; - new_route.network = network; - new_route.prefix_length = prefix_length; + new_route.peer_info.node_id = node_id; + new_route.peer_info.network = network; + new_route.peer_info.prefix_length = prefix_length; new_route.next_hop = from_conn; new_route.type = ROUTE_TYPE_LEARNED; new_route.flags = ROUTE_FLAG_ACTIVE | ROUTE_FLAG_LEARNED; - new_route.hop_count = hop_count + 1; - new_route.latency = latency + (from_conn->rtt_last * 10); - new_route.metrics.latency_ms = new_route.latency / 10; + new_route.peer_info.hop_count = hop_count + 1; + new_route.peer_info.latency = latency + (from_conn->rtt_last * 10); + new_route.metrics.latency_ms = new_route.peer_info.latency / 10; // Set defaults for other metrics new_route.metrics.bandwidth_kbps = 100000; // example default new_route.metrics.packet_loss_rate = 0; @@ -364,13 +364,13 @@ static void route_bgp_receive_cbk(struct ETCP_CONN* from_conn, struct ll_entry* for (size_t i = 0; i < table->count; i++) { struct ROUTE_ENTRY* existing = &table->entries[i]; - if (existing->network == network && existing->prefix_length == prefix_length) { + if (existing->peer_info.network == network && existing->peer_info.prefix_length == prefix_length) { exists = true; - if (new_route.hop_count < existing->hop_count) { + if (new_route.peer_info.hop_count < existing->peer_info.hop_count) { // Update to better route existing->next_hop = new_route.next_hop; - existing->hop_count = new_route.hop_count; - existing->latency = new_route.latency; + existing->peer_info.hop_count = new_route.peer_info.hop_count; + existing->peer_info.latency = new_route.peer_info.latency; existing->metrics = new_route.metrics; existing->last_update = get_time_tb(); if (table->change_callback) { @@ -415,9 +415,9 @@ static void route_bgp_receive_cbk(struct ETCP_CONN* from_conn, struct ll_entry* size_t i = 0; while (i < table->count) { struct ROUTE_ENTRY* rte = &table->entries[i]; - if (rte->node_id == withdrawn_node_id) { + if (rte->peer_info.node_id == withdrawn_node_id) { if (rte->next_hop == from_conn) { - route_table_delete_entry(table, rte->network, rte->prefix_length, from_conn); + route_table_delete_entry(table, rte->peer_info.network, rte->peer_info.prefix_length, from_conn); // Do not increment i, array shifted } else { has_alternative = true; @@ -431,7 +431,7 @@ static void route_bgp_receive_cbk(struct ETCP_CONN* from_conn, struct ll_entry* if (has_alternative) { // Send reroute for each remaining route to from_conn for (size_t j = 0; j < table->count; j++) { - if (table->entries[j].node_id == withdrawn_node_id) { + if (table->entries[j].peer_info.node_id == withdrawn_node_id) { route_bgp_send_reroute(bgp, from_conn, withdrawn_node_id, &table->entries[j]); } } @@ -458,7 +458,7 @@ static void route_bgp_receive_cbk(struct ETCP_CONN* from_conn, struct ll_entry* struct ROUTE_TABLE* table = instance->rt; for (size_t i = 0; i < table->count; i++) { - if (table->entries[i].node_id == target_node_id) { + if (table->entries[i].peer_info.node_id == target_node_id) { size_t size = sizeof(struct NODE_CONNS_INFO) + sizeof(struct NODE_CONN_INFO); struct NODE_CONNS_INFO *new_list = u_malloc(size); diff --git a/src/route_lib.c b/src/route_lib.c index edd1e34..adba497 100644 --- a/src/route_lib.c +++ b/src/route_lib.c @@ -5,7 +5,7 @@ #include "route_lib.h" #include "etcp_connections.h" -#include "u_async.h" +#include "../lib/u_async.h" #include "../lib/debug_config.h" #include #include @@ -123,10 +123,10 @@ bool route_update_quality(struct ROUTE_ENTRY *entry) { // Простая формула: latency * 2 + loss * 10 + hops * 50 // Меньше значение = лучше маршрут // Используем latency из PEER_INFO (x0.1 ms) -> конвертируем в ms - uint16_t latency_ms = entry->latency / 10; + uint16_t latency_ms = entry->peer_info.latency / 10; entry->metrics.metric = latency_ms * 2 + entry->metrics.packet_loss_rate * 10 + - entry->hop_count * 50; + entry->peer_info.hop_count * 50; return true; } @@ -144,8 +144,8 @@ static int compare_routes_by_quality(const void *a, const void *b) { } // Затем по длине префикса (больше - лучше) - if (route_b->prefix_length != route_a->prefix_length) { - return route_b->prefix_length - route_a->prefix_length; + if (route_b->peer_info.prefix_length != route_a->peer_info.prefix_length) { + return route_b->peer_info.prefix_length - route_a->peer_info.prefix_length; } return 0; @@ -300,8 +300,8 @@ bool route_table_insert(struct ROUTE_TABLE *table, const struct ROUTE_ENTRY *ent } char ip_str[16]; - ip_to_string(entry->network, ip_str); - DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "Inserted route: %s/%d (type: %d)", ip_str, entry->prefix_length, entry->type); + ip_to_string(entry->peer_info.network, ip_str); + DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "Inserted route: %s/%d (type: %d)", ip_str, entry->peer_info.prefix_length, entry->type); return true; } @@ -355,8 +355,8 @@ bool route_table_delete_entry(struct ROUTE_TABLE *table, uint32_t network, if (!table) return false; for (size_t i = 0; i < table->count; i++) { - if (table->entries[i].network == network && - table->entries[i].prefix_length == prefix_length && + if (table->entries[i].peer_info.network == network && + table->entries[i].peer_info.prefix_length == prefix_length && (next_hop == NULL || table->entries[i].next_hop == next_hop)) { // Call change callback @@ -418,8 +418,8 @@ struct ROUTE_ARRAY* route_table_lookup(struct ROUTE_TABLE *table, uint32_t dest_ struct ROUTE_ENTRY* entry = &table->entries[i]; // Calculate netmask - uint32_t netmask = (uint32_t)(~0) << (32 - entry->prefix_length); - uint32_t entry_net = entry->network & netmask; + uint32_t netmask = (uint32_t)(~0) << (32 - entry->peer_info.prefix_length); + uint32_t entry_net = entry->peer_info.network & netmask; uint32_t dest_net = dest_ip & netmask; if (entry_net == dest_net && (entry->flags & ROUTE_FLAG_ACTIVE)) { @@ -499,7 +499,7 @@ bool route_get_all_routes(const struct ROUTE_TABLE *table, uint32_t network, uin // Count matching routes for (size_t i = 0; i < table->count; i++) { - if (table->entries[i].network == network && table->entries[i].prefix_length == prefix_length) { + if (table->entries[i].peer_info.network == network && table->entries[i].peer_info.prefix_length == prefix_length) { (*count)++; } } @@ -516,7 +516,7 @@ bool route_get_all_routes(const struct ROUTE_TABLE *table, uint32_t network, uin // Copy matching routes size_t result_index = 0; for (size_t i = 0; i < table->count; i++) { - if (table->entries[i].network == network && table->entries[i].prefix_length == prefix_length) { + if (table->entries[i].peer_info.network == network && table->entries[i].peer_info.prefix_length == prefix_length) { (*routes)[result_index] = table->entries[i]; result_index++; } @@ -548,10 +548,10 @@ void route_table_print(const struct ROUTE_TABLE *table) { const struct ROUTE_ENTRY *entry = &table->entries[i]; char network_str[16]; - ip_to_string(entry->network, network_str); + ip_to_string(entry->peer_info.network, network_str); DEBUG_INFO(DEBUG_CATEGORY_ROUTING, " %zu: %s/%d -> %p [%s] (quality: %u)", - i + 1, network_str, entry->prefix_length, + i + 1, network_str, entry->peer_info.prefix_length, (void*)entry->next_hop, route_type_to_string(entry->type), entry->metrics.metric); @@ -560,7 +560,7 @@ void route_table_print(const struct ROUTE_TABLE *table) { entry->metrics.bandwidth_kbps, entry->metrics.packet_loss_rate / 100.0, entry->metrics.latency_ms, - entry->hop_count); + entry->peer_info.hop_count); DEBUG_INFO(DEBUG_CATEGORY_ROUTING, " Flags: %s%s%s%s", (entry->flags & ROUTE_FLAG_ACTIVE) ? "ACTIVE " : "", diff --git a/src/route_lib.h b/src/route_lib.h index ea8798b..fcf33dc 100644 --- a/src/route_lib.h +++ b/src/route_lib.h @@ -93,7 +93,7 @@ typedef void (*route_change_callback_fn)(struct ROUTE_TABLE* table, * Структура представляет собой отдельную запись в таблице маршрутизации с детальной информацией о маршруте. */ struct ROUTE_ENTRY { - struct ROUTE_PEER_INFO;// сюда перенесли часть полей: + struct ROUTE_PEER_INFO peer_info;// сюда перенесли часть полей: // uint32_t network; /**< Сетевой адрес */ moved // uint8_t prefix_length; /**< Длина префикса подсети */ moved struct ETCP_CONN* next_hop; // Указатель на подключение до следующего хопа diff --git a/src/utun_instance.c b/src/utun_instance.c index 355481e..3c756f5 100644 --- a/src/utun_instance.c +++ b/src/utun_instance.c @@ -70,27 +70,27 @@ static int instance_init_common(struct UTUN_INSTANCE* instance, struct UASYNC* u struct CFG_ROUTE_ENTRY* subnet = config->my_subnets; while (subnet) { struct ROUTE_ENTRY entry = {0}; - entry.network = ntohl(subnet->ip.addr.v4.s_addr); // Convert network byte order to host - entry.prefix_length = subnet->netmask; + entry.peer_info.network = ntohl(subnet->ip.addr.v4.s_addr); // Convert network byte order to host + entry.peer_info.prefix_length = subnet->netmask; entry.next_hop = NULL; // Local route entry.type = ROUTE_TYPE_LOCAL; entry.flags = ROUTE_FLAG_ACTIVE | ROUTE_FLAG_VALIDATED; entry.metrics.bandwidth_kbps = UINT32_MAX; // Unlimited entry.metrics.latency_ms = 0; entry.metrics.packet_loss_rate = 0; - entry.hop_count = 0; - entry.node_id = instance->node_id; + entry.peer_info.hop_count = 0; + entry.peer_info.node_id = instance->node_id; if (route_table_insert(instance->rt, &entry)) { struct in_addr addr; - addr.s_addr = htonl(entry.network); + addr.s_addr = htonl(entry.peer_info.network); DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "Added local route: %s/%d", - ip_to_str(&addr, AF_INET).str, entry.prefix_length); + ip_to_str(&addr, AF_INET).str, entry.peer_info.prefix_length); } else { struct in_addr addr; - addr.s_addr = htonl(entry.network); + addr.s_addr = htonl(entry.peer_info.network); DEBUG_WARN(DEBUG_CATEGORY_ROUTING, "Failed to add local route: %s/%d (skipping)", - ip_to_str(&addr, AF_INET).str, entry.prefix_length); + ip_to_str(&addr, AF_INET).str, entry.peer_info.prefix_length); } subnet = subnet->next; diff --git a/tests/test_bgp_route_exchange.c b/tests/test_bgp_route_exchange.c index 7de02bb..6d7cf41 100644 --- a/tests/test_bgp_route_exchange.c +++ b/tests/test_bgp_route_exchange.c @@ -193,7 +193,7 @@ static void print_routing_table(struct UTUN_INSTANCE* inst, const char* name) { struct ROUTE_ENTRY* entry = &inst->rt->entries[i]; char network_str[16]; struct in_addr addr; - addr.s_addr = htonl(entry->network); + addr.s_addr = htonl(entry->peer_info.network); inet_ntop(AF_INET, &addr, network_str, sizeof(network_str)); const char* type_str = "UNKNOWN"; @@ -205,9 +205,9 @@ static void print_routing_table(struct UTUN_INSTANCE* inst, const char* name) { } DEBUG_INFO(DEBUG_CATEGORY_ROUTING, " Route %zu: %s/%d [%s] node_id=%016llX hops=%d", - i + 1, network_str, entry->prefix_length, type_str, - (unsigned long long)entry->node_id, - entry->hop_count); + i + 1, network_str, entry->peer_info.prefix_length, type_str, + (unsigned long long)entry->peer_info.node_id, + entry->peer_info.hop_count); } DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "=====================================\n"); } @@ -222,10 +222,10 @@ static int check_learned_route(struct UTUN_INSTANCE* inst, uint32_t network, for (size_t i = 0; i < inst->rt->count; i++) { struct ROUTE_ENTRY* entry = &inst->rt->entries[i]; - if (entry->network == network && - entry->prefix_length == prefix_len && + if (entry->peer_info.network == network && + entry->peer_info.prefix_length == prefix_len && entry->type == ROUTE_TYPE_LEARNED && - entry->node_id == expected_node_id) { + entry->peer_info.node_id == expected_node_id) { return 1; } } diff --git a/tests/test_route_lib.c b/tests/test_route_lib.c index eb03732..64c7bde 100644 --- a/tests/test_route_lib.c +++ b/tests/test_route_lib.c @@ -46,14 +46,14 @@ static void create_test_route(struct ROUTE_ENTRY *entry, uint32_t network, uint8_t prefix_len, route_type_t type, struct ETCP_CONN *next_hop) { memset(entry, 0, sizeof(*entry)); - entry->network = network; - entry->prefix_length = prefix_len; + entry->peer_info.network = network; + entry->peer_info.prefix_length = prefix_len; entry->type = type; entry->next_hop = next_hop; entry->flags = ROUTE_FLAG_ACTIVE; entry->metrics.latency_ms = 10 + (rand() % 100); entry->metrics.packet_loss_rate = rand() % 50; - entry->hop_count = 1 + (rand() % 5); + entry->peer_info.hop_count = 1 + (rand() % 5); entry->metrics.bandwidth_kbps = 1000 + (rand() % 9000); route_update_quality(entry); } @@ -124,7 +124,7 @@ static void test_route_lookup_basic(void) { struct ROUTE_ARRAY *result = route_table_lookup(table, 0xC0A80164); ASSERT(result != NULL, "should find route for 192.168.1.100"); ASSERT_EQ(result->routes, 1, "should find exactly 1 route"); - ASSERT_EQ(result->entries[0]->network, 0xC0A80100, "wrong network"); + ASSERT_EQ(result->entries[0]->peer_info.network, 0xC0A80100, "wrong network"); route_cache_clear(); route_table_destroy(table); @@ -170,8 +170,8 @@ static void test_longest_prefix_match(void) { int found_16 = 0, found_24 = 0; for (uint32_t i = 0; i < result->routes; i++) { - if (result->entries[i]->prefix_length == 16) found_16 = 1; - if (result->entries[i]->prefix_length == 24) found_24 = 1; + if (result->entries[i]->peer_info.prefix_length == 16) found_16 = 1; + if (result->entries[i]->peer_info.prefix_length == 24) found_24 = 1; } ASSERT(found_16 && found_24, "should find both prefix lengths"); @@ -251,9 +251,9 @@ static void test_update_quality(void) { struct ROUTE_ENTRY entry; memset(&entry, 0, sizeof(entry)); - entry.latency = 100; // x0.1 ms units (= 10 ms) + entry.peer_info.latency = 100; // x0.1 ms units (= 10 ms) entry.metrics.packet_loss_rate = 0; - entry.hop_count = 1; + entry.peer_info.hop_count = 1; ASSERT(route_update_quality(&entry), "failed to update quality"); @@ -261,9 +261,9 @@ static void test_update_quality(void) { struct ROUTE_ENTRY entry2; memset(&entry2, 0, sizeof(entry2)); - entry2.latency = 50; // x0.1 ms units (= 5 ms) + entry2.peer_info.latency = 50; // x0.1 ms units (= 5 ms) entry2.metrics.packet_loss_rate = 0; - entry2.hop_count = 1; + entry2.peer_info.hop_count = 1; route_update_quality(&entry2); ASSERT(entry2.metrics.metric < entry.metrics.metric, @@ -542,12 +542,12 @@ static void test_route_withdraw_scenario(void) { // Имитируем получение маршрута от пира (learned route) memset(&entry, 0, sizeof(entry)); - entry.network = 0x0A000000; // 10.0.0.0 - entry.prefix_length = 8; + entry.peer_info.network = 0x0A000000; // 10.0.0.0 + entry.peer_info.prefix_length = 8; entry.next_hop = peer; entry.type = ROUTE_TYPE_LEARNED; entry.flags = ROUTE_FLAG_ACTIVE | ROUTE_FLAG_LEARNED; - entry.node_id = 0x1234567890ABCDEFULL; + entry.peer_info.node_id = 0x1234567890ABCDEFULL; ASSERT(route_table_insert(table, &entry), "failed to insert learned route"); ASSERT_EQ(table->stats.learned_routes, 1, "should have 1 learned route");