src/java.base/windows/native/libnet/Inet6AddressImpl.c
author igerasim
Sun, 18 Feb 2018 17:06:20 -0800
changeset 48911 edaa989b4e67
parent 47216 71c04702a3d5
child 51151 d6b131d2bc8b
permissions -rw-r--r--
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win] Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37362
diff changeset
     2
 * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 910
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: 910
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: 910
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 910
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 910
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 <malloc.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    27
#include "net_util.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "java_net_InetAddress.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "java_net_Inet4AddressImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "java_net_Inet6AddressImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Method:    getLocalHostName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Signature: ()Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
JNIEXPORT jstring JNICALL
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    43
Java_java_net_Inet6AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    44
    char hostname[256];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    46
    if (gethostname(hostname, sizeof(hostname)) == -1) {
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    47
        strcpy(hostname, "localhost");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    49
    return JNU_NewStringPlatform(env, hostname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    52
/*
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    53
 * Class:     java_net_Inet6AddressImpl
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    54
 * Method:    lookupAllHostAddr
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    55
 * Signature: (Ljava/lang/String;)[[B
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    56
 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
JNIEXPORT jobjectArray JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    59
                                                 jstring host) {
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    60
    jobjectArray ret = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    const char *hostname;
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    62
    int error = 0;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    63
    struct addrinfo hints, *res = NULL, *resNew = NULL, *last = NULL,
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    64
        *iterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
    66
    initInetAddressIDs(env);
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
    67
    JNU_CHECK_EXCEPTION_RETURN(env, NULL);
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 22640
diff changeset
    68
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    if (IS_NULL(host)) {
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    70
        JNU_ThrowNullPointerException(env, "host argument is null");
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    71
        return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    CHECK_NULL_RETURN(hostname, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    76
    // try once, with our static buffer
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
    77
    memset(&hints, 0, sizeof(hints));
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
    78
    hints.ai_flags = AI_CANONNAME;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
    79
    hints.ai_family = AF_UNSPEC;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
    80
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
    81
    error = getaddrinfo(hostname, NULL, &hints, &res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
21325
8ccf86fcccda 8026806: Incomplete test of getaddrinfo() return value could lead to incorrect exception for Windows Inet 6
bpb
parents: 20857
diff changeset
    83
    if (error) {
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    84
        // report error
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    85
        NET_ThrowByNameWithLastError(env, "java/net/UnknownHostException",
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    86
                                     hostname);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    87
        goto cleanupAndReturn;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
    88
    } else {
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    89
        int i = 0, inetCount = 0, inet6Count = 0, inetIndex = 0,
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    90
            inet6Index = 0, originalIndex = 0;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    91
        int addressPreference =
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    92
            (*env)->GetStaticIntField(env, ia_class, ia_preferIPv6AddressID);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    93
        iterator = res;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
    94
        while (iterator != NULL) {
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    95
            // skip duplicates
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
    96
            int skip = 0;
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    97
            struct addrinfo *iteratorNew = resNew;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    98
            while (iteratorNew != NULL) {
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
    99
                if (iterator->ai_family == iteratorNew->ai_family &&
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   100
                    iterator->ai_addrlen == iteratorNew->ai_addrlen) {
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   101
                    if (iteratorNew->ai_family == AF_INET) { /* AF_INET */
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   102
                        struct sockaddr_in *addr1, *addr2;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   103
                        addr1 = (struct sockaddr_in *)iterator->ai_addr;
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   104
                        addr2 = (struct sockaddr_in *)iteratorNew->ai_addr;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   105
                        if (addr1->sin_addr.s_addr == addr2->sin_addr.s_addr) {
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   106
                            skip = 1;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   107
                            break;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   108
                        }
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   109
                    } else {
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   110
                        int t;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   111
                        struct sockaddr_in6 *addr1, *addr2;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   112
                        addr1 = (struct sockaddr_in6 *)iterator->ai_addr;
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   113
                        addr2 = (struct sockaddr_in6 *)iteratorNew->ai_addr;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   114
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   115
                        for (t = 0; t < 16; t++) {
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   116
                            if (addr1->sin6_addr.s6_addr[t] !=
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   117
                                addr2->sin6_addr.s6_addr[t]) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        }
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   121
                        if (t < 16) {
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   122
                            iteratorNew = iteratorNew->ai_next;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   123
                            continue;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   124
                        } else {
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   125
                            skip = 1;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   126
                            break;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   127
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    }
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   129
                } else if (iterator->ai_family != AF_INET &&
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   130
                           iterator->ai_family != AF_INET6) {
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   131
                    // we can't handle other family types
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   132
                    skip = 1;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   133
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   135
                iteratorNew = iteratorNew->ai_next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   138
            if (!skip) {
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   139
                struct addrinfo *next
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   140
                    = (struct addrinfo *)malloc(sizeof(struct addrinfo));
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   141
                if (!next) {
13245
7ab3ef5b9520 7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents: 9050
diff changeset
   142
                    JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    goto cleanupAndReturn;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   145
                }
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   146
                memcpy(next, iterator, sizeof(struct addrinfo));
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   147
                next->ai_next = NULL;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   148
                if (resNew == NULL) {
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   149
                    resNew = next;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   150
                } else {
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   151
                    last->ai_next = next;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   152
                }
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   153
                last = next;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   154
                i++;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   155
                if (iterator->ai_family == AF_INET) {
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   156
                    inetCount++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                } else if (iterator->ai_family == AF_INET6) {
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   158
                    inet6Count++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   161
            iterator = iterator->ai_next;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   162
        }
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   163
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   164
        // allocate array - at this point i contains the number of addresses
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   165
        ret = (*env)->NewObjectArray(env, i, ia_class, NULL);
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   166
        if (IS_NULL(ret)) {
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   167
            /* we may have memory to free at the end of this */
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   168
            goto cleanupAndReturn;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   169
        }
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   170
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   171
        if (addressPreference == java_net_InetAddress_PREFER_IPV6_VALUE) {
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   172
            inetIndex = inet6Count;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   173
            inet6Index = 0;
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   174
        } else if (addressPreference == java_net_InetAddress_PREFER_IPV4_VALUE) {
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   175
            inetIndex = 0;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   176
            inet6Index = inetCount;
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   177
        } else if (addressPreference == java_net_InetAddress_PREFER_SYSTEM_VALUE) {
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37362
diff changeset
   178
            inetIndex = inet6Index = originalIndex = 0;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   179
        }
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   180
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   181
        iterator = resNew;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   182
        while (iterator != NULL) {
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   183
            if (iterator->ai_family == AF_INET) {
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   184
                jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   185
                if (IS_NULL(iaObj)) {
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   186
                    ret = NULL;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   187
                    goto cleanupAndReturn;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   188
                }
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   189
                setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr));
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   190
                setInetAddress_hostName(env, iaObj, host);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   191
                (*env)->SetObjectArrayElement(env, ret, (inetIndex | originalIndex), iaObj);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   192
                inetIndex++;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   193
            } else if (iterator->ai_family == AF_INET6) {
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   194
                jint scope = 0;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   195
                jboolean ret1;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   196
                jobject iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   197
                if (IS_NULL(iaObj)) {
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   198
                    ret = NULL;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   199
                    goto cleanupAndReturn;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   200
                }
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   201
                ret1 = setInet6Address_ipaddress(env, iaObj, (char *)&(((struct sockaddr_in6*)iterator->ai_addr)->sin6_addr));
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   202
                if (ret1 == JNI_FALSE) {
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   203
                    ret = NULL;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   204
                    goto cleanupAndReturn;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   205
                }
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   206
                scope = ((struct sockaddr_in6 *)iterator->ai_addr)->sin6_scope_id;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   207
                if (scope != 0) { // zero is default value, no need to set
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   208
                    setInet6Address_scopeid(env, iaObj, scope);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   209
                }
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   210
                setInetAddress_hostName(env, iaObj, host);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   211
                (*env)->SetObjectArrayElement(env, ret, (inet6Index | originalIndex), iaObj);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   212
                inet6Index++;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   213
            }
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   214
            if (addressPreference == java_net_InetAddress_PREFER_SYSTEM_VALUE) {
38552
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37362
diff changeset
   215
                originalIndex++;
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37362
diff changeset
   216
                inetIndex = inet6Index = 0;
ca398af91529 8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents: 37362
diff changeset
   217
            }
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   218
            iterator = iterator->ai_next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
cleanupAndReturn:
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   222
    JNU_ReleaseStringPlatformChars(env, host, hostname);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   223
    while (resNew != NULL) {
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   224
        last = resNew;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   225
        resNew = resNew->ai_next;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   226
        free(last);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   228
    if (res != NULL) {
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   229
        freeaddrinfo(res);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   230
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 * Method:    getHostByAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 * Signature: (I)Ljava/lang/String;
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   238
 *
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   239
 * Theoretically the UnknownHostException could be enriched with gai error
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   240
 * information. But as it is silently ignored anyway, there's no need for this.
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   241
 * It's only important that either a valid hostname is returned or an
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   242
 * UnknownHostException is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
Java_java_net_Inet6AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   246
                                             jbyteArray addrArray) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    jstring ret = NULL;
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   248
    char host[NI_MAXHOST + 1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    jbyte caddr[16];
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   251
    SOCKETADDRESS sa;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   253
    memset((void *)&sa, 0, sizeof(SOCKETADDRESS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   255
    // construct a sockaddr_in structure (AF_INET or AF_INET6)
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   256
    if ((*env)->GetArrayLength(env, addrArray) == 4) {
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   257
        jint addr;
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   258
        (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   259
        addr = ((caddr[0] << 24) & 0xff000000);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   260
        addr |= ((caddr[1] << 16) & 0xff0000);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   261
        addr |= ((caddr[2] << 8) & 0xff00);
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   262
        addr |= (caddr[3] & 0xff);
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   263
        sa.sa4.sin_addr.s_addr = htonl(addr);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   264
        sa.sa4.sin_family = AF_INET;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   265
        len = sizeof(struct sockaddr_in);
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   266
    } else {
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   267
        (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   268
        memcpy((void *)&sa.sa6.sin6_addr, caddr, sizeof(struct in6_addr));
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   269
        sa.sa6.sin6_family = AF_INET6;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   270
        len = sizeof(struct sockaddr_in6);
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   271
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   273
    if (getnameinfo(&sa.sa, len, host, NI_MAXHOST, NULL, 0, NI_NAMEREQD)) {
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   274
        JNU_ThrowByName(env, "java/net/UnknownHostException", NULL);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   275
    } else {
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7814
diff changeset
   276
        ret = (*env)->NewStringUTF(env, host);
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   277
        if (ret == NULL) {
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   278
            JNU_ThrowByName(env, "java/net/UnknownHostException", NULL);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   279
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   285
/**
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   286
 * ping implementation using tcp port 7 (echo)
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   287
 */
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   288
static jboolean
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   289
tcp_ping6(JNIEnv *env, SOCKETADDRESS *sa, SOCKETADDRESS *netif, jint timeout,
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   290
          jint ttl)
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   291
{
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   292
    jint fd;
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   293
    int connect_rv = -1;
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   294
    WSAEVENT hEvent;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   295
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   296
    // open a TCP socket
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   297
    fd = NET_Socket(AF_INET6, SOCK_STREAM, 0);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   298
    if (fd == SOCKET_ERROR) {
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   299
        // note: if you run out of fds, you may not be able to load
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   300
        // the exception class, and get a NoClassDefFoundError instead.
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   301
        NET_ThrowNew(env, WSAGetLastError(), "Can't create socket");
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   302
        return JNI_FALSE;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   303
    }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   304
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   305
    // set TTL
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   306
    if (ttl > 0) {
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   307
        setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (const char *)&ttl, sizeof(ttl));
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   308
    }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   309
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   310
    // A network interface was specified, so let's bind to it.
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   311
    if (netif != NULL) {
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   312
        if (bind(fd, &netif->sa, sizeof(struct sockaddr_in6)) < 0) {
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   313
            NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket to interface");
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   314
            closesocket(fd);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   315
            return JNI_FALSE;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   316
        }
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   317
    }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   318
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   319
    // Make the socket non blocking so we can use select/poll.
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   320
    hEvent = WSACreateEvent();
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   321
    WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   322
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   323
    sa->sa6.sin6_port = htons(7); // echo port
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   324
    connect_rv = connect(fd, &sa->sa, sizeof(struct sockaddr_in6));
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   325
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   326
    // connection established or refused immediately, either way it means
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   327
    // we were able to reach the host!
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   328
    if (connect_rv == 0 || WSAGetLastError() == WSAECONNREFUSED) {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   329
        WSACloseEvent(hEvent);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   330
        closesocket(fd);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   331
        return JNI_TRUE;
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   332
    }
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   333
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   334
    switch (WSAGetLastError()) {
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   335
    case WSAEHOSTUNREACH:   // Host Unreachable
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   336
    case WSAENETUNREACH:    // Network Unreachable
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   337
    case WSAENETDOWN:       // Network is down
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   338
    case WSAEPFNOSUPPORT:   // Protocol Family unsupported
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   339
        WSACloseEvent(hEvent);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   340
        closesocket(fd);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   341
        return JNI_FALSE;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   342
    case WSAEWOULDBLOCK:    // this is expected as we'll probably have to wait
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   343
        break;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   344
    default:
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   345
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   346
                                     "connect failed");
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   347
        WSACloseEvent(hEvent);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   348
        closesocket(fd);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   349
        return JNI_FALSE;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   350
    }
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   351
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   352
    timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   353
    if (timeout >= 0) {
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   354
        // connection has been established, check for error condition
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   355
        int optlen = sizeof(connect_rv);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   356
        if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&connect_rv,
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   357
                       &optlen) < 0)
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   358
        {
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   359
            connect_rv = WSAGetLastError();
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   360
        }
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   361
        if (connect_rv == 0 || connect_rv == WSAECONNREFUSED) {
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   362
            WSACloseEvent(hEvent);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   363
            closesocket(fd);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   364
            return JNI_TRUE;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   365
        }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   366
    }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   367
    WSACloseEvent(hEvent);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   368
    closesocket(fd);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   369
    return JNI_FALSE;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   370
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
 * ping implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
 * Send a ICMP_ECHO_REQUEST packet every second until either the timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
 * expires or a answer is received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
 * Returns true is an ECHO_REPLY is received, otherwise, false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
static jboolean
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   379
ping6(JNIEnv *env, HANDLE hIcmpFile, SOCKETADDRESS *sa,
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   380
      SOCKETADDRESS *netif, jint timeout)
32266
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   381
{
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   382
    DWORD dwRetVal = 0;
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   383
    char SendData[32] = {0};
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   384
    LPVOID ReplyBuffer = NULL;
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   385
    DWORD ReplySize = 0;
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   386
    IP_OPTION_INFORMATION ipInfo = {255, 0, 0, 0, NULL};
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   387
    SOCKETADDRESS dftNetif;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
32266
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   389
    ReplySize = sizeof(ICMPV6_ECHO_REPLY) + sizeof(SendData);
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   390
    ReplyBuffer = (VOID *)malloc(ReplySize);
32266
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   391
    if (ReplyBuffer == NULL) {
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   392
        IcmpCloseHandle(hIcmpFile);
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   393
        NET_ThrowNew(env, WSAGetLastError(), "Unable to allocate memory");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
32266
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   397
    //define local source information
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   398
    if (netif == NULL) {
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   399
        dftNetif.sa6.sin6_addr = in6addr_any;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   400
        dftNetif.sa6.sin6_family = AF_INET6;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   401
        dftNetif.sa6.sin6_flowinfo = 0;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   402
        dftNetif.sa6.sin6_port = 0;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   403
        netif = &dftNetif;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   404
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
32266
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   406
    dwRetVal = Icmp6SendEcho2(hIcmpFile,    // HANDLE IcmpHandle,
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   407
                              NULL,         // HANDLE Event,
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   408
                              NULL,         // PIO_APC_ROUTINE ApcRoutine,
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   409
                              NULL,         // PVOID ApcContext,
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   410
                              &netif->sa6,  // struct sockaddr_in6 *SourceAddress,
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   411
                              &sa->sa6,     // struct sockaddr_in6 *DestinationAddress,
32266
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   412
                              SendData,     // LPVOID RequestData,
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   413
                              sizeof(SendData), // WORD RequestSize,
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   414
                              &ipInfo,      // PIP_OPTION_INFORMATION RequestOptions,
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   415
                              ReplyBuffer,  // LPVOID ReplyBuffer,
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   416
                              ReplySize,    // DWORD ReplySize,
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   417
                              timeout);     // DWORD Timeout
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
32266
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   419
    free(ReplyBuffer);
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   420
    IcmpCloseHandle(hIcmpFile);
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   421
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   422
    if (dwRetVal == 0) { // if the call failed
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   423
        return JNI_FALSE;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   424
    } else {
32266
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   425
        return JNI_TRUE;
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   426
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
 * Class:     java_net_Inet6AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
 * Method:    isReachable0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
 * Signature: ([bII[bI)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
Java_java_net_Inet6AddressImpl_isReachable0(JNIEnv *env, jobject this,
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   436
                                            jbyteArray addrArray, jint scope,
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   437
                                            jint timeout, jbyteArray ifArray,
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   438
                                            jint ttl, jint if_scope)
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   439
{
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    jbyte caddr[16];
32266
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   441
    jint sz;
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   442
    SOCKETADDRESS sa, inf, *netif = NULL;
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   443
    HANDLE hIcmpFile;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   445
    // If IPv6 is not enabled, then we can't reach an IPv6 address, can we?
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   446
    // Actually, we probably shouldn't even get here.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    if (!ipv6_available()) {
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   448
        return JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   450
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   451
    // If it's an IPv4 address, ICMP won't work with IPv4 mapped address,
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   452
    // therefore, let's delegate to the Inet4Address method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    sz = (*env)->GetArrayLength(env, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    if (sz == 4) {
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   455
        return Java_java_net_Inet4AddressImpl_isReachable0(env, this,
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   456
                                                           addrArray, timeout,
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   457
                                                           ifArray, ttl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   460
    // load address to SOCKETADDRESS
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   461
    memset((char *)caddr, 0, 16);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    (*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   463
    memset((char *)&sa, 0, sizeof(SOCKETADDRESS));
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   464
    memcpy((void *)&sa.sa6.sin6_addr, caddr, sizeof(struct in6_addr));
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   465
    sa.sa6.sin6_family = AF_INET6;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    if (scope > 0) {
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   467
        sa.sa6.sin6_scope_id = scope;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   470
    // load network interface address to SOCKETADDRESS, if specified
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   471
    if (!(IS_NULL(ifArray))) {
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   472
        memset((char *)caddr, 0, 16);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   473
        (*env)->GetByteArrayRegion(env, ifArray, 0, 16, caddr);
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   474
        memset((char *)&inf, 0, sizeof(SOCKETADDRESS));
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   475
        memcpy((void *)&inf.sa6.sin6_addr, caddr, sizeof(struct in6_addr));
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   476
        inf.sa6.sin6_family = AF_INET6;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   477
        inf.sa6.sin6_scope_id = if_scope;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   478
        netif = &inf;
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   479
    }
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   480
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   481
    // Let's try to create an ICMP handle.
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   482
    hIcmpFile = Icmp6CreateFile();
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   483
    if (hIcmpFile == INVALID_HANDLE_VALUE) {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   484
        int err = WSAGetLastError();
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   485
        if (err == ERROR_ACCESS_DENIED) {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   486
            // fall back to TCP echo if access is denied to ICMP
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   487
            return tcp_ping6(env, &sa, netif, timeout, ttl);
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   488
        } else {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   489
            NET_ThrowNew(env, err, "Unable to create ICMP file handle");
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   490
            return JNI_FALSE;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   491
        }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   492
    } else {
43615
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   493
        // It didn't fail, so we can use ICMP.
809f9acb701a 8167457: Fixes for InetAddressImpl native coding on Windows
clanger
parents: 41834
diff changeset
   494
        return ping6(env, hIcmpFile, &sa, netif, timeout);
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 32266
diff changeset
   495
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
}