net: Prevent node from binding to the same CService

Currently, if the user inadvertently starts the node with duplicate bind options,
such as `-bind=0.0.0.0 -bind=0.0.0.0`, it will cause a fatal error with the
misleading message "Bitcoin Core is probably already running".

This commit adds early validation to detect duplicate bindings across all binding
configurations (-bind, -whitebind, and onion bindings) before attempting to bind.
When duplicates are detected, the node terminates with a clear, specific error
message: "Duplicate binding configuration for address <addr>. Please check your
-bind, -bind=...=onion and -whitebind settings."

The validation catches duplicates both within the same option type (e.g.,
`-bind=X -bind=X`) and across different types (e.g., `-bind=X -whitebind=Y@X`),
helping users identify and fix configuration mistakes.
This commit is contained in:
woltx
2025-08-25 20:22:33 -07:00
parent 7d9789401b
commit 4d4789dffa
2 changed files with 55 additions and 3 deletions

View File

@@ -1233,6 +1233,40 @@ bool CheckHostPortOptions(const ArgsManager& args) {
return true;
}
/**
* @brief Checks for duplicate bindings across all binding configurations.
*
* @param[in] conn_options Connection options containing the binding vectors to check
* @return std::optional<CService> containing the first duplicate found, or std::nullopt if no duplicates
*/
static std::optional<CService> CheckBindingConflicts(const CConnman::Options& conn_options)
{
std::set<CService> seen;
// Check all whitelisted bindings
for (const auto& wb : conn_options.vWhiteBinds) {
if (!seen.insert(wb.m_service).second) {
return wb.m_service;
}
}
// Check regular bindings
for (const auto& bind : conn_options.vBinds) {
if (!seen.insert(bind).second) {
return bind;
}
}
// Check onion bindings
for (const auto& onion_bind : conn_options.onion_binds) {
if (!seen.insert(onion_bind).second) {
return onion_bind;
}
}
return std::nullopt;
}
// A GUI user may opt to retry once with do_reindex set if there is a failure during chainstate initialization.
// The function therefore has to support re-entry.
static ChainstateLoadResult InitAndLoadChainstate(
@@ -2142,6 +2176,13 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
connOptions.m_i2p_accept_incoming = args.GetBoolArg("-i2pacceptincoming", DEFAULT_I2P_ACCEPT_INCOMING);
if (auto conflict = CheckBindingConflicts(connOptions)) {
return InitError(strprintf(
_("Duplicate binding configuration for address %s. "
"Please check your -bind, -bind=...=onion and -whitebind settings."),
conflict->ToStringAddrPort()));
}
if (!node.connman->Start(scheduler, connOptions)) {
return false;
}