Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / native / jni / java-net / gnu_java_net_VMPlainSocketImpl.c
blob3d48b9195c444cd456020e8256b3a09aa579fd0e
1 /* VMPlainSocketImpl.c - Native methods for PlainSocketImpl class
2 Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 /* do not move; needed here because of some macro definitions */
39 #include <config.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
45 #include <jni.h>
46 #include <jcl.h>
48 #include "javanet.h"
50 #include "target_native.h"
51 #ifndef WITHOUT_NETWORK
52 #include "target_native_file.h" /* Get FIONREAD on Solaris. */
53 #include "target_native_network.h"
54 #endif /* WITHOUT_NETWORK */
56 #include "gnu_java_net_VMPlainSocketImpl.h"
59 * Note that the functions in this module simply redirect to another
60 * internal function. Why? Because many of these functions are shared
61 * with PlainDatagramSocketImpl. The unshared ones were done the same
62 * way for consistency.
65 /*************************************************************************/
68 * Creates a new stream or datagram socket
70 JNIEXPORT void JNICALL
71 Java_gnu_java_net_VMPlainSocketImpl_create(JNIEnv *env,
72 jclass klass __attribute__ ((__unused__)),
73 jobject obj)
75 #ifndef WITHOUT_NETWORK
76 _javanet_create(env, obj, JNI_TRUE);
77 #else /* not WITHOUT_NETWORK */
78 #endif /* not WITHOUT_NETWORK */
81 /*************************************************************************/
84 * Close the socket. Any underlying streams will be closed by this
85 * action as well.
87 JNIEXPORT void JNICALL
88 Java_gnu_java_net_VMPlainSocketImpl_close(JNIEnv *env,
89 jclass klass __attribute__ ((__unused__)),
90 jobject obj)
92 #ifndef WITHOUT_NETWORK
93 _javanet_close(env, obj, 1);
94 #else /* not WITHOUT_NETWORK */
95 #endif /* not WITHOUT_NETWORK */
98 /*************************************************************************/
101 * Connects to the specified destination.
103 JNIEXPORT void JNICALL
104 Java_gnu_java_net_VMPlainSocketImpl_connect(JNIEnv *env,
105 jclass klass __attribute__ ((__unused__)),
106 jobject obj,
107 jobject addr, jint port)
109 #ifndef WITHOUT_NETWORK
110 _javanet_connect(env, obj, addr, port, 1);
111 #else /* not WITHOUT_NETWORK */
112 #endif /* not WITHOUT_NETWORK */
115 /*************************************************************************/
118 * This method binds the specified address to the specified local port.
119 * Note that we have to set the local address and local port public instance
120 * variables.
122 JNIEXPORT void JNICALL
123 Java_gnu_java_net_VMPlainSocketImpl_bind(JNIEnv *env,
124 jclass klass __attribute__ ((__unused__)),
125 jobject obj, jobject addr,
126 jint port)
128 #ifndef WITHOUT_NETWORK
129 _javanet_bind(env, obj, addr, port, 1);
130 #else /* not WITHOUT_NETWORK */
131 #endif /* not WITHOUT_NETWORK */
134 /*************************************************************************/
137 * Starts listening on a socket with the specified number of pending
138 * connections allowed.
140 JNIEXPORT void JNICALL
141 Java_gnu_java_net_VMPlainSocketImpl_listen(JNIEnv *env,
142 jclass klass __attribute__ ((__unused__)),
143 jobject obj, jint queuelen)
145 #ifndef WITHOUT_NETWORK
146 _javanet_listen(env, obj, queuelen);
147 #else /* not WITHOUT_NETWORK */
148 #endif /* not WITHOUT_NETWORK */
151 /*************************************************************************/
154 * Accepts a new connection and assigns it to the passed in SocketImpl
155 * object. Note that we assume this is a PlainSocketImpl just like us.
157 JNIEXPORT void JNICALL
158 Java_gnu_java_net_VMPlainSocketImpl_accept(JNIEnv *env,
159 jclass klass __attribute__ ((__unused__)),
160 jobject obj, jobject impl)
162 #ifndef WITHOUT_NETWORK
163 _javanet_accept(env, obj, impl);
164 #else /* not WITHOUT_NETWORK */
165 #endif /* not WITHOUT_NETWORK */
168 /*************************************************************************/
170 JNIEXPORT jint JNICALL
171 Java_gnu_java_net_VMPlainSocketImpl_available(JNIEnv *env,
172 jclass klass __attribute__ ((__unused__)),
173 jobject obj)
175 #ifndef WITHOUT_NETWORK
176 jclass cls;
177 jfieldID fid;
178 int fd;
179 int bytesAvailable;
180 int result;
182 cls = (*env)->GetObjectClass(env, obj);
183 if (cls == 0)
185 JCL_ThrowException(env, IO_EXCEPTION, "internal error");
186 return 0;
189 fid = (*env)->GetFieldID(env, cls, "native_fd", "I");
190 if (fid == 0)
192 JCL_ThrowException(env, IO_EXCEPTION, "internal error");
193 return 0;
196 fd = (*env)->GetIntField(env, obj, fid);
198 TARGET_NATIVE_NETWORK_SOCKET_RECEIVE_AVAILABLE(fd,bytesAvailable,result);
199 if (result != TARGET_NATIVE_OK)
201 JCL_ThrowException(env, IO_EXCEPTION, TARGET_NATIVE_LAST_ERROR_STRING());
202 return 0;
205 return bytesAvailable;
206 #else /* not WITHOUT_NETWORK */
207 return 0;
208 #endif /* not WITHOUT_NETWORK */
211 /*************************************************************************/
214 * This method sets the specified option for a socket
216 JNIEXPORT void JNICALL
217 Java_gnu_java_net_VMPlainSocketImpl_setOption(JNIEnv *env,
218 jclass klass __attribute__ ((__unused__)),
219 jobject obj,
220 jint option_id, jobject val)
222 #ifndef WITHOUT_NETWORK
223 _javanet_set_option(env, obj, option_id, val);
224 #else /* not WITHOUT_NETWORK */
225 #endif /* not WITHOUT_NETWORK */
228 /*************************************************************************/
231 * This method gets the specified option for a socket
233 JNIEXPORT jobject JNICALL
234 Java_gnu_java_net_VMPlainSocketImpl_getOption(JNIEnv *env,
235 jclass klass __attribute__ ((__unused__)),
236 jobject obj,
237 jint option_id)
239 #ifndef WITHOUT_NETWORK
240 return(_javanet_get_option(env, obj, option_id));
241 #else /* not WITHOUT_NETWORK */
242 return NULL;
243 #endif /* not WITHOUT_NETWORK */
246 /*************************************************************************/
249 * Reads a buffer from a remote host
251 JNIEXPORT jint JNICALL
252 Java_gnu_java_net_VMPlainSocketImpl_read(JNIEnv *env,
253 jclass klass __attribute__ ((__unused__)),
254 jobject obj, jarray buf,
255 jint offset, jint len)
257 #ifndef WITHOUT_NETWORK
258 return(_javanet_recvfrom(env, obj, buf, offset, len, 0, 0));
259 #else /* not WITHOUT_NETWORK */
260 return 0;
261 #endif /* not WITHOUT_NETWORK */
264 /*************************************************************************/
267 * Writes a buffer to the remote host
269 JNIEXPORT void JNICALL
270 Java_gnu_java_net_VMPlainSocketImpl_write(JNIEnv *env,
271 jclass klass __attribute__ ((__unused__)),
272 jobject obj, jarray buf,
273 jint offset, jint len)
275 #ifndef WITHOUT_NETWORK
276 _javanet_sendto(env, obj, buf, offset, len, 0, 0);
277 #else /* not WITHOUT_NETWORK */
278 #endif /* not WITHOUT_NETWORK */
281 JNIEXPORT void JNICALL
282 Java_gnu_java_net_VMPlainSocketImpl_shutdownInput (JNIEnv * env,
283 jclass klass __attribute__ ((__unused__)),
284 jobject this)
286 #ifndef WITHOUT_NETWORK
287 _javanet_shutdownInput (env, this);
288 #else /* not WITHOUT_NETWORK */
289 #endif /* not WITHOUT_NETWORK */
292 JNIEXPORT void JNICALL
293 Java_gnu_java_net_VMPlainSocketImpl_shutdownOutput (JNIEnv * env,
294 jclass klass __attribute__ ((__unused__)),
295 jobject this)
297 #ifndef WITHOUT_NETWORK
298 _javanet_shutdownOutput (env, this);
299 #else /* not WITHOUT_NETWORK */
300 #endif /* not WITHOUT_NETWORK */
303 /* end of file */