jdk/src/java.base/windows/native/libnet/Inet4AddressImpl.c
author robm
Fri, 23 Sep 2016 15:31:46 +0100
changeset 41126 7a2fc45a887d
parent 37362 9b3486880437
child 41137 cbc639238d91
permissions -rw-r--r--
8159410: InetAddress.isReachable returns true for non existing IP adresses Reviewed-by: chegar, coffeys
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 22646
diff changeset
     2
 * Copyright (c) 2000, 2013, 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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <winsock2.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <malloc.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <sys/types.h>
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 715
diff changeset
    33
#include <process.h>
32266
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
    34
#include <iphlpapi.h>
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
    35
#include <icmpapi.h>
41126
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
    36
#include <WinError.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include "java_net_InetAddress.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include "java_net_Inet4AddressImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include "icmp.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Returns true if hostname is in dotted IP address format. Note that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * function performs a syntax check only. For each octet it just checks that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * the octet is at most 3 digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
jboolean isDottedIPAddress(const char *hostname, unsigned int *addrp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    char *c = (char *)hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    int octets = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    unsigned int cur = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    int digit_cnt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    while (*c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (*c == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            if (digit_cnt == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                if (octets < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                    addrp[octets++] = cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                    cur = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    digit_cnt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            c++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if ((*c < '0') || (*c > '9')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        digit_cnt++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (digit_cnt > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        /* don't check if current octet > 255 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        cur = cur*10 + (*c - '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        /* Move onto next character and check for EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        c++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (*c == '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (octets < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                addrp[octets++] = cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    return (jboolean)(octets == 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * Method:    getLocalHostName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * Signature: ()Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
Java_java_net_Inet4AddressImpl_getLocalHostName (JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    char hostname[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    if (gethostname(hostname, sizeof hostname) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        strcpy(hostname, "localhost");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    return JNU_NewStringPlatform(env, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * Find an internet address for a given hostname.  Not this this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * code only works for addresses of type INET. The translation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * of %d.%d.%d.%d to an address (int) occurs in java now, so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * String "host" shouldn't be a %d.%d.%d.%d string. The only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * exception should be when any of the %d are out of range and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * we fallback to a lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * Method:    lookupAllHostAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * Signature: (Ljava/lang/String;)[[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * This is almost shared code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
JNIEXPORT jobjectArray JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                                jstring host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    const char *hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    struct hostent *hp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    unsigned int addr[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    jobjectArray ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   141
    initInetAddressIDs(env);
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   142
    JNU_CHECK_EXCEPTION_RETURN(env, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    if (IS_NULL(host)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        JNU_ThrowNullPointerException(env, "host argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    CHECK_NULL_RETURN(hostname, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * The NT/2000 resolver tolerates a space in front of localhost. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * is not consistent with other implementations of gethostbyname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * In addition we must do a white space check on Solaris to avoid a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * bug whereby 0.0.0.0 is returned if any host name has a white space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    if (isspace(hostname[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * If the format is x.x.x.x then don't use gethostbyname as Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * is unable to handle octets which are out of range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    if (isDottedIPAddress(hostname, &addr[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        unsigned int address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        jobject iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         * Are any of the octets out of range?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (addr[0] > 255 || addr[1] > 255 || addr[2] > 255 || addr[3] > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         * Return an byte array with the populated address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        address = (addr[3]<<24) & 0xff000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        address |= (addr[2]<<16) & 0xff0000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        address |= (addr[1]<<8) & 0xff00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        address |= addr[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   186
        ret = (*env)->NewObjectArray(env, 1, ia_class, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (IS_NULL(ret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   192
        iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
          ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
          goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 9035
diff changeset
   197
        setInetAddress_addr(env, iaObj, ntohl(address));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        (*env)->SetObjectArrayElement(env, ret, 0, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        JNU_ReleaseStringPlatformChars(env, host, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Perform the lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    if ((hp = gethostbyname((char*)hostname)) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        struct in_addr **addrp = (struct in_addr **) hp->h_addr_list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int len = sizeof(struct in_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        while (*addrp != (struct in_addr *) 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            addrp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   216
        ret = (*env)->NewObjectArray(env, i, ia_class, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (IS_NULL(ret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        addrp = (struct in_addr **) hp->h_addr_list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        while (*addrp != (struct in_addr *) 0) {
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   225
          jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
          if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
          }
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 9035
diff changeset
   230
          setInetAddress_addr(env, iaObj, ntohl((*addrp)->s_addr));
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 9035
diff changeset
   231
          setInetAddress_hostName(env, iaObj, host);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
          (*env)->SetObjectArrayElement(env, ret, i, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
          addrp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
          i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
20857
734dc47ee277 8010371: getaddrinfo can fail with EAI_SYSTEM/EAGAIN, causes UnknownHostException to be thrown
bpb
parents: 16870
diff changeset
   236
    } else if (WSAGetLastError() == WSATRY_AGAIN) {
734dc47ee277 8010371: getaddrinfo can fail with EAI_SYSTEM/EAGAIN, causes UnknownHostException to be thrown
bpb
parents: 16870
diff changeset
   237
        NET_ThrowByNameWithLastError(env,
734dc47ee277 8010371: getaddrinfo can fail with EAI_SYSTEM/EAGAIN, causes UnknownHostException to be thrown
bpb
parents: 16870
diff changeset
   238
                                     JNU_JAVANETPKG "UnknownHostException",
734dc47ee277 8010371: getaddrinfo can fail with EAI_SYSTEM/EAGAIN, causes UnknownHostException to be thrown
bpb
parents: 16870
diff changeset
   239
                                     hostname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
cleanupAndReturn:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    JNU_ReleaseStringPlatformChars(env, host, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 * Method:    getHostByAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 * Signature: (I)Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
Java_java_net_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                            jbyteArray addrArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    struct hostent *hp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    jbyte caddr[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    jint addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    addr = ((caddr[0]<<24) & 0xff000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    addr |= ((caddr[1] <<16) & 0xff0000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    addr |= ((caddr[2] <<8) & 0xff00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    addr |= (caddr[3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    addr = htonl(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    if (hp == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    if (hp->h_name == NULL) { /* Deal with bug in Windows XP */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    return JNU_NewStringPlatform(env, hp->h_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   279
static jboolean
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   280
tcp_ping4(JNIEnv *env,
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   281
          jbyteArray addrArray,
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   282
          jint timeout,
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   283
          jbyteArray ifArray,
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   284
          jint ttl)
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   285
{
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   286
    jint addr;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   287
    jbyte caddr[4];
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   288
    jint fd;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   289
    struct sockaddr_in him;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   290
    struct sockaddr_in* netif = NULL;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   291
    struct sockaddr_in inf;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   292
    int len = 0;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   293
    WSAEVENT hEvent;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   294
    int connect_rv = -1;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   295
    int sz;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   296
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   297
    /**
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   298
     * Convert IP address from byte array to integer
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   299
     */
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   300
    sz = (*env)->GetArrayLength(env, addrArray);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   301
    if (sz != 4) {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   302
        return JNI_FALSE;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   303
    }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   304
    memset((char *) &him, 0, sizeof(him));
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   305
    memset((char *) caddr, 0, sizeof(caddr));
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   306
    (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   307
    addr = ((caddr[0]<<24) & 0xff000000);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   308
    addr |= ((caddr[1] <<16) & 0xff0000);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   309
    addr |= ((caddr[2] <<8) & 0xff00);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   310
    addr |= (caddr[3] & 0xff);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   311
    addr = htonl(addr);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   312
    /**
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   313
     * Socket address
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   314
     */
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   315
    him.sin_addr.s_addr = addr;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   316
    him.sin_family = AF_INET;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   317
    len = sizeof(him);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   318
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   319
    /**
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   320
     * If a network interface was specified, let's convert its address
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   321
     * as well.
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   322
     */
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   323
    if (!(IS_NULL(ifArray))) {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   324
        memset((char *) caddr, 0, sizeof(caddr));
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   325
        (*env)->GetByteArrayRegion(env, ifArray, 0, 4, caddr);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   326
        addr = ((caddr[0]<<24) & 0xff000000);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   327
        addr |= ((caddr[1] <<16) & 0xff0000);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   328
        addr |= ((caddr[2] <<8) & 0xff00);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   329
        addr |= (caddr[3] & 0xff);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   330
        addr = htonl(addr);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   331
        inf.sin_addr.s_addr = addr;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   332
        inf.sin_family = AF_INET;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   333
        inf.sin_port = 0;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   334
        netif = &inf;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   335
    }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   336
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   337
    /*
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   338
     * Can't create a raw socket, so let's try a TCP socket
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   339
     */
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   340
    fd = NET_Socket(AF_INET, SOCK_STREAM, 0);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   341
    if (fd == -1) {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   342
        /* note: if you run out of fds, you may not be able to load
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   343
         * the exception class, and get a NoClassDefFoundError
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   344
         * instead.
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   345
         */
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   346
        NET_ThrowNew(env, WSAGetLastError(), "Can't create socket");
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   347
        return JNI_FALSE;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   348
    }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   349
    if (ttl > 0) {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   350
        setsockopt(fd, IPPROTO_IP, IP_TTL, (const char *)&ttl, sizeof(ttl));
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   351
    }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   352
    /*
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   353
     * A network interface was specified, so let's bind to it.
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   354
     */
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   355
    if (netif != NULL) {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   356
        if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   357
            NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket");
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   358
            closesocket(fd);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   359
            return JNI_FALSE;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   360
        }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   361
    }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   363
    /*
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   364
     * Make the socket non blocking so we can use select/poll.
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   365
     */
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   366
    hEvent = WSACreateEvent();
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   367
    WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   368
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   369
    /* no need to use NET_Connect as non-blocking */
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   370
    him.sin_port = htons(7);    /* Echo */
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   371
    connect_rv = connect(fd, (struct sockaddr *)&him, len);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   372
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   373
    /**
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   374
     * connection established or refused immediately, either way it means
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   375
     * we were able to reach the host!
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   376
     */
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   377
    if (connect_rv == 0 || WSAGetLastError() == WSAECONNREFUSED) {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   378
        WSACloseEvent(hEvent);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   379
        closesocket(fd);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   380
        return JNI_TRUE;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   381
    } else {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   382
        int optlen;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   383
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   384
        switch (WSAGetLastError()) {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   385
        case WSAEHOSTUNREACH:   /* Host Unreachable */
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   386
        case WSAENETUNREACH:    /* Network Unreachable */
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   387
        case WSAENETDOWN:       /* Network is down */
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   388
        case WSAEPFNOSUPPORT:   /* Protocol Family unsupported */
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   389
            WSACloseEvent(hEvent);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   390
            closesocket(fd);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   391
            return JNI_FALSE;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   392
        }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   393
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   394
        if (WSAGetLastError() != WSAEWOULDBLOCK) {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   395
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   396
                                         "connect failed");
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   397
            WSACloseEvent(hEvent);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   398
            closesocket(fd);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   399
            return JNI_FALSE;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   400
        }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   401
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   402
        timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   403
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   404
        /* has connection been established */
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   405
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   406
        if (timeout >= 0) {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   407
            optlen = sizeof(connect_rv);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   408
            if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   409
                           &optlen) <0) {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   410
                connect_rv = WSAGetLastError();
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   411
            }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   412
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   413
            if (connect_rv == 0 || connect_rv == WSAECONNREFUSED) {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   414
                WSACloseEvent(hEvent);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   415
                closesocket(fd);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   416
                return JNI_TRUE;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   417
            }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   418
        }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   419
    }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   420
    WSACloseEvent(hEvent);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   421
    closesocket(fd);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   422
    return JNI_FALSE;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   423
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
 * ping implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
 * Send a ICMP_ECHO_REQUEST packet every second until either the timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
 * expires or a answer is received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
 * Returns true is an ECHO_REPLY is received, otherwise, false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
static jboolean
32845
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   432
ping4(JNIEnv *env,
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   433
      unsigned long src_addr,
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   434
      unsigned long dest_addr,
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   435
      jint timeout,
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   436
      HANDLE hIcmpFile)
32845
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   437
{
32266
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   438
    // See https://msdn.microsoft.com/en-us/library/aa366050%28VS.85%29.aspx
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
32266
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   440
    DWORD dwRetVal = 0;
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   441
    char SendData[32] = {0};
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   442
    LPVOID ReplyBuffer = NULL;
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   443
    DWORD ReplySize = 0;
34529
3a49a6c8b989 8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents: 32845
diff changeset
   444
    jboolean ret = JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
41126
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   446
    // https://msdn.microsoft.com/en-us/library/windows/desktop/aa366051%28v=vs.85%29.aspx
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   447
    ReplySize = sizeof(ICMP_ECHO_REPLY)   // The buffer should be large enough
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   448
                                          // to hold at least one ICMP_ECHO_REPLY
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   449
                                          // structure
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   450
                + sizeof(SendData)        // plus RequestSize bytes of data.
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   451
                + 8;                      // This buffer should also be large enough
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   452
                                          // to also hold 8 more bytes of data
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   453
                                          // (the size of an ICMP error message)
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   454
32266
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   455
    ReplyBuffer = (VOID*) malloc(ReplySize);
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   456
    if (ReplyBuffer == NULL) {
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   457
        IcmpCloseHandle(hIcmpFile);
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   458
        NET_ThrowNew(env, WSAGetLastError(), "Unable to allocate memory");
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   459
        return JNI_FALSE;
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   460
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
32845
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   462
    if (src_addr == 0) {
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   463
        dwRetVal = IcmpSendEcho(hIcmpFile,  // HANDLE IcmpHandle,
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   464
                                dest_addr,  // IPAddr DestinationAddress,
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   465
                                SendData,   // LPVOID RequestData,
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   466
                                sizeof(SendData),   // WORD RequestSize,
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   467
                                NULL,       // PIP_OPTION_INFORMATION RequestOptions,
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   468
                                ReplyBuffer,// LPVOID ReplyBuffer,
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   469
                                ReplySize,  // DWORD ReplySize,
34529
3a49a6c8b989 8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents: 32845
diff changeset
   470
                                // Note: IcmpSendEcho and its derivatives
3a49a6c8b989 8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents: 32845
diff changeset
   471
                                // seem to have an undocumented minimum
3a49a6c8b989 8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents: 32845
diff changeset
   472
                                // timeout of 1000ms below which the
3a49a6c8b989 8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents: 32845
diff changeset
   473
                                // api behaves inconsistently.
3a49a6c8b989 8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents: 32845
diff changeset
   474
                                (timeout < 1000) ? 1000 : timeout);   // DWORD Timeout
32845
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   475
    } else {
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   476
        dwRetVal = IcmpSendEcho2Ex(hIcmpFile,  // HANDLE IcmpHandle,
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   477
                                   NULL,       // HANDLE Event
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   478
                                   NULL,       // PIO_APC_ROUTINE ApcRoutine
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   479
                                   NULL,       // ApcContext
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   480
                                   src_addr,   // IPAddr SourceAddress,
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   481
                                   dest_addr,  // IPAddr DestinationAddress,
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   482
                                   SendData,   // LPVOID RequestData,
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   483
                                   sizeof(SendData),   // WORD RequestSize,
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   484
                                   NULL,       // PIP_OPTION_INFORMATION RequestOptions,
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   485
                                   ReplyBuffer,// LPVOID ReplyBuffer,
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   486
                                   ReplySize,  // DWORD ReplySize,
34529
3a49a6c8b989 8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents: 32845
diff changeset
   487
                                   (timeout < 1000) ? 1000 : timeout);   // DWORD Timeout
3a49a6c8b989 8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents: 32845
diff changeset
   488
    }
3a49a6c8b989 8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents: 32845
diff changeset
   489
41126
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   490
    if (dwRetVal == 0) { // if the call failed
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   491
        TCHAR *buf;
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   492
        DWORD err = WSAGetLastError();
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   493
        switch (err) {
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   494
            case ERROR_NO_NETWORK:
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   495
            case ERROR_NETWORK_UNREACHABLE:
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   496
            case ERROR_HOST_UNREACHABLE:
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   497
            case ERROR_PROTOCOL_UNREACHABLE:
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   498
            case ERROR_PORT_UNREACHABLE:
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   499
            case ERROR_REQUEST_ABORTED:
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   500
            case ERROR_INCORRECT_ADDRESS:
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   501
            case ERROR_HOST_DOWN:
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   502
            case WSAEHOSTUNREACH:   /* Host Unreachable */
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   503
            case WSAENETUNREACH:    /* Network Unreachable */
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   504
            case WSAENETDOWN:       /* Network is down */
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   505
            case WSAEPFNOSUPPORT:   /* Protocol Family unsupported */
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   506
            case IP_REQ_TIMED_OUT:
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   507
                break;
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   508
            default:
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   509
                FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   510
                        NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   511
                        (LPTSTR)&buf, 0, NULL);
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   512
                NET_ThrowNew(env, err, buf);
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   513
                LocalFree(buf);
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   514
                break;
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   515
        }
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   516
    } else {
34529
3a49a6c8b989 8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents: 32845
diff changeset
   517
        PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
41126
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   518
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   519
        // This is to take into account the undocumented minimum
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   520
        // timeout mentioned in the IcmpSendEcho call above.
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   521
        // We perform an extra check to make sure that our
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   522
        // roundtrip time was less than our desired timeout
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   523
        // for cases where that timeout is < 1000ms.
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   524
        if (pEchoReply->Status == IP_SUCCESS
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   525
                && (int)pEchoReply->RoundTripTime <= timeout)
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   526
        {
34529
3a49a6c8b989 8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents: 32845
diff changeset
   527
            ret = JNI_TRUE;
41126
7a2fc45a887d 8159410: InetAddress.isReachable returns true for non existing IP adresses
robm
parents: 37362
diff changeset
   528
        }
32845
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   529
    }
32266
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   530
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   531
    free(ReplyBuffer);
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   532
    IcmpCloseHandle(hIcmpFile);
e0a235a11254 8133015: InetAddress.isReachable(tmout) returning wrong value on Windows for IPv6
robm
parents: 25859
diff changeset
   533
34529
3a49a6c8b989 8143397: It looks like InetAddress.isReachable(timeout) works incorrectly
robm
parents: 32845
diff changeset
   534
    return ret;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
 * Method:    isReachable0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
 * Signature: ([bI[bI)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
Java_java_net_Inet4AddressImpl_isReachable0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                                           jbyteArray addrArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                                           jint timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                                           jbyteArray ifArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                                           jint ttl) {
32845
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   548
    jint src_addr = 0;
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   549
    jint dest_addr = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    jbyte caddr[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    int sz;
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   552
    HANDLE hIcmpFile;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Convert IP address from byte array to integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    sz = (*env)->GetArrayLength(env, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    if (sz != 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
      return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    memset((char *) caddr, 0, sizeof(caddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
32845
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   563
    dest_addr = ((caddr[0]<<24) & 0xff000000);
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   564
    dest_addr |= ((caddr[1] <<16) & 0xff0000);
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   565
    dest_addr |= ((caddr[2] <<8) & 0xff00);
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   566
    dest_addr |= (caddr[3] & 0xff);
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   567
    dest_addr = htonl(dest_addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
32845
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   569
    /**
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   570
     * If a network interface was specified, let's convert its address
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   571
     * as well.
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   572
     */
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   573
    if (!(IS_NULL(ifArray))) {
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   574
        memset((char *) caddr, 0, sizeof(caddr));
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   575
        (*env)->GetByteArrayRegion(env, ifArray, 0, 4, caddr);
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   576
        src_addr = ((caddr[0]<<24) & 0xff000000);
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   577
        src_addr |= ((caddr[1] <<16) & 0xff0000);
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   578
        src_addr |= ((caddr[2] <<8) & 0xff00);
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   579
        src_addr |= (caddr[3] & 0xff);
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   580
        src_addr = htonl(src_addr);
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   581
    }
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   582
37362
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   583
    hIcmpFile = IcmpCreateFile();
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   584
    if (hIcmpFile == INVALID_HANDLE_VALUE) {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   585
        int err = WSAGetLastError();
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   586
        if (err == ERROR_ACCESS_DENIED) {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   587
            // fall back to TCP echo if access is denied to ICMP
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   588
            return tcp_ping4(env, addrArray, timeout, ifArray, ttl);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   589
        } else {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   590
            NET_ThrowNew(env, err, "Unable to create ICMP file handle");
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   591
            return JNI_FALSE;
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   592
        }
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   593
    } else {
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   594
        return ping4(env, src_addr, dest_addr, timeout, hIcmpFile);
9b3486880437 8150234: Windows 10 App Containers disallow access to ICMP calls
robm
parents: 34529
diff changeset
   595
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
}
32845
e630c3008f2c 8135305: InetAddress.isReachable reports true when loopback interface is specified
robm
parents: 32266
diff changeset
   597