src/windows/native/java/net/Inet4AddressImpl.c

Print this page




 460      * We may revisit and enable this code in the future.
 461      */
 462 
 463     /*
 464      * Let's try to create a RAW socket to send ICMP packets
 465      * This usually requires "root" privileges, so it's likely to fail.
 466      */
 467     fd = NET_Socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
 468     if (fd != -1) {
 469       /*
 470        * It didn't fail, so we can use ICMP_ECHO requests.
 471        */
 472         return ping4(env, fd, &him, timeout, netif, ttl);
 473     }
 474 #endif
 475 
 476     /*
 477      * Can't create a raw socket, so let's try a TCP socket
 478      */
 479     fd = NET_Socket(AF_INET, SOCK_STREAM, 0);
 480     if (fd == JVM_IO_ERR) {
 481         /* note: if you run out of fds, you may not be able to load
 482          * the exception class, and get a NoClassDefFoundError
 483          * instead.
 484          */
 485         NET_ThrowNew(env, WSAGetLastError(), "Can't create socket");
 486         return JNI_FALSE;
 487     }
 488     if (ttl > 0) {
 489       setsockopt(fd, IPPROTO_IP, IP_TTL, (const char *)&ttl, sizeof(ttl));
 490     }
 491     /*
 492      * A network interface was specified, so let's bind to it.
 493      */
 494     if (netif != NULL) {
 495       if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) {
 496         NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket");
 497         closesocket(fd);
 498         return JNI_FALSE;
 499       }
 500     }




 460      * We may revisit and enable this code in the future.
 461      */
 462 
 463     /*
 464      * Let's try to create a RAW socket to send ICMP packets
 465      * This usually requires "root" privileges, so it's likely to fail.
 466      */
 467     fd = NET_Socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
 468     if (fd != -1) {
 469       /*
 470        * It didn't fail, so we can use ICMP_ECHO requests.
 471        */
 472         return ping4(env, fd, &him, timeout, netif, ttl);
 473     }
 474 #endif
 475 
 476     /*
 477      * Can't create a raw socket, so let's try a TCP socket
 478      */
 479     fd = NET_Socket(AF_INET, SOCK_STREAM, 0);
 480     if (fd == SOCKET_ERROR) {
 481         /* note: if you run out of fds, you may not be able to load
 482          * the exception class, and get a NoClassDefFoundError
 483          * instead.
 484          */
 485         NET_ThrowNew(env, WSAGetLastError(), "Can't create socket");
 486         return JNI_FALSE;
 487     }
 488     if (ttl > 0) {
 489       setsockopt(fd, IPPROTO_IP, IP_TTL, (const char *)&ttl, sizeof(ttl));
 490     }
 491     /*
 492      * A network interface was specified, so let's bind to it.
 493      */
 494     if (netif != NULL) {
 495       if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) {
 496         NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket");
 497         closesocket(fd);
 498         return JNI_FALSE;
 499       }
 500     }