* src/vm/vm.cpp (vm_abort_errnum, vm_abort_errno): Removed obsolete functions.
[cacao.git] / src / native / vm / cldc1.1 / com_sun_cldc_io_j2me_socket_Protocol.cpp
1 /* src/native/vm/cldc1.1/com_sun_cldc_io_j2me_socket_Protocol.cpp
2
3    Copyright (C) 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <stdint.h>
29 #include <errno.h>
30 #include <netdb.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34
35 #include "vm/types.h"
36
37 #include "mm/memory.hpp"
38
39 #include "native/jni.hpp"
40 #include "native/llni.h"
41 #include "native/native.hpp"
42
43 #if defined(ENABLE_JNI_HEADERS)
44 # include "native/include/com_sun_cldc_io_j2me_socket_Protocol.h"
45 #endif
46
47 #include "vm/global.h"
48 #include "vm/os.hpp"
49 #include "vm/vm.hpp" /* REMOVE ME: temporarily */
50
51
52 // Native functions are exported as C functions.
53 extern "C" {
54
55 /*
56  * Class:     com/sun/cldc/io/j2me/socket/Protocol
57  * Method:    open0
58  * Signature: ([BII)I
59  */
60 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_open0(JNIEnv *env, jclass clazz, jbyteArray hostname, jint port, jint mode)
61 {
62         struct hostent *phostent;
63     struct sockaddr_in serv_addr;
64
65         // The hostname byte-array is a NULL terminated C-string.
66         // XXX Not GC safe.
67         char* name = (char*) &(LLNI_array_data((java_handle_bytearray_t*) hostname));
68
69         /* get the host */
70
71         phostent = gethostbyname(name);
72
73         if (phostent == NULL)
74                 return -1;
75
76         /* fill the sockaddr structure */
77
78         serv_addr.sin_family = AF_INET;
79         serv_addr.sin_port   = htons(port);
80
81         MCOPY(&serv_addr.sin_addr, phostent->h_addr, u1, phostent->h_length);
82
83         /* create the socket */
84
85         int sockfd = socket(AF_INET, SOCK_STREAM, 0);
86
87         if (sockfd < 0)
88                 return -1;
89
90         /* connect the socket */
91
92         int result = connect(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr));
93
94         if (result < 0)
95                 return -1;
96
97         return sockfd;
98 }
99
100
101 /*
102  * Class:     com/sun/cldc/io/j2me/socket/Protocol
103  * Method:    readBuf
104  * Signature: (I[BII)I
105  */
106 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf(JNIEnv *env, jclass clazz, jint handle, jbyteArray b, jint off, jint len)
107 {
108         // Get pointer to the buffer.
109         // XXX Not GC safe.
110         void* buf = &(LLNI_array_direct((java_handle_bytearray_t*) b, off));
111
112         // Receive from the socket.
113         ssize_t result = recv(handle, buf, len, 0);
114
115         if (result == 0) {
116                 // The peer has performed an orderly shutdown.
117                 return -1;
118         }
119         else if (result < 0) {
120                 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf: recv failed");
121         }
122
123         return result;
124 }
125
126
127 /*
128  * Class:     com/sun/cldc/io/j2me/socket/Protocol
129  * Method:    readByte
130  * Signature: (I)I
131  */
132 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readByte(JNIEnv *env, jclass clazz, jint handle)
133 {
134         char byte;
135         
136         // Receive from the socket.
137         ssize_t result = recv(handle, &byte, 1, 0);
138
139         if (result == 0) {
140                 // The peer has performed an orderly shutdown.
141                 return -1;
142         }
143         else if (result < 0) {
144                 // TODO Should throw an IOException.
145                 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_readByte: recv failed");
146         }
147
148         return byte;
149 }
150
151
152 /*
153  * Class:     com/sun/cldc/io/j2me/socket/Protocol
154  * Method:    writeBuf
155  * Signature: (I[BII)I
156  */
157 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf(JNIEnv *env, jclass clazz, jint handle, jbyteArray b, jint off, jint len)
158 {
159         // Get pointer to the buffer.
160         // XXX Not GC safe.
161         void* buf = &(LLNI_array_direct((java_handle_bytearray_t*) b, off));
162         
163         // Send the given byte to the socket.
164         ssize_t result = send(handle, buf, len, 0);
165
166         if (result < 0) {
167                 // TODO Should throw an IOException.
168                 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf: send failed");
169         }
170
171         return result;
172 }
173
174
175 /*
176  * Class:     com/sun/cldc/io/j2me/socket/Protocol
177  * Method:    writeByte
178  * Signature: (II)I
179  */
180 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte(JNIEnv *env, jclass clazz, jint handle, jint b)
181 {
182         char byte = (char) b;
183
184         // Send the given byte to the socket.
185         ssize_t result = send(handle, &byte, 1, 0);
186
187         if (result < 0)
188                 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte: send failed");
189
190         return result;
191 }
192
193
194 /*
195  * Class:     com/sun/cldc/io/j2me/socket/Protocol
196  * Method:    available0
197  * Signature: (I)I
198  */
199 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_available0(JNIEnv *env, jclass clazz, jint handle)
200 {
201         // NOTE: Sun doesn't have an implementation too.
202         return 0;
203 }
204
205
206 /*
207  * Class:     com/sun/cldc/io/j2me/socket/Protocol
208  * Method:    close0
209  * Signature: (I)V
210  */
211 JNIEXPORT void JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_close0(JNIEnv *env, jclass clazz, jint handle)
212 {
213         // Close the file descriptor.
214         int result = close(handle);
215
216         if (result < 0)
217                 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_close0: close failed");
218 }
219
220 } // extern "C"
221
222
223 /* native methods implemented by this file ************************************/
224  
225 static JNINativeMethod methods[] = {
226         { (char*) "open0",      (char*) "([BII)I",  (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_open0      },
227         { (char*) "readBuf",    (char*) "(I[BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf    },
228         { (char*) "readByte",   (char*) "(I)I",     (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_readByte   },
229         { (char*) "writeBuf",   (char*) "(I[BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf   },
230         { (char*) "writeByte",  (char*) "(II)I",    (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte  },
231         { (char*) "available0", (char*) "(I)I",     (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_available0 },
232         { (char*) "close0",     (char*) "(I)V",     (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_close0     },
233 };
234
235  
236 /* _Jv_com_sun_cldc_io_j2me_socket_Protocol_init *******************************
237  
238    Register native functions.
239  
240 *******************************************************************************/
241  
242 void _Jv_com_sun_cldc_io_j2me_socket_Protocol_init(void)
243 {
244         utf* u = utf_new_char("com/sun/cldc/io/j2me/socket/Protocol");
245  
246         NativeMethods& nm = VM::get_current()->get_nativemethods();
247         nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
248 }
249
250
251 /*
252  * These are local overrides for various environment variables in Emacs.
253  * Please do not remove this and leave it at the end of the file, where
254  * Emacs will automagically detect them.
255  * ---------------------------------------------------------------------
256  * Local variables:
257  * mode: c++
258  * indent-tabs-mode: t
259  * c-basic-offset: 4
260  * tab-width: 4
261  * End:
262  * vim:noexpandtab:sw=4:ts=4:
263  */