CACAO
com_sun_cldc_io_j2me_socket_Protocol.cpp
Go to the documentation of this file.
1 /* src/native/vm/cldc1.1/com_sun_cldc_io_j2me_socket_Protocol.cpp
2 
3  Copyright (C) 2007-2013
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 
34 #include "vm/types.hpp"
35 
36 #include "mm/memory.hpp"
37 
38 #include "native/jni.hpp"
39 #include "native/native.hpp"
40 
41 #if defined(ENABLE_JNI_HEADERS)
42 # include "native/include/com_sun_cldc_io_j2me_socket_Protocol.h"
43 #endif
44 
45 #include "vm/array.hpp"
46 #include "vm/global.hpp"
47 #include "vm/os.hpp"
48 #include "vm/vm.hpp" /* REMOVE ME: temporarily */
49 
50 
51 // Native functions are exported as C functions.
52 extern "C" {
53 
54 /*
55  * Class: com/sun/cldc/io/j2me/socket/Protocol
56  * Method: open0
57  * Signature: ([BII)I
58  */
59 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_open0(JNIEnv *env, jclass clazz, jbyteArray hostname, jint port, jint mode)
60 {
61  struct hostent *phostent;
62  struct sockaddr_in serv_addr;
63 
64  // The hostname byte-array is a NULL terminated C-string.
65  // XXX Not GC safe.
66  ByteArray ba(hostname);
67  char* name = (char*) ba.get_raw_data_ptr();
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  ByteArray ba(b);
111  void* buf = (void*) (((int8_t*) ba.get_raw_data_ptr()) + off);
112 
113  // Receive from the socket.
114  ssize_t result = recv(handle, buf, len, 0);
115 
116  if (result == 0) {
117  // The peer has performed an orderly shutdown.
118  return -1;
119  }
120  else if (result < 0) {
121  os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf: recv failed");
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 jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readByte(JNIEnv *env, jclass clazz, jint handle)
134 {
135  char byte;
136 
137  // Receive from the socket.
138  ssize_t result = recv(handle, &byte, 1, 0);
139 
140  if (result == 0) {
141  // The peer has performed an orderly shutdown.
142  return -1;
143  }
144  else if (result < 0) {
145  // TODO Should throw an IOException.
146  os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_readByte: recv failed");
147  }
148 
149  return byte;
150 }
151 
152 
153 /*
154  * Class: com/sun/cldc/io/j2me/socket/Protocol
155  * Method: writeBuf
156  * Signature: (I[BII)I
157  */
158 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf(JNIEnv *env, jclass clazz, jint handle, jbyteArray b, jint off, jint len)
159 {
160  // Get pointer to the buffer.
161  // XXX Not GC safe.
162  ByteArray ba(b);
163  void* buf = (void*) (((int8_t*) ba.get_raw_data_ptr()) + off);
164 
165  // Send the given byte to the socket.
166  ssize_t result = send(handle, buf, len, 0);
167 
168  if (result < 0) {
169  // TODO Should throw an IOException.
170  os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf: send failed");
171  }
172 
173  return result;
174 }
175 
176 
177 /*
178  * Class: com/sun/cldc/io/j2me/socket/Protocol
179  * Method: writeByte
180  * Signature: (II)I
181  */
182 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte(JNIEnv *env, jclass clazz, jint handle, jint b)
183 {
184  char byte = (char) b;
185 
186  // Send the given byte to the socket.
187  ssize_t result = send(handle, &byte, 1, 0);
188 
189  if (result < 0)
190  os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte: send failed");
191 
192  return result;
193 }
194 
195 
196 /*
197  * Class: com/sun/cldc/io/j2me/socket/Protocol
198  * Method: available0
199  * Signature: (I)I
200  */
201 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_available0(JNIEnv *env, jclass clazz, jint handle)
202 {
203  // NOTE: Sun doesn't have an implementation too.
204  return 0;
205 }
206 
207 
208 /*
209  * Class: com/sun/cldc/io/j2me/socket/Protocol
210  * Method: close0
211  * Signature: (I)V
212  */
213 JNIEXPORT void JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_close0(JNIEnv *env, jclass clazz, jint handle)
214 {
215  // Close the file descriptor.
216  int result = close(handle);
217 
218  if (result < 0)
219  os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_close0: close failed");
220 }
221 
222 } // extern "C"
223 
224 
225 /* native methods implemented by this file ************************************/
226 
227 static JNINativeMethod methods[] = {
228  { (char*) "open0", (char*) "([BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_open0 },
229  { (char*) "readBuf", (char*) "(I[BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf },
230  { (char*) "readByte", (char*) "(I)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_readByte },
231  { (char*) "writeBuf", (char*) "(I[BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf },
232  { (char*) "writeByte", (char*) "(II)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte },
233  { (char*) "available0", (char*) "(I)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_available0 },
234  { (char*) "close0", (char*) "(I)V", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_close0 },
235 };
236 
237 
238 /* _Jv_com_sun_cldc_io_j2me_socket_Protocol_init *******************************
239 
240  Register native functions.
241 
242 *******************************************************************************/
243 
245 {
246  Utf8String u = Utf8String::from_utf8("com/sun/cldc/io/j2me/socket/Protocol");
247 
250 }
251 
252 
253 /*
254  * These are local overrides for various environment variables in Emacs.
255  * Please do not remove this and leave it at the end of the file, where
256  * Emacs will automagically detect them.
257  * ---------------------------------------------------------------------
258  * Local variables:
259  * mode: c++
260  * indent-tabs-mode: t
261  * c-basic-offset: 4
262  * tab-width: 4
263  * End:
264  * vim:noexpandtab:sw=4:ts=4:
265  */
JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf(JNIEnv *env, jclass clazz, jint handle, jbyteArray b, jint off, jint len)
JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readByte(JNIEnv *env, jclass clazz, jint handle)
Table containing all native methods registered with the VM.
Definition: native.hpp:132
NativeMethods & get_nativemethods()
Definition: vm.hpp:128
void register_methods(Utf8String classname, const JNINativeMethod *methods, size_t count)
Register native methods with the VM.
Definition: native.cpp:242
_Jv_JNIEnv JNIEnv
Definition: jni.hpp:112
static void abort_errno(const char *text,...)
Equal to abort_errnum, but uses errno to get the error number.
Definition: os.cpp:165
#define NATIVE_METHODS_COUNT
Definition: native.hpp:45
int8_t * get_raw_data_ptr()
Definition: array.hpp:333
uint8_t u1
Definition: types.hpp:40
JNIEnv jclass jobject const char * name
Definition: jvmti.h:312
void _Jv_com_sun_cldc_io_j2me_socket_Protocol_init(void)
JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf(JNIEnv *env, jclass clazz, jint handle, jbyteArray b, jint off, jint len)
static Utf8String from_utf8(const char *, size_t)
Definition: utf8.cpp:335
JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_open0(JNIEnv *env, jclass clazz, jbyteArray hostname, jint port, jint mode)
JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_available0(JNIEnv *env, jclass clazz, jint handle)
static JNINativeMethod methods[]
JNIEXPORT void JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_close0(JNIEnv *env, jclass clazz, jint handle)
#define MCOPY(dest, src, type, num)
Definition: memory.hpp:103
static VM * get_current()
Definition: vm.hpp:99
JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte(JNIEnv *env, jclass clazz, jint handle, jint b)