src/java.base/unix/native/libnet/Inet4AddressImpl.c
author clanger
Mon, 16 Apr 2018 12:50:10 +0530
changeset 49817 a838e3707f3a
parent 47216 71c04702a3d5
child 49985 44e581f54d08
permissions -rw-r--r--
8201369: Inet4AddressImpl_getLocalHostName reverse lookup on Solaris only Reviewed-by: clanger, chegar Contributed-by: sshamaia@in.ibm.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49817
a838e3707f3a 8201369: Inet4AddressImpl_getLocalHostName reverse lookup on Solaris only
clanger
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4814
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4814
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4814
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4814
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4814
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    25
#include <ctype.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <sys/types.h>
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    28
#include <netinet/in.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <netinet/in_systm.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <netinet/ip.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <netinet/ip_icmp.h>
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    32
#include <stdlib.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <string.h>
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
    34
#include <sys/time.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include "java_net_Inet4AddressImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
42763
31a6badb98e8 8171075: Inet4AddressImpl: Remove duplicate and (no longer used ?) native coding for BSD
clanger
parents: 41771
diff changeset
    40
#if defined(MACOSX)
20863
2cdfa2825d21 8026245: InetAddress.getLocalHost crash if IPv6 disabled (macosx)
robm
parents: 20857
diff changeset
    41
extern jobjectArray lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6);
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
    42
#endif
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
    43
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/* the initial size of our hostent buffers */
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
    45
#ifndef NI_MAXHOST
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
    46
#define NI_MAXHOST 1025
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
    47
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
    49
