Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/sun/nio/ch/WindowsAsynchronousSocketChannelImpl.c
32288 views
/*1* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <windows.h>26#include <winsock2.h>27#include <stddef.h>2829#include "jni.h"30#include "jni_util.h"31#include "jlong.h"32#include "nio.h"33#include "nio_util.h"34#include "net_util.h"3536#include "sun_nio_ch_WindowsAsynchronousSocketChannelImpl.h"3738#ifndef WSAID_CONNECTEX39#define WSAID_CONNECTEX {0x25a207b9,0xddf3,0x4660,{0x8e,0xe9,0x76,0xe5,0x8c,0x74,0x06,0x3e}}40#endif4142#ifndef SO_UPDATE_CONNECT_CONTEXT43#define SO_UPDATE_CONNECT_CONTEXT 0x701044#endif4546typedef BOOL (PASCAL *ConnectEx_t)47(48SOCKET s,49const struct sockaddr* name,50int namelen,51PVOID lpSendBuffer,52DWORD dwSendDataLength,53LPDWORD lpdwBytesSent,54LPOVERLAPPED lpOverlapped55);5657static ConnectEx_t ConnectEx_func;585960JNIEXPORT void JNICALL61Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_initIDs(JNIEnv* env, jclass this) {62GUID GuidConnectEx = WSAID_CONNECTEX;63SOCKET s;64int rv;65DWORD dwBytes;6667s = socket(AF_INET, SOCK_STREAM, 0);68if (s == INVALID_SOCKET) {69JNU_ThrowIOExceptionWithLastError(env, "socket failed");70return;71}72rv = WSAIoctl(s,73SIO_GET_EXTENSION_FUNCTION_POINTER,74(LPVOID)&GuidConnectEx,75sizeof(GuidConnectEx),76&ConnectEx_func,77sizeof(ConnectEx_func),78&dwBytes,79NULL,80NULL);81if (rv != 0)82JNU_ThrowIOExceptionWithLastError(env, "WSAIoctl failed");83closesocket(s);84}8586JNIEXPORT jint JNICALL87Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_connect0(JNIEnv* env, jclass this,88jlong socket, jboolean preferIPv6, jobject iao, jint port, jlong ov)89{90SOCKET s = (SOCKET) jlong_to_ptr(socket);91OVERLAPPED* lpOverlapped = (OVERLAPPED*) jlong_to_ptr(ov);9293SOCKETADDRESS sa;94int sa_len;95BOOL res;9697if (NET_InetAddressToSockaddr(env, iao, port, (struct sockaddr *)&sa, &sa_len, preferIPv6) != 0) {98return IOS_THROWN;99}100101ZeroMemory((PVOID)lpOverlapped, sizeof(OVERLAPPED));102103res = (*ConnectEx_func)(s,104(struct sockaddr *)&sa,105sa_len,106NULL,1070,108NULL,109lpOverlapped);110if (res == 0) {111int error = GetLastError();112if (error == ERROR_IO_PENDING) {113return IOS_UNAVAILABLE;114}115JNU_ThrowIOExceptionWithLastError(env, "ConnectEx failed");116return IOS_THROWN;117}118return 0;119}120121JNIEXPORT void JNICALL122Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_updateConnectContext(JNIEnv* env, jclass this,123jlong socket)124{125SOCKET s = (SOCKET)jlong_to_ptr(socket);126setsockopt(s, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, NULL, 0);127}128129130JNIEXPORT void JNICALL131Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_shutdown0(JNIEnv *env, jclass cl,132jlong socket, jint how)133{134SOCKET s =(SOCKET) jlong_to_ptr(socket);135if (shutdown(s, how) == SOCKET_ERROR) {136JNU_ThrowIOExceptionWithLastError(env, "shutdown failed");137}138}139140141JNIEXPORT void JNICALL142Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_closesocket0(JNIEnv* env, jclass this,143jlong socket)144{145SOCKET s = (SOCKET)jlong_to_ptr(socket);146if (closesocket(s) == SOCKET_ERROR)147JNU_ThrowIOExceptionWithLastError(env, "closesocket failed");148}149150151JNIEXPORT jint JNICALL152Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_read0(JNIEnv* env, jclass this,153jlong socket, jint count, jlong address, jlong ov)154{155SOCKET s = (SOCKET) jlong_to_ptr(socket);156WSABUF* lpWsaBuf = (WSABUF*) jlong_to_ptr(address);157OVERLAPPED* lpOverlapped = (OVERLAPPED*) jlong_to_ptr(ov);158BOOL res;159DWORD flags = 0;160161ZeroMemory((PVOID)lpOverlapped, sizeof(OVERLAPPED));162res = WSARecv(s,163lpWsaBuf,164(DWORD)count,165NULL,166&flags,167lpOverlapped,168NULL);169170if (res == SOCKET_ERROR) {171int error = WSAGetLastError();172if (error == WSA_IO_PENDING) {173return IOS_UNAVAILABLE;174}175if (error == WSAESHUTDOWN) {176return IOS_EOF; // input shutdown177}178JNU_ThrowIOExceptionWithLastError(env, "WSARecv failed");179return IOS_THROWN;180}181return IOS_UNAVAILABLE;182}183184JNIEXPORT jint JNICALL185Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_write0(JNIEnv* env, jclass this,186jlong socket, jint count, jlong address, jlong ov)187{188SOCKET s = (SOCKET) jlong_to_ptr(socket);189WSABUF* lpWsaBuf = (WSABUF*) jlong_to_ptr(address);190OVERLAPPED* lpOverlapped = (OVERLAPPED*) jlong_to_ptr(ov);191BOOL res;192193ZeroMemory((PVOID)lpOverlapped, sizeof(OVERLAPPED));194res = WSASend(s,195lpWsaBuf,196(DWORD)count,197NULL,1980,199lpOverlapped,200NULL);201202if (res == SOCKET_ERROR) {203int error = WSAGetLastError();204if (error == WSA_IO_PENDING) {205return IOS_UNAVAILABLE;206}207if (error == WSAESHUTDOWN) {208return IOS_EOF; // output shutdown209}210JNU_ThrowIOExceptionWithLastError(env, "WSASend failed");211return IOS_THROWN;212}213return IOS_UNAVAILABLE;214}215216217