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