Prefer untouched helper IPs and exercise NAT through real groups

This commit is contained in:
evgeny committed 2026-10-12 18:13:29 +02:00
1 parent 8990574c1d
commit 0ee431b490
3 files changed
+30 -15

No files matched your search

+9 -3
View File
@@ -63,7 +63,7 @@ struct nat_socket {
uint8_t contacts[NAT_MAX_CONTACTS][17];
unsigned contact_count, phase, reason, retries;
uint8_t history_loaded, history_complete, history_incomplete, filter_clean, pre_received, post_received, pre_healthy;
uint8_t helper_socket, active_ping, forced, cycling, using_fallback;
uint8_t helper_socket, active_ping, forced, cycling, using_fallback, prefer_fresh;
uint32_t rounds, helper_reselections;
};
@@ -104,6 +104,7 @@ static void owner_finish(struct nat_socket *, uint8_t);
static void helper_remove(struct nat_probe *);
static int helper_send(struct nat_probe *);
static void owner_start(struct nat_socket *);
static int contacted(struct nat_socket *, const struct sockaddr_storage *);
static uint64_t read64(const uint8_t *data) {
uint64_t value = 0;
@@ -231,6 +232,7 @@ static int select_pair(struct nat_socket *state) {
const struct TOPO_NODE *cn = group_node(detector, group->group_id, helper);
struct sockaddr_storage ca = {0}; uint8_t cs = 0;
if (!state->socket->is_tcp && !node_udp_endpoint(detector, cn, state->local.ss_family, 1, &ca, &cs)) continue;
if (!state->socket->is_tcp && state->prefer_fresh && !state->using_fallback && contacted(state, &ca)) continue;
struct nat_pair candidate = {group->group_id, observer, helper};
if (state->using_fallback) {
if (memcmp(&candidate, &state->fallback, sizeof(candidate))) continue;
@@ -242,6 +244,9 @@ static int select_pair(struct nat_socket *state) {
}
}
if (!best.group) {
if (state->prefer_fresh && !state->using_fallback) {
state->prefer_fresh = 0; state->cursor = (struct nat_pair){0}; return select_pair(state);
}
if (!state->using_fallback && state->fallback.group) { state->using_fallback = 1; return select_pair(state); }
state->reason = observers ? NAT_REASON_NO_HELPER : NAT_REASON_NO_OBSERVER;
DEBUG_DEBUG(DEBUG_CATEGORY_NAT, "NAT candidate cycle exhausted socket=%s groups=%u observers=%u reason=%s",
@@ -1051,11 +1056,12 @@ static void owner_start(struct nat_socket *state) {
state->socket->name, sockaddr_storage_to_str(&state->local).str); return;
}
if (!state->cycling) {
state->cycling = 1; state->cursor = (struct nat_pair){0}; state->fallback = (struct nat_pair){0}; state->using_fallback = 0;
state->cycling = 1; state->cursor = (struct nat_pair){0}; state->fallback = (struct nat_pair){0};
state->using_fallback = 0; state->prefer_fresh = 1;
}
history_load(state);
if (select_pair(state)) { owner_finish(state, state->reason); return; }
if (random_id(&state->round)) { owner_finish(state, NAT_REASON_SEND_ERROR); return; }
history_load(state);
state->rounds++;
memset(&state->helper_seen, 0, sizeof(state->helper_seen)); memset(&state->alternate_seen, 0, sizeof(state->alternate_seen));
state->retries = 0; state->pre_received = 0; state->post_received = 0; state->filter_clean = 0; state->pre_healthy = 0;