Path: blob/master/src/java.base/windows/native/libnet/net_util_md.h
41119 views
/*1* Copyright (c) 1997, 2018, 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 <winsock2.h>25#include <WS2tcpip.h>26#include <iphlpapi.h>27#include <icmpapi.h>28#include <mstcpip.h>2930/* used to disable connection reset messages on Windows XP */31#ifndef SIO_UDP_CONNRESET32#define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12)33#endif3435#ifndef IN6_IS_ADDR_ANY36#define IN6_IS_ADDR_ANY(a) \37(((a)->s6_words[0] == 0) && ((a)->s6_words[1] == 0) && \38((a)->s6_words[2] == 0) && ((a)->s6_words[3] == 0) && \39((a)->s6_words[4] == 0) && ((a)->s6_words[5] == 0) && \40((a)->s6_words[6] == 0) && ((a)->s6_words[7] == 0))41#endif4243#ifndef IPV6_V6ONLY44#define IPV6_V6ONLY 27 /* Treat wildcard bind as AF_INET6-only. */45#endif4647#define MAX_BUFFER_LEN 204848#define MAX_HEAP_BUFFER_LEN 655364950/* true if SO_RCVTIMEO is supported by underlying provider */51extern jboolean isRcvTimeoutSupported;5253void NET_ThrowCurrent(JNIEnv *env, char *msg);5455typedef union {56struct sockaddr sa;57struct sockaddr_in sa4;58struct sockaddr_in6 sa6;59} SOCKETADDRESS;6061/*62* passed to NET_BindV6. Both ipv4_fd and ipv6_fd must be created and unbound63* sockets. On return they may refer to different sockets.64*/65struct ipv6bind {66SOCKETADDRESS *addr;67SOCKET ipv4_fd;68SOCKET ipv6_fd;69};7071#define SOCKETADDRESS_COPY(DST,SRC) { \72if ((SRC)->sa_family == AF_INET6) { \73memcpy ((DST), (SRC), sizeof (struct sockaddr_in6)); \74} else { \75memcpy ((DST), (SRC), sizeof (struct sockaddr_in)); \76} \77}7879#define SET_PORT(X,Y) { \80if ((X)->sa.sa_family == AF_INET) { \81(X)->sa4.sin_port = (Y); \82} else { \83(X)->sa6.sin6_port = (Y); \84} \85}8687#define GET_PORT(X) ((X)->sa.sa_family == AF_INET ? (X)->sa4.sin_port : (X)->sa6.sin6_port)8889/**90* With dual socket implementation the91* IPv4 addresseses might be mapped as IPv6.92* The IPv4 loopback adapter address ranges (127.0.0.0 through 127.255.255.255) will93* be mapped as the following IPv6 ::ffff:127.0.0.0 through ::ffff:127.255.255.255.94* For example, this is done by NET_InetAddressToSockaddr.95*/96#define IN6_IS_ADDR_V4MAPPED_LOOPBACK(x) ( \97(((x)->s6_words[0] == 0) && \98((x)->s6_words[1] == 0) && \99((x)->s6_words[2] == 0) && \100((x)->s6_words[3] == 0) && \101((x)->s6_words[4] == 0) && \102((x)->s6_words[5] == 0xFFFF) && \103(((x)->s6_words[6] & 0x00FF) == 0x007F)) \104)105106/**107* Check for IPv4 loopback adapter address ranges (127.0.0.0 through 127.255.255.255)108*/109#define IN4_IS_ADDR_NETLONG_LOOPBACK(l) ( \110((l & 0xFF000000) == 0x7F000000) \111)112113#define IS_LOOPBACK_ADDRESS(x) ( \114((x)->sa.sa_family == AF_INET) ? \115(IN4_IS_ADDR_NETLONG_LOOPBACK(ntohl((x)->sa4.sin_addr.s_addr))) : \116((IN6_IS_ADDR_LOOPBACK(&(x)->sa6.sin6_addr)) || \117(IN6_IS_ADDR_V4MAPPED_LOOPBACK(&(x)->sa6.sin6_addr))) \118)119120JNIEXPORT int JNICALL NET_SocketClose(int fd);121122JNIEXPORT int JNICALL NET_Timeout(int fd, long timeout);123124int NET_Socket(int domain, int type, int protocol);125126void NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,127const char *defaultDetail);128129/*130* differs from NET_Timeout() as follows:131*132* If timeout = -1, it blocks forever.133*134* returns 1 or 2 depending if only one or both sockets135* fire at same time.136*137* *fdret is (one of) the active fds. If both sockets138* fire at same time, *fd == fd always.139*/140JNIEXPORT int JNICALL NET_Timeout2(int fd, int fd1, long timeout, int *fdret);141142JNIEXPORT int JNICALL NET_BindV6(struct ipv6bind *b, jboolean exclBind);143144JNIEXPORT int JNICALL NET_WinBind(int s, SOCKETADDRESS *sa, int len,145jboolean exclBind);146147JNIEXPORT jint JNICALL NET_EnableFastTcpLoopbackConnect(int fd);148149/* XP versions of the native routines */150151JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0_XP152(JNIEnv *env, jclass cls, jstring name);153154JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0_XP155(JNIEnv *env, jclass cls, jint index);156157JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0_XP158(JNIEnv *env, jclass cls, jobject iaObj);159160JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll_XP161(JNIEnv *env, jclass cls);162163JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_supportsMulticast0_XP164(JNIEnv *env, jclass cls, jstring name, jint index);165166JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0_XP167(JNIEnv *env, jclass cls, jstring name, jint index);168169JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isP2P0_XP170(JNIEnv *env, jclass cls, jstring name, jint index);171172JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0_XP173(JNIEnv *env, jclass cls, jstring name, jint index);174175JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0_XP176(JNIEnv *env, jclass class, jstring name, jint index);177178JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isLoopback0_XP179(JNIEnv *env, jclass cls, jstring name, jint index);180181182