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 = -1;
  56     long prevtime = 0, newtime;
  57     struct timeval t;
  58     gettimeofday(&t, NULL);
  59     prevtime = t.tv_sec * 1000 + t.tv_usec / 1000;
  60     while (timeout > 0) {
  61         result = NET_Timeout(fd, timeout);
  62         if (result <= 0) {
  63             if (result == 0) {
  64                 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", "Read timed out");
  65             } else if (result == -1) {
  66                 if (errno == EBADF) {
  67                     JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
  68                 } else if (errno == ENOMEM) {
  69                     JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
  70                 } else {
  71                     JNU_ThrowByNameWithMessageAndLastError
  72                             (env, JNU_JAVANETPKG "SocketException", "select/poll failed");
  73                 }
  74             }
  75             return -1;
  76         }
  77         result = NET_NonBlockingRead(fd, bufP, len);
  78         if (result == -1 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) {
  79             gettimeofday(&t, NULL);
  80             newtime = t.tv_sec * 1000 + t.tv_usec / 1000;
  81             timeout -= newtime - prevtime;
  82             if (timeout > 0) {
  83                 prevtime = newtime;
  84             }
  85         } else {
  86             break;
  87         }
  88     }
  89     return result;
  90 }
  91 
  92 /*
  93  * Class:     java_net_SocketInputStream
  94  * Method:    socketRead0
  95  * Signature: (Ljava/io/FileDescriptor;[BIII)I
  96  */
  97 JNIEXPORT jint JNICALL
  98 Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this,
  99                                             jobject fdObj, jbyteArray data,
 100                                             jint off, jint len, jint timeout)
 101 {
 102     char BUF[MAX_BUFFER_LEN];
 103     char *bufP;
 104     jint fd, nread;
 105     
 106     if (IS_NULL(fdObj)) {
 107         /* shouldn't this be a NullPointerException? -br */
 108         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
 109                         "Socket closed");
 110         return -1;
 111     } else {
 112         fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
 113         /* Bug 4086704 - If the Socket associated with this file descriptor
 114          * was closed (sysCloseFD), then the file descriptor is set to -1.
 115          */
 116         if (fd == -1) {
 117             JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
 118             return -1;
 119         }
 120     }
 121 
 122     /*
 123      * If the read is greater than our stack allocated buffer then
 124      * we allocate from the heap (up to a limit)
 125      */
 126     if (len > MAX_BUFFER_LEN) {
 127         if (len > MAX_HEAP_BUFFER_LEN) {
 128             len = MAX_HEAP_BUFFER_LEN;
 129         }
 130         bufP = (char *)malloc((size_t)len);
 131         if (bufP == NULL) {
 132             bufP = BUF;
 133             len = MAX_BUFFER_LEN;
 134         }
 135     } else {
 136         bufP = BUF;
 137     }

 138     if (timeout) {
 139         nread = NET_ReadWithTimeout(env, fd, bufP, len, timeout);









 140     } else {











 141         nread = NET_Read(fd, bufP, len);
 142     }
 143     if (nread <= 0) {
 144         if (nread < 0) {
 145 
 146             switch (errno) {
 147                 case ECONNRESET:
 148                 case EPIPE:
 149                     JNU_ThrowByName(env, "sun/net/ConnectionResetException",
 150                         "Connection reset");
 151                     break;
 152 
 153                 case EBADF:
 154                     JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
 155                         "Socket closed");
 156                     break;
 157 
 158                 case EINTR:
 159                      JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
 160                            "Operation interrupted");
 161                      break;

 162                 default:
 163                     JNU_ThrowByNameWithMessageAndLastError
 164                         (env, JNU_JAVANETPKG "SocketException", "Read failed");
 165             }
 166         }
 167     } else {
 168         (*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP);
 169     }
 170 
 171     if (bufP != BUF) {
 172         free(bufP);
 173     }
 174     return nread;
 175 }
--- EOF ---