corda/sgx-jvm/avian/classpath/java-net.cpp

145 lines
4.6 KiB
C++
Raw Normal View History

2015-03-13 18:52:59 +00:00
/* Copyright (c) 2008-2015, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
2017-01-19 16:55:54 +00:00
#ifndef SGX
#include "jni.h"
2013-11-04 21:07:43 +00:00
#include "avian/machine.h"
#include "sockets.h"
#include "jni-util.h"
2013-11-04 21:07:43 +00:00
using namespace avian::classpath::sockets;
2014-07-11 15:50:18 +00:00
extern "C" JNIEXPORT void JNICALL Java_java_net_Socket_init(JNIEnv* e, jclass)
{
init(e);
2013-11-04 21:07:43 +00:00
}
extern "C" JNIEXPORT SOCKET JNICALL
2014-07-11 15:50:18 +00:00
Java_java_net_Socket_create(JNIEnv* e, jclass)
{
return create(e);
2013-11-04 21:07:43 +00:00
}
2014-07-11 15:50:18 +00:00
extern "C" JNIEXPORT void JNICALL Java_java_net_Socket_connect(JNIEnv* e,
jclass,
jlong sock,
jlong addr,
jshort port)
2014-07-11 15:50:18 +00:00
{
connect(e, static_cast<SOCKET>(sock), addr, port);
2013-11-04 21:07:43 +00:00
}
2014-07-11 15:50:18 +00:00
extern "C" JNIEXPORT void JNICALL Java_java_net_Socket_bind(JNIEnv* e,
jclass,
jlong sock,
jlong addr,
jshort port)
2014-07-11 15:50:18 +00:00
{
bind(e, static_cast<SOCKET>(sock), addr, port);
2013-11-04 21:07:43 +00:00
}
extern "C" JNIEXPORT void JNICALL
Java_java_net_Socket_abort(JNIEnv* e, jclass, jlong sock)
2014-07-11 15:50:18 +00:00
{
abort(e, static_cast<SOCKET>(sock));
2013-11-04 21:07:43 +00:00
}
extern "C" JNIEXPORT void JNICALL
Java_java_net_Socket_close(JNIEnv* e, jclass, jlong sock)
2014-07-11 15:50:18 +00:00
{
close(e, static_cast<SOCKET>(sock));
2013-11-04 21:07:43 +00:00
}
extern "C" JNIEXPORT void JNICALL
Java_java_net_Socket_closeOutput(JNIEnv* e, jclass, jlong sock)
2014-07-11 15:50:18 +00:00
{
close_output(e, static_cast<SOCKET>(sock));
2013-11-04 21:07:43 +00:00
}
extern "C" JNIEXPORT void JNICALL
Java_java_net_Socket_closeInput(JNIEnv* e, jclass, jlong sock)
2014-07-11 15:50:18 +00:00
{
close_input(e, static_cast<SOCKET>(sock));
2013-11-04 21:07:43 +00:00
}
extern "C" JNIEXPORT void JNICALL
2014-07-11 15:50:18 +00:00
Avian_java_net_Socket_send(vm::Thread* t, vm::object, uintptr_t* arguments)
{ /* SOCKET s, object buffer_obj, int start_pos, int count */
SOCKET& s = *(reinterpret_cast<SOCKET*>(&arguments[0]));
vm::GcByteArray* buffer_obj = vm::cast<vm::GcByteArray>(
t, reinterpret_cast<vm::object>(arguments[2]));
int32_t& start_pos = *(reinterpret_cast<int32_t*>(&arguments[3]));
int32_t& count = *(reinterpret_cast<int32_t*>(&arguments[4]));
char* buffer = reinterpret_cast<char*>(&buffer_obj->body()[start_pos]);
avian::classpath::sockets::send((JNIEnv*)t, s, buffer, count);
2013-11-04 21:07:43 +00:00
}
extern "C" JNIEXPORT int64_t JNICALL
2014-07-11 15:50:18 +00:00
Avian_java_net_Socket_recv(vm::Thread* t, vm::object, uintptr_t* arguments)
{ /* SOCKET s, object buffer_obj, int start_pos, int count */
SOCKET& s = *(reinterpret_cast<SOCKET*>(&arguments[0]));
vm::GcByteArray* buffer_obj = vm::cast<vm::GcByteArray>(
t, reinterpret_cast<vm::object>(arguments[2]));
int32_t& start_pos = *(reinterpret_cast<int32_t*>(&arguments[3]));
int32_t& count = *(reinterpret_cast<int32_t*>(&arguments[4]));
char* buffer = reinterpret_cast<char*>(&buffer_obj->body()[start_pos]);
return avian::classpath::sockets::recv((JNIEnv*)t, s, buffer, count);
}
extern "C" JNIEXPORT jint JNICALL
2014-07-11 15:50:18 +00:00
Java_java_net_InetAddress_ipv4AddressForName(JNIEnv* e,
jclass,
jstring name)
{
if(!name) {
throwNew(e, "java/lang/NullPointerException", 0);
return 0;
}
const char* chars = e->GetStringUTFChars(name, 0);
if (chars) {
#ifdef PLATFORM_WINDOWS
hostent* host = gethostbyname(chars);
e->ReleaseStringUTFChars(name, chars);
if (host) {
2013-11-04 21:07:43 +00:00
return ntohl(reinterpret_cast<in_addr*>(host->h_addr_list[0])->s_addr);
} else {
throwNew(e, "java/net/UnknownHostException", 0);
return 0;
}
#else
addrinfo hints;
memset(&hints, 0, sizeof(addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
addrinfo* result;
int r = getaddrinfo(chars, 0, &hints, &result);
e->ReleaseStringUTFChars(name, chars);
if (r != 0) {
throwNew(e, "java/net/UnknownHostException", 0);
return 0;
} else {
2014-07-11 15:50:18 +00:00
int address = ntohl(
reinterpret_cast<sockaddr_in*>(result->ai_addr)->sin_addr.s_addr);
freeaddrinfo(result);
return address;
}
#endif
} else {
throwNew(e, "java/lang/OutOfMemoryError", 0);
return 0;
}
}
2017-01-19 16:55:54 +00:00
#endif // !SGX