Path: blob/master/src/java.base/share/native/libnet/net_util.h
41149 views
/*1* Copyright (c) 1997, 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*/2425#ifndef NET_UTILS_H26#define NET_UTILS_H2728#include "jvm.h"29#include "jni_util.h"30#include "net_util_md.h"3132/************************************************************************33* Macros and misc constants34*/3536#define MAX_PACKET_LEN 655363738#define NET_WAIT_READ 0x0139#define NET_WAIT_WRITE 0x0240#define NET_WAIT_CONNECT 0x044142/************************************************************************43* Cached field IDs44*45* The naming convention for field IDs is46* <class abbrv>_<fieldName>ID47* i.e. psi_timeoutID is PlainSocketImpl's timeout field's ID.48*/49extern jclass ia_class;50extern jfieldID iac_addressID;51extern jfieldID iac_familyID;52extern jfieldID iac_hostNameID;53extern jfieldID iac_origHostNameID;54extern jfieldID ia_preferIPv6AddressID;5556JNIEXPORT void JNICALL initInetAddressIDs(JNIEnv *env);5758/** (Inet6Address accessors)59* set_ methods return JNI_TRUE on success JNI_FALSE on error60* get_ methods that return int/boolean, return -1 on error61* get_ methods that return objects return NULL on error.62*/63extern jboolean setInet6Address_scopeifname(JNIEnv *env, jobject ia6Obj, jobject scopeifname);64extern unsigned int getInet6Address_scopeid(JNIEnv *env, jobject ia6Obj);65extern jboolean setInet6Address_scopeid(JNIEnv *env, jobject ia6Obj, int scopeid);66extern jboolean getInet6Address_ipaddress(JNIEnv *env, jobject ia6Obj, char *dest);67extern jboolean setInet6Address_ipaddress(JNIEnv *env, jobject ia6Obj, char *address);6869extern void setInetAddress_addr(JNIEnv *env, jobject iaObj, int address);70extern void setInetAddress_family(JNIEnv *env, jobject iaObj, int family);71extern void setInetAddress_hostName(JNIEnv *env, jobject iaObj, jobject h);72extern int getInetAddress_addr(JNIEnv *env, jobject iaObj);73extern int getInetAddress_family(JNIEnv *env, jobject iaObj);7475extern jclass ia4_class;76extern jmethodID ia4_ctrID;7778/* NetworkInterface fields */79extern jclass ni_class;80extern jfieldID ni_nameID;81extern jfieldID ni_indexID;82extern jfieldID ni_addrsID;83extern jfieldID ni_descID;84extern jmethodID ni_ctrID;8586/* PlainSocketImpl fields */87extern jfieldID psi_timeoutID;88extern jfieldID psi_fdID;89extern jfieldID psi_addressID;90extern jfieldID psi_portID;91extern jfieldID psi_localportID;9293/* DatagramPacket fields */94extern jfieldID dp_addressID;95extern jfieldID dp_portID;96extern jfieldID dp_bufID;97extern jfieldID dp_offsetID;98extern jfieldID dp_lengthID;99extern jfieldID dp_bufLengthID;100101/* Inet6Address fields */102extern jclass ia6_class;103extern jfieldID ia6_holder6ID;104extern jfieldID ia6_ipaddressID;105extern jfieldID ia6_scopeidID;106extern jfieldID ia6_scopeidsetID;107extern jfieldID ia6_scopeifnameID;108extern jmethodID ia6_ctrID;109110/************************************************************************111* Utilities112*/113JNIEXPORT void JNICALL Java_java_net_InetAddress_init(JNIEnv *env, jclass cls);114JNIEXPORT void JNICALL Java_java_net_Inet4Address_init(JNIEnv *env, jclass cls);115JNIEXPORT void JNICALL Java_java_net_Inet6Address_init(JNIEnv *env, jclass cls);116JNIEXPORT void JNICALL Java_java_net_NetworkInterface_init(JNIEnv *env, jclass cls);117118JNIEXPORT void JNICALL NET_ThrowNew(JNIEnv *env, int errorNum, char *msg);119120void NET_ThrowCurrent(JNIEnv *env, char *msg);121122jfieldID NET_GetFileDescriptorID(JNIEnv *env);123124JNIEXPORT jint JNICALL ipv4_available();125JNIEXPORT jint JNICALL ipv6_available();126127JNIEXPORT jint JNICALL reuseport_available();128129/**130* This function will fill a SOCKETADDRESS structure from an InetAddress131* object.132*133* The parameter 'sa' must point to valid storage of size134* 'sizeof(SOCKETADDRESS)'.135*136* The parameter 'len' is a pointer to an int and is used for returning137* the actual sockaddr length, e.g. 'sizeof(struct sockaddr_in)' or138* 'sizeof(struct sockaddr_in6)'.139*140* If the type of the InetAddress object is IPv6, the function will fill a141* sockaddr_in6 structure. IPv6 must be available in that case, otherwise an142* exception is thrown.143* In the case of an IPv4 InetAddress, when IPv6 is available and144* v4MappedAddress is TRUE, this method will fill a sockaddr_in6 structure145* containing an IPv4 mapped IPv6 address. Otherwise a sockaddr_in146* structure will be filled.147*/148JNIEXPORT int JNICALL149NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port,150SOCKETADDRESS *sa, int *len,151jboolean v4MappedAddress);152153JNIEXPORT jobject JNICALL154NET_SockaddrToInetAddress(JNIEnv *env, SOCKETADDRESS *sa, int *port);155156void platformInit();157158JNIEXPORT jint JNICALL NET_GetPortFromSockaddr(SOCKETADDRESS *sa);159160JNIEXPORT jboolean JNICALL161NET_SockaddrEqualsInetAddress(JNIEnv *env, SOCKETADDRESS *sa, jobject iaObj);162163int NET_IsIPv4Mapped(jbyte* caddr);164165int NET_IPv4MappedToIPv4(jbyte* caddr);166167int NET_IsEqual(jbyte* caddr1, jbyte* caddr2);168169int NET_IsZeroAddr(jbyte* caddr);170171/* Socket operations172*173* These work just like the system calls, except that they may do some174* platform-specific pre/post processing of the arguments and/or results.175*/176177JNIEXPORT int JNICALL178NET_SocketAvailable(int fd, int *pbytes);179180JNIEXPORT int JNICALL181NET_GetSockOpt(int fd, int level, int opt, void *result, int *len);182183JNIEXPORT int JNICALL184NET_SetSockOpt(int fd, int level, int opt, const void *arg, int len);185186JNIEXPORT int JNICALL187NET_Bind(int fd, SOCKETADDRESS *sa, int len);188189JNIEXPORT int JNICALL190NET_MapSocketOption(jint cmd, int *level, int *optname);191192JNIEXPORT int JNICALL193NET_MapSocketOptionV6(jint cmd, int *level, int *optname);194195JNIEXPORT jint JNICALL196NET_EnableFastTcpLoopback(int fd);197198unsigned short in_cksum(unsigned short *addr, int len);199200jint NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout);201202#endif /* NET_UTILS_H */203204205