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