#define SET_NONBLOCKING(fd) {       \
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
    50
    int flags = fcntl(fd, F_GETFL); \
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
    51
    flags |= O_NONBLOCK;            \
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
    52
    fcntl(fd, F_SETFL, flags);      \
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
    53
}
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
    54
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
    55
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Method:    getLocalHostName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Signature: ()Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
    66
    char hostname[NI_MAXHOST + 1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    hostname[0] = '\0';
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
    69
    if (gethostname(hostname, NI_MAXHOST) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        strcpy(hostname, "localhost");
49817
a838e3707f3a 8201369: Inet4AddressImpl_getLocalHostName reverse lookup on Solaris only
clanger
parents: 47216
diff changeset
    71
#if defined(__solaris__)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    } else {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
    73
        // try to resolve hostname via nameservice
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
    74
        // if it is known but getnameinfo fails, hostname will still be the
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
    75
        // value from gethostname
11033
ef047e386299 7114558: Inet4AddressImpl should use memset (rather than bzero) and NI_MAXHOST (rather than MAXHOSTNAMELEN)
ngmr
parents: 11032
diff changeset
    76
        struct addrinfo hints, *res;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
    78
        // make sure string is null-terminated
11033
ef047e386299 7114558: Inet4AddressImpl should use memset (rather than bzero) and NI_MAXHOST (rather than MAXHOSTNAMELEN)
ngmr
parents: 11032
diff changeset
    79
        hostname[NI_MAXHOST] = '\0';
ef047e386299 7114558: Inet4AddressImpl should use memset (rather than bzero) and NI_MAXHOST (rather than MAXHOSTNAMELEN)
ngmr
parents: 11032
diff changeset
    80
        memset(&hints, 0, sizeof(hints));
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
    81
        hints.ai_flags = AI_CANONNAME;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
    82
        hints.ai_family = AF_INET;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
    83
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
    84
        if (getaddrinfo(hostname, NULL, &hints, &res) == 0) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
    85
            getnameinfo(res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
    86
                        NULL, 0, NI_NAMEREQD);
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
    87
            freeaddrinfo(res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
49817
a838e3707f3a 8201369: Inet4AddressImpl_getLocalHostName reverse lookup on Solaris only
clanger
parents: 47216
diff changeset
    90
#else
a838e3707f3a 8201369: Inet4AddressImpl_getLocalHostName reverse lookup on Solaris only
clanger
parents: 47216
diff changeset
    91
    } else {
a838e3707f3a 8201369: Inet4AddressImpl_getLocalHostName reverse lookup on Solaris only
clanger
parents: 47216
diff changeset
    92
        // make sure string is null-terminated
a838e3707f3a 8201369: Inet4AddressImpl_getLocalHostName reverse lookup on Solaris only
clanger
parents: 47216
diff changeset
    93
        hostname[NI_MAXHOST] = '\0';
a838e3707f3a 8201369: Inet4AddressImpl_getLocalHostName reverse lookup on Solaris only
clanger
parents: 47216
diff changeset
    94
    }
a838e3707f3a 8201369: Inet4AddressImpl_getLocalHostName reverse lookup on Solaris only
clanger
parents: 47216
diff changeset
    95
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    return (*env)->NewStringUTF(env, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
/*
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   100
 * Find an internet address for a given hostname. Note that this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * code only works for addresses of type INET. The translation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * of %d.%d.%d.%d to an address (int) occurs in java now, so the
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   103
 * String "host" shouldn't be a %d.%d.%d.%d string. The only
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   104
 * exception should be when any of the %d are out of range and
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   105
 * we fallback to a lookup.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * Method:    lookupAllHostAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * Signature: (Ljava/lang/String;)[[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
JNIEXPORT jobjectArray JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   113
                                                 jstring host) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   114
    jobjectArray ret = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    const char *hostname;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   116
    int error = 0;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   117
    struct addrinfo hints, *res = NULL, *resNew = NULL, *last = NULL,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   118
        *iterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   120
    initInetAddressIDs(env);
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   121
    JNU_CHECK_EXCEPTION_RETURN(env, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    if (IS_NULL(host)) {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   124
        JNU_ThrowNullPointerException(env, "host argument is null");
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   125
        return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    CHECK_NULL_RETURN(hostname, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   130
    // try once, with our static buffer
11033
ef047e386299 7114558: Inet4AddressImpl should use memset (rather than bzero) and NI_MAXHOST (rather than MAXHOSTNAMELEN)
ngmr
parents: 11032
diff changeset
   131
    memset(&hints, 0, sizeof(hints));
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   132
    hints.ai_flags = AI_CANONNAME;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   133
    hints.ai_family = AF_INET;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   134
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   135
    error = getaddrinfo(hostname, NULL, &hints, &res);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   136
42763
31a6badb98e8 8171075: Inet4AddressImpl: Remove duplicate and (no longer used ?) native coding for BSD
clanger
parents: 41771
diff changeset
   137
    if (error) {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   138
#if defined(MACOSX)
42763
31a6badb98e8 8171075: Inet4AddressImpl: Remove duplicate and (no longer used ?) native coding for BSD
clanger
parents: 41771
diff changeset
   139
        // If getaddrinfo fails try getifaddrs, see bug 8170910.
31a6badb98e8 8171075: Inet4AddressImpl: Remove duplicate and (no longer used ?) native coding for BSD
clanger
parents: 41771
diff changeset
   140
        ret = lookupIfLocalhost(env, hostname, JNI_FALSE);
31a6badb98e8 8171075: Inet4AddressImpl: Remove duplicate and (no longer used ?) native coding for BSD
clanger
parents: 41771
diff changeset
   141
        if (ret != NULL || (*env)->ExceptionCheck(env)) {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   142
            goto cleanupAndReturn;
42763
31a6badb98e8 8171075: Inet4AddressImpl: Remove duplicate and (no longer used ?) native coding for BSD
clanger
parents: 41771
diff changeset
   143
        }
31a6badb98e8 8171075: Inet4AddressImpl: Remove duplicate and (no longer used ?) native coding for BSD
clanger
parents: 41771
diff changeset
   144
#endif
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   145
        // report error
23033
0cc7c83fde3c 8035949: Remove unused macro USE_SELECT and clean up Unix version of net_util_md.{c,h}
simonis
parents: 23015
diff changeset
   146
        NET_ThrowUnknownHostExceptionWithGaiError(env, hostname, error);
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   147
        goto cleanupAndReturn;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   148
    } else {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   149
        int i = 0;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   150
        iterator = res;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   151
        while (iterator != NULL) {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   152
            // skip duplicates
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   153
            int skip = 0;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   154
            struct addrinfo *iteratorNew = resNew;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   155
            while (iteratorNew != NULL) {
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   156
                struct sockaddr_in *addr1, *addr2;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   157
                addr1 = (struct sockaddr_in *)iterator->ai_addr;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   158
                addr2 = (struct sockaddr_in *)iteratorNew->ai_addr;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   159
                if (addr1->sin_addr.s_addr == addr2->sin_addr.s_addr) {
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   160
                    skip = 1;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   161
                    break;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   162
                }
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   163
                iteratorNew = iteratorNew->ai_next;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   164
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   166
            if (!skip) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   167
                struct addrinfo *next
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41380
diff changeset
   168
                    = (struct addrinfo *)malloc(sizeof(struct addrinfo));
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   169
                if (!next) {
13245
7ab3ef5b9520 7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents: 12688
diff changeset
   170
                    JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed");
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   171
                    ret = NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   172
                    goto cleanupAndReturn;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   173
                }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   174
                memcpy(next, iterator, sizeof(struct addrinfo));
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   175
                next->ai_next = NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   176
                if (resNew == NULL) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   177
                    resNew = next;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   178
                } else {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   179
                    last->ai_next = next;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   180
                }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   181
                last = next;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   182
                i++;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   183
            }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   184
            iterator = iterator->ai_next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   187
        // allocate array - at this point i contains the number of addresses
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   188
        ret = (*env)->NewObjectArray(env, i, ia_class, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (IS_NULL(ret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   192
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        i = 0;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   194
        iterator = resNew;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   195
        while (iterator != NULL) {
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   196
            jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   197
            if (IS_NULL(iaObj)) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   198
                ret = NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   199
                goto cleanupAndReturn;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   200
            }
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   201
            setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in *)
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   202
                                (iterator->ai_addr))->sin_addr.s_addr));
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   203
            setInetAddress_hostName(env, iaObj, host);
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   204
            (*env)->SetObjectArrayElement(env, ret, i++, iaObj);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   205
            iterator = iterator->ai_next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
42763
31a6badb98e8 8171075: Inet4AddressImpl: Remove duplicate and (no longer used ?) native coding for BSD
clanger
parents: 41771
diff changeset
   208
cleanupAndReturn:
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   209
    JNU_ReleaseStringPlatformChars(env, host, hostname);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   210
    while (resNew != NULL) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   211
        last = resNew;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   212
        resNew = resNew->ai_next;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   213
        free(last);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   215
    if (res != NULL) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   216
        freeaddrinfo(res);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   217
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * Method:    getHostByAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 * Signature: (I)Ljava/lang/String;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   225
 *
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   226
 * Theoretically the UnknownHostException could be enriched with gai error
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   227
 * information. But as it is silently ignored anyway, there's no need for this.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   228
 * It's only important that either a valid hostname is returned or an
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   229
 * UnknownHostException is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
Java_java_net_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   233
                                             jbyteArray addrArray) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    jstring ret = NULL;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   235
    char host[NI_MAXHOST + 1];
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   236
    jbyte caddr[4];
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   237
    jint addr;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   238
    struct sockaddr_in sa;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   239
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   240
    // construct a sockaddr_in structure
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   241
    memset((char *)&sa, 0, sizeof(struct sockaddr_in));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   243
    addr = ((caddr[0] << 24) & 0xff000000);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   244
    addr |= ((caddr[1] << 16) & 0xff0000);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   245
    addr |= ((caddr[2] << 8) & 0xff00);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    addr |= (caddr[3] & 0xff);
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   247
    sa.sin_addr.s_addr = htonl(addr);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   248
    sa.sin_family = AF_INET;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   249
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   250
    if (getnameinfo((struct sockaddr *)&sa, sizeof(struct sockaddr_in),
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   251
                    host, NI_MAXHOST, NULL, 0, NI_NAMEREQD)) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   252
        JNU_ThrowByName(env, "java/net/UnknownHostException", NULL);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   253
    } else {
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   254
        ret = (*env)->NewStringUTF(env, host);
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   255
        if (ret == NULL) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   256
            JNU_ThrowByName(env, "java/net/UnknownHostException", NULL);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   257
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   259
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   263
/**
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   264
 * ping implementation using tcp port 7 (echo)
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   265
 */
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   266
static jboolean
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   267
tcp_ping4(JNIEnv *env, SOCKETADDRESS *sa, SOCKETADDRESS *netif, jint timeout,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   268
          jint ttl)
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   269
{
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   270
    jint fd;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   271
    int connect_rv = -1;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   272
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   273
    // open a TCP socket
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   274
    fd = socket(AF_INET, SOCK_STREAM, 0);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   275
    if (fd == -1) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   276
        // note: if you run out of fds, you may not be able to load
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   277
        // the exception class, and get a NoClassDefFoundError instead.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   278
        NET_ThrowNew(env, errno, "Can't create socket");
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   279
        return JNI_FALSE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   280
    }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   281
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   282
    // set TTL
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   283
    if (ttl > 0) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   284
        setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   285
    }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   286
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   287
    // A network interface was specified, so let's bind to it.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   288
    if (netif != NULL) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   289
        if (bind(fd, &netif->sa, sizeof(struct sockaddr_in)) < 0) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   290
            NET_ThrowNew(env, errno, "Can't bind socket");
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   291
            close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   292
            return JNI_FALSE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   293
        }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   294
    }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   295
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   296
    // Make the socket non blocking so we can use select/poll.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   297
    SET_NONBLOCKING(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   298
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   299
    sa->sa4.sin_port = htons(7); // echo port
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   300
    connect_rv = NET_Connect(fd, &sa->sa, sizeof(struct sockaddr_in));
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   301
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   302
    // connection established or refused immediately, either way it means
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   303
    // we were able to reach the host!
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   304
    if (connect_rv == 0 || errno == ECONNREFUSED) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   305
        close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   306
        return JNI_TRUE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   307
    }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   308
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   309
    switch (errno) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   310
    case ENETUNREACH:   // Network Unreachable
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   311
    case EAFNOSUPPORT:  // Address Family not supported
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   312
    case EADDRNOTAVAIL: // address is not available on the remote machine
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   313
#if defined(__linux__) || defined(_AIX)
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   314
        // On some Linux versions, when a socket is bound to the loopback
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   315
        // interface, connect will fail and errno will be set to EINVAL
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   316
        // or EHOSTUNREACH.  When that happens, don't throw an exception,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   317
        // just return false.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   318
    case EINVAL:
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   319
    case EHOSTUNREACH:  // No route to host
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   320
#endif
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   321
        close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   322
        return JNI_FALSE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   323
    case EINPROGRESS:   // this is expected as we'll probably have to wait
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   324
        break;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   325
    default:
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   326
        NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   327
                                     "connect failed");
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   328
        close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   329
        return JNI_FALSE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   330
    }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   331
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   332
    timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   333
    if (timeout >= 0) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   334
        // connection has been established, check for error condition
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   335
        socklen_t optlen = (socklen_t)sizeof(connect_rv);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   336
        if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   337
                       &optlen) <0)
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   338
        {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   339
            connect_rv = errno;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   340
        }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   341
        if (connect_rv == 0 || connect_rv == ECONNREFUSED) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   342
            close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   343
            return JNI_TRUE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   344
        }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   345
    }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   346
    close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   347
    return JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
 * ping implementation.
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   352
 * Send an ICMP_ECHO_REQUEST packet every second until either the timeout
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   353
 * expires or an answer is received.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   354
 * Returns true if an ECHO_REPLY is received, false otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
