Skip to content

Commit ded449b

Browse files
committed
zmq: Enable IPv6 on listening socket
1 parent f66ecea commit ded449b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/zmq/zmqpublishnotifier.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <chain.h>
88
#include <chainparams.h>
9+
#include <netbase.h>
910
#include <node/blockstorage.h>
1011
#include <rpc/server.h>
1112
#include <streams.h>
@@ -73,6 +74,20 @@ static int zmq_send_multipart(void *sock, const void* data, size_t size, ...)
7374
return 0;
7475
}
7576

77+
static bool IsZMQAddressIPV6(const std::string &zmq_address)
78+
{
79+
const std::string tcp_prefix = "tcp://";
80+
const size_t tcp_index = zmq_address.rfind(tcp_prefix);
81+
const size_t colon_index = zmq_address.rfind(":");
82+
if (tcp_index == 0 && colon_index != std::string::npos) {
83+
const std::string ip = zmq_address.substr(tcp_prefix.length(), colon_index - tcp_prefix.length());
84+
CNetAddr addr;
85+
LookupHost(ip, addr, false);
86+
if (addr.IsIPv6()) return true;
87+
}
88+
return false;
89+
}
90+
7691
bool CZMQAbstractPublishNotifier::Initialize(void *pcontext)
7792
{
7893
assert(!psocket);
@@ -107,6 +122,15 @@ bool CZMQAbstractPublishNotifier::Initialize(void *pcontext)
107122
return false;
108123
}
109124

125+
// On some systems (e.g. OpenBSD) the ZMQ_IPV6 must not be enabled, if the address to bind isn't IPv6
126+
const int enable_ipv6 { IsZMQAddressIPV6(address) ? 1 : 0};
127+
rc = zmq_setsockopt(psocket, ZMQ_IPV6, &enable_ipv6, sizeof(enable_ipv6));
128+
if (rc != 0) {
129+
zmqError("Failed to set ZMQ_IPV6");
130+
zmq_close(psocket);
131+
return false;
132+
}
133+
110134
rc = zmq_bind(psocket, address.c_str());
111135
if (rc != 0)
112136
{

0 commit comments

Comments
 (0)