views:

486

answers:

2

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!

A: 

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.

Bert F
That's pretty horrible.
Rayne
@Rayne - Agreed
Bert F
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.
EJP
@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
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
@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
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
A: 

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
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
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