jdk/src/java.base/unix/native/libnet/Inet4AddressImpl.c
author clanger
Mon, 10 Oct 2016 22:42:45 +0200
changeset 41380 c27cf95dd7e6
parent 31059 0ec777e00e2e
child 41771 18c9669e76ca
permissions -rw-r--r--
8167295: Further cleanup to the native parts of libnet/libnio Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 31059
diff changeset
     2
 * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <errno.h>
3226
b142b3d06bf5 6562614: Compiler warnings for gettimeofday in Inet4/Inet6AddressImpl.c
andrew
parents: 2056
diff changeset
    27
#include <sys/time.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <sys/socket.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <netinet/in_systm.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <netinet/in.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <netinet/ip.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <netinet/ip_icmp.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <netdb.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    39
#ifdef _ALLBSD_SOURCE
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    40
#include <unistd.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    41
#include <sys/param.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    42
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    43
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#include "java_net_Inet4AddressImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    50
#if defined(__GLIBC__) || (defined(__FreeBSD__) && (__FreeBSD_version >= 601104))
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    51
#define HAS_GLIBC_GETHOSTBY_R   1
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    52
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    53
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
    54
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    55
#if defined(_ALLBSD_SOURCE) && !defined(HAS_GLIBC_GETHOSTBY_R)
20863
2cdfa2825d21 8026245: InetAddress.getLocalHost crash if IPv6 disabled (macosx)
robm
parents: 20857
diff changeset
    56
