2019-10-08 14:52:01 +00:00
|
|
|
#include "network_loki_lokinet_LokinetVPN.h"
|
|
|
|
#include "lokinet_jni_vpnio.hpp"
|
|
|
|
#include "lokinet_jni_common.hpp"
|
|
|
|
#include <net/ip.hpp>
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
JNIEXPORT jint JNICALL
|
2020-04-07 18:38:56 +00:00
|
|
|
Java_network_loki_lokinet_LokinetVPN_PacketSize(JNIEnv*, jclass)
|
2019-10-08 14:52:01 +00:00
|
|
|
{
|
|
|
|
return llarp::net::IPPacket::MaxSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jobject JNICALL
|
2020-04-07 18:38:56 +00:00
|
|
|
Java_network_loki_lokinet_LokinetVPN_Alloc(JNIEnv* env, jclass)
|
2019-10-08 14:52:01 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
lokinet_jni_vpnio* vpn = new lokinet_jni_vpnio();
|
2019-10-08 14:52:01 +00:00
|
|
|
return env->NewDirectByteBuffer(vpn, sizeof(lokinet_jni_vpnio));
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL
|
2020-04-07 18:38:56 +00:00
|
|
|
Java_network_loki_lokinet_LokinetVPN_Free(JNIEnv* env, jclass, jobject buf)
|
2019-10-08 14:52:01 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
lokinet_jni_vpnio* vpn = FromBuffer<lokinet_jni_vpnio>(env, buf);
|
|
|
|
if (vpn == nullptr)
|
2019-10-08 14:52:01 +00:00
|
|
|
return;
|
|
|
|
delete vpn;
|
|
|
|
}
|
|
|
|
JNIEXPORT void JNICALL
|
2020-04-07 18:38:56 +00:00
|
|
|
Java_network_loki_lokinet_LokinetVPN_Stop(JNIEnv* env, jobject self)
|
2019-10-08 14:52:01 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
lokinet_jni_vpnio* vpn = GetImpl<lokinet_jni_vpnio>(env, self);
|
|
|
|
if (vpn)
|
2019-10-08 14:52:01 +00:00
|
|
|
{
|
|
|
|
vpn->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jint JNICALL
|
2020-04-07 18:38:56 +00:00
|
|
|
Java_network_loki_lokinet_LokinetVPN_ReadPkt(JNIEnv* env, jobject self, jobject pkt)
|
2019-10-08 14:52:01 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
lokinet_jni_vpnio* vpn = GetImpl<lokinet_jni_vpnio>(env, self);
|
|
|
|
if (vpn == nullptr)
|
2019-10-08 14:52:01 +00:00
|
|
|
return -1;
|
2020-04-07 18:38:56 +00:00
|
|
|
void* pktbuf = env->GetDirectBufferAddress(pkt);
|
|
|
|
auto pktlen = env->GetDirectBufferCapacity(pkt);
|
|
|
|
if (pktbuf == nullptr)
|
2019-10-08 14:52:01 +00:00
|
|
|
return -1;
|
|
|
|
return vpn->ReadPacket(pktbuf, pktlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jboolean JNICALL
|
2020-04-07 18:38:56 +00:00
|
|
|
Java_network_loki_lokinet_LokinetVPN_WritePkt(JNIEnv* env, jobject self, jobject pkt)
|
2019-10-08 14:52:01 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
lokinet_jni_vpnio* vpn = GetImpl<lokinet_jni_vpnio>(env, self);
|
|
|
|
if (vpn == nullptr)
|
2019-10-08 14:52:01 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
void* pktbuf = env->GetDirectBufferAddress(pkt);
|
|
|
|
auto pktlen = env->GetDirectBufferCapacity(pkt);
|
|
|
|
if (pktbuf == nullptr)
|
2019-10-08 14:52:01 +00:00
|
|
|
return false;
|
|
|
|
return vpn->WritePacket(pktbuf, pktlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL
|
2020-04-07 18:38:56 +00:00
|
|
|
Java_network_loki_lokinet_LokinetVPN_SetInfo(JNIEnv* env, jobject self, jobject info)
|
2019-10-08 14:52:01 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
lokinet_jni_vpnio* vpn = GetImpl<lokinet_jni_vpnio>(env, self);
|
|
|
|
if (vpn == nullptr)
|
2019-10-08 14:52:01 +00:00
|
|
|
return;
|
2020-04-07 18:38:56 +00:00
|
|
|
VisitObjectMemberStringAsStringView<bool>(
|
2019-10-08 14:52:01 +00:00
|
|
|
env, info, "ifaddr", [vpn](llarp::string_view val) -> bool {
|
|
|
|
vpn->SetIfAddr(val);
|
|
|
|
return true;
|
|
|
|
});
|
2020-04-07 18:38:56 +00:00
|
|
|
VisitObjectMemberStringAsStringView<bool>(
|
2019-10-08 14:52:01 +00:00
|
|
|
env, info, "ifname", [vpn](llarp::string_view val) -> bool {
|
|
|
|
vpn->SetIfName(val);
|
|
|
|
return true;
|
|
|
|
});
|
2020-04-07 18:38:56 +00:00
|
|
|
vpn->info.netmask = GetObjectMemberAsInt<uint8_t>(env, info, "netmask");
|
2019-10-08 14:52:01 +00:00
|
|
|
}
|
|
|
|
}
|