What privilege should be obtained for ICMP request in Java?

I want to execute an IP address via Java. When I ping from the command line and monitor network packets, I see ICMP requests. When I ping from my application and monitor network packets, I see TCP requests on port 7.

I checked the documentation InetAddress.isReachable()

:

The best effort is made by an implementation to try to reach the host, but firewalls and server configuration can block requests resulting in an unreachable status while some specific ports may be available.

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 target host.

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

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

PS: I asked this question on stackoverflow.com because it has more to do with a Java issue (what Java source code is required to run) than a system issue.

+3


source to share


1 answer


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

For one implementation see, for example Java_java_net_Inet4AddressImpl_isReachable0

, where it basically just boils down to:



/*
 * Let try to create a RAW socket to send ICMP packets
 * This usually requires "root" privileges, so it 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 try a TCP socket
  */
 ...

      

+1


source







All Articles