Skip to content

Commit bb145c9

Browse files
committed
net: Extend -bind config option with optional network type
1 parent 92bd3c1 commit bb145c9

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

src/init.cpp

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ void SetupServerArgs(NodeContext& node)
441441
argsman.AddArg("-addnode=<ip>", "Add a node to connect to and attempt to keep the connection open (see the `addnode` RPC command help for more info). This option can be specified multiple times to add multiple nodes.", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION);
442442
argsman.AddArg("-asmap=<file>", strprintf("Specify asn mapping used for bucketing of the peers (default: %s). Relative paths will be prefixed by the net-specific datadir location.", DEFAULT_ASMAP_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
443443
argsman.AddArg("-bantime=<n>", strprintf("Default duration (in seconds) of manually configured bans (default: %u)", DEFAULT_MISBEHAVING_BANTIME), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
444-
argsman.AddArg("-bind=<addr>", "Bind to given address and always listen on it. Use [host]:port notation for IPv6", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION);
444+
argsman.AddArg("-bind=<addr>[:<port>][=onion]", strprintf("Bind to given address and always listen on it (default: 0.0.0.0). Use [host]:port notation for IPv6. Append =onion to tag any incoming connections to that address and port as incoming Tor connections (default: 127.0.0.1:%u=onion, testnet: 127.0.0.1:%u=onion, signet: 127.0.0.1:%u=onion, regtest: 127.0.0.1:%u=onion)", defaultBaseParams->OnionServiceTargetPort(), testnetBaseParams->OnionServiceTargetPort(), signetBaseParams->OnionServiceTargetPort(), regtestBaseParams->OnionServiceTargetPort()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION);
445445
argsman.AddArg("-connect=<ip>", "Connect only to the specified node; -noconnect disables automatic connections (the rules for this peer are the same as for -addnode). This option can be specified multiple times to connect to multiple nodes.", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION);
446446
argsman.AddArg("-discover", "Discover own IP addresses (default: 1 when listening and no -externalip or -proxy)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
447447
argsman.AddArg("-dns", strprintf("Allow DNS lookups for -addnode, -seednode and -connect (default: %u)", DEFAULT_NAME_LOOKUP), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
@@ -1911,9 +1911,6 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
19111911
}
19121912
LogPrintf("nBestHeight = %d\n", chain_active_height);
19131913

1914-
if (args.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION))
1915-
StartTorControl(DefaultOnionServiceTarget());
1916-
19171914
Discover();
19181915

19191916
// Map ports with UPnP
@@ -1940,13 +1937,39 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
19401937
connOptions.nMaxOutboundLimit = nMaxOutboundLimit;
19411938
connOptions.m_peer_connect_timeout = peer_connect_timeout;
19421939

1943-
for (const std::string& strBind : args.GetArgs("-bind")) {
1944-
CService addrBind;
1945-
if (!Lookup(strBind, addrBind, GetListenPort(), false)) {
1946-
return InitError(ResolveErrMsg("bind", strBind));
1940+
for (const std::string& bind_arg : args.GetArgs("-bind")) {
1941+
CService bind_addr;
1942+
const size_t index = bind_arg.rfind('=');
1943+
if (index == std::string::npos) {
1944+
if (Lookup(bind_arg, bind_addr, GetListenPort(), false)) {
1945+
connOptions.vBinds.push_back(bind_addr);
1946+
continue;
1947+
}
1948+
} else {
1949+
const std::string network_type = bind_arg.substr(index + 1);
1950+
if (network_type == "onion") {
1951+
const std::string truncated_bind_arg = bind_arg.substr(0, index);
1952+
if (Lookup(truncated_bind_arg, bind_addr, BaseParams().OnionServiceTargetPort(), false)) {
1953+
connOptions.onion_binds.push_back(bind_addr);
1954+
continue;
1955+
}
1956+
}
19471957
}
1948-
connOptions.vBinds.push_back(addrBind);
1958+
return InitError(ResolveErrMsg("bind", bind_arg));
19491959
}
1960+
1961+
if (connOptions.onion_binds.empty()) {
1962+
connOptions.onion_binds.push_back(DefaultOnionServiceTarget());
1963+
}
1964+
1965+
if (args.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION)) {
1966+
const auto bind_addr = connOptions.onion_binds.front();
1967+
if (connOptions.onion_binds.size() > 1) {
1968+
InitWarning(strprintf(_("More than one onion bind address is provided. Using %s for the automatically created Tor onion service."), bind_addr.ToStringIPPort()));
1969+
}
1970+
StartTorControl(bind_addr);
1971+
}
1972+
19501973
for (const std::string& strBind : args.GetArgs("-whitebind")) {
19511974
NetWhitebindPermissions whitebind;
19521975
bilingual_str error;

src/net.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ enum BindFlags {
8484
BF_NONE = 0,
8585
BF_EXPLICIT = (1U << 0),
8686
BF_REPORT_ERROR = (1U << 1),
87+
/**
88+
* Do not call AddLocal() for our special addresses, e.g., for incoming
89+
* Tor connections, to prevent gossiping them over the network.
90+
*/
91+
BF_DONT_ADVERTISE = (1U << 2),
8792
};
8893

8994
// The set of sockets cannot be modified while waiting
@@ -2305,14 +2310,17 @@ bool CConnman::Bind(const CService &addr, unsigned int flags, NetPermissionFlags
23052310
return false;
23062311
}
23072312

2308-
if (addr.IsRoutable() && fDiscover && (permissions & PF_NOBAN) == 0) {
2313+
if (addr.IsRoutable() && fDiscover && !(flags & BF_DONT_ADVERTISE) && !(permissions & PF_NOBAN)) {
23092314
AddLocal(addr, LOCAL_BIND);
23102315
}
23112316

23122317
return true;
23132318
}
23142319

2315-
bool CConnman::InitBinds(const std::vector<CService>& binds, const std::vector<NetWhitebindPermissions>& whiteBinds)
2320+
bool CConnman::InitBinds(
2321+
const std::vector<CService>& binds,
2322+
const std::vector<NetWhitebindPermissions>& whiteBinds,
2323+
const std::vector<CService>& onion_binds)
23162324
{
23172325
bool fBound = false;
23182326
for (const auto& addrBind : binds) {
@@ -2328,6 +2336,11 @@ bool CConnman::InitBinds(const std::vector<CService>& binds, const std::vector<N
23282336
fBound |= Bind(CService(inaddr6_any, GetListenPort()), BF_NONE, NetPermissionFlags::PF_NONE);
23292337
fBound |= Bind(CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE, NetPermissionFlags::PF_NONE);
23302338
}
2339+
2340+
for (const auto& addr_bind : onion_binds) {
2341+
fBound |= Bind(addr_bind, BF_EXPLICIT | BF_DONT_ADVERTISE, NetPermissionFlags::PF_NONE);
2342+
}
2343+
23312344
return fBound;
23322345
}
23332346

@@ -2346,7 +2359,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
23462359
nMaxOutboundCycleStartTime = 0;
23472360
}
23482361

2349-
if (fListen && !InitBinds(connOptions.vBinds, connOptions.vWhiteBinds)) {
2362+
if (fListen && !InitBinds(connOptions.vBinds, connOptions.vWhiteBinds, connOptions.onion_binds)) {
23502363
if (clientInterface) {
23512364
clientInterface->ThreadSafeMessageBox(
23522365
_("Failed to listen on any port. Use -listen=0 if you want this."),

src/net.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ class CConnman
209209
std::vector<NetWhitelistPermissions> vWhitelistedRange;
210210
std::vector<NetWhitebindPermissions> vWhiteBinds;
211211
std::vector<CService> vBinds;
212+
std::vector<CService> onion_binds;
212213
bool m_use_addrman_outgoing = true;
213214
std::vector<std::string> m_specified_outgoing;
214215
std::vector<std::string> m_added_nodes;
@@ -406,7 +407,11 @@ class CConnman
406407

407408
bool BindListenPort(const CService& bindAddr, bilingual_str& strError, NetPermissionFlags permissions);
408409
bool Bind(const CService& addr, unsigned int flags, NetPermissionFlags permissions);
409-
bool InitBinds(const std::vector<CService>& binds, const std::vector<NetWhitebindPermissions>& whiteBinds);
410+
bool InitBinds(
411+
const std::vector<CService>& binds,
412+
const std::vector<NetWhitebindPermissions>& whiteBinds,
413+
const std::vector<CService>& onion_binds);
414+
410415
void ThreadOpenAddedConnections();
411416
void AddAddrFetch(const std::string& strDest);
412417
void ProcessAddrFetch();

0 commit comments

Comments
 (0)