static jboolean
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   357
ping4(JNIEnv *env, jint fd, SOCKETADDRESS *sa, SOCKETADDRESS *netif,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   358
      jint timeout, jint ttl)
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   359
{
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   360
    jint n, size = 60 * 1024, hlen, tmout2, seq = 1;
2056
115e09b7a004 6799040: Portability issues in src/solaris/native/java/net/Inet4AddressImpl.c
chegar
parents: 910
diff changeset
   361
    socklen_t len;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   362
    unsigned char sendbuf[1500], recvbuf[1500];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    struct icmp *icmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    struct ip *ip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    struct sockaddr_in sa_recv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    jchar pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    struct timeval tv;
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   368
    size_t plen = ICMP_ADVLENMIN + sizeof(tv);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   371
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   372
    // sets the ttl (max number of hops)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    if (ttl > 0) {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   374
        setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   376
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   377
    // a specific interface was specified, so let's bind the socket
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   378
    // to that interface to ensure the requests are sent only through it.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    if (netif != NULL) {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   380
        if (bind(fd, &netif->sa, sizeof(struct sockaddr_in)) < 0) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   381
            NET_ThrowNew(env, errno, "Can't bind socket");
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   382
            close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   383
            return JNI_FALSE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   384
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   386
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   387
    // icmp_id is a 16 bit data type, therefore down cast the pid
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   388
    pid = (jchar)getpid();
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   389
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   390
    // Make the socket non blocking so we can use select
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    SET_NONBLOCKING(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    do {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   393
        // create the ICMP request
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   394
        icmp = (struct icmp *)sendbuf;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   395
        icmp->icmp_type = ICMP_ECHO;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   396
        icmp->icmp_code = 0;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   397
        // let's tag the ECHO packet with our pid so we can identify it
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   398
        icmp->icmp_id = htons(pid);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   399
        icmp->icmp_seq = htons(seq);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   400
        seq++;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   401
        gettimeofday(&tv, NULL);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   402
        memcpy(icmp->icmp_data, &tv, sizeof(tv));
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   403
        icmp->icmp_cksum = 0;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   404
        // manually calculate checksum
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   405
        icmp->icmp_cksum = in_cksum((u_short *)icmp, plen);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   406
        // send it
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   407
        n = sendto(fd, sendbuf, plen, 0, &sa->sa, sizeof(struct sockaddr_in));
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   408
        if (n < 0 && errno != EINPROGRESS) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   409
#if defined(__linux__)
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   410
            /*
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   411
             * On some Linux versions, when a socket is bound to the loopback
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   412
             * interface, sendto will fail and errno will be set to
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   413
             * EINVAL or EHOSTUNREACH. When that happens, don't throw an
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   414
             * exception, just return false.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   415
             */
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   416
            if (errno != EINVAL && errno != EHOSTUNREACH) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   417
                NET_ThrowNew(env, errno, "Can't send ICMP packet");
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   418
            }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   419
#else
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   420
            NET_ThrowNew(env, errno, "Can't send ICMP packet");
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   421
#endif
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   422
            close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   423
            return JNI_FALSE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   424
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   426
        tmout2 = timeout > 1000 ? 1000 : timeout;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   427
        do {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   428
            tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   429
            if (tmout2 >= 0) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   430
                len = sizeof(sa_recv);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   431
                n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   432
                             (struct sockaddr *)&sa_recv, &len);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   433
                // check if we received enough data
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   434
                if (n < (jint)sizeof(struct ip)) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   435
                    continue;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   436
                }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   437
                ip = (struct ip *)recvbuf;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   438
                hlen = ((jint)(unsigned int)(ip->ip_hl)) << 2;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   439
                // check if we received enough data
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   440
                if (n < (jint)(hlen + sizeof(struct icmp))) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   441
                    continue;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   442
                }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   443
                icmp = (struct icmp *)(recvbuf + hlen);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   444
                // We did receive something, but is it what we were expecting?
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   445
                // I.E.: An ICMP_ECHO_REPLY packet with the proper PID and
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   446
                //       from the host that we are trying to determine is reachable.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   447
                if (icmp->icmp_type == ICMP_ECHOREPLY &&
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   448
                    (ntohs(icmp->icmp_id) == pid))
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   449
                {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   450
                    if (sa->sa4.sin_addr.s_addr == sa_recv.sin_addr.s_addr) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   451
                        close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   452
                        return JNI_TRUE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   453
                    } else if (sa->sa4.sin_addr.s_addr == 0) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   454
                        close(fd);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   455
                        return JNI_TRUE;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   456
                    }
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   457
                }
