src/java.base/unix/native/libnet/Inet6AddressImpl.c
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 43614 jdk/src/java.base/unix/native/libnet/Inet6AddressImpl.c@d8e1fecbb8f2
child 49985 44e581f54d08
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    41
#include "java_net_InetAddress.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include "java_net_Inet4AddressImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include "java_net_Inet6AddressImpl.h"
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
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    50
#define SET_NONBLOCKING(fd) {       \
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    51
    int flags = fcntl(fd, F_GETFL); \
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    52
    flags |= O_NONBLOCK;            \
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    53
    fcntl(fd, F_SETFL, flags);      \
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    54
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    56
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Method:    getLocalHostName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * Signature: ()Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
Java_java_net_Inet6AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    67
    char hostname[NI_MAXHOST + 1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    hostname[0] = '\0';
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    70
    if (gethostname(hostname, NI_MAXHOST) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        strcpy(hostname, "localhost");
41834
99c4f72c0a91 8168771: Remove #ifdef AF_INET6 guards in libnet native coding
clanger
parents: 41771
diff changeset
    72
#if defined(__solaris__)
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    73
    } else {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    74
        // try to resolve hostname via nameservice
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    75
        // if it is known but getnameinfo fails, hostname will still be the
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    76
        // value from gethostname
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    77
        struct addrinfo hints, *res;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    79
        // make sure string is null-terminated
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    80
        hostname[NI_MAXHOST] = '\0';
11033
ef047e386299 7114558: Inet4AddressImpl should use memset (rather than bzero) and NI_MAXHOST (rather than MAXHOSTNAMELEN)
ngmr
parents: 11032
diff changeset
    81
        memset(&hints, 0, sizeof(hints));
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
    82
        hints.ai_flags = AI_CANONNAME;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
    83
        hints.ai_family = AF_UNSPEC;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    85
        if (getaddrinfo(hostname, NULL, &hints, &res) == 0) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    86
            getnameinfo(res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    87
                        NULL, 0, NI_NAMEREQD);
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
    88
            freeaddrinfo(res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    91
#else
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    92
    } else {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    93
        // make sure string is null-terminated
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    94
        hostname[NI_MAXHOST] = '\0';
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
    95
    }
22956
322ce28a7e20 8034182: Misc. warnings in java.net code
alanb
parents: 22646
diff changeset
    96
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    return (*env)->NewStringUTF(env, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   100
#if defined(MACOSX)
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   101
/* also called from Inet4AddressImpl.c */
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   102
__private_extern__ jobjectArray
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   103
lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   104
{
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   105
    jobjectArray result = NULL;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   106
    char myhostname[NI_MAXHOST+1];
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   107
    struct ifaddrs *ifa = NULL;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   108
    int familyOrder = 0;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   109
    int count = 0, i, j;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   110
    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
   111
    jboolean includeLoopback = JNI_FALSE;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   112
    jobject name;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   113
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
   114
    initInetAddressIDs(env);
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
   115
    JNU_CHECK_EXCEPTION_RETURN(env, NULL);
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   116
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   117
    /* 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
   118
     * 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
   119
     * 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
   120
     * 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
   121
     */
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   122
    myhostname[0] = '\0';
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22956
diff changeset
   123
    if (gethostname(myhostname, NI_MAXHOST) == -1) {
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   124
        /* Something went wrong, maybe networking is not setup? */
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   125
        return NULL;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   126
    }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   127
    myhostname[NI_MAXHOST] = '\0';
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   128
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   129
    if (strcmp(myhostname, hostname) != 0) {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   130
        // Non-self lookup
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   131
        return NULL;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   132
    }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   133
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   134
    if (getifaddrs(&ifa) != 0) {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   135
        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
   136
        return NULL;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   137
    }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   138
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   139
    name = (*env)->NewStringUTF(env, hostname);
38563
3ab5f00bd5a3 8157811: Additional minor fixes and cleanups in Networking native code
chegar
parents: 38552
diff changeset
   140
    if (name == NULL) {
3ab5f00bd5a3 8157811: Additional minor fixes and cleanups in Networking native code
chegar
parents: 38552
diff changeset
   141
        freeifaddrs(ifa);
3ab5f00bd5a3 8157811: Additional minor fixes and cleanups in Networking native code
chegar
parents: 38552
diff changeset
   142
        return NULL;
3ab5f00bd5a3 8157811: Additional minor fixes and cleanups in Networking native code
chegar
parents: 38552
diff changeset
   143
    }
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   144
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   145
    /* 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
   146
     * 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
   147
     * 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
   148
     * address.
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   149
     */
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   150
    struct ifaddrs *iter = ifa;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   151
    while (iter) {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   152
        int family = iter->ifa_addr->sa_family;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   153
        if (iter->ifa_name[0] != '\0' && iter->ifa_addr)
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   154
        {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   155
            jboolean isLoopback = iter->ifa_flags & IFF_LOOPBACK;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   156
            if (family == AF_INET) {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   157
                addrs4++;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   158
                if (isLoopback) numV4Loopbacks++;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   159
            } else if (family == AF_INET6 && includeV6) {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   160
                addrs6++;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   161
                if (isLoopback) numV6Loopbacks++;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   162
            } // else we don't care, e.g. AF_LINK
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   163
        }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   164
        iter = iter->ifa_next;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   165
    }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   166
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   167
    if (addrs4 == numV4Loopbacks && addrs6 == numV6Loopbacks) {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   168
        // 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
   169
        // loopback in our results.
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   170
        includeLoopback = JNI_TRUE;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   171
    }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   172
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   173
    /* Create and fill the Java array. */
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   174
    int arraySize = addrs4 + addrs6 -
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   175
        (includeLoopback ? 0 : (numV4Loopbacks + numV6Loopbacks));
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
   176
    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
   177
    if (!result) goto done;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   178
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
   179
    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
   180
        i = includeLoopback ? addrs6 : (addrs6 - numV6Loopbacks);
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   181
        j = 0;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   182
    } else {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   183
        i = 0;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   184
        j = includeLoopback ? addrs4 : (addrs4 - numV4Loopbacks);
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   185
    }
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
    // Now loop around the ifaddrs
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   188
    iter = ifa;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   189
    while (iter != NULL) {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   190
        jboolean isLoopback = iter->ifa_flags & IFF_LOOPBACK;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   191
        int family = iter->ifa_addr->sa_family;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   192
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   193
        if (iter->ifa_name[0] != '\0' && iter->ifa_addr &&
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   194
            (family == AF_INET || (family == AF_INET6 && includeV6)) &&
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   195
            (!isLoopback || includeLoopback))
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   196
        {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   197
            int port;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   198
            int index = (family == AF_INET) ? i++ : j++;
43100
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   199
            jobject o = NET_SockaddrToInetAddress(env,
a7e3457672c7 8170544: Fix code scan findings in libnet
clanger
parents: 41834
diff changeset
   200
                            (SOCKETADDRESS *)iter->ifa_addr, &port);
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   201
            if (!o) {
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   202
                freeifaddrs(ifa);
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   203
                if (!(*env)->ExceptionCheck(env))
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   204
                    JNU_ThrowOutOfMemoryError(env, "Object allocation failed");
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   205
                return NULL;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   206
            }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   207
            setInetAddress_hostName(env, o, name);
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   208
            (*env)->SetObjectArrayElement(env, result, index, o);
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   209
            (*env)->DeleteLocalRef(env, o);
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   210
        }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   211
        iter = iter->ifa_next;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   212
    }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   213
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   214
  done:
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   215
    freeifaddrs(ifa);
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   216
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   217
    return result;
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   218
}
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   219
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * Method:    lookupAllHostAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 * Signature: (Ljava/lang/String;)[[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
JNIEXPORT jobjectArray JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   228
                                                 jstring host) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   229
    jobjectArray ret = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    const char *hostname;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   231
    int error = 0;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   232
    struct addrinfo hints, *res = NULL, *resNew = NULL, *last = NULL,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   233
        *iterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
   235
    initInetAddressIDs(env);
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
   236
    JNU_CHECK_EXCEPTION_RETURN(env, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    if (IS_NULL(host)) {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   239
        JNU_ThrowNullPointerException(env, "host argument is null");
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   240
        return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    CHECK_NULL_RETURN(hostname, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   245
    // 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
   246
    memset(&hints, 0, sizeof(hints));
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   247
    hints.ai_flags = AI_CANONNAME;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   248
    hints.ai_family = AF_UNSPEC;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   249
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   250
    error = getaddrinfo(hostname, NULL, &hints, &res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   252
    if (error) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   253
#if defined(MACOSX)
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   254
        // if getaddrinfo fails try getifaddrs
31059
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   255
        ret = lookupIfLocalhost(env, hostname, JNI_TRUE);
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   256
        if (ret != NULL || (*env)->ExceptionCheck(env)) {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   257
            goto cleanupAndReturn;
31059
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   258
        }
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   259
#endif
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   260
        // report error
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   261
        NET_ThrowUnknownHostExceptionWithGaiError(env, hostname, error);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   262
        goto cleanupAndReturn;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   263
    } else {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   264
        int i = 0, inetCount = 0, inet6Count = 0, inetIndex = 0,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   265
            inet6Index = 0, originalIndex = 0;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   266
        int addressPreference =
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   267
            (*env)->GetStaticIntField(env, ia_class, ia_preferIPv6AddressID);;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   268
        iterator = res;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   269
        while (iterator != NULL) {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   270
            // skip duplicates
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   271
            int skip = 0;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   272
            struct addrinfo *iteratorNew = resNew;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   273
            while (iteratorNew != NULL) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   274
                if (iterator->ai_family == iteratorNew->ai_family &&
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   275
                    iterator->ai_addrlen == iteratorNew->ai_addrlen) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   276
                    if (iteratorNew->ai_family == AF_INET) { /* AF_INET */
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   277
                        struct sockaddr_in *addr1, *addr2;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   278
                        addr1 = (struct sockaddr_in *)iterator->ai_addr;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   279
                        addr2 = (struct sockaddr_in *)iteratorNew->ai_addr;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   280
                        if (addr1->sin_addr.s_addr == addr2->sin_addr.s_addr) {
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   281
                            skip = 1;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   282
                            break;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   283
                        }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   284
                    } else {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   285
                        int t;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   286
                        struct sockaddr_in6 *addr1, *addr2;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   287
                        addr1 = (struct sockaddr_in6 *)iterator->ai_addr;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   288
                        addr2 = (struct sockaddr_in6 *)iteratorNew->ai_addr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   290
                        for (t = 0; t < 16; t++) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   291
                            if (addr1->sin6_addr.s6_addr[t] !=
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   292
                                addr2->sin6_addr.s6_addr[t]) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                        }
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   296
                        if (t < 16) {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   297
                            iteratorNew = iteratorNew->ai_next;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   298
                            continue;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   299
                        } else {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   300
                            skip = 1;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   301
                            break;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   302
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    }
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   304
                } else if (iterator->ai_family != AF_INET &&
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   305
                           iterator->ai_family != AF_INET6) {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   306
                    // we can't handle other family types
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   307
                    skip = 1;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   308
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                }
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   310
                iteratorNew = iteratorNew->ai_next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   313
            if (!skip) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   314
                struct addrinfo *next
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   315
                    = (struct addrinfo *)malloc(sizeof(struct addrinfo));
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   316
                if (!next) {
13245
7ab3ef5b9520 7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents: 12688
diff changeset
   317
                    JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed");
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   318
                    ret = NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   319
                    goto cleanupAndReturn;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   320
                }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   321
                memcpy(next, iterator, sizeof(struct addrinfo));
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   322
                next->ai_next = NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   323
                if (resNew == NULL) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   324
                    resNew = next;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   325
                } else {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   326
                    last->ai_next = next;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   327
                }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   328
                last = next;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   329
                i++;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   330
                if (iterator->ai_family == AF_INET) {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   331
                    inetCount++;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   332
                } else if (iterator->ai_family == AF_INET6) {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   333
                    inet6Count++;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   334
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            }
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   336
            iterator = iterator->ai_next;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   337
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   339
        // allocate array - at this point i contains the number of addresses
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   340
        ret = (*env)->NewObjectArray(env, i, ia_class, NULL);
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   341
        if (IS_NULL(ret)) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   342
            /* we may have memory to free at the end of this */
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   343
            goto cleanupAndReturn;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   344
        }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   345
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   346
        if (addressPreference == java_net_InetAddress_PREFER_IPV6_VALUE) {
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   347
            inetIndex = inet6Count;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   348
            inet6Index = 0;
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   349
        } else if (addressPreference == java_net_InetAddress_PREFER_IPV4_VALUE) {
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   350
            inetIndex = 0;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   351
            inet6Index = inetCount;
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   352
        } 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
   353
            inetIndex = inet6Index = originalIndex = 0;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   354
        }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   355
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   356
        iterator = resNew;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   357
        while (iterator != NULL) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   358
            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
   359
                jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                if (IS_NULL(iaObj)) {
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   361
                    ret = NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   362
                    goto cleanupAndReturn;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                }
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 13999
diff changeset
   364
                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
   365
                setInetAddress_hostName(env, iaObj, host);
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   366
                (*env)->SetObjectArrayElement(env, ret, (inetIndex | originalIndex), iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                inetIndex++;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   368
            } else if (iterator->ai_family == AF_INET6) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                jint scope = 0;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   370
                jboolean ret1;
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
   371
                jobject iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                if (IS_NULL(iaObj)) {
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   373
                    ret = NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   374
                    goto cleanupAndReturn;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                }
20821
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 16870
diff changeset
   376
                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
   377
                if (ret1 == JNI_FALSE) {
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   378
                    ret = NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   379
                    goto cleanupAndReturn;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                }
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   381
                scope = ((struct sockaddr_in6 *)iterator->ai_addr)->sin6_scope_id;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   382
                if (scope != 0) { // zero is default value, no need to set
20821
e0d0a585aa49 8015743: Address internet addresses
michaelm
parents: 16870
diff changeset
   383
                    setInet6Address_scopeid(env, iaObj, scope);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                }
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 13999
diff changeset
   385
                setInetAddress_hostName(env, iaObj, host);
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   386
                (*env)->SetObjectArrayElement(env, ret, (inet6Index | originalIndex), iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                inet6Index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   389
            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
   390
                originalIndex++;
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   391
                inetIndex = inet6Index = 0;
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 31059
diff changeset
   392
            }
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   393
            iterator = iterator->ai_next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   396
cleanupAndReturn:
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   397
    JNU_ReleaseStringPlatformChars(env, host, hostname);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   398
    while (resNew != NULL) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   399
        last = resNew;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   400
        resNew = resNew->ai_next;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   401
        free(last);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   403
    if (res != NULL) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   404
        freeaddrinfo(res);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   405
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
 * Method:    getHostByAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
 * Signature: (I)Ljava/lang/String;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   413
 *
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   414
 * Theoretically the UnknownHostException could be enriched with gai error
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   415
 * information. But as it is silently ignored anyway, there's no need for this.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   416
 * It's only important that either a valid hostname is returned or an
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   417
 * UnknownHostException is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
Java_java_net_Inet6AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   421
                                             jbyteArray addrArray) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    jstring ret = NULL;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   423
    char host[NI_MAXHOST + 1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    jbyte caddr[16];
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   426
    SOCKETADDRESS sa;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   428
    memset((void *)&sa, 0, sizeof(SOCKETADDRESS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   430
    // construct a sockaddr_in structure (AF_INET or AF_INET6)
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   431
    if ((*env)->GetArrayLength(env, addrArray) == 4) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   432
        jint addr;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   433
        (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   434
        addr = ((caddr[0] << 24) & 0xff000000);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   435
        addr |= ((caddr[1] << 16) & 0xff0000);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   436
        addr |= ((caddr[2] << 8) & 0xff00);
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   437
        addr |= (caddr[3] & 0xff);
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   438
        sa.sa4.sin_addr.s_addr = htonl(addr);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   439
        sa.sa4.sin_family = AF_INET;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   440
        len = sizeof(struct sockaddr_in);
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   441
    } else {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   442
        (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   443
        memcpy((void *)&sa.sa6.sin6_addr, caddr, sizeof(struct in6_addr));
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   444
        sa.sa6.sin6_family = AF_INET6;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   445
        len = sizeof(struct sockaddr_in6);
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   446
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   448
    if (getnameinfo(&sa.sa, len, host, NI_MAXHOST, NULL, 0, NI_NAMEREQD)) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   449
        JNU_ThrowByName(env, "java/net/UnknownHostException", NULL);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   450
    } else {
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 9035
diff changeset
   451
        ret = (*env)->NewStringUTF(env, host);
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   452
        if (ret == NULL) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   453
            JNU_ThrowByName(env, "java/net/UnknownHostException", NULL);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   454
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   460
/**
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   461
 * ping implementation using tcp port 7 (echo)
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   462
 */
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   463
static jboolean
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   464
tcp_ping6(JNIEnv *env, SOCKETADDRESS *sa, SOCKETADDRESS *netif, jint timeout,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   465
          jint ttl)
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   466
{
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   467
    jint fd;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   468
    int connect_rv = -1;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   469
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   470
    // open a TCP socket
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   471
    fd = socket(AF_INET6, SOCK_STREAM, 0);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   472
    if (fd == -1) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   473
        // note: if you run out of fds, you may not be able to load
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   474
        // the exception class, and get a NoClassDefFoundError instead.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   475
        NET_ThrowNew(env, errno, "Can't create socket");
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   476
        return JNI_FALSE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   477
    }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   478
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   479
    // set TTL
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   480
    if (ttl > 0) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   481
        setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   482
    }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   483
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   484
    // A network interface was specified, so let's bind to it.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   485
    if (netif != NULL) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   486
        if (bind(fd, &netif->sa, sizeof(struct sockaddr_in6)) <0) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   487
            NET_ThrowNew(env, errno, "Can't bind socket");
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   488
            close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   489
            return JNI_FALSE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   490
        }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   491
    }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   492
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   493
    // Make the socket non blocking so we can use select/poll.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   494
    SET_NONBLOCKING(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   495
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   496
    sa->sa6.sin6_port = htons(7); // echo port
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   497
    connect_rv = NET_Connect(fd, &sa->sa, sizeof(struct sockaddr_in6));
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   498
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   499
    // connection established or refused immediately, either way it means
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   500
    // we were able to reach the host!
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   501
    if (connect_rv == 0 || errno == ECONNREFUSED) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   502
        close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   503
        return JNI_TRUE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   504
    }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   505
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   506
    switch (errno) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   507
    case ENETUNREACH:   // Network Unreachable
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   508
    case EAFNOSUPPORT:  // Address Family not supported
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   509
    case EADDRNOTAVAIL: // address is not available on the remote machine
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   510
#if defined(__linux__) || defined(_AIX)
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   511
        // On some Linux versions, when a socket is bound to the loopback
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   512
        // interface, connect will fail and errno will be set to EINVAL
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   513
        // or EHOSTUNREACH.  When that happens, don't throw an exception,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   514
        // just return false.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   515
    case EINVAL:
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   516
    case EHOSTUNREACH:  // No route to host
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   517
#endif
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   518
        close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   519
        return JNI_FALSE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   520
    case EINPROGRESS:   // this is expected as we'll probably have to wait
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   521
        break;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   522
    default:
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   523
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   524
                                     "connect failed");
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   525
        close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   526
        return JNI_FALSE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   527
    }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   528
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   529
    timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   530
    if (timeout >= 0) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   531
        // connection has been established, check for error condition
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   532
        socklen_t optlen = (socklen_t)sizeof(connect_rv);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   533
        if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   534
                       &optlen) <0)
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   535
        {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   536
            connect_rv = errno;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   537
        }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   538
        if (connect_rv == 0 || connect_rv == ECONNREFUSED) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   539
            close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   540
            return JNI_TRUE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   541
        }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   542
    }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   543
    close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   544
    return JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   547
