author | michaelm |
Wed, 13 Feb 2013 10:40:31 +0000 | |
changeset 16870 | f35b2bd19761 |
parent 14342 | 8435a30053c1 |
child 20741 | 6eb501508669 |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
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 | 28 |
#include <sys/types.h> |
29 |
#include <sys/socket.h> |
|
30 |
#include <netinet/in_systm.h> |
|
31 |
#include <netinet/in.h> |
|
32 |
#include <netinet/ip.h> |
|
33 |
#include <netinet/ip_icmp.h> |
|
34 |
#include <netdb.h> |
|
35 |
#include <string.h> |
|
36 |
#include <stdlib.h> |
|
37 |
#include <ctype.h> |
|
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 | 44 |
#include "jvm.h" |
45 |
#include "jni_util.h" |
|
46 |
#include "net_util.h" |
|
47 |
||
48 |
#include "java_net_Inet4AddressImpl.h" |
|
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 | 238 |
setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in*)(iterator->ai_addr))->sin_addr.s_addr)); |
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 | 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 | 321 |
|
322 |
/************************************************************************ |
|
323 |
* Inet4AddressImpl |
|
324 |
*/ |
|
325 |
||
326 |
/* |
|
327 |
* Class: java_net_Inet4AddressImpl |
|
328 |
* Method: getLocalHostName |
|
329 |
* Signature: ()Ljava/lang/String; |
|
330 |
*/ |
|
331 |
JNIEXPORT jstring JNICALL |
|
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 | 334 |
|
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 | 337 |
/* Something went wrong, maybe networking is not setup? */ |
338 |
strcpy(hostname, "localhost"); |
|
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 | 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 | 363 |
} |
364 |
} |
|
365 |
return (*env)->NewStringUTF(env, hostname); |
|
366 |
} |
|
367 |
||
368 |
static jclass ni_iacls; |
|
369 |
static jclass ni_ia4cls; |
|
370 |
static jmethodID ni_ia4ctrID; |
|
371 |
static int initialized = 0; |
|
372 |
||
373 |
/* |
|
374 |
* Find an internet address for a given hostname. Note that this |
|
375 |
* code only works for addresses of type INET. The translation |
|
376 |
* of %d.%d.%d.%d to an address (int) occurs in java now, so the |
|
377 |
* String "host" shouldn't *ever* be a %d.%d.%d.%d string |
|
378 |
* |
|
379 |
* Class: java_net_Inet4AddressImpl |
|
380 |
* Method: lookupAllHostAddr |
|
381 |
* Signature: (Ljava/lang/String;)[[B |
|
382 |
*/ |
|
383 |
||
384 |
JNIEXPORT jobjectArray JNICALL |
|
385 |
Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this, |
|
386 |
jstring host) { |
|
387 |
const char *hostname; |
|
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 | 392 |
|
393 |
if (!initialized) { |
|
394 |
ni_iacls = (*env)->FindClass(env, "java/net/InetAddress"); |
|
395 |
ni_iacls = (*env)->NewGlobalRef(env, ni_iacls); |
|
396 |
ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address"); |
|
397 |
ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls); |
|
398 |
ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V"); |
|
399 |
initialized = 1; |
|
400 |
} |
|
401 |
||
402 |
if (IS_NULL(host)) { |
|
403 |
JNU_ThrowNullPointerException(env, "host is null"); |
|
404 |
return 0; |
|
405 |
} |
|
406 |
hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE); |
|
407 |
CHECK_NULL_RETURN(hostname, NULL); |
|
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 | 415 |
/* |
416 |
* Workaround for Solaris bug 4160367 - if a hostname contains a |
|
417 |
* white space then 0.0.0.0 is returned |
|
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 | 420 |
JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", |
421 |
(char *)hostname); |
|
422 |
JNU_ReleaseStringPlatformChars(env, host, hostname); |
|
423 |
return NULL; |
|
424 |
} |
|
2056
115e09b7a004
6799040: Portability issues in src/solaris/native/java/net/Inet4AddressImpl.c
chegar
parents:
910
diff
changeset
|
425 |
#endif |
2 | 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 | 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 | 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 | 473 |
} |
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 | 480 |
if (IS_NULL(ret)) { |
481 |
/* we may have memory to free at the end of this */ |
|
482 |
goto cleanupAndReturn; |
|
483 |
} |
|
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
8774
diff
changeset
|
484 |
|
2 | 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 | 492 |
setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr)); |
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 | 496 |
} |
497 |
} |
|
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 | 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 | 513 |
return ret; |
514 |
} |
|
515 |
||
516 |
/* |
|
517 |
* Class: java_net_Inet4AddressImpl |
|
518 |
* Method: getHostByAddr |
|
519 |
* Signature: (I)Ljava/lang/String; |
|
520 |
*/ |
|
521 |
JNIEXPORT jstring JNICALL |
|
522 |
Java_java_net_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this, |
|
523 |
jbyteArray addrArray) { |
|
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 | 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 | 535 |
(*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr); |
536 |
addr = ((caddr[0]<<24) & 0xff000000); |
|
537 |
addr |= ((caddr[1] <<16) & 0xff0000); |
|
538 |
addr |= ((caddr[2] <<8) & 0xff00); |
|
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 | 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 | 554 |
JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", NULL); |
555 |
} |
|
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
8774
diff
changeset
|
556 |
|
2 | 557 |
return ret; |
558 |
} |
|
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 | 562 |
#define SET_NONBLOCKING(fd) { \ |
563 |
int flags = fcntl(fd, F_GETFL); \ |
|
564 |
flags |= O_NONBLOCK; \ |
|
565 |
fcntl(fd, F_SETFL, flags); \ |
|
566 |
} |
|
567 |
||
568 |
/** |
|
569 |
* ping implementation. |
|
570 |
* Send a ICMP_ECHO_REQUEST packet every second until either the timeout |
|
571 |
* expires or a answer is received. |
|
572 |
* Returns true is an ECHO_REPLY is received, otherwise, false. |
|
573 |
*/ |
|
574 |
static jboolean |
|
575 |
ping4(JNIEnv *env, jint fd, struct sockaddr_in* him, jint timeout, |
|
576 |
struct sockaddr_in* netif, jint ttl) { |
|
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 | 580 |
char sendbuf[1500]; |
581 |
char recvbuf[1500]; |
|
582 |
struct icmp *icmp; |
|
583 |
struct ip *ip; |
|
584 |
struct sockaddr_in sa_recv; |
|
585 |
jchar pid; |
|
586 |
jint tmout2, seq = 1; |
|
587 |
struct timeval tv; |
|
588 |
size_t plen; |
|
589 |
||
590 |
/* icmp_id is a 16 bit data type, therefore down cast the pid */ |
|
591 |
pid = (jchar)getpid(); |
|
592 |
size = 60*1024; |
|
593 |
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)); |
|
594 |
/* |
|
595 |
* sets the ttl (max number of hops) |
|
596 |
*/ |
|
597 |
if (ttl > 0) { |
|
598 |
setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)); |
|
599 |
} |
|
600 |
/* |
|
601 |
* a specific interface was specified, so let's bind the socket |
|
602 |
* to that interface to ensure the requests are sent only through it. |
|
603 |
*/ |
|
604 |
if (netif != NULL) { |
|
605 |
if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) { |
|
606 |
NET_ThrowNew(env, errno, "Can't bind socket"); |
|
607 |
close(fd); |
|
608 |
return JNI_FALSE; |
|
609 |
} |
|
610 |
} |
|
611 |
/* |
|
612 |
* Make the socket non blocking so we can use select |
|
613 |
*/ |
|
614 |
SET_NONBLOCKING(fd); |
|
615 |
do { |
|
616 |
/* |
|
617 |
* create the ICMP request |
|
618 |
*/ |
|
619 |
icmp = (struct icmp *) sendbuf; |
|
620 |
icmp->icmp_type = ICMP_ECHO; |
|
621 |
icmp->icmp_code = 0; |
|
622 |
icmp->icmp_id = htons(pid); |
|
623 |
icmp->icmp_seq = htons(seq); |
|
624 |
seq++; |
|
625 |
gettimeofday(&tv, NULL); |
|
626 |
memcpy(icmp->icmp_data, &tv, sizeof(tv)); |
|
627 |
plen = ICMP_ADVLENMIN + sizeof(tv); |
|
628 |
icmp->icmp_cksum = 0; |
|
629 |
icmp->icmp_cksum = in_cksum((u_short *)icmp, plen); |
|
630 |
/* |
|
631 |
* send it |
|
632 |
*/ |
|
633 |
n = sendto(fd, sendbuf, plen, 0, (struct sockaddr *)him, |
|
634 |
sizeof(struct sockaddr)); |
|
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 | 646 |
close(fd); |
647 |
return JNI_FALSE; |
|
648 |
} |
|
649 |
||
650 |
tmout2 = timeout > 1000 ? 1000 : timeout; |
|
651 |
do { |
|
652 |
tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2); |
|
653 |
if (tmout2 >= 0) { |
|
654 |
len = sizeof(sa_recv); |
|
655 |
n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr *)&sa_recv, &len); |
|
656 |
ip = (struct ip*) recvbuf; |
|
657 |
hlen1 = (ip->ip_hl) << 2; |
|
658 |
icmp = (struct icmp *) (recvbuf + hlen1); |
|
659 |
icmplen = n - hlen1; |
|
660 |
/* |
|
661 |
* We did receive something, but is it what we were expecting? |
|
662 |
* I.E.: A ICMP_ECHOREPLY packet with the proper PID. |
|
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 | 677 |
} |
678 |
} while (tmout2 > 0); |
|
679 |
timeout -= 1000; |
|
680 |
} while (timeout >0); |
|
681 |
close(fd); |
|
682 |
return JNI_FALSE; |
|
683 |
} |
|
684 |
||
685 |
/* |
|
686 |
* Class: java_net_Inet4AddressImpl |
|
687 |
* Method: isReachable0 |
|
688 |
* Signature: ([bI[bI)Z |
|
689 |
*/ |
|
690 |
JNIEXPORT jboolean JNICALL |
|
691 |
Java_java_net_Inet4AddressImpl_isReachable0(JNIEnv *env, jobject this, |
|
692 |
jbyteArray addrArray, |
|
693 |
jint timeout, |
|
694 |
jbyteArray ifArray, |
|
695 |
jint ttl) { |
|
696 |
jint addr; |
|
697 |
jbyte caddr[4]; |
|
698 |
jint fd; |
|
699 |
struct sockaddr_in him; |
|
700 |
struct sockaddr_in* netif = NULL; |
|
701 |
struct sockaddr_in inf; |
|
702 |
int len = 0; |
|
703 |
int connect_rv = -1; |
|
704 |
int sz; |
|
705 |
||
706 |
memset((char *) caddr, 0, sizeof(caddr)); |
|
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 | 709 |
sz = (*env)->GetArrayLength(env, addrArray); |
710 |
if (sz != 4) { |
|
711 |
return JNI_FALSE; |
|
712 |
} |
|
713 |
(*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr); |
|
714 |
addr = ((caddr[0]<<24) & 0xff000000); |
|
715 |
addr |= ((caddr[1] <<16) & 0xff0000); |
|
716 |
addr |= ((caddr[2] <<8) & 0xff00); |
|
717 |
addr |= (caddr[3] & 0xff); |
|
718 |
addr = htonl(addr); |
|
719 |
him.sin_addr.s_addr = addr; |
|
720 |
him.sin_family = AF_INET; |
|
721 |
len = sizeof(him); |
|
722 |
/* |
|
723 |
* If a network interface was specified, let's create the address |
|
724 |
* for it. |
|
725 |
*/ |
|
726 |
if (!(IS_NULL(ifArray))) { |
|
727 |
memset((char *) caddr, 0, sizeof(caddr)); |
|
728 |
(*env)->GetByteArrayRegion(env, ifArray, 0, 4, caddr); |
|
729 |
addr = ((caddr[0]<<24) & 0xff000000); |
|
730 |
addr |= ((caddr[1] <<16) & 0xff0000); |
|
731 |
addr |= ((caddr[2] <<8) & 0xff00); |
|
732 |
addr |= (caddr[3] & 0xff); |
|
733 |
addr = htonl(addr); |
|
734 |
inf.sin_addr.s_addr = addr; |
|
735 |
inf.sin_family = AF_INET; |
|
736 |
inf.sin_port = 0; |
|
737 |
netif = &inf; |
|
738 |
} |
|
739 |
||
740 |
/* |
|
741 |
* Let's try to create a RAW socket to send ICMP packets |
|
742 |
* This usually requires "root" privileges, so it's likely to fail. |
|
743 |
*/ |
|
744 |
fd = JVM_Socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); |
|
745 |
if (fd != -1) { |
|
746 |
/* |
|
747 |
* It didn't fail, so we can use ICMP_ECHO requests. |
|
748 |
*/ |
|
749 |
return ping4(env, fd, &him, timeout, netif, ttl); |
|
750 |
} |
|
751 |
||
752 |
/* |
|
753 |
* Can't create a raw socket, so let's try a TCP socket |
|
754 |
*/ |
|
755 |
fd = JVM_Socket(AF_INET, SOCK_STREAM, 0); |
|
756 |
if (fd == JVM_IO_ERR) { |
|
757 |
/* note: if you run out of fds, you may not be able to load |
|
758 |
* the exception class, and get a NoClassDefFoundError |
|
759 |
* instead. |
|
760 |
*/ |
|
761 |
NET_ThrowNew(env, errno, "Can't create socket"); |
|
762 |
return JNI_FALSE; |
|
763 |
} |
|
764 |
if (ttl > 0) { |
|
765 |
setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)); |
|
766 |
} |
|
767 |
||
768 |
/* |
|
769 |
* A network interface was specified, so let's bind to it. |
|
770 |
*/ |
|
771 |
if (netif != NULL) { |
|
772 |
if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in)) < 0) { |
|
773 |
NET_ThrowNew(env, errno, "Can't bind socket"); |
|
774 |
close(fd); |
|
775 |
return JNI_FALSE; |
|
776 |
} |
|
777 |
} |
|
778 |
||
779 |
/* |
|
780 |
* Make the socket non blocking so we can use select/poll. |
|
781 |
*/ |
|
782 |
SET_NONBLOCKING(fd); |
|
783 |
||
784 |
/* no need to use NET_Connect as non-blocking */ |
|
785 |
him.sin_port = htons(7); /* Echo */ |
|
786 |
connect_rv = JVM_Connect(fd, (struct sockaddr *)&him, len); |
|
787 |
||
788 |
/** |
|
789 |
* connection established or refused immediately, either way it means |
|
790 |
* we were able to reach the host! |
|
791 |
*/ |
|
792 |
if (connect_rv == 0 || errno == ECONNREFUSED) { |
|
793 |
close(fd); |
|
794 |
return JNI_TRUE; |
|
795 |
} else { |
|
796 |
int optlen; |
|
797 |
||
798 |
switch (errno) { |
|
799 |
case ENETUNREACH: /* Network Unreachable */ |
|
800 |
case EAFNOSUPPORT: /* Address Family not supported */ |
|
801 |
case EADDRNOTAVAIL: /* address is not available on the remote machine */ |
|
802 |
#ifdef __linux__ |
|
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 | 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 | 810 |
*/ |
811 |
#endif /* __linux__ */ |
|
812 |
close(fd); |
|
813 |
return JNI_FALSE; |
|
814 |
} |
|
815 |
||
816 |
if (errno != EINPROGRESS) { |
|
817 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException", |
|
818 |
"connect failed"); |
|
819 |
close(fd); |
|
820 |
return JNI_FALSE; |
|
821 |
} |
|
822 |
||
823 |
timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout); |
|
824 |
if (timeout >= 0) { |
|
825 |
/* has connection been established? */ |
|
826 |
optlen = sizeof(connect_rv); |
|
827 |
if (JVM_GetSockOpt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv, |
|
828 |
&optlen) <0) { |
|
829 |
connect_rv = errno; |
|
830 |
} |
|
831 |
if (connect_rv == 0 || connect_rv == ECONNREFUSED) { |
|
832 |
close(fd); |
|
833 |
return JNI_TRUE; |
|
834 |
} |
|
835 |
} |
|
836 |
close(fd); |
|
837 |
return JNI_FALSE; |
|
838 |
} |
|
839 |
} |