< prev index next >

src/java.base/unix/native/libnet/PlainSocketImpl.c

Print this page




 132     CHECK_NULL(psi_fdLockID);
 133     psi_closePendingID = (*env)->GetFieldID(env, cls, "closePending", "Z");
 134     CHECK_NULL(psi_closePendingID);
 135     IO_fd_fdID = NET_GetFileDescriptorID(env);
 136     CHECK_NULL(IO_fd_fdID);
 137 
 138     initInetAddressIDs(env);
 139     JNU_CHECK_EXCEPTION(env);
 140 
 141     /* Create the marker fd used for dup2 */
 142     marker_fd = getMarkerFD();
 143 }
 144 
 145 /* a global reference to the java.net.SocketException class. In
 146  * socketCreate, we ensure that this is initialized. This is to
 147  * prevent the problem where socketCreate runs out of file
 148  * descriptors, and is then unable to load the exception class.
 149  */
 150 static jclass socketExceptionCls;
 151 








 152 /*
 153  * Class:     java_net_PlainSocketImpl
 154  * Method:    socketCreate
 155  * Signature: (ZZ)V */
 156 JNIEXPORT void JNICALL
 157 Java_java_net_PlainSocketImpl_socketCreate(JNIEnv *env, jobject this,
 158                                            jboolean stream, jboolean isServer) {
 159     jobject fdObj, ssObj;
 160     int fd;
 161     int type = (stream ? SOCK_STREAM : SOCK_DGRAM);
 162     int domain = ipv6_available() ? AF_INET6 : AF_INET;
 163 
 164     if (socketExceptionCls == NULL) {
 165         jclass c = (*env)->FindClass(env, "java/net/SocketException");
 166         CHECK_NULL(c);
 167         socketExceptionCls = (jclass)(*env)->NewGlobalRef(env, c);
 168         CHECK_NULL(socketExceptionCls);
 169     }
 170     fdObj = (*env)->GetObjectField(env, this, psi_fdID);
 171 
 172     if (fdObj == NULL) {
 173         (*env)->ThrowNew(env, socketExceptionCls, "null fd object");
 174         return;
 175     }
 176 
 177     if ((fd = socket(domain, type, 0)) == -1) {
 178         /* note: if you run out of fds, you may not be able to load
 179          * the exception class, and get a NoClassDefFoundError
 180          * instead.
 181          */
 182         NET_ThrowNew(env, errno, "can't create socket");
 183         return;
 184     }
 185 
 186     /*
 187      * If IPv4 is available, disable IPV6_V6ONLY to ensure dual-socket support.
 188      */
 189     if (domain == AF_INET6 && ipv4_available()) {
 190         int arg = 0;
 191         if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&arg,
 192                        sizeof(int)) < 0) {
 193             NET_ThrowNew(env, errno, "cannot set IPPROTO_IPV6");
 194             close(fd);
 195             return;
 196         }
 197     }




 132     CHECK_NULL(psi_fdLockID);
 133     psi_closePendingID = (*env)->GetFieldID(env, cls, "closePending", "Z");
 134     CHECK_NULL(psi_closePendingID);
 135     IO_fd_fdID = NET_GetFileDescriptorID(env);
 136     CHECK_NULL(IO_fd_fdID);
 137 
 138     initInetAddressIDs(env);
 139     JNU_CHECK_EXCEPTION(env);
 140 
 141     /* Create the marker fd used for dup2 */
 142     marker_fd = getMarkerFD();
 143 }
 144 
 145 /* a global reference to the java.net.SocketException class. In
 146  * socketCreate, we ensure that this is initialized. This is to
 147  * prevent the problem where socketCreate runs out of file
 148  * descriptors, and is then unable to load the exception class.
 149  */
 150 static jclass socketExceptionCls;
 151 
 152 static int socket0(int domain, int type, int protocol) {
 153     if (domain == AF_INET) {
 154         errno = EAFNOSUPPORT;
 155         return -1;
 156     }
 157     return socket(domain, type, protocol);
 158 }
 159 
 160 /*
 161  * Class:     java_net_PlainSocketImpl
 162  * Method:    socketCreate
 163  * Signature: (ZZ)V */
 164 JNIEXPORT void JNICALL
 165 Java_java_net_PlainSocketImpl_socketCreate(JNIEnv *env, jobject this,
 166                                            jboolean stream, jboolean isServer) {
 167     jobject fdObj, ssObj;
 168     int fd;
 169     int type = (stream ? SOCK_STREAM : SOCK_DGRAM);
 170     int domain = ipv6_available() ? AF_INET6 : AF_INET;
 171 
 172     if (socketExceptionCls == NULL) {
 173         jclass c = (*env)->FindClass(env, "java/net/SocketException");
 174         CHECK_NULL(c);
 175         socketExceptionCls = (jclass)(*env)->NewGlobalRef(env, c);
 176         CHECK_NULL(socketExceptionCls);
 177     }
 178     fdObj = (*env)->GetObjectField(env, this, psi_fdID);
 179 
 180     if (fdObj == NULL) {
 181         (*env)->ThrowNew(env, socketExceptionCls, "null fd object");
 182         return;
 183     }
 184 
 185     if ((fd = socket0(domain, type, 0)) == -1) {
 186         /* note: if you run out of fds, you may not be able to load
 187          * the exception class, and get a NoClassDefFoundError
 188          * instead.
 189          */
 190         NET_ThrowNew(env, errno, "can't create socket");
 191         return;
 192     }
 193 
 194     /*
 195      * If IPv4 is available, disable IPV6_V6ONLY to ensure dual-socket support.
 196      */
 197     if (domain == AF_INET6 && ipv4_available()) {
 198         int arg = 0;
 199         if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&arg,
 200                        sizeof(int)) < 0) {
 201             NET_ThrowNew(env, errno, "cannot set IPPROTO_IPV6");
 202             close(fd);
 203             return;
 204         }
 205     }


< prev index next >