893N/A/*
2362N/A * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/A#include <windows.h>
893N/A#include <winsock2.h>
893N/A
893N/A#include "jni.h"
893N/A#include "jni_util.h"
893N/A#include "jlong.h"
893N/A#include "nio.h"
893N/A#include "nio_util.h"
893N/A#include "net_util.h"
893N/A
893N/A#include "sun_nio_ch_WindowsAsynchronousServerSocketChannelImpl.h"
893N/A
893N/A
893N/A#ifndef WSAID_ACCEPTEX
893N/A#define WSAID_ACCEPTEX {0xb5367df1,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
893N/A#endif
893N/A
893N/A#ifndef SO_UPDATE_ACCEPT_CONTEXT
893N/A#define SO_UPDATE_ACCEPT_CONTEXT 0x700B
893N/A#endif
893N/A
893N/A
893N/Atypedef BOOL (*AcceptEx_t)
893N/A(
893N/A SOCKET sListenSocket,
893N/A SOCKET sAcceptSocket,
893N/A PVOID lpOutputBuffer,
893N/A DWORD dwReceiveDataLength,
893N/A DWORD dwLocalAddressLength,
893N/A DWORD dwRemoteAddressLength,
893N/A LPDWORD lpdwBytesReceived,
893N/A LPOVERLAPPED lpOverlapped
893N/A);
893N/A
893N/A
893N/Astatic AcceptEx_t AcceptEx_func;
893N/A
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_ch_WindowsAsynchronousServerSocketChannelImpl_initIDs(JNIEnv* env, jclass this) {
893N/A GUID GuidAcceptEx = WSAID_ACCEPTEX;
893N/A SOCKET s;
893N/A int rv;
893N/A DWORD dwBytes;
893N/A
893N/A s = socket(AF_INET, SOCK_STREAM, 0);
893N/A if (s == INVALID_SOCKET) {
893N/A JNU_ThrowIOExceptionWithLastError(env, "socket failed");
893N/A return;
893N/A }
893N/A rv = WSAIoctl(s,
893N/A SIO_GET_EXTENSION_FUNCTION_POINTER,
893N/A (LPVOID)&GuidAcceptEx,
893N/A sizeof(GuidAcceptEx),
893N/A &AcceptEx_func,
893N/A sizeof(AcceptEx_func),
893N/A &dwBytes,
893N/A NULL,
893N/A NULL);
893N/A if (rv != 0)
893N/A JNU_ThrowIOExceptionWithLastError(env, "WSAIoctl failed");
893N/A closesocket(s);
893N/A}
893N/A
893N/AJNIEXPORT jint JNICALL
893N/AJava_sun_nio_ch_WindowsAsynchronousServerSocketChannelImpl_accept0(JNIEnv* env, jclass this,
893N/A jlong listenSocket, jlong acceptSocket, jlong ov, jlong buf)
893N/A{
893N/A BOOL res;
893N/A SOCKET s1 = (SOCKET)jlong_to_ptr(listenSocket);
893N/A SOCKET s2 = (SOCKET)jlong_to_ptr(acceptSocket);
893N/A PVOID outputBuffer = (PVOID)jlong_to_ptr(buf);
893N/A
893N/A DWORD nread = 0;
893N/A OVERLAPPED* lpOverlapped = (OVERLAPPED*)jlong_to_ptr(ov);
893N/A ZeroMemory((PVOID)lpOverlapped, sizeof(OVERLAPPED));
893N/A
893N/A res = (*AcceptEx_func)(s1,
893N/A s2,
893N/A outputBuffer,
893N/A 0,
893N/A sizeof(SOCKETADDRESS)+16,
893N/A sizeof(SOCKETADDRESS)+16,
893N/A &nread,
893N/A lpOverlapped);
893N/A if (res == 0) {
893N/A int error = WSAGetLastError();
893N/A if (error == ERROR_IO_PENDING) {
893N/A return IOS_UNAVAILABLE;
893N/A }
893N/A JNU_ThrowIOExceptionWithLastError(env, "AcceptEx failed");
893N/A return IOS_THROWN;
893N/A }
893N/A
893N/A return 0;
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_ch_WindowsAsynchronousServerSocketChannelImpl_updateAcceptContext(JNIEnv* env, jclass this,
893N/A jlong listenSocket, jlong acceptSocket)
893N/A{
893N/A SOCKET s1 = (SOCKET)jlong_to_ptr(listenSocket);
893N/A SOCKET s2 = (SOCKET)jlong_to_ptr(acceptSocket);
893N/A
893N/A setsockopt(s2, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, (char *)&s1, sizeof(s1));
893N/A}
893N/A
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_ch_WindowsAsynchronousServerSocketChannelImpl_closesocket0(JNIEnv* env, jclass this,
893N/A jlong socket)
893N/A{
893N/A SOCKET s = (SOCKET)jlong_to_ptr(socket);
893N/A
893N/A if (closesocket(s) == SOCKET_ERROR)
893N/A JNU_ThrowIOExceptionWithLastError(env, "closesocket failed");
893N/A}