0e6ea1dd15016bf687ff5ad4c26a600b1c2eb510
[cacao.git] / src / native / vm / cldc1.1 / com_sun_cldc_io_j2me_socket_Protocol.c
1 /* src/native/vm/cldc1.1/com_sun_cldc_io_j2me_socket_Protocol.c
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25    $Id: java_lang_VMRuntime.c 5900 2006-11-04 17:30:44Z michi $
26
27 */
28
29
30 #include "config.h"
31
32 #include <errno.h>
33 #include <netdb.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37
38 #include "vm/types.h"
39
40 #include "mm/memory.h"
41
42 #include "native/jni.h"
43 #include "native/llni.h"
44 #include "native/native.h"
45
46 #include "native/include/com_sun_cldc_io_j2me_socket_Protocol.h"
47
48 #include "vm/global.h"
49 #include "vm/vm.h" /* REMOVE ME: temporarily */
50
51
52 /* native methods implemented by this file ************************************/
53  
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     },
62 };
63
64  
65 /* _Jv_com_sun_cldc_io_j2me_socket_Protocol_init *******************************
66  
67    Register native functions.
68  
69 *******************************************************************************/
70  
71 void _Jv_com_sun_cldc_io_j2me_socket_Protocol_init(void)
72 {
73         utf *u;
74  
75         u = utf_new_char("com/sun/cldc/io/j2me/socket/Protocol");
76  
77         native_method_register(u, methods, NATIVE_METHODS_COUNT);
78 }
79
80
81 /*
82  * Class:     com/sun/cldc/io/j2me/socket/Protocol
83  * Method:    open0
84  * Signature: ([BII)I
85  */
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)
87 {
88         struct hostent *phostent;
89     struct sockaddr_in serv_addr;
90         char           *name;
91         s4              sockfd;
92         s4              result;
93
94         /* The hostname byte-array is a NULL terminated C-string. */
95
96         name = (char *) &(LLNI_array_data(hostname));
97
98         /* get the host */
99
100         phostent = gethostbyname(name);
101
102         if (phostent == NULL)
103                 return -1;
104
105         /* fill the sockaddr structure */
106
107         serv_addr.sin_family = AF_INET;
108         serv_addr.sin_port   = htons(port);
109
110         MCOPY(&serv_addr.sin_addr, phostent->h_addr, u1, phostent->h_length);
111
112         /* create the socket */
113
114         sockfd = socket(AF_INET, SOCK_STREAM, 0);
115
116         if (sockfd < 0)
117                 return -1;
118
119         /* connect the socket */
120
121         result = connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
122
123         if (result < 0)
124                 return -1;
125
126         return sockfd;
127 }
128
129
130 /*
131  * Class:     com/sun/cldc/io/j2me/socket/Protocol
132  * Method:    readBuf
133  * Signature: (I[BII)I
134  */
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)
136 {
137         void    *buf;
138         ssize_t  result;
139
140         /* get pointer to the buffer */
141
142         buf = &(LLNI_array_direct(b, off));
143
144         /* receive from the socket */
145
146         result = recv(handle, buf, len, 0);
147
148         if (result == 0) {
149                 /* the peer has performed an orderly shutdown */
150
151                 return -1;
152         }
153         else if (result < 0) {
154                 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf: recv failed: %s", strerror(errno));
155         }
156
157         return result;
158 }
159
160
161 /*
162  * Class:     com/sun/cldc/io/j2me/socket/Protocol
163  * Method:    readByte
164  * Signature: (I)I
165  */
166 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readByte(JNIEnv *env, jclass clazz, s4 handle) {
167         
168         char    byte;
169         ssize_t result;
170         
171         /* receive from the socket */
172
173         result = recv(handle, &byte, 1, 0);
174
175         if (result == 0) {
176                 /* the peer has performed an orderly shutdown */
177
178                 return -1;
179         }
180         else if (result < 0) {
181                 /* should throw an IOException */
182
183                 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_readByte: recv failed: %s", strerror(errno));
184         }
185
186         return byte;
187 }
188
189
190 /*
191  * Class:     com/sun/cldc/io/j2me/socket/Protocol
192  * Method:    writeBuf
193  * Signature: (I[BII)I
194  */
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) {
196
197         void    *buf;
198         ssize_t  result;
199
200         /* get pointer to the buffer */
201
202         buf = &(LLNI_array_direct(b, off));
203         
204         /* send the given byte to the socket */
205
206         result = send(handle, buf, len, 0);
207
208         if (result < 0)
209                 /* should throw an IOException */
210
211                 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf: send failed: %s", strerror(errno));
212
213         return result;
214
215 }
216
217
218 /*
219  * Class:     com/sun/cldc/io/j2me/socket/Protocol
220  * Method:    writeByte
221  * Signature: (II)I
222  */
223 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte(JNIEnv *env, jclass clazz, s4 handle, s4 b)
224 {
225         char    byte;
226         ssize_t result;
227
228         byte = (char) b;
229
230         /* send the given byte to the socket */
231
232         result = send(handle, &byte, 1, 0);
233
234         if (result < 0)
235                 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte: send failed: %s", strerror(errno));
236
237         return result;
238 }
239
240
241 /*
242  * Class:     com/sun/cldc/io/j2me/socket/Protocol
243  * Method:    available0
244  * Signature: (I)I
245  */
246 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_available0(JNIEnv *env, jclass clazz, s4 handle)
247 {
248         /* NOTE: Sun doesn't have an implementation too */
249
250         return 0;
251 }
252
253
254 /*
255  * Class:     com/sun/cldc/io/j2me/socket/Protocol
256  * Method:    close0
257  * Signature: (I)V
258  */
259 JNIEXPORT void JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_close0(JNIEnv *env, jclass clazz, s4 handle)
260 {
261         int result;
262
263         /* close the file descriptor */
264
265         result = close(handle);
266
267         if (result < 0)
268                 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_close0: close failed: %s", strerror(errno));
269 }
270
271
272 /*
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  * ---------------------------------------------------------------------
277  * Local variables:
278  * mode: c
279  * indent-tabs-mode: t
280  * c-basic-offset: 4
281  * tab-width: 4
282  * End:
283  * vim:noexpandtab:sw=4:ts=4:
284  */