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