extern jobjectArray lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6);
2cdfa2825d21 8026245: InetAddress.getLocalHost crash if IPv6 disabled (macosx)
robm
parents: 20857
diff changeset
    57
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    58
/* Use getaddrinfo(3), which is thread safe */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    59
/************************************************************************
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    60
 * Inet4AddressImpl
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    61
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    62
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    63
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    64
 * Class:     java_net_Inet4AddressImpl
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    65
 * Method:    getLocalHostName
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    66
 * Signature: ()Ljava/lang/String;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    67
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    68
JNIEXPORT jstring JNICALL
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    69
Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    70
    char hostname[NI_MAXHOST+1];
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    71
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    72
    hostname[0] = '\0';
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
    73
    if (gethostname(hostname, NI_MAXHOST)) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    74
        /* Something went wrong, maybe networking is not setup? */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    75
        strcpy(hostname, "localhost");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    76
    } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    77
         struct addrinfo  hints, *res;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    78
         int error;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    79
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    80
         memset(&hints, 0, sizeof(hints));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    81
         hints.ai_flags = AI_CANONNAME;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    82
         hints.ai_family = AF_UNSPEC;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    83
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    84
         error = getaddrinfo(hostname, NULL, &hints, &res);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    85
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    86
         if (error == 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    87
             /* host is known to name service */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    88
             error = getnameinfo(res->ai_addr,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    89
                                 res->ai_addrlen,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    90
                                 hostname,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    91
                                 NI_MAXHOST,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    92
                                 NULL,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    93
                                 0,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    94
                                 NI_NAMEREQD);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    95
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    96
             /* if getnameinfo fails hostname is still the value
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    97
                from gethostname */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    98
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
    99
             freeaddrinfo(res);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   100
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   101
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   102
    return (*env)->NewStringUTF(env, hostname);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   103
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   104
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   105
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   106
 * Find an internet address for a given hostname.  Note that this
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   107
 * code only works for addresses of type INET. The translation
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   108
 * of %d.%d.%d.%d to an address (int) occurs in java now, so the
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   109
 * String "host" shouldn't *ever* be a %d.%d.%d.%d string
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   110
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   111
 * Class:     java_net_Inet4AddressImpl
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   112
 * Method:    lookupAllHostAddr
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   113
 * Signature: (Ljava/lang/String;)[[B
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   114
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   115
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   116
JNIEXPORT jobjectArray JNICALL
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   117
Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   118
                                                jstring host) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   119
    const char *hostname;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   120
    jobject name;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   121
    jobjectArray ret = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   122
    int retLen = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   123
31059
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   124
    int getaddrinfo_error=0;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   125
    struct addrinfo hints, *res, *resNew = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   126
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   127
    initInetAddressIDs(env);
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   128
    JNU_CHECK_EXCEPTION_RETURN(env, NULL);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   129
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   130
    if (IS_NULL(host)) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   131
        JNU_ThrowNullPointerException(env, "host is null");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   132
        return 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   133
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   134
    hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   135
    CHECK_NULL_RETURN(hostname, NULL);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   136
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   137
    memset(&hints, 0, sizeof(hints));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   138
    hints.ai_flags = AI_CANONNAME;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   139
    hints.ai_family = AF_INET;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   140
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   141
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   142
     * Workaround for Solaris bug 4160367 - if a hostname contains a
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   143
     * white space then 0.0.0.0 is returned
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   144
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   145
    if (isspace((unsigned char)hostname[0])) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   146
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   147
                        (char *)hostname);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   148
        JNU_ReleaseStringPlatformChars(env, host, hostname);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   149
        return NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   150
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   151
31059
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   152
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   153
    getaddrinfo_error = getaddrinfo(hostname, NULL, &hints, &res);
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   154
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   155
#ifdef MACOSX
31059
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   156
    if (getaddrinfo_error) {
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   157
        // If getaddrinfo fails try getifaddrs.
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   158
        ret = lookupIfLocalhost(env, hostname, JNI_FALSE);
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   159
        if (ret != NULL || (*env)->ExceptionCheck(env)) {
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   160
            JNU_ReleaseStringPlatformChars(env, host, hostname);
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   161
            return ret;
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   162
        }
20741
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   163
    }
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   164
#endif
6eb501508669 7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents: 16870
diff changeset
   165
31059
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   166
    if (getaddrinfo_error) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   167
        /* report error */
31059
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   168
        NET_ThrowUnknownHostExceptionWithGaiError(
0ec777e00e2e 8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents: 25859
diff changeset
   169
            env, hostname, getaddrinfo_error);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   170
        JNU_ReleaseStringPlatformChars(env, host, hostname);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   171
        return NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   172
    } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   173
        int i = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   174
        struct addrinfo *itr, *last = NULL, *iterator = res;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   175
        while (iterator != NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   176
            int skip = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   177
            itr = resNew;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   178
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   179
            while (itr != NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   180
                struct sockaddr_in *addr1, *addr2;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   181
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   182
                addr1 = (struct sockaddr_in *)iterator->ai_addr;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   183
                addr2 = (struct sockaddr_in *)itr->ai_addr;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   184
                if (addr1->sin_addr.s_addr ==
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   185
                    addr2->sin_addr.s_addr) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   186
                    skip = 1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   187
                    break;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   188
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   189
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   190
                itr = itr->ai_next;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   191
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   192
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   193
            if (!skip) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   194
                struct addrinfo *next
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   195
                    = (struct addrinfo*) malloc(sizeof(struct addrinfo));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   196
                if (!next) {
13245
7ab3ef5b9520 7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents: 12688
diff changeset
   197
                    JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed");
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   198
                    ret = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   199
                    goto cleanupAndReturn;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   200
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   201
                memcpy(next, iterator, sizeof(struct addrinfo));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   202
                next->ai_next = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   203
                if (resNew == NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   204
                    resNew = next;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   205
                } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   206
                    last->ai_next = next;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   207
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   208
                last = next;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   209
                i++;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   210
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   211
            iterator = iterator->ai_next;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   212
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   213
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   214
        retLen = i;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   215
        iterator = resNew;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   216
        i = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   217
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   218
        name = (*env)->NewStringUTF(env, hostname);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   219
        if (IS_NULL(name)) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   220
          goto cleanupAndReturn;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   221
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   222
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   223
        ret = (*env)->NewObjectArray(env, retLen, ia_class, NULL);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   224
        if (IS_NULL(ret)) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   225
            /* we may have memory to free at the end of this */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   226
            goto cleanupAndReturn;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   227
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   228
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   229
        while (iterator != NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   230
            /* We need 4 bytes to store ipv4 address; */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   231
            int len = 4;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   232
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   233
            jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   234
            if (IS_NULL(iaObj)) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   235
                /* we may have memory to free at the end of this */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   236
                ret = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   237
                goto cleanupAndReturn;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   238
            }
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   239
            setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in*)(iterator->ai_addr))->sin_addr.s_addr));
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   240
            setInetAddress_hostName(env, iaObj, name);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   241
            (*env)->SetObjectArrayElement(env, ret, retLen - i -1, iaObj);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   242
            i++;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   243
            iterator = iterator->ai_next;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   244
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   245
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   246
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   247
cleanupAndReturn:
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   248
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   249
        struct addrinfo *iterator, *tmp;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   250
        iterator = resNew;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   251
        while (iterator != NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   252
            tmp = iterator;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   253
            iterator = iterator->ai_next;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   254
            free(tmp);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   255
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   256
        JNU_ReleaseStringPlatformChars(env, host, hostname);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   257
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   258
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   259
    freeaddrinfo(res);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   260
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   261
    return ret;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   262
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   263
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   264
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   265
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   266
 * Class:     java_net_Inet4AddressImpl
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   267
 * Method:    getHostByAddr
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   268
 * Signature: (I)Ljava/lang/String;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   269
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   270
JNIEXPORT jstring JNICALL
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   271
Java_java_net_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   272
                                            jbyteArray addrArray) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   273
    jstring ret = NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   274
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   275
    char host[NI_MAXHOST+1];
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   276
    jfieldID fid;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   277
    int error = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   278
    jint family;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   279
    struct sockaddr *him ;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   280
    int len = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   281
    jbyte caddr[4];
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   282
    jint addr;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   283
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   284
    struct sockaddr_in him4;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   285
    struct sockaddr *sa;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   286
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   287
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   288
         * For IPv4 addresses construct a sockaddr_in structure.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   289
         */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   290
    (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   291
    addr = ((caddr[0]<<24) & 0xff000000);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   292
    addr |= ((caddr[1] <<16) & 0xff0000);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   293
    addr |= ((caddr[2] <<8) & 0xff00);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   294
    addr |= (caddr[3] & 0xff);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   295
    memset((char *) &him4, 0, sizeof(him4));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   296
    him4.sin_addr.s_addr = (uint32_t) htonl(addr);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   297
    him4.sin_family = AF_INET;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   298
    sa = (struct sockaddr *) &him4;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   299
    len = sizeof(him4);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   300
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   301
    error = getnameinfo(sa, len, host, NI_MAXHOST, NULL, 0,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   302
                               NI_NAMEREQD);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   303
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   304
    if (!error) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   305
        ret = (*env)->NewStringUTF(env, host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   306
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   307
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   308
    if (ret == NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   309
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", NULL);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   310
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   311
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   312
    return ret;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   313
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   314
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   315
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   316
#else /* defined(_ALLBSD_SOURCE) && !defined(HAS_GLIBC_GETHOSTBY_R) */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   317
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
/* the initial size of our hostent buffers */
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   319
#ifndef NI_MAXHOST
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   320
#define NI_MAXHOST 1025
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   321
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 * Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
 * Method:    getLocalHostName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
 * Signature: ()Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   334
    char hostname[NI_MAXHOST+1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    hostname[0] = '\0';
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   337
    if (gethostname(hostname, NI_MAXHOST)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        /* Something went wrong, maybe networking is not setup? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        strcpy(hostname, "localhost");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    } else {
11033
ef047e386299 7114558: Inet4AddressImpl should use memset (rather than bzero) and NI_MAXHOST (rather than MAXHOSTNAMELEN)
ngmr
parents: 11032
diff changeset
   341
        struct addrinfo hints, *res;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   342
        int error;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
11033
ef047e386299 7114558: Inet4AddressImpl should use memset (rather than bzero) and NI_MAXHOST (rather than MAXHOSTNAMELEN)
ngmr
parents: 11032
diff changeset
   344
        hostname[NI_MAXHOST] = '\0';
ef047e386299 7114558: Inet4AddressImpl should use memset (rather than bzero) and NI_MAXHOST (rather than MAXHOSTNAMELEN)
ngmr
parents: 11032
diff changeset
   345
        memset(&hints, 0, sizeof(hints));
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   346
        hints.ai_flags = AI_CANONNAME;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   347
        hints.ai_family = AF_INET;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   348
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   349
        error = getaddrinfo(hostname, NULL, &hints, &res);
8774
4403ce77f306 7024560: InetAddress.getLocalHost().getHostName() returns localhost for hostnames of length HOST_NAME_MAX
chegar
parents: 8555
diff changeset
   350
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   351
        if (error == 0) {/* host is known to name service */
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   352
            getnameinfo(res->ai_addr,
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   353
                        res->ai_addrlen,
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   354
                        hostname,
11033
ef047e386299 7114558: Inet4AddressImpl should use memset (rather than bzero) and NI_MAXHOST (rather than MAXHOSTNAMELEN)
ngmr
parents: 11032
diff changeset
   355
                        NI_MAXHOST,
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   356
                        NULL,
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   357
                        0,
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   358
                        NI_NAMEREQD);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   359
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   360
            /* if getnameinfo fails hostname is still the value
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   361
               from gethostname */
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   362
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   363
            freeaddrinfo(res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    return (*env)->NewStringUTF(env, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
 * Find an internet address for a given hostname.  Note that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
 * code only works for addresses of type INET. The translation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
 * of %d.%d.%d.%d to an address (int) occurs in java now, so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
 * String "host" shouldn't *ever* be a %d.%d.%d.%d string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
 * Method:    lookupAllHostAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
 * Signature: (Ljava/lang/String;)[[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
JNIEXPORT jobjectArray JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                                                jstring host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    const char *hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    jobjectArray ret = 0;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   385
    int retLen = 0;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   386
    int error = 0;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   387
    struct addrinfo hints, *res, *resNew = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   389
    initInetAddressIDs(env);
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   390
    JNU_CHECK_EXCEPTION_RETURN(env, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    if (IS_NULL(host)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        JNU_ThrowNullPointerException(env, "host is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    CHECK_NULL_RETURN(hostname, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   399
    /* 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
   400
    memset(&hints, 0, sizeof(hints));
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   401
    hints.ai_flags = AI_CANONNAME;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   402
    hints.ai_family = AF_INET;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   403
2056
115e09b7a004 6799040: Portability issues in src/solaris/native/java/net/Inet4AddressImpl.c
chegar
parents: 910
diff changeset
   404
#ifdef __solaris__
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * Workaround for Solaris bug 4160367 - if a hostname contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * white space then 0.0.0.0 is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
2056
115e09b7a004 6799040: Portability issues in src/solaris/native/java/net/Inet4AddressImpl.c
chegar
parents: 910
diff changeset
   409
    if (isspace((unsigned char)hostname[0])) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                        (char *)hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        JNU_ReleaseStringPlatformChars(env, host, hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
2056
115e09b7a004 6799040: Portability issues in src/solaris/native/java/net/Inet4AddressImpl.c
chegar
parents: 910
diff changeset
   415
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   417
    error = getaddrinfo(hostname, NULL, &hints, &res);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   418
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   419
    if (error) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   420
        /* 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
   421
        NET_ThrowUnknownHostExceptionWithGaiError(env, hostname, error);
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   422
        JNU_ReleaseStringPlatformChars(env, host, hostname);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   423
        return NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   424
    } else {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   425
        int i = 0;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   426
        struct addrinfo *itr, *last = NULL, *iterator = res;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   428
        while (iterator != NULL) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   429
            // remove the duplicate one
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   430
            int skip = 0;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   431
            itr = resNew;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   432
            while (itr != NULL) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   433
                struct sockaddr_in *addr1, *addr2;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   434
                addr1 = (struct sockaddr_in *)iterator->ai_addr;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   435
                addr2 = (struct sockaddr_in *)itr->ai_addr;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   436
                if (addr1->sin_addr.s_addr ==
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   437
                    addr2->sin_addr.s_addr) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   438
                    skip = 1;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   439
                    break;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   440
                }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   441
                itr = itr->ai_next;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   442
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   444
            if (!skip) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   445
                struct addrinfo *next
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   446
                    = (struct addrinfo*) malloc(sizeof(struct addrinfo));
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   447
                if (!next) {
13245
7ab3ef5b9520 7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents: 12688
diff changeset
   448
                    JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed");
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   449
                    ret = NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   450
                    goto cleanupAndReturn;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   451
                }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   452
                memcpy(next, iterator, sizeof(struct addrinfo));
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   453
                next->ai_next = NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   454
                if (resNew == NULL) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   455
                    resNew = next;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   456
                } else {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   457
                    last->ai_next = next;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   458
                }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   459
                last = next;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   460
                i++;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   461
            }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   462
            iterator = iterator->ai_next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   465
        retLen = i;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   466
        iterator = resNew;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   467
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   468
        ret = (*env)->NewObjectArray(env, retLen, ia_class, NULL);
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   469
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        if (IS_NULL(ret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            /* we may have memory to free at the end of this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            goto cleanupAndReturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   474
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        i = 0;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   476
        while (iterator != NULL) {
22646
5fa3669fd35d 8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents: 21620
diff changeset
   477
            jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   478
            if (IS_NULL(iaObj)) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   479
                ret = NULL;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   480
                goto cleanupAndReturn;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   481
            }
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   482
            setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr));
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   483
            setInetAddress_hostName(env, iaObj, host);
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   484
            (*env)->SetObjectArrayElement(env, ret, i++, iaObj);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   485
            iterator = iterator->ai_next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   489
 cleanupAndReturn:
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   490
    {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   491
        struct addrinfo *iterator, *tmp;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   492
        iterator = resNew;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   493
        while (iterator != NULL) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   494
            tmp = iterator;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   495
            iterator = iterator->ai_next;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   496
            free(tmp);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   497
        }
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   498
        JNU_ReleaseStringPlatformChars(env, host, hostname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   500
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   501
    freeaddrinfo(res);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   502
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
 * Method:    getHostByAddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
 * Signature: (I)Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
Java_java_net_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                            jbyteArray addrArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    jstring ret = NULL;
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   515
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   516
    char host[NI_MAXHOST+1];
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   517
    int error = 0;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   518
    int len = 0;
11033
ef047e386299 7114558: Inet4AddressImpl should use memset (rather than bzero) and NI_MAXHOST (rather than MAXHOSTNAMELEN)
ngmr
parents: 11032
diff changeset
   519
    jbyte caddr[4];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   521
    struct sockaddr_in him4;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   522
    struct sockaddr *sa;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   523
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   524
    jint addr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    addr = ((caddr[0]<<24) & 0xff000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    addr |= ((caddr[1] <<16) & 0xff0000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    addr |= ((caddr[2] <<8) & 0xff00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    addr |= (caddr[3] & 0xff);
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   530
    memset((void *) &him4, 0, sizeof(him4));
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   531
    him4.sin_addr.s_addr = (uint32_t) htonl(addr);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   532
    him4.sin_family = AF_INET;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   533
    sa = (struct sockaddr *) &him4;
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   534
    len = sizeof(him4);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   535
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   536
    error = getnameinfo(sa, len, host, NI_MAXHOST, NULL, 0,
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   537
                        NI_NAMEREQD);
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   538
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   539
    if (!error) {
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   540
        ret = (*env)->NewStringUTF(env, host);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   542
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   543
    if (ret == NULL) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
11032
235588f77727 7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents: 8774
diff changeset
   546
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   550
#endif /* _ALLBSD_SOURCE */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   551
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
#define SET_NONBLOCKING(fd) {           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        int flags = fcntl(fd, F_GETFL); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        flags |= O_NONBLOCK;            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        fcntl(fd, F_SETFL, flags);      \
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
 * ping implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
 * Send a ICMP_ECHO_REQUEST packet every second until either the timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
 * expires or a answer is received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
 * Returns true is an ECHO_REPLY is received, otherwise, false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
static jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
ping4(JNIEnv *env, jint fd, struct sockaddr_in* him, jint timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
      struct sockaddr_in* netif, jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    jint size;
2056
115e09b7a004 6799040: Portability issues in src/solaris/native/java/net/Inet4AddressImpl.c
chegar
parents: 910
diff changeset
   568
    jint n, hlen1, icmplen;
115e09b7a004 6799040: Portability issues in src/solaris/native/java/net/Inet4AddressImpl.c
chegar
parents: 910
diff changeset
   569
    socklen_t len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    char sendbuf[1500];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    char recvbuf[1500];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    struct icmp *icmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    struct ip *ip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    struct sockaddr_in sa_recv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    jchar pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    jint tmout2, seq = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    struct timeval tv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    size_t plen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /* icmp_id is a 16 bit data type, therefore down cast the pid */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    pid = (jchar)getpid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    size = 60*1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * sets the ttl (max number of hops)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    if (ttl > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
      setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * a specific interface was specified, so let's bind the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * to that interface to ensure the requests are sent only through it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    if (netif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
      if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        NET_ThrowNew(env, errno, "Can't bind socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * Make the socket non blocking so we can use select
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    SET_NONBLOCKING(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
       * create the ICMP request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
      icmp = (struct icmp *) sendbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
      icmp->icmp_type = ICMP_ECHO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
      icmp->icmp_code = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
      icmp->icmp_id = htons(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
      icmp->icmp_seq = htons(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
      seq++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
      gettimeofday(&tv, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
      memcpy(icmp->icmp_data, &tv, sizeof(tv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
      plen = ICMP_ADVLENMIN + sizeof(tv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
      icmp->icmp_cksum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
      icmp->icmp_cksum = in_cksum((u_short *)icmp, plen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
       * send it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
      n = sendto(fd, sendbuf, plen, 0, (struct sockaddr *)him,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                 sizeof(struct sockaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
      if (n < 0 && errno != EINPROGRESS ) {
7024
25bd21af28bf 6947677: InetAddress.isReachable() throws "java.net.SocketException:Invalid argument" on Linux if run as root
chegar
parents: 5506
diff changeset
   626
#ifdef __linux__
11278
ce6c46d16022 7118907: InetAddress.isReachable() should return false if sendto fails with EHOSTUNREACH
ngmr
parents: 11033
diff changeset
   627
        if (errno != EINVAL && errno != EHOSTUNREACH)
7024
25bd21af28bf 6947677: InetAddress.isReachable() throws "java.net.SocketException:Invalid argument" on Linux if run as root
chegar
parents: 5506
diff changeset
   628
          /*
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13245
diff changeset
   629
           * On some Linux versions, when a socket is bound to the loopback
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13245
diff changeset
   630
           * interface, sendto will fail and errno will be set to
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13245
diff changeset
   631
           * EINVAL or EHOSTUNREACH. When that happens, don't throw an
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13245
diff changeset
   632
           * exception, just return false.
7024
25bd21af28bf 6947677: InetAddress.isReachable() throws "java.net.SocketException:Invalid argument" on Linux if run as root
chegar
parents: 5506
diff changeset
   633
           */
25bd21af28bf 6947677: InetAddress.isReachable() throws "java.net.SocketException:Invalid argument" on Linux if run as root
chegar
parents: 5506
diff changeset
   634
#endif /*__linux__ */
25bd21af28bf 6947677: InetAddress.isReachable() throws "java.net.SocketException:Invalid argument" on Linux if run as root
chegar
parents: 5506
diff changeset
   635
          NET_ThrowNew(env, errno, "Can't send ICMP packet");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
      tmout2 = timeout > 1000 ? 1000 : timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
      do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        if (tmout2 >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
          len = sizeof(sa_recv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
          n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr *)&sa_recv, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
          ip = (struct ip*) recvbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
          hlen1 = (ip->ip_hl) << 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
          icmp = (struct icmp *) (recvbuf + hlen1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
          icmplen = n - hlen1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
          /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
           * We did receive something, but is it what we were expecting?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
           * I.E.: A ICMP_ECHOREPLY packet with the proper PID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
           */
12688
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12047
diff changeset
   654
          if (icmplen >= 8 && icmp->icmp_type == ICMP_ECHOREPLY
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12047
diff changeset
   655
               && (ntohs(icmp->icmp_id) == pid)) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12047
diff changeset
   656
            if ((him->sin_addr.s_addr == sa_recv.sin_addr.s_addr)) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12047
diff changeset
   657
              close(fd);
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12047
diff changeset
   658
              return JNI_TRUE;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12047
diff changeset
   659
            }
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12047
diff changeset
   660
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12047
diff changeset
   661
            if (him->sin_addr.s_addr == 0) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12047
diff changeset
   662
              close(fd);
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12047
diff changeset
   663
              return JNI_TRUE;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12047
diff changeset
   664
            }
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12047
diff changeset
   665
         }
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents: 12047
diff changeset
   666
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
      } while (tmout2 > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
      timeout -= 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    } while (timeout >0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
 * Class:     java_net_Inet4AddressImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
 * Method:    isReachable0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
 * Signature: ([bI[bI)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
Java_java_net_Inet4AddressImpl_isReachable0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                                           jbyteArray addrArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                                           jint timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                                           jbyteArray ifArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                                           jint ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    jint addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    jbyte caddr[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    struct sockaddr_in him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    struct sockaddr_in* netif = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    struct sockaddr_in inf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    int connect_rv = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    int sz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    memset((char *) caddr, 0, sizeof(caddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    memset((char *) &him, 0, sizeof(him));
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11278
diff changeset
   698
    memset((char *) &inf, 0, sizeof(inf));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    sz = (*env)->GetArrayLength(env, addrArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    if (sz != 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
      return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    addr = ((caddr[0]<<24) & 0xff000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    addr |= ((caddr[1] <<16) & 0xff0000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    addr |= ((caddr[2] <<8) & 0xff00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    addr |= (caddr[3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    addr = htonl(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    him.sin_addr.s_addr = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    him.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    len = sizeof(him);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * If a network interface was specified, let's create the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    if (!(IS_NULL(ifArray))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
      memset((char *) caddr, 0, sizeof(caddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
      (*env)->GetByteArrayRegion(env, ifArray, 0, 4, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
      addr = ((caddr[0]<<24) & 0xff000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
      addr |= ((caddr[1] <<16) & 0xff0000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
      addr |= ((caddr[2] <<8) & 0xff00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
      addr |= (caddr[3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
      addr = htonl(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
      inf.sin_addr.s_addr = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
      inf.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
      inf.sin_port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
      netif = &inf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * Let's try to create a RAW socket to send ICMP packets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * This usually requires "root" privileges, so it's likely to fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     */
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   734
    fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    if (fd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
       * It didn't fail, so we can use ICMP_ECHO requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
      return ping4(env, fd, &him, timeout, netif, ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * Can't create a raw socket, so let's try a TCP socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     */
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   745
    fd = socket(AF_INET, SOCK_STREAM, 0);
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   746
    if (fd == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        /* note: if you run out of fds, you may not be able to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
         * the exception class, and get a NoClassDefFoundError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
         * instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        NET_ThrowNew(env, errno, "Can't create socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    if (ttl > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
      setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * A network interface was specified, so let's bind to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    if (netif != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
      if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        NET_ThrowNew(env, errno, "Can't bind socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * Make the socket non blocking so we can use select/poll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    SET_NONBLOCKING(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    him.sin_port = htons(7);    /* Echo */
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   775
    connect_rv = NET_Connect(fd, (struct sockaddr *)&him, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * connection established or refused immediately, either way it means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * we were able to reach the host!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    if (connect_rv == 0 || errno == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    } else {
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   785
        socklen_t optlen = (socklen_t)sizeof(connect_rv);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        switch (errno) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        case ENETUNREACH: /* Network Unreachable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        case EAFNOSUPPORT: /* Address Family not supported */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        case EADDRNOTAVAIL: /* address is not available on  the  remote machine */
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 31059
diff changeset
   791
#if defined(__linux__) || defined(_AIX)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        case EINVAL:
41380
c27cf95dd7e6 8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents: 31059
diff changeset
   793
        case EHOSTUNREACH: /* No route to host */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
          /*
13999
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13245
diff changeset
   795
           * On some Linux versions, when a socket is bound to the loopback
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13245
diff changeset
   796
           * interface, connect will fail and errno will be set to EINVAL
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13245
diff changeset
   797
           * or EHOSTUNREACH.  When that happens, don't throw an exception,
f6a2ce6a3e40 7193520: Removed references to Linux kernel version 2.2
chegar
parents: 13245
diff changeset
   798
           * just return false.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
#endif /* __linux__ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
          close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
          return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        if (errno != EINPROGRESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
          NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                                       "connect failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
          close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
          return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        if (timeout >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
          /* has connection been established? */
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   815
          if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv,
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22646
diff changeset
   816
                         &optlen) <0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            connect_rv = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
          if (connect_rv == 0 || connect_rv == ECONNREFUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
}