From 032f8e83b2bd3a5c5b379c2e38a65dde8c70a346 Mon Sep 17 00:00:00 2001 From: evgeny Date: Fri, 23 Oct 2026 23:57:51 +0200 Subject: [PATCH] Publish incoming UDP peers before callbacks and preserve accepted ownership --- src/transport_layer/etcp_connections.c | 17 +++++++++++++++-- src/transport_layer/node_conn_direct.c | 23 +++-------------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/transport_layer/etcp_connections.c b/src/transport_layer/etcp_connections.c index 11252f45..be56f309 100644 --- a/src/transport_layer/etcp_connections.c +++ b/src/transport_layer/etcp_connections.c @@ -138,7 +138,7 @@ void tcp_server_on_link(struct stcp_link *link, struct ETCP_SOCKET *tcp_sock) { etcp_connection_close(conn); stcp_link_close(link); return; } etcp_update_log_name(conn); - { struct conn_queue_entry* ce = (struct conn_queue_entry*)conn->conn_queue_entry->data; ce->peer_node_id = node_id; queue_remove_data(conn->conn_queue, conn->conn_queue_entry); queue_data_put_with_index(conn->conn_queue, conn->conn_queue_entry); } + if (etcp_conn_index_peer(conn) < 0) { etcp_connection_close(conn); stcp_link_close(link); return; } DEBUG_INFO(DEBUG_CATEGORY_ETCP, "tcp_server_on_link: new ETCP_CONN peer=0x%016llx", (unsigned long long)node_id); } @@ -2514,6 +2514,9 @@ static void etcp_process_packet(struct ETCP_SOCKET* e_sock, uint8_t* data, (unsigned long long)peer_id, conn->crypto_ctx.session_ready); conn->peer_node_id = peer_id; etcp_update_log_name(conn); + if (etcp_conn_index_peer(conn) < 0) { + etcp_connection_close(conn); errorcode = 55; goto ec_fr; + } DEBUG_INFO(DEBUG_CATEGORY_ETCP, "New connection received on socket %s: log_name=%s peer_id=%lu peer:%s", e_sock->name, conn->log_name, (unsigned long)peer_id, sockaddr_storage_to_str(&addr).str); } else {// check keys если существующее подключение @@ -2658,7 +2661,17 @@ create_new_link: DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "[%s] NO existing link for id=%d, socket=[%s]", conn->log_name, req->link_id, e_sock->name); // Create new link link = etcp_link_new(conn, e_sock, &addr, 1); - if (!link) { if (new_conn) etcp_connection_close(conn); errorcode=66; DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "failed to create link for connection"); goto ec_fr; }// облом + if (!link) { + DEBUG_WARN(DEBUG_CATEGORY_ETCP, "[%s] UDP link creation failed/cancelled peer=%016llx from=%s", + conn->log_name, (unsigned long long)peer_id, sockaddr_storage_to_str(&addr).str); + if (new_conn) { + int owners = node_conn_direct_handle_count(conn); + if (!conn->links && !owners) etcp_connection_close(conn); + else DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "[%s] cancelled UDP accept preserves connection: other_links=%u owners=%d closing=%u", + conn->log_name, conn->links != NULL, owners, conn->close_requested); + } + errorcode = 66; goto ec_fr; + } link->remote_link_id = req->link_id; link->remote_socket_id = req->socket_id; link->remote_only_local = req->only_local; diff --git a/src/transport_layer/node_conn_direct.c b/src/transport_layer/node_conn_direct.c index 1710c0d1..c7438d67 100644 --- a/src/transport_layer/node_conn_direct.c +++ b/src/transport_layer/node_conn_direct.c @@ -892,16 +892,8 @@ int node_conn_direct_open(struct UTUN_INSTANCE* inst, uint64_t node_id, etcp_connection_close(conn); topo_node_registry_unref(inst->topo_groups, ni->node_id); return NCD_ERR; } - /* Подключаем conn в inst->connections с индексом по node_id */ - if (conn->conn_queue && conn->conn_queue_entry) { - queue_remove_data(conn->conn_queue, conn->conn_queue_entry); - queue_entry_free(conn->conn_queue_entry); - } - { struct ll_entry* qe = queue_entry_new(sizeof(struct conn_queue_entry)); - if (qe) { struct conn_queue_entry* ce = (struct conn_queue_entry*)qe->data; - ce->peer_node_id = node_id; ce->conn = conn; - conn->conn_queue_entry = qe; conn->conn_queue = inst->connections; - queue_data_put_with_index(inst->connections, qe); } + if (etcp_conn_index_peer(conn) < 0) { + etcp_connection_close(conn); topo_node_registry_unref(inst->topo_groups, ni->node_id); return NCD_ERR; } entry = u_calloc(1, sizeof(*entry)); @@ -1043,16 +1035,7 @@ static int ncd_open_node(struct UTUN_INSTANCE* inst, uint64_t node_id, etcp_connection_close(conn); return NCD_ERR; } - if (conn->conn_queue && conn->conn_queue_entry) { - queue_remove_data(conn->conn_queue, conn->conn_queue_entry); - queue_entry_free(conn->conn_queue_entry); - } - { struct ll_entry* qe = queue_entry_new(sizeof(struct conn_queue_entry)); - if (qe) { struct conn_queue_entry* ce = (struct conn_queue_entry*)qe->data; - ce->peer_node_id = node_id; ce->conn = conn; - conn->conn_queue_entry = qe; conn->conn_queue = inst->connections; - queue_data_put_with_index(inst->connections, qe); } - } + if (etcp_conn_index_peer(conn) < 0) { etcp_connection_close(conn); return NCD_ERR; } struct ncd_entry* entry = u_calloc(1, sizeof(*entry)); if (!entry) {