* src/vm/exceptions.c (exceptions_throw_unsatisfiedlinkerror)
[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 <sys/types.h>
35 #include <sys/socket.h>
36
37 #include "vm/types.h"
38
39 #include "mm/memory.h"
40
41 #include "native/jni.h"
42
43 #include "vm/global.h"
44
45
46 /*
47  * Class:     com/sun/cldc/io/j2me/socket/Protocol
48  * Method:    open0
49  * Signature: ([BII)I
50  */
51 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_open0(JNIEnv *env, jclass clazz, java_bytearray *hostname, s4 port, s4 mode)
52 {
53         struct hostent *phostent;
54     struct sockaddr_in serv_addr;
55         char           *name;
56         s4              sockfd;
57         s4              result;
58
59         /* The hostname byte-array is a NULL terminated C-string. */
60
61         name = (char *) &(hostname->data);
62
63         /* get the host */
64
65         phostent = gethostbyname(name);
66
67         if (phostent == NULL)
68                 return -1;
69
70         /* fill the sockaddr structure */
71
72     serv_addr.sin_family = AF_INET;
73     serv_addr.sin_port   = htons(port);
74
75     MCOPY(&serv_addr.sin_addr, phostent->h_addr, u1, phostent->h_length);
76
77         /* create the socket */
78
79     sockfd = socket(AF_INET, SOCK_STREAM, 0);
80
81         if (sockfd < 0)
82                 return -1;
83
84         /* connect the socket */
85
86         result = connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
87
88         if (result < 0)
89                 return -1;
90
91         return sockfd;
92 }
93
94
95 /*
96  * Class:     com/sun/cldc/io/j2me/socket/Protocol
97  * Method:    readBuf
98  * Signature: (I[BII)I
99  */
100 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)
101 {
102         void    *buf;
103         ssize_t  result;
104
105         /* get pointer to the buffer */
106
107         buf = &(b->data[off]);
108
109         /* receive from the socket */
110
111         result = recv(handle, buf, len, 0);
112
113         if (result == 0) {
114                 /* the peer has performed an orderly shutdown */
115
116                 return -1;
117         }
118         else if (result < 0) {
119                 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf: recv failed: %s", strerror(errno));
120         }
121
122         return result;
123 }
124
125
126 /*
127  * Class:     com/sun/cldc/io/j2me/socket/Protocol
128  * Method:    writeByte
129  * Signature: (II)I
130  */
131 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte(JNIEnv *env, jclass clazz, s4 handle, s4 b)
132 {
133         char    byte;
134         ssize_t result;
135
136         byte = (char) b;
137
138         /* send the given byte to the socket */
139
140         result = send(handle, &byte, 1, 0);
141
142         if (result < 0)
143                 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte: send failed: %s", strerror(errno));
144
145         return result;
146 }
147
148
149 /*
150  * These are local overrides for various environment variables in Emacs.
151  * Please do not remove this and leave it at the end of the file, where
152  * Emacs will automagically detect them.
153  * ---------------------------------------------------------------------
154  * Local variables:
155  * mode: c
156  * indent-tabs-mode: t
157  * c-basic-offset: 4
158  * tab-width: 4
159  * End:
160  * vim:noexpandtab:sw=4:ts=4:
161  */