Skip to content

Commit 83b67f2

Browse files
tdb3ryanofsky
andcommitted
refactor: move host/port checking
Reduces the size of AppInitMain() by moving checks to CheckHostPortOptions() Co-Authored-By: Ryan Ofsky <ryan@ofsky.org>
1 parent 73c2439 commit 83b67f2

File tree

1 file changed

+48
-42
lines changed

1 file changed

+48
-42
lines changed

src/init.cpp

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,53 @@ bool AppInitInterfaces(NodeContext& node)
11451145
return true;
11461146
}
11471147

1148+
bool CheckHostPortOptions(const ArgsManager& args) {
1149+
for (const std::string port_option : {
1150+
"-port",
1151+
"-rpcport",
1152+
}) {
1153+
if (args.IsArgSet(port_option)) {
1154+
const std::string port = args.GetArg(port_option, "");
1155+
uint16_t n;
1156+
if (!ParseUInt16(port, &n) || n == 0) {
1157+
return InitError(InvalidPortErrMsg(port_option, port));
1158+
}
1159+
}
1160+
}
1161+
1162+
for ([[maybe_unused]] const auto& [arg, unix] : std::vector<std::pair<std::string, bool>>{
1163+
// arg name UNIX socket support
1164+
{"-i2psam", false},
1165+
{"-onion", true},
1166+
{"-proxy", true},
1167+
{"-rpcbind", false},
1168+
{"-torcontrol", false},
1169+
{"-whitebind", false},
1170+
{"-zmqpubhashblock", true},
1171+
{"-zmqpubhashtx", true},
1172+
{"-zmqpubrawblock", true},
1173+
{"-zmqpubrawtx", true},
1174+
{"-zmqpubsequence", true},
1175+
}) {
1176+
for (const std::string& socket_addr : args.GetArgs(arg)) {
1177+
std::string host_out;
1178+
uint16_t port_out{0};
1179+
if (!SplitHostPort(socket_addr, port_out, host_out)) {
1180+
#ifdef HAVE_SOCKADDR_UN
1181+
// Allow unix domain sockets for some options e.g. unix:/some/file/path
1182+
if (!unix || !socket_addr.starts_with(ADDR_PREFIX_UNIX)) {
1183+
return InitError(InvalidPortErrMsg(arg, socket_addr));
1184+
}
1185+
#else
1186+
return InitError(InvalidPortErrMsg(arg, socket_addr));
1187+
#endif
1188+
}
1189+
}
1190+
}
1191+
1192+
return true;
1193+
}
1194+
11481195
bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
11491196
{
11501197
const ArgsManager& args = *Assert(node.args);
@@ -1323,48 +1370,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
13231370
}
13241371

13251372
// Check port numbers
1326-
for (const std::string port_option : {
1327-
"-port",
1328-
"-rpcport",
1329-
}) {
1330-
if (args.IsArgSet(port_option)) {
1331-
const std::string port = args.GetArg(port_option, "");
1332-
uint16_t n;
1333-
if (!ParseUInt16(port, &n) || n == 0) {
1334-
return InitError(InvalidPortErrMsg(port_option, port));
1335-
}
1336-
}
1337-
}
1338-
1339-
for ([[maybe_unused]] const auto& [arg, unix] : std::vector<std::pair<std::string, bool>>{
1340-
// arg name UNIX socket support
1341-
{"-i2psam", false},
1342-
{"-onion", true},
1343-
{"-proxy", true},
1344-
{"-rpcbind", false},
1345-
{"-torcontrol", false},
1346-
{"-whitebind", false},
1347-
{"-zmqpubhashblock", true},
1348-
{"-zmqpubhashtx", true},
1349-
{"-zmqpubrawblock", true},
1350-
{"-zmqpubrawtx", true},
1351-
{"-zmqpubsequence", true},
1352-
}) {
1353-
for (const std::string& socket_addr : args.GetArgs(arg)) {
1354-
std::string host_out;
1355-
uint16_t port_out{0};
1356-
if (!SplitHostPort(socket_addr, port_out, host_out)) {
1357-
#ifdef HAVE_SOCKADDR_UN
1358-
// Allow unix domain sockets for some options e.g. unix:/some/file/path
1359-
if (!unix || !socket_addr.starts_with(ADDR_PREFIX_UNIX)) {
1360-
return InitError(InvalidPortErrMsg(arg, socket_addr));
1361-
}
1362-
#else
1363-
return InitError(InvalidPortErrMsg(arg, socket_addr));
1364-
#endif
1365-
}
1366-
}
1367-
}
1373+
if (!CheckHostPortOptions(args)) return false;
13681374

13691375
for (const std::string& socket_addr : args.GetArgs("-bind")) {
13701376
std::string host_out;

0 commit comments

Comments
 (0)