Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/nio/ch/InheritedChannel.c
32288 views
/*1* Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdlib.h>26#include <sys/types.h>27#include <sys/socket.h>28#include <unistd.h>29#include <fcntl.h>3031#include "jni.h"3233#include "jni.h"34#include "jni_util.h"35#include "net_util.h"3637#include "sun_nio_ch_InheritedChannel.h"3839static int matchFamily(struct sockaddr *sa) {40int family = sa->sa_family;41#ifdef AF_INET642if (ipv6_available()) {43return (family == AF_INET6);44}45#endif46return (family == AF_INET);47}4849JNIEXPORT jobject JNICALL50Java_sun_nio_ch_InheritedChannel_peerAddress0(JNIEnv *env, jclass cla, jint fd)51{52struct sockaddr *sa;53socklen_t sa_len;54jobject remote_ia = NULL;55jint remote_port;5657NET_AllocSockaddr(&sa, (int *)&sa_len);58if (getpeername(fd, sa, &sa_len) == 0) {59if (matchFamily(sa)) {60remote_ia = NET_SockaddrToInetAddress(env, sa, (int *)&remote_port);61}62}63free((void *)sa);6465return remote_ia;66}676869JNIEXPORT jint JNICALL70Java_sun_nio_ch_InheritedChannel_peerPort0(JNIEnv *env, jclass cla, jint fd)71{72struct sockaddr *sa;73socklen_t sa_len;74jint remote_port = -1;7576NET_AllocSockaddr(&sa, (int *)&sa_len);77if (getpeername(fd, sa, &sa_len) == 0) {78if (matchFamily(sa)) {79NET_SockaddrToInetAddress(env, sa, (int *)&remote_port);80}81}82free((void *)sa);8384return remote_port;85}8687JNIEXPORT jint JNICALL88Java_sun_nio_ch_InheritedChannel_soType0(JNIEnv *env, jclass cla, jint fd)89{90int sotype;91socklen_t arglen=sizeof(sotype);92if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, &arglen) == 0) {93if (sotype == SOCK_STREAM)94return sun_nio_ch_InheritedChannel_SOCK_STREAM;95if (sotype == SOCK_DGRAM)96return sun_nio_ch_InheritedChannel_SOCK_DGRAM;97}98return sun_nio_ch_InheritedChannel_UNKNOWN;99}100101JNIEXPORT jint JNICALL102Java_sun_nio_ch_InheritedChannel_dup(JNIEnv *env, jclass cla, jint fd)103{104int newfd = dup(fd);105if (newfd < 0) {106JNU_ThrowIOExceptionWithLastError(env, "dup failed");107}108return (jint)newfd;109}110111JNIEXPORT void JNICALL112Java_sun_nio_ch_InheritedChannel_dup2(JNIEnv *env, jclass cla, jint fd, jint fd2)113{114if (dup2(fd, fd2) < 0) {115JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");116}117}118119JNIEXPORT jint JNICALL120Java_sun_nio_ch_InheritedChannel_open0(JNIEnv *env, jclass cla, jstring path, jint oflag)121{122const char* str;123int oflag_actual;124125/* convert to OS specific value */126switch (oflag) {127case sun_nio_ch_InheritedChannel_O_RDWR :128oflag_actual = O_RDWR;129break;130case sun_nio_ch_InheritedChannel_O_RDONLY :131oflag_actual = O_RDONLY;132break;133case sun_nio_ch_InheritedChannel_O_WRONLY :134oflag_actual = O_WRONLY;135break;136default :137JNU_ThrowInternalError(env, "Unrecognized file mode");138return -1;139}140141str = JNU_GetStringPlatformChars(env, path, NULL);142if (str == NULL) {143return (jint)-1;144} else {145int fd = open(str, oflag_actual);146if (fd < 0) {147JNU_ThrowIOExceptionWithLastError(env, str);148}149JNU_ReleaseStringPlatformChars(env, path, str);150return (jint)fd;151}152}153154JNIEXPORT void JNICALL155Java_sun_nio_ch_InheritedChannel_close0(JNIEnv *env, jclass cla, jint fd)156{157if (close(fd) < 0) {158JNU_ThrowIOExceptionWithLastError(env, "close failed");159}160}161162163