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
25 $Id: java_lang_VMRuntime.c 5900 2006-11-04 17:30:44Z michi $
35 #include <sys/types.h>
36 #include <sys/socket.h>
40 #include "mm/memory.h"
42 #include "native/jni.h"
43 #include "native/llni.h"
44 #include "native/native.h"
46 #include "native/include/com_sun_cldc_io_j2me_socket_Protocol.h"
48 #include "vm/global.h"
49 #include "vm/vm.h" /* REMOVE ME: temporarily */
52 /* native methods implemented by this file ************************************/
54 static JNINativeMethod methods[] = {
55 { "open0", "([BII)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_open0 },
56 { "readBuf", "(I[BII)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf },
57 { "readByte", "(I)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_readByte },
58 { "writeBuf", "(I[BII)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf },
59 { "writeByte", "(II)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte },
60 { "available0", "(I)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_available0 },
61 { "close0", "(I)V", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_close0 },
65 /* _Jv_com_sun_cldc_io_j2me_socket_Protocol_init *******************************
67 Register native functions.
69 *******************************************************************************/
71 void _Jv_com_sun_cldc_io_j2me_socket_Protocol_init(void)
75 u = utf_new_char("com/sun/cldc/io/j2me/socket/Protocol");
77 native_method_register(u, methods, NATIVE_METHODS_COUNT);
82 * Class: com/sun/cldc/io/j2me/socket/Protocol
86 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)
88 struct hostent *phostent;
89 struct sockaddr_in serv_addr;
94 /* The hostname byte-array is a NULL terminated C-string. */
96 name = (char *) &(LLNI_array_data(hostname));
100 phostent = gethostbyname(name);
102 if (phostent == NULL)
105 /* fill the sockaddr structure */
107 serv_addr.sin_family = AF_INET;
108 serv_addr.sin_port = htons(port);
110 MCOPY(&serv_addr.sin_addr, phostent->h_addr, u1, phostent->h_length);
112 /* create the socket */
114 sockfd = socket(AF_INET, SOCK_STREAM, 0);
119 /* connect the socket */
121 result = connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
131 * Class: com/sun/cldc/io/j2me/socket/Protocol
133 * Signature: (I[BII)I
135 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)
140 /* get pointer to the buffer */
142 buf = &(LLNI_array_direct(b, off));
144 /* receive from the socket */
146 result = recv(handle, buf, len, 0);
149 /* the peer has performed an orderly shutdown */
153 else if (result < 0) {
154 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf: recv failed: %s", strerror(errno));
162 * Class: com/sun/cldc/io/j2me/socket/Protocol
166 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readByte(JNIEnv *env, jclass clazz, s4 handle) {
171 /* receive from the socket */
173 result = recv(handle, &byte, 1, 0);
176 /* the peer has performed an orderly shutdown */
180 else if (result < 0) {
181 /* should throw an IOException */
183 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_readByte: recv failed: %s", strerror(errno));
191 * Class: com/sun/cldc/io/j2me/socket/Protocol
193 * Signature: (I[BII)I
195 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) {
200 /* get pointer to the buffer */
202 buf = &(LLNI_array_direct(b, off));
204 /* send the given byte to the socket */
206 result = send(handle, buf, len, 0);
209 /* should throw an IOException */
211 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf: send failed: %s", strerror(errno));
219 * Class: com/sun/cldc/io/j2me/socket/Protocol
223 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte(JNIEnv *env, jclass clazz, s4 handle, s4 b)
230 /* send the given byte to the socket */
232 result = send(handle, &byte, 1, 0);
235 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte: send failed: %s", strerror(errno));
242 * Class: com/sun/cldc/io/j2me/socket/Protocol
246 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_available0(JNIEnv *env, jclass clazz, s4 handle)
248 /* NOTE: Sun doesn't have an implementation too */
255 * Class: com/sun/cldc/io/j2me/socket/Protocol
259 JNIEXPORT void JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_close0(JNIEnv *env, jclass clazz, s4 handle)
263 /* close the file descriptor */
265 result = close(handle);
268 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_close0: close failed: %s", strerror(errno));
273 * These are local overrides for various environment variables in Emacs.
274 * Please do not remove this and leave it at the end of the file, where
275 * Emacs will automagically detect them.
276 * ---------------------------------------------------------------------
279 * indent-tabs-mode: t
283 * vim:noexpandtab:sw=4:ts=4: