0N/A/*
3261N/A * Copyright (c) 2001, 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 "jni.h"
0N/A#include "jni_util.h"
0N/A#include "jvm.h"
0N/A#include "jlong.h"
0N/A#include <io.h>
0N/A#include "sun_nio_ch_DatagramChannelImpl.h"
0N/A#include "nio.h"
0N/A#include "nio_util.h"
0N/A#include "net_util.h"
0N/A#include <winsock2.h>
0N/A
0N/Astatic jfieldID dci_senderID; /* sender in sun.nio.ch.DatagramChannelImpl */
0N/Astatic jfieldID dci_senderAddrID; /* sender InetAddress in sun.nio.ch.DatagramChannelImpl */
0N/Astatic jfieldID dci_senderPortID; /* sender port in sun.nio.ch.DatagramChannelImpl */
0N/Astatic jclass isa_class; /* java.net.InetSocketAddress */
524N/Astatic jmethodID isa_ctorID; /* java.net.InetSocketAddress(InetAddress, int) */
0N/A
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_nio_ch_DatagramChannelImpl_initIDs(JNIEnv *env, jclass clazz)
0N/A{
0N/A clazz = (*env)->FindClass(env, "java/net/InetSocketAddress");
0N/A isa_class = (*env)->NewGlobalRef(env, clazz);
0N/A isa_ctorID = (*env)->GetMethodID(env, clazz, "<init>",
0N/A "(Ljava/net/InetAddress;I)V");
0N/A
0N/A clazz = (*env)->FindClass(env, "sun/nio/ch/DatagramChannelImpl");
0N/A dci_senderID = (*env)->GetFieldID(env, clazz, "sender",
0N/A "Ljava/net/SocketAddress;");
0N/A dci_senderAddrID = (*env)->GetFieldID(env, clazz,
0N/A "cachedSenderInetAddress",
0N/A "Ljava/net/InetAddress;");
0N/A dci_senderPortID = (*env)->GetFieldID(env, clazz,
0N/A "cachedSenderPort", "I");
0N/A}
0N/A
0N/A/*
0N/A * This function "purges" all outstanding ICMP port unreachable packets
0N/A * outstanding on a socket and returns JNI_TRUE if any ICMP messages
0N/A * have been purged. The rational for purging is to emulate normal BSD
0N/A * behaviour whereby receiving a "connection reset" status resets the
0N/A * socket.
0N/A */
0N/Ajboolean purgeOutstandingICMP(JNIEnv *env, jclass clazz, jint fd)
0N/A{
0N/A jboolean got_icmp = JNI_FALSE;
0N/A char buf[1];
0N/A fd_set tbl;
0N/A struct timeval t = { 0, 0 };
524N/A SOCKETADDRESS sa;
524N/A int addrlen = sizeof(sa);
0N/A
0N/A /*
0N/A * Peek at the queue to see if there is an ICMP port unreachable. If there
0N/A * is then receive it.
0N/A */
0N/A FD_ZERO(&tbl);
0N/A FD_SET((u_int)fd, &tbl);
0N/A while(1) {
0N/A if (select(/*ignored*/fd+1, &tbl, 0, 0, &t) <= 0) {
0N/A break;
0N/A }
0N/A if (recvfrom(fd, buf, 1, MSG_PEEK,
524N/A (struct sockaddr *)&sa, &addrlen) != SOCKET_ERROR) {
0N/A break;
0N/A }
0N/A if (WSAGetLastError() != WSAECONNRESET) {
0N/A /* some other error - we don't care here */
0N/A break;
0N/A }
0N/A
524N/A recvfrom(fd, buf, 1, 0, (struct sockaddr *)&sa, &addrlen);
0N/A got_icmp = JNI_TRUE;
0N/A }
0N/A
0N/A return got_icmp;
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_nio_ch_DatagramChannelImpl_disconnect0(JNIEnv *env, jobject this,
5205N/A jobject fdo, jboolean isIPv6)
0N/A{
0N/A jint fd = fdval(env, fdo);
0N/A int rv = 0;
524N/A SOCKETADDRESS sa;
524N/A int sa_len = sizeof(sa);
0N/A
524N/A memset(&sa, 0, sa_len);
0N/A
524N/A rv = connect((SOCKET)fd, (struct sockaddr *)&sa, sa_len);
0N/A if (rv == SOCKET_ERROR) {
0N/A handleSocketError(env, WSAGetLastError());
2464N/A } else {
2464N/A /* Disable WSAECONNRESET errors as socket is no longer connected */
2464N/A BOOL enable = FALSE;
2464N/A DWORD bytesReturned = 0;
2464N/A WSAIoctl((SOCKET)fd, SIO_UDP_CONNRESET, &enable, sizeof(enable),
2464N/A NULL, 0, &bytesReturned, NULL, NULL);
0N/A }
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this,
0N/A jobject fdo, jlong address,
0N/A jint len, jboolean connected)
0N/A{
0N/A jint fd = fdval(env, fdo);
0N/A void *buf = (void *)jlong_to_ptr(address);
524N/A SOCKETADDRESS sa;
524N/A int sa_len = sizeof(sa);
0N/A BOOL retry = FALSE;
0N/A jint n;
524N/A jobject senderAddr;
0N/A
0N/A do {
0N/A retry = FALSE;
0N/A n = recvfrom((SOCKET)fd,
0N/A (char *)buf,
0N/A len,
0N/A 0,
524N/A (struct sockaddr *)&sa,
0N/A &sa_len);
0N/A
0N/A if (n == SOCKET_ERROR) {
0N/A int theErr = (jint)WSAGetLastError();
0N/A if (theErr == WSAEMSGSIZE) {
0N/A /* Spec says the rest of the data will be discarded... */
0N/A n = len;
0N/A } else if (theErr == WSAECONNRESET) {
0N/A purgeOutstandingICMP(env, this, fd);
0N/A if (connected == JNI_FALSE) {
0N/A retry = TRUE;
0N/A } else {
0N/A JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
0N/A return IOS_THROWN;
0N/A }
0N/A } else if (theErr == WSAEWOULDBLOCK) {
0N/A return IOS_UNAVAILABLE;
0N/A } else return handleSocketError(env, theErr);
0N/A }
0N/A } while (retry);
0N/A
524N/A /*
524N/A * If the source address and port match the cached address
524N/A * and port in DatagramChannelImpl then we don't need to
524N/A * create InetAddress and InetSocketAddress objects.
524N/A */
524N/A senderAddr = (*env)->GetObjectField(env, this, dci_senderAddrID);
524N/A if (senderAddr != NULL) {
524N/A if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&sa,
524N/A senderAddr)) {
524N/A senderAddr = NULL;
524N/A } else {
524N/A jint port = (*env)->GetIntField(env, this, dci_senderPortID);
524N/A if (port != NET_GetPortFromSockaddr((struct sockaddr *)&sa)) {
524N/A senderAddr = NULL;
524N/A }
524N/A }
524N/A }
524N/A if (senderAddr == NULL) {
0N/A jobject isa = NULL;
524N/A int port;
524N/A jobject ia = NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa,
524N/A &port);
0N/A
0N/A if (ia != NULL) {
0N/A isa = (*env)->NewObject(env, isa_class, isa_ctorID, ia, port);
0N/A }
0N/A
0N/A if (isa == NULL) {
0N/A JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
0N/A return IOS_THROWN;
0N/A }
0N/A
0N/A // update cachedSenderInetAddress/cachedSenderPort
0N/A (*env)->SetObjectField(env, this, dci_senderAddrID, ia);
524N/A (*env)->SetIntField(env, this, dci_senderPortID,
524N/A NET_GetPortFromSockaddr((struct sockaddr *)&sa));
0N/A (*env)->SetObjectField(env, this, dci_senderID, isa);
0N/A }
0N/A return n;
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_nio_ch_DatagramChannelImpl_send0(JNIEnv *env, jobject this,
524N/A jboolean preferIPv6, jobject fdo,
5697N/A jlong address, jint len,
5697N/A jobject destAddress, jint destPort)
0N/A{
0N/A jint fd = fdval(env, fdo);
0N/A void *buf = (void *)jlong_to_ptr(address);
524N/A SOCKETADDRESS sa;
524N/A int sa_len;
0N/A jint rv = 0;
0N/A
0N/A if (NET_InetAddressToSockaddr(env, destAddress, destPort,
524N/A (struct sockaddr *)&sa,
524N/A &sa_len, preferIPv6) != 0) {
0N/A return IOS_THROWN;
0N/A }
0N/A
0N/A rv = sendto((SOCKET)fd,
0N/A buf,
0N/A len,
0N/A 0,
524N/A (struct sockaddr *)&sa,
0N/A sa_len);
0N/A if (rv == SOCKET_ERROR) {
0N/A int theErr = (jint)WSAGetLastError();
0N/A if (theErr == WSAEWOULDBLOCK) {
0N/A return IOS_UNAVAILABLE;
0N/A }
0N/A return handleSocketError(env, (jint)WSAGetLastError());
0N/A }
0N/A return rv;
0N/A}