38
39 #ifdef _ALLBSD_SOURCE
40 #include <unistd.h>
41 #include <sys/param.h>
42 #endif
43
44 #include "jvm.h"
45 #include "jni_util.h"
46 #include "net_util.h"
47
48 #include "java_net_Inet4AddressImpl.h"
49
50 #if defined(__GLIBC__) || (defined(__FreeBSD__) && (__FreeBSD_version >= 601104))
51 #define HAS_GLIBC_GETHOSTBY_R 1
52 #endif
53
54 static jclass ni_iacls;
55 static jclass ni_ia4cls;
56 static jmethodID ni_ia4ctrID;
57
58 static void initializeInetClasses(JNIEnv *env)
59 {
60 static int initialized = 0;
61 if (!initialized) {
62 ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
63 ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
64 ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
65 ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
66 ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
67 initialized = 1;
68 }
69 }
70
71
72 #if defined(_ALLBSD_SOURCE) && !defined(HAS_GLIBC_GETHOSTBY_R)
73 extern jobjectArray lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6);
74
75 /* Use getaddrinfo(3), which is thread safe */
76 /************************************************************************
77 * Inet4AddressImpl
78 */
79
80 /*
81 * Class: java_net_Inet4AddressImpl
82 * Method: getLocalHostName
83 * Signature: ()Ljava/lang/String;
84 */
85 JNIEXPORT jstring JNICALL
86 Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
87 char hostname[NI_MAXHOST+1];
88
124 * code only works for addresses of type INET. The translation
125 * of %d.%d.%d.%d to an address (int) occurs in java now, so the
126 * String "host" shouldn't *ever* be a %d.%d.%d.%d string
127 *
128 * Class: java_net_Inet4AddressImpl
129 * Method: lookupAllHostAddr
130 * Signature: (Ljava/lang/String;)[[B
131 */
132
133 JNIEXPORT jobjectArray JNICALL
134 Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
135 jstring host) {
136 const char *hostname;
137 jobject name;
138 jobjectArray ret = 0;
139 int retLen = 0;
140
141 int error=0;
142 struct addrinfo hints, *res, *resNew = NULL;
143
144 initializeInetClasses(env);
145
146 if (IS_NULL(host)) {
147 JNU_ThrowNullPointerException(env, "host is null");
148 return 0;
149 }
150 hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
151 CHECK_NULL_RETURN(hostname, NULL);
152
153 memset(&hints, 0, sizeof(hints));
154 hints.ai_flags = AI_CANONNAME;
155 hints.ai_family = AF_INET;
156
157 /*
158 * Workaround for Solaris bug 4160367 - if a hostname contains a
159 * white space then 0.0.0.0 is returned
160 */
161 if (isspace((unsigned char)hostname[0])) {
162 JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
163 (char *)hostname);
164 JNU_ReleaseStringPlatformChars(env, host, hostname);
383 /*
384 * Find an internet address for a given hostname. Note that this
385 * code only works for addresses of type INET. The translation
386 * of %d.%d.%d.%d to an address (int) occurs in java now, so the
387 * String "host" shouldn't *ever* be a %d.%d.%d.%d string
388 *
389 * Class: java_net_Inet4AddressImpl
390 * Method: lookupAllHostAddr
391 * Signature: (Ljava/lang/String;)[[B
392 */
393
394 JNIEXPORT jobjectArray JNICALL
395 Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
396 jstring host) {
397 const char *hostname;
398 jobjectArray ret = 0;
399 int retLen = 0;
400 int error = 0;
401 struct addrinfo hints, *res, *resNew = NULL;
402
403 initializeInetClasses(env);
404
405 if (IS_NULL(host)) {
406 JNU_ThrowNullPointerException(env, "host is null");
407 return 0;
408 }
409 hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
410 CHECK_NULL_RETURN(hostname, NULL);
411
412 /* Try once, with our static buffer. */
413 memset(&hints, 0, sizeof(hints));
414 hints.ai_flags = AI_CANONNAME;
415 hints.ai_family = AF_INET;
416
417 #ifdef __solaris__
418 /*
419 * Workaround for Solaris bug 4160367 - if a hostname contains a
420 * white space then 0.0.0.0 is returned
421 */
422 if (isspace((unsigned char)hostname[0])) {
423 JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
|
38
39 #ifdef _ALLBSD_SOURCE
40 #include <unistd.h>
41 #include <sys/param.h>
42 #endif
43
44 #include "jvm.h"
45 #include "jni_util.h"
46 #include "net_util.h"
47
48 #include "java_net_Inet4AddressImpl.h"
49
50 #if defined(__GLIBC__) || (defined(__FreeBSD__) && (__FreeBSD_version >= 601104))
51 #define HAS_GLIBC_GETHOSTBY_R 1
52 #endif
53
54 static jclass ni_iacls;
55 static jclass ni_ia4cls;
56 static jmethodID ni_ia4ctrID;
57
58 static jboolean initializeInetClasses(JNIEnv *env)
59 {
60 static int initialized = 0;
61 if (!initialized) {
62 ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
63 CHECK_NULL_RETURN(ni_iacls, JNI_FALSE);
64 ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
65 CHECK_NULL_RETURN(ni_iacls, JNI_FALSE);
66 ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
67 CHECK_NULL_RETURN(ni_ia4cls, JNI_FALSE);
68 ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
69 CHECK_NULL_RETURN(ni_ia4cls, JNI_FALSE);
70 ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
71 CHECK_NULL_RETURN(ni_ia4ctrID, JNI_FALSE);
72 initialized = 1;
73 }
74 return JNI_TRUE;
75 }
76
77
78 #if defined(_ALLBSD_SOURCE) && !defined(HAS_GLIBC_GETHOSTBY_R)
79 extern jobjectArray lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6);
80
81 /* Use getaddrinfo(3), which is thread safe */
82 /************************************************************************
83 * Inet4AddressImpl
84 */
85
86 /*
87 * Class: java_net_Inet4AddressImpl
88 * Method: getLocalHostName
89 * Signature: ()Ljava/lang/String;
90 */
91 JNIEXPORT jstring JNICALL
92 Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
93 char hostname[NI_MAXHOST+1];
94
130 * code only works for addresses of type INET. The translation
131 * of %d.%d.%d.%d to an address (int) occurs in java now, so the
132 * String "host" shouldn't *ever* be a %d.%d.%d.%d string
133 *
134 * Class: java_net_Inet4AddressImpl
135 * Method: lookupAllHostAddr
136 * Signature: (Ljava/lang/String;)[[B
137 */
138
139 JNIEXPORT jobjectArray JNICALL
140 Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
141 jstring host) {
142 const char *hostname;
143 jobject name;
144 jobjectArray ret = 0;
145 int retLen = 0;
146
147 int error=0;
148 struct addrinfo hints, *res, *resNew = NULL;
149
150 if (!initializeInetClasses(env))
151 return NULL;
152
153 if (IS_NULL(host)) {
154 JNU_ThrowNullPointerException(env, "host is null");
155 return 0;
156 }
157 hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
158 CHECK_NULL_RETURN(hostname, NULL);
159
160 memset(&hints, 0, sizeof(hints));
161 hints.ai_flags = AI_CANONNAME;
162 hints.ai_family = AF_INET;
163
164 /*
165 * Workaround for Solaris bug 4160367 - if a hostname contains a
166 * white space then 0.0.0.0 is returned
167 */
168 if (isspace((unsigned char)hostname[0])) {
169 JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
170 (char *)hostname);
171 JNU_ReleaseStringPlatformChars(env, host, hostname);
390 /*
391 * Find an internet address for a given hostname. Note that this
392 * code only works for addresses of type INET. The translation
393 * of %d.%d.%d.%d to an address (int) occurs in java now, so the
394 * String "host" shouldn't *ever* be a %d.%d.%d.%d string
395 *
396 * Class: java_net_Inet4AddressImpl
397 * Method: lookupAllHostAddr
398 * Signature: (Ljava/lang/String;)[[B
399 */
400
401 JNIEXPORT jobjectArray JNICALL
402 Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
403 jstring host) {
404 const char *hostname;
405 jobjectArray ret = 0;
406 int retLen = 0;
407 int error = 0;
408 struct addrinfo hints, *res, *resNew = NULL;
409
410 if (!initializeInetClasses(env))
411 return NULL;
412
413 if (IS_NULL(host)) {
414 JNU_ThrowNullPointerException(env, "host is null");
415 return 0;
416 }
417 hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
418 CHECK_NULL_RETURN(hostname, NULL);
419
420 /* Try once, with our static buffer. */
421 memset(&hints, 0, sizeof(hints));
422 hints.ai_flags = AI_CANONNAME;
423 hints.ai_family = AF_INET;
424
425 #ifdef __solaris__
426 /*
427 * Workaround for Solaris bug 4160367 - if a hostname contains a
428 * white space then 0.0.0.0 is returned
429 */
430 if (isspace((unsigned char)hostname[0])) {
431 JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException",
|