merge bitcoin#22079: Add support to listen on IPv6 addresses

This commit is contained in:
Kittywhiskers Van Gogh 2021-05-26 19:18:58 +02:00
parent 3ac3714957
commit 2965093c4a
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
3 changed files with 49 additions and 1 deletions

View File

@ -112,6 +112,7 @@ For instance:
$ dashd -zmqpubhashtx=tcp://127.0.0.1:28332 \ $ dashd -zmqpubhashtx=tcp://127.0.0.1:28332 \
-zmqpubhashtx=tcp://192.168.1.2:28332 \ -zmqpubhashtx=tcp://192.168.1.2:28332 \
-zmqpubhashblock="tcp://[::1]:28333" \
-zmqpubrawtx=ipc:///tmp/dashd.tx.raw \ -zmqpubrawtx=ipc:///tmp/dashd.tx.raw \
-zmqpubhashtxhwm=10000 -zmqpubhashtxhwm=10000
@ -153,6 +154,9 @@ Setting the keepalive values appropriately for your operating environment may
improve connectivity in situations where long-lived connections are silently improve connectivity in situations where long-lived connections are silently
dropped by network middle boxes. dropped by network middle boxes.
Also, the socket's ZMQ_IPV6 option is enabled to accept connections from IPv6
hosts as well. If needed, this option has to be set on the client side too.
## Remarks ## Remarks
From the perspective of dashd, the ZeroMQ socket is write-only; PUB From the perspective of dashd, the ZeroMQ socket is write-only; PUB

View File

@ -6,6 +6,7 @@
#include <chain.h> #include <chain.h>
#include <chainparams.h> #include <chainparams.h>
#include <netbase.h>
#include <node/blockstorage.h> #include <node/blockstorage.h>
#include <streams.h> #include <streams.h>
#include <validation.h> #include <validation.h>
@ -92,6 +93,20 @@ static int zmq_send_multipart(void *sock, const void* data, size_t size, ...)
return 0; return 0;
} }
static bool IsZMQAddressIPV6(const std::string &zmq_address)
{
const std::string tcp_prefix = "tcp://";
const size_t tcp_index = zmq_address.rfind(tcp_prefix);
const size_t colon_index = zmq_address.rfind(":");
if (tcp_index == 0 && colon_index != std::string::npos) {
const std::string ip = zmq_address.substr(tcp_prefix.length(), colon_index - tcp_prefix.length());
CNetAddr addr;
LookupHost(ip, addr, false);
if (addr.IsIPv6()) return true;
}
return false;
}
bool CZMQAbstractPublishNotifier::Initialize(void *pcontext) bool CZMQAbstractPublishNotifier::Initialize(void *pcontext)
{ {
assert(!psocket); assert(!psocket);
@ -126,6 +141,15 @@ bool CZMQAbstractPublishNotifier::Initialize(void *pcontext)
return false; return false;
} }
// On some systems (e.g. OpenBSD) the ZMQ_IPV6 must not be enabled, if the address to bind isn't IPv6
const int enable_ipv6 { IsZMQAddressIPV6(address) ? 1 : 0};
rc = zmq_setsockopt(psocket, ZMQ_IPV6, &enable_ipv6, sizeof(enable_ipv6));
if (rc != 0) {
zmqError("Failed to set ZMQ_IPV6");
zmq_close(psocket);
return false;
}
rc = zmq_bind(psocket, address.c_str()); rc = zmq_bind(psocket, address.c_str());
if (rc != 0) if (rc != 0)
{ {

View File

@ -17,6 +17,7 @@ from test_framework.util import (
assert_equal, assert_equal,
assert_raises_rpc_error, assert_raises_rpc_error,
) )
from test_framework.netutil import test_ipv6_local
from time import sleep from time import sleep
# Test may be skipped and not have zmq installed # Test may be skipped and not have zmq installed
@ -120,6 +121,7 @@ class ZMQTest (BitcoinTestFramework):
self.test_mempool_sync() self.test_mempool_sync()
self.test_reorg() self.test_reorg()
self.test_multiple_interfaces() self.test_multiple_interfaces()
self.test_ipv6()
finally: finally:
# Destroy the ZMQ context. # Destroy the ZMQ context.
self.log.debug("Destroying ZMQ context") self.log.debug("Destroying ZMQ context")
@ -127,10 +129,12 @@ class ZMQTest (BitcoinTestFramework):
# Restart node with the specified zmq notifications enabled, subscribe to # Restart node with the specified zmq notifications enabled, subscribe to
# all of them and return the corresponding ZMQSubscriber objects. # all of them and return the corresponding ZMQSubscriber objects.
def setup_zmq_test(self, services, *, recv_timeout=60, sync_blocks=True): def setup_zmq_test(self, services, *, recv_timeout=60, sync_blocks=True, ipv6=False):
subscribers = [] subscribers = []
for topic, address in services: for topic, address in services:
socket = self.ctx.socket(zmq.SUB) socket = self.ctx.socket(zmq.SUB)
if ipv6:
socket.setsockopt(zmq.IPV6, 1)
subscribers.append(ZMQSubscriber(socket, topic.encode())) subscribers.append(ZMQSubscriber(socket, topic.encode()))
self.restart_node(0, ["-zmqpub%s=%s" % (topic, address) for topic, address in services] + self.restart_node(0, ["-zmqpub%s=%s" % (topic, address) for topic, address in services] +
@ -553,6 +557,22 @@ class ZMQTest (BitcoinTestFramework):
assert_equal(self.nodes[0].getbestblockhash(), subscribers[0].receive().hex()) assert_equal(self.nodes[0].getbestblockhash(), subscribers[0].receive().hex())
assert_equal(self.nodes[0].getbestblockhash(), subscribers[1].receive().hex()) assert_equal(self.nodes[0].getbestblockhash(), subscribers[1].receive().hex())
def test_ipv6(self):
if not test_ipv6_local():
self.log.info("Skipping IPv6 test, because IPv6 is not supported.")
return
self.log.info("Testing IPv6")
# Set up subscriber using IPv6 loopback address
subscribers = self.setup_zmq_test([
("hashblock", "tcp://[::1]:28332")
], ipv6=True)
# Generate 1 block in nodes[0]
self.nodes[0].generatetoaddress(1, ADDRESS_BCRT1_UNSPENDABLE)
# Should receive the same block hash
assert_equal(self.nodes[0].getbestblockhash(), subscribers[0].receive().hex())
if __name__ == '__main__': if __name__ == '__main__':
ZMQTest().main() ZMQTest().main()