5

I'm using Clojure, but I can read Java, so this isn't a Clojure specific question. This doesn't even seem to be working from Java.

I'm trying to implement a bit of a 'ping' function using isReachable. The code I'm using is this:

(.isReachable (java.net.InetAddress/getByName "www.microsoft.com") 5000)

Translated to Java by a good friend of mine:

public class NetTest {
  public static void main (String[] args) throws Exception{
    String host = "acidrayne.net";
    InetAddress a = InetAddress.getByName(host);

    System.out.println(a.isReachable(10000));
  }
}

Both of these return false. I suppose I must be doin' it wrong, but Google research is telling me differently. I'm confuzzled!

Rayne
  • 31,473
  • 17
  • 86
  • 101
  • Can you ping acidrayne.net from the machine you are running this on? – Yishai May 05 '10 at 22:38
  • I can ping acidrayne.net via `ping -c 1 acidrayne.net`, but not via the code posted above. And that is on any machine. – Rayne May 05 '10 at 22:41

2 Answers2

3

Updated in response to comment that this is wrong:

Using Unix/Linux??

http://bordet.blogspot.com/2006/07/icmp-and-inetaddressisreachable.html says:

Linux/Unix, instead, supports an ICMP "ping" system call. So the implementation of java.net.InetAddress.isReachable() first tries to perform the "ping" system call**; if this fails, it falls back trying to open a TCP socket on [sic - to] port 7, as in Windows.

It turns out that in Linux/Unix the ping system call requires root privileges, so most of the times java.net.InetAddress.isReachable() will fail, because many Java programs are not run as root, and because the target address unlikely has the echo service up and running. Too bad.

The comment below from @EJP indicates the part of about the echo service is wrong, wrong wrong:

That's not correct. isReachable returns true if it gets a ConnectException trying to connect to port 7, as that proves that the host is up and able to send RST segments.

In cases like these, I use a packet sniffer like WireShark, tcpdump (WinDump on Windows) or snoop (Solaris) to confirm what is really happening on the wire.

Community
  • 1
  • 1
Bert F
  • 85,407
  • 12
  • 106
  • 123
  • 2
    That's not correct. isReachable returns true if it gets a ConnectException trying to connect to port 7, as that proves that the host is up and able to send RST segments. – user207421 May 06 '10 at 05:59
  • @EJP - thanks for the correction - my bad for taking the internet blog post at face value. I updated the answer, but I may just delete the answer altogether later. Since I updated, the up votes for it should be able to be taken back. – Bert F May 06 '10 at 11:04
  • I don't care whether or not you were "a little incorrect". Your answer was spot on, and it works just fine if I run it as root (which is impossible in this situation). isReachable isn't working on my, or a friends computers for anything but localhost and other machines it appears. – Rayne May 06 '10 at 15:19
  • @Rayne - Thanks. I'll leave it up since it did provide you part of the answer. It would be nice to know what really happened with your system. Did ping fail (do to privileges) and not fall back to trying the echo service? Or did ping fail, it did fall back to the echo service, but echo "failed" (did not even process the RSTs as host exists). Since it reportedly works for other people, it may be a version issue or a OS-specific issue. – Bert F May 07 '10 at 19:24
  • I assume ping failed due to lack of privileges, and the echo service failed on acidrayne as well. It works for a lot of sites (including this one), and a couple of other people tried to ping acidrayne in the same way, and it failed. acidrayne is hosted on a shared hostgator server. I have no idea why it didn't work. Doesn't really matter though. – Rayne May 07 '10 at 23:27
  • The problem isn't 'root' priveldges, the problem is capabilities allowed/set in the linux subsystem. `# setcap cap_net_raw+p /bin/yourprogram` You should set the same capabiltities that the 'ping' program has, assuming you need the same functionality. –  Sep 19 '16 at 13:24
1

The correct answer is not actually correct I think. Microsoft.com simply ignore ICMP requests, probably to avoid basic ping flood attacks. As for the second host I've no idea what the problem with the ping might be, but I'm using GNU/Linux and isReachable works just fine.

Bozhidar Batsov
  • 55,802
  • 13
  • 100
  • 117
  • It works just fine if I run it as root, and a friend experiences the exact same thing, so I'm fairly certain that his answer was at least correct up to that point. – Rayne May 06 '10 at 15:17
  • Maybe so. I'm not expert, I'm just saying that running code using isReachable() while not root under Linux has worked for some hosts... – Bozhidar Batsov May 06 '10 at 16:28