jdk/src/java.base/windows/native/libnet/Inet4AddressImpl.c
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 23015 jdk/src/windows/native/java/net/Inet4AddressImpl.c@73b21ab36615
child 32266 e0a235a11254
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
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>
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * Find an internet address for a given hostname.  Not this this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * code only works for addresses of type INET. The translation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * of %d.%d.%d.%d to an address (int) occurs in java now, so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * String "host" shouldn't be a %d.%d.%d.%d string. The only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * exception should be when any of the %d are out of range and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * we fallback to a lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * Method:    lookupAllHostAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * Signature: (Ljava/lang/String;)[[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * This is almost shared code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
JNIEXPORT jobjectArray JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                                jstring host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    const char *hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    struct hostent *hp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    unsigned int addr[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    jobjectArray ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   138
    initInetAddressIDs(env);
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   139
    JNU_CHECK_EXCEPTION_RETURN(env, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    if (IS_NULL(host)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        JNU_ThrowNullPointerException(env, "host argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    CHECK_NULL_RETURN(hostname, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * The NT/2000 resolver tolerates a space in front of localhost. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * is not consistent with other implementations of gethostbyname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * In addition we must do a white space check on Solaris to avoid a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * bug whereby 0.0.0.0 is returned if any host name has a white space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    if (isspace(hostname[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * If the format is x.x.x.x then don't use gethostbyname as Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * is unable to handle octets which are out of range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    if (isDottedIPAddress(hostname, &addr[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        unsigned int address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        jobject iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
         * Are any of the octets out of range?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (addr[0] > 255 || addr[1] > 255 || addr[2] > 255 || addr[3] > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         * Return an byte array with the populated address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        address = (addr[3]<<24) & 0xff000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        address |= (addr[2]<<16) & 0xff0000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        address |= (addr[1]<<8) & 0xff00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        address |= addr[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   183
        ret = (*env)->NewObjectArray(env, 1, ia_class, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (IS_NULL(ret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   189
        iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
          ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
          goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 9035
diff changeset
   194
        setInetAddress_addr(env, iaObj, ntohl(address));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        (*env)->SetObjectArrayElement(env, ret, 0, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        JNU_ReleaseStringPlatformChars(env, host, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Perform the lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    if ((hp = gethostbyname((char*)hostname)) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        struct in_addr **addrp = (struct in_addr **) hp->h_addr_list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        int len = sizeof(struct in_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        while (*addrp != (struct in_addr *) 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            addrp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   213
        ret = (*env)->NewObjectArray(env, i, ia_class, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (IS_NULL(ret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        addrp = (struct in_addr **) hp->h_addr_list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        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
   222
          jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
          if (IS_NULL(iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
          }
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 9035
diff changeset
   227
          setInetAddress_addr(env, iaObj, ntohl((*addrp)->s_addr));
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 9035
diff changeset
   228
          setInetAddress_hostName(env, iaObj, host);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
          (*env)->SetObjectArrayElement(env, ret, i, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
          addrp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
          i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
20857
734dc47ee277 8010371: getaddrinfo can fail with EAI_SYSTEM/EAGAIN, causes UnknownHostException to be thrown
bpb
parents: 16870
diff changeset
   233
    } else if (WSAGetLastError() == WSATRY_AGAIN) {
734dc47ee277 8010371: getaddrinfo can fail with EAI_SYSTEM/EAGAIN, causes UnknownHostException to be thrown
bpb
parents: 16870
diff changeset
   234
        NET_ThrowByNameWithLastError(env,
734dc47ee277 8010371: getaddrinfo can fail with EAI_SYSTEM/EAGAIN, causes UnknownHostException to be thrown
bpb
parents: 16870
diff changeset
   235
                                     JNU_JAVANETPKG "UnknownHostException",
734dc47ee277 8010371: getaddrinfo can fail with EAI_SYSTEM/EAGAIN, causes UnknownHostException to be thrown
bpb
parents: 16870
diff changeset
   236
                                     hostname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
cleanupAndReturn:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    JNU_ReleaseStringPlatformChars(env, host, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 * Method:    getHostByAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 * Signature: (I)Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
Java_java_net_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                            jbyteArray addrArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    struct hostent *hp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    jbyte caddr[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    jint addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    addr = ((caddr[0]<<24) & 0xff000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    addr |= ((caddr[1] <<16) & 0xff0000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    addr |= ((caddr[2] <<8) & 0xff00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    addr |= (caddr[3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    addr = htonl(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    if (hp == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    if (hp->h_name == NULL) { /* Deal with bug in Windows XP */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    return JNU_NewStringPlatform(env, hp->h_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 * ping implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 * Send a ICMP_ECHO_REQUEST packet every second until either the timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 * expires or a answer is received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 * Returns true is an ECHO_REPLY is received, otherwise, false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
static jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
ping4(JNIEnv *env, jint fd, struct sockaddr_in* him, jint timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
      struct sockaddr_in* netif, jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    jint size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    jint n, len, hlen1, icmplen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    char sendbuf[1500];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    char recvbuf[1500];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    struct icmp *icmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    struct ip *ip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    WSAEVENT hEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    struct sockaddr sa_recv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    jint tmout2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    u_short pid, seq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    int read_rv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /* Initialize the sequence number to a suitable random number and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
       shift right one place to allow sufficient room for increamenting. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    seq = ((unsigned short)rand()) >> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /* 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
   303
    pid = (u_short) _getpid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    size = 60*1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char *) &size, sizeof(size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * A TTL was specified, let's set the socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    if (ttl > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
      setsockopt(fd, IPPROTO_IP, IP_TTL, (const char *) &ttl, sizeof(ttl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * A network interface was specified, let's bind to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    if (netif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
      if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Let's make the socket non blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    hEvent = WSACreateEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * send 1 ICMP REQUEST every second until either we get a valid reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * or the timeout expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
      /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
       * construct the ICMP header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
      memset(sendbuf, 0, 1500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
      icmp = (struct icmp *) sendbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
      icmp->icmp_type = ICMP_ECHO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
      icmp->icmp_code = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
      icmp->icmp_id = htons(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
      icmp->icmp_seq = htons(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
      /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
       * checksum has to be set to zero before we can calculate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
       * real checksum!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
      icmp->icmp_cksum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
      icmp->icmp_cksum = in_cksum((u_short *)icmp, 64);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
      /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
       * Ping!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
      n = sendto(fd, sendbuf, 64, 0, (struct sockaddr *)him,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                 sizeof(struct sockaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
      if (n < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        NET_ThrowNew(env, WSAGetLastError(), "Can't send ICMP packet");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
       * wait for 1 second at most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
      tmout2 = timeout > 1000 ? 1000 : timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
      do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (tmout2 >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
          len = sizeof(sa_recv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
          n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0, &sa_recv, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
          ip = (struct ip*) recvbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
          hlen1 = (ip->ip_hl) << 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
          icmp = (struct icmp *) (recvbuf + hlen1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
          icmplen = n - hlen1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
          /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
           * Is that a proper ICMP reply?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
          if (icmplen >= 8 && icmp->icmp_type == ICMP_ECHOREPLY &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
              (ntohs(icmp->icmp_seq) == seq) && (ntohs(icmp->icmp_id) == pid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
      } while (tmout2 > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
      timeout -= 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
      seq++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    } while (timeout > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
 * Method:    isReachable0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
 * Signature: ([bI[bI)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
Java_java_net_Inet4AddressImpl_isReachable0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                                           jbyteArray addrArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                                           jint timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                                           jbyteArray ifArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                                           jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    jint addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    jbyte caddr[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    struct sockaddr_in him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    struct sockaddr_in* netif = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    struct sockaddr_in inf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    WSAEVENT hEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    int connect_rv = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    int sz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Convert IP address from byte array to integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    sz = (*env)->GetArrayLength(env, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    if (sz != 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
      return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    memset((char *) &him, 0, sizeof(him));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    memset((char *) caddr, 0, sizeof(caddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    addr = ((caddr[0]<<24) & 0xff000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    addr |= ((caddr[1] <<16) & 0xff0000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    addr |= ((caddr[2] <<8) & 0xff00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    addr |= (caddr[3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    addr = htonl(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * Socket address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    him.sin_addr.s_addr = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    him.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    len = sizeof(him);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * If a network interface was specified, let's convert its address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    if (!(IS_NULL(ifArray))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
      memset((char *) caddr, 0, sizeof(caddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
      (*env)->GetByteArrayRegion(env, ifArray, 0, 4, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
      addr = ((caddr[0]<<24) & 0xff000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
      addr |= ((caddr[1] <<16) & 0xff0000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
      addr |= ((caddr[2] <<8) & 0xff00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
      addr |= (caddr[3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
      addr = htonl(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
      inf.sin_addr.s_addr = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
      inf.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
      inf.sin_port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
      netif = &inf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
#if 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * Windows implementation of ICMP & RAW sockets is too unreliable for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * Therefore it's best not to try it at all and rely only on TCP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * We may revisit and enable this code in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * Let's try to create a RAW socket to send ICMP packets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * This usually requires "root" privileges, so it's likely to fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    fd = NET_Socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    if (fd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
       * It didn't fail, so we can use ICMP_ECHO requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        return ping4(env, fd, &him, timeout, netif, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Can't create a raw socket, so let's try a TCP socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    fd = NET_Socket(AF_INET, SOCK_STREAM, 0);
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   480
    if (fd == SOCKET_ERROR) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        /* note: if you run out of fds, you may not be able to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
         * the exception class, and get a NoClassDefFoundError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
         * instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        NET_ThrowNew(env, WSAGetLastError(), "Can't create socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    if (ttl > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
      setsockopt(fd, IPPROTO_IP, IP_TTL, (const char *)&ttl, sizeof(ttl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * A network interface was specified, so let's bind to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    if (netif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
      if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * Make the socket non blocking so we can use select/poll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    hEvent = WSACreateEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    /* no need to use NET_Connect as non-blocking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    him.sin_port = htons(7);    /* Echo */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    connect_rv = connect(fd, (struct sockaddr *)&him, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * connection established or refused immediately, either way it means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * we were able to reach the host!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    if (connect_rv == 0 || WSAGetLastError() == WSAECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        int optlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        switch (WSAGetLastError()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        case WSAEHOSTUNREACH:   /* Host Unreachable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        case WSAENETUNREACH:    /* Network Unreachable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        case WSAENETDOWN:       /* Network is down */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        case WSAEPFNOSUPPORT:   /* Protocol Family unsupported */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
          WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
          closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
          return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        if (WSAGetLastError() != WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                                         "connect failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        /* has connection been established */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (timeout >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
          optlen = sizeof(connect_rv);
7814
dff0b40f9ac8 7010192: InetAddress.isReachable hits ShouldNotReachHere with hs20-b04 (win)
alanb
parents: 5506
diff changeset
   547
          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
   548
                         &optlen) <0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            connect_rv = WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
          if (connect_rv == 0 || connect_rv == WSAECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    WSACloseEvent(hEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    closesocket(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
}