3

I want to ping an IP via Java. When I ping from command line and track the network packages I see ICMP requests. When I ping from my application and track the network packages I see TCP requests at port 7.

I've checked the documentation of InetAddress.isReachable():

Best effort is made by the implementation to try to reach the host, but firewalls and server configuration may block requests resulting in a unreachable status while some specific ports may be accessible.

A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.

I can run sudo needed commands with the user that I run my Java application.

Which privilege should be obtained for my purpose? What should I check?

PS: I've asked this question at stackoverflow.com because of it is more related to Java problem(which Java source code requires to run) than a system problem.

Community
  • 1
  • 1
kamaci
  • 72,915
  • 69
  • 228
  • 366
  • @maksimov that link says that: (ICMP) usually requires administrative (root) rights but it is not my case? – kamaci Nov 17 '14 at 11:52
  • 1
    This seems not a duplicate of [java code to ping an IP address](http://stackoverflow.com/questions/11506321/java-code-to-ping-an-ip-address) because it asks about a specific detail - the privilege mentioned in the documentation - whereas the other question is asking a more general question – SpaceTrucker Nov 17 '14 at 11:54
  • 1
    Does "requires root rights" not answer your question "which privilege?" – maksimov Nov 17 '14 at 11:57
  • @maksimov no it is not. I want to learn what are the privileges mentioned in the documentation. – kamaci Nov 17 '14 at 11:59
  • @kamaci thanks, I have retracted my comment – maksimov Nov 17 '14 at 12:00
  • 1
    @maksimov Why? You are correct. 'Root privilege' is the answer to the question. – user207421 Nov 17 '14 at 16:12

1 Answers1

1

Are you asking about Linux or about Java? - Obviously, the JVM process running your program will need to have all the Linux privileges required to open a "raw" ICMP socket, just as any other process.

For one implementation see e.g. Java_java_net_Inet4AddressImpl_isReachable0 where it basically just boils down to:

/*
 * Let's try to create a RAW socket to send ICMP packets
 * This usually requires "root" privileges, so it's likely to fail.
 */
 fd = JVM_Socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
 if (fd != -1) {
    /*
     * It didn't fail, so we can use ICMP_ECHO requests.
     */
     return ping4(env, fd, &him, timeout, netif, ttl);
 }

 /*
  * Can't create a raw socket, so let's try a TCP socket
  */
 ...
JimmyB
  • 12,101
  • 2
  • 28
  • 44