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 #include "JNIHelp.h"
41
42 #define NATIVE_METHOD(className, functionName, signature) \
43 { #functionName, signature, (void*)(Java_sun_nio_ch_ ## className ## _ ## functionName) }
44
matchFamily(struct sockaddr * sa)45 static int matchFamily(struct sockaddr *sa) {
46 int family = sa->sa_family;
47 #ifdef AF_INET6
48 if (ipv6_available()) {
49 return (family == AF_INET6);
50 }
51 #endif
52 return (family == AF_INET);
53 }
54
55 JNIEXPORT jobject JNICALL
Java_sun_nio_ch_InheritedChannel_peerAddress0(JNIEnv * env,jclass cla,jint fd)56 Java_sun_nio_ch_InheritedChannel_peerAddress0(JNIEnv *env, jclass cla, jint fd)
57 {
58 struct sockaddr *sa;
59 socklen_t sa_len;
60 jobject remote_ia = NULL;
61 jint remote_port;
62
63 NET_AllocSockaddr(&sa, (int *)&sa_len);
64 if (getpeername(fd, sa, &sa_len) == 0) {
65 if (matchFamily(sa)) {
66 remote_ia = NET_SockaddrToInetAddress(env, sa, (int *)&remote_port);
67 }
68 }
69 free((void *)sa);
70
71 return remote_ia;
72 }
73
74
75 JNIEXPORT jint JNICALL
Java_sun_nio_ch_InheritedChannel_peerPort0(JNIEnv * env,jclass cla,jint fd)76 Java_sun_nio_ch_InheritedChannel_peerPort0(JNIEnv *env, jclass cla, jint fd)
77 {
78 struct sockaddr *sa;
79 socklen_t sa_len;
80 jint remote_port = -1;
81
82 NET_AllocSockaddr(&sa, (int *)&sa_len);
83 if (getpeername(fd, sa, &sa_len) == 0) {
84 if (matchFamily(sa)) {
85 NET_SockaddrToInetAddress(env, sa, (int *)&remote_port);
86 }
87 }
88 free((void *)sa);
89
90 return remote_port;
91 }
92
93 JNIEXPORT jint JNICALL
Java_sun_nio_ch_InheritedChannel_soType0(JNIEnv * env,jclass cla,jint fd)94 Java_sun_nio_ch_InheritedChannel_soType0(JNIEnv *env, jclass cla, jint fd)
95 {
96 int sotype;
97 socklen_t arglen=sizeof(sotype);
98 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, &arglen) == 0) {
99 if (sotype == SOCK_STREAM)
100 return sun_nio_ch_InheritedChannel_SOCK_STREAM;
101 if (sotype == SOCK_DGRAM)
102 return sun_nio_ch_InheritedChannel_SOCK_DGRAM;
103 }
104 return sun_nio_ch_InheritedChannel_UNKNOWN;
105 }
106
107 JNIEXPORT jint JNICALL
Java_sun_nio_ch_InheritedChannel_dup(JNIEnv * env,jclass cla,jint fd)108 Java_sun_nio_ch_InheritedChannel_dup(JNIEnv *env, jclass cla, jint fd)
109 {
110 int newfd = dup(fd);
111 if (newfd < 0) {
112 JNU_ThrowIOExceptionWithLastError(env, "dup failed");
113 }
114 return (jint)newfd;
115 }
116
117 JNIEXPORT void JNICALL
Java_sun_nio_ch_InheritedChannel_dup2(JNIEnv * env,jclass cla,jint fd,jint fd2)118 Java_sun_nio_ch_InheritedChannel_dup2(JNIEnv *env, jclass cla, jint fd, jint fd2)
119 {
120 if (dup2(fd, fd2) < 0) {
121 JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
122 }
123 }
124
125 JNIEXPORT jint JNICALL
Java_sun_nio_ch_InheritedChannel_open0(JNIEnv * env,jclass cla,jstring path,jint oflag)126 Java_sun_nio_ch_InheritedChannel_open0(JNIEnv *env, jclass cla, jstring path, jint oflag)
127 {
128 const char* str;
129 int oflag_actual;
130
131 /* convert to OS specific value */
132 switch (oflag) {
133 case sun_nio_ch_InheritedChannel_O_RDWR :
134 oflag_actual = O_RDWR;
135 break;
136 case sun_nio_ch_InheritedChannel_O_RDONLY :
137 oflag_actual = O_RDONLY;
138 break;
139 case sun_nio_ch_InheritedChannel_O_WRONLY :
140 oflag_actual = O_WRONLY;
141 break;
142 default :
143 JNU_ThrowInternalError(env, "Unrecognized file mode");
144 return -1;
145 }
146
147 str = JNU_GetStringPlatformChars(env, path, NULL);
148 if (str == NULL) {
149 return (jint)-1;
150 } else {
151 int fd = open(str, oflag_actual);
152 if (fd < 0) {
153 JNU_ThrowIOExceptionWithLastError(env, str);
154 }
155 JNU_ReleaseStringPlatformChars(env, path, str);
156 return (jint)fd;
157 }
158 }
159
160 JNIEXPORT void JNICALL
Java_sun_nio_ch_InheritedChannel_close0(JNIEnv * env,jclass cla,jint fd)161 Java_sun_nio_ch_InheritedChannel_close0(JNIEnv *env, jclass cla, jint fd)
162 {
163 if (close(fd) < 0) {
164 JNU_ThrowIOExceptionWithLastError(env, "close failed");
165 }
166 }
167
168 static JNINativeMethod gMethods[] = {
169 NATIVE_METHOD(InheritedChannel, dup, "(I)I"),
170 NATIVE_METHOD(InheritedChannel, dup2, "(II)V"),
171 NATIVE_METHOD(InheritedChannel, open0, "(Ljava/lang/String;I)I"),
172 NATIVE_METHOD(InheritedChannel, close0, "(I)V"),
173 NATIVE_METHOD(InheritedChannel, soType0, "(I)I"),
174 NATIVE_METHOD(InheritedChannel, peerAddress0, "(I)Ljava/net/InetAddress;"),
175 NATIVE_METHOD(InheritedChannel, peerPort0, "(I)I"),
176
177 };
178
register_sun_nio_ch_InheritedChannel(JNIEnv * env)179 void register_sun_nio_ch_InheritedChannel(JNIEnv* env) {
180 jniRegisterNativeMethods(env, "sun/nio/ch/InheritedChannel", gMethods, NELEM(gMethods));
181 }
182