1 /* 2 * Copyright (c) 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 #include "jdk_internal_net_rdma_RdmaServerSocketChannelImpl.h" 27 #include "net_util.h" 28 #include "nio.h" 29 #include <Rsocket.h> 30 31 static jfieldID fd_fdID; /* java.io.FileDescriptor.fd */ 32 static jclass isa_class; /* java.net.InetSocketAddress */ 33 static jmethodID isa_ctorID; /* .InetSocketAddress(InetAddress, int) */ 34 35 36 JNIEXPORT void JNICALL 37 Java_jdk_internal_net_rdma_RdmaServerSocketChannelImpl_initIDs(JNIEnv *env, 38 jclass c) { 39 loadRdmaFuncs(env); 40 41 jclass cls; 42 43 cls = (*env)->FindClass(env, "java/io/FileDescriptor"); 44 CHECK_NULL(cls); 45 fd_fdID = (*env)->GetFieldID(env, cls, "fd", "I"); 46 CHECK_NULL(fd_fdID); 47 48 cls = (*env)->FindClass(env, "java/net/InetSocketAddress"); 49 CHECK_NULL(cls); 50 isa_class = (*env)->NewGlobalRef(env, cls); 51 if (isa_class == NULL) { 52 JNU_ThrowOutOfMemoryError(env, NULL); 53 return; 54 } 55 isa_ctorID = (*env)->GetMethodID(env, cls, "<init>", 56 "(Ljava/net/InetAddress;I)V"); 57 CHECK_NULL(isa_ctorID); 58 } 59 60 JNIEXPORT jint JNICALL 61 Java_jdk_internal_net_rdma_RdmaServerSocketChannelImpl_accept0(JNIEnv *env, 62 jobject this, jobject ssfdo, jobject newfdo, jobjectArray isaa) { 63 jint ssfd = (*env)->GetIntField(env, ssfdo, fd_fdID); 64 jint newfd; 65 SOCKETADDRESS sa; 66 socklen_t sa_len = sizeof(SOCKETADDRESS); 67 jobject remote_ia = 0; 68 jobject isa; 69 jint remote_port = 0; 70 71 /* 72 * accept connection but ignore ECONNABORTED indicating that 73 * a connection was eagerly accepted but was reset before 74 * accept() was called. 75 */ 76 for (;;) { 77 newfd = rs_accept(ssfd, &sa.sa, &sa_len); 78 if (newfd >= 0) { 79 break; 80 } 81 if (errno != ECONNABORTED) { 82 break; 83 } 84 /* ECONNABORTED => restart accept */ 85 } 86 87 if (newfd < 0) { 88 if (errno == EAGAIN) 89 return IOS_UNAVAILABLE; 90 if (errno == EINTR) 91 return IOS_INTERRUPTED; 92 JNU_ThrowIOExceptionWithLastError(env, "Accept failed"); 93 return IOS_THROWN; 94 } 95 96 (*env)->SetIntField(env, newfdo, fd_fdID, newfd); 97 remote_ia = NET_SockaddrToInetAddress(env, &sa, (int *)&remote_port); 98 CHECK_NULL_RETURN(remote_ia, IOS_THROWN); 99 isa = (*env)->NewObject(env, isa_class, isa_ctorID, 100 remote_ia, remote_port); 101 CHECK_NULL_RETURN(isa, IOS_THROWN); 102 (*env)->SetObjectArrayElement(env, isaa, 0, isa); 103 return 1; 104 }