zmq: accept unix domain socket address for notifier

This commit is contained in:
Matthew Zipkin 2024-03-13 11:41:04 -04:00
parent 312f54278f
commit c87b0a0ff4
No known key found for this signature in database
GPG key ID: E7E2984B6289C93A
3 changed files with 30 additions and 18 deletions

View file

@ -1301,30 +1301,33 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
} }
} }
for (const std::string port_option : { for (const auto &port_option : std::vector<std::pair<std::string, bool>>{
"-i2psam", // arg name UNIX socket support
"-onion", {"-i2psam", false},
"-proxy", {"-onion", true},
"-rpcbind", {"-proxy", true},
"-torcontrol", {"-rpcbind", false},
"-whitebind", {"-torcontrol", false},
"-zmqpubhashblock", {"-whitebind", false},
"-zmqpubhashtx", {"-zmqpubhashblock", true},
"-zmqpubrawblock", {"-zmqpubhashtx", true},
"-zmqpubrawtx", {"-zmqpubrawblock", true},
"-zmqpubsequence", {"-zmqpubrawtx", true},
{"-zmqpubsequence", true}
}) { }) {
for (const std::string& socket_addr : args.GetArgs(port_option)) { const std::string arg{port_option.first};
const bool unix{port_option.second};
for (const std::string& socket_addr : args.GetArgs(arg)) {
std::string host_out; std::string host_out;
uint16_t port_out{0}; uint16_t port_out{0};
if (!SplitHostPort(socket_addr, port_out, host_out)) { if (!SplitHostPort(socket_addr, port_out, host_out)) {
#if HAVE_SOCKADDR_UN #if HAVE_SOCKADDR_UN
// Allow unix domain sockets for -proxy and -onion e.g. unix:/some/file/path // Allow unix domain sockets for some options e.g. unix:/some/file/path
if ((port_option != "-proxy" && port_option != "-onion") || socket_addr.find(ADDR_PREFIX_UNIX) != 0) { if (!unix || socket_addr.find(ADDR_PREFIX_UNIX) != 0) {
return InitError(InvalidPortErrMsg(port_option, socket_addr)); return InitError(InvalidPortErrMsg(arg, socket_addr));
} }
#else #else
return InitError(InvalidPortErrMsg(port_option, socket_addr)); return InitError(InvalidPortErrMsg(arg, socket_addr));
#endif #endif
} }
} }

View file

@ -8,6 +8,7 @@
#include <kernel/chain.h> #include <kernel/chain.h>
#include <kernel/mempool_entry.h> #include <kernel/mempool_entry.h>
#include <logging.h> #include <logging.h>
#include <netbase.h>
#include <primitives/block.h> #include <primitives/block.h>
#include <primitives/transaction.h> #include <primitives/transaction.h>
#include <validationinterface.h> #include <validationinterface.h>
@ -57,7 +58,12 @@ std::unique_ptr<CZMQNotificationInterface> CZMQNotificationInterface::Create(std
{ {
std::string arg("-zmq" + entry.first); std::string arg("-zmq" + entry.first);
const auto& factory = entry.second; const auto& factory = entry.second;
for (const std::string& address : gArgs.GetArgs(arg)) { for (std::string& address : gArgs.GetArgs(arg)) {
// libzmq uses prefix "ipc://" for UNIX domain sockets
if (address.substr(0, ADDR_PREFIX_UNIX.length()) == ADDR_PREFIX_UNIX) {
address.replace(0, ADDR_PREFIX_UNIX.length(), ADDR_PREFIX_IPC);
}
std::unique_ptr<CZMQAbstractNotifier> notifier = factory(); std::unique_ptr<CZMQAbstractNotifier> notifier = factory();
notifier->SetType(entry.first); notifier->SetType(entry.first);
notifier->SetAddress(address); notifier->SetAddress(address);

View file

@ -9,4 +9,7 @@
void zmqError(const std::string& str); void zmqError(const std::string& str);
/** Prefix for unix domain socket addresses (which are local filesystem paths) */
const std::string ADDR_PREFIX_IPC = "ipc://"; // used by libzmq, example "ipc:///root/path/to/file"
#endif // BITCOIN_ZMQ_ZMQUTIL_H #endif // BITCOIN_ZMQ_ZMQUTIL_H