1 /*
   2  * Copyright (c) 1997, 2013, 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 <windows.h>
  27 #include <winsock2.h>
  28 #include <ctype.h>
  29 #include <stdio.h>
  30 #include <stdlib.h>
  31 #include <malloc.h>
  32 #include <sys/types.h>
  33 
  34 #include "java_net_SocketInputStream.h"
  35 
  36 #include "net_util.h"
  37 #include "jni_util.h"
  38 
  39 /*************************************************************************
  40  * SocketInputStream
  41  */
  42 
  43 static jfieldID IO_fd_fdID;
  44 
  45 /*
  46  * Class:     java_net_SocketInputStream
  47  * Method:    init
  48  * Signature: ()V
  49  */
  50 JNIEXPORT void JNICALL
  51 Java_java_net_SocketInputStream_init(JNIEnv *env, jclass cls) {
  52     IO_fd_fdID = NET_GetFileDescriptorID(env);
  53 }
  54 
  55 /*
  56  * Class:     java_net_SocketInputStream
  57  * Method:    socketRead
  58  * Signature: (Ljava/io/FileDescriptor;[BIII)I
  59  */
  60 JNIEXPORT jint JNICALL
  61 Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this,
  62                                             jobject fdObj, jbyteArray data,
  63                                             jint off, jint len, jint timeout)
  64 {
  65     char *bufP;
  66     char BUF[MAX_BUFFER_LEN];
  67     jint fd, newfd;
  68     jint nread;
  69 
  70     if (IS_NULL(fdObj)) {
  71         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed");
  72         return -1;
  73     }
  74     fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
  75     if (fd == -1) {
  76         NET_ThrowSocketException(env, "Socket closed");
  77         return -1;
  78     }
  79 
  80     /*
  81      * If the caller buffer is large than our stack buffer then we allocate
  82      * from the heap (up to a limit). If memory is exhausted we always use
  83      * the stack buffer.
  84      */
  85     if (len <= MAX_BUFFER_LEN) {
  86         bufP = BUF;
  87     } else {
  88         if (len > MAX_HEAP_BUFFER_LEN) {
  89             len = MAX_HEAP_BUFFER_LEN;
  90         }
  91         bufP = (char *)malloc((size_t)len);
  92         if (bufP == NULL) {
  93             /* allocation failed so use stack buffer */
  94             bufP = BUF;
  95             len = MAX_BUFFER_LEN;
  96         }
  97     }
  98 
  99 
 100     if (timeout) {
 101         if (timeout <= 5000 || !isRcvTimeoutSupported) {
 102             int ret = NET_Timeout (fd, timeout);
 103 
 104             if (ret <= 0) {
 105                 if (ret == 0) {
 106                     JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
 107                                     "Read timed out");
 108                 } else if (ret == -1) {
 109                     JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed");
 110                 }
 111                 if (bufP != BUF) {
 112                     free(bufP);
 113                 }
 114                 return -1;
 115             }
 116 
 117             /*check if the socket has been closed while we were in timeout*/
 118             newfd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
 119             if (newfd == -1) {
 120                 NET_ThrowSocketException(env, "Socket Closed");
 121                 if (bufP != BUF) {
 122                     free(bufP);
 123                 }
 124                 return -1;
 125             }
 126         }
 127     }
 128 
 129     nread = recv(fd, bufP, len, 0);
 130     if (nread > 0) {
 131         (*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP);
 132     } else {
 133         if (nread < 0) {
 134             // Check if the socket has been closed since we last checked.
 135             // This could be a reason for recv failing.
 136             if ((*env)->GetIntField(env, fdObj, IO_fd_fdID) == -1) {
 137                 NET_ThrowSocketException(env, "Socket closed");
 138             } else {
 139                 switch (WSAGetLastError()) {
 140                     case WSAEINTR:
 141                         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
 142                             "socket closed");
 143                         break;
 144 
 145                     case WSAECONNRESET:
 146                     case WSAESHUTDOWN:
 147                         /*
 148                          * Connection has been reset - Windows sometimes reports
 149                          * the reset as a shutdown error.
 150                          */
 151                         JNU_ThrowByName(env, "sun/net/ConnectionResetException",
 152                             "");
 153                         break;
 154 
 155                     case WSAETIMEDOUT :
 156                         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
 157                                        "Read timed out");
 158                         break;
 159 
 160                     default:
 161                         NET_ThrowCurrent(env, "recv failed");
 162                 }
 163             }
 164         }
 165     }
 166     if (bufP != BUF) {
 167         free(bufP);
 168     }
 169     return nread;
 170 }