1 /*
  2  * Copyright (c) 2000, 2025, 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 <io.h>
 29 #include "jni.h"
 30 #include "jni_util.h"
 31 #include "jvm.h"
 32 #include "jlong.h"
 33 
 34 #include "java_lang_Long.h"
 35 #include "nio.h"
 36 #include "nio_util.h"
 37 #include "net_util.h"
 38 #include "sun_nio_ch_IOUtil.h"
 39 
 40 /* field id for jlong 'handle' in java.io.FileDescriptor used for file fds */
 41 static jfieldID handle_fdID;
 42 
 43 /* field id for jint 'fd' in java.io.FileDescriptor used for socket fds */
 44 static jfieldID fd_fdID;
 45 
 46 JNIEXPORT jboolean JNICALL
 47 Java_sun_security_provider_NativeSeedGenerator_nativeGenerateSeed
 48 (JNIEnv *env, jclass clazz, jbyteArray randArray);
 49 
 50 /**************************************************************
 51  * static method to store field IDs in initializers
 52  */
 53 
 54 JNIEXPORT void JNICALL
 55 Java_sun_nio_ch_IOUtil_initIDs(JNIEnv *env, jclass clazz)
 56 {
 57     CHECK_NULL(clazz = (*env)->FindClass(env, "java/io/FileDescriptor"));
 58     CHECK_NULL(fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I"));
 59     CHECK_NULL(handle_fdID = (*env)->GetFieldID(env, clazz, "handle", "J"));
 60 }
 61 
 62 /**************************************************************
 63  * IOUtil.c
 64  */
 65 JNIEXPORT jboolean JNICALL
 66 Java_sun_nio_ch_IOUtil_randomBytes(JNIEnv *env, jclass clazz,
 67                                   jbyteArray randArray)
 68 {
 69     return
 70         Java_sun_security_provider_NativeSeedGenerator_nativeGenerateSeed(env,
 71                                                                     clazz,
 72                                                                     randArray);
 73 }
 74 
 75 JNIEXPORT jint JNICALL
 76 Java_sun_nio_ch_IOUtil_iovMax(JNIEnv *env, jclass this)
 77 {
 78     return 16;
 79 }
 80 
 81 JNIEXPORT jlong JNICALL
 82 Java_sun_nio_ch_IOUtil_writevMax(JNIEnv *env, jclass this)
 83 {
 84     return java_lang_Long_MAX_VALUE;
 85 }
 86 
 87 jint
 88 convertReturnVal(JNIEnv *env, jint n, jboolean reading)
 89 {
 90     if (n > 0) /* Number of bytes written */
 91         return n;
 92     if (n == 0) {
 93         if (reading) {
 94             return IOS_EOF; /* EOF is -1 in javaland */
 95         } else {
 96             return 0;
 97         }
 98     }
 99     JNU_ThrowIOExceptionWithLastError(env, "Read/write failed");
100     return IOS_THROWN;
101 }
102 
103 jlong
104 convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading)
105 {
106     if (n > 0) /* Number of bytes written */
107         return n;
108     if (n == 0) {
109         if (reading) {
110             return IOS_EOF; /* EOF is -1 in javaland */
111         } else {
112             return 0;
113         }
114     }
115     JNU_ThrowIOExceptionWithLastError(env, "Read/write failed");
116     return IOS_THROWN;
117 }
118 
119 JNIEXPORT jint JNICALL
120 Java_sun_nio_ch_IOUtil_fdVal(JNIEnv *env, jclass clazz, jobject fdo)
121 {
122     return fdval(env, fdo);
123 }
124 
125 JNIEXPORT void JNICALL
126 Java_sun_nio_ch_IOUtil_setfdVal(JNIEnv *env, jclass clazz, jobject fdo, jint val)
127 {
128     setfdval(env, fdo, val);
129 }
130 
131 
132 #define SET_BLOCKING 0
133 #define SET_NONBLOCKING 1
134 
135 JNIEXPORT void JNICALL
136 Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz,
137                                          jint fd, jboolean blocking)
138 {
139     u_long argp;
140     int result = 0;
141 
142     if (blocking == JNI_FALSE) {
143         argp = SET_NONBLOCKING;
144     } else {
145         argp = SET_BLOCKING;
146         /* Blocking fd cannot be registered with EventSelect */
147         WSAEventSelect(fd, NULL, 0);
148     }
149     result = ioctlsocket(fd, FIONBIO, &argp);
150     if (result == SOCKET_ERROR) {
151         NET_ThrowNew(env, WSAGetLastError(), "ioctlsocket");
152     }
153 }
154 
155 JNIEXPORT jboolean JNICALL
156 Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd)
157 {
158     char buf[16];
159     jboolean readBytes = JNI_FALSE;
160     for (;;) {
161         int n = recv((SOCKET) fd, buf, sizeof(buf), 0);
162         if (n == SOCKET_ERROR) {
163             if (WSAGetLastError() != WSAEWOULDBLOCK) {
164                 JNU_ThrowIOExceptionWithLastError(env, "recv failed");
165             }
166             return readBytes;
167         }
168         if (n <= 0)
169             return readBytes;
170         if (n < (int)sizeof(buf))
171             return JNI_TRUE;
172         readBytes = JNI_TRUE;
173     }
174 }
175 
176 JNIEXPORT jint JNICALL
177 Java_sun_nio_ch_IOUtil_write1(JNIEnv *env, jclass cl, jint fd, jbyte b)
178 {
179     int n = send((SOCKET) fd, &b, 1, 0);
180     if (n == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK) {
181         JNU_ThrowIOExceptionWithLastError(env, "send failed");
182         return IOS_THROWN;
183     }
184     return (n == 1) ? 1 : 0;
185 }
186 
187 /* Note: This function returns the int fd value from file descriptor.
188    It is mostly used for sockets which should use the int fd value.
189 */
190 jint
191 fdval(JNIEnv *env, jobject fdo)
192 {
193     return (*env)->GetIntField(env, fdo, fd_fdID);
194 }
195 
196 void
197 setfdval(JNIEnv *env, jobject fdo, jint val)
198 {
199     (*env)->SetIntField(env, fdo, fd_fdID, val);
200 }
201 
202 jlong
203 handleval(JNIEnv *env, jobject fdo)
204 {
205     return (*env)->GetLongField(env, fdo, handle_fdID);
206 }