jdk/src/solaris/native/java/net/Inet6AddressImpl.c
author chegar
Mon, 28 Jul 2008 13:02:11 +0100
changeset 910 1f53246fb014
parent 715 f16baef3a20e
child 2056 115e09b7a004
permissions -rw-r--r--
6729881: Compiler warning in networking native code Summary: Cleanup compiler warnings Reviewed-by: alanb, jccollet, michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 707
diff changeset
     2
 * Copyright 2000-2008 Sun Microsystems, Inc.  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
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 <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <sys/socket.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <netinet/in.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <netdb.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <strings.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#ifndef IPV6_DEFS_H
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include <netinet/icmp6.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include "java_net_Inet4AddressImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include "java_net_Inet6AddressImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/* the initial size of our hostent buffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#ifndef NI_MAXHOST
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#define NI_MAXHOST 1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#ifndef __GLIBC__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/* gethostname() is in libc.so but I can't find a header file for it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
extern int gethostname(char *buf, int buf_len);
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[NI_MAXHOST+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    hostname[0] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    if (JVM_GetHostName(hostname, MAXHOSTNAMELEN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        /* Something went wrong, maybe networking is not setup? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        strcpy(hostname, "localhost");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        /* On Linux gethostname() says "host.domain.sun.com".  On
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
         * Solaris gethostname() says "host", so extra work is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        /* Solaris doesn't want to give us a fully qualified domain name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
         * We do a reverse lookup to try and get one.  This works
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
         * if DNS occurs before NIS in /etc/resolv.conf, but fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
         * if NIS comes first (it still gets only a partial name).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
         * We use thread-safe system calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (NET_addrtransAvailable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            struct addrinfo  hints, *res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            int error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            bzero(&hints, sizeof(hints));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            hints.ai_flags = AI_CANONNAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            hints.ai_family = AF_UNSPEC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            error = (*getaddrinfo_ptr)(hostname, NULL, &hints, &res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (error == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                /* host is known to name service */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                error = (*getnameinfo_ptr)(res->ai_addr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                           res->ai_addrlen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                           hostname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                           NI_MAXHOST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                           NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                           0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                           NI_NAMEREQD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                /* if getnameinfo fails hostname is still the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                   from gethostname */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                (*freeaddrinfo_ptr)(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
#endif /* __linux__ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    return (*env)->NewStringUTF(env, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
static jclass ni_iacls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
static jclass ni_ia4cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
static jclass ni_ia6cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
static jmethodID ni_ia4ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
static jmethodID ni_ia6ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
static jfieldID ni_iaaddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
static jfieldID ni_iahostID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
static jfieldID ni_iafamilyID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
static jfieldID ni_ia6ipaddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
static int initialized = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * Find an internet address for a given hostname.  Not this this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * code only works for addresses of type INET. The translation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * of %d.%d.%d.%d to an address (int) occurs in java now, so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * String "host" shouldn't *ever* be a %d.%d.%d.%d string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * Method:    lookupAllHostAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * Signature: (Ljava/lang/String;)[[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
JNIEXPORT jobjectArray JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                                jstring host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    const char *hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    jobjectArray ret = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    int retLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    jboolean preferIPv6Address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    int error=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    struct addrinfo hints, *res, *resNew = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
      ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
      ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
      ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
      ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
      ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
      ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
      ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
      ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
      ni_iaaddressID = (*env)->GetFieldID(env, ni_iacls, "address", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
      ni_iafamilyID = (*env)->GetFieldID(env, ni_iacls, "family", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
      ni_iahostID = (*env)->GetFieldID(env, ni_iacls, "hostName", "Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
      ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
      initialized = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    if (IS_NULL(host)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        JNU_ThrowNullPointerException(env, "host is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    CHECK_NULL_RETURN(hostname, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    if (NET_addrtransAvailable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        static jfieldID ia_preferIPv6AddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (ia_preferIPv6AddressID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            jclass c = (*env)->FindClass(env,"java/net/InetAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (c)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                ia_preferIPv6AddressID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    (*env)->GetStaticFieldID(env, c, "preferIPv6Address", "Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (ia_preferIPv6AddressID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                JNU_ReleaseStringPlatformChars(env, host, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        /* get the address preference */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        preferIPv6Address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            = (*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        /* Try once, with our static buffer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        bzero(&hints, sizeof(hints));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        hints.ai_flags = AI_CANONNAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        hints.ai_family = AF_UNSPEC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
         * Workaround for Solaris bug 4160367 - if a hostname contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
         * white space then 0.0.0.0 is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (isspace(hostname[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                            (char *)hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            JNU_ReleaseStringPlatformChars(env, host, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        error = (*getaddrinfo_ptr)(hostname, NULL, &hints, &res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            /* report error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                            (char *)hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            JNU_ReleaseStringPlatformChars(env, host, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            int inetCount = 0, inet6Count = 0, inetIndex, inet6Index;
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 715
diff changeset
   221
            struct addrinfo *itr, *last = NULL, *iterator = res;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            while (iterator != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                int skip = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                itr = resNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                while (itr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    if (iterator->ai_family == itr->ai_family &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                        iterator->ai_addrlen == itr->ai_addrlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                        if (itr->ai_family == AF_INET) { /* AF_INET */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                            struct sockaddr_in *addr1, *addr2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                            addr1 = (struct sockaddr_in *)iterator->ai_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                            addr2 = (struct sockaddr_in *)itr->ai_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                            if (addr1->sin_addr.s_addr ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                addr2->sin_addr.s_addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                skip = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                            int t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                            struct sockaddr_in6 *addr1, *addr2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                            addr1 = (struct sockaddr_in6 *)iterator->ai_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                            addr2 = (struct sockaddr_in6 *)itr->ai_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                            for (t = 0; t < 16; t++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                if (addr1->sin6_addr.s6_addr[t] !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                    addr2->sin6_addr.s6_addr[t]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                            if (t < 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                                itr = itr->ai_next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                skip = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    } else if (iterator->ai_family != AF_INET &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                               iterator->ai_family != AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        /* we can't handle other family types */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                        skip = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    itr = itr->ai_next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                if (!skip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    struct addrinfo *next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        = (struct addrinfo*) malloc(sizeof(struct addrinfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    if (!next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                        JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                        ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    memcpy(next, iterator, sizeof(struct addrinfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    next->ai_next = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    if (resNew == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                        resNew = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        last->ai_next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    last = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    if (iterator->ai_family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        inetCount ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    } else if (iterator->ai_family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                        inet6Count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                iterator = iterator->ai_next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            retLen = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            iterator = resNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            ret = (*env)->NewObjectArray(env, retLen, ni_iacls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (IS_NULL(ret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                /* we may have memory to free at the end of this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (preferIPv6Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                /* AF_INET addresses will be offset by inet6Count */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                inetIndex = inet6Count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                inet6Index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                /* AF_INET6 addresses will be offset by inetCount */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                inetIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                inet6Index = inetCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            while (iterator != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
              if (iterator->ai_family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                jobject iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                  ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                  goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                (*env)->SetIntField(env, iaObj, ni_iaaddressID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                    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
   320
                (*env)->SetObjectField(env, iaObj, ni_iahostID, host);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                (*env)->SetObjectArrayElement(env, ret, inetIndex, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                inetIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
              } else if (iterator->ai_family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                jint scope = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                jbyteArray ipaddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                jobject iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                  ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                  goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                ipaddress = (*env)->NewByteArray(env, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                if (IS_NULL(ipaddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                  ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                  goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                           (jbyte *)&(((struct sockaddr_in6*)iterator->ai_addr)->sin6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                if (!kernelIsV22()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                  scope = ((struct sockaddr_in6*)iterator->ai_addr)->sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                scope = ((struct sockaddr_in6*)iterator->ai_addr)->sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                if (scope != 0) { /* zero is default value, no need to set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                  (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                  (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
707
4e060643fb54 6698625: InetAddress.getLocalHost() failed in returning chinese local host name
chegar
parents: 2
diff changeset
   351
                (*env)->SetObjectField(env, iaObj, ni_iahostID, host);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                (*env)->SetObjectArrayElement(env, ret, inet6Index, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                inet6Index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
              iterator = iterator->ai_next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
cleanupAndReturn:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        struct addrinfo *iterator, *tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        iterator = resNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        while (iterator != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            tmp = iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            iterator = iterator->ai_next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            free(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        JNU_ReleaseStringPlatformChars(env, host, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    if (NET_addrtransAvailable())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        (*freeaddrinfo_ptr)(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
 * Method:    getHostByAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
 * Signature: (I)Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
Java_java_net_Inet6AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                                            jbyteArray addrArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    jstring ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    char host[NI_MAXHOST+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    int error = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    if (NET_addrtransAvailable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        struct sockaddr_in him4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        struct sockaddr_in6 him6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        struct sockaddr *sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         * For IPv4 addresses construct a sockaddr_in structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if ((*env)->GetArrayLength(env, addrArray) == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            jint addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            addr = ((caddr[0]<<24) & 0xff000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            addr |= ((caddr[1] <<16) & 0xff0000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            addr |= ((caddr[2] <<8) & 0xff00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            addr |= (caddr[3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            memset((char *) &him4, 0, sizeof(him4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            him4.sin_addr.s_addr = (uint32_t) htonl(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            him4.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            sa = (struct sockaddr *) &him4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            len = sizeof(him4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
             * For IPv6 address construct a sockaddr_in6 structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            memset((char *) &him6, 0, sizeof(him6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            memcpy((void *)&(him6.sin6_addr), caddr, sizeof(struct in6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            him6.sin6_family = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            sa = (struct sockaddr *) &him6 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            len = sizeof(him6) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        error = (*getnameinfo_ptr)(sa, len, host, NI_MAXHOST, NULL, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                                   NI_NAMEREQD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        if (!error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            ret = (*env)->NewStringUTF(env, host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    if (ret == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
#define SET_NONBLOCKING(fd) {           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        int flags = fcntl(fd, F_GETFL); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        flags |= O_NONBLOCK;            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        fcntl(fd, F_SETFL, flags);      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
static jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
ping6(JNIEnv *env, jint fd, struct sockaddr_in6* him, jint timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
      struct sockaddr_in6* netif, jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    jint size;
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 715
diff changeset
   458
    jint n, len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    char sendbuf[1500];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    unsigned char recvbuf[1500];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    struct icmp6_hdr *icmp6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    struct sockaddr_in6 sa_recv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    jbyte *caddr, *recv_caddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    jchar pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    jint tmout2, seq = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    struct timeval tv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    size_t plen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    int csum_offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * For some strange reason, the linux kernel won't calculate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * checksum of ICMPv6 packets unless you set this socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    csum_offset = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    setsockopt(fd, SOL_RAW, IPV6_CHECKSUM, &csum_offset, sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    caddr = (jbyte *)&(him->sin6_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /* icmp_id is a 16 bit data type, therefore down cast the pid */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    pid = (jchar)getpid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    size = 60*1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    if (ttl > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
      setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    if (netif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
      if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in6)) <0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        NET_ThrowNew(env, errno, "Can't bind socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    SET_NONBLOCKING(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
      icmp6 = (struct icmp6_hdr *) sendbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
      icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
      icmp6->icmp6_code = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
      /* let's tag the ECHO packet with our pid so we can identify it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
      icmp6->icmp6_id = htons(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
      icmp6->icmp6_seq = htons(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
      seq++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
      icmp6->icmp6_cksum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
      gettimeofday(&tv, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
      memcpy(sendbuf + sizeof(struct icmp6_hdr), &tv, sizeof(tv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
      plen = sizeof(struct icmp6_hdr) + sizeof(tv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
      n = sendto(fd, sendbuf, plen, 0, (struct sockaddr*) him, sizeof(struct sockaddr_in6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
      if (n < 0 && errno != EINPROGRESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        NET_ThrowNew(env, errno, "Can't send ICMP packet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
      tmout2 = timeout > 1000 ? 1000 : timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
      do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        if (tmout2 >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
          len = sizeof(sa_recv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
          n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr*) &sa_recv, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
          icmp6 = (struct icmp6_hdr *) (recvbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
          recv_caddr = (jbyte *)&(sa_recv.sin6_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
          /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
           * We did receive something, but is it what we were expecting?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
           * I.E.: An ICMP6_ECHO_REPLY packet with the proper PID and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
           *       from the host that we are trying to determine is reachable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
          if (n >= 8 && icmp6->icmp6_type == ICMP6_ECHO_REPLY &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
              (ntohs(icmp6->icmp6_id) == pid) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
              NET_IsEqual(caddr, recv_caddr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
      } while (tmout2 > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
      timeout -= 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    } while (timeout > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
 * Method:    isReachable0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
 * Signature: ([bII[bI)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
Java_java_net_Inet6AddressImpl_isReachable0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                                           jbyteArray addrArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                                           jint scope,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                                           jint timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                                           jbyteArray ifArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                                           jint ttl, jint if_scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    jint fd, sz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    struct sockaddr_in6 him6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    struct sockaddr_in6 inf6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    struct sockaddr_in6* netif = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    int connect_rv = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * If IPv6 is not enable, then we can't reach an IPv6 address, can we?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    if (!ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
      return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * If it's an IPv4 address, ICMP won't work with IPv4 mapped address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * therefore, let's delegate to the Inet4Address method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    sz = (*env)->GetArrayLength(env, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    if (sz == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
      return Java_java_net_Inet4AddressImpl_isReachable0(env, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                                                         addrArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                                                         timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                                                         ifArray, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    memset((char *) caddr, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    memset((char *) &him6, 0, sizeof(him6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    memcpy((void *)&(him6.sin6_addr), caddr, sizeof(struct in6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    him6.sin6_family = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    if (scope > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
      him6.sin6_scope_id = scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
      him6.sin6_scope_id = getDefaultIPv6Interface( &(him6.sin6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    len = sizeof(struct sockaddr_in6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    if (scope > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
      him6.sin6_scope_id = scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    len = sizeof(struct sockaddr_in6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * If a network interface was specified, let's create the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    if (!(IS_NULL(ifArray))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
      memset((char *) caddr, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
      memset((char *) &inf6, 0, sizeof(inf6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
      (*env)->GetByteArrayRegion(env, ifArray, 0, 16, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
      memcpy((void *)&(inf6.sin6_addr), caddr, sizeof(struct in6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
      inf6.sin6_family = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
      inf6.sin6_scope_id = if_scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
      netif = &inf6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * If we can create a RAW socket, then when can use the ICMP ECHO_REQUEST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * otherwise we'll try a tcp socket to the Echo port (7).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * Note that this is empiric, and not connecting could mean it's blocked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * or the echo servioe has been disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    fd = JVM_Socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    if (fd != -1) { /* Good to go, let's do a ping */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        return ping6(env, fd, &him6, timeout, netif, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    /* No good, let's fall back on TCP */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    fd = JVM_Socket(AF_INET6, SOCK_STREAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    if (fd == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        /* note: if you run out of fds, you may not be able to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
         * the exception class, and get a NoClassDefFoundError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         * instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        NET_ThrowNew(env, errno, "Can't create socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    if (ttl > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
      setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * A network interface was specified, so let's bind to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    if (netif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
      if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in6)) <0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        NET_ThrowNew(env, errno, "Can't bind socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    SET_NONBLOCKING(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /* no need to use NET_Connect as non-blocking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    him6.sin6_port = htons((short) 7); /* Echo port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    connect_rv = JVM_Connect(fd, (struct sockaddr *)&him6, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * connection established or refused immediately, either way it means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * we were able to reach the host!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    if (connect_rv == 0 || errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        int optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        switch (errno) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        case ENETUNREACH: /* Network Unreachable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        case EAFNOSUPPORT: /* Address Family not supported */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        case EADDRNOTAVAIL: /* address is not available on  the  remote machine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        case EINVAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
          /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
           * On some Linuxes, when bound to the loopback interface, connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
           * will fail and errno will be set to EINVAL. When that happens,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
           * don't throw an exception, just return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
#endif /* __linux__ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
          close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
          return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        if (errno != EINPROGRESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                                         "connect failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        if (timeout >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
          /* has connection been established */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
          optlen = sizeof(connect_rv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
          if (JVM_GetSockOpt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                             &optlen) <0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            connect_rv = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
          if (connect_rv == 0 || ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
#else /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
}