jdk/src/windows/native/java/net/Inet6AddressImpl.c
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7814 dff0b40f9ac8
child 9050 26c2c1de1631
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7814
diff changeset
     2
 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 910
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 910
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 910
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 910
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 910
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <winsock2.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <malloc.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <sys/types.h>
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 715
diff changeset
    33
#include <process.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "java_net_InetAddress.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "java_net_Inet4AddressImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "java_net_Inet6AddressImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include "icmp.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#ifdef WIN32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#ifndef _WIN64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/* Retain this code a little longer to support building in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * old environments.  _MSC_VER is defined as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *     1200 for MSVC++ 6.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *     1310 for Vc7
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#if defined(_MSC_VER) && _MSC_VER < 1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#define sockaddr_in6 SOCKADDR_IN6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#define uint32_t UINT32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Method:    getLocalHostName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * Signature: ()Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
Java_java_net_Inet6AddressImpl_getLocalHostName (JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    char hostname [256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    if (gethostname (hostname, sizeof (hostname)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        strcpy (hostname, "localhost");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    return JNU_NewStringPlatform (env, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
static jclass ni_iacls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
static jclass ni_ia4cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
static jclass ni_ia6cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
static jmethodID ni_ia4ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
static jmethodID ni_ia6ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
static jfieldID ni_iaaddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
static jfieldID ni_iahostID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
static jfieldID ni_iafamilyID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
static jfieldID ni_ia6ipaddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
static int initialized = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
JNIEXPORT jobjectArray JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                                jstring host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    const char *hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    jobjectArray ret = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    int retLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    jboolean preferIPv6Address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    int error=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    struct addrinfo hints, *res, *resNew = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
      ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
      ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
      ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
      ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
      ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
      ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
      ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
      ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
      ni_iaaddressID = (*env)->GetFieldID(env, ni_iacls, "address", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
      ni_iafamilyID = (*env)->GetFieldID(env, ni_iacls, "family", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
      ni_iahostID = (*env)->GetFieldID(env, ni_iacls, "hostName", "Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
      ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
      initialized = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    if (IS_NULL(host)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        JNU_ThrowNullPointerException(env, "host is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    CHECK_NULL_RETURN(hostname, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    if (NET_addrtransAvailable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        static jfieldID ia_preferIPv6AddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (ia_preferIPv6AddressID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            jclass c = (*env)->FindClass(env,"java/net/InetAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (c)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                ia_preferIPv6AddressID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    (*env)->GetStaticFieldID(env, c, "preferIPv6Address", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (ia_preferIPv6AddressID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                JNU_ReleaseStringPlatformChars(env, host, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        /* get the address preference */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        preferIPv6Address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            = (*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        /* Try once, with our static buffer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        memset(&hints, 0, sizeof(hints));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        hints.ai_flags = AI_CANONNAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        hints.ai_family = AF_UNSPEC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        error = (*getaddrinfo_ptr)(hostname, NULL, &hints, &res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            /* report error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                            (char *)hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            JNU_ReleaseStringPlatformChars(env, host, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            int inetCount = 0, inet6Count = 0, inetIndex, inet6Index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            struct addrinfo *itr, *last, *iterator = res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            while (iterator != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                int skip = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                itr = resNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                while (itr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    if (iterator->ai_family == itr->ai_family &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        iterator->ai_addrlen == itr->ai_addrlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        if (itr->ai_family == AF_INET) { /* AF_INET */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                            struct sockaddr_in *addr1, *addr2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                            addr1 = (struct sockaddr_in *)iterator->ai_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                            addr2 = (struct sockaddr_in *)itr->ai_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                            if (addr1->sin_addr.s_addr ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                addr2->sin_addr.s_addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                skip = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                            int t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                            struct sockaddr_in6 *addr1, *addr2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                            addr1 = (struct sockaddr_in6 *)iterator->ai_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                            addr2 = (struct sockaddr_in6 *)itr->ai_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                            for (t = 0; t < 16; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                if (addr1->sin6_addr.s6_addr[t] !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                    addr2->sin6_addr.s6_addr[t]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                            if (t < 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                itr = itr->ai_next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                skip = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    } else if (iterator->ai_family != AF_INET &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                               iterator->ai_family != AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        /* we can't handle other family types */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        skip = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    itr = itr->ai_next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                if (!skip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    struct addrinfo *next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                        = (struct addrinfo*) malloc(sizeof(struct addrinfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    if (!next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    memcpy(next, iterator, sizeof(struct addrinfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    next->ai_next = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    if (resNew == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        resNew = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        last->ai_next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    last = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    if (iterator->ai_family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        inetCount ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    } else if (iterator->ai_family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        inet6Count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                iterator = iterator->ai_next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            retLen = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            iterator = resNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            ret = (*env)->NewObjectArray(env, retLen, ni_iacls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (IS_NULL(ret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                /* we may have memory to free at the end of this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (preferIPv6Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                inetIndex = inet6Count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                inet6Index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                inetIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                inet6Index = inetCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            while (iterator != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                if (iterator->ai_family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                  jobject iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                  if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                  (*env)->SetIntField(env, iaObj, ni_iaaddressID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                      ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr));
707
4e060643fb54 6698625: InetAddress.getLocalHost() failed in returning chinese local host name
chegar
parents: 2
diff changeset
   249
                  (*env)->SetObjectField(env, iaObj, ni_iahostID, host);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                  (*env)->SetObjectArrayElement(env, ret, inetIndex, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    inetIndex ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                } else if (iterator->ai_family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                  jint scope = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                  jbyteArray ipaddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                  jobject iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                  if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                  ipaddress = (*env)->NewByteArray(env, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                  if (IS_NULL(ipaddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                  (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                             (jbyte *)&(((struct sockaddr_in6*)iterator->ai_addr)->sin6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                  scope = ((struct sockaddr_in6*)iterator->ai_addr)->sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                  if (scope != 0) { /* zero is default value, no need to set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                  (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
707
4e060643fb54 6698625: InetAddress.getLocalHost() failed in returning chinese local host name
chegar
parents: 2
diff changeset
   273
                  (*env)->SetObjectField(env, iaObj, ni_iahostID, host);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                  (*env)->SetObjectArrayElement(env, ret, inet6Index, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                  inet6Index ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                iterator = iterator->ai_next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
cleanupAndReturn:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        struct addrinfo *iterator, *tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        iterator = resNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        while (iterator != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            tmp = iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            iterator = iterator->ai_next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            free(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        JNU_ReleaseStringPlatformChars(env, host, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    if (NET_addrtransAvailable())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        (*freeaddrinfo_ptr)(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 * Method:    getHostByAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 * Signature: (I)Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
Java_java_net_Inet6AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                            jbyteArray addrArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    jstring ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    char host[NI_MAXHOST+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    int error = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    if (NET_addrtransAvailable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        struct sockaddr_in him4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        struct sockaddr_in6 him6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        struct sockaddr *sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         * For IPv4 addresses construct a sockaddr_in structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if ((*env)->GetArrayLength(env, addrArray) == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            jint addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            addr = ((caddr[0]<<24) & 0xff000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            addr |= ((caddr[1] <<16) & 0xff0000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            addr |= ((caddr[2] <<8) & 0xff00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            addr |= (caddr[3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            memset((char *) &him4, 0, sizeof(him4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            him4.sin_addr.s_addr = (uint32_t) htonl(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            him4.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            sa = (struct sockaddr *) &him4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            len = sizeof(him4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
             * For IPv6 address construct a sockaddr_in6 structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            memset((char *) &him6, 0, sizeof(him6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            memcpy((void *)&(him6.sin6_addr), caddr, sizeof(struct in6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            him6.sin6_family = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            sa = (struct sockaddr *) &him6 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            len = sizeof(him6) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        error = (*getnameinfo_ptr)(sa, len, host, NI_MAXHOST, NULL, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                   NI_NAMEREQD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if (!error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            ret = (*env)->NewStringUTF(env, host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    if (ret == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
 * ping implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
 * Send a ICMP_ECHO_REQUEST packet every second until either the timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
 * expires or a answer is received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
 * Returns true is an ECHO_REPLY is received, otherwise, false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
static jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
ping6(JNIEnv *env, jint fd, struct SOCKADDR_IN6* him, jint timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
      struct SOCKADDR_IN6* netif, jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    jint size;
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 715
diff changeset
   375
    jint n, len, i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    char sendbuf[1500];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    char auxbuf[1500];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    unsigned char recvbuf[1500];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    struct icmp6_hdr *icmp6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    struct SOCKADDR_IN6 sa_recv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    unsigned short pid, seq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    int read_rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    WSAEVENT hEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    struct ip6_pseudo_hdr *pseudo_ip6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    int timestamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    int tmout2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /* Initialize the sequence number to a suitable random number and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
       shift right one place to allow sufficient room for increamenting. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    seq = ((unsigned short)rand()) >> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /* icmp_id is a 16 bit data type, therefore down cast the pid */
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 715
diff changeset
   393
    pid = (unsigned short) _getpid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    size = 60*1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char *)&size, sizeof(size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * A TTL was specified, let's set the socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    if (ttl > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
      setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (const char *) &ttl, sizeof(ttl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * A network interface was specified, let's bind to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    if (netif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
      if (NET_Bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in6)) < 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket to interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Make the socket non blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    hEvent = WSACreateEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * send 1 ICMP REQUEST every second until either we get a valid reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * or the timeout expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
      /* let's tag the ECHO packet with our pid so we can identify it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
      timestamp = GetCurrentTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
      memset(sendbuf, 0, 1500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
      icmp6 = (struct icmp6_hdr *) sendbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
      icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
      icmp6->icmp6_code = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
      icmp6->icmp6_id = htons(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
      icmp6->icmp6_seq = htons(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
      icmp6->icmp6_cksum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
      memcpy((icmp6 + 1), &timestamp, sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
      if (netif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        memset(auxbuf, 0, 1500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        pseudo_ip6 = (struct ip6_pseudo_hdr*) auxbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        memcpy(&pseudo_ip6->ip6_src, &netif->sin6_addr, sizeof(struct in6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        memcpy(&pseudo_ip6->ip6_dst, &him->sin6_addr, sizeof(struct in6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        pseudo_ip6->ip6_plen= htonl( 64 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        pseudo_ip6->ip6_nxt = htonl( IPPROTO_ICMPV6 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        memcpy(auxbuf + sizeof(struct ip6_pseudo_hdr), icmp6, 64);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         * We shouldn't have to do that as computing the checksum is supposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
         * to be done by the IPv6 stack. Unfortunately windows, here too, is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
         * uterly broken, or non compliant, so let's do it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
         * Problem is to compute the checksum I need to know the source address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
         * which happens only if I know the interface to be used...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        icmp6->icmp6_cksum = in_cksum((u_short *)pseudo_ip6, sizeof(struct ip6_pseudo_hdr) + 64);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
      /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
       * Ping!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
      n = sendto(fd, sendbuf, 64, 0, (struct sockaddr*) him, sizeof(struct sockaddr_in6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
      if (n < 0 && (WSAGetLastError() == WSAEINTR || WSAGetLastError() == WSAEADDRNOTAVAIL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        // Happens when using a "tunnel interface" for instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        // Or trying to send a packet on a different scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
      if (n < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        NET_ThrowNew(env, WSAGetLastError(), "Can't send ICMP packet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
      tmout2 = timeout > 1000 ? 1000 : timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
      do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        if (tmout2 >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
          len = sizeof(sa_recv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
          memset(recvbuf, 0, 1500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
          /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
           * For some unknown reason, besides plain stupidity, windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
           * truncates the first 4 bytes of the icmpv6 header some we can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
           * check for the ICMP_ECHOREPLY value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
           * we'll check the other values, though
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
          n = recvfrom(fd, recvbuf + 4, sizeof(recvbuf) - 4, 0, (struct sockaddr*) &sa_recv, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
          icmp6 = (struct icmp6_hdr *) (recvbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
          memcpy(&i, (icmp6 + 1), sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
          /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
           * Is that the reply we were expecting?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
          if (n >= 8 && ntohs(icmp6->icmp6_seq) == seq &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
              ntohs(icmp6->icmp6_id) == pid && i == timestamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
      } while (tmout2 > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
      timeout -= 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
      seq++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    } while (timeout > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
 * Method:    isReachable0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
 * Signature: ([bII[bI)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
Java_java_net_Inet6AddressImpl_isReachable0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                                           jbyteArray addrArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                                           jint scope,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                                           jint timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                                           jbyteArray ifArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                                           jint ttl, jint if_scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    jint fd, sz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    struct sockaddr_in6 him6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    struct sockaddr_in6* netif = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    struct sockaddr_in6 inf6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    WSAEVENT hEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    int connect_rv = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * If IPv6 is not enable, then we can't reach an IPv6 address, can we?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * Actually, we probably shouldn't even get here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    if (!ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
      return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * If it's an IPv4 address, ICMP won't work with IPv4 mapped address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * therefore, let's delegate to the Inet4Address method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    sz = (*env)->GetArrayLength(env, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    if (sz == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
      return Java_java_net_Inet4AddressImpl_isReachable0(env, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                                                         addrArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                                                         timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                                                         ifArray, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    memset((char *) caddr, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    memset((char *) &him6, 0, sizeof(him6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    memcpy((void *)&(him6.sin6_addr), caddr, sizeof(struct in6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    him6.sin6_family = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    if (scope > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
      him6.sin6_scope_id = scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    len = sizeof(struct sockaddr_in6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * A network interface was specified, let's convert the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    if (!(IS_NULL(ifArray))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
      memset((char *) caddr, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
      memset((char *) &inf6, 0, sizeof(inf6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
      (*env)->GetByteArrayRegion(env, ifArray, 0, 16, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
      memcpy((void *)&(inf6.sin6_addr), caddr, sizeof(struct in6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
      inf6.sin6_family = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
      inf6.sin6_port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
      inf6.sin6_scope_id = if_scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
      netif = &inf6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
#if 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * Windows implementation of ICMP & RAW sockets is too unreliable for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * Therefore it's best not to try it at all and rely only on TCP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * We may revisit and enable this code in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * Right now, windows doesn't generate the ICMP checksum automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * so we have to compute it, but we can do it only if we know which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * interface will be used. Therefore, don't try to use ICMP if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * interface was specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * When ICMPv6 support improves in windows, we may change this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    if (!(IS_NULL(ifArray))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
       * If we can create a RAW socket, then when can use the ICMP ECHO_REQUEST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
       * otherwise we'll try a tcp socket to the Echo port (7).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
       * Note that this is empiric, and not connecting could mean it's blocked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
       * or the echo servioe has been disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
      fd = NET_Socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
      if (fd != -1) { /* Good to go, let's do a ping */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        return ping6(env, fd, &him6, timeout, netif, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    /* No good, let's fall back on TCP */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    fd = NET_Socket(AF_INET6, SOCK_STREAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    if (fd == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        /* note: if you run out of fds, you may not be able to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
         * the exception class, and get a NoClassDefFoundError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         * instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        NET_ThrowNew(env, errno, "Can't create socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * A TTL was specified, let's set the socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    if (ttl > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
      setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (const char *)&ttl, sizeof(ttl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * A network interface was specified, let's bind to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    if (netif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
      if (NET_Bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in6)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket to interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * Make the socket non blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    hEvent = WSACreateEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    /* no need to use NET_Connect as non-blocking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    him6.sin6_port = htons((short) 7); /* Echo port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    connect_rv = connect(fd, (struct sockaddr *)&him6, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * connection established or refused immediately, either way it means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * we were able to reach the host!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    if (connect_rv == 0 || WSAGetLastError() == WSAECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        int optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        switch (WSAGetLastError()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        case WSAEHOSTUNREACH:   /* Host Unreachable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        case WSAENETUNREACH:    /* Network Unreachable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        case WSAENETDOWN:       /* Network is down */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        case WSAEPFNOSUPPORT:   /* Protocol Family unsupported */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
          WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
          closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
          return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        if (WSAGetLastError() != WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                                         "connect failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        if (timeout >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
          /* has connection been established? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
          optlen = sizeof(connect_rv);
7814
dff0b40f9ac8 7010192: InetAddress.isReachable hits ShouldNotReachHere with hs20-b04 (win)
alanb
parents: 5506
diff changeset
   674
          if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
dff0b40f9ac8 7010192: InetAddress.isReachable hits ShouldNotReachHere with hs20-b04 (win)
alanb
parents: 5506
diff changeset
   675
                         &optlen) <0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            connect_rv = WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
          if (connect_rv == 0 || connect_rv == WSAECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
}