/**
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   548
 * ping implementation.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   549
 * Send an ICMP_ECHO_REQUEST packet every second until either the timeout
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   550
 * expires or an answer is received.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   551
 * Returns true if an ECHO_REPLY is received, false otherwise.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   552
 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
static jboolean
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   554
ping6(JNIEnv *env, jint fd, SOCKETADDRESS *sa, SOCKETADDRESS *netif,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   555
      jint timeout, jint ttl)
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   556
{
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   557
    jint n, size = 60 * 1024, tmout2, seq = 1;
2056
115e09b7a004 6799040: Portability issues in src/solaris/native/java/net/Inet4AddressImpl.c
chegar
parents: 910
diff changeset
   558
    socklen_t len;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   559
    unsigned char sendbuf[1500], recvbuf[1500];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    struct icmp6_hdr *icmp6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    struct sockaddr_in6 sa_recv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    jchar pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    struct timeval tv;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   564
    size_t plen = sizeof(struct icmp6_hdr) + sizeof(tv);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   566
#if defined(__linux__)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * For some strange reason, the linux kernel won't calculate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * checksum of ICMPv6 packets unless you set this socket option
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     */
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   571
    int csum_offset = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    setsockopt(fd, SOL_RAW, IPV6_CHECKSUM, &csum_offset, sizeof(int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   575
    setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   577
    // sets the ttl (max number of hops)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    if (ttl > 0) {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   579
        setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   581
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   582
    // a specific interface was specified, so let's bind the socket
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   583
    // to that interface to ensure the requests are sent only through it.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    if (netif != NULL) {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   585
        if (bind(fd, &netif->sa, sizeof(struct sockaddr_in6)) <0) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   586
            NET_ThrowNew(env, errno, "Can't bind socket");
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   587
            close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   588
            return JNI_FALSE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   589
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   591
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   592
    // icmp_id is a 16 bit data type, therefore down cast the pid
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   593
    pid = (jchar)getpid();
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   594
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   595
    // Make the socket non blocking so we can use select
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    SET_NONBLOCKING(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    do {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   598
        // create the ICMP request
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   599
        icmp6 = (struct icmp6_hdr *)sendbuf;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   600
        icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   601
        icmp6->icmp6_code = 0;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   602
        // let's tag the ECHO packet with our pid so we can identify it
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   603
        icmp6->icmp6_id = htons(pid);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   604
        icmp6->icmp6_seq = htons(seq);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   605
        seq++;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   606
        gettimeofday(&tv, NULL);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   607
        memcpy(sendbuf + sizeof(struct icmp6_hdr), &tv, sizeof(tv));
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   608
        icmp6->icmp6_cksum = 0;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   609
        // send it
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   610
        n = sendto(fd, sendbuf, plen, 0, &sa->sa, sizeof(struct sockaddr_in6));
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   611
        if (n < 0 && errno != EINPROGRESS) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   612
#if defined(__linux__)
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   613
            /*
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   614
             * On some Linux versions, when a socket is bound to the loopback
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   615
             * interface, sendto will fail and errno will be set to
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   616
             * EINVAL or EHOSTUNREACH. When that happens, don't throw an
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   617
             * exception, just return false.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   618
             */
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   619
            if (errno != EINVAL && errno != EHOSTUNREACH) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   620
                NET_ThrowNew(env, errno, "Can't send ICMP packet");
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   621
            }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   622
#else
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   623
            NET_ThrowNew(env, errno, "Can't send ICMP packet");
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   624
#endif
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   625
            close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   626
            return JNI_FALSE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   627
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   629
        tmout2 = timeout > 1000 ? 1000 : timeout;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   630
        do {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   631
            tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   632
            if (tmout2 >= 0) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   633
                len = sizeof(sa_recv);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   634
                n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   635
                             (struct sockaddr *)&sa_recv, &len);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   636
                // check if we received enough data
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   637
                if (n < (jint)sizeof(struct icmp6_hdr)) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   638
                    continue;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   639
                }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   640
                icmp6 = (struct icmp6_hdr *)recvbuf;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   641
                // We did receive something, but is it what we were expecting?
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   642
                // I.E.: An ICMP6_ECHO_REPLY packet with the proper PID and
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   643
                //       from the host that we are trying to determine is reachable.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   644
                if (icmp6->icmp6_type == ICMP6_ECHO_REPLY &&
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   645
                    (ntohs(icmp6->icmp6_id) == pid))
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   646
                {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   647
                    if (NET_IsEqual((jbyte *)&sa->sa6.sin6_addr,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   648
                                    (jbyte *)&sa_recv.sin6_addr)) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   649
                        close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   650
                        return JNI_TRUE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   651
                    } else if (NET_IsZeroAddr((jbyte *)&sa->sa6.sin6_addr)) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   652
                        close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   653
                        return JNI_TRUE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   654
                    }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   655
                }
12688
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12673
diff changeset
   656
            }
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   657
        } while (tmout2 > 0);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   658
        timeout -= 1000;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    } while (timeout > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
 * Method:    isReachable0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
 * Signature: ([bII[bI)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
Java_java_net_Inet6AddressImpl_isReachable0(JNIEnv *env, jobject this,
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   671
                                            jbyteArray addrArray, jint scope,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   672
                                            jint timeout, jbyteArray ifArray,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   673
                                            jint ttl, jint if_scope)
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   674
{
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    jbyte caddr[16];
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   676
    jint sz, fd;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   677
    SOCKETADDRESS sa, inf, *netif = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   679
    // If IPv6 is not enabled, then we can't reach an IPv6 address, can we?
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   680
    // Actually, we probably shouldn't even get here.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    if (!ipv6_available()) {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   682
        return JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   685
    // If it's an IPv4 address, ICMP won't work with IPv4 mapped address,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   686
    // therefore, let's delegate to the Inet4Address method.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   687
    sz = (*env)->GetArrayLength(env, addrArray);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   688
    if (sz == 4) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   689
        return Java_java_net_Inet4AddressImpl_isReachable0(env, this,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   690
                                                           addrArray, timeout,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   691
                                                           ifArray, ttl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   694
    // load address to SOCKETADDRESS
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   695
    memset((char *)caddr, 0, 16);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   696
    (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   697
    memset((char *)&sa, 0, sizeof(SOCKETADDRESS));
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   698
    memcpy((void *)&sa.sa6.sin6_addr, caddr, sizeof(struct in6_addr));
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   699
    sa.sa6.sin6_family = AF_INET6;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   700
    if (scope > 0) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   701
        sa.sa6.sin6_scope_id = scope;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   702
#if defined(__linux__)
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   703
    } else {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   704
        sa.sa6.sin6_scope_id = getDefaultIPv6Interface(&sa.sa6.sin6_addr);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   705
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   708
    // load network interface address to SOCKETADDRESS, if specified
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   709
    if (!(IS_NULL(ifArray))) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   710
        memset((char *)caddr, 0, 16);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   711
        (*env)->GetByteArrayRegion(env, ifArray, 0, 16, caddr);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   712
        memset((char *)&inf, 0, sizeof(SOCKETADDRESS));
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   713
        memcpy((void *)&inf.sa6.sin6_addr, caddr, sizeof(struct in6_addr));
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   714
        inf.sa6.sin6_family = AF_INET6;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   715
        inf.sa6.sin6_scope_id = if_scope;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   716
        netif = &inf;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   719
    // Let's try to create a RAW socket to send ICMP packets.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   720
    // This usually requires "root" privileges, so it's likely to fail.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   721
    fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   722
    if (fd == -1) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   723
        return tcp_ping6(env, &sa, netif, timeout, ttl);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   724
    } else {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   725
        // It didn't fail, so we can use ICMP_ECHO requests.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 43100
diff changeset
   726
        return ping6(env, fd, &sa, netif, timeout, ttl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
}