jdk/src/java.base/unix/native/libnet/Inet6AddressImpl.c
author clanger
Thu, 27 Oct 2016 11:00:28 +0200
changeset 41771 18c9669e76ca
parent 41380 c27cf95dd7e6
child 41834 99c4f72c0a91
permissions -rw-r--r--
8167481: cleanup of headers and includes for native libnet Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
     2
 * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4814
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4814
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4814
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4814
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4814
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    25
#include <ctype.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <errno.h>
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    27
#include <stdlib.h>
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    28
#include <string.h>
3226
b142b3d06bf5 6562614: Compiler warnings for gettimeofday in Inet4/Inet6AddressImpl.c
andrew
parents: 2056
diff changeset
    29
#include <sys/time.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <netinet/in.h>
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    32
#include <netinet/icmp6.h>
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    33
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    34
#if defined(_ALLBSD_SOURCE)
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
    35
#include <ifaddrs.h>
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
    36
#include <net/if.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    37
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include "java_net_Inet4AddressImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include "java_net_Inet6AddressImpl.h"
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
    43
#include "java_net_InetAddress.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/* the initial size of our hostent buffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#ifndef NI_MAXHOST
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#define NI_MAXHOST 1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Method:    getLocalHostName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Signature: ()Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
Java_java_net_Inet6AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
22956
322ce28a7e20 8034182: Misc. warnings in java.net code
alanb
parents: 22646
diff changeset
    62
    int ret;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    char hostname[NI_MAXHOST+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    hostname[0] = '\0';
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
    66
    ret = gethostname(hostname, NI_MAXHOST);
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
    67
    if (ret == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        /* Something went wrong, maybe networking is not setup? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        strcpy(hostname, "localhost");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    } else {
8774
4403ce77f306 7024560: InetAddress.getLocalHost().getHostName() returns localhost for hostnames of length HOST_NAME_MAX
chegar
parents: 7668
diff changeset
    71
        // ensure null-terminated
4403ce77f306 7024560: InetAddress.getLocalHost().getHostName() returns localhost for hostnames of length HOST_NAME_MAX
chegar
parents: 7668
diff changeset
    72
        hostname[NI_MAXHOST] = '\0';
22956
322ce28a7e20 8034182: Misc. warnings in java.net code
alanb
parents: 22646
diff changeset
    73
    }
322ce28a7e20 8034182: Misc. warnings in java.net code
alanb
parents: 22646
diff changeset
    74
322ce28a7e20 8034182: Misc. warnings in java.net code
alanb
parents: 22646
diff changeset
    75
#if defined(__solaris__) && defined(AF_INET6)
322ce28a7e20 8034182: Misc. warnings in java.net code
alanb
parents: 22646
diff changeset
    76
    if (ret == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        /* Solaris doesn't want to give us a fully qualified domain name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
         * We do a reverse lookup to try and get one.  This works
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
         * if DNS occurs before NIS in /etc/resolv.conf, but fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
         * if NIS comes first (it still gets only a partial name).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
         * We use thread-safe system calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
         */
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
    83
        struct addrinfo  hints, *res;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
    84
        int error;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
11033
ef047e386299 7114558: Inet4AddressImpl should use memset (rather than bzero) and NI_MAXHOST (rather than MAXHOSTNAMELEN)
ngmr
parents: 11032
diff changeset
    86
        memset(&hints, 0, sizeof(hints));
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
    87
        hints.ai_flags = AI_CANONNAME;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
    88
        hints.ai_family = AF_UNSPEC;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
    90
        error = getaddrinfo(hostname, NULL, &hints, &res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
    92
        if (error == 0) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
    93
            /* host is known to name service */
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
    94
            error = getnameinfo(res->ai_addr,
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
    95
                                res->ai_addrlen,
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
    96
                                hostname,
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
    97
                                NI_MAXHOST,
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
    98
                                NULL,
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
    99
                                0,
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   100
                                NI_NAMEREQD);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   102
            /* if getnameinfo fails hostname is still the value
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   103
               from gethostname */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   105
            freeaddrinfo(res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
22956
322ce28a7e20 8034182: Misc. warnings in java.net code
alanb
parents: 22646
diff changeset
   108
#endif
322ce28a7e20 8034182: Misc. warnings in java.net code
alanb
parents: 22646
diff changeset
   109
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    return (*env)->NewStringUTF(env, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   113
#ifdef MACOSX
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   114
/* also called from Inet4AddressImpl.c */
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   115
__private_extern__ jobjectArray
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   116
lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   117
{
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   118
    jobjectArray result = NULL;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   119
    char myhostname[NI_MAXHOST+1];
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   120
    struct ifaddrs *ifa = NULL;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   121
    int familyOrder = 0;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   122
    int count = 0, i, j;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   123
    int addrs4 = 0, addrs6 = 0, numV4Loopbacks = 0, numV6Loopbacks = 0;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   124
    jboolean includeLoopback = JNI_FALSE;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   125
    jobject name;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   126
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
   127
    initInetAddressIDs(env);
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
   128
    JNU_CHECK_EXCEPTION_RETURN(env, NULL);
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   129
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   130
    /* If the requested name matches this host's hostname, return IP addresses
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   131
     * from all attached interfaces. (#2844683 et al) This prevents undesired
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   132
     * PPP dialup, but may return addresses that don't actually correspond to
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   133
     * the name (if the name actually matches something in DNS etc.
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   134
     */
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   135
    myhostname[0] = '\0';
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
   136
    if (gethostname(myhostname, NI_MAXHOST) == -1) {
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   137
        /* Something went wrong, maybe networking is not setup? */
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   138
        return NULL;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   139
    }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   140
    myhostname[NI_MAXHOST] = '\0';
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   141
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   142
    if (strcmp(myhostname, hostname) != 0) {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   143
        // Non-self lookup
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   144
        return NULL;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   145
    }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   146
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   147
    if (getifaddrs(&ifa) != 0) {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   148
        NET_ThrowNew(env, errno, "Can't get local interface addresses");
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   149
        return NULL;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   150
    }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   151
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   152
    name = (*env)->NewStringUTF(env, hostname);
38563
3ab5f00bd5a3 8157811: Additional minor fixes and cleanups in Networking native code
chegar
parents: 38552
diff changeset
   153
    if (name == NULL) {
3ab5f00bd5a3 8157811: Additional minor fixes and cleanups in Networking native code
chegar
parents: 38552
diff changeset
   154
        freeifaddrs(ifa);
3ab5f00bd5a3 8157811: Additional minor fixes and cleanups in Networking native code
chegar
parents: 38552
diff changeset
   155
        return NULL;
3ab5f00bd5a3 8157811: Additional minor fixes and cleanups in Networking native code
chegar
parents: 38552
diff changeset
   156
    }
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   157
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   158
    /* Iterate over the interfaces, and total up the number of IPv4 and IPv6
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   159
     * addresses we have. Also keep a count of loopback addresses. We need to
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   160
     * exclude them in the normal case, but return them if we don't get an IP
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   161
     * address.
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   162
     */
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   163
    struct ifaddrs *iter = ifa;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   164
    while (iter) {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   165
        int family = iter->ifa_addr->sa_family;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   166
        if (iter->ifa_name[0] != '\0'  &&  iter->ifa_addr)
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   167
        {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   168
            jboolean isLoopback = iter->ifa_flags & IFF_LOOPBACK;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   169
            if (family == AF_INET) {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   170
                addrs4++;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   171
                if (isLoopback) numV4Loopbacks++;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   172
            } else if (family == AF_INET6 && includeV6) {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   173
                addrs6++;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   174
                if (isLoopback) numV6Loopbacks++;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   175
            } else {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   176
                /* We don't care e.g. AF_LINK */
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   177
            }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   178
        }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   179
        iter = iter->ifa_next;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   180
    }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   181
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   182
    if (addrs4 == numV4Loopbacks && addrs6 == numV6Loopbacks) {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   183
        // We don't have a real IP address, just loopback. We need to include
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   184
        // loopback in our results.
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   185
        includeLoopback = JNI_TRUE;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   186
    }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   187
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   188
    /* Create and fill the Java array. */
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   189
    int arraySize = addrs4 + addrs6 -
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   190
        (includeLoopback ? 0 : (numV4Loopbacks + numV6Loopbacks));
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
   191
    result = (*env)->NewObjectArray(env, arraySize, ia_class, NULL);
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   192
    if (!result) goto done;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   193
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
   194
    if ((*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID)) {
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   195
        i = includeLoopback ? addrs6 : (addrs6 - numV6Loopbacks);
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   196
        j = 0;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   197
    } else {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   198
        i = 0;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   199
        j = includeLoopback ? addrs4 : (addrs4 - numV4Loopbacks);
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   200
    }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   201
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   202
    // Now loop around the ifaddrs
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   203
    iter = ifa;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   204
    while (iter != NULL) {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   205
        jboolean isLoopback = iter->ifa_flags & IFF_LOOPBACK;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   206
        int family = iter->ifa_addr->sa_family;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   207
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   208
        if (iter->ifa_name[0] != '\0'  &&  iter->ifa_addr
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   209
            && (family == AF_INET || (family == AF_INET6 && includeV6))
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   210
            && (!isLoopback || includeLoopback))
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   211
        {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   212
            int port;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   213
            int index = (family == AF_INET) ? i++ : j++;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   214
            jobject o = NET_SockaddrToInetAddress(env, iter->ifa_addr, &port);
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   215
            if (!o) {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   216
                freeifaddrs(ifa);
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   217
                if (!(*env)->ExceptionCheck(env))
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   218
                    JNU_ThrowOutOfMemoryError(env, "Object allocation failed");
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   219
                return NULL;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   220
            }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   221
            setInetAddress_hostName(env, o, name);
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   222
            (*env)->SetObjectArrayElement(env, result, index, o);
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   223
            (*env)->DeleteLocalRef(env, o);
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   224
        }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   225
        iter = iter->ifa_next;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   226
    }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   227
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   228
  done:
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   229
    freeifaddrs(ifa);
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   230
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   231
    return result;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   232
}
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   233
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
/*
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   236
 * Find an internet address for a given hostname.  Note that this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 * code only works for addresses of type INET. The translation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 * of %d.%d.%d.%d to an address (int) occurs in java now, so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 * String "host" shouldn't *ever* be a %d.%d.%d.%d string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * Method:    lookupAllHostAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * Signature: (Ljava/lang/String;)[[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
JNIEXPORT jobjectArray JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                                jstring host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    const char *hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    jobjectArray ret = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    int retLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
31059
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   253
    int getaddrinfo_error=0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    struct addrinfo hints, *res, *resNew = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
   258
    initInetAddressIDs(env);
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
   259
    JNU_CHECK_EXCEPTION_RETURN(env, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    if (IS_NULL(host)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        JNU_ThrowNullPointerException(env, "host is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    CHECK_NULL_RETURN(hostname, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
#ifdef AF_INET6
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   269
    /* Try once, with our static buffer. */
11033
ef047e386299 7114558: Inet4AddressImpl should use memset (rather than bzero) and NI_MAXHOST (rather than MAXHOSTNAMELEN)
ngmr
parents: 11032
diff changeset
   270
    memset(&hints, 0, sizeof(hints));
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   271
    hints.ai_flags = AI_CANONNAME;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   272
    hints.ai_family = AF_UNSPEC;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   273
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   274
#ifdef __solaris__
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   275
    /*
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   276
     * Workaround for Solaris bug 4160367 - if a hostname contains a
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   277
     * white space then 0.0.0.0 is returned
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   278
     */
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   279
    if (isspace((unsigned char)hostname[0])) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   280
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   281
                        hostname);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   282
        JNU_ReleaseStringPlatformChars(env, host, hostname);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   283
        return NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   284
    }
2056
115e09b7a004 6799040: Portability issues in src/solaris/native/java/net/Inet4AddressImpl.c
chegar
parents: 910
diff changeset
   285
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
31059
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   287
    getaddrinfo_error = getaddrinfo(hostname, NULL, &hints, &res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
31059
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   289
#ifdef MACOSX
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   290
    if (getaddrinfo_error) {
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   291
        /*
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   292
         * If getaddrinfo fails looking up the local machine, attempt to get the
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   293
         * address from getifaddrs. This ensures we get an IPv6 address for the
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   294
         * local machine.
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   295
         */
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   296
        ret = lookupIfLocalhost(env, hostname, JNI_TRUE);
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   297
        if (ret != NULL || (*env)->ExceptionCheck(env)) {
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   298
            JNU_ReleaseStringPlatformChars(env, host, hostname);
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   299
            return ret;
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   300
        }
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   301
    }
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   302
#endif
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   303
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   304
    if (getaddrinfo_error) {
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   305
        /* report error */
31059
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   306
        NET_ThrowUnknownHostExceptionWithGaiError(
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   307
            env, hostname, getaddrinfo_error);
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   308
        JNU_ReleaseStringPlatformChars(env, host, hostname);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   309
        return NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   310
    } else {
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   311
        int i = 0, addressPreference = -1;
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   312
        int inetCount = 0, inet6Count = 0, inetIndex = 0, inet6Index = 0, originalIndex = 0;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   313
        struct addrinfo *itr, *last = NULL, *iterator = res;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   314
        while (iterator != NULL) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   315
            int skip = 0;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   316
            itr = resNew;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   317
            while (itr != NULL) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   318
                if (iterator->ai_family == itr->ai_family &&
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   319
                    iterator->ai_addrlen == itr->ai_addrlen) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   320
                    if (itr->ai_family == AF_INET) { /* AF_INET */
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   321
                        struct sockaddr_in *addr1, *addr2;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   322
                        addr1 = (struct sockaddr_in *)iterator->ai_addr;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   323
                        addr2 = (struct sockaddr_in *)itr->ai_addr;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   324
                        if (addr1->sin_addr.s_addr ==
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   325
                            addr2->sin_addr.s_addr) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   326
                            skip = 1;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   327
                            break;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   328
                        }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   329
                    } else {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   330
                        int t;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   331
                        struct sockaddr_in6 *addr1, *addr2;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   332
                        addr1 = (struct sockaddr_in6 *)iterator->ai_addr;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   333
                        addr2 = (struct sockaddr_in6 *)itr->ai_addr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   335
                        for (t = 0; t < 16; t++) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   336
                            if (addr1->sin6_addr.s6_addr[t] !=
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   337
                                addr2->sin6_addr.s6_addr[t]) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        }
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   341
                        if (t < 16) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   342
                            itr = itr->ai_next;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   343
                            continue;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   344
                        } else {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   345
                            skip = 1;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   346
                            break;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   347
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    }
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   349
                } else if (iterator->ai_family != AF_INET &&
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   350
                           iterator->ai_family != AF_INET6) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   351
                    /* we can't handle other family types */
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   352
                    skip = 1;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   353
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                }
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   355
                itr = itr->ai_next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   358
            if (!skip) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   359
                struct addrinfo *next
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   360
                    = (struct addrinfo*) malloc(sizeof(struct addrinfo));
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   361
                if (!next) {
13245
7ab3ef5b9520 7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents: 12688
diff changeset
   362
                    JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed");
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   363
                    ret = NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   364
                    goto cleanupAndReturn;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   365
                }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   366
                memcpy(next, iterator, sizeof(struct addrinfo));
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   367
                next->ai_next = NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   368
                if (resNew == NULL) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   369
                    resNew = next;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   370
                } else {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   371
                    last->ai_next = next;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   372
                }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   373
                last = next;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   374
                i++;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   375
                if (iterator->ai_family == AF_INET) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   376
                    inetCount ++;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   377
                } else if (iterator->ai_family == AF_INET6) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   378
                    inet6Count ++;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   379
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            }
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   381
            iterator = iterator->ai_next;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   382
        }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   383
        retLen = i;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   384
        iterator = resNew;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
   386
        ret = (*env)->NewObjectArray(env, retLen, ia_class, NULL);
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   387
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   388
        if (IS_NULL(ret)) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   389
            /* we may have memory to free at the end of this */
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   390
            goto cleanupAndReturn;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   391
        }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   392
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   393
        addressPreference = (*env)->GetStaticIntField(env, ia_class, ia_preferIPv6AddressID);
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   394
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   395
        if (addressPreference == java_net_InetAddress_PREFER_IPV6_VALUE) {
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   396
            /* AF_INET addresses will be offset by inet6Count */
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   397
            inetIndex = inet6Count;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   398
            inet6Index = 0;
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   399
        } else if (addressPreference == java_net_InetAddress_PREFER_IPV4_VALUE) {
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   400
            /* AF_INET6 addresses will be offset by inetCount */
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   401
            inetIndex = 0;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   402
            inet6Index = inetCount;
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   403
        } else if (addressPreference == java_net_InetAddress_PREFER_SYSTEM_VALUE) {
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   404
            inetIndex = inet6Index = originalIndex = 0;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   405
        }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   406
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   407
        while (iterator != NULL) {
22956
322ce28a7e20 8034182: Misc. warnings in java.net code
alanb
parents: 22646
diff changeset
   408
            jboolean ret1;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   409
            if (iterator->ai_family == AF_INET) {
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
   410
                jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                if (IS_NULL(iaObj)) {
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   412
                    ret = NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   413
                    goto cleanupAndReturn;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                }
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 13999
diff changeset
   415
                setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr));
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 13999
diff changeset
   416
                setInetAddress_hostName(env, iaObj, host);
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   417
                (*env)->SetObjectArrayElement(env, ret, (inetIndex | originalIndex), iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                inetIndex++;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   419
            } else if (iterator->ai_family == AF_INET6) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                jint scope = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
   422
                jobject iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                if (IS_NULL(iaObj)) {
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   424
                    ret = NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   425
                    goto cleanupAndReturn;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                }
