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