1 /* 2 * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include <stdlib.h> 27 #include <sys/types.h> 28 #include <sys/socket.h> 29 #include <unistd.h> 30 #include <fcntl.h> 31 32 #include "jni.h" 33 34 #include "jni.h" 35 #include "jni_util.h" 36 #include "net_util.h" 37 38 #include "sun_nio_ch_InheritedChannel.h" 39 40 static int matchFamily(struct sockaddr *sa) { 41 int family = sa->sa_family; 42 #ifdef AF_INET6 43 if (ipv6_available()) { 44 return (family == AF_INET6); 45 } 46 #endif 47 return (family == AF_INET); 48 } 49 50 JNIEXPORT void JNICALL 51 Java_sun_nio_ch_InheritedChannel_initIDs(JNIEnv *env, jclass cla) 52 { 53 /* Initialize InetAddress IDs before later use of NET_XXX functions */ 54 initInetAddressIDs(env); 55 } 56 57 JNIEXPORT jobject JNICALL 58 Java_sun_nio_ch_InheritedChannel_peerAddress0(JNIEnv *env, jclass cla, jint fd) 59 { 60 struct sockaddr *sa; 61 socklen_t sa_len; 62 jobject remote_ia = NULL; 63 jint remote_port; 64 65 NET_AllocSockaddr(&sa, (int *)&sa_len); 66 if (getpeername(fd, sa, &sa_len) == 0) { 67 if (matchFamily(sa)) { 68 remote_ia = NET_SockaddrToInetAddress(env, sa, (int *)&remote_port); 69 } 70 } 71 free((void *)sa); 72 73 return remote_ia; 74 } 75 76 77 JNIEXPORT jint JNICALL 78 Java_sun_nio_ch_InheritedChannel_peerPort0(JNIEnv *env, jclass cla, jint fd) 79 { 80 struct sockaddr *sa; 81 socklen_t sa_len; 82 jint remote_port = -1; 83 84 NET_AllocSockaddr(&sa, (int *)&sa_len); 85 if (getpeername(fd, sa, &sa_len) == 0) { 86 if (matchFamily(sa)) { 87 NET_SockaddrToInetAddress(env, sa, (int *)&remote_port); 88 } 89 } 90 free((void *)sa); 91 92 return remote_port; 93 } 94 95 JNIEXPORT jint JNICALL 96 Java_sun_nio_ch_InheritedChannel_soType0(JNIEnv *env, jclass cla, jint fd) 97 { 98 int sotype; 99 socklen_t arglen=sizeof(sotype); 100 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, &arglen) == 0) { 101 if (sotype == SOCK_STREAM) 102 return sun_nio_ch_InheritedChannel_SOCK_STREAM; 103 if (sotype == SOCK_DGRAM) 104 return sun_nio_ch_InheritedChannel_SOCK_DGRAM; 105 } 106 return sun_nio_ch_InheritedChannel_UNKNOWN; 107 } 108 109 JNIEXPORT jint JNICALL 110 Java_sun_nio_ch_InheritedChannel_dup(JNIEnv *env, jclass cla, jint fd) 111 { 112 int newfd = dup(fd); 113 if (newfd < 0) { 114 JNU_ThrowIOExceptionWithLastError(env, "dup failed"); 115 } 116 return (jint)newfd; 117 } 118 119 JNIEXPORT void JNICALL 120 Java_sun_nio_ch_InheritedChannel_dup2(JNIEnv *env, jclass cla, jint fd, jint fd2) 121 { 122 if (dup2(fd, fd2) < 0) { 123 JNU_ThrowIOExceptionWithLastError(env, "dup2 failed"); 124 } 125 } 126 127 JNIEXPORT jint JNICALL 128 Java_sun_nio_ch_InheritedChannel_open0(JNIEnv *env, jclass cla, jstring path, jint oflag) 129 { 130 const char* str; 131 int oflag_actual; 132 133 /* convert to OS specific value */ 134 switch (oflag) { 135 case sun_nio_ch_InheritedChannel_O_RDWR : 136 oflag_actual = O_RDWR; 137 break; 138 case sun_nio_ch_InheritedChannel_O_RDONLY : 139 oflag_actual = O_RDONLY; 140 break; 141 case sun_nio_ch_InheritedChannel_O_WRONLY : 142 oflag_actual = O_WRONLY; 143 break; 144 default : 145 JNU_ThrowInternalError(env, "Unrecognized file mode"); 146 return -1; 147 } 148 149 str = JNU_GetStringPlatformChars(env, path, NULL); 150 if (str == NULL) { 151 return (jint)-1; 152 } else { 153 int fd = open(str, oflag_actual); 154 if (fd < 0) { 155 JNU_ThrowIOExceptionWithLastError(env, str); 156 } 157 JNU_ReleaseStringPlatformChars(env, path, str); 158 return (jint)fd; 159 } 160 } 161 162 JNIEXPORT void JNICALL 163 Java_sun_nio_ch_InheritedChannel_close0(JNIEnv *env, jclass cla, jint fd) 164 { 165 if (close(fd) < 0) { 166 JNU_ThrowIOExceptionWithLastError(env, "close failed"); 167 } 168 }