1 /* 2 * Copyright (c) 1997, 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 26 #ifndef NET_UTILS_H 27 #define NET_UTILS_H 28 29 #include "jvm.h" 30 #include "jni_util.h" 31 #include "net_util_md.h" 32 33 /************************************************************************ 34 * Macros and misc constants 35 */ 36 37 #define MAX_PACKET_LEN 65536 38 39 #define NET_WAIT_READ 0x01 40 #define NET_WAIT_WRITE 0x02 41 #define NET_WAIT_CONNECT 0x04 42 43 /************************************************************************ 44 * Cached field IDs 45 * 46 * The naming convention for field IDs is 47 * <class abbrv>_<fieldName>ID 48 */ 49 extern jclass ia_class; 50 extern jfieldID iac_addressID; 51 extern jfieldID iac_familyID; 52 extern jfieldID iac_hostNameID; 53 extern jfieldID iac_origHostNameID; 54 extern jfieldID ia_preferIPv6AddressID; 55 56 JNIEXPORT void JNICALL initInetAddressIDs(JNIEnv *env); 57 58 /** (Inet6Address accessors) 59 * set_ methods return JNI_TRUE on success JNI_FALSE on error 60 * get_ methods that return int/boolean, return -1 on error 61 * get_ methods that return objects return NULL on error. 62 */ 63 extern jboolean setInet6Address_scopeifname(JNIEnv *env, jobject ia6Obj, jobject scopeifname); 64 extern unsigned int getInet6Address_scopeid(JNIEnv *env, jobject ia6Obj); 65 extern jboolean setInet6Address_scopeid(JNIEnv *env, jobject ia6Obj, int scopeid); 66 extern jboolean getInet6Address_ipaddress(JNIEnv *env, jobject ia6Obj, char *dest); 67 extern jboolean setInet6Address_ipaddress(JNIEnv *env, jobject ia6Obj, char *address); 68 69 extern void setInetAddress_addr(JNIEnv *env, jobject iaObj, int address); 70 extern void setInetAddress_family(JNIEnv *env, jobject iaObj, int family); 71 extern void setInetAddress_hostName(JNIEnv *env, jobject iaObj, jobject h); 72 extern int getInetAddress_addr(JNIEnv *env, jobject iaObj); 73 extern int getInetAddress_family(JNIEnv *env, jobject iaObj); 74 75 extern jclass ia4_class; 76 extern jmethodID ia4_ctrID; 77 78 /* NetworkInterface fields */ 79 extern jclass ni_class; 80 extern jfieldID ni_nameID; 81 extern jfieldID ni_indexID; 82 extern jfieldID ni_addrsID; 83 extern jfieldID ni_descID; 84 extern jmethodID ni_ctrID; 85 86 /* DatagramPacket fields */ 87 extern jfieldID dp_addressID; 88 extern jfieldID dp_portID; 89 extern jfieldID dp_bufID; 90 extern jfieldID dp_offsetID; 91 extern jfieldID dp_lengthID; 92 extern jfieldID dp_bufLengthID; 93 94 /* Inet6Address fields */ 95 extern jclass ia6_class; 96 extern jfieldID ia6_holder6ID; 97 extern jfieldID ia6_ipaddressID; 98 extern jfieldID ia6_scopeidID; 99 extern jfieldID ia6_scopeidsetID; 100 extern jfieldID ia6_scopeifnameID; 101 extern jmethodID ia6_ctrID; 102 103 /************************************************************************ 104 * Utilities 105 */ 106 JNIEXPORT void JNICALL Java_java_net_InetAddress_init(JNIEnv *env, jclass cls); 107 JNIEXPORT void JNICALL Java_java_net_Inet4Address_init(JNIEnv *env, jclass cls); 108 JNIEXPORT void JNICALL Java_java_net_Inet6Address_init(JNIEnv *env, jclass cls); 109 JNIEXPORT void JNICALL Java_java_net_NetworkInterface_init(JNIEnv *env, jclass cls); 110 111 JNIEXPORT void JNICALL NET_ThrowNew(JNIEnv *env, int errorNum, char *msg); 112 113 void NET_ThrowCurrent(JNIEnv *env, char *msg); 114 115 jfieldID NET_GetFileDescriptorID(JNIEnv *env); 116 117 JNIEXPORT jint JNICALL ipv4_available(); 118 JNIEXPORT jint JNICALL ipv6_available(); 119 120 JNIEXPORT jint JNICALL reuseport_available(); 121 122 /** 123 * This function will fill a SOCKETADDRESS structure from an InetAddress 124 * object. 125 * 126 * The parameter 'sa' must point to valid storage of size 127 * 'sizeof(SOCKETADDRESS)'. 128 * 129 * The parameter 'len' is a pointer to an int and is used for returning 130 * the actual sockaddr length, e.g. 'sizeof(struct sockaddr_in)' or 131 * 'sizeof(struct sockaddr_in6)'. 132 * 133 * If the type of the InetAddress object is IPv6, the function will fill a 134 * sockaddr_in6 structure. IPv6 must be available in that case, otherwise an 135 * exception is thrown. 136 * In the case of an IPv4 InetAddress, when IPv6 is available and 137 * v4MappedAddress is TRUE, this method will fill a sockaddr_in6 structure 138 * containing an IPv4 mapped IPv6 address. Otherwise a sockaddr_in 139 * structure will be filled. 140 */ 141 JNIEXPORT int JNICALL 142 NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, 143 SOCKETADDRESS *sa, int *len, 144 jboolean v4MappedAddress); 145 146 JNIEXPORT jobject JNICALL 147 NET_SockaddrToInetAddress(JNIEnv *env, SOCKETADDRESS *sa, int *port); 148 149 void platformInit(); 150 151 JNIEXPORT jint JNICALL NET_GetPortFromSockaddr(SOCKETADDRESS *sa); 152 153 JNIEXPORT jboolean JNICALL 154 NET_SockaddrEqualsInetAddress(JNIEnv *env, SOCKETADDRESS *sa, jobject iaObj); 155 156 int NET_IsIPv4Mapped(jbyte* caddr); 157 158 int NET_IPv4MappedToIPv4(jbyte* caddr); 159 160 int NET_IsEqual(jbyte* caddr1, jbyte* caddr2); 161 162 int NET_IsZeroAddr(jbyte* caddr); 163 164 /* Socket operations 165 * 166 * These work just like the system calls, except that they may do some 167 * platform-specific pre/post processing of the arguments and/or results. 168 */ 169 170 JNIEXPORT int JNICALL 171 NET_SocketAvailable(int fd, int *pbytes); 172 173 JNIEXPORT int JNICALL 174 NET_GetSockOpt(int fd, int level, int opt, void *result, int *len); 175 176 JNIEXPORT int JNICALL 177 NET_SetSockOpt(int fd, int level, int opt, const void *arg, int len); 178 179 JNIEXPORT int JNICALL 180 NET_Bind(int fd, SOCKETADDRESS *sa, int len); 181 182 JNIEXPORT int JNICALL 183 NET_MapSocketOption(jint cmd, int *level, int *optname); 184 185 JNIEXPORT int JNICALL 186 NET_MapSocketOptionV6(jint cmd, int *level, int *optname); 187 188 JNIEXPORT jint JNICALL 189 NET_EnableFastTcpLoopback(int fd); 190 191 unsigned short in_cksum(unsigned short *addr, int len); 192 193 jint NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout); 194 195 #endif /* NET_UTILS_H */