Inetaddress.Getlocalhost() Slow to Run (30+ Seconds)

InetAddress.getLocalHost() slow to run (30+ seconds)

The issue can be solved by adding the following to /etc/hosts (assuming output of hostname command is my-macbook:

127.0.0.1   my-macbook
::1 my-macbook

This returns the time to something more suitable (< 1 second)

Jvm takes a long time to resolve ip-address for localhost

I had the same problem. Tomcat went from 15 seconds to 6 minutes to initialise spring context after the upgrade... disabling csrutils didn't solve the issue for me.

I solved the problem by adding my Mac hostname (i.e. Macbook.local, or whatever your Mac is called) on the /etc/hosts file mapped to the 127.0.0.1 address as well as the ::1 like this:

127.0.0.1   localhost mbpro.local
::1 localhost mbpro.local

If you're interested you can find some details on the issue and solution here:
https://thoeni.io/post/macos-sierra-java/

On the post I also link to a github project to help troubleshooting the issue and validating the solution.

The problem is related (I believe) on how the localhost name resolution works and how the java.net.InetAddr class is retrieving the addresses. I verified with few colleagues and apparently it doesn't happen to everyone who upgraded to Sierra, but I'm still investigating the roots of this change.

The solution anyway was the same that antid0te implemented and worked immediately.

InetAddress.getLocalHost() throws UnknownHostException

In good tradition, I can answer my own question once again:

It seems that InetAddress.getLocalHost() ignores the /etc/resolv.conf, but only looks at the /etc/hosts file (where I hadn't specified anything besides localhost). Adding the IP and hostname to this file solves the problem and the exception is gone.


Another answer is almost correct and I got hint from above and my problem get resolved...Thanks.

But to improve this, I am adding steps-by-steps changes, so that it will be helpful for even naive users.

Steps:

  • Open /etc/hosts, the entries might look like below.

     127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4  
    ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  • You need to add one more line above of this by any editor like vi or gedit (e.g. <your-machine-ip> <your-machine-name> localhost).

     192.168.1.73 my_foo localhost

Now, overall file may look like this:

192.168.1.73 my_foo localhost
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  • Just save it and run again your Java code... your work is done.

UnetStack3 simulator & audio takes long time to start

It seems the TCPServer class which is being opened when you start a new simulated node (at this place in the log)

1569411449729|INFO|org.arl.unet.sim.SimulationMasterContainer@1:openTcpServer|Listening on port 1102

seems to take almost five seconds to start. Once it's started, it prints this log

1569411459738|INFO|org.arl.fjage.connectors.TcpServer@26:run|Listening on port 1102

Relevant code is here https://github.com/org-arl/fjage/blob/master/src/main/java/org/arl/fjage/connectors/TcpServer.java#L71

Digging through it seems that Java's InetAddress.getLocalHost().getHostAddress() is taking a long time.

This seems to be a common problem on macOS. See InetAddress.getLocalHost() slow to run (30+ seconds)

This tool can help you check if it indeed is an issue https://github.com/thoeni/inetTester

It seems that Java tries to do a DNS lookup on 127.0.0.1 before returning from getHostName. And depending on your DNS settings, this could take up to 5 seconds to time out.

A simple fix that seems to work based on the Stackoverflow article linked above is to add an alias to your /etc/hosts file for 127.0.0.1.

This bash oneliner can do that for you.

n=$(sudo scutil --get LocalHostName); sudo sed -i.bak "/127.0.0.1/s/$/ $n/" /etc/hosts; sudo sed -i.bak "/::1/s/$/ $n/" /etc/hosts

InetAddress.getLocalHost().getHostAddress() is returning 127.0.1.1

You'll need to use NetworkInterface to enumerate network interfaces; InetAddress.getLocalHost() always returns loopback. This doesn't explain why you get 127.0.1.1 instead of 127.0.0.1, but since that method doesn't do what you're trying to do, it doesn't seem especially pertinent. See: http://docs.oracle.com/javase/6/docs/api/java/net/NetworkInterface.html#getInetAddresses()

InetAddress.getCanonicalHostName() returns IP instead of Hostname

The problem lies in the fact that the java.net.InetAdress has a certain procedure against ip-spoofing.

It first resolves the name into (an) ip address(es). This works fine.
In your case the result are two IP adresses. InetAdress then checks back if (at least one of) these adresses resolve to the original input name.

If they do not, it just returns the original ip adress. The following picture shows the situation after the check for baiduspider-123-125-71-75.crawl.baidu.com

The debugger after the check for `original ip reverse lookup -> name -> dns lookup of name

Note: The ip adresses resolved by getAllByName0 are the same as via nslookup, namely:

nslookup baiduspider-123-125-71-75.crawl.baidu.com
Server: 192.168.2.1
Address: 192.168.2.1#53

Non-authoritative answer:
Name: baiduspider-123-125-71-75.crawl.baidu.com
Address: 62.157.140.133
Name: baiduspider-123-125-71-75.crawl.baidu.com
Address: 80.156.86.78

A solution would be to use the dnsjava library. It skips the spoofing check and therefore works fine.

dnsjava example:

String addr = Address.getHostName(InetAddress.getByName("123.125.71.75"));
outputs just as expected baiduspider-123-125-71-75.crawl.baidu.com

Disclaimer: As i am a Java developer and not a security expert, i am not totally aware of the security implications of using a spoofed ip address.



Related Topics



Leave a reply



Submit