107 NI_NAMEREQD);
108
109 /* if getnameinfo fails hostname is still the value
110 from gethostname */
111
112 freeaddrinfo(res);
113 }
114 #endif /* AF_INET6 */
115 #endif /* __linux__ || _ALLBSD_SOURCE */
116 }
117 return (*env)->NewStringUTF(env, hostname);
118 }
119
120 static jclass ni_iacls;
121 static jclass ni_ia4cls;
122 static jclass ni_ia6cls;
123 static jmethodID ni_ia4ctrID;
124 static jmethodID ni_ia6ctrID;
125 static jboolean preferIPv6Address;
126
127 static void initializeInetClasses(JNIEnv *env)
128 {
129 jfieldID ni_preferIPv6AddressID;
130 static int initialized = 0;
131 if (!initialized) {
132 ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
133 ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
134 ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
135 ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
136 ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
137 ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
138 ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
139 ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
140 ni_preferIPv6AddressID =
141 (*env)->GetStaticFieldID(env, ni_iacls, "preferIPv6Address", "Z");
142 preferIPv6Address =
143 (*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID);
144 initialized = 1;
145 }
146 }
147
148 #ifdef MACOSX
149 /* also called from Inet4AddressImpl.c */
150 __private_extern__ jobjectArray
151 lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
152 {
153 jobjectArray result = NULL;
154 char myhostname[NI_MAXHOST+1];
155 struct ifaddrs *ifa = NULL;
156 int familyOrder = 0;
157 int count = 0, i, j;
158 int addrs4 = 0, addrs6 = 0, numV4Loopbacks = 0, numV6Loopbacks = 0;
159 jboolean includeLoopback = JNI_FALSE;
160 jobject name;
161
162 // Make sure static variables we need are set.
163 initializeInetClasses(env);
164
165 /* If the requested name matches this host's hostname, return IP addresses
166 * from all attached interfaces. (#2844683 et al) This prevents undesired
167 * PPP dialup, but may return addresses that don't actually correspond to
168 * the name (if the name actually matches something in DNS etc.
169 */
170 myhostname[0] = '\0';
171 if (JVM_GetHostName(myhostname, NI_MAXHOST)) {
172 /* Something went wrong, maybe networking is not setup? */
173 return NULL;
174 }
175 myhostname[NI_MAXHOST] = '\0';
176
177 if (strcmp(myhostname, hostname) != 0) {
178 // Non-self lookup
179 return NULL;
180 }
181
182 if (getifaddrs(&ifa) != 0) {
183 NET_ThrowNew(env, errno, "Can't get local interface addresses");
269 * of %d.%d.%d.%d to an address (int) occurs in java now, so the
270 * String "host" shouldn't *ever* be a %d.%d.%d.%d string
271 *
272 * Class: java_net_Inet6AddressImpl
273 * Method: lookupAllHostAddr
274 * Signature: (Ljava/lang/String;)[[B
275 */
276
277 JNIEXPORT jobjectArray JNICALL
278 Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
279 jstring host) {
280 const char *hostname;
281 jobjectArray ret = 0;
282 int retLen = 0;
283
284 int error=0;
285 #ifdef AF_INET6
286 struct addrinfo hints, *res, *resNew = NULL;
287 #endif /* AF_INET6 */
288
289 initializeInetClasses(env);
290
291 if (IS_NULL(host)) {
292 JNU_ThrowNullPointerException(env, "host is null");
293 return 0;
294 }
295 hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
296 CHECK_NULL_RETURN(hostname, NULL);
297
298 #ifdef MACOSX
299 /*
300 * If we're looking up the local machine, attempt to get the address
301 * from getifaddrs. This ensures we get an IPv6 address for the local
302 * machine.
303 */
304 ret = lookupIfLocalhost(env, hostname, JNI_TRUE);
305 if (ret != NULL || (*env)->ExceptionCheck(env)) {
306 JNU_ReleaseStringPlatformChars(env, host, hostname);
307 return ret;
308 }
309 #endif
|
107 NI_NAMEREQD);
108
109 /* if getnameinfo fails hostname is still the value
110 from gethostname */
111
112 freeaddrinfo(res);
113 }
114 #endif /* AF_INET6 */
115 #endif /* __linux__ || _ALLBSD_SOURCE */
116 }
117 return (*env)->NewStringUTF(env, hostname);
118 }
119
120 static jclass ni_iacls;
121 static jclass ni_ia4cls;
122 static jclass ni_ia6cls;
123 static jmethodID ni_ia4ctrID;
124 static jmethodID ni_ia6ctrID;
125 static jboolean preferIPv6Address;
126
127 static jboolean initializeInetClasses(JNIEnv *env)
128 {
129 jfieldID ni_preferIPv6AddressID;
130 static int initialized = 0;
131 if (!initialized) {
132 ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
133 CHECK_NULL_RETURN(ni_iacls, JNI_FALSE);
134 ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
135 CHECK_NULL_RETURN(ni_iacls, JNI_FALSE);
136 ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
137 CHECK_NULL_RETURN(ni_ia4cls, JNI_FALSE);
138 ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
139 CHECK_NULL_RETURN(ni_ia4cls, JNI_FALSE);
140 ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
141 CHECK_NULL_RETURN(ni_ia6cls, JNI_FALSE);
142 ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
143 CHECK_NULL_RETURN(ni_ia6cls, JNI_FALSE);
144 ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
145 CHECK_NULL_RETURN(ni_ia4ctrID, JNI_FALSE);
146 ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
147 CHECK_NULL_RETURN(ni_ia6ctrID, JNI_FALSE);
148 ni_preferIPv6AddressID =
149 (*env)->GetStaticFieldID(env, ni_iacls, "preferIPv6Address", "Z");
150 CHECK_NULL_RETURN(ni_preferIPv6AddressID, JNI_FALSE);
151 preferIPv6Address =
152 (*env)->GetStaticBooleanField(env, ia_class, ia_preferIPv6AddressID);
153 initialized = 1;
154 }
155 return JNI_TRUE;
156 }
157
158 #ifdef MACOSX
159 /* also called from Inet4AddressImpl.c */
160 __private_extern__ jobjectArray
161 lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
162 {
163 jobjectArray result = NULL;
164 char myhostname[NI_MAXHOST+1];
165 struct ifaddrs *ifa = NULL;
166 int familyOrder = 0;
167 int count = 0, i, j;
168 int addrs4 = 0, addrs6 = 0, numV4Loopbacks = 0, numV6Loopbacks = 0;
169 jboolean includeLoopback = JNI_FALSE;
170 jobject name;
171
172 // Make sure static variables we need are set.
173 if (!initializeInetClasses(env))
174 return NULL;
175
176 /* If the requested name matches this host's hostname, return IP addresses
177 * from all attached interfaces. (#2844683 et al) This prevents undesired
178 * PPP dialup, but may return addresses that don't actually correspond to
179 * the name (if the name actually matches something in DNS etc.
180 */
181 myhostname[0] = '\0';
182 if (JVM_GetHostName(myhostname, NI_MAXHOST)) {
183 /* Something went wrong, maybe networking is not setup? */
184 return NULL;
185 }
186 myhostname[NI_MAXHOST] = '\0';
187
188 if (strcmp(myhostname, hostname) != 0) {
189 // Non-self lookup
190 return NULL;
191 }
192
193 if (getifaddrs(&ifa) != 0) {
194 NET_ThrowNew(env, errno, "Can't get local interface addresses");
280 * of %d.%d.%d.%d to an address (int) occurs in java now, so the
281 * String "host" shouldn't *ever* be a %d.%d.%d.%d string
282 *
283 * Class: java_net_Inet6AddressImpl
284 * Method: lookupAllHostAddr
285 * Signature: (Ljava/lang/String;)[[B
286 */
287
288 JNIEXPORT jobjectArray JNICALL
289 Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
290 jstring host) {
291 const char *hostname;
292 jobjectArray ret = 0;
293 int retLen = 0;
294
295 int error=0;
296 #ifdef AF_INET6
297 struct addrinfo hints, *res, *resNew = NULL;
298 #endif /* AF_INET6 */
299
300 if (!initializeInetClasses(env))
301 return NULL;
302
303 if (IS_NULL(host)) {
304 JNU_ThrowNullPointerException(env, "host is null");
305 return 0;
306 }
307 hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
308 CHECK_NULL_RETURN(hostname, NULL);
309
310 #ifdef MACOSX
311 /*
312 * If we're looking up the local machine, attempt to get the address
313 * from getifaddrs. This ensures we get an IPv6 address for the local
314 * machine.
315 */
316 ret = lookupIfLocalhost(env, hostname, JNI_TRUE);
317 if (ret != NULL || (*env)->ExceptionCheck(env)) {
318 JNU_ReleaseStringPlatformChars(env, host, hostname);
319 return ret;
320 }
321 #endif
|