Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change IP for local node connection to Loopback address #4373

Merged
merged 1 commit into from
Jul 29, 2020

Conversation

QuantumRipple
Copy link
Contributor

If the local Bitcoin full node is bound to only listen on the loopback interface (127.0.0.1), attempting to open a socket to InetAddress.getLocalHost() - the return of which is variable but usually NOT 127.0.0.1 - will not work. Changing to InetAddress.getLoopbackAddress() resolves this.

@boring-cyborg
Copy link

boring-cyborg bot commented Jul 23, 2020

Thanks for opening this pull request!

Please check out our contributor checklist and check if Travis or Codacy found any issues with your PR. Also make sure your commits are signed, and that you applied Bisq's code style and formatting.

A maintainer will add an is:priority label to your PR if it is up for compensation. Please see our Bisq Q1 2020 Update post for more details.

If the local Bitcoin full node is bound to only listen on the loopback interface (127.0.0.1), attempting to open a socket to `InetAddress.getLocalHost()` - the return of which is variable but usually NOT 127.0.0.1 - will not work. Changing to `InetAddress.getLoopbackAddress()` resolves this.
@ripcurlx
Copy link
Contributor

@dmos62 Could you please have a look at this? Thanks!

@dmos62
Copy link
Contributor

dmos62 commented Jul 27, 2020

If the local Bitcoin full node is bound to only listen on the loopback interface (127.0.0.1), attempting to open a socket to InetAddress.getLocalHost() - the return of which is variable but usually NOT 127.0.0.1 - will not work. Changing to InetAddress.getLoopbackAddress() resolves this.

Researched this a bit, but I'm still a bit fuzzy. As far as I can tell localhost normally does resolve to the loopback interface. Under what conditions doesn't it?

@QuantumRipple
Copy link
Contributor Author

On my linux system, getLocalHost() returns 127.0.1.1 - the hosts file alias for my hostname. The typical usecase for getLocalHost() is to get the LAN address of the host (e.g. 192.168.1.x) but even that it does unreliably as evidenced by it giving 127.0.1.1 on my system. All of these addresses will connect to local machine so long as the listener is bound to all incoming addresses (0.0.0.0) rather than a specific interface.

@dmos62
Copy link
Contributor

dmos62 commented Jul 28, 2020

On my linux system, getLocalHost() returns 127.0.1.1 - the hosts file alias for my hostname. The typical usecase for getLocalHost() is to get the LAN address of the host (e.g. 192.168.1.x) but even that it does unreliably as evidenced by it giving 127.0.1.1 on my system. All of these addresses will connect to local machine so long as the listener is bound to all incoming addresses (0.0.0.0) rather than a specific interface.

Hm, I'll lay out how I understand things, which conflict with some of the things you said:

  • 0.0.0.0 doesn't, as far as ipv4 is concerned, signify "all interfaces". It signifies an invalid address and participants are free to interpret it as they like (the "all interfaces" interpretation is a common convention, but not the only one):

    In the Internet Protocol Version 4, the address 0.0.0.0 is a non-routable meta-address used to designate an invalid, unknown or non-applicable target. This address is assigned specific meanings in a number of contexts, such as on clients or on servers. Wikipedia

  • localhost, as defined by ipv4, is meant to resolve to your default loopback interface, not the IP address of your physical interface; systems that can have multiple loopback interfaces (e.g. 127.0.0.1, 127.0.1.1, ...), like Linux, have ways to specify which is the default; sounds like on yours it's the hosts file.

In your situation, it seems like, the problem is that bitcoind is binding to a non-default loopback interface. Either reconfigure bitcoind, or change the default loopback interface by editing the hosts file.

I am against the suggestion to query for a loopback address directly in Bisq, because it's undefined what loopback address it should return. The purpose of localhost is to define which loopback address is the default, so it's idiomatic to use it.

@QuantumRipple
Copy link
Contributor Author

QuantumRipple commented Jul 28, 2020

Perhaps I have not been clear about what this PR fixes - my localhost is 127.0.0.1. InetAddress.getLocalHost() does not (always) return the address for localhost, and is thus not the idiomatic way to get the default loopback address. On my system, for example, it returns the address for my computer's hostname (just like the docs say it is intended to do). InetAddress.getLoopbackAddress() returns the IP for localhost. Furthermore, InetAddress.getLocalHost() can fail to resolve and thow an exception. InetAddress.getLoopbackAddress()cannot.

jshell> InetAddress.getLocalHost()
$1 ==> computername/127.0.1.1

jshell> InetAddress.getLoopbackAddress()
$2 ==> localhost/127.0.0.1

0.0.0.0 in the context of the bitcoind configuration file's bind address means listen on all addresses. Other definitions are not applicable.

@dmos62
Copy link
Contributor

dmos62 commented Jul 28, 2020

What is the output of dig +short localhost on that system? Btw, could you drop a link to the docs you mentioned?

@QuantumRipple
Copy link
Contributor Author

QuantumRipple commented Jul 28, 2020

$ dig +short localhost
127.0.0.1
$ cat /etc/hosts
# Host addresses
127.0.0.1  localhost
127.0.1.1  computername
::1        localhost ip6-localhost ip6-loopback
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters

API Docs: http://cr.openjdk.java.net/~iris/se/11/build/latest/api/java.base/java/net/InetAddress.html
Note that "the local host name" is not the same as localhost.

@dmos62
Copy link
Contributor

dmos62 commented Jul 28, 2020

Note that "the local host name" is not the same as localhost.

That's the part I missed. I was confused why there's both a resolve localhost method and a get loopback interface method.

Copy link
Contributor

@dmos62 dmos62 left a comment

Choose a reason for hiding this comment

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

ACK. Behaviour being fixed is indeed a bug.

Copy link
Contributor

@ripcurlx ripcurlx left a comment

Choose a reason for hiding this comment

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

utACK - based on #4373 (review)

@ripcurlx ripcurlx merged commit d9eb1d8 into bisq-network:master Jul 29, 2020
@boring-cyborg
Copy link

boring-cyborg bot commented Jul 29, 2020

Awesome work, congrats on your first merged pull request!

@ripcurlx ripcurlx added this to the v1.3.7 milestone Jul 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants