Path: blob/master/src/java.base/windows/native/libnet/Inet6AddressImpl.c
41119 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24#include <malloc.h>2526#include "net_util.h"2728#include "java_net_InetAddress.h"29#include "java_net_Inet4AddressImpl.h"30#include "java_net_Inet6AddressImpl.h"3132/*33* Inet6AddressImpl34*/3536/*37* Class: java_net_Inet6AddressImpl38* Method: getLocalHostName39* Signature: ()Ljava/lang/String;40*/41JNIEXPORT jstring JNICALL42Java_java_net_Inet6AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {43char hostname[256];4445if (gethostname(hostname, sizeof(hostname)) == -1) {46strcpy(hostname, "localhost");47}48return JNU_NewStringPlatform(env, hostname);49}5051/*52* Class: java_net_Inet6AddressImpl53* Method: lookupAllHostAddr54* Signature: (Ljava/lang/String;)[[B55*/56JNIEXPORT jobjectArray JNICALL57Java_java_net_Inet6AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,58jstring host) {59jobjectArray ret = NULL;60const char *hostname;61int error = 0;62struct addrinfo hints, *res = NULL, *resNew = NULL, *last = NULL,63*iterator;6465initInetAddressIDs(env);66JNU_CHECK_EXCEPTION_RETURN(env, NULL);6768if (IS_NULL(host)) {69JNU_ThrowNullPointerException(env, "host argument is null");70return NULL;71}72hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);73CHECK_NULL_RETURN(hostname, NULL);7475// try once, with our static buffer76memset(&hints, 0, sizeof(hints));77hints.ai_flags = AI_CANONNAME;78hints.ai_family = AF_UNSPEC;7980error = getaddrinfo(hostname, NULL, &hints, &res);8182if (error) {83// report error84NET_ThrowByNameWithLastError(env, "java/net/UnknownHostException",85hostname);86goto cleanupAndReturn;87} else {88int i = 0, inetCount = 0, inet6Count = 0, inetIndex = 0,89inet6Index = 0, originalIndex = 0;90int addressPreference =91(*env)->GetStaticIntField(env, ia_class, ia_preferIPv6AddressID);92iterator = res;93while (iterator != NULL) {94// skip duplicates95int skip = 0;96struct addrinfo *iteratorNew = resNew;97while (iteratorNew != NULL) {98if (iterator->ai_family == iteratorNew->ai_family &&99iterator->ai_addrlen == iteratorNew->ai_addrlen) {100if (iteratorNew->ai_family == AF_INET) { /* AF_INET */101struct sockaddr_in *addr1, *addr2;102addr1 = (struct sockaddr_in *)iterator->ai_addr;103addr2 = (struct sockaddr_in *)iteratorNew->ai_addr;104if (addr1->sin_addr.s_addr == addr2->sin_addr.s_addr) {105skip = 1;106break;107}108} else {109int t;110struct sockaddr_in6 *addr1, *addr2;111addr1 = (struct sockaddr_in6 *)iterator->ai_addr;112addr2 = (struct sockaddr_in6 *)iteratorNew->ai_addr;113114for (t = 0; t < 16; t++) {115if (addr1->sin6_addr.s6_addr[t] !=116addr2->sin6_addr.s6_addr[t]) {117break;118}119}120if (t < 16) {121iteratorNew = iteratorNew->ai_next;122continue;123} else {124skip = 1;125break;126}127}128} else if (iterator->ai_family != AF_INET &&129iterator->ai_family != AF_INET6) {130// we can't handle other family types131skip = 1;132break;133}134iteratorNew = iteratorNew->ai_next;135}136137if (!skip) {138struct addrinfo *next139= (struct addrinfo *)malloc(sizeof(struct addrinfo));140if (!next) {141JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed");142ret = NULL;143goto cleanupAndReturn;144}145memcpy(next, iterator, sizeof(struct addrinfo));146next->ai_next = NULL;147if (resNew == NULL) {148resNew = next;149} else {150last->ai_next = next;151}152last = next;153i++;154if (iterator->ai_family == AF_INET) {155inetCount++;156} else if (iterator->ai_family == AF_INET6) {157inet6Count++;158}159}160iterator = iterator->ai_next;161}162163// allocate array - at this point i contains the number of addresses164ret = (*env)->NewObjectArray(env, i, ia_class, NULL);165if (IS_NULL(ret)) {166/* we may have memory to free at the end of this */167goto cleanupAndReturn;168}169170if (addressPreference == java_net_InetAddress_PREFER_IPV6_VALUE) {171inetIndex = inet6Count;172inet6Index = 0;173} else if (addressPreference == java_net_InetAddress_PREFER_IPV4_VALUE) {174inetIndex = 0;175inet6Index = inetCount;176} else if (addressPreference == java_net_InetAddress_PREFER_SYSTEM_VALUE) {177inetIndex = inet6Index = originalIndex = 0;178}179180iterator = resNew;181while (iterator != NULL) {182if (iterator->ai_family == AF_INET) {183jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);184if (IS_NULL(iaObj)) {185ret = NULL;186goto cleanupAndReturn;187}188setInetAddress_addr(env, iaObj, ntohl(((struct sockaddr_in*)iterator->ai_addr)->sin_addr.s_addr));189if ((*env)->ExceptionCheck(env))190goto cleanupAndReturn;191setInetAddress_hostName(env, iaObj, host);192if ((*env)->ExceptionCheck(env))193goto cleanupAndReturn;194(*env)->SetObjectArrayElement(env, ret, (inetIndex | originalIndex), iaObj);195inetIndex++;196} else if (iterator->ai_family == AF_INET6) {197jint scope = 0;198jboolean ret1;199jobject iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);200if (IS_NULL(iaObj)) {201ret = NULL;202goto cleanupAndReturn;203}204ret1 = setInet6Address_ipaddress(env, iaObj, (char *)&(((struct sockaddr_in6*)iterator->ai_addr)->sin6_addr));205if (ret1 == JNI_FALSE) {206ret = NULL;207goto cleanupAndReturn;208}209scope = ((struct sockaddr_in6 *)iterator->ai_addr)->sin6_scope_id;210if (scope != 0) { // zero is default value, no need to set211setInet6Address_scopeid(env, iaObj, scope);212}213setInetAddress_hostName(env, iaObj, host);214if ((*env)->ExceptionCheck(env))215goto cleanupAndReturn;216(*env)->SetObjectArrayElement(env, ret, (inet6Index | originalIndex), iaObj);217inet6Index++;218}219if (addressPreference == java_net_InetAddress_PREFER_SYSTEM_VALUE) {220originalIndex++;221inetIndex = inet6Index = 0;222}223iterator = iterator->ai_next;224}225}226cleanupAndReturn:227JNU_ReleaseStringPlatformChars(env, host, hostname);228while (resNew != NULL) {229last = resNew;230resNew = resNew->ai_next;231free(last);232}233if (res != NULL) {234freeaddrinfo(res);235}236return ret;237}238239/*240* Class: java_net_Inet6AddressImpl241* Method: getHostByAddr242* Signature: ([B)Ljava/lang/String;243*244* Theoretically the UnknownHostException could be enriched with gai error245* information. But as it is silently ignored anyway, there's no need for this.246* It's only important that either a valid hostname is returned or an247* UnknownHostException is thrown.248*/249JNIEXPORT jstring JNICALL250Java_java_net_Inet6AddressImpl_getHostByAddr(JNIEnv *env, jobject this,251jbyteArray addrArray) {252jstring ret = NULL;253char host[NI_MAXHOST + 1];254int len = 0;255jbyte caddr[16];256SOCKETADDRESS sa;257258memset((void *)&sa, 0, sizeof(SOCKETADDRESS));259260// construct a sockaddr_in structure (AF_INET or AF_INET6)261if ((*env)->GetArrayLength(env, addrArray) == 4) {262jint addr;263(*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);264addr = ((caddr[0] << 24) & 0xff000000);265addr |= ((caddr[1] << 16) & 0xff0000);266addr |= ((caddr[2] << 8) & 0xff00);267addr |= (caddr[3] & 0xff);268sa.sa4.sin_addr.s_addr = htonl(addr);269sa.sa4.sin_family = AF_INET;270len = sizeof(struct sockaddr_in);271} else {272(*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);273memcpy((void *)&sa.sa6.sin6_addr, caddr, sizeof(struct in6_addr));274sa.sa6.sin6_family = AF_INET6;275len = sizeof(struct sockaddr_in6);276}277278if (getnameinfo(&sa.sa, len, host, NI_MAXHOST, NULL, 0, NI_NAMEREQD)) {279JNU_ThrowByName(env, "java/net/UnknownHostException", NULL);280} else {281ret = (*env)->NewStringUTF(env, host);282if (ret == NULL) {283JNU_ThrowByName(env, "java/net/UnknownHostException", NULL);284}285}286287return ret;288}289290/**291* ping implementation using tcp port 7 (echo)292*/293static jboolean294tcp_ping6(JNIEnv *env, SOCKETADDRESS *sa, SOCKETADDRESS *netif, jint timeout,295jint ttl)296{297jint fd;298int connect_rv = -1;299WSAEVENT hEvent;300301// open a TCP socket302fd = NET_Socket(AF_INET6, SOCK_STREAM, 0);303if (fd == SOCKET_ERROR) {304// note: if you run out of fds, you may not be able to load305// the exception class, and get a NoClassDefFoundError instead.306NET_ThrowNew(env, WSAGetLastError(), "Can't create socket");307return JNI_FALSE;308}309310// set TTL311if (ttl > 0) {312setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (const char *)&ttl, sizeof(ttl));313}314315// A network interface was specified, so let's bind to it.316if (netif != NULL) {317if (bind(fd, &netif->sa, sizeof(struct sockaddr_in6)) < 0) {318NET_ThrowNew(env, WSAGetLastError(), "Can't bind socket to interface");319closesocket(fd);320return JNI_FALSE;321}322}323324// Make the socket non blocking so we can use select/poll.325hEvent = WSACreateEvent();326WSAEventSelect(fd, hEvent, FD_READ|FD_CONNECT|FD_CLOSE);327328sa->sa6.sin6_port = htons(7); // echo port329connect_rv = connect(fd, &sa->sa, sizeof(struct sockaddr_in6));330331// connection established or refused immediately, either way it means332// we were able to reach the host!333if (connect_rv == 0 || WSAGetLastError() == WSAECONNREFUSED) {334WSACloseEvent(hEvent);335closesocket(fd);336return JNI_TRUE;337}338339switch (WSAGetLastError()) {340case WSAEHOSTUNREACH: // Host Unreachable341case WSAENETUNREACH: // Network Unreachable342case WSAENETDOWN: // Network is down343case WSAEPFNOSUPPORT: // Protocol Family unsupported344WSACloseEvent(hEvent);345closesocket(fd);346return JNI_FALSE;347case WSAEWOULDBLOCK: // this is expected as we'll probably have to wait348break;349default:350NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",351"connect failed");352WSACloseEvent(hEvent);353closesocket(fd);354return JNI_FALSE;355}356357timeout = NET_Wait(env, fd, NET_WAIT_CONNECT, timeout);358if (timeout >= 0) {359// connection has been established, check for error condition360int optlen = sizeof(connect_rv);361if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&connect_rv,362&optlen) < 0)363{364connect_rv = WSAGetLastError();365}366if (connect_rv == 0 || connect_rv == WSAECONNREFUSED) {367WSACloseEvent(hEvent);368closesocket(fd);369return JNI_TRUE;370}371}372WSACloseEvent(hEvent);373closesocket(fd);374return JNI_FALSE;375}376377/**378* ping implementation.379* Send a ICMP_ECHO_REQUEST packet every second until either the timeout380* expires or a answer is received.381* Returns true is an ECHO_REPLY is received, otherwise, false.382*/383static jboolean384ping6(JNIEnv *env, HANDLE hIcmpFile, SOCKETADDRESS *sa,385SOCKETADDRESS *netif, jint timeout)386{387DWORD dwRetVal = 0;388char SendData[32] = {0};389LPVOID ReplyBuffer = NULL;390DWORD ReplySize = 0;391IP_OPTION_INFORMATION ipInfo = {255, 0, 0, 0, NULL};392SOCKETADDRESS dftNetif;393394ReplySize = sizeof(ICMPV6_ECHO_REPLY) + sizeof(SendData);395ReplyBuffer = (VOID *)malloc(ReplySize);396if (ReplyBuffer == NULL) {397IcmpCloseHandle(hIcmpFile);398NET_ThrowNew(env, -1, "Unable to allocate memory");399return JNI_FALSE;400}401402//define local source information403if (netif == NULL) {404dftNetif.sa6.sin6_addr = in6addr_any;405dftNetif.sa6.sin6_family = AF_INET6;406dftNetif.sa6.sin6_flowinfo = 0;407dftNetif.sa6.sin6_port = 0;408netif = &dftNetif;409}410411dwRetVal = Icmp6SendEcho2(hIcmpFile, // HANDLE IcmpHandle,412NULL, // HANDLE Event,413NULL, // PIO_APC_ROUTINE ApcRoutine,414NULL, // PVOID ApcContext,415&netif->sa6, // struct sockaddr_in6 *SourceAddress,416&sa->sa6, // struct sockaddr_in6 *DestinationAddress,417SendData, // LPVOID RequestData,418sizeof(SendData), // WORD RequestSize,419&ipInfo, // PIP_OPTION_INFORMATION RequestOptions,420ReplyBuffer, // LPVOID ReplyBuffer,421ReplySize, // DWORD ReplySize,422timeout); // DWORD Timeout423424free(ReplyBuffer);425IcmpCloseHandle(hIcmpFile);426427if (dwRetVal == 0) { // if the call failed428return JNI_FALSE;429} else {430return JNI_TRUE;431}432}433434/*435* Class: java_net_Inet6AddressImpl436* Method: isReachable0437* Signature: ([BII[BII)Z438*/439JNIEXPORT jboolean JNICALL440Java_java_net_Inet6AddressImpl_isReachable0(JNIEnv *env, jobject this,441jbyteArray addrArray, jint scope,442jint timeout, jbyteArray ifArray,443jint ttl, jint if_scope)444{445jbyte caddr[16];446jint sz;447SOCKETADDRESS sa, inf, *netif = NULL;448HANDLE hIcmpFile;449450// If IPv6 is not enabled, then we can't reach an IPv6 address, can we?451// Actually, we probably shouldn't even get here.452if (!ipv6_available()) {453return JNI_FALSE;454}455456// If it's an IPv4 address, ICMP won't work with IPv4 mapped address,457// therefore, let's delegate to the Inet4Address method.458sz = (*env)->GetArrayLength(env, addrArray);459if (sz == 4) {460return Java_java_net_Inet4AddressImpl_isReachable0(env, this,461addrArray, timeout,462ifArray, ttl);463}464465// load address to SOCKETADDRESS466memset((char *)caddr, 0, 16);467(*env)->GetByteArrayRegion(env, addrArray, 0, 16, caddr);468memset((char *)&sa, 0, sizeof(SOCKETADDRESS));469memcpy((void *)&sa.sa6.sin6_addr, caddr, sizeof(struct in6_addr));470sa.sa6.sin6_family = AF_INET6;471if (scope > 0) {472sa.sa6.sin6_scope_id = scope;473}474475// load network interface address to SOCKETADDRESS, if specified476if (!(IS_NULL(ifArray))) {477memset((char *)caddr, 0, 16);478(*env)->GetByteArrayRegion(env, ifArray, 0, 16, caddr);479memset((char *)&inf, 0, sizeof(SOCKETADDRESS));480memcpy((void *)&inf.sa6.sin6_addr, caddr, sizeof(struct in6_addr));481inf.sa6.sin6_family = AF_INET6;482inf.sa6.sin6_scope_id = if_scope;483netif = &inf;484}485486// Let's try to create an ICMP handle.487hIcmpFile = Icmp6CreateFile();488if (hIcmpFile == INVALID_HANDLE_VALUE) {489int err = WSAGetLastError();490if (err == ERROR_ACCESS_DENIED) {491// fall back to TCP echo if access is denied to ICMP492return tcp_ping6(env, &sa, netif, timeout, ttl);493} else {494NET_ThrowNew(env, err, "Unable to create ICMP file handle");495return JNI_FALSE;496}497} else {498// It didn't fail, so we can use ICMP.499return ping6(env, hIcmpFile, &sa, netif, timeout);500}501}502503504