< prev index next >

src/java.base/unix/native/libnet/Inet4AddressImpl.c

Print this page

  1 /*
  2  * Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  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
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 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  *
 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.
 24  */
 25 #include <ctype.h>
 26 #include <errno.h>
 27 #include <sys/types.h>
 28 #include <netinet/in.h>
 29 #include <netinet/in_systm.h>
 30 #include <netinet/ip.h>
 31 #include <netinet/ip_icmp.h>
 32 #include <stdlib.h>
 33 #include <string.h>
 34 #include <sys/time.h>
 35 
 36 #include "net_util.h"
 37 
 38 #include "java_net_Inet4AddressImpl.h"

 39 
 40 #if defined(MACOSX)
 41 extern jobjectArray lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6);

 42 #endif
 43 
 44 #define SET_NONBLOCKING(fd) {       \
 45     int flags = fcntl(fd, F_GETFL); \
 46     flags |= O_NONBLOCK;            \
 47     fcntl(fd, F_SETFL, flags);      \
 48 }
 49 
 50 /*
 51  * Inet4AddressImpl
 52  */
 53 
 54 /*
 55  * Class:     java_net_Inet4AddressImpl
 56  * Method:    getLocalHostName
 57  * Signature: ()Ljava/lang/String;
 58  */
 59 JNIEXPORT jstring JNICALL
 60 Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
 61     char hostname[NI_MAXHOST + 1];

 94     initInetAddressIDs(env);
 95     JNU_CHECK_EXCEPTION_RETURN(env, NULL);
 96 
 97     if (IS_NULL(host)) {
 98         JNU_ThrowNullPointerException(env, "host argument is null");
 99         return NULL;
100     }
101     hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
102     CHECK_NULL_RETURN(hostname, NULL);
103 
104     // try once, with our static buffer
105     memset(&hints, 0, sizeof(hints));
106     hints.ai_flags = AI_CANONNAME;
107     hints.ai_family = AF_INET;
108 
109     error = getaddrinfo(hostname, NULL, &hints, &res);
110 
111     if (error) {
112 #if defined(MACOSX)
113         // If getaddrinfo fails try getifaddrs, see bug 8170910.
114         ret = lookupIfLocalhost(env, hostname, JNI_FALSE);


115         if (ret != NULL || (*env)->ExceptionCheck(env)) {
116             goto cleanupAndReturn;
117         }
118 #endif
119         // report error
120         NET_ThrowUnknownHostExceptionWithGaiError(env, hostname, error);
121         goto cleanupAndReturn;
122     } else {
123         int i = 0;
124         iterator = res;
125         while (iterator != NULL) {
126             // skip duplicates
127             int skip = 0;
128             struct addrinfo *iteratorNew = resNew;
129             while (iteratorNew != NULL) {
130                 struct sockaddr_in *addr1, *addr2;
131                 addr1 = (struct sockaddr_in *)iterator->ai_addr;
132                 addr2 = (struct sockaddr_in *)iteratorNew->ai_addr;
133                 if (addr1->sin_addr.s_addr == addr2->sin_addr.s_addr) {
134                     skip = 1;

  1 /*
  2  * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
  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
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 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  *
 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.
 24  */
 25 #include <ctype.h>
 26 #include <errno.h>
 27 #include <sys/types.h>
 28 #include <netinet/in.h>
 29 #include <netinet/in_systm.h>
 30 #include <netinet/ip.h>
 31 #include <netinet/ip_icmp.h>
 32 #include <stdlib.h>
 33 #include <string.h>
 34 #include <sys/time.h>
 35 
 36 #include "net_util.h"
 37 
 38 #include "java_net_Inet4AddressImpl.h"
 39 #include "java_net_spi_InetAddressResolver_LookupPolicy.h"
 40 
 41 #if defined(MACOSX)
 42 extern jobjectArray lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6,
 43                                       int addressesOrder);
 44 #endif
 45 
 46 #define SET_NONBLOCKING(fd) {       \
 47     int flags = fcntl(fd, F_GETFL); \
 48     flags |= O_NONBLOCK;            \
 49     fcntl(fd, F_SETFL, flags);      \
 50 }
 51 
 52 /*
 53  * Inet4AddressImpl
 54  */
 55 
 56 /*
 57  * Class:     java_net_Inet4AddressImpl
 58  * Method:    getLocalHostName
 59  * Signature: ()Ljava/lang/String;
 60  */
 61 JNIEXPORT jstring JNICALL
 62 Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
 63     char hostname[NI_MAXHOST + 1];

 96     initInetAddressIDs(env);
 97     JNU_CHECK_EXCEPTION_RETURN(env, NULL);
 98 
 99     if (IS_NULL(host)) {
100         JNU_ThrowNullPointerException(env, "host argument is null");
101         return NULL;
102     }
103     hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
104     CHECK_NULL_RETURN(hostname, NULL);
105 
106     // try once, with our static buffer
107     memset(&hints, 0, sizeof(hints));
108     hints.ai_flags = AI_CANONNAME;
109     hints.ai_family = AF_INET;
110 
111     error = getaddrinfo(hostname, NULL, &hints, &res);
112 
113     if (error) {
114 #if defined(MACOSX)
115         // If getaddrinfo fails try getifaddrs, see bug 8170910.
116         // java_net_spi_InetAddressResolver_LookupPolicy_IPV4_FIRST and no ordering is ok
117         // here since only AF_INET addresses will be returned.
118         ret = lookupIfLocalhost(env, hostname, JNI_FALSE, java_net_spi_InetAddressResolver_LookupPolicy_IPV4);
119         if (ret != NULL || (*env)->ExceptionCheck(env)) {
120             goto cleanupAndReturn;
121         }
122 #endif
123         // report error
124         NET_ThrowUnknownHostExceptionWithGaiError(env, hostname, error);
125         goto cleanupAndReturn;
126     } else {
127         int i = 0;
128         iterator = res;
129         while (iterator != NULL) {
130             // skip duplicates
131             int skip = 0;
132             struct addrinfo *iteratorNew = resNew;
133             while (iteratorNew != NULL) {
134                 struct sockaddr_in *addr1, *addr2;
135                 addr1 = (struct sockaddr_in *)iterator->ai_addr;
136                 addr2 = (struct sockaddr_in *)iteratorNew->ai_addr;
137                 if (addr1->sin_addr.s_addr == addr2->sin_addr.s_addr) {
138                     skip = 1;
< prev index next >