12688
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12047
diff changeset
   458
            }
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   459
        } while (tmout2 > 0);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   460
        timeout -= 1000;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   461
    } while (timeout > 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
 * Method:    isReachable0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
 * Signature: ([bI[bI)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
Java_java_net_Inet4AddressImpl_isReachable0(JNIEnv *env, jobject this,
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   473
                                            jbyteArray addrArray, jint timeout,
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   474
                                            jbyteArray ifArray, jint ttl)
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   475
{
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    jbyte caddr[4];
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   477
    jint addr = 0, sz, fd;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   478
    SOCKETADDRESS sa, inf, *netif = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   480
    // check if address array size is 4 (IPv4 address)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    sz = (*env)->GetArrayLength(env, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    if (sz != 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
      return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   485
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   486
    // convert IP address from byte array to integer
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   487
    memset((char *)caddr, 0, sizeof(caddr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   489
    addr = ((caddr[0] << 24) & 0xff000000);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   490
    addr |= ((caddr[1] << 16) & 0xff0000);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   491
    addr |= ((caddr[2] << 8) & 0xff00);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    addr |= (caddr[3] & 0xff);
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   493
    memset((char *)&sa, 0, sizeof(SOCKETADDRESS));
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   494
    sa.sa4.sin_addr.s_addr = htonl(addr);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   495
    sa.sa4.sin_family = AF_INET;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   497
    // If a network interface was specified, let's convert its address as well.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   498
    if (!(IS_NULL(ifArray))) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   499
        memset((char *)caddr, 0, sizeof(caddr));
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   500
        (*env)->GetByteArrayRegion(env, ifArray, 0, 4, caddr);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   501
        addr = ((caddr[0] << 24) & 0xff000000);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   502
        addr |= ((caddr[1] << 16) & 0xff0000);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   503
        addr |= ((caddr[2] << 8) & 0xff00);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   504
        addr |= (caddr[3] & 0xff);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   505
        memset((char *)&inf, 0, sizeof(SOCKETADDRESS));
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   506
        inf.sa4.sin_addr.s_addr = htonl(addr);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   507
        inf.sa4.sin_family = AF_INET;
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   508
        netif = &inf;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   511
    // Let's try to create a RAW socket to send ICMP packets.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   512
    // This usually requires "root" privileges, so it's likely to fail.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   513
    fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   514
    if (fd == -1) {
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   515
        return tcp_ping4(env, &sa, netif, timeout, ttl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    } else {
43614
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   517
        // It didn't fail, so we can use ICMP_ECHO requests.
d8e1fecbb8f2 8167420: Fixes for InetAddressImpl native coding on Linux/Unix platforms
clanger
parents: 42763
diff changeset
   518
        return ping4(env, fd, &sa, netif, timeout, ttl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
}