diff --git a/src/routing_layer/nat_detection.c b/src/routing_layer/nat_detection.c index 540593392..165756dab 100644 --- a/src/routing_layer/nat_detection.c +++ b/src/routing_layer/nat_detection.c @@ -50,12 +50,12 @@ struct nat_pair { uint64_t group, observer, helper; }; struct nat_socket { struct nat_socket *next; struct NAT_DETECTION *detector; - struct ETCP_SOCKET *socket, *auxiliary; + struct ETCP_SOCKET *socket, *helper_primary, *auxiliary; struct sockaddr_storage local, observer_address, helper_address, alternate_address; struct sockaddr_storage baseline, helper_seen, alternate_seen; struct sockaddr_storage measured_address; struct address_profile measured_profile; - uint64_t select_due, available_fingerprint; + uint64_t select_due, available_fingerprint, reservation_fingerprint, cold_until; uint8_t fallback_allowed; uint8_t observer_key[32], helper_key[32]; uint64_t group_id, round, observer, helper, token, deadline, due, completed_tb, preferred_observer; @@ -92,7 +92,7 @@ struct NAT_DETECTION { void *timer; void *topology_wake; uint64_t publish_after; - uint8_t allow_local, stopping, history_table_ready, automatic, publish_pending; + uint8_t allow_local, stopping, history_table_ready, automatic, publish_pending, reservation_dirty; }; static void owner_ping_cb(int, uint16_t, void *, uint64_t, const uint8_t *, size_t); @@ -281,6 +281,7 @@ static struct nat_socket *socket_state(struct NAT_DETECTION *detector, struct ET state->detector = detector; state->socket = socket; state->local = socket->local_addr; if (socket->is_tcp) state->local = socket->interface_addr; state->due = get_time_tb() + 2500; + state->cold_until = state->due + NAT_ROUND_TB; state->next = detector->sockets; detector->sockets = state; DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT endpoint created socket=%s id=%u generation=%016llx protocol=%s local=%s", socket->name, socket->sock_id, (unsigned long long)socket->generation, @@ -480,6 +481,7 @@ static void clear_profile(struct nat_socket *state, uint8_t reason) { memset(&state->socket->address_profile, 0, sizeof(state->socket->address_profile)); memset(&state->socket->nat_addr, 0, sizeof(state->socket->nat_addr)); state->completed_tb = 0; + state->cold_until = get_time_tb() + NAT_ROUND_TB + 2500; memset(&state->measured_profile, 0, sizeof(state->measured_profile)); memset(&state->measured_address, 0, sizeof(state->measured_address)); if (changed) { @@ -690,25 +692,26 @@ void nat_detection_receive_ingress(struct NAT_DETECTION *detector, struct ETCP_S } /* Дополнительный порт наблюдателя не публикуется и не участвует в NCD. */ -static struct ETCP_SOCKET *auxiliary_socket(struct NAT_DETECTION *detector, struct ETCP_SOCKET *source) { +static struct ETCP_SOCKET *helper_socket(struct NAT_DETECTION *detector, struct ETCP_SOCKET *source, int alternate) { struct nat_socket *state = socket_state(detector, source, 1); if (!state) return NULL; - if (state->auxiliary) return state->auxiliary; + struct ETCP_SOCKET **slot = alternate ? &state->auxiliary : &state->helper_primary; + if (*slot) return *slot; struct CFG_SERVER server; memset(&server, 0, sizeof(server)); server.ip = source->local_addr; server.netif_index = source->netif_index; server.type = CFG_SERVER_TYPE_PRIVATE; server.mtu = source->mtu; server.fib = -1; for (const struct CFG_SERVER *cfg = detector->instance->config->servers; cfg; cfg = cfg->next) if (!strcmp(cfg->name, source->name)) { server.so_mark = cfg->so_mark; server.fib = cfg->fib; break; } - snprintf(server.name, sizeof(server.name), "nat-observer-%u", source->sock_id); + snprintf(server.name, sizeof(server.name), "nat-helper-%u-%u", source->sock_id, alternate); if (server.ip.ss_family == AF_INET) ((struct sockaddr_in *)&server.ip)->sin_port = 0; else ((struct sockaddr_in6 *)&server.ip)->sin6_port = 0; - state->auxiliary = etcp_socket_add(detector->instance, &server); - if (!state->auxiliary) { DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT alternate observer socket failed"); return NULL; } - state->auxiliary->probe_only = 1; - DEBUG_INFO(DEBUG_CATEGORY_NAT, "NAT alternate observer socket=%s primary=%s alternate=%s", - source->name, sockaddr_storage_to_str(&source->local_addr).str, - sockaddr_storage_to_str(&state->auxiliary->local_addr).str); - return state->auxiliary; + *slot = etcp_socket_add(detector->instance, &server); + if (!*slot) { DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT helper socket failed source=%s alternate=%d", source->name, alternate); return NULL; } + (*slot)->probe_only = 1; + DEBUG_INFO(DEBUG_CATEGORY_NAT, "NAT isolated helper socket source=%s alternate=%d local=%s helper=%s", + source->name, alternate, sockaddr_storage_to_str(&source->local_addr).str, + sockaddr_storage_to_str(&(*slot)->local_addr).str); + return *slot; } static void helper_remove(struct nat_probe *probe) { @@ -804,7 +807,7 @@ static int helper_send(struct nat_probe *probe) { } else if (probe->message.step == NAT_STEP_PREPARE || probe->message.step == NAT_STEP_ALT) { struct ETCP_SOCKET *source = find_socket(detector, probe->source_socket, probe->source_generation); if (!source) { DEBUG_WARN(DEBUG_CATEGORY_NAT, "NAT helper source generation disappeared"); return -1; } - struct ETCP_SOCKET *socket = probe->message.step == NAT_STEP_ALT ? auxiliary_socket(detector, source) : source; + struct ETCP_SOCKET *socket = helper_socket(detector, source, probe->message.step == NAT_STEP_ALT); if (!socket) return -1; const uint8_t *peer_key = probe->owner_key; if (probe->message.step == NAT_STEP_PREPARE || probe->alternate_fallback) { @@ -815,8 +818,11 @@ static int helper_send(struct nat_probe *probe) { result = route_ping_observe(socket, peer_key, &target, probe->group_id, read64(probe->message.round), timeout, helper_ping_cb, probe); } else { - struct ETCP_SOCKET *socket = find_socket(detector, probe->source_socket, probe->source_generation); - if (!socket || socket->is_tcp || socket->socks_udp) return -1; + struct ETCP_SOCKET *source = find_socket(detector, probe->source_socket, probe->source_generation); + if (!source || source->is_tcp || source->socks_udp) return -1; + struct ETCP_SOCKET *socket = helper_socket(detector, source, 0); + if (!socket) return -1; + probe->io_socket = socket->sock_id; probe->io_generation = socket->generation; uint8_t tag[NAT_QUERY_TAG_SIZE]; ingress_tag(&probe->message, probe->group_id, tag); result = etcp_send_ping_to_socket(detector->instance, socket, probe->owner_key, &target, timeout, @@ -1114,16 +1120,19 @@ static void detector_tick(void *arg) { } /* Отпечаток доступных endpoint не содержит timestamp/имя/профиль: обычный повтор NODEINFO не запускает пробы. */ -static uint64_t candidate_fingerprint(struct nat_socket *state) { +static uint64_t candidate_fingerprint(struct nat_socket *state, int ready) { struct NAT_DETECTION *detector = state->detector; uint64_t total = 0; for (struct ll_entry *e = detector->instance->topo_groups->group_list->head; e; e = e->next) { struct TOPO_GROUP *group = (struct TOPO_GROUP *)e; if (group->stopping || !group->local_node) continue; + uint64_t group_total = 0; unsigned candidates = 0, observers = 0; for (struct ll_entry *n = group->nodes->head; n; n = n->next) { uint64_t id = ((struct TOPO_GROUP_NODE *)n)->node_id; - if (id == detector->instance->node_id || !control_route(detector, group->group_id, id)) continue; + if (id == detector->instance->node_id || (ready ? !control_route(detector, group->group_id, id) : + !topo_node_best_path((struct TOPO_GROUP_NODE *)n))) continue; const struct TOPO_NODE *node = group_node(detector, group->group_id, id); + if (!node) continue; struct sockaddr_storage endpoint = {0}; uint8_t socket_id = 0; uint8_t bytes[16 + ROUTE_ADDR_WIRE_SIZE + 1] = {0}; write64(bytes, group->group_id); write64(bytes + 8, id); @@ -1131,11 +1140,15 @@ static uint64_t candidate_fingerprint(struct nat_socket *state) { if (!node_udp_endpoint(detector, node, state->local.ss_family, 1, &endpoint, &socket_id)) continue; if (route_ping_addr_encode(bytes + 16, &endpoint)) continue; bytes[16 + ROUTE_ADDR_WIRE_SIZE] = socket_id; + struct sockaddr_storage observer_address = {0}; uint8_t observer_socket = 0; + observers += node_udp_endpoint(detector, node, state->local.ss_family, 0, &observer_address, &observer_socket); } + candidates++; uint64_t hash = UINT64_C(14695981039346656037); for (unsigned i = 0; i < sizeof(bytes); i++) { hash ^= bytes[i]; hash *= UINT64_C(1099511628211); } - total += hash; + group_total += hash; } + if (ready || (candidates >= 2 && observers)) total += group_total; } return total; } @@ -1144,7 +1157,7 @@ static uint64_t candidate_fingerprint(struct nat_socket *state) { static void topology_wake(void *arg) { struct NAT_DETECTION *detector = arg; detector->topology_wake = NULL; for (struct nat_socket *state = detector->sockets; state; state = state->next) { - uint64_t fingerprint = candidate_fingerprint(state); + uint64_t fingerprint = candidate_fingerprint(state, 1); int changed = fingerprint != state->available_fingerprint; state->available_fingerprint = fingerprint; if (state->phase) { if (!control_route(detector, state->group_id, state->observer) || !control_route(detector, state->group_id, state->helper)) @@ -1161,7 +1174,9 @@ static void topology_wake(void *arg) { } void nat_detection_topology_changed(struct NAT_DETECTION *detector) { - if (!detector || detector->stopping || detector->topology_wake) return; + if (!detector || detector->stopping) return; + detector->reservation_dirty = 1; + if (detector->topology_wake) return; detector->topology_wake = uasync_call_soon(detector->instance->ua, detector, topology_wake); if (!detector->topology_wake) DEBUG_ERROR(DEBUG_CATEGORY_NAT, "NAT topology wake scheduling failed"); } @@ -1236,10 +1251,11 @@ void nat_detection_socket_removed(struct NAT_DETECTION *detector, struct ETCP_SO if (!*cursor) return; struct nat_socket *state = *cursor; *cursor = state->next; etcp_cancel_pings(detector->instance, owner_ping_cb, state); - struct ETCP_SOCKET *auxiliary = state->auxiliary; + struct ETCP_SOCKET *primary = state->helper_primary, *auxiliary = state->auxiliary; DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT endpoint removed socket=%s generation=%016llx", socket->name, (unsigned long long)socket->generation); u_free(state); + if (primary) etcp_socket_remove(primary); if (auxiliary) etcp_socket_remove(auxiliary); } @@ -1287,7 +1303,8 @@ void nat_detection_destroy(struct NAT_DETECTION *detector) { while (detector->sockets) { struct nat_socket *state = detector->sockets; detector->sockets = state->next; etcp_cancel_pings(detector->instance, owner_ping_cb, state); - struct ETCP_SOCKET *auxiliary = state->auxiliary; u_free(state); + struct ETCP_SOCKET *primary = state->helper_primary, *auxiliary = state->auxiliary; u_free(state); + if (primary) etcp_socket_remove(primary); if (auxiliary) etcp_socket_remove(auxiliary); } while (detector->bindings) { diff --git a/src/routing_layer/nat_detection.h b/src/routing_layer/nat_detection.h index a68ba7a9d..b2387ce79 100644 --- a/src/routing_layer/nat_detection.h +++ b/src/routing_layer/nat_detection.h @@ -40,6 +40,11 @@ void nat_detection_destroy(struct NAT_DETECTION *detector); void nat_detection_topology_changed(struct NAT_DETECTION *detector); /* Отменить раунды, задачи помощников и наблюдения до закрытия router-каналов группы. */ void nat_detection_cancel_group(struct NAT_DETECTION *detector, uint64_t group_id); +/* Короткий приоритет начального измерения перед фоновыми UDP-пробами достижимости; задержка в timebase. */ +uint64_t nat_detection_probe_delay(struct NAT_DETECTION *detector); +/* Холодные измерения сериализуются по node_id; делегированные пробы более раннего owner пропускаются. */ +int nat_detection_observe_allowed(struct NAT_DETECTION *detector, struct ETCP_SOCKET *socket, + const uint8_t peer_key[32], const uint8_t *tag, size_t length); void nat_detection_link_ready(struct NAT_DETECTION *detector, struct ETCP_LINK *link); void nat_detection_cancel_for_conn(struct NAT_DETECTION *detector, struct ETCP_CONN *connection); void nat_detection_socket_removed(struct NAT_DETECTION *detector, struct ETCP_SOCKET *socket);