jdk/src/java.base/unix/native/libnet/AbstractPlainDatagramSocketImpl.c
author michaelm
Wed, 28 May 2014 14:51:24 +0100
changeset 27078 39275d6a8cac
permissions -rw-r--r--
8039509: Wrap sockets more thoroughly Reviewed-by: chegar, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27078
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
     1
/*
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
     4
 *
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    10
 *
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    15
 * accompanied this code).
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    16
 *
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    20
 *
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    23
 * questions.
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    24
 */
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    25
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    26
#include <sys/types.h>
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    27
#include <sys/socket.h>
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    28
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    29
#ifdef __solaris__
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    30
#include <unistd.h>
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    31
#include <stropts.h>
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    32
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    33
#ifndef BSD_COMP
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    34
#define BSD_COMP
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    35
#endif
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    36
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    37
#endif
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    38
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    39
#include <sys/ioctl.h>
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    40
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    41
#include "jvm.h"
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    42
#include "jni_util.h"
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    43
#include "net_util.h"
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    44
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    45
#include "java_net_AbstractPlainDatagramSocketImpl.h"
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    46
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    47
static jfieldID IO_fd_fdID;
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    48
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    49
static jfieldID apdsi_fdID;
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    50
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    51
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    52
/*
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    53
 * Class:     java_net_AbstractPlainDatagramSocketImpl
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    54
 * Method:    init
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    55
 * Signature: ()V
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    56
 */
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    57
JNIEXPORT void JNICALL
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    58
Java_java_net_AbstractPlainDatagramSocketImpl_init(JNIEnv *env, jclass cls) {
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    59
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    60
    apdsi_fdID = (*env)->GetFieldID(env, cls, "fd",
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    61
                                   "Ljava/io/FileDescriptor;");
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    62
    CHECK_NULL(apdsi_fdID);
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    63
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    64
    IO_fd_fdID = NET_GetFileDescriptorID(env);
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    65
}
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    66
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    67
/*
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    68
 * Class:     java_net_AbstractPlainDatagramSocketImpl
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    69
 * Method:    dataAvailable
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    70
 * Signature: ()I
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    71
 */
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    72
JNIEXPORT jint JNICALL Java_java_net_AbstractPlainDatagramSocketImpl_dataAvailable
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    73
(JNIEnv *env, jobject this) {
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    74
    int fd, retval;
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    75
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    76
    jobject fdObj = (*env)->GetObjectField(env, this, apdsi_fdID);
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    77
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    78
    if (IS_NULL(fdObj)) {
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    79
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    80
                        "Socket closed");
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    81
        return -1;
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    82
    }
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    83
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    84
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    85
    if (ioctl(fd, FIONREAD, &retval) < 0) {
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    86
        return -1;
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    87
    }
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    88
    return retval;
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents:
diff changeset
    89
}