0N/A/*
2362N/A * Copyright (c) 2003, 2008, 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 <stdlib.h>
0N/A#include <sys/types.h>
0N/A#include <sys/socket.h>
0N/A#include <unistd.h>
0N/A#include <fcntl.h>
0N/A
0N/A#include "jni.h"
0N/A
0N/A#include "jni.h"
0N/A#include "jni_util.h"
0N/A#include "net_util.h"
0N/A
0N/A#include "sun_nio_ch_InheritedChannel.h"
0N/A
0N/Astatic int matchFamily(struct sockaddr *sa) {
0N/A int family = sa->sa_family;
0N/A#ifdef AF_INET6
0N/A if (ipv6_available()) {
0N/A return (family == AF_INET6);
0N/A }
0N/A#endif
0N/A return (family == AF_INET);
0N/A}
0N/A
0N/AJNIEXPORT jobject JNICALL
0N/AJava_sun_nio_ch_InheritedChannel_peerAddress0(JNIEnv *env, jclass cla, jint fd)
0N/A{
0N/A struct sockaddr *sa;
0N/A socklen_t sa_len;
0N/A jobject remote_ia = NULL;
0N/A jint remote_port;
0N/A
0N/A NET_AllocSockaddr(&sa, (int *)&sa_len);
0N/A if (getpeername(fd, sa, &sa_len) == 0) {
0N/A if (matchFamily(sa)) {
0N/A remote_ia = NET_SockaddrToInetAddress(env, sa, (int *)&remote_port);
0N/A }
0N/A }
0N/A free((void *)sa);
0N/A
0N/A return remote_ia;
0N/A}
0N/A
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_nio_ch_InheritedChannel_peerPort0(JNIEnv *env, jclass cla, jint fd)
0N/A{
0N/A struct sockaddr *sa;
0N/A socklen_t sa_len;
0N/A jint remote_port = -1;
0N/A
0N/A NET_AllocSockaddr(&sa, (int *)&sa_len);
0N/A if (getpeername(fd, sa, &sa_len) == 0) {
0N/A if (matchFamily(sa)) {
0N/A NET_SockaddrToInetAddress(env, sa, (int *)&remote_port);
0N/A }
0N/A }
0N/A free((void *)sa);
0N/A
0N/A return remote_port;
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_nio_ch_InheritedChannel_soType0(JNIEnv *env, jclass cla, jint fd)
0N/A{
438N/A int sotype;
438N/A socklen_t arglen=sizeof(sotype);
0N/A if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, &arglen) == 0) {
0N/A if (sotype == SOCK_STREAM)
0N/A return sun_nio_ch_InheritedChannel_SOCK_STREAM;
0N/A if (sotype == SOCK_DGRAM)
0N/A return sun_nio_ch_InheritedChannel_SOCK_DGRAM;
0N/A }
0N/A return sun_nio_ch_InheritedChannel_UNKNOWN;
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_nio_ch_InheritedChannel_dup(JNIEnv *env, jclass cla, jint fd)
0N/A{
0N/A int newfd = dup(fd);
0N/A if (newfd < 0) {
0N/A JNU_ThrowIOExceptionWithLastError(env, "dup failed");
0N/A }
0N/A return (jint)newfd;
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_nio_ch_InheritedChannel_dup2(JNIEnv *env, jclass cla, jint fd, jint fd2)
0N/A{
0N/A if (dup2(fd, fd2) < 0) {
0N/A JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
0N/A }
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_nio_ch_InheritedChannel_open0(JNIEnv *env, jclass cla, jstring path, jint oflag)
0N/A{
0N/A const char* str;
0N/A int oflag_actual;
0N/A
0N/A /* convert to OS specific value */
0N/A switch (oflag) {
0N/A case sun_nio_ch_InheritedChannel_O_RDWR :
0N/A oflag_actual = O_RDWR;
0N/A break;
0N/A case sun_nio_ch_InheritedChannel_O_RDONLY :
0N/A oflag_actual = O_RDONLY;
0N/A break;
0N/A case sun_nio_ch_InheritedChannel_O_WRONLY :
0N/A oflag_actual = O_WRONLY;
0N/A break;
0N/A default :
0N/A JNU_ThrowInternalError(env, "Unrecognized file mode");
0N/A return -1;
0N/A }
0N/A
0N/A str = JNU_GetStringPlatformChars(env, path, NULL);
0N/A if (str == NULL) {
0N/A return (jint)-1;
0N/A } else {
0N/A int fd = open(str, oflag_actual);
0N/A if (fd < 0) {
0N/A JNU_ThrowIOExceptionWithLastError(env, str);
0N/A }
0N/A JNU_ReleaseStringPlatformChars(env, path, str);
0N/A return (jint)fd;
0N/A }
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_nio_ch_InheritedChannel_close0(JNIEnv *env, jclass cla, jint fd)
0N/A{
0N/A if (close(fd) < 0) {
0N/A JNU_ThrowIOExceptionWithLastError(env, "close failed");
0N/A }
0N/A}