Path: blob/master/src/java.base/windows/native/libnio/ch/WindowsAsynchronousSocketChannelImpl.c
41134 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 = 0;95BOOL res;9697if (NET_InetAddressToSockaddr(env, iao, port, &sa, &sa_len,98preferIPv6) != 0) {99return IOS_THROWN;100}101102ZeroMemory((PVOID)lpOverlapped, sizeof(OVERLAPPED));103104res = (*ConnectEx_func)(s, &sa.sa, sa_len, NULL, 0, NULL, lpOverlapped);105if (res == 0) {106int error = GetLastError();107if (error == ERROR_IO_PENDING) {108return IOS_UNAVAILABLE;109}110JNU_ThrowIOExceptionWithLastError(env, "ConnectEx failed");111return IOS_THROWN;112}113return 0;114}115116JNIEXPORT void JNICALL117Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_updateConnectContext(JNIEnv* env, jclass this,118jlong socket)119{120SOCKET s = (SOCKET)jlong_to_ptr(socket);121setsockopt(s, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, NULL, 0);122}123124125JNIEXPORT void JNICALL126Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_shutdown0(JNIEnv *env, jclass cl,127jlong socket, jint how)128{129SOCKET s =(SOCKET) jlong_to_ptr(socket);130if (shutdown(s, how) == SOCKET_ERROR) {131JNU_ThrowIOExceptionWithLastError(env, "shutdown failed");132}133}134135136JNIEXPORT void JNICALL137Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_closesocket0(JNIEnv* env, jclass this,138jlong socket)139{140SOCKET s = (SOCKET)jlong_to_ptr(socket);141if (closesocket(s) == SOCKET_ERROR)142JNU_ThrowIOExceptionWithLastError(env, "closesocket failed");143}144145146JNIEXPORT jint JNICALL147Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_read0(JNIEnv* env, jclass this,148jlong socket, jint count, jlong address, jlong ov)149{150SOCKET s = (SOCKET) jlong_to_ptr(socket);151WSABUF* lpWsaBuf = (WSABUF*) jlong_to_ptr(address);152OVERLAPPED* lpOverlapped = (OVERLAPPED*) jlong_to_ptr(ov);153BOOL res;154DWORD flags = 0;155156ZeroMemory((PVOID)lpOverlapped, sizeof(OVERLAPPED));157res = WSARecv(s,158lpWsaBuf,159(DWORD)count,160NULL,161&flags,162lpOverlapped,163NULL);164165if (res == SOCKET_ERROR) {166int error = WSAGetLastError();167if (error == WSA_IO_PENDING) {168return IOS_UNAVAILABLE;169}170if (error == WSAESHUTDOWN) {171return IOS_EOF; // input shutdown172}173JNU_ThrowIOExceptionWithLastError(env, "WSARecv failed");174return IOS_THROWN;175}176return IOS_UNAVAILABLE;177}178179JNIEXPORT jint JNICALL180Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_write0(JNIEnv* env, jclass this,181jlong socket, jint count, jlong address, jlong ov)182{183SOCKET s = (SOCKET) jlong_to_ptr(socket);184WSABUF* lpWsaBuf = (WSABUF*) jlong_to_ptr(address);185OVERLAPPED* lpOverlapped = (OVERLAPPED*) jlong_to_ptr(ov);186BOOL res;187188ZeroMemory((PVOID)lpOverlapped, sizeof(OVERLAPPED));189res = WSASend(s,190lpWsaBuf,191(DWORD)count,192NULL,1930,194lpOverlapped,195NULL);196197if (res == SOCKET_ERROR) {198int error = WSAGetLastError();199if (error == WSA_IO_PENDING) {200return IOS_UNAVAILABLE;201}202if (error == WSAESHUTDOWN) {203return IOS_EOF; // output shutdown204}205JNU_ThrowIOExceptionWithLastError(env, "WSASend failed");206return IOS_THROWN;207}208return IOS_UNAVAILABLE;209}210211212