Skip to content

Conversation

adriansmares
Copy link

@adriansmares adriansmares commented Jun 20, 2021

Closes #22236

This PR forces the Tor control plane address to be resolved before a connection attempt is made, similar to how the -proxy / -onion address is resolved.

The use case for this change is that the control plane may not have a stable address - in a containerized environment perhaps.

@DrahtBot DrahtBot added the P2P label Jun 20, 2021
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

ACK 5ed8657

Tested this branch on POP!_OS with:

$ cat /etc/hosts
127.0.0.1	localghost
$ cat .bitcoin/bitcoin.conf
torcontrol=localghost:9051

Confirmed that it fails on master branch with error:

tor: Error parsing socket address localghost:9051
tor: Initializing connection to Tor Control Port localghost:9051 failed

Copy link
Contributor

@Zero-1729 Zero-1729 left a comment

Choose a reason for hiding this comment

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

tACK 5ed8657

Tested branch on MacOS 11.3.1.

Used the following configurations in the /etc/hosts/ and bitcoin.conf files respectively:

$ cat /etc/hosts
127.0.0.1   	zerohost
$ cat bitcoin.conf
torcontrol=zerohost:9051

I got the following errors on master (as expected):

tor: Error parsing socket address zerohost:9051
tor: Initiating connection to Tor control port zerohost:9051 failed

After this patch, the errors are no longer reported, and the tor connections are established.

Nice first-time contribution and a warm welcome @adriansmares as a contributor! ;)

Copy link
Member

@jonatack jonatack left a comment

Choose a reason for hiding this comment

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

Concept ACK, thanks for looking into this.

@adriansmares adriansmares requested a review from jonatack June 20, 2021 16:58
Copy link
Contributor

@jrawsthorne jrawsthorne left a comment

Choose a reason for hiding this comment

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

tACK 809d020

Thanks for looking into this. This change fixes my original issue

@ghost
Copy link

ghost commented Jun 21, 2021

re-ACK 809d020

@Zero-1729
Copy link
Contributor

re-ACK 809d020

@jonatack
Copy link
Member

ACK 809d020 tested various configurations on signet with this branch versus master

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

Concept ACK

Welcome as a new contributor adriansmares! 🎉

Note that on the PR branch connecting to a IPv6 Tor control port doesn't work anymore:

$ ./src/bitcoind -torcontrol=::1:9051
...
tor: Error parsing socket address ::1:9051
tor: Initiating connection to Tor control port ::1:9051 failed
...

Looking a bit deeper into the problem, it seems like the address length passed to GetSockAddr() is too small (16 bytes on my machine), leading to the failure. On all other places in the codebase, sizeof(sockaddr) refers to the size of an sockaddr_storage instance (always defined one line above), whereas in this case it takes the size of the sockaddr structure. See fix below.

@adriansmares adriansmares requested a review from theStack June 27, 2021 17:27
Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

ACK cdd51e8 🪐
Tested on Debian bullseye/sid by running ./src/bitcoind -torcontrol=localhost:9051 twice, once with localhost set to 127.0.0.1 in /etc/hosts, once set to ::1.

luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request Jun 27, 2021
@jonatack
Copy link
Member

@theStack nice catch.

ACK cdd51e8 tested various configurations on signet with this branch versus master

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Looking a bit deeper into the problem, it seems like the address length passed to GetSockAddr() is too small (16 bytes on my machine), leading to the failure

Good observation.

ACK cdd51e8

Confirmed it works for ipv6 as well

@laanwj
Copy link
Member

laanwj commented Jul 21, 2021

One argument for not using DNS in the case of Tor is to prevent DNS leaks. I don't think this change, in itself, adds any extra risk of that though (as it only looks up the control host, which hopefully is local).

LGTM ACK cdd51e8

@laanwj laanwj merged commit 1c046bb into bitcoin:master Jul 21, 2021
@adriansmares adriansmares deleted the feature/tor-control-dns-resolve branch July 21, 2021 10:42
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jul 23, 2021
cdd51e8 torcontrol: Resolve Tor control plane address (Adrian-Stefan Mares)

Pull request description:

  Closes bitcoin#22236

  This PR forces the Tor control plane address to be resolved before a connection attempt is made, similar to how the `-proxy` / `-onion` address is resolved.

  The use case for this change is that the control plane may not have a stable address - in a containerized environment perhaps.

ACKs for top commit:
  jonatack:
    ACK cdd51e8 tested various configurations on signet with this branch versus master
  laanwj:
    LGTM ACK cdd51e8
  theStack:
    ACK cdd51e8 🪐
  prayank23:
    ACK bitcoin@cdd51e8

Tree-SHA512: 5335cfcb89089a2acd6d02b88c2022dec60bb74388a99187c901c1c35d32896814d5f81df55c053953276c51fcec263c6ddadd068316f8e428b841bd599fc21e
@schildbach
Copy link
Contributor

I'm currently running into this issue of the Tor control address not being resolved, as I have set up Tor within a separate Docker container. Unfortunately, this PR doesn't seem to have made it into 22.0. Does someone know a quick workaround to deal with the dynamically changing IP address, until a 22.1 is released?

I could go back to managing the hidden service within Tor (and not use a control port) as I have done in the past, but actually I'd prefer to hand over control to bitcoind.

@schildbach
Copy link
Contributor

Should this be considered for backporting to 22.x?

@bitcoin bitcoin locked and limited conversation to collaborators Oct 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

-torcontrol does not resolve a hostname if it is given
8 participants