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 <stdio.h>
0N/A#include <stdlib.h>
0N/A#include <malloc.h>
0N/A#include <sys/types.h>
0N/A
0N/A#include "jni.h"
0N/A#include "jni_util.h"
0N/A#include "jvm.h"
0N/A#include "jlong.h"
0N/A
0N/A#include "nio.h"
0N/A#include "nio_util.h"
0N/A#include "net_util.h"
0N/A
0N/A#include "sun_nio_ch_ServerSocketChannelImpl.h"
0N/A
0N/A
0N/Astatic jfieldID fd_fdID; /* java.io.FileDescriptor.fd */
0N/Astatic jclass isa_class; /* java.net.InetSocketAddress */
0N/Astatic jmethodID isa_ctorID; /* InetSocketAddress(InetAddress, int) */
0N/A
0N/A
0N/A/**************************************************************
0N/A * static method to store field IDs in initializers
0N/A */
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_nio_ch_ServerSocketChannelImpl_initIDs(JNIEnv *env, jclass cls)
0N/A{
0N/A cls = (*env)->FindClass(env, "java/io/FileDescriptor");
0N/A fd_fdID = (*env)->GetFieldID(env, cls, "fd", "I");
0N/A
0N/A cls = (*env)->FindClass(env, "java/net/InetSocketAddress");
0N/A isa_class = (*env)->NewGlobalRef(env, cls);
0N/A isa_ctorID = (*env)->GetMethodID(env, cls, "<init>",
0N/A "(Ljava/net/InetAddress;I)V");
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_nio_ch_ServerSocketChannelImpl_listen(JNIEnv *env, jclass cl,
0N/A jobject fdo, jint backlog)
0N/A{
0N/A if (listen(fdval(env,fdo), backlog) == SOCKET_ERROR) {
0N/A NET_ThrowNew(env, WSAGetLastError(), "listen");
0N/A }
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_nio_ch_ServerSocketChannelImpl_accept0(JNIEnv *env, jobject this,
0N/A jobject ssfdo, jobject newfdo,
0N/A jobjectArray isaa)
0N/A{
0N/A jint ssfd = (*env)->GetIntField(env, ssfdo, fd_fdID);
0N/A jint newfd;
524N/A SOCKETADDRESS sa;
524N/A jobject remote_ia;
524N/A int remote_port;
0N/A jobject isa;
0N/A int addrlen = sizeof(sa);
0N/A
0N/A memset((char *)&sa, 0, sizeof(sa));
0N/A newfd = (jint)accept(ssfd, (struct sockaddr *)&sa, &addrlen);
0N/A if (newfd == INVALID_SOCKET) {
0N/A int theErr = (jint)WSAGetLastError();
0N/A if (theErr == WSAEWOULDBLOCK) {
0N/A return IOS_UNAVAILABLE;
0N/A }
0N/A JNU_ThrowIOExceptionWithLastError(env, "Accept failed");
0N/A return IOS_THROWN;
0N/A }
524N/A
0N/A (*env)->SetIntField(env, newfdo, fd_fdID, newfd);
524N/A remote_ia = NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, (int *)&remote_port);
0N/A
524N/A isa = (*env)->NewObject(env, isa_class, isa_ctorID,
524N/A remote_ia, remote_port);
524N/A (*env)->SetObjectArrayElement(env, isaa, 0, isa);
0N/A
0N/A return 1;
0N/A}