20821
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 16870
diff changeset
   427
                ret1 = setInet6Address_ipaddress(env, iaObj, (char *)&(((struct sockaddr_in6*)iterator->ai_addr)->sin6_addr));
22956
322ce28a7e20 8034182: Misc. warnings in java.net code
alanb
parents: 22646
diff changeset
   428
                if (ret1 == JNI_FALSE) {
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   429
                    ret = NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   430
                    goto cleanupAndReturn;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                }
20821
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 16870
diff changeset
   432
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                scope = ((struct sockaddr_in6*)iterator->ai_addr)->sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                if (scope != 0) { /* zero is default value, no need to set */
20821
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 16870
diff changeset
   435
                    setInet6Address_scopeid(env, iaObj, scope);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                }
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 13999
diff changeset
   437
                setInetAddress_hostName(env, iaObj, host);
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   438
                (*env)->SetObjectArrayElement(env, ret, (inet6Index | originalIndex), iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                inet6Index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            }
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   441
            if (addressPreference == java_net_InetAddress_PREFER_SYSTEM_VALUE) {
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   442
                originalIndex++;
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   443
                inetIndex = inet6Index = 0;
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   444
            }
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   445
            iterator = iterator->ai_next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   449
 cleanupAndReturn:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    {
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   451
      struct addrinfo *iterator, *tmp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        iterator = resNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        while (iterator != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            tmp = iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            iterator = iterator->ai_next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            free(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        JNU_ReleaseStringPlatformChars(env, host, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   461
    freeaddrinfo(res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
 * Method:    getHostByAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
 * Signature: (I)Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
Java_java_net_Inet6AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                                            jbyteArray addrArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    jstring ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    char host[NI_MAXHOST+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    int error = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   484
    struct sockaddr_in him4;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   485
    struct sockaddr_in6 him6;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   486
    struct sockaddr *sa;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   488
    /*
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   489
     * For IPv4 addresses construct a sockaddr_in structure.
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   490
     */
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   491
    if ((*env)->GetArrayLength(env, addrArray) == 4) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   492
        jint addr;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   493
        (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   494
        addr = ((caddr[0]<<24) & 0xff000000);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   495
        addr |= ((caddr[1] <<16) & 0xff0000);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   496
        addr |= ((caddr[2] <<8) & 0xff00);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   497
        addr |= (caddr[3] & 0xff);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   498
        memset((void *) &him4, 0, sizeof(him4));
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
   499
        him4.sin_addr.s_addr = htonl(addr);
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   500
        him4.sin_family = AF_INET;
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
   501
        sa = (struct sockaddr *)&him4;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   502
        len = sizeof(him4);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   503
    } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        /*
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   505
         * For IPv6 address construct a sockaddr_in6 structure.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
         */
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   507
        (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
   508
        memset((void *)&him6, 0, sizeof(him6));
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
   509
        memcpy((void *)&(him6.sin6_addr), caddr, sizeof(struct in6_addr));
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   510
        him6.sin6_family = AF_INET6;
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
   511
        sa = (struct sockaddr *)&him6;
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
   512
        len = sizeof(him6);
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   513
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
   515
    error = getnameinfo(sa, len, host, NI_MAXHOST, NULL, 0, NI_NAMEREQD);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   517
    if (!error) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   518
        ret = (*env)->NewStringUTF(env, host);
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
   519
        CHECK_NULL_RETURN(ret, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    if (ret == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
#define SET_NONBLOCKING(fd) {           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        int flags = fcntl(fd, F_GETFL); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        flags |= O_NONBLOCK;            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        fcntl(fd, F_SETFL, flags);      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
static jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
ping6(JNIEnv *env, jint fd, struct sockaddr_in6* him, jint timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
      struct sockaddr_in6* netif, jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    jint size;
2056
115e09b7a004 6799040: Portability issues in src/solaris/native/java/net/Inet4AddressImpl.c
chegar
parents: 910
diff changeset
   541
    jint n;
115e09b7a004 6799040: Portability issues in src/solaris/native/java/net/Inet4AddressImpl.c
chegar
parents: 910
diff changeset
   542
    socklen_t len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    char sendbuf[1500];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    unsigned char recvbuf[1500];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    struct icmp6_hdr *icmp6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    struct sockaddr_in6 sa_recv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    jbyte *caddr, *recv_caddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    jchar pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    jint tmout2, seq = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    struct timeval tv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    size_t plen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    int csum_offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * For some strange reason, the linux kernel won't calculate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * checksum of ICMPv6 packets unless you set this socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    csum_offset = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    setsockopt(fd, SOL_RAW, IPV6_CHECKSUM, &csum_offset, sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    caddr = (jbyte *)&(him->sin6_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /* icmp_id is a 16 bit data type, therefore down cast the pid */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    pid = (jchar)getpid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    size = 60*1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    if (ttl > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
      setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    if (netif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
      if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in6)) <0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        NET_ThrowNew(env, errno, "Can't bind socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    SET_NONBLOCKING(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
      icmp6 = (struct icmp6_hdr *) sendbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
      icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
      icmp6->icmp6_code = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
      /* let's tag the ECHO packet with our pid so we can identify it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
      icmp6->icmp6_id = htons(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
      icmp6->icmp6_seq = htons(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
      seq++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
      icmp6->icmp6_cksum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
      gettimeofday(&tv, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
      memcpy(sendbuf + sizeof(struct icmp6_hdr), &tv, sizeof(tv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
      plen = sizeof(struct icmp6_hdr) + sizeof(tv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
      n = sendto(fd, sendbuf, plen, 0, (struct sockaddr*) him, sizeof(struct sockaddr_in6));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
      if (n < 0 && errno != EINPROGRESS) {
7024
25bd21af28bf 6947677: InetAddress.isReachable() throws "java.net.SocketException:Invalid argument" on Linux if run as root
chegar
parents: 6670
diff changeset
   597
#ifdef __linux__
11278
ce6c46d16022 7118907: InetAddress.isReachable() should return false if sendto fails with EHOSTUNREACH
ngmr
parents: 11033
diff changeset
   598
        if (errno != EINVAL && errno != EHOSTUNREACH)
7024
25bd21af28bf 6947677: InetAddress.isReachable() throws "java.net.SocketException:Invalid argument" on Linux if run as root
chegar
parents: 6670
diff changeset
   599
          /*
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13245
diff changeset
   600
           * On some Linux versions, when a socket is  bound to the
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13245
diff changeset
   601
           * loopback interface, sendto will fail and errno will be
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13245
diff changeset
   602
           * set to EINVAL or EHOSTUNREACH.
11278
ce6c46d16022 7118907: InetAddress.isReachable() should return false if sendto fails with EHOSTUNREACH
ngmr
parents: 11033
diff changeset
   603
           * When that happens, don't throw an exception, just return false.
7024
25bd21af28bf 6947677: InetAddress.isReachable() throws "java.net.SocketException:Invalid argument" on Linux if run as root
chegar
parents: 6670
diff changeset
   604
           */
25bd21af28bf 6947677: InetAddress.isReachable() throws "java.net.SocketException:Invalid argument" on Linux if run as root
chegar
parents: 6670
diff changeset
   605
#endif /*__linux__ */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        NET_ThrowNew(env, errno, "Can't send ICMP packet");
7024
25bd21af28bf 6947677: InetAddress.isReachable() throws "java.net.SocketException:Invalid argument" on Linux if run as root
chegar
parents: 6670
diff changeset
   607
        close(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
      tmout2 = timeout > 1000 ? 1000 : timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
      do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        if (tmout2 >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
          len = sizeof(sa_recv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
          n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr*) &sa_recv, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
          icmp6 = (struct icmp6_hdr *) (recvbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
          recv_caddr = (jbyte *)&(sa_recv.sin6_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
          /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
           * We did receive something, but is it what we were expecting?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
           * I.E.: An ICMP6_ECHO_REPLY packet with the proper PID and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
           *       from the host that we are trying to determine is reachable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
          if (n >= 8 && icmp6->icmp6_type == ICMP6_ECHO_REPLY &&
12688
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12673
diff changeset
   626
              (ntohs(icmp6->icmp6_id) == pid)) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12673
diff changeset
   627
            if (NET_IsEqual(caddr, recv_caddr)) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12673
diff changeset
   628
              close(fd);
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12673
diff changeset
   629
              return JNI_TRUE;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12673
diff changeset
   630
            }
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12673
diff changeset
   631
            if (NET_IsZeroAddr(caddr)) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12673
diff changeset
   632
              close(fd);
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12673
diff changeset
   633
              return JNI_TRUE;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12673
diff changeset
   634
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
      } while (tmout2 > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
      timeout -= 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    } while (timeout > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
 * Method:    isReachable0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
 * Signature: ([bII[bI)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
Java_java_net_Inet6AddressImpl_isReachable0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                                           jbyteArray addrArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                                           jint scope,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                                           jint timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                                           jbyteArray ifArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                                           jint ttl, jint if_scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    jbyte caddr[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    jint fd, sz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    struct sockaddr_in6 him6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    struct sockaddr_in6 inf6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    struct sockaddr_in6* netif = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    int connect_rv = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * If IPv6 is not enable, then we can't reach an IPv6 address, can we?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    if (!ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
      return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * If it's an IPv4 address, ICMP won't work with IPv4 mapped address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * therefore, let's delegate to the Inet4Address method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    sz = (*env)->GetArrayLength(env, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    if (sz == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
      return Java_java_net_Inet4AddressImpl_isReachable0(env, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                                                         addrArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                                                         timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                                                         ifArray, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   684
    memset((void *) caddr, 0, 16);
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   685
    memset((void *) &him6, 0, sizeof(him6));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    memcpy((void *)&(him6.sin6_addr), caddr, sizeof(struct in6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    him6.sin6_family = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    if (scope > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
      him6.sin6_scope_id = scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
      him6.sin6_scope_id = getDefaultIPv6Interface( &(him6.sin6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    len = sizeof(struct sockaddr_in6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    if (scope > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
      him6.sin6_scope_id = scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    len = sizeof(struct sockaddr_in6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * If a network interface was specified, let's create the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    if (!(IS_NULL(ifArray))) {
6670
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   705
      memset((void *) caddr, 0, 16);
ae13809f3ce7 6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents: 5506
diff changeset
   706
      memset((void *) &inf6, 0, sizeof(inf6));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
      (*env)->GetByteArrayRegion(env, ifArray, 0, 16, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
      memcpy((void *)&(inf6.sin6_addr), caddr, sizeof(struct in6_addr) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
      inf6.sin6_family = AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
      inf6.sin6_scope_id = if_scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
      netif = &inf6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * If we can create a RAW socket, then when can use the ICMP ECHO_REQUEST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * otherwise we'll try a tcp socket to the Echo port (7).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * Note that this is empiric, and not connecting could mean it's blocked
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13245
diff changeset
   717
     * or the echo service has been disabled.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
   720
    fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    if (fd != -1) { /* Good to go, let's do a ping */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        return ping6(env, fd, &him6, timeout, netif, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    /* No good, let's fall back on TCP */
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
   727
    fd = socket(AF_INET6, SOCK_STREAM, 0);
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
   728
    if (fd == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        /* note: if you run out of fds, you may not be able to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
         * the exception class, and get a NoClassDefFoundError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
         * instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        NET_ThrowNew(env, errno, "Can't create socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    if (ttl > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
      setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * A network interface was specified, so let's bind to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    if (netif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
      if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in6)) <0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        NET_ThrowNew(env, errno, "Can't bind socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    SET_NONBLOCKING(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    him6.sin6_port = htons((short) 7); /* Echo port */
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
   753
    connect_rv = NET_Connect(fd, (struct sockaddr *)&him6, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * connection established or refused immediately, either way it means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * we were able to reach the host!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    if (connect_rv == 0 || errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    } else {
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
   763
        socklen_t optlen = (socklen_t)sizeof(connect_rv);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        switch (errno) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        case ENETUNREACH: /* Network Unreachable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        case EAFNOSUPPORT: /* Address Family not supported */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        case EADDRNOTAVAIL: /* address is not available on  the  remote machine */
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 38563
diff changeset
   769
#if defined(__linux__) || defined(_AIX)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        case EINVAL:
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 38563
diff changeset
   771
        case EHOSTUNREACH: /* No route to host */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
          /*
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13245
diff changeset
   773
           * On some Linux versions, when  a socket is bound to the
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13245
diff changeset
   774
           * loopback interface, connect will fail and errno will
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13245
diff changeset
   775
           * be set to EINVAL or EHOSTUNREACH.  When that happens,
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13245
diff changeset
   776
           * don't throw an exception, just return false.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
#endif /* __linux__ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
          close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
          return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        if (errno != EINPROGRESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                                         "connect failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        if (timeout >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
          /* has connection been established */
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
   794
          if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
   795
                         &optlen) <0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            connect_rv = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
          }
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 38563
diff changeset
   798
          if (connect_rv == 0 || connect_rv == ECONNREFUSED) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
#else /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
}