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