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