1 /* src/native/vm/cldc1.1/com_sun_cldc_io_j2me_socket_Protocol.c
3 Copyright (C) 2007 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
33 #include <sys/types.h>
34 #include <sys/socket.h>
38 #include "mm/memory.h"
40 #include "native/jni.h"
41 #include "native/llni.h"
42 #include "native/native.h"
44 #include "native/include/com_sun_cldc_io_j2me_socket_Protocol.h"
46 #include "vm/global.h"
47 #include "vm/vm.hpp" /* REMOVE ME: temporarily */
50 /* native methods implemented by this file ************************************/
52 static JNINativeMethod methods[] = {
53 { "open0", "([BII)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_open0 },
54 { "readBuf", "(I[BII)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf },
55 { "readByte", "(I)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_readByte },
56 { "writeBuf", "(I[BII)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf },
57 { "writeByte", "(II)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte },
58 { "available0", "(I)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_available0 },
59 { "close0", "(I)V", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_close0 },
63 /* _Jv_com_sun_cldc_io_j2me_socket_Protocol_init *******************************
65 Register native functions.
67 *******************************************************************************/
69 void _Jv_com_sun_cldc_io_j2me_socket_Protocol_init(void)
73 u = utf_new_char("com/sun/cldc/io/j2me/socket/Protocol");
75 native_method_register(u, methods, NATIVE_METHODS_COUNT);
80 * Class: com/sun/cldc/io/j2me/socket/Protocol
84 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_open0(JNIEnv *env, jclass clazz, java_handle_bytearray_t *hostname, s4 port, s4 mode)
86 struct hostent *phostent;
87 struct sockaddr_in serv_addr;
92 /* The hostname byte-array is a NULL terminated C-string. */
94 name = (char *) &(LLNI_array_data(hostname));
98 phostent = gethostbyname(name);
100 if (phostent == NULL)
103 /* fill the sockaddr structure */
105 serv_addr.sin_family = AF_INET;
106 serv_addr.sin_port = htons(port);
108 MCOPY(&serv_addr.sin_addr, phostent->h_addr, u1, phostent->h_length);
110 /* create the socket */
112 sockfd = socket(AF_INET, SOCK_STREAM, 0);
117 /* connect the socket */
119 result = connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
129 * Class: com/sun/cldc/io/j2me/socket/Protocol
131 * Signature: (I[BII)I
133 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf(JNIEnv *env, jclass clazz, s4 handle, java_handle_bytearray_t *b, s4 off, s4 len)
138 /* get pointer to the buffer */
140 buf = &(LLNI_array_direct(b, off));
142 /* receive from the socket */
144 result = recv(handle, buf, len, 0);
147 /* the peer has performed an orderly shutdown */
151 else if (result < 0) {
152 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf: recv failed: %s", strerror(errno));
160 * Class: com/sun/cldc/io/j2me/socket/Protocol
164 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readByte(JNIEnv *env, jclass clazz, s4 handle) {
169 /* receive from the socket */
171 result = recv(handle, &byte, 1, 0);
174 /* the peer has performed an orderly shutdown */
178 else if (result < 0) {
179 /* should throw an IOException */
181 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_readByte: recv failed: %s", strerror(errno));
189 * Class: com/sun/cldc/io/j2me/socket/Protocol
191 * Signature: (I[BII)I
193 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf(JNIEnv *env, jclass clazz, s4 handle, java_handle_bytearray_t * b, s4 off, s4 len) {
198 /* get pointer to the buffer */
200 buf = &(LLNI_array_direct(b, off));
202 /* send the given byte to the socket */
204 result = send(handle, buf, len, 0);
207 /* should throw an IOException */
209 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf: send failed: %s", strerror(errno));
217 * Class: com/sun/cldc/io/j2me/socket/Protocol
221 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte(JNIEnv *env, jclass clazz, s4 handle, s4 b)
228 /* send the given byte to the socket */
230 result = send(handle, &byte, 1, 0);
233 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte: send failed: %s", strerror(errno));
240 * Class: com/sun/cldc/io/j2me/socket/Protocol
244 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_available0(JNIEnv *env, jclass clazz, s4 handle)
246 /* NOTE: Sun doesn't have an implementation too */
253 * Class: com/sun/cldc/io/j2me/socket/Protocol
257 JNIEXPORT void JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_close0(JNIEnv *env, jclass clazz, s4 handle)
261 /* close the file descriptor */
263 result = close(handle);
266 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_close0: close failed: %s", strerror(errno));
271 * These are local overrides for various environment variables in Emacs.
272 * Please do not remove this and leave it at the end of the file, where
273 * Emacs will automagically detect them.
274 * ---------------------------------------------------------------------
277 * indent-tabs-mode: t
281 * vim:noexpandtab:sw=4:ts=4: