Skip to content

Commit 69fd120

Browse files
committed
Force genesis server to .1 and fix bind crash on restart
- Genesis server (no seed_peers) always gets 10.64.0.1 regardless of existing IPAM allocations. Previous code checked IPAM first and used a stale allocation from earlier runs. - Add SO_REUSEADDR to HttpServer sockets to prevent "bind: Address already in use" crash when systemd restarts the service quickly. - Clean up resolve_tunnel_ip logic: genesis check first, then joining server path with existing allocation or gossip fallback.
1 parent 2140aa0 commit 69fd120

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

projects/LemonadeNexus/src/Core/ServerIdentity.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -152,29 +152,29 @@ std::string resolve_tunnel_ip(
152152

153153
std::string tunnel_bind_ip;
154154

155-
// Check for existing IPAM allocation
156-
auto existing = ipam.get_allocation(server_node_id);
157-
if (existing && existing->tunnel) {
158-
tunnel_bind_ip = existing->tunnel->base_network;
159-
} else if (!config.seed_peers.empty()) {
160-
// Joining server: wait for gossip to assign us a tunnel IP via ServerHello
161-
spdlog::info("Waiting for tunnel IP assignment from network peers...");
162-
for (int i = 0; i < 15 && gossip.our_tunnel_ip().empty(); ++i) {
163-
std::this_thread::sleep_for(std::chrono::seconds(1));
164-
}
165-
tunnel_bind_ip = gossip.our_tunnel_ip();
166-
if (tunnel_bind_ip.empty()) {
167-
spdlog::warn("No tunnel IP received from peers -- self-allocating");
168-
auto alloc = ipam.allocate_tunnel_ip(server_node_id);
169-
tunnel_bind_ip = alloc.base_network;
170-
}
171-
} else {
155+
if (config.seed_peers.empty()) {
172156
// Genesis/root server: always use .1 as the gateway address.
173-
// Record an allocation so IPAM tracks this node, but override to .1.
174157
auto server_alloc = ipam.allocate_tunnel_ip(server_node_id);
175158
(void)server_alloc;
176159
tunnel_bind_ip = "10.64.0.1";
177160
spdlog::info("Genesis server -- gateway tunnel IP: {}", tunnel_bind_ip);
161+
} else {
162+
// Joining server: check existing allocation or wait for gossip assignment
163+
auto existing = ipam.get_allocation(server_node_id);
164+
if (existing && existing->tunnel) {
165+
tunnel_bind_ip = existing->tunnel->base_network;
166+
} else {
167+
spdlog::info("Waiting for tunnel IP assignment from network peers...");
168+
for (int i = 0; i < 15 && gossip.our_tunnel_ip().empty(); ++i) {
169+
std::this_thread::sleep_for(std::chrono::seconds(1));
170+
}
171+
tunnel_bind_ip = gossip.our_tunnel_ip();
172+
if (tunnel_bind_ip.empty()) {
173+
spdlog::warn("No tunnel IP received from peers -- self-allocating");
174+
auto alloc = ipam.allocate_tunnel_ip(server_node_id);
175+
tunnel_bind_ip = alloc.base_network;
176+
}
177+
}
178178
}
179179

180180
// Strip CIDR suffix (e.g. "10.64.0.1/32" -> "10.64.0.1")

projects/LemonadeNexus/src/Network/HttpServer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ bool HttpServer::reload_tls_certs(const std::string& cert_path,
127127
// ---------------------------------------------------------------------------
128128

129129
void HttpServer::on_start() {
130+
server_->set_address_family(AF_INET);
131+
server_->set_socket_options([](socket_t sock) {
132+
int yes = 1;
133+
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
134+
});
130135
listen_thread_ = std::thread([this]() {
131136
if (is_tls_) {
132137
spdlog::info("HttpServer (HTTPS) listening on {}:{}", bind_address_, port_);

0 commit comments

Comments
 (0)