Skip to content

zmq: Add support to listen on IPv6 addresses - #22079

Merged
laanwj merged 3 commits into
bitcoin:masterfrom
n-thumann:zmq-listen-ipv6
Sep 9, 2021
Merged

laanwj merged 3 commits into
bitcoin:masterfrom
n-thumann:zmq-listen-ipv6

Conversation

@n-thumann

Copy link
Copy Markdown
Contributor

This PR adds support for listening on IPv6 addresses with bitcoinds ZMQ interface, just like the RPC server.
Currently, it is not possible to specify an IPv6 address, as the ZMQ_IPV6 socket option is not set and therefore the ZMQ initialization fails, if one does so. The absence of this option has also been noted here.
With this PR one can e.g. set -zmqpubhashblock=tcp://[::1]:28333 to listen on the IPv6 loopback address.

@promag

promag commented May 26, 2021

Copy link
Copy Markdown
Contributor

Concept ACK.

@laanwj

laanwj commented May 27, 2021

Copy link
Copy Markdown
Member

Concept ACK, it's kind of fascinating this went without IPv6 support for so long.

Comment thread doc/zmq.md Resolved
@theStack

Copy link
Copy Markdown
Contributor

Concept ACK

@theStack

Copy link
Copy Markdown
Contributor

@n-thumann: Would you mind rebasing this PR on master? Then #22103 is included, which fixed the IPv6 detection for *BSDs and macOS systems.

@n-thumann

Copy link
Copy Markdown
Contributor Author

@n-thumann: Would you mind rebasing this PR on master? Then #22103 is included, which fixed the IPv6 detection for *BSDs and macOS systems.

Done ✌🏻

@theStack

Copy link
Copy Markdown
Contributor

@n-thumann: Would you mind rebasing this PR on master? Then #22103 is included, which fixed the IPv6 detection for *BSDs and macOS systems.

Done ✌🏻

Thanks!

On my system (OpenBSD 6.9), the functional test interface_zmq.py doesn't seem to get any notifications in the subscriber sockets in the PR branch. Only if I remove settings the sockopt ZMQ_IPV6 from both server (bitcoind, publisher) and client (zmq interface Test, subscriber) it works again :/ would be interesting to know if this behaviour also shows up on other systems (that are not covered by the CI).
Maybe it is connected to this ZMQ change: zeromq/libzmq#2631 but that's just a vague guess...

@n-thumann

n-thumann commented Jun 13, 2021 •

Copy link
Copy Markdown
Contributor Author

On my system (OpenBSD 6.9), the functional test interface_zmq.py doesn't seem to get any notifications in the subscriber sockets in the PR branch. Only if I remove settings the sockopt ZMQ_IPV6 from both server (bitcoind, publisher) and client (zmq interface Test, subscriber) it works again :/ would be interesting to know if this behaviour also shows up on other systems (that are not covered by the CI).
Maybe it is connected to this ZMQ change: zeromq/libzmq#2631 but that's just a vague guess...

I was able to confirm this behavior on OpenBSD, but not on any other OS (Debian, Ubuntu, macOS, FreeBSD).
It looks like that OpenBSD is a bit picky, when it comes to setting the ZMQ_IPV6 option: While other OSes simply don't care (or bind dual-stacked), if the supplied address to listen on even is an IPv6 address, OpenBSD checks that. Therefore we must only set this option on OpenBSD, if it really is an IPv6 address. Same with the client side, so the functional test. If you want to have dual stack zmq on OpenBSD, you need to bind the socket twice, once for v4 and once for v6.
@theStack feel free to test my latest changes 😊

@theStack

Copy link
Copy Markdown
Contributor

It looks like that OpenBSD is a bit picky, when it comes to setting the ZMQ_IPV6 option: While other OSes simply don't care (or bind dual-stacked), if the supplied address to listen on even is an IPv6 address, OpenBSD checks that. Therefore we must only set this option on OpenBSD, if it really is an IPv6 address. Same with the client side, so the functional test. If you want to have dual stack zmq on OpenBSD, you need to bind the socket twice, once for v4 and once for v6.
@theStack feel free to test my latest changes 😊

Very interesting, thanks for investigating deeper! I can confirm that the test succeeds with your latest changes.
Looking at your fix, I'm wondering if we could simply always only set ZMQ_IPV6 if needed? That would avoid pre-processor conditionals depending on the OS (which is always kind of ugly and error-prone, considering that we check in code that is never compiled/run in the CI, in case of OpenBSD) and not cause troubles on other OSes that we didn't think of (e.g. DragonflyBSD?) but possibly have the same behaviour.

@n-thumann

Copy link
Copy Markdown
Contributor Author

Looking at your fix, I'm wondering if we could simply always only set ZMQ_IPV6 if needed? That would avoid pre-processor conditionals depending on the OS (which is always kind of ugly and error-prone, considering that we check in code that is never compiled/run in the CI, in case of OpenBSD) and not cause troubles on other OSes that we didn't think of (e.g. DragonflyBSD?) but possibly have the same behavior.

Good point! I've ran some more tests and indeed DragonFly BSD is also affected. I also confirmed that only setting ZMQ_IPV6 if need has no negative impact / different behavior than always setting it on other OSes. Thus, I'll remove the pre-processor stuff 😊

