1 /* class: java/net/PlainSocketImpl */
3 #include <sys/socket.h>
5 #include <netinet/tcp.h>
8 * Class: java/net/PlainSocketImpl
12 JNIEXPORT void JNICALL
13 Java_java_net_PlainSocketImpl_initProto (JNIEnv *env )
16 log_text("Java_java_net_PlainSocketImpl_initProto called");
20 * Class: java/net/PlainSocketImpl
21 * Method: socketAccept
22 * Signature: (Ljava/net/SocketImpl;)V
24 JNIEXPORT void JNICALL
25 Java_java_net_PlainSocketImpl_socketAccept (JNIEnv *env , struct java_net_PlainSocketImpl* this , struct java_net_SocketImpl* sock)
29 struct sockaddr_in addr;
32 log_text("Java_java_net_PlainSocketImpl_socketAccept called");
36 addr.sin_len = sizeof(addr);
38 addr.sin_family = AF_INET;
39 addr.sin_port = htons(sock->localport);
40 addr.sin_addr.s_addr = htonl(sock->address->address);
42 r = threadedAccept(this->fd->fd, (struct sockaddr*)&addr, &alen);
44 exceptionptr = native_new_and_init (class_java_io_IOException);
49 /* Enter information into socket object */
51 r = getpeername(r, (struct sockaddr*)&addr, &alen);
53 exceptionptr = native_new_and_init (class_java_io_IOException);
57 sock->address->address = ntohl(addr.sin_addr.s_addr);
58 sock->port = ntohs(addr.sin_port);
62 * Class: java/net/PlainSocketImpl
63 * Method: socketAvailable
67 Java_java_net_PlainSocketImpl_socketAvailable (JNIEnv *env , struct java_net_PlainSocketImpl* this )
73 log_text("Java_java_net_PlainSocketImpl_socketAvailable called");
75 r = ioctl(this->fd->fd, FIONREAD, &len);
77 exceptionptr = native_new_and_init (class_java_io_IOException);
84 * Class: java/net/PlainSocketImpl
86 * Signature: (Ljava/net/InetAddress;I)V
88 JNIEXPORT void JNICALL
89 Java_java_net_PlainSocketImpl_socketBind (JNIEnv *env , struct java_net_PlainSocketImpl* this , struct java_net_InetAddress* laddr, s4 lport)
92 struct sockaddr_in addr;
98 log_text("Java_java_net_PlainSocketImpl_socketBind called");
101 addr.sin_len = sizeof(addr);
103 addr.sin_family = AF_INET;
104 addr.sin_port = htons(lport);
105 addr.sin_addr.s_addr = htonl(laddr->address);
109 /* Allow rebinding to socket - ignore errors */
110 (void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
111 r = bind(fd, (struct sockaddr*)&addr, sizeof(addr));
113 exceptionptr = native_new_and_init (class_java_io_IOException);
117 /* Enter information into socket object */
118 this->address = laddr;
121 r = getsockname(fd, (struct sockaddr*)&addr, &alen);
123 exceptionptr = native_new_and_init (class_java_io_IOException);
126 lport = ntohs(addr.sin_port);
128 this->localport = lport;
132 * Class: java/net/PlainSocketImpl
133 * Method: socketClose
136 JNIEXPORT void JNICALL
137 Java_java_net_PlainSocketImpl_socketClose (JNIEnv *env , struct java_net_PlainSocketImpl* this)
142 log_text("Java_java_net_PlainSocketImpl_socketClose called");
144 if (this->fd->fd != -1) {
145 r = close(this->fd->fd);
148 exceptionptr = native_new_and_init (class_java_io_IOException);
155 * Class: java/net/PlainSocketImpl
156 * Method: socketConnect
157 * Signature: (Ljava/net/InetAddress;I)V
159 JNIEXPORT void JNICALL
160 Java_java_net_PlainSocketImpl_socketConnect (JNIEnv *env , struct java_net_PlainSocketImpl* this , struct java_net_InetAddress* daddr, s4 dport)
164 struct sockaddr_in addr;
168 log_text("Java_java_net_PlainSocketImpl_socketConnect called");
171 addr.sin_len = sizeof(addr);
173 addr.sin_family = AF_INET;
174 addr.sin_port = htons(dport);
175 addr.sin_addr.s_addr = htonl(daddr->address);
178 r = threadedConnect(fd, (struct sockaddr*)&addr, sizeof(addr));
180 exceptionptr = native_new_and_init (class_java_io_IOException);
184 /* Enter information into socket object */
186 r = getsockname(fd, (struct sockaddr*)&addr, &alen);
188 exceptionptr = native_new_and_init (class_java_io_IOException);
192 this->address = daddr;
194 this->localport = ntohs(addr.sin_port);
198 * Class: java/net/PlainSocketImpl
199 * Method: socketCreate
202 JNIEXPORT void JNICALL
203 Java_java_net_PlainSocketImpl_socketCreate (JNIEnv *env , struct java_net_PlainSocketImpl* this , s4 /* bool */ stream)
209 log_text("Java_java_net_PlainSocketImpl_socketCreate called");
218 fd = threadedSocket(AF_INET, type, 0);
220 exceptionptr = native_new_and_init (class_java_io_IOException);
227 * Class: java/net/PlainSocketImpl
228 * Method: socketGetOption
232 Java_java_net_PlainSocketImpl_socketGetOption (JNIEnv *env , struct java_net_PlainSocketImpl* this , s4 opt)
239 struct linger linger;
243 log_text("Java_java_net_PlainSocketImpl_socketGetOption called");
247 case /* SO_LINGER */ 0x0080 :
251 optlen = sizeof(struct linger);
254 case /* TCP_NODELAY */ 0x0001 :
256 optname = TCP_NODELAY;
258 optlen = sizeof(int);
261 case /* SO_SNDBUF */ 0x1001 :
262 case /* SO_RCVBUF */ 0x1002 :
264 optname = (opt == 0x1001) ? SO_SNDBUF : SO_RCVBUF;
266 optlen = sizeof(int);
269 case /* SO_BINDADDR */ 0x000f :
270 return this->address->address;
276 if (getsockopt(this->fd->fd, level, optname, optval, &optlen) == -1)
277 exceptionptr = native_new_and_init(class_java_net_SocketException);
283 return linger.l_linger;
289 return -1; /* -1 == false? */
302 * Class: java/net/PlainSocketImpl
303 * Method: socketListen
306 JNIEXPORT void JNICALL
307 Java_java_net_PlainSocketImpl_socketListen (JNIEnv *env , struct java_net_PlainSocketImpl* this , s4 count)
312 log_text("java_net_PlainSocketImpl_socketListen called");
314 r = listen(this->fd->fd, count);
316 exceptionptr = native_new_and_init (class_java_io_IOException);
322 * Class: java/net/PlainSocketImpl
323 * Method: socketSetOption
324 * Signature: (IZLjava/lang/Object;)V
326 JNIEXPORT void JNICALL
327 Java_java_net_PlainSocketImpl_socketSetOption (JNIEnv *env,
328 struct java_net_PlainSocketImpl* this , s4 cmd, s4 /* bool */ on, struct java_lang_Object* value)
335 struct linger linger;
340 log_text("Java_java_net_PlainSocketImpl_socketSetOption called");
344 case /* SO_LINGER */ 0x0080 :
348 linger.l_linger = ((java_lang_Integer*)value)->value;
356 optlen = sizeof(struct linger);
359 case /* TCP_NODELAY */ 0x0001 :
360 nodelay = on ? 1 : 0;
363 optname = TCP_NODELAY;
365 optlen = sizeof(int);
368 case /* SO_SNDBUF */ 0x1001 :
369 case /* SO_RCVBUF */ 0x1002 :
370 bufsize = ((java_lang_Integer*)value)->value;
373 optname = (cmd == 0x1001) ? SO_SNDBUF : SO_RCVBUF;
375 optlen = sizeof(int);
382 if (setsockopt(this->fd->fd, level, optname, optval, optlen) == -1)
383 exceptionptr = native_new_and_init(class_java_net_SocketException);