Browse Source

Исправлен byte order для IP адресов в NAT detection flow

- etcp_connections.c: парсинг NAT IP через memcpy (сохраняет NBO)
- route_bgp.c: убраны ntohl() для IP в NAT detection и route_bgp_send_nat_info
- route_ping.c: убран htonl() для IP в ping request
- тесты: обновлены ожидания NBO для nat_ip
congestion
Evgeny 5 months ago
parent
commit
0984ea749a
  1. 4
      src/etcp_connections.c
  2. 22
      src/route_bgp.c
  3. 5
      src/route_ping.c
  4. 8
      tests/test_etcp_two_instances.c
  5. 20
      tests/test_nat_detection.c
  6. 6
      tests/test_route_ping.c

4
src/etcp_connections.c

@ -1497,8 +1497,8 @@ process_decrypted:
// Parse NAT IP:port from response (new format includes 4+2 bytes)
if (pkt_len >= 19) {
uint32_t new_nat_ip = (pkt->data[offset+3] << 24) | (pkt->data[offset+2] << 16) |
(pkt->data[offset+1] << 8) | pkt->data[offset+0];
uint32_t new_nat_ip;
memcpy(&new_nat_ip, &pkt->data[offset], 4);
offset += 4;
uint16_t new_nat_port = (pkt->data[offset] << 8) | pkt->data[offset+1];
offset += 2;

22
src/route_bgp.c

@ -607,17 +607,16 @@ static void route_bgp_start_link_nat_check(struct ROUTE_BGP* bgp, struct ETCP_LI
}
// Get target address - use nat_ip/nat_port if set, otherwise use remote_addr
/* link->nat_ip и sin_addr.s_addr в network byte order,
конвертируем в host order для API route_ping_send_req_addr */
/* link->nat_ip и sin_addr.s_addr в network byte order */
uint32_t target_ip;
uint16_t target_port;
if (link->nat_ip != 0 && link->nat_port != 0) {
target_ip = ntohl(link->nat_ip); /* network → host order */
target_port = link->nat_port; /* уже в host order */
target_ip = link->nat_ip;
target_port = link->nat_port;
} else {
struct sockaddr_in* sin = (struct sockaddr_in*)&link->remote_addr;
target_ip = ntohl(sin->sin_addr.s_addr); /* network → host order */
target_port = ntohs(sin->sin_port); /* network → host order */
target_ip = sin->sin_addr.s_addr;
target_port = ntohs(sin->sin_port); /* port в host order для API */
}
if (!bgp->allow_nat_check_local && is_local_subnet(target_ip)) {
@ -1037,12 +1036,11 @@ void route_bgp_send_nat_info(struct ETCP_CONN* conn, uint8_t socket_id, uint32_t
pkt->cmd = ETCP_ID_ROUTE_ENTRY;
pkt->subcmd = ROUTE_SUBCMD_NAT_INFO;
pkt->socket_id = socket_id;
/* IP в пакете хранится в network byte order (big-endian bytes) */
uint32_t nat_ip_be = htonl(nat_ip);
pkt->nat_ip[0] = (nat_ip_be >> 24) & 0xFF;
pkt->nat_ip[1] = (nat_ip_be >> 16) & 0xFF;
pkt->nat_ip[2] = (nat_ip_be >> 8) & 0xFF;
pkt->nat_ip[3] = nat_ip_be & 0xFF;
/* IP уже в network byte order */
pkt->nat_ip[0] = (nat_ip >> 24) & 0xFF;
pkt->nat_ip[1] = (nat_ip >> 16) & 0xFF;
pkt->nat_ip[2] = (nat_ip >> 8) & 0xFF;
pkt->nat_ip[3] = nat_ip & 0xFF;
pkt->nat_port = htons(nat_port);
pkt->nat_type = nat_type;
DEBUG_INFO(DEBUG_CATEGORY_BGP, "NAT_INFO send: ip_host=0x%08x ip_bytes=0x%02x%02x%02x%02x port_host=%u port_net=%u",

5
src/route_ping.c

@ -123,9 +123,8 @@ int route_ping_send_req_addr(struct ROUTE_BGP* bgp, struct ETCP_CONN* to_conn,
req_pkt->count = count;
req_pkt->interval_ms = interval_ms;
req_pkt->timeout_ms = timeout_ms;
/* IP и порт хранятся в network byte order (big-endian) в пакете */
uint32_t target_ip_be = htonl(target_ip);
memcpy(req_pkt->target_ipv4, &target_ip_be, 4);
/* IP уже в network byte order, порт конвертируем */
memcpy(req_pkt->target_ipv4, &target_ip, 4);
req_pkt->target_port = htons(target_port);
if (pubkey) {
memcpy(req_pkt->pubkey, pubkey, SC_PUBKEY_SIZE);

8
tests/test_etcp_two_instances.c

@ -173,7 +173,7 @@ static void monitor_connections(void* arg) {
// Convert nat_ip to string for display
char nat_ip_str[INET_ADDRSTRLEN];
struct in_addr nat_addr;
nat_addr.s_addr = htonl(link->nat_ip);
nat_addr.s_addr = link->nat_ip; // уже в network byte order
inet_ntop(AF_INET, &nat_addr, nat_ip_str, sizeof(nat_ip_str));
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLIENT] PASS: NAT address is set: %s:%u",
@ -182,9 +182,9 @@ static void monitor_connections(void* arg) {
link->nat_changes_count, link->nat_hits_count);
// Verify NAT IP is 127.0.0.1 (localhost) since client connects from localhost
// In host byte order: 127.0.0.1 = 0x7F000001
if (link->nat_ip != 0x7F000001) {
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[CLIENT] FAIL: NAT IP is not 127.0.0.1 (expected 0x7F000001, got 0x%08X)",
// В network byte order: 127.0.0.1 = 0x0100007F
if (link->nat_ip != 0x0100007F) {
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[CLIENT] FAIL: NAT IP is not 127.0.0.1 (expected 0x0100007F, got 0x%08X)",
link->nat_ip);
test_completed = 2; // Failure
return;

20
tests/test_nat_detection.c

@ -425,12 +425,12 @@ int main(void) {
}
memset(&nat_ping_result, 0, sizeof(nat_ping_result));
/* nat_ip в network byte order, конвертируем в host order для API */
uint32_t target_ip_host = ntohl(link_sc1->nat_ip);
DEBUG_INFO(DEBUG_CATEGORY_BGP, "TEST: sending ping with pubkey, nat_ip_net=0x%08x nat_ip_host=0x%08x nat_port=%u",
link_sc1->nat_ip, target_ip_host, link_sc1->nat_port);
/* nat_ip уже в network byte order, API тоже принимает NBO */
uint32_t target_ip_net = link_sc1->nat_ip;
DEBUG_INFO(DEBUG_CATEGORY_BGP, "TEST: sending ping with pubkey, nat_ip_net=0x%08x nat_port=%u",
link_sc1->nat_ip, link_sc1->nat_port);
int ret = route_ping_send_req_addr(inst_s->bgp, conn_sc2,
target_ip_host, link_sc1->nat_port,
target_ip_net, link_sc1->nat_port,
3, 10, 200, 3000, nat_ping_resp_cb, NULL,
node_c1->node.public_key);
if (ret != 0) {
@ -473,12 +473,12 @@ int main(void) {
DEBUG_INFO(DEBUG_CATEGORY_BGP, "Testing route ping without embedded pubkey...");
memset(&nat_ping_result, 0, sizeof(nat_ping_result));
/* nat_ip в network byte order, конвертируем в host order для API */
target_ip_host = ntohl(link_sc1->nat_ip);
DEBUG_INFO(DEBUG_CATEGORY_BGP, "TEST: sending ping NO pubkey, nat_ip_net=0x%08x nat_ip_host=0x%08x nat_port=%u",
link_sc1->nat_ip, target_ip_host, link_sc1->nat_port);
/* nat_ip уже в network byte order, API тоже принимает NBO */
target_ip_net = link_sc1->nat_ip;
DEBUG_INFO(DEBUG_CATEGORY_BGP, "TEST: sending ping NO pubkey, nat_ip_net=0x%08x nat_port=%u",
link_sc1->nat_ip, link_sc1->nat_port);
ret = route_ping_send_req_addr(inst_s->bgp, conn_sc2,
target_ip_host, link_sc1->nat_port,
target_ip_net, link_sc1->nat_port,
3, 10, 200, 3000, nat_ping_resp_cb, NULL,
node_c1->node.public_key);
if (ret != 0) {

6
tests/test_route_ping.c

@ -267,10 +267,8 @@ int main(void) {
const struct NODEINFO_IPV4_SOCKET* sockets;
int sc = get_node_v4_sockets(nq, &sockets);
if (sc > 0) {
uint32_t target_ip_be;
memcpy(&target_ip_be, sockets[0].addr, 4);
target_ip = ntohl(target_ip_be); /* network → host order */
target_port = sockets[0].port; /* уже в host order */
memcpy(&target_ip, sockets[0].addr, 4); /* network byte order */
target_port = sockets[0].port; /* уже в host order */
}
}
if (target_ip == 0 || target_port == 0) {

Loading…
Cancel
Save