1 /* 2 * Copyright (c) 2014, 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 <sys/types.h> 27 #include <sys/socket.h> 28 29 #ifdef __solaris__ 30 #include <unistd.h> 31 #include <stropts.h> 32 33 #ifndef BSD_COMP 34 #define BSD_COMP 35 #endif 36 37 #endif 38 39 #include <sys/ioctl.h> 40 41 #include "jvm.h" 42 #include "jni_util.h" 43 #include "net_util.h" 44 45 #include "java_net_AbstractPlainDatagramSocketImpl.h" 46 47 static jfieldID IO_fd_fdID; 48 49 static jfieldID apdsi_fdID; 50 51 52 /* 53 * Class: java_net_AbstractPlainDatagramSocketImpl 54 * Method: init 55 * Signature: ()V 56 */ 57 JNIEXPORT void JNICALL 58 Java_java_net_AbstractPlainDatagramSocketImpl_init(JNIEnv *env, jclass cls) { 59 60 apdsi_fdID = (*env)->GetFieldID(env, cls, "fd", 61 "Ljava/io/FileDescriptor;"); 62 CHECK_NULL(apdsi_fdID); 63 64 IO_fd_fdID = NET_GetFileDescriptorID(env); 65 } 66 67 /* 68 * Class: java_net_AbstractPlainDatagramSocketImpl 69 * Method: dataAvailable 70 * Signature: ()I 71 */ 72 JNIEXPORT jint JNICALL Java_java_net_AbstractPlainDatagramSocketImpl_dataAvailable 73 (JNIEnv *env, jobject this) { 74 int fd, retval; 75 76 jobject fdObj = (*env)->GetObjectField(env, this, apdsi_fdID); 77 78 if (IS_NULL(fdObj)) { 79 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", 80 "Socket closed"); 81 return -1; 82 } 83 fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); 84 85 if (ioctl(fd, FIONREAD, &retval) < 0) { 86 return -1; 87 } 88 return retval; 89 }