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