jdk/src/windows/native/java/net/Inet6AddressImpl.c
author ohair
Tue, 25 Mar 2008 14:38:56 -0700
changeset 20 41658053480c
parent 2 90ce3da70b43
child 707 4e060643fb54
permissions -rw-r--r--
6623832: Cleanup old j2se makefile targets Summary: Just removing unneeded makefile rules and 'control' logic. Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "java_net_InetAddress.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "java_net_Inet4AddressImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "java_net_Inet6AddressImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include "icmp.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#ifdef WIN32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#ifndef _WIN64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/* Retain this code a little longer to support building in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * old environments.  _MSC_VER is defined as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *     1200 for MSVC++ 6.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *     1310 for Vc7
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#if defined(_MSC_VER) && _MSC_VER < 1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#define sockaddr_in6 SOCKADDR_IN6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#define uint32_t UINT32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Method:    getLocalHostName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Signature: ()Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
Java_java_net_Inet6AddressImpl_getLocalHostName (JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    char hostname [256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    if (gethostname (hostname, sizeof (hostname)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        strcpy (hostname, "localhost");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    return JNU_NewStringPlatform (env, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
static jclass ni_iacls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
static jclass ni_ia4cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
static jclass ni_ia6cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
static jmethodID ni_ia4ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
static jmethodID ni_ia6ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
static jfieldID ni_iaaddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
static jfieldID ni_iahostID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
static jfieldID ni_iafamilyID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
static jfieldID ni_ia6ipaddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
static int initialized = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
JNIEXPORT jobjectArray JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                                jstring host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    const char *hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    jobject name;
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
            name = (*env)->NewStringUTF(env, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (IS_NULL(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
              ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
              goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            while (iterator != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                if (iterator->ai_family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                  jobject iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                  if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                  (*env)->SetIntField(env, iaObj, ni_iaaddressID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                      ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                  (*env)->SetObjectField(env, iaObj, ni_iahostID, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                  (*env)->SetObjectArrayElement(env, ret, inetIndex, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    inetIndex ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                } else if (iterator->ai_family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                  jint scope = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                  jbyteArray ipaddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                  jobject iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                  if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                  ipaddress = (*env)->NewByteArray(env, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                  if (IS_NULL(ipaddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                  (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                                             (jbyte *)&(((struct sockaddr_in6*)iterator->ai_addr)->sin6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                  scope = ((struct sockaddr_in6*)iterator->ai_addr)->sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                  if (scope != 0) { /* zero is default value, no need to set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                  (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                  (*env)->SetObjectField(env, iaObj, ni_iahostID, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                  (*env)->SetObjectArrayElement(env, ret, inet6Index, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                  inet6Index ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                iterator = iterator->ai_next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
cleanupAndReturn:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        struct addrinfo *iterator, *tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        iterator = resNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        while (iterator != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            tmp = iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            iterator = iterator->ai_next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            free(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        JNU_ReleaseStringPlatformChars(env, host, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    if (NET_addrtransAvailable())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        (*freeaddrinfo_ptr)(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
 * Method:    getHostByAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
 * Signature: (I)Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
Java_java_net_Inet6AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                            jbyteArray addrArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    jstring ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    char host[NI_MAXHOST+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    int error = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    jint family;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    struct sockaddr *him ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    if (NET_addrtransAvailable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        struct sockaddr_in him4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        struct sockaddr_in6 him6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        struct sockaddr *sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         * For IPv4 addresses construct a sockaddr_in structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if ((*env)->GetArrayLength(env, addrArray) == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            jint addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            addr = ((caddr[0]<<24) & 0xff000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            addr |= ((caddr[1] <<16) & 0xff0000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            addr |= ((caddr[2] <<8) & 0xff00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            addr |= (caddr[3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            memset((char *) &him4, 0, sizeof(him4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            him4.sin_addr.s_addr = (uint32_t) htonl(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            him4.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            sa = (struct sockaddr *) &him4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            len = sizeof(him4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
             * For IPv6 address construct a sockaddr_in6 structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            memset((char *) &him6, 0, sizeof(him6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            memcpy((void *)&(him6.sin6_addr), caddr, sizeof(struct in6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            him6.sin6_family = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            sa = (struct sockaddr *) &him6 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            len = sizeof(him6) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        error = (*getnameinfo_ptr)(sa, len, host, NI_MAXHOST, NULL, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                   NI_NAMEREQD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        if (!error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            ret = (*env)->NewStringUTF(env, host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    if (ret == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
 * ping implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
 * Send a ICMP_ECHO_REQUEST packet every second until either the timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
 * expires or a answer is received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
 * Returns true is an ECHO_REPLY is received, otherwise, false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
static jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
ping6(JNIEnv *env, jint fd, struct SOCKADDR_IN6* him, jint timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
      struct SOCKADDR_IN6* netif, jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    jint size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    jint n, len, hlen1, icmplen, i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    char sendbuf[1500];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    char auxbuf[1500];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    unsigned char recvbuf[1500];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    struct icmp6_hdr *icmp6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    struct SOCKADDR_IN6 sa_recv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    unsigned short pid, seq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    int read_rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    WSAEVENT hEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    struct ip6_pseudo_hdr *pseudo_ip6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    int timestamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    int tmout2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /* Initialize the sequence number to a suitable random number and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
       shift right one place to allow sufficient room for increamenting. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    seq = ((unsigned short)rand()) >> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /* icmp_id is a 16 bit data type, therefore down cast the pid */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    pid = (unsigned short) getpid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    size = 60*1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char *)&size, sizeof(size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * A TTL was specified, let's set the socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    if (ttl > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
      setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (const char *) &ttl, sizeof(ttl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * A network interface was specified, let's bind to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    if (netif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
      if (NET_Bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in6)) < 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket to interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Make the socket non blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    hEvent = WSACreateEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * send 1 ICMP REQUEST every second until either we get a valid reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * or the timeout expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
      /* let's tag the ECHO packet with our pid so we can identify it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
      timestamp = GetCurrentTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
      memset(sendbuf, 0, 1500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
      icmp6 = (struct icmp6_hdr *) sendbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
      icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
      icmp6->icmp6_code = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
      icmp6->icmp6_id = htons(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
      icmp6->icmp6_seq = htons(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
      icmp6->icmp6_cksum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
      memcpy((icmp6 + 1), &timestamp, sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
      if (netif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        memset(auxbuf, 0, 1500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        pseudo_ip6 = (struct ip6_pseudo_hdr*) auxbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        memcpy(&pseudo_ip6->ip6_src, &netif->sin6_addr, sizeof(struct in6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        memcpy(&pseudo_ip6->ip6_dst, &him->sin6_addr, sizeof(struct in6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        pseudo_ip6->ip6_plen= htonl( 64 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        pseudo_ip6->ip6_nxt = htonl( IPPROTO_ICMPV6 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        memcpy(auxbuf + sizeof(struct ip6_pseudo_hdr), icmp6, 64);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
         * We shouldn't have to do that as computing the checksum is supposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
         * to be done by the IPv6 stack. Unfortunately windows, here too, is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
         * uterly broken, or non compliant, so let's do it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
         * Problem is to compute the checksum I need to know the source address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
         * which happens only if I know the interface to be used...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        icmp6->icmp6_cksum = in_cksum((u_short *)pseudo_ip6, sizeof(struct ip6_pseudo_hdr) + 64);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
      /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
       * Ping!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
      n = sendto(fd, sendbuf, 64, 0, (struct sockaddr*) him, sizeof(struct sockaddr_in6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
      if (n < 0 && (WSAGetLastError() == WSAEINTR || WSAGetLastError() == WSAEADDRNOTAVAIL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        // Happens when using a "tunnel interface" for instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        // Or trying to send a packet on a different scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
      if (n < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        NET_ThrowNew(env, WSAGetLastError(), "Can't send ICMP packet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
      tmout2 = timeout > 1000 ? 1000 : timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
      do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        if (tmout2 >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
          len = sizeof(sa_recv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
          memset(recvbuf, 0, 1500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
          /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
           * For some unknown reason, besides plain stupidity, windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
           * truncates the first 4 bytes of the icmpv6 header some we can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
           * check for the ICMP_ECHOREPLY value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
           * we'll check the other values, though
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
          n = recvfrom(fd, recvbuf + 4, sizeof(recvbuf) - 4, 0, (struct sockaddr*) &sa_recv, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
          icmp6 = (struct icmp6_hdr *) (recvbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
          memcpy(&i, (icmp6 + 1), sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
          /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
           * Is that the reply we were expecting?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
          if (n >= 8 && ntohs(icmp6->icmp6_seq) == seq &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
              ntohs(icmp6->icmp6_id) == pid && i == timestamp) {
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_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
      } while (tmout2 > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
      timeout -= 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
      seq++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    } while (timeout > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
 * Method:    isReachable0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
 * Signature: ([bII[bI)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
Java_java_net_Inet6AddressImpl_isReachable0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                                           jbyteArray addrArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                                           jint scope,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                                           jint timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                                           jbyteArray ifArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                                           jint ttl, jint if_scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    jint addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    jint fd, sz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    struct sockaddr_in6 him6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    struct sockaddr_in6* netif = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    struct sockaddr_in6 inf6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    WSAEVENT hEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    int connect_rv = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * If IPv6 is not enable, then we can't reach an IPv6 address, can we?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Actually, we probably shouldn't even get here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    if (!ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
      return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * If it's an IPv4 address, ICMP won't work with IPv4 mapped address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * therefore, let's delegate to the Inet4Address method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    sz = (*env)->GetArrayLength(env, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    if (sz == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
      return Java_java_net_Inet4AddressImpl_isReachable0(env, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                                                         addrArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                                                         timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                                                         ifArray, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    memset((char *) caddr, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    memset((char *) &him6, 0, sizeof(him6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    memcpy((void *)&(him6.sin6_addr), caddr, sizeof(struct in6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    him6.sin6_family = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    if (scope > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
      him6.sin6_scope_id = scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    len = sizeof(struct sockaddr_in6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * A network interface was specified, let's convert the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    if (!(IS_NULL(ifArray))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
      memset((char *) caddr, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
      memset((char *) &inf6, 0, sizeof(inf6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
      (*env)->GetByteArrayRegion(env, ifArray, 0, 16, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
      memcpy((void *)&(inf6.sin6_addr), caddr, sizeof(struct in6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
      inf6.sin6_family = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
      inf6.sin6_port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
      inf6.sin6_scope_id = if_scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
      netif = &inf6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
#if 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * Windows implementation of ICMP & RAW sockets is too unreliable for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * Therefore it's best not to try it at all and rely only on TCP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * We may revisit and enable this code in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * Right now, windows doesn't generate the ICMP checksum automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * so we have to compute it, but we can do it only if we know which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * interface will be used. Therefore, don't try to use ICMP if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * interface was specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * When ICMPv6 support improves in windows, we may change this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    if (!(IS_NULL(ifArray))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
       * If we can create a RAW socket, then when can use the ICMP ECHO_REQUEST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
       * otherwise we'll try a tcp socket to the Echo port (7).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
       * Note that this is empiric, and not connecting could mean it's blocked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
       * or the echo servioe has been disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
      fd = NET_Socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
      if (fd != -1) { /* Good to go, let's do a ping */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return ping6(env, fd, &him6, timeout, netif, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    /* No good, let's fall back on TCP */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    fd = NET_Socket(AF_INET6, SOCK_STREAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    if (fd == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        /* note: if you run out of fds, you may not be able to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
         * the exception class, and get a NoClassDefFoundError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
         * instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        NET_ThrowNew(env, errno, "Can't create socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * A TTL was specified, let's set the socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    if (ttl > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
      setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (const char *)&ttl, sizeof(ttl));
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
     * A network interface was specified, let's bind to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    if (netif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
      if (NET_Bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in6)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket to interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * Make the socket non blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    hEvent = WSACreateEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /* no need to use NET_Connect as non-blocking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    him6.sin6_port = htons((short) 7); /* Echo port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    connect_rv = connect(fd, (struct sockaddr *)&him6, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * connection established or refused immediately, either way it means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * we were able to reach the host!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    if (connect_rv == 0 || WSAGetLastError() == WSAECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        int optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        switch (WSAGetLastError()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        case WSAEHOSTUNREACH:   /* Host Unreachable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        case WSAENETUNREACH:    /* Network Unreachable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        case WSAENETDOWN:       /* Network is down */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        case WSAEPFNOSUPPORT:   /* Protocol Family unsupported */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
          WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
          closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
          return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        if (WSAGetLastError() != WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                                         "connect failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        if (timeout >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
          /* has connection been established? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
          optlen = sizeof(connect_rv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
          if (JVM_GetSockOpt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                             &optlen) <0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            connect_rv = WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
          if (connect_rv == 0 || connect_rv == WSAECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
}