0N/A/*
3261N/A * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/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
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/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.
0N/A */
0N/A
0N/A#include <windows.h>
0N/A#include <winsock2.h>
0N/A#include <ctype.h>
0N/A#include "jni.h"
0N/A#include "jni_util.h"
0N/A#include "jvm.h"
0N/A#include "jlong.h"
0N/A#include "sun_nio_ch_SocketChannelImpl.h"
0N/A
0N/A#include "nio.h"
0N/A#include "nio_util.h"
0N/A#include "net_util.h"
0N/A
0N/A
0N/Astatic jfieldID ia_addrID; /* java.net.InetAddress.address */
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_nio_ch_SocketChannelImpl_initIDs(JNIEnv *env, jclass cls)
0N/A{
0N/A cls = (*env)->FindClass(env, "java/net/InetAddress");
0N/A ia_addrID = (*env)->GetFieldID(env, cls, "address", "I");
0N/A}
0N/A
0N/Ajint
0N/AhandleSocketError(JNIEnv *env, int errorValue)
0N/A{
0N/A NET_ThrowNew(env, errorValue, NULL);
0N/A return IOS_THROWN;
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_nio_ch_SocketChannelImpl_checkConnect(JNIEnv *env, jobject this,
0N/A jobject fdo, jboolean block,
0N/A jboolean ready)
0N/A{
0N/A int optError = 0;
0N/A int lastError = 0;
0N/A int result = 0;
0N/A int retry = 0;
0N/A int n = sizeof(int);
0N/A jint fd = fdval(env, fdo);
0N/A fd_set wr, ex;
0N/A struct timeval t = { 0, 0 };
0N/A
0N/A FD_ZERO(&wr);
0N/A FD_ZERO(&ex);
0N/A FD_SET((u_int)fd, &wr);
0N/A FD_SET((u_int)fd, &ex);
0N/A
0N/A result = select(fd+1, 0, &wr, &ex, block ? NULL : &t);
0N/A
0N/A /* save last winsock error */
0N/A if (result == SOCKET_ERROR) {
0N/A lastError = WSAGetLastError();
0N/A }
0N/A
0N/A if (block) { /* must configure socket back to blocking state */
0N/A u_long argp = 0;
0N/A int r = ioctlsocket(fd, FIONBIO, &argp);
0N/A if (r == SOCKET_ERROR) {
0N/A handleSocketError(env, WSAGetLastError());
0N/A }
0N/A }
0N/A
0N/A if (result == 0) { /* timeout */
0N/A return block ? 0 : IOS_UNAVAILABLE;
0N/A } else {
0N/A if (result == SOCKET_ERROR) { /* select failed */
0N/A handleSocketError(env, lastError);
0N/A return IOS_THROWN;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Socket is writable or error occured. On some Windows editions
0N/A * the socket will appear writable when the connect fails so we
0N/A * check for error rather than writable.
0N/A */
0N/A if (!FD_ISSET(fd, &ex)) {
0N/A return 1; /* connection established */
0N/A }
0N/A
0N/A /*
0N/A * A getsockopt( SO_ERROR ) may indicate success on NT4 even
0N/A * though the connection has failed. The workaround is to allow
0N/A * winsock to be scheduled and this is done via by yielding.
0N/A * As the yield approach is problematic in heavy load situations
0N/A * we attempt up to 3 times to get the failure reason.
0N/A */
0N/A for (retry=0; retry<3; retry++) {
0N/A result = getsockopt((SOCKET)fd,
0N/A SOL_SOCKET,
0N/A SO_ERROR,
0N/A (char *)&optError,
0N/A &n);
0N/A if (result == SOCKET_ERROR) {
0N/A int lastError = WSAGetLastError();
0N/A if (lastError == WSAEINPROGRESS) {
0N/A return IOS_UNAVAILABLE;
0N/A }
0N/A NET_ThrowNew(env, lastError, "getsockopt");
0N/A return IOS_THROWN;
0N/A }
0N/A if (optError) {
0N/A break;
0N/A }
0N/A Sleep(0);
0N/A }
0N/A
0N/A if (optError != NO_ERROR) {
0N/A handleSocketError(env, optError);
0N/A return IOS_THROWN;
0N/A }
0N/A
0N/A return 0;
0N/A}
2614N/A
2614N/AJNIEXPORT jint JNICALL
2614N/AJava_sun_nio_ch_SocketChannelImpl_sendOutOfBandData(JNIEnv* env, jclass this,
2614N/A jobject fdo, jbyte b)
2614N/A{
2614N/A int n = send(fdval(env, fdo), (const char*)&b, 1, MSG_OOB);
2614N/A if (n == SOCKET_ERROR) {
2614N/A handleSocketError(env, WSAGetLastError());
2614N/A return IOS_THROWN;
2614N/A } else {
2614N/A return n;
2614N/A }
2614N/A}