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/native.h"
45 #include "native/include/com_sun_cldc_io_j2me_socket_Protocol.h"
47 #include "vm/global.h"
48 #include "vm/vm.h" /* REMOVE ME: temporarily */
51 /* native methods implemented by this file ************************************/
53 static JNINativeMethod methods[] = {
54 { "open0", "([BII)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_open0 },
55 { "readBuf", "(I[BII)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf },
56 { "readByte", "(I)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_readByte },
57 { "writeBuf", "(I[BII)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf },
58 { "writeByte", "(II)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte },
59 { "available0", "(I)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_available0 },
60 { "close0", "(I)V", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_close0 },
64 /* _Jv_com_sun_cldc_io_j2me_socket_Protocol_init *******************************
66 Register native functions.
68 *******************************************************************************/
70 void _Jv_com_sun_cldc_io_j2me_socket_Protocol_init(void)
74 u = utf_new_char("com/sun/cldc/io/j2me/socket/Protocol");
76 native_method_register(u, methods, NATIVE_METHODS_COUNT);
81 * Class: com/sun/cldc/io/j2me/socket/Protocol
85 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_open0(JNIEnv *env, jclass clazz, java_bytearray *hostname, s4 port, s4 mode)
87 struct hostent *phostent;
88 struct sockaddr_in serv_addr;
93 /* The hostname byte-array is a NULL terminated C-string. */
95 name = (char *) &(hostname->data);
99 phostent = gethostbyname(name);
101 if (phostent == NULL)
104 /* fill the sockaddr structure */
106 serv_addr.sin_family = AF_INET;
107 serv_addr.sin_port = htons(port);
109 MCOPY(&serv_addr.sin_addr, phostent->h_addr, u1, phostent->h_length);
111 /* create the socket */
113 sockfd = socket(AF_INET, SOCK_STREAM, 0);
118 /* connect the socket */
120 result = connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
130 * Class: com/sun/cldc/io/j2me/socket/Protocol
132 * Signature: (I[BII)I
134 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf(JNIEnv *env, jclass clazz, s4 handle, java_bytearray *b, s4 off, s4 len)
139 /* get pointer to the buffer */
141 buf = &(b->data[off]);
143 /* receive from the socket */
145 result = recv(handle, buf, len, 0);
148 /* the peer has performed an orderly shutdown */
152 else if (result < 0) {
153 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf: recv failed: %s", strerror(errno));
161 * Class: com/sun/cldc/io/j2me/socket/Protocol
165 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readByte(JNIEnv *env, jclass clazz, s4 handle) {
170 /* receive from the socket */
172 result = recv(handle, &byte, 1, 0);
175 /* the peer has performed an orderly shutdown */
179 else if (result < 0) {
180 /* should throw an IOException */
182 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_readByte: recv failed: %s", strerror(errno));
190 * Class: com/sun/cldc/io/j2me/socket/Protocol
192 * Signature: (I[BII)I
194 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf(JNIEnv *env, jclass clazz, s4 handle, java_bytearray* b, s4 off, s4 len) {
199 /* get pointer to the buffer */
201 buf = &(b->data[off]);
203 /* send the given byte to the socket */
205 result = send(handle, buf, len, 0);
208 /* should throw an IOException */
210 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf: send failed: %s", strerror(errno));
218 * Class: com/sun/cldc/io/j2me/socket/Protocol
222 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte(JNIEnv *env, jclass clazz, s4 handle, s4 b)
229 /* send the given byte to the socket */
231 result = send(handle, &byte, 1, 0);
234 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte: send failed: %s", strerror(errno));
241 * Class: com/sun/cldc/io/j2me/socket/Protocol
245 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_available0(JNIEnv *env, jclass clazz, s4 handle)
247 /* NOTE: Sun doesn't have an implementation too */
254 * Class: com/sun/cldc/io/j2me/socket/Protocol
258 JNIEXPORT void JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_close0(JNIEnv *env, jclass clazz, s4 handle)
262 /* close the file descriptor */
264 result = close(handle);
267 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_close0: close failed: %s", strerror(errno));
272 * These are local overrides for various environment variables in Emacs.
273 * Please do not remove this and leave it at the end of the file, where
274 * Emacs will automagically detect them.
275 * ---------------------------------------------------------------------
278 * indent-tabs-mode: t
282 * vim:noexpandtab:sw=4:ts=4: