mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
refactor: move host/port checking
Reduces the size of AppInitMain() by moving checks to CheckHostPortOptions() Co-Authored-By: Ryan Ofsky <ryan@ofsky.org>
This commit is contained in:
parent
73c243965a
commit
83b67f2e6d
1 changed files with 48 additions and 42 deletions
90
src/init.cpp
90
src/init.cpp
|
@ -1145,6 +1145,53 @@ bool AppInitInterfaces(NodeContext& node)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CheckHostPortOptions(const ArgsManager& args) {
|
||||||
|
for (const std::string port_option : {
|
||||||
|
"-port",
|
||||||
|
"-rpcport",
|
||||||
|
}) {
|
||||||
|
if (args.IsArgSet(port_option)) {
|
||||||
|
const std::string port = args.GetArg(port_option, "");
|
||||||
|
uint16_t n;
|
||||||
|
if (!ParseUInt16(port, &n) || n == 0) {
|
||||||
|
return InitError(InvalidPortErrMsg(port_option, port));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for ([[maybe_unused]] const auto& [arg, unix] : std::vector<std::pair<std::string, bool>>{
|
||||||
|
// arg name UNIX socket support
|
||||||
|
{"-i2psam", false},
|
||||||
|
{"-onion", true},
|
||||||
|
{"-proxy", true},
|
||||||
|
{"-rpcbind", false},
|
||||||
|
{"-torcontrol", false},
|
||||||
|
{"-whitebind", false},
|
||||||
|
{"-zmqpubhashblock", true},
|
||||||
|
{"-zmqpubhashtx", true},
|
||||||
|
{"-zmqpubrawblock", true},
|
||||||
|
{"-zmqpubrawtx", true},
|
||||||
|
{"-zmqpubsequence", true},
|
||||||
|
}) {
|
||||||
|
for (const std::string& socket_addr : args.GetArgs(arg)) {
|
||||||
|
std::string host_out;
|
||||||
|
uint16_t port_out{0};
|
||||||
|
if (!SplitHostPort(socket_addr, port_out, host_out)) {
|
||||||
|
#ifdef HAVE_SOCKADDR_UN
|
||||||
|
// Allow unix domain sockets for some options e.g. unix:/some/file/path
|
||||||
|
if (!unix || !socket_addr.starts_with(ADDR_PREFIX_UNIX)) {
|
||||||
|
return InitError(InvalidPortErrMsg(arg, socket_addr));
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
return InitError(InvalidPortErrMsg(arg, socket_addr));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
||||||
{
|
{
|
||||||
const ArgsManager& args = *Assert(node.args);
|
const ArgsManager& args = *Assert(node.args);
|
||||||
|
@ -1323,48 +1370,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check port numbers
|
// Check port numbers
|
||||||
for (const std::string port_option : {
|
if (!CheckHostPortOptions(args)) return false;
|
||||||
"-port",
|
|
||||||
"-rpcport",
|
|
||||||
}) {
|
|
||||||
if (args.IsArgSet(port_option)) {
|
|
||||||
const std::string port = args.GetArg(port_option, "");
|
|
||||||
uint16_t n;
|
|
||||||
if (!ParseUInt16(port, &n) || n == 0) {
|
|
||||||
return InitError(InvalidPortErrMsg(port_option, port));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for ([[maybe_unused]] const auto& [arg, unix] : std::vector<std::pair<std::string, bool>>{
|
|
||||||
// arg name UNIX socket support
|
|
||||||
{"-i2psam", false},
|
|
||||||
{"-onion", true},
|
|
||||||
{"-proxy", true},
|
|
||||||
{"-rpcbind", false},
|
|
||||||
{"-torcontrol", false},
|
|
||||||
{"-whitebind", false},
|
|
||||||
{"-zmqpubhashblock", true},
|
|
||||||
{"-zmqpubhashtx", true},
|
|
||||||
{"-zmqpubrawblock", true},
|
|
||||||
{"-zmqpubrawtx", true},
|
|
||||||
{"-zmqpubsequence", true},
|
|
||||||
}) {
|
|
||||||
for (const std::string& socket_addr : args.GetArgs(arg)) {
|
|
||||||
std::string host_out;
|
|
||||||
uint16_t port_out{0};
|
|
||||||
if (!SplitHostPort(socket_addr, port_out, host_out)) {
|
|
||||||
#ifdef HAVE_SOCKADDR_UN
|
|
||||||
// Allow unix domain sockets for some options e.g. unix:/some/file/path
|
|
||||||
if (!unix || !socket_addr.starts_with(ADDR_PREFIX_UNIX)) {
|
|
||||||
return InitError(InvalidPortErrMsg(arg, socket_addr));
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
return InitError(InvalidPortErrMsg(arg, socket_addr));
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const std::string& socket_addr : args.GetArgs("-bind")) {
|
for (const std::string& socket_addr : args.GetArgs("-bind")) {
|
||||||
std::string host_out;
|
std::string host_out;
|
||||||
|
|
Loading…
Add table
Reference in a new issue