91 if (IS_NULL(host)) {
92 JNU_ThrowNullPointerException(env, "host is null");
93 return 0;
94 }
95 hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
96 CHECK_NULL_RETURN(hostname, NULL);
97
98 /* get the address preference */
99 preferIPv6Address
100 = (*env)->GetStaticIntField(env, ia_class, ia_preferIPv6AddressID);
101
102 /* Try once, with our static buffer. */
103 memset(&hints, 0, sizeof(hints));
104 hints.ai_flags = AI_CANONNAME;
105 hints.ai_family = AF_UNSPEC;
106
107 error = getaddrinfo(hostname, NULL, &hints, &res);
108
109 if (error) {
110 if (WSAGetLastError() == WSATRY_AGAIN) {
111 NET_ThrowByNameWithLastError(env,
112 JNU_JAVANETPKG "UnknownHostException",
113 hostname);
114 JNU_ReleaseStringPlatformChars(env, host, hostname);
115 return NULL;
116 } else {
117 /* report error */
118 JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
119 (char *)hostname);
120 JNU_ReleaseStringPlatformChars(env, host, hostname);
121 return NULL;
122 }
123 } else {
124 int i = 0;
125 int inetCount = 0, inet6Count = 0, inetIndex = 0, inet6Index = 0, originalIndex = 0;
126 struct addrinfo *itr, *last, *iterator = res;
127 while (iterator != NULL) {
128 int skip = 0;
129 itr = resNew;
130 while (itr != NULL) {
131 if (iterator->ai_family == itr->ai_family &&
390 * we were able to reach the host!
391 */
392 if (connect_rv == 0 || WSAGetLastError() == WSAECONNREFUSED) {
393 WSACloseEvent(hEvent);
394 closesocket(fd);
395 return JNI_TRUE;
396 } else {
397 int optlen;
398
399 switch (WSAGetLastError()) {
400 case WSAEHOSTUNREACH: /* Host Unreachable */
401 case WSAENETUNREACH: /* Network Unreachable */
402 case WSAENETDOWN: /* Network is down */
403 case WSAEPFNOSUPPORT: /* Protocol Family unsupported */
404 WSACloseEvent(hEvent);
405 closesocket(fd);
406 return JNI_FALSE;
407 }
408
409 if (WSAGetLastError() != WSAEWOULDBLOCK) {
410 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
411 "connect failed");
412 WSACloseEvent(hEvent);
413 closesocket(fd);
414 return JNI_FALSE;
415 }
416
417 timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
418
419 if (timeout >= 0) {
420 /* has connection been established? */
421 optlen = sizeof(connect_rv);
422 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
423 &optlen) <0) {
424 connect_rv = WSAGetLastError();
425 }
426
427 if (connect_rv == 0 || connect_rv == WSAECONNREFUSED) {
428 WSACloseEvent(hEvent);
429 closesocket(fd);
430 return JNI_TRUE;
|
91 if (IS_NULL(host)) {
92 JNU_ThrowNullPointerException(env, "host is null");
93 return 0;
94 }
95 hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
96 CHECK_NULL_RETURN(hostname, NULL);
97
98 /* get the address preference */
99 preferIPv6Address
100 = (*env)->GetStaticIntField(env, ia_class, ia_preferIPv6AddressID);
101
102 /* Try once, with our static buffer. */
103 memset(&hints, 0, sizeof(hints));
104 hints.ai_flags = AI_CANONNAME;
105 hints.ai_family = AF_UNSPEC;
106
107 error = getaddrinfo(hostname, NULL, &hints, &res);
108
109 if (error) {
110 if (WSAGetLastError() == WSATRY_AGAIN) {
111 JNU_ThrowByNameWithLastError(env,
112 JNU_JAVANETPKG "UnknownHostException",
113 hostname);
114 JNU_ReleaseStringPlatformChars(env, host, hostname);
115 return NULL;
116 } else {
117 /* report error */
118 JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
119 (char *)hostname);
120 JNU_ReleaseStringPlatformChars(env, host, hostname);
121 return NULL;
122 }
123 } else {
124 int i = 0;
125 int inetCount = 0, inet6Count = 0, inetIndex = 0, inet6Index = 0, originalIndex = 0;
126 struct addrinfo *itr, *last, *iterator = res;
127 while (iterator != NULL) {
128 int skip = 0;
129 itr = resNew;
130 while (itr != NULL) {
131 if (iterator->ai_family == itr->ai_family &&
390 * we were able to reach the host!
391 */
392 if (connect_rv == 0 || WSAGetLastError() == WSAECONNREFUSED) {
393 WSACloseEvent(hEvent);
394 closesocket(fd);
395 return JNI_TRUE;
396 } else {
397 int optlen;
398
399 switch (WSAGetLastError()) {
400 case WSAEHOSTUNREACH: /* Host Unreachable */
401 case WSAENETUNREACH: /* Network Unreachable */
402 case WSAENETDOWN: /* Network is down */
403 case WSAEPFNOSUPPORT: /* Protocol Family unsupported */
404 WSACloseEvent(hEvent);
405 closesocket(fd);
406 return JNI_FALSE;
407 }
408
409 if (WSAGetLastError() != WSAEWOULDBLOCK) {
410 JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
411 "connect failed");
412 WSACloseEvent(hEvent);
413 closesocket(fd);
414 return JNI_FALSE;
415 }
416
417 timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
418
419 if (timeout >= 0) {
420 /* has connection been established? */
421 optlen = sizeof(connect_rv);
422 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
423 &optlen) <0) {
424 connect_rv = WSAGetLastError();
425 }
426
427 if (connect_rv == 0 || connect_rv == WSAECONNREFUSED) {
428 WSACloseEvent(hEvent);
429 closesocket(fd);
430 return JNI_TRUE;
|