Raw test results
OS always set option only set option, if v6 listen address  socket type (netstat)  reachable via
macOS X 127.0.0.1 tcp4 v4
macOS X 127.0.0.1 tcp4 v4
macOS X ::1 tcp6 v6
macOS X ::1 tcp6 v6
macOS X 0.0.0.0 tcp4 v4
macOS X 0.0.0.0 tcp4 v4
macOS X :: tcp46 v4 & v6
macOS X :: tcp46 v4 & v6
--- --- --- --- --- ---
Debian X 127.0.0.1 tcp v4
Debian X 127.0.0.1 tcp6 v4
Debian X ::1 tcp6 v6
Debian X ::1 tcp6 v6
Debian X 0.0.0.0 tcp v4
Debian X 0.0.0.0 tcp6 v4
Debian X :: tcp6 v4 & v6
Debian X :: tcp6 v4 & v6
--- --- --- --- --- ---
FreeBSD X 127.0.0.1 tcp4 v4
FreeBSD X 127.0.0.1 tcp4 v4
FreeBSD X ::1 tcp6 v6
FreeBSD X ::1 tcp6 v6
FreeBSD X 0.0.0.0 tcp v4
FreeBSD X 0.0.0.0 tcp4 v4
FreeBSD X :: tcp46 v4 & v6
FreeBSD X :: tcp46 v4 & v6
--- --- --- --- --- ---
OpenBSD X 127.0.0.1 tcp v4
OpenBSD X 127.0.0.1 fails fails
OpenBSD X ::1 tcp6 v6
OpenBSD X ::1 tcp6 v6
OpenBSD X 0.0.0.0 tcp v4
OpenBSD X 0.0.0.0 fails fails
OpenBSD X :: tcp6 v6
OpenBSD X :: tcp6 v6
--- --- --- --- --- ---
DragonFly BSD X 127.0.0.1 tcp4 v4
DragonFly BSD X 127.0.0.1 fails fails
DragonFly BSD X ::1 tcp6 v6
DragonFly BSD X ::1 tcp6 v6
DragonFly BSD X 0.0.0.0 tcp4 v4
DragonFly BSD X 0.0.0.0 fails fails
DragonFly BSD X :: tcp6 v6
DragonFly BSD X :: tcp6 v6

Comment thread src/zmq/zmqpublishnotifier.cpp Outdated
Comment on lines 116 to 121

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty ugly. Can we just ignore errors setting ZMQ_IPV6 if the subsequent bind succeeds?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cleaned up a little bit, but I'm not sure, if ignoring an error while setting ZMQ_IPV6 is a good idea: If it fails, the binding of an IPv6 address will also fail and and prior error could help narrowing down the issue. And if we set the option on e.g. OpenBSD without checking, the bind will always fail for IPv4.
Did I got you right? You want to always set ZMQ_IPV6 and see if the binding succeeds. If it does, all fine. If it didn't, what then?

@theStack theStack left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

7bbf6d8c92b still works here (OpenBSD 6.9) and the code looks cleaner than the previous version, with the pre-processor dependency gone. I tend to agree with @luke-jr that having to analyze the address string first in order to set (or not set) a socket option is "ugly", but on the other hand I don't see another solution.

Some suggestions:

  • Would add a short comment on why we only set ZMQ_IPV6 on IPv6 endpoints (like you had already in one of your earlier commits; you could simply take that and replace "OpenBSD" with "On some systems (like e.g. OpenBSD)...")
  • Could introduce a helper function like IsZMQAddressIPV6 that contains the IPv6 determination logic from the ZMQ address string; then you could simply write const int enable_ipv6 { IsZMQAddressIPV6(address) ? 1 : 0 };

@n-thumann

Copy link
Copy Markdown
Contributor Author

I tend to agree with @luke-jr that having to analyze the address string first in order to set (or not set) a socket option is "ugly", but on the other hand I don't see another solution.

Absolutely! I'd be nice to have the plain address to bind, but the string entered by the user is the only thing to work with :/

Some suggestions: [...]

Applied!

@theStack

Copy link
Copy Markdown
Contributor

@n-thumann: looks good to me. Could you squash at least the last two commits (or probably better even the last three, in order to avoid an "in-between" state where the "only set zmq_ipv6 if needed" fix has only been applied on one side), in order to ease review?

@n-thumann

Copy link
Copy Markdown
Contributor Author

Squashed :)

@theStack theStack left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some feedback regarding coding style and structure (that I unfortunately missed before), regarding the first commit:

  • variables should be named in snake_case rather than (lower) camelCase (e.g. s/tcpIndex/tcp_index/)
  • "If an if only has a single-statement then-clause, it can appear on the same line as the if, without braces. In every other case, braces are required, and the then and else clauses must appear correctly indented on a new line."
  • Since the IsZMQAddressIPV6 helper doesn't use any instance specific data, I'd suggest to move it out of the class and simply put it in a static function on top of the file

@n-thumann

Copy link
Copy Markdown
Contributor Author

Done! Btw, I really do appreciate your feedback!

@theStack theStack left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested ACK e699883 🌱
(reviewed code, built PR branch and ran included test on both OpenBSD 6.9 and Debian bullseye/sid, works as expected)

Done! Btw, I really do appreciate your feedback!

You're welcome. Looking forward to see more contributions from you in the future :-)

@laanwj

laanwj commented Sep 9, 2021

Copy link
Copy Markdown
Member

Code review ACK e699883

@laanwj
laanwj merged commit b05d3e7 into bitcoin:master Sep 9, 2021
@fanquake

Copy link
Copy Markdown
Member

This went into 23.0, but I'm not sure it actually got a release note. Removing the label.

jamesdorfman added a commit to jamesdorfman/elements that referenced this pull request Apr 21, 2023
@bitcoin bitcoin locked and limited conversation to collaborators Sep 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants