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