author | clanger |
Thu, 27 Oct 2016 11:00:28 +0200 | |
changeset 41771 | 18c9669e76ca |
parent 41380 | c27cf95dd7e6 |
child 41834 | 99c4f72c0a91 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
38552
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
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 |
*/ |
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
25 |
#include <ctype.h> |
2 | 26 |
#include <errno.h> |
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
27 |
#include <stdlib.h> |
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
28 |
#include <string.h> |
3226
b142b3d06bf5
6562614: Compiler warnings for gettimeofday in Inet4/Inet6AddressImpl.c
andrew
parents:
2056
diff
changeset
|
29 |
#include <sys/time.h> |
2 | 30 |
#include <sys/types.h> |
31 |
#include <netinet/in.h> |
|
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
32 |
#include <netinet/icmp6.h> |
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
33 |
|
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
34 |
#if defined(_ALLBSD_SOURCE) |
20741
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
35 |
#include <ifaddrs.h> |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
36 |
#include <net/if.h> |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
11278
diff
changeset
|
37 |
#endif |
2 | 38 |
|
39 |
#include "net_util.h" |
|
40 |
||
41 |
#include "java_net_Inet4AddressImpl.h" |
|
42 |
#include "java_net_Inet6AddressImpl.h" |
|
38552
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
31059
diff
changeset
|
43 |
#include "java_net_InetAddress.h" |
2 | 44 |
|
45 |
/* the initial size of our hostent buffers */ |
|
46 |
#ifndef NI_MAXHOST |
|
47 |
#define NI_MAXHOST 1025 |
|
48 |
#endif |
|
49 |
||
50 |
||
51 |
/************************************************************************ |
|
52 |
* Inet6AddressImpl |
|
53 |
*/ |
|
54 |
||
55 |
/* |
|
56 |
* Class: java_net_Inet6AddressImpl |
|
57 |
* Method: getLocalHostName |
|
58 |
* Signature: ()Ljava/lang/String; |
|
59 |
*/ |
|
60 |
JNIEXPORT jstring JNICALL |
|
61 |
Java_java_net_Inet6AddressImpl_getLocalHostName(JNIEnv *env, jobject this) { |
|
22956 | 62 |
int ret; |
2 | 63 |
char hostname[NI_MAXHOST+1]; |
64 |
||
65 |
hostname[0] = '\0'; |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22956
diff
changeset
|
66 |
ret = gethostname(hostname, NI_MAXHOST); |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22956
diff
changeset
|
67 |
if (ret == -1) { |
2 | 68 |
/* Something went wrong, maybe networking is not setup? */ |
69 |
strcpy(hostname, "localhost"); |
|
70 |
} else { |
|
8774
4403ce77f306
7024560: InetAddress.getLocalHost().getHostName() returns localhost for hostnames of length HOST_NAME_MAX
chegar
parents:
7668
diff
changeset
|
71 |
// ensure null-terminated |
4403ce77f306
7024560: InetAddress.getLocalHost().getHostName() returns localhost for hostnames of length HOST_NAME_MAX
chegar
parents:
7668
diff
changeset
|
72 |
hostname[NI_MAXHOST] = '\0'; |
22956 | 73 |
} |
74 |
||
75 |
#if defined(__solaris__) && defined(AF_INET6) |
|
76 |
if (ret == 0) { |
|
2 | 77 |
/* Solaris doesn't want to give us a fully qualified domain name. |
78 |
* We do a reverse lookup to try and get one. This works |
|
79 |
* if DNS occurs before NIS in /etc/resolv.conf, but fails |
|
80 |
* if NIS comes first (it still gets only a partial name). |
|
81 |
* We use thread-safe system calls. |
|
82 |
*/ |
|
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
83 |
struct addrinfo hints, *res; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
84 |
int error; |
2 | 85 |
|
11033
ef047e386299
7114558: Inet4AddressImpl should use memset (rather than bzero) and NI_MAXHOST (rather than MAXHOSTNAMELEN)
ngmr
parents:
11032
diff
changeset
|
86 |
memset(&hints, 0, sizeof(hints)); |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
87 |
hints.ai_flags = AI_CANONNAME; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
88 |
hints.ai_family = AF_UNSPEC; |
2 | 89 |
|
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
90 |
error = getaddrinfo(hostname, NULL, &hints, &res); |
2 | 91 |
|
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
92 |
if (error == 0) { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
93 |
/* host is known to name service */ |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
94 |
error = getnameinfo(res->ai_addr, |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
95 |
res->ai_addrlen, |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
96 |
hostname, |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
97 |
NI_MAXHOST, |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
98 |
NULL, |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
99 |
0, |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
100 |
NI_NAMEREQD); |
2 | 101 |
|
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
102 |
/* if getnameinfo fails hostname is still the value |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
103 |
from gethostname */ |
2 | 104 |
|
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
105 |
freeaddrinfo(res); |
2 | 106 |
} |
107 |
} |
|
22956 | 108 |
#endif |
109 |
||
2 | 110 |
return (*env)->NewStringUTF(env, hostname); |
111 |
} |
|
112 |
||
20741
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
113 |
#ifdef MACOSX |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
114 |
/* also called from Inet4AddressImpl.c */ |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
115 |
__private_extern__ jobjectArray |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
116 |
lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6) |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
117 |
{ |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
118 |
jobjectArray result = NULL; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
119 |
char myhostname[NI_MAXHOST+1]; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
120 |
struct ifaddrs *ifa = NULL; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
121 |
int familyOrder = 0; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
122 |
int count = 0, i, j; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
123 |
int addrs4 = 0, addrs6 = 0, numV4Loopbacks = 0, numV6Loopbacks = 0; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
124 |
jboolean includeLoopback = JNI_FALSE; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
125 |
jobject name; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
126 |
|
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22640
diff
changeset
|
127 |
initInetAddressIDs(env); |
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22640
diff
changeset
|
128 |
JNU_CHECK_EXCEPTION_RETURN(env, NULL); |
20741
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
129 |
|
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
130 |
/* If the requested name matches this host's hostname, return IP addresses |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
131 |
* from all attached interfaces. (#2844683 et al) This prevents undesired |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
132 |
* PPP dialup, but may return addresses that don't actually correspond to |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
133 |
* the name (if the name actually matches something in DNS etc. |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
134 |
*/ |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
135 |
myhostname[0] = '\0'; |
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22956
diff
changeset
|
136 |
if (gethostname(myhostname, NI_MAXHOST) == -1) { |
20741
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
137 |
/* Something went wrong, maybe networking is not setup? */ |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
138 |
return NULL; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
139 |
} |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
140 |
myhostname[NI_MAXHOST] = '\0'; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
141 |
|
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
142 |
if (strcmp(myhostname, hostname) != 0) { |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
143 |
// Non-self lookup |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
144 |
return NULL; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
145 |
} |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
146 |
|
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
147 |
if (getifaddrs(&ifa) != 0) { |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
148 |
NET_ThrowNew(env, errno, "Can't get local interface addresses"); |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
149 |
return NULL; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
150 |
} |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
151 |
|
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
152 |
name = (*env)->NewStringUTF(env, hostname); |
38563
3ab5f00bd5a3
8157811: Additional minor fixes and cleanups in Networking native code
chegar
parents:
38552
diff
changeset
|
153 |
if (name == NULL) { |
3ab5f00bd5a3
8157811: Additional minor fixes and cleanups in Networking native code
chegar
parents:
38552
diff
changeset
|
154 |
freeifaddrs(ifa); |
3ab5f00bd5a3
8157811: Additional minor fixes and cleanups in Networking native code
chegar
parents:
38552
diff
changeset
|
155 |
return NULL; |
3ab5f00bd5a3
8157811: Additional minor fixes and cleanups in Networking native code
chegar
parents:
38552
diff
changeset
|
156 |
} |
20741
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
157 |
|
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
158 |
/* Iterate over the interfaces, and total up the number of IPv4 and IPv6 |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
159 |
* addresses we have. Also keep a count of loopback addresses. We need to |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
160 |
* exclude them in the normal case, but return them if we don't get an IP |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
161 |
* address. |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
162 |
*/ |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
163 |
struct ifaddrs *iter = ifa; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
164 |
while (iter) { |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
165 |
int family = iter->ifa_addr->sa_family; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
166 |
if (iter->ifa_name[0] != '\0' && iter->ifa_addr) |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
167 |
{ |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
168 |
jboolean isLoopback = iter->ifa_flags & IFF_LOOPBACK; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
169 |
if (family == AF_INET) { |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
170 |
addrs4++; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
171 |
if (isLoopback) numV4Loopbacks++; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
172 |
} else if (family == AF_INET6 && includeV6) { |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
173 |
addrs6++; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
174 |
if (isLoopback) numV6Loopbacks++; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
175 |
} else { |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
176 |
/* We don't care e.g. AF_LINK */ |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
177 |
} |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
178 |
} |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
179 |
iter = iter->ifa_next; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
180 |
} |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
181 |
|
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
182 |
if (addrs4 == numV4Loopbacks && addrs6 == numV6Loopbacks) { |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
183 |
// We don't have a real IP address, just loopback. We need to include |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
184 |
// loopback in our results. |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
185 |
includeLoopback = JNI_TRUE; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
186 |
} |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
187 |
|
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
188 |
/* Create and fill the Java array. */ |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
189 |
int arraySize = addrs4 + addrs6 - |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
190 |
(includeLoopback ? 0 : (numV4Loopbacks + numV6Loopbacks)); |
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22640
diff
changeset
|
191 |
result = (*env)->NewObjectArray(env, arraySize, ia_class, NULL); |
20741
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
192 |
if (!result) goto done; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
193 |
|
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22640
diff
changeset
|
194 |
if ((*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID)) { |
20741
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
195 |
i = includeLoopback ? addrs6 : (addrs6 - numV6Loopbacks); |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
196 |
j = 0; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
197 |
} else { |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
198 |
i = 0; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
199 |
j = includeLoopback ? addrs4 : (addrs4 - numV4Loopbacks); |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
200 |
} |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
201 |
|
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
202 |
// Now loop around the ifaddrs |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
203 |
iter = ifa; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
204 |
while (iter != NULL) { |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
205 |
jboolean isLoopback = iter->ifa_flags & IFF_LOOPBACK; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
206 |
int family = iter->ifa_addr->sa_family; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
207 |
|
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
208 |
if (iter->ifa_name[0] != '\0' && iter->ifa_addr |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
209 |
&& (family == AF_INET || (family == AF_INET6 && includeV6)) |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
210 |
&& (!isLoopback || includeLoopback)) |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
211 |
{ |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
212 |
int port; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
213 |
int index = (family == AF_INET) ? i++ : j++; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
214 |
jobject o = NET_SockaddrToInetAddress(env, iter->ifa_addr, &port); |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
215 |
if (!o) { |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
216 |
freeifaddrs(ifa); |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
217 |
if (!(*env)->ExceptionCheck(env)) |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
218 |
JNU_ThrowOutOfMemoryError(env, "Object allocation failed"); |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
219 |
return NULL; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
220 |
} |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
221 |
setInetAddress_hostName(env, o, name); |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
222 |
(*env)->SetObjectArrayElement(env, result, index, o); |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
223 |
(*env)->DeleteLocalRef(env, o); |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
224 |
} |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
225 |
iter = iter->ifa_next; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
226 |
} |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
227 |
|
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
228 |
done: |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
229 |
freeifaddrs(ifa); |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
230 |
|
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
231 |
return result; |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
232 |
} |
6eb501508669
7180557: InetAddress.getLocalHost throws UnknownHostException on java7u5 on OSX webbugs
robm
parents:
16870
diff
changeset
|
233 |
#endif |
2 | 234 |
|
235 |
/* |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
236 |
* Find an internet address for a given hostname. Note that this |
2 | 237 |
* code only works for addresses of type INET. The translation |
238 |
* of %d.%d.%d.%d to an address (int) occurs in java now, so the |
|
239 |
* String "host" shouldn't *ever* be a %d.%d.%d.%d string |
|
240 |
* |
|
241 |
* Class: java_net_Inet6AddressImpl |
|
242 |
* Method: lookupAllHostAddr |
|
243 |
* Signature: (Ljava/lang/String;)[[B |
|
244 |
*/ |
|
245 |
||
246 |
JNIEXPORT jobjectArray JNICALL |
|
247 |
Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this, |
|
248 |
jstring host) { |
|
249 |
const char *hostname; |
|
250 |
jobjectArray ret = 0; |
|
251 |
int retLen = 0; |
|
252 |
||
31059
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
253 |
int getaddrinfo_error=0; |
2 | 254 |
#ifdef AF_INET6 |
255 |
struct addrinfo hints, *res, *resNew = NULL; |
|
256 |
#endif /* AF_INET6 */ |
|
257 |
||
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22640
diff
changeset
|
258 |
initInetAddressIDs(env); |
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22640
diff
changeset
|
259 |
JNU_CHECK_EXCEPTION_RETURN(env, NULL); |
2 | 260 |
|
261 |
if (IS_NULL(host)) { |
|
262 |
JNU_ThrowNullPointerException(env, "host is null"); |
|
263 |
return 0; |
|
264 |
} |
|
265 |
hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE); |
|
266 |
CHECK_NULL_RETURN(hostname, NULL); |
|
267 |
||
268 |
#ifdef AF_INET6 |
|
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
269 |
/* 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
|
270 |
memset(&hints, 0, sizeof(hints)); |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
271 |
hints.ai_flags = AI_CANONNAME; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
272 |
hints.ai_family = AF_UNSPEC; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
273 |
|
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
274 |
#ifdef __solaris__ |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
275 |
/* |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
276 |
* Workaround for Solaris bug 4160367 - if a hostname contains a |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
277 |
* white space then 0.0.0.0 is returned |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
278 |
*/ |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
279 |
if (isspace((unsigned char)hostname[0])) { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
280 |
JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
281 |
hostname); |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
282 |
JNU_ReleaseStringPlatformChars(env, host, hostname); |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
283 |
return NULL; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
284 |
} |
2056
115e09b7a004
6799040: Portability issues in src/solaris/native/java/net/Inet4AddressImpl.c
chegar
parents:
910
diff
changeset
|
285 |
#endif |
2 | 286 |
|
31059
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
287 |
getaddrinfo_error = getaddrinfo(hostname, NULL, &hints, &res); |
2 | 288 |
|
31059
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
289 |
#ifdef MACOSX |
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
290 |
if (getaddrinfo_error) { |
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
291 |
/* |
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
292 |
* If getaddrinfo fails looking up the local machine, attempt to get the |
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
293 |
* address from getifaddrs. This ensures we get an IPv6 address for the |
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
294 |
* local machine. |
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
295 |
*/ |
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
296 |
ret = lookupIfLocalhost(env, hostname, JNI_TRUE); |
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
297 |
if (ret != NULL || (*env)->ExceptionCheck(env)) { |
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
298 |
JNU_ReleaseStringPlatformChars(env, host, hostname); |
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
299 |
return ret; |
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
300 |
} |
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
301 |
} |
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
302 |
#endif |
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
303 |
|
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
304 |
if (getaddrinfo_error) { |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
305 |
/* report error */ |
31059
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
306 |
NET_ThrowUnknownHostExceptionWithGaiError( |
0ec777e00e2e
8080819: Inet4AddressImpl regression caused by JDK-7180557
robm
parents:
25859
diff
changeset
|
307 |
env, hostname, getaddrinfo_error); |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
308 |
JNU_ReleaseStringPlatformChars(env, host, hostname); |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
309 |
return NULL; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
310 |
} else { |
38552
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
31059
diff
changeset
|
311 |
int i = 0, addressPreference = -1; |
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
31059
diff
changeset
|
312 |
int inetCount = 0, inet6Count = 0, inetIndex = 0, inet6Index = 0, originalIndex = 0; |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
313 |
struct addrinfo *itr, *last = NULL, *iterator = res; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
314 |
while (iterator != NULL) { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
315 |
int skip = 0; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
316 |
itr = resNew; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
317 |
while (itr != NULL) { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
318 |
if (iterator->ai_family == itr->ai_family && |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
319 |
iterator->ai_addrlen == itr->ai_addrlen) { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
320 |
if (itr->ai_family == AF_INET) { /* AF_INET */ |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
321 |
struct sockaddr_in *addr1, *addr2; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
322 |
addr1 = (struct sockaddr_in *)iterator->ai_addr; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
323 |
addr2 = (struct sockaddr_in *)itr->ai_addr; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
324 |
if (addr1->sin_addr.s_addr == |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
325 |
addr2->sin_addr.s_addr) { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
326 |
skip = 1; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
327 |
break; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
328 |
} |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
329 |
} else { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
330 |
int t; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
331 |
struct sockaddr_in6 *addr1, *addr2; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
332 |
addr1 = (struct sockaddr_in6 *)iterator->ai_addr; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
333 |
addr2 = (struct sockaddr_in6 *)itr->ai_addr; |
2 | 334 |
|
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
335 |
for (t = 0; t < 16; t++) { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
336 |
if (addr1->sin6_addr.s6_addr[t] != |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
337 |
addr2->sin6_addr.s6_addr[t]) { |
2 | 338 |
break; |
339 |
} |
|
340 |
} |
|
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
341 |
if (t < 16) { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
342 |
itr = itr->ai_next; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
343 |
continue; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
344 |
} else { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
345 |
skip = 1; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
346 |
break; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
347 |
} |
2 | 348 |
} |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
349 |
} else if (iterator->ai_family != AF_INET && |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
350 |
iterator->ai_family != AF_INET6) { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
351 |
/* we can't handle other family types */ |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
352 |
skip = 1; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
353 |
break; |
2 | 354 |
} |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
355 |
itr = itr->ai_next; |
2 | 356 |
} |
357 |
||
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
358 |
if (!skip) { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
359 |
struct addrinfo *next |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
360 |
= (struct addrinfo*) malloc(sizeof(struct addrinfo)); |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
361 |
if (!next) { |
13245
7ab3ef5b9520
7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents:
12688
diff
changeset
|
362 |
JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed"); |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
363 |
ret = NULL; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
364 |
goto cleanupAndReturn; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
365 |
} |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
366 |
memcpy(next, iterator, sizeof(struct addrinfo)); |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
367 |
next->ai_next = NULL; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
368 |
if (resNew == NULL) { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
369 |
resNew = next; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
370 |
} else { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
371 |
last->ai_next = next; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
372 |
} |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
373 |
last = next; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
374 |
i++; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
375 |
if (iterator->ai_family == AF_INET) { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
376 |
inetCount ++; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
377 |
} else if (iterator->ai_family == AF_INET6) { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
378 |
inet6Count ++; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
379 |
} |
2 | 380 |
} |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
381 |
iterator = iterator->ai_next; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
382 |
} |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
383 |
retLen = i; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
384 |
iterator = resNew; |
2 | 385 |
|
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22640
diff
changeset
|
386 |
ret = (*env)->NewObjectArray(env, retLen, ia_class, NULL); |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
387 |
|
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
388 |
if (IS_NULL(ret)) { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
389 |
/* we may have memory to free at the end of this */ |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
390 |
goto cleanupAndReturn; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
391 |
} |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
392 |
|
38552
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
31059
diff
changeset
|
393 |
addressPreference = (*env)->GetStaticIntField(env, ia_class, ia_preferIPv6AddressID); |
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
31059
diff
changeset
|
394 |
|
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
31059
diff
changeset
|
395 |
if (addressPreference == java_net_InetAddress_PREFER_IPV6_VALUE) { |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
396 |
/* AF_INET addresses will be offset by inet6Count */ |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
397 |
inetIndex = inet6Count; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
398 |
inet6Index = 0; |
38552
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
31059
diff
changeset
|
399 |
} else if (addressPreference == java_net_InetAddress_PREFER_IPV4_VALUE) { |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
400 |
/* AF_INET6 addresses will be offset by inetCount */ |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
401 |
inetIndex = 0; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
402 |
inet6Index = inetCount; |
38552
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
31059
diff
changeset
|
403 |
} else if (addressPreference == java_net_InetAddress_PREFER_SYSTEM_VALUE) { |
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
31059
diff
changeset
|
404 |
inetIndex = inet6Index = originalIndex = 0; |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
405 |
} |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
406 |
|
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
407 |
while (iterator != NULL) { |
22956 | 408 |
jboolean ret1; |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
409 |
if (iterator->ai_family == AF_INET) { |
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22640
diff
changeset
|
410 |
jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID); |
2 | 411 |
if (IS_NULL(iaObj)) { |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
412 |
ret = NULL; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
413 |
goto cleanupAndReturn; |
2 | 414 |
} |
16870 | 415 |
setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr)); |
416 |
setInetAddress_hostName(env, iaObj, host); |
|
38552
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
31059
diff
changeset
|
417 |
(*env)->SetObjectArrayElement(env, ret, (inetIndex | originalIndex), iaObj); |
2 | 418 |
inetIndex++; |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
419 |
} else if (iterator->ai_family == AF_INET6) { |
2 | 420 |
jint scope = 0; |
421 |
||
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22640
diff
changeset
|
422 |
jobject iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID); |
2 | 423 |
if (IS_NULL(iaObj)) { |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
424 |
ret = NULL; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
425 |
goto cleanupAndReturn; |
2 | 426 |
} |
20821 | 427 |
ret1 = setInet6Address_ipaddress(env, iaObj, (char *)&(((struct sockaddr_in6*)iterator->ai_addr)->sin6_addr)); |
22956 | 428 |
if (ret1 == JNI_FALSE) { |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
429 |
ret = NULL; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
430 |
goto cleanupAndReturn; |
2 | 431 |
} |
20821 | 432 |
|
2 | 433 |
scope = ((struct sockaddr_in6*)iterator->ai_addr)->sin6_scope_id; |
434 |
if (scope != 0) { /* zero is default value, no need to set */ |
|
20821 | 435 |
setInet6Address_scopeid(env, iaObj, scope); |
2 | 436 |
} |
16870 | 437 |
setInetAddress_hostName(env, iaObj, host); |
38552
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
31059
diff
changeset
|
438 |
(*env)->SetObjectArrayElement(env, ret, (inet6Index | originalIndex), iaObj); |
2 | 439 |
inet6Index++; |
440 |
} |
|
38552
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
31059
diff
changeset
|
441 |
if (addressPreference == java_net_InetAddress_PREFER_SYSTEM_VALUE) { |
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
31059
diff
changeset
|
442 |
originalIndex++; |
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
31059
diff
changeset
|
443 |
inetIndex = inet6Index = 0; |
ca398af91529
8016521: InetAddress should not always re-order addresses returned from name service
vtewari
parents:
31059
diff
changeset
|
444 |
} |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
445 |
iterator = iterator->ai_next; |
2 | 446 |
} |
447 |
} |
|
448 |
||
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
449 |
cleanupAndReturn: |
2 | 450 |
{ |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
451 |
struct addrinfo *iterator, *tmp; |
2 | 452 |
iterator = resNew; |
453 |
while (iterator != NULL) { |
|
454 |
tmp = iterator; |
|
455 |
iterator = iterator->ai_next; |
|
456 |
free(tmp); |
|
457 |
} |
|
458 |
JNU_ReleaseStringPlatformChars(env, host, hostname); |
|
459 |
} |
|
460 |
||
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
461 |
freeaddrinfo(res); |
2 | 462 |
#endif /* AF_INET6 */ |
463 |
||
464 |
return ret; |
|
465 |
} |
|
466 |
||
467 |
/* |
|
468 |
* Class: java_net_Inet6AddressImpl |
|
469 |
* Method: getHostByAddr |
|
470 |
* Signature: (I)Ljava/lang/String; |
|
471 |
*/ |
|
472 |
JNIEXPORT jstring JNICALL |
|
473 |
Java_java_net_Inet6AddressImpl_getHostByAddr(JNIEnv *env, jobject this, |
|
474 |
jbyteArray addrArray) { |
|
475 |
||
476 |
jstring ret = NULL; |
|
477 |
||
478 |
#ifdef AF_INET6 |
|
479 |
char host[NI_MAXHOST+1]; |
|
480 |
int error = 0; |
|
481 |
int len = 0; |
|
482 |
jbyte caddr[16]; |
|
483 |
||
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
484 |
struct sockaddr_in him4; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
485 |
struct sockaddr_in6 him6; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
486 |
struct sockaddr *sa; |
2 | 487 |
|
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
488 |
/* |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
489 |
* For IPv4 addresses construct a sockaddr_in structure. |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
490 |
*/ |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
491 |
if ((*env)->GetArrayLength(env, addrArray) == 4) { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
492 |
jint addr; |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
493 |
(*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr); |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
494 |
addr = ((caddr[0]<<24) & 0xff000000); |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
495 |
addr |= ((caddr[1] <<16) & 0xff0000); |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
496 |
addr |= ((caddr[2] <<8) & 0xff00); |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
497 |
addr |= (caddr[3] & 0xff); |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
498 |
memset((void *) &him4, 0, sizeof(him4)); |
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
499 |
him4.sin_addr.s_addr = htonl(addr); |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
500 |
him4.sin_family = AF_INET; |
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
501 |
sa = (struct sockaddr *)&him4; |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
502 |
len = sizeof(him4); |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
503 |
} else { |
2 | 504 |
/* |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
505 |
* For IPv6 address construct a sockaddr_in6 structure. |
2 | 506 |
*/ |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
507 |
(*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr); |
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
508 |
memset((void *)&him6, 0, sizeof(him6)); |
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
509 |
memcpy((void *)&(him6.sin6_addr), caddr, sizeof(struct in6_addr)); |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
510 |
him6.sin6_family = AF_INET6; |
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
511 |
sa = (struct sockaddr *)&him6; |
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
512 |
len = sizeof(him6); |
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
513 |
} |
2 | 514 |
|
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41380
diff
changeset
|
515 |
error = getnameinfo(sa, len, host, NI_MAXHOST, NULL, 0, NI_NAMEREQD); |
2 | 516 |
|
11032
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
517 |
if (!error) { |
235588f77727
7112670: Inet4AddressImpl should use getaddrinfo/getnameinfo
ngmr
parents:
9035
diff
changeset
|
518 |
ret = (*env)->NewStringUTF(env, host); |
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22640
diff
changeset
|
519 |
CHECK_NULL_RETURN(ret, NULL); |
2 | 520 |
} |
521 |
#endif /* AF_INET6 */ |
|
522 |
||
523 |
if (ret == NULL) { |
|
524 |
JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", NULL); |
|
525 |
} |
|
526 |
||
527 |
return ret; |
|
528 |
} |
|
529 |
||
530 |
#define SET_NONBLOCKING(fd) { \ |
|
531 |
int flags = fcntl(fd, F_GETFL); \ |
|
532 |
flags |= O_NONBLOCK; \ |
|
533 |
fcntl(fd, F_SETFL, flags); \ |
|
534 |
} |
|
535 |
||
536 |
#ifdef AF_INET6 |
|
537 |
static jboolean |
|
538 |
ping6(JNIEnv *env, jint fd, struct sockaddr_in6* him, jint timeout, |
|
539 |
struct sockaddr_in6* netif, jint ttl) { |
|
540 |
jint size; |
|
2056
115e09b7a004
6799040: Portability issues in src/solaris/native/java/net/Inet4AddressImpl.c
chegar
parents:
910
diff
changeset
|
541 |
jint n; |
115e09b7a004
6799040: Portability issues in src/solaris/native/java/net/Inet4AddressImpl.c
chegar
parents:
910
diff
changeset
|
542 |
socklen_t len; |
2 | 543 |
char sendbuf[1500]; |
544 |
unsigned char recvbuf[1500]; |
|
545 |
struct icmp6_hdr *icmp6; |
|
546 |
struct sockaddr_in6 sa_recv; |
|
547 |
jbyte *caddr, *recv_caddr; |
|
548 |
jchar pid; |
|
549 |
jint tmout2, seq = 1; |
|
550 |
struct timeval tv; |
|
551 |
size_t plen; |
|
552 |
||
553 |
#ifdef __linux__ |
|
554 |
{ |
|
555 |
int csum_offset; |
|
556 |
/** |
|
557 |
* For some strange reason, the linux kernel won't calculate the |
|
558 |
* checksum of ICMPv6 packets unless you set this socket option |
|
559 |
*/ |
|
560 |
csum_offset = 2; |
|
561 |
setsockopt(fd, SOL_RAW, IPV6_CHECKSUM, &csum_offset, sizeof(int)); |
|
562 |
} |
|
563 |
#endif |
|
564 |
||
565 |
caddr = (jbyte *)&(him->sin6_addr); |
|
566 |
||
567 |
/* icmp_id is a 16 bit data type, therefore down cast the pid */ |
|
568 |
pid = (jchar)getpid(); |
|
569 |
size = 60*1024; |
|
570 |
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)); |
|
571 |
if (ttl > 0) { |
|
572 |
setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)); |
|
573 |
} |
|
574 |
if (netif != NULL) { |
|
575 |
if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in6)) <0) { |
|
576 |
NET_ThrowNew(env, errno, "Can't bind socket"); |
|
577 |
close(fd); |
|
578 |
return JNI_FALSE; |
|
579 |
} |
|
580 |
} |
|
581 |
SET_NONBLOCKING(fd); |
|
582 |
||
583 |
do { |
|
584 |
icmp6 = (struct icmp6_hdr *) sendbuf; |
|
585 |
icmp6->icmp6_type = ICMP6_ECHO_REQUEST; |
|
586 |
icmp6->icmp6_code = 0; |
|
587 |
/* let's tag the ECHO packet with our pid so we can identify it */ |
|
588 |
icmp6->icmp6_id = htons(pid); |
|
589 |
icmp6->icmp6_seq = htons(seq); |
|
590 |
seq++; |
|
591 |
icmp6->icmp6_cksum = 0; |
|
592 |
gettimeofday(&tv, NULL); |
|
593 |
memcpy(sendbuf + sizeof(struct icmp6_hdr), &tv, sizeof(tv)); |
|
594 |
plen = sizeof(struct icmp6_hdr) + sizeof(tv); |
|
595 |
n = sendto(fd, sendbuf, plen, 0, (struct sockaddr*) him, sizeof(struct sockaddr_in6)); |
|
596 |
if (n < 0 && errno != EINPROGRESS) { |
|
7024
25bd21af28bf
6947677: InetAddress.isReachable() throws "java.net.SocketException:Invalid argument" on Linux if run as root
chegar
parents:
6670
diff
changeset
|
597 |
#ifdef __linux__ |
11278
ce6c46d16022
7118907: InetAddress.isReachable() should return false if sendto fails with EHOSTUNREACH
ngmr
parents:
11033
diff
changeset
|
598 |
if (errno != EINVAL && errno != EHOSTUNREACH) |
7024
25bd21af28bf
6947677: InetAddress.isReachable() throws "java.net.SocketException:Invalid argument" on Linux if run as root
chegar
parents:
6670
diff
changeset
|
599 |
/* |
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13245
diff
changeset
|
600 |
* On some Linux versions, when a socket is bound to the |
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13245
diff
changeset
|
601 |
* loopback interface, sendto will fail and errno will be |
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13245
diff
changeset
|
602 |
* set to EINVAL or EHOSTUNREACH. |
11278
ce6c46d16022
7118907: InetAddress.isReachable() should return false if sendto fails with EHOSTUNREACH
ngmr
parents:
11033
diff
changeset
|
603 |
* When that happens, don't throw an exception, just return false. |
7024
25bd21af28bf
6947677: InetAddress.isReachable() throws "java.net.SocketException:Invalid argument" on Linux if run as root
chegar
parents:
6670
diff
changeset
|
604 |
*/ |
25bd21af28bf
6947677: InetAddress.isReachable() throws "java.net.SocketException:Invalid argument" on Linux if run as root
chegar
parents:
6670
diff
changeset
|
605 |
#endif /*__linux__ */ |
2 | 606 |
NET_ThrowNew(env, errno, "Can't send ICMP packet"); |
7024
25bd21af28bf
6947677: InetAddress.isReachable() throws "java.net.SocketException:Invalid argument" on Linux if run as root
chegar
parents:
6670
diff
changeset
|
607 |
close(fd); |
2 | 608 |
return JNI_FALSE; |
609 |
} |
|
610 |
||
611 |
tmout2 = timeout > 1000 ? 1000 : timeout; |
|
612 |
do { |
|
613 |
tmout2 = NET_Wait(env, fd, NET_WAIT_READ, tmout2); |
|
614 |
||
615 |
if (tmout2 >= 0) { |
|
616 |
len = sizeof(sa_recv); |
|
617 |
n = recvfrom(fd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr*) &sa_recv, &len); |
|
618 |
icmp6 = (struct icmp6_hdr *) (recvbuf); |
|
619 |
recv_caddr = (jbyte *)&(sa_recv.sin6_addr); |
|
620 |
/* |
|
621 |
* We did receive something, but is it what we were expecting? |
|
622 |
* I.E.: An ICMP6_ECHO_REPLY packet with the proper PID and |
|
623 |
* from the host that we are trying to determine is reachable. |
|
624 |
*/ |
|
625 |
if (n >= 8 && icmp6->icmp6_type == ICMP6_ECHO_REPLY && |
|
12688
f15fd32c975e
7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
12673
diff
changeset
|
626 |
(ntohs(icmp6->icmp6_id) == pid)) { |
f15fd32c975e
7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
12673
diff
changeset
|
627 |
if (NET_IsEqual(caddr, recv_caddr)) { |
f15fd32c975e
7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
12673
diff
changeset
|
628 |
close(fd); |
f15fd32c975e
7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
12673
diff
changeset
|
629 |
return JNI_TRUE; |
f15fd32c975e
7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
12673
diff
changeset
|
630 |
} |
f15fd32c975e
7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
12673
diff
changeset
|
631 |
if (NET_IsZeroAddr(caddr)) { |
f15fd32c975e
7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
12673
diff
changeset
|
632 |
close(fd); |
f15fd32c975e
7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
12673
diff
changeset
|
633 |
return JNI_TRUE; |
f15fd32c975e
7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
12673
diff
changeset
|
634 |
} |
2 | 635 |
} |
636 |
} |
|
637 |
} while (tmout2 > 0); |
|
638 |
timeout -= 1000; |
|
639 |
} while (timeout > 0); |
|
640 |
close(fd); |
|
641 |
return JNI_FALSE; |
|
642 |
} |
|
643 |
#endif /* AF_INET6 */ |
|
644 |
||
645 |
/* |
|
646 |
* Class: java_net_Inet6AddressImpl |
|
647 |
* Method: isReachable0 |
|
648 |
* Signature: ([bII[bI)Z |
|
649 |
*/ |
|
650 |
JNIEXPORT jboolean JNICALL |
|
651 |
Java_java_net_Inet6AddressImpl_isReachable0(JNIEnv *env, jobject this, |
|
652 |
jbyteArray addrArray, |
|
653 |
jint scope, |
|
654 |
jint timeout, |
|
655 |
jbyteArray ifArray, |
|
656 |
jint ttl, jint if_scope) { |
|
657 |
#ifdef AF_INET6 |
|
658 |
jbyte caddr[16]; |
|
659 |
jint fd, sz; |
|
660 |
struct sockaddr_in6 him6; |
|
661 |
struct sockaddr_in6 inf6; |
|
662 |
struct sockaddr_in6* netif = NULL; |
|
663 |
int len = 0; |
|
664 |
int connect_rv = -1; |
|
665 |
||
666 |
/* |
|
667 |
* If IPv6 is not enable, then we can't reach an IPv6 address, can we? |
|
668 |
*/ |
|
669 |
if (!ipv6_available()) { |
|
670 |
return JNI_FALSE; |
|
671 |
} |
|
672 |
/* |
|
673 |
* If it's an IPv4 address, ICMP won't work with IPv4 mapped address, |
|
674 |
* therefore, let's delegate to the Inet4Address method. |
|
675 |
*/ |
|
676 |
sz = (*env)->GetArrayLength(env, addrArray); |
|
677 |
if (sz == 4) { |
|
678 |
return Java_java_net_Inet4AddressImpl_isReachable0(env, this, |
|
679 |
addrArray, |
|
680 |
timeout, |
|
681 |
ifArray, ttl); |
|
682 |
} |
|
683 |
||
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
684 |
memset((void *) caddr, 0, 16); |
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
685 |
memset((void *) &him6, 0, sizeof(him6)); |
2 | 686 |
(*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr); |
687 |
memcpy((void *)&(him6.sin6_addr), caddr, sizeof(struct in6_addr) ); |
|
688 |
him6.sin6_family = AF_INET6; |
|
689 |
#ifdef __linux__ |
|
690 |
if (scope > 0) |
|
691 |
him6.sin6_scope_id = scope; |
|
692 |
else |
|
693 |
him6.sin6_scope_id = getDefaultIPv6Interface( &(him6.sin6_addr)); |
|
694 |
len = sizeof(struct sockaddr_in6); |
|
695 |
#else |
|
696 |
if (scope > 0) |
|
697 |
him6.sin6_scope_id = scope; |
|
698 |
len = sizeof(struct sockaddr_in6); |
|
699 |
#endif |
|
700 |
/* |
|
701 |
* If a network interface was specified, let's create the address |
|
702 |
* for it. |
|
703 |
*/ |
|
704 |
if (!(IS_NULL(ifArray))) { |
|
6670
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
705 |
memset((void *) caddr, 0, 16); |
ae13809f3ce7
6981157: Improve UnknownHostException with EAI error details and other cleanups
martin
parents:
5506
diff
changeset
|
706 |
memset((void *) &inf6, 0, sizeof(inf6)); |
2 | 707 |
(*env)->GetByteArrayRegion(env, ifArray, 0, 16, caddr); |
708 |
memcpy((void *)&(inf6.sin6_addr), caddr, sizeof(struct in6_addr) ); |
|
709 |
inf6.sin6_family = AF_INET6; |
|
710 |
inf6.sin6_scope_id = if_scope; |
|
711 |
netif = &inf6; |
|
712 |
} |
|
713 |
/* |
|
714 |
* If we can create a RAW socket, then when can use the ICMP ECHO_REQUEST |
|
715 |
* otherwise we'll try a tcp socket to the Echo port (7). |
|
716 |
* Note that this is empiric, and not connecting could mean it's blocked |
|
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13245
diff
changeset
|
717 |
* or the echo service has been disabled. |
2 | 718 |
*/ |
719 |
||
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22956
diff
changeset
|
720 |
fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6); |
2 | 721 |
|
722 |
if (fd != -1) { /* Good to go, let's do a ping */ |
|
723 |
return ping6(env, fd, &him6, timeout, netif, ttl); |
|
724 |
} |
|
725 |
||
726 |
/* No good, let's fall back on TCP */ |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22956
diff
changeset
|
727 |
fd = socket(AF_INET6, SOCK_STREAM, 0); |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22956
diff
changeset
|
728 |
if (fd == -1) { |
2 | 729 |
/* note: if you run out of fds, you may not be able to load |
730 |
* the exception class, and get a NoClassDefFoundError |
|
731 |
* instead. |
|
732 |
*/ |
|
733 |
NET_ThrowNew(env, errno, "Can't create socket"); |
|
734 |
return JNI_FALSE; |
|
735 |
} |
|
736 |
if (ttl > 0) { |
|
737 |
setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)); |
|
738 |
} |
|
739 |
||
740 |
/* |
|
741 |
* A network interface was specified, so let's bind to it. |
|
742 |
*/ |
|
743 |
if (netif != NULL) { |
|
744 |
if (bind(fd, (struct sockaddr*)netif, sizeof(struct sockaddr_in6)) <0) { |
|
745 |
NET_ThrowNew(env, errno, "Can't bind socket"); |
|
746 |
close(fd); |
|
747 |
return JNI_FALSE; |
|
748 |
} |
|
749 |
} |
|
750 |
SET_NONBLOCKING(fd); |
|
751 |
||
752 |
him6.sin6_port = htons((short) 7); /* Echo port */ |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22956
diff
changeset
|
753 |
connect_rv = NET_Connect(fd, (struct sockaddr *)&him6, len); |
2 | 754 |
|
755 |
/** |
|
756 |
* connection established or refused immediately, either way it means |
|
757 |
* we were able to reach the host! |
|
758 |
*/ |
|
759 |
if (connect_rv == 0 || errno == ECONNREFUSED) { |
|
760 |
close(fd); |
|
761 |
return JNI_TRUE; |
|
762 |
} else { |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22956
diff
changeset
|
763 |
socklen_t optlen = (socklen_t)sizeof(connect_rv); |
2 | 764 |
|
765 |
switch (errno) { |
|
766 |
case ENETUNREACH: /* Network Unreachable */ |
|
767 |
case EAFNOSUPPORT: /* Address Family not supported */ |
|
768 |
case EADDRNOTAVAIL: /* address is not available on the remote machine */ |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
38563
diff
changeset
|
769 |
#if defined(__linux__) || defined(_AIX) |
2 | 770 |
case EINVAL: |
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
38563
diff
changeset
|
771 |
case EHOSTUNREACH: /* No route to host */ |
2 | 772 |
/* |
13999
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13245
diff
changeset
|
773 |
* On some Linux versions, when a socket is bound to the |
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13245
diff
changeset
|
774 |
* loopback interface, connect will fail and errno will |
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13245
diff
changeset
|
775 |
* be set to EINVAL or EHOSTUNREACH. When that happens, |
f6a2ce6a3e40
7193520: Removed references to Linux kernel version 2.2
chegar
parents:
13245
diff
changeset
|
776 |
* don't throw an exception, just return false. |
2 | 777 |
*/ |
778 |
#endif /* __linux__ */ |
|
779 |
close(fd); |
|
780 |
return JNI_FALSE; |
|
781 |
} |
|
782 |
||
783 |
if (errno != EINPROGRESS) { |
|
784 |
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException", |
|
785 |
"connect failed"); |
|
786 |
close(fd); |
|
787 |
return JNI_FALSE; |
|
788 |
} |
|
789 |
||
790 |
timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout); |
|
791 |
||
792 |
if (timeout >= 0) { |
|
793 |
/* has connection been established */ |
|
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22956
diff
changeset
|
794 |
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&connect_rv, |
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
22956
diff
changeset
|
795 |
&optlen) <0) { |
2 | 796 |
connect_rv = errno; |
797 |
} |
|
41380
c27cf95dd7e6
8167295: Further cleanup to the native parts of libnet/libnio
clanger
parents:
38563
diff
changeset
|
798 |
if (connect_rv == 0 || connect_rv == ECONNREFUSED) { |
2 | 799 |
close(fd); |
800 |
return JNI_TRUE; |
|
801 |
} |
|
802 |
} |
|
803 |
close(fd); |
|
804 |
return JNI_FALSE; |
|
805 |
} |
|
806 |
#else /* AF_INET6 */ |
|
807 |
return JNI_FALSE; |
|
808 |
#endif /* AF_INET6 */ |
|
809 |
} |