route redesign + tests
This commit is contained in:
1 parent
027ccb262b
commit
b72d89da2d
13 files changed
+524
-324
No files matched your search
+69
-96
@@ -12,103 +12,7 @@
|
||||
#include "route_bgp.h"
|
||||
#include "etcp_debug.h"
|
||||
|
||||
// Build MY_NODEINFO packet for this instance
|
||||
int route_bgp_build_my_nodeinfo(struct UTUN_INSTANCE* instance, struct ROUTE_BGP* bgp) {
|
||||
if (!instance || !bgp) return -1;
|
||||
|
||||
// Count IPv4 sockets
|
||||
int v4_socket_count = 0;
|
||||
struct ETCP_SOCKET* sock = instance->etcp_sockets;
|
||||
while (sock) {
|
||||
if (sock->local_addr.ss_family == AF_INET) {
|
||||
v4_socket_count++;
|
||||
}
|
||||
sock = sock->next;
|
||||
}
|
||||
|
||||
// Count IPv4 routes (my_subnets)
|
||||
int v4_route_count = 0;
|
||||
struct CFG_ROUTE_ENTRY* subnet = instance->config->my_subnets;
|
||||
while (subnet) {
|
||||
if (subnet->ip.family == AF_INET) {
|
||||
v4_route_count++;
|
||||
}
|
||||
subnet = subnet->next;
|
||||
}
|
||||
|
||||
// Get name length
|
||||
size_t name_len = strlen(instance->name);
|
||||
|
||||
// Calculate total size
|
||||
size_t pkt_size = sizeof(struct BGP_NODEINFO_PACKET)
|
||||
+ name_len
|
||||
+ v4_socket_count * sizeof(struct NODEINFO_IPV4_SOCKET)
|
||||
+ v4_route_count * sizeof(struct NODEINFO_IPV4_SUBNET);
|
||||
|
||||
// Allocate
|
||||
struct BGP_NODEINFO_PACKET* pkt = u_malloc(pkt_size);
|
||||
if (!pkt) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "route_bgp_build_my_nodeinfo: malloc failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Fill fixed fields
|
||||
pkt->cmd = ETCP_ID_ROUTE_ENTRY;
|
||||
pkt->subcmd = ROUTE_SUBCMD_NODEINFO;
|
||||
pkt->node.node_id = htobe64(instance->node_id);
|
||||
pkt->node.ver = bgp->my_nodeinfo ? bgp->my_nodeinfo->node.ver + 1 : 1;
|
||||
memcpy(pkt->node.public_key, instance->my_keys.public_key, SC_PUBKEY_SIZE);
|
||||
pkt->node.node_name_len = (uint8_t)name_len;
|
||||
pkt->node.local_v4_sockets = (uint8_t)v4_socket_count;
|
||||
pkt->node.local_v6_sockets = 0;
|
||||
pkt->node.local_v4_subnets = (uint8_t)v4_route_count;
|
||||
pkt->node.local_v6_subnets = 0;
|
||||
pkt->node.tranzit_nodes = 0;
|
||||
pkt->node.hop_count = 0;
|
||||
|
||||
// Fill variable data
|
||||
uint8_t* ptr = (uint8_t*)pkt + sizeof(struct BGP_NODEINFO_PACKET);
|
||||
|
||||
// Name
|
||||
if (name_len > 0) {
|
||||
memcpy(ptr, instance->name, name_len);
|
||||
ptr += name_len;
|
||||
}
|
||||
|
||||
// IPv4 sockets
|
||||
struct NODEINFO_IPV4_SOCKET* sock_arr = (struct NODEINFO_IPV4_SOCKET*)ptr;
|
||||
sock = instance->etcp_sockets;
|
||||
while (sock) {
|
||||
if (sock->local_addr.ss_family == AF_INET) {
|
||||
struct sockaddr_in* in = (struct sockaddr_in*)&sock->local_addr;
|
||||
memcpy(sock_arr->addr, &in->sin_addr, 4);
|
||||
sock_arr->port = in->sin_port;
|
||||
sock_arr++;
|
||||
}
|
||||
sock = sock->next;
|
||||
}
|
||||
ptr = (uint8_t*)sock_arr;
|
||||
|
||||
// IPv4 routes
|
||||
struct NODEINFO_IPV4_SUBNET* route_arr = (struct NODEINFO_IPV4_SUBNET*)ptr;
|
||||
subnet = instance->config->my_subnets;
|
||||
while (subnet) {
|
||||
if (subnet->ip.family == AF_INET) {
|
||||
memcpy(route_arr->addr, &subnet->ip.addr.v4, 4);
|
||||
route_arr->prefix_length = subnet->netmask;
|
||||
route_arr++;
|
||||
}
|
||||
subnet = subnet->next;
|
||||
}
|
||||
|
||||
bgp->my_nodeinfo = pkt;
|
||||
bgp->my_nodeinfo_size = pkt_size;
|
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_BGP, "Built my_nodeinfo: name='%s', v4_sockets=%d, v4_routes=%d",
|
||||
instance->name, v4_socket_count, v4_route_count);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Получает указатель на массив IPv4-подсетей узла (без malloc/копирования).
|
||||
@@ -153,3 +57,72 @@ int get_node_routes(struct NODEINFO_Q *node, const struct NODEINFO_IPV4_SUBNET *
|
||||
(unsigned)info->local_v4_subnets, (void*)node);
|
||||
return (int)info->local_v4_subnets;
|
||||
}
|
||||
|
||||
int route_bgp_update_my_nodeinfo(struct UTUN_INSTANCE* instance, struct ROUTE_BGP* bgp) {
|
||||
if (!instance || !bgp) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "route_bgp_update_my_nodeinfo: invalid args");
|
||||
return -1;
|
||||
}
|
||||
if (!bgp->local_node) {
|
||||
int vc = 0;
|
||||
struct CFG_ROUTE_ENTRY* s = instance->config->my_subnets;
|
||||
while (s) {
|
||||
if (s->ip.family == AF_INET) vc++;
|
||||
s = s->next;
|
||||
}
|
||||
size_t d = vc * sizeof(struct NODEINFO_IPV4_SUBNET);
|
||||
bgp->local_node = u_calloc(1, sizeof(struct NODEINFO_Q) + d);
|
||||
if (!bgp->local_node) return -1;
|
||||
bgp->local_node->node.node_id = htobe64(instance->node_id);
|
||||
bgp->local_node->node.hop_count = 0;
|
||||
bgp->local_node->node.ver = 1;
|
||||
bgp->local_node->dirty = 1;
|
||||
bgp->local_node->last_ver = 1;
|
||||
bgp->local_node->node.local_v4_subnets = vc;
|
||||
}
|
||||
int vc = 0;
|
||||
struct CFG_ROUTE_ENTRY* s = instance->config->my_subnets;
|
||||
while (s) {
|
||||
if (s->ip.family == AF_INET) vc++;
|
||||
s = s->next;
|
||||
}
|
||||
int changed = (vc != (int)bgp->local_node->node.local_v4_subnets);
|
||||
if (!changed && vc > 0) {
|
||||
uint8_t* current = (uint8_t*)&bgp->local_node->node + sizeof(struct NODEINFO);
|
||||
struct NODEINFO_IPV4_SUBNET* ra = (struct NODEINFO_IPV4_SUBNET*)current;
|
||||
s = instance->config->my_subnets;
|
||||
bool same = true;
|
||||
while (s) {
|
||||
if (s->ip.family == AF_INET) {
|
||||
if (memcmp(ra->addr, &s->ip.addr.v4, 4) != 0 || ra->prefix_length != s->netmask) {
|
||||
same = false;
|
||||
break;
|
||||
}
|
||||
ra++;
|
||||
}
|
||||
s = s->next;
|
||||
}
|
||||
changed = !same;
|
||||
}
|
||||
if (changed) {
|
||||
uint8_t oldv = bgp->local_node->node.ver;
|
||||
bgp->local_node->node.ver = ((oldv + 1) % 255) + 1;
|
||||
bgp->local_node->node.local_v4_subnets = vc;
|
||||
bgp->local_node->dirty = 1;
|
||||
bgp->local_node->last_ver = bgp->local_node->node.ver;
|
||||
uint8_t* dp = (uint8_t*)&bgp->local_node->node + sizeof(struct NODEINFO);
|
||||
struct NODEINFO_IPV4_SUBNET* ra = (struct NODEINFO_IPV4_SUBNET*)dp;
|
||||
s = instance->config->my_subnets;
|
||||
while (s) {
|
||||
if (s->ip.family == AF_INET) {
|
||||
memcpy(ra->addr, &s->ip.addr.v4, 4);
|
||||
ra->prefix_length = s->netmask;
|
||||
ra++;
|
||||
}
|
||||
s = s->next;
|
||||
}
|
||||
} else {
|
||||
bgp->local_node->last_ver = bgp->local_node->node.ver;
|
||||
}
|
||||
return vc;
|
||||
}
|
||||
Reference in new issue
Block a user