39 |
39 |
40 #if defined(MACOSX) |
40 #if defined(MACOSX) |
41 extern jobjectArray lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6); |
41 extern jobjectArray lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6); |
42 #endif |
42 #endif |
43 |
43 |
44 /* the initial size of our hostent buffers */ |
|
45 #ifndef NI_MAXHOST |
|
46 #define NI_MAXHOST 1025 |
|
47 #endif |
|
48 |
|
49 #define SET_NONBLOCKING(fd) { \ |
44 #define SET_NONBLOCKING(fd) { \ |
50 int flags = fcntl(fd, F_GETFL); \ |
45 int flags = fcntl(fd, F_GETFL); \ |
51 flags |= O_NONBLOCK; \ |
46 flags |= O_NONBLOCK; \ |
52 fcntl(fd, F_SETFL, flags); \ |
47 fcntl(fd, F_SETFL, flags); \ |
53 } |
48 } |
64 JNIEXPORT jstring JNICALL |
59 JNIEXPORT jstring JNICALL |
65 Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) { |
60 Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) { |
66 char hostname[NI_MAXHOST + 1]; |
61 char hostname[NI_MAXHOST + 1]; |
67 |
62 |
68 hostname[0] = '\0'; |
63 hostname[0] = '\0'; |
69 if (gethostname(hostname, NI_MAXHOST) != 0) { |
64 if (gethostname(hostname, sizeof(hostname)) != 0) { |
70 strcpy(hostname, "localhost"); |
65 strcpy(hostname, "localhost"); |
|
66 } else { |
71 #if defined(__solaris__) |
67 #if defined(__solaris__) |
72 } else { |
|
73 // try to resolve hostname via nameservice |
68 // try to resolve hostname via nameservice |
74 // if it is known but getnameinfo fails, hostname will still be the |
69 // if it is known but getnameinfo fails, hostname will still be the |
75 // value from gethostname |
70 // value from gethostname |
76 struct addrinfo hints, *res; |
71 struct addrinfo hints, *res; |
77 |
72 |
80 memset(&hints, 0, sizeof(hints)); |
75 memset(&hints, 0, sizeof(hints)); |
81 hints.ai_flags = AI_CANONNAME; |
76 hints.ai_flags = AI_CANONNAME; |
82 hints.ai_family = AF_INET; |
77 hints.ai_family = AF_INET; |
83 |
78 |
84 if (getaddrinfo(hostname, NULL, &hints, &res) == 0) { |
79 if (getaddrinfo(hostname, NULL, &hints, &res) == 0) { |
85 getnameinfo(res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST, |
80 getnameinfo(res->ai_addr, res->ai_addrlen, hostname, sizeof(hostname), |
86 NULL, 0, NI_NAMEREQD); |
81 NULL, 0, NI_NAMEREQD); |
87 freeaddrinfo(res); |
82 freeaddrinfo(res); |
88 } |
83 } |
89 } |
|
90 #else |
84 #else |
91 } else { |
|
92 // make sure string is null-terminated |
85 // make sure string is null-terminated |
93 hostname[NI_MAXHOST] = '\0'; |
86 hostname[NI_MAXHOST] = '\0'; |
94 } |
|
95 #endif |
87 #endif |
|
88 } |
96 return (*env)->NewStringUTF(env, hostname); |
89 return (*env)->NewStringUTF(env, hostname); |
97 } |
90 } |
98 |
91 |
99 /* |
92 /* |
100 * Find an internet address for a given hostname. Note that this |
93 * Find an internet address for a given hostname. Note that this |
246 addr |= (caddr[3] & 0xff); |
239 addr |= (caddr[3] & 0xff); |
247 sa.sin_addr.s_addr = htonl(addr); |
240 sa.sin_addr.s_addr = htonl(addr); |
248 sa.sin_family = AF_INET; |
241 sa.sin_family = AF_INET; |
249 |
242 |
250 if (getnameinfo((struct sockaddr *)&sa, sizeof(struct sockaddr_in), |
243 if (getnameinfo((struct sockaddr *)&sa, sizeof(struct sockaddr_in), |
251 host, NI_MAXHOST, NULL, 0, NI_NAMEREQD)) { |
244 host, sizeof(host), NULL, 0, NI_NAMEREQD)) { |
252 JNU_ThrowByName(env, "java/net/UnknownHostException", NULL); |
245 JNU_ThrowByName(env, "java/net/UnknownHostException", NULL); |
253 } else { |
246 } else { |
254 ret = (*env)->NewStringUTF(env, host); |
247 ret = (*env)->NewStringUTF(env, host); |
255 if (ret == NULL) { |
248 if (ret == NULL) { |
256 JNU_ThrowByName(env, "java/net/UnknownHostException", NULL); |
249 JNU_ThrowByName(env, "java/net/UnknownHostException", NULL); |