1 /*
   2  * Copyright (c) 1997, 2016, 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 <stdlib.h>
  27 #include <errno.h>
  28 #include <string.h>
  29 #include <sys/types.h>
  30 #include <sys/socket.h>
  31 #include <sys/time.h>
  32 #include "jvm.h"
  33 #include "jni_util.h"
  34 #include "net_util.h"
  35 
  36 #include "java_net_SocketInputStream.h"
  37 
  38 /************************************************************************
  39  * SocketInputStream
  40  */
  41 
  42 static jfieldID IO_fd_fdID;
  43 
  44 /*
  45  * Class:     java_net_SocketInputStream
  46  * Method:    init
  47  * Signature: ()V
  48  */
  49 JNIEXPORT void JNICALL
  50 Java_java_net_SocketInputStream_init(JNIEnv *env, jclass cls) {
  51     IO_fd_fdID = NET_GetFileDescriptorID(env);
  52 }
  53 
  54 static int NET_ReadWithTimeout(JNIEnv *env, int fd, char *bufP, int len, long timeout) {
  55     int result = 0;
  56     long prevtime = NET_GetCurrentTime(), newtime;
  57     while (timeout > 0) {
  58         result = NET_TimeoutWithCurrentTime(fd, timeout, prevtime);
  59         if (result <= 0) {
  60             if (result == 0) {
  61                 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", "Read timed out");
  62             } else if (result == -1) {
  63                 if (errno == EBADF) {
  64                     JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
  65                 } else if (errno == ENOMEM) {
  66                     JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
  67                 } else {
  68                     JNU_ThrowByNameWithMessageAndLastError
  69                             (env, JNU_JAVANETPKG "SocketException", "select/poll failed");
  70                 }
  71             }
  72             return -1;
  73         }
  74         result = NET_NonBlockingRead(fd, bufP, len);
  75         if (result == -1 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) {
  76             newtime = NET_GetCurrentTime();
  77             timeout -= newtime - prevtime;
  78             if (timeout > 0) {
  79                 prevtime = newtime;
  80             }
  81         } else {
  82             break;
  83         }
  84     }
  85     return result;
  86 }
  87 
  88 /*
  89  * Class:     java_net_SocketInputStream
  90  * Method:    socketRead0
  91  * Signature: (Ljava/io/FileDescriptor;[BIII)I
  92  */
  93 JNIEXPORT jint JNICALL
  94 Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this,
  95                                             jobject fdObj, jbyteArray data,
  96                                             jint off, jint len, jint timeout)
  97 {
  98     char BUF[MAX_BUFFER_LEN];
  99     char *bufP;
 100     jint fd, nread;
 101     
 102     if (IS_NULL(fdObj)) {
 103         /* shouldn't this be a NullPointerException? -br */
 104         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
 105                         "Socket closed");
 106         return -1;
 107     } else {
 108         fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
 109         /* Bug 4086704 - If the Socket associated with this file descriptor
 110          * was closed (sysCloseFD), then the file descriptor is set to -1.
 111          */
 112         if (fd == -1) {
 113             JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
 114             return -1;
 115         }
 116     }
 117 
 118     /*
 119      * If the read is greater than our stack allocated buffer then
 120      * we allocate from the heap (up to a limit)
 121      */
 122     if (len > MAX_BUFFER_LEN) {
 123         if (len > MAX_HEAP_BUFFER_LEN) {
 124             len = MAX_HEAP_BUFFER_LEN;
 125         }
 126         bufP = (char *)malloc((size_t)len);
 127         if (bufP == NULL) {
 128             bufP = BUF;
 129             len = MAX_BUFFER_LEN;
 130         }
 131     } else {
 132         bufP = BUF;
 133     }
 134     if (timeout) {
 135         nread = NET_ReadWithTimeout(env, fd, bufP, len, timeout);
 136         if ((*env)->ExceptionCheck(env)) {
 137             if (bufP != BUF) {
 138                 free(bufP);
 139             }
 140             return nread;
 141         }
 142     } else {
 143         nread = NET_Read(fd, bufP, len);
 144     }
 145     
 146     if (nread <= 0) {
 147         if (nread < 0) {
 148 
 149             switch (errno) {
 150                 case ECONNRESET:
 151                 case EPIPE:
 152                     JNU_ThrowByName(env, "sun/net/ConnectionResetException",
 153                         "Connection reset");
 154                     break;
 155 
 156                 case EBADF:
 157                     JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
 158                         "Socket closed");
 159                     break;
 160 
 161                 case EINTR:
 162                      JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
 163                            "Operation interrupted");
 164                      break;
 165                 default:
 166                     JNU_ThrowByNameWithMessageAndLastError
 167                         (env, JNU_JAVANETPKG "SocketException", "Read failed");
 168             }
 169         }
 170     } else {
 171         (*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP);
 172     }
 173 
 174     if (bufP != BUF) {
 175         free(bufP);
 176     }
 177     return nread;
 178 }