jdk/src/java.base/windows/native/libnet/Inet4AddressImpl.c
changeset 41126 7a2fc45a887d
parent 37362 9b3486880437
child 41137 cbc639238d91
equal deleted inserted replaced
41125:88d76c8db1fa 41126:7a2fc45a887d
    31 #include <malloc.h>
    31 #include <malloc.h>
    32 #include <sys/types.h>
    32 #include <sys/types.h>
    33 #include <process.h>
    33 #include <process.h>
    34 #include <iphlpapi.h>
    34 #include <iphlpapi.h>
    35 #include <icmpapi.h>
    35 #include <icmpapi.h>
       
    36 #include <WinError.h>
    36 
    37 
    37 #include "java_net_InetAddress.h"
    38 #include "java_net_InetAddress.h"
    38 #include "java_net_Inet4AddressImpl.h"
    39 #include "java_net_Inet4AddressImpl.h"
    39 #include "net_util.h"
    40 #include "net_util.h"
    40 #include "icmp.h"
    41 #include "icmp.h"
   440     char SendData[32] = {0};
   441     char SendData[32] = {0};
   441     LPVOID ReplyBuffer = NULL;
   442     LPVOID ReplyBuffer = NULL;
   442     DWORD ReplySize = 0;
   443     DWORD ReplySize = 0;
   443     jboolean ret = JNI_FALSE;
   444     jboolean ret = JNI_FALSE;
   444 
   445 
   445     ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData);
   446     // https://msdn.microsoft.com/en-us/library/windows/desktop/aa366051%28v=vs.85%29.aspx
       
   447     ReplySize = sizeof(ICMP_ECHO_REPLY)   // The buffer should be large enough
       
   448                                           // to hold at least one ICMP_ECHO_REPLY
       
   449                                           // structure
       
   450                 + sizeof(SendData)        // plus RequestSize bytes of data.
       
   451                 + 8;                      // This buffer should also be large enough
       
   452                                           // to also hold 8 more bytes of data
       
   453                                           // (the size of an ICMP error message)
       
   454 
   446     ReplyBuffer = (VOID*) malloc(ReplySize);
   455     ReplyBuffer = (VOID*) malloc(ReplySize);
   447     if (ReplyBuffer == NULL) {
   456     if (ReplyBuffer == NULL) {
   448         IcmpCloseHandle(hIcmpFile);
   457         IcmpCloseHandle(hIcmpFile);
   449         NET_ThrowNew(env, WSAGetLastError(), "Unable to allocate memory");
   458         NET_ThrowNew(env, WSAGetLastError(), "Unable to allocate memory");
   450         return JNI_FALSE;
   459         return JNI_FALSE;
   476                                    ReplyBuffer,// LPVOID ReplyBuffer,
   485                                    ReplyBuffer,// LPVOID ReplyBuffer,
   477                                    ReplySize,  // DWORD ReplySize,
   486                                    ReplySize,  // DWORD ReplySize,
   478                                    (timeout < 1000) ? 1000 : timeout);   // DWORD Timeout
   487                                    (timeout < 1000) ? 1000 : timeout);   // DWORD Timeout
   479     }
   488     }
   480 
   489 
   481     if (dwRetVal != 0) {
   490     if (dwRetVal == 0) { // if the call failed
       
   491         TCHAR *buf;
       
   492         DWORD err = WSAGetLastError();
       
   493         switch (err) {
       
   494             case ERROR_NO_NETWORK:
       
   495             case ERROR_NETWORK_UNREACHABLE:
       
   496             case ERROR_HOST_UNREACHABLE:
       
   497             case ERROR_PROTOCOL_UNREACHABLE:
       
   498             case ERROR_PORT_UNREACHABLE:
       
   499             case ERROR_REQUEST_ABORTED:
       
   500             case ERROR_INCORRECT_ADDRESS:
       
   501             case ERROR_HOST_DOWN:
       
   502             case WSAEHOSTUNREACH:   /* Host Unreachable */
       
   503             case WSAENETUNREACH:    /* Network Unreachable */
       
   504             case WSAENETDOWN:       /* Network is down */
       
   505             case WSAEPFNOSUPPORT:   /* Protocol Family unsupported */
       
   506             case IP_REQ_TIMED_OUT:
       
   507                 break;
       
   508             default:
       
   509                 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
       
   510                         NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
       
   511                         (LPTSTR)&buf, 0, NULL);
       
   512                 NET_ThrowNew(env, err, buf);
       
   513                 LocalFree(buf);
       
   514                 break;
       
   515         }
       
   516     } else {
   482         PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
   517         PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
   483         if ((int)pEchoReply->RoundTripTime <= timeout)
   518 
       
   519         // This is to take into account the undocumented minimum
       
   520         // timeout mentioned in the IcmpSendEcho call above.
       
   521         // We perform an extra check to make sure that our
       
   522         // roundtrip time was less than our desired timeout
       
   523         // for cases where that timeout is < 1000ms.
       
   524         if (pEchoReply->Status == IP_SUCCESS
       
   525                 && (int)pEchoReply->RoundTripTime <= timeout)
       
   526         {
   484             ret = JNI_TRUE;
   527             ret = JNI_TRUE;
       
   528         }
   485     }
   529     }
   486 
   530 
   487     free(ReplyBuffer);
   531     free(ReplyBuffer);
   488     IcmpCloseHandle(hIcmpFile);
   532     IcmpCloseHandle(hIcmpFile);
   489 
   533