jdk/src/windows/native/java/net/Inet4AddressImpl.c
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7814 dff0b40f9ac8
child 16870 f35b2bd19761
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7814
diff changeset
     2
 * Copyright (c) 2000, 2011, 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>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "java_net_InetAddress.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "java_net_Inet4AddressImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include "icmp.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Returns true if hostname is in dotted IP address format. Note that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * function performs a syntax check only. For each octet it just checks that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the octet is at most 3 digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
jboolean isDottedIPAddress(const char *hostname, unsigned int *addrp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    char *c = (char *)hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    int octets = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    unsigned int cur = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    int digit_cnt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    while (*c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if (*c == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            if (digit_cnt == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                if (octets < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                    addrp[octets++] = cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                    cur = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                    digit_cnt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            c++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if ((*c < '0') || (*c > '9')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        digit_cnt++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (digit_cnt > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        /* don't check if current octet > 255 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        cur = cur*10 + (*c - '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        /* Move onto next character and check for EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        c++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (*c == '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (octets < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                addrp[octets++] = cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    return (jboolean)(octets == 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * Method:    getLocalHostName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * Signature: ()Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
Java_java_net_Inet4AddressImpl_getLocalHostName (JNIEnv *env, jobject this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    char hostname[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    if (gethostname(hostname, sizeof hostname) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        strcpy(hostname, "localhost");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    return JNU_NewStringPlatform(env, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
static jclass ni_iacls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
static jclass ni_ia4cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
static jmethodID ni_ia4ctrID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
static jfieldID ni_iaaddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
static jfieldID ni_iahostID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
static jfieldID ni_iafamilyID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
static int initialized = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * Find an internet address for a given hostname.  Not this this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * code only works for addresses of type INET. The translation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * of %d.%d.%d.%d to an address (int) occurs in java now, so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * String "host" shouldn't be a %d.%d.%d.%d string. The only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * exception should be when any of the %d are out of range and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * we fallback to a lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * Method:    lookupAllHostAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * Signature: (Ljava/lang/String;)[[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * This is almost shared code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
JNIEXPORT jobjectArray JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                                jstring host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    const char *hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    struct hostent *hp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    unsigned int addr[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    jobjectArray ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
      ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
      ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
      ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
      ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
      ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
      ni_iaaddressID = (*env)->GetFieldID(env, ni_iacls, "address", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
      ni_iafamilyID = (*env)->GetFieldID(env, ni_iacls, "family", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
      ni_iahostID = (*env)->GetFieldID(env, ni_iacls, "hostName", "Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
      initialized = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    if (IS_NULL(host)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        JNU_ThrowNullPointerException(env, "host argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    CHECK_NULL_RETURN(hostname, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * The NT/2000 resolver tolerates a space in front of localhost. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * is not consistent with other implementations of gethostbyname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * In addition we must do a white space check on Solaris to avoid a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * bug whereby 0.0.0.0 is returned if any host name has a white space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    if (isspace(hostname[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * If the format is x.x.x.x then don't use gethostbyname as Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * is unable to handle octets which are out of range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    if (isDottedIPAddress(hostname, &addr[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        unsigned int address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        jobject iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         * Are any of the octets out of range?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (addr[0] > 255 || addr[1] > 255 || addr[2] > 255 || addr[3] > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
         * Return an byte array with the populated address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        address = (addr[3]<<24) & 0xff000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        address |= (addr[2]<<16) & 0xff0000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        address |= (addr[1]<<8) & 0xff00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        address |= addr[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        ret = (*env)->NewObjectArray(env, 1, ni_iacls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (IS_NULL(ret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
          ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
          goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        (*env)->SetIntField(env, iaObj, ni_iaaddressID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                            ntohl(address));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        (*env)->SetObjectArrayElement(env, ret, 0, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        JNU_ReleaseStringPlatformChars(env, host, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Perform the lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    if ((hp = gethostbyname((char*)hostname)) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        struct in_addr **addrp = (struct in_addr **) hp->h_addr_list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        int len = sizeof(struct in_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        while (*addrp != (struct in_addr *) 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            addrp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        ret = (*env)->NewObjectArray(env, i, ni_iacls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (IS_NULL(ret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        addrp = (struct in_addr **) hp->h_addr_list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        while (*addrp != (struct in_addr *) 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
          jobject iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
          if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
          (*env)->SetIntField(env, iaObj, ni_iaaddressID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                              ntohl((*addrp)->s_addr));
707
4e060643fb54 6698625: InetAddress.getLocalHost() failed in returning chinese local host name
chegar
parents: 2
diff changeset
   247
          (*env)->SetObjectField(env, iaObj, ni_iahostID, host);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
          (*env)->SetObjectArrayElement(env, ret, i, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
          addrp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
          i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
cleanupAndReturn:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    JNU_ReleaseStringPlatformChars(env, host, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * Method:    getHostByAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 * Signature: (I)Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
Java_java_net_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                            jbyteArray addrArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    struct hostent *hp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    jbyte caddr[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    jint addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    addr = ((caddr[0]<<24) & 0xff000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    addr |= ((caddr[1] <<16) & 0xff0000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    addr |= ((caddr[2] <<8) & 0xff00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    addr |= (caddr[3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    addr = htonl(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    if (hp == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    if (hp->h_name == NULL) { /* Deal with bug in Windows XP */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    return JNU_NewStringPlatform(env, hp->h_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
 * ping implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
 * Send a ICMP_ECHO_REQUEST packet every second until either the timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
 * expires or a answer is received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
 * Returns true is an ECHO_REPLY is received, otherwise, false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
static jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
ping4(JNIEnv *env, jint fd, struct sockaddr_in* him, jint timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
      struct sockaddr_in* netif, jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    jint size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    jint n, len, hlen1, icmplen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    char sendbuf[1500];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    char recvbuf[1500];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    struct icmp *icmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    struct ip *ip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    WSAEVENT hEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    struct sockaddr sa_recv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    jint tmout2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    u_short pid, seq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    int read_rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /* Initialize the sequence number to a suitable random number and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
       shift right one place to allow sufficient room for increamenting. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    seq = ((unsigned short)rand()) >> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /* icmp_id is a 16 bit data type, therefore down cast the pid */
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 715
diff changeset
   318
    pid = (u_short) _getpid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    size = 60*1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char *) &size, sizeof(size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * A TTL was specified, let's set the socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    if (ttl > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
      setsockopt(fd, IPPROTO_IP, IP_TTL, (const char *) &ttl, sizeof(ttl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * A network interface was specified, let's bind to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    if (netif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
      if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Let's make the socket non blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    hEvent = WSACreateEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * send 1 ICMP REQUEST every second until either we get a valid reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * or the timeout expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
      /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
       * construct the ICMP header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
      memset(sendbuf, 0, 1500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
      icmp = (struct icmp *) sendbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
      icmp->icmp_type = ICMP_ECHO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
      icmp->icmp_code = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
      icmp->icmp_id = htons(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
      icmp->icmp_seq = htons(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
      /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
       * checksum has to be set to zero before we can calculate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
       * real checksum!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
      icmp->icmp_cksum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
      icmp->icmp_cksum = in_cksum((u_short *)icmp, 64);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
      /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
       * Ping!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
      n = sendto(fd, sendbuf, 64, 0, (struct sockaddr *)him,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                 sizeof(struct sockaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
      if (n < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        NET_ThrowNew(env, WSAGetLastError(), "Can't send ICMP packet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
       * wait for 1 second at most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
      tmout2 = timeout > 1000 ? 1000 : timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
      do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (tmout2 >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
          len = sizeof(sa_recv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
          n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0, &sa_recv, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
          ip = (struct ip*) recvbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
          hlen1 = (ip->ip_hl) << 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
          icmp = (struct icmp *) (recvbuf + hlen1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
          icmplen = n - hlen1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
          /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
           * Is that a proper ICMP reply?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
          if (icmplen >= 8 && icmp->icmp_type == ICMP_ECHOREPLY &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
              (ntohs(icmp->icmp_seq) == seq) && (ntohs(icmp->icmp_id) == pid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
      } while (tmout2 > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
      timeout -= 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
      seq++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    } while (timeout > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
 * Method:    isReachable0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
 * Signature: ([bI[bI)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
Java_java_net_Inet4AddressImpl_isReachable0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                                           jbyteArray addrArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                                           jint timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                                           jbyteArray ifArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                                           jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    jint addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    jbyte caddr[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    struct sockaddr_in him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    struct sockaddr_in* netif = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    struct sockaddr_in inf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    WSAEVENT hEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    int connect_rv = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    int sz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * Convert IP address from byte array to integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    sz = (*env)->GetArrayLength(env, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    if (sz != 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
      return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    memset((char *) &him, 0, sizeof(him));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    memset((char *) caddr, 0, sizeof(caddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    addr = ((caddr[0]<<24) & 0xff000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    addr |= ((caddr[1] <<16) & 0xff0000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    addr |= ((caddr[2] <<8) & 0xff00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    addr |= (caddr[3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    addr = htonl(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Socket address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    him.sin_addr.s_addr = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    him.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    len = sizeof(him);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * If a network interface was specified, let's convert its address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    if (!(IS_NULL(ifArray))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
      memset((char *) caddr, 0, sizeof(caddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
      (*env)->GetByteArrayRegion(env, ifArray, 0, 4, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
      addr = ((caddr[0]<<24) & 0xff000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
      addr |= ((caddr[1] <<16) & 0xff0000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
      addr |= ((caddr[2] <<8) & 0xff00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
      addr |= (caddr[3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
      addr = htonl(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
      inf.sin_addr.s_addr = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
      inf.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
      inf.sin_port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
      netif = &inf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
#if 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Windows implementation of ICMP & RAW sockets is too unreliable for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Therefore it's best not to try it at all and rely only on TCP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * We may revisit and enable this code in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * Let's try to create a RAW socket to send ICMP packets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * This usually requires "root" privileges, so it's likely to fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    fd = NET_Socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    if (fd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
       * It didn't fail, so we can use ICMP_ECHO requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        return ping4(env, fd, &him, timeout, netif, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * Can't create a raw socket, so let's try a TCP socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    fd = NET_Socket(AF_INET, SOCK_STREAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    if (fd == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        /* note: if you run out of fds, you may not be able to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
         * the exception class, and get a NoClassDefFoundError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
         * instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        NET_ThrowNew(env, WSAGetLastError(), "Can't create socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    if (ttl > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
      setsockopt(fd, IPPROTO_IP, IP_TTL, (const char *)&ttl, sizeof(ttl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * A network interface was specified, so let's bind to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    if (netif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
      if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * Make the socket non blocking so we can use select/poll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    hEvent = WSACreateEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /* no need to use NET_Connect as non-blocking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    him.sin_port = htons(7);    /* Echo */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    connect_rv = connect(fd, (struct sockaddr *)&him, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * connection established or refused immediately, either way it means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * we were able to reach the host!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    if (connect_rv == 0 || WSAGetLastError() == WSAECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        int optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        switch (WSAGetLastError()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        case WSAEHOSTUNREACH:   /* Host Unreachable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        case WSAENETUNREACH:    /* Network Unreachable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        case WSAENETDOWN:       /* Network is down */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        case WSAEPFNOSUPPORT:   /* Protocol Family unsupported */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
          WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
          closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
          return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        if (WSAGetLastError() != WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                                         "connect failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        /* has connection been established */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if (timeout >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
          optlen = sizeof(connect_rv);
7814
dff0b40f9ac8 7010192: InetAddress.isReachable hits ShouldNotReachHere with hs20-b04 (win)
alanb
parents: 5506
diff changeset
   562
          if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
dff0b40f9ac8 7010192: InetAddress.isReachable hits ShouldNotReachHere with hs20-b04 (win)
alanb
parents: 5506
diff changeset
   563
                         &optlen) <0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            connect_rv = WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
          if (connect_rv == 0 || connect_rv == WSAECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
}