Skip to content

Commit c87b0a0

Browse files
committed
zmq: accept unix domain socket address for notifier
1 parent 312f542 commit c87b0a0

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

src/init.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,30 +1301,33 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
13011301
}
13021302
}
13031303

1304-
for (const std::string port_option : {
1305-
"-i2psam",
1306-
"-onion",
1307-
"-proxy",
1308-
"-rpcbind",
1309-
"-torcontrol",
1310-
"-whitebind",
1311-
"-zmqpubhashblock",
1312-
"-zmqpubhashtx",
1313-
"-zmqpubrawblock",
1314-
"-zmqpubrawtx",
1315-
"-zmqpubsequence",
1304+
for (const auto &port_option : std::vector<std::pair<std::string, bool>>{
1305+
// arg name UNIX socket support
1306+
{"-i2psam", false},
1307+
{"-onion", true},
1308+
{"-proxy", true},
1309+
{"-rpcbind", false},
1310+
{"-torcontrol", false},
1311+
{"-whitebind", false},
1312+
{"-zmqpubhashblock", true},
1313+
{"-zmqpubhashtx", true},
1314+
{"-zmqpubrawblock", true},
1315+
{"-zmqpubrawtx", true},
1316+
{"-zmqpubsequence", true}
13161317
}) {
1317-
for (const std::string& socket_addr : args.GetArgs(port_option)) {
1318+
const std::string arg{port_option.first};
1319+
const bool unix{port_option.second};
1320+
for (const std::string& socket_addr : args.GetArgs(arg)) {
13181321
std::string host_out;
13191322
uint16_t port_out{0};
13201323
if (!SplitHostPort(socket_addr, port_out, host_out)) {
13211324
#if HAVE_SOCKADDR_UN
1322-
// Allow unix domain sockets for -proxy and -onion e.g. unix:/some/file/path
1323-
if ((port_option != "-proxy" && port_option != "-onion") || socket_addr.find(ADDR_PREFIX_UNIX) != 0) {
1324-
return InitError(InvalidPortErrMsg(port_option, socket_addr));
1325+
// Allow unix domain sockets for some options e.g. unix:/some/file/path
1326+
if (!unix || socket_addr.find(ADDR_PREFIX_UNIX) != 0) {
1327+
return InitError(InvalidPortErrMsg(arg, socket_addr));
13251328
}
13261329
#else
1327-
return InitError(InvalidPortErrMsg(port_option, socket_addr));
1330+
return InitError(InvalidPortErrMsg(arg, socket_addr));
13281331
#endif
13291332
}
13301333
}

src/zmq/zmqnotificationinterface.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <kernel/chain.h>
99
#include <kernel/mempool_entry.h>
1010
#include <logging.h>
11+
#include <netbase.h>
1112
#include <primitives/block.h>
1213
#include <primitives/transaction.h>
1314
#include <validationinterface.h>
@@ -57,7 +58,12 @@ std::unique_ptr<CZMQNotificationInterface> CZMQNotificationInterface::Create(std
5758
{
5859
std::string arg("-zmq" + entry.first);
5960
const auto& factory = entry.second;
60-
for (const std::string& address : gArgs.GetArgs(arg)) {
61+
for (std::string& address : gArgs.GetArgs(arg)) {
62+
// libzmq uses prefix "ipc://" for UNIX domain sockets
63+
if (address.substr(0, ADDR_PREFIX_UNIX.length()) == ADDR_PREFIX_UNIX) {
64+
address.replace(0, ADDR_PREFIX_UNIX.length(), ADDR_PREFIX_IPC);
65+
}
66+
6167
std::unique_ptr<CZMQAbstractNotifier> notifier = factory();
6268
notifier->SetType(entry.first);
6369
notifier->SetAddress(address);

src/zmq/zmqutil.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@
99

1010
void zmqError(const std::string& str);
1111

12+
/** Prefix for unix domain socket addresses (which are local filesystem paths) */
13+
const std::string ADDR_PREFIX_IPC = "ipc://"; // used by libzmq, example "ipc:///root/path/to/file"
14+
1215
#endif // BITCOIN_ZMQ_ZMQUTIL_H

0 commit comments

Comments
 (0)