2019-10-08 14:52:01 +00:00
|
|
|
#include "network_loki_lokinet_LokinetDaemon.h"
|
|
|
|
#include "lokinet_jni_common.hpp"
|
|
|
|
#include "lokinet_jni_vpnio.hpp"
|
|
|
|
#include <llarp.h>
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
JNIEXPORT jobject JNICALL
|
2020-04-07 18:38:56 +00:00
|
|
|
Java_network_loki_lokinet_LokinetDaemon_Obtain(JNIEnv* env, jclass)
|
2019-10-08 14:52:01 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_main* ptr = llarp_main_default_init();
|
|
|
|
if (ptr == nullptr)
|
2019-10-08 14:52:01 +00:00
|
|
|
return nullptr;
|
|
|
|
return env->NewDirectByteBuffer(ptr, llarp_main_size());
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL
|
2020-04-07 18:38:56 +00:00
|
|
|
Java_network_loki_lokinet_LokinetDaemon_Free(JNIEnv* env, jclass, jobject buf)
|
2019-10-08 14:52:01 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_main* ptr = FromBuffer<llarp_main>(env, buf);
|
2019-10-08 14:52:01 +00:00
|
|
|
llarp_main_free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jboolean JNICALL
|
2020-04-07 18:38:56 +00:00
|
|
|
Java_network_loki_lokinet_LokinetDaemon_Configure(JNIEnv* env, jobject self, jobject conf)
|
2019-10-08 14:52:01 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_main* ptr = GetImpl<llarp_main>(env, self);
|
|
|
|
llarp_config* config = GetImpl<llarp_config>(env, conf);
|
|
|
|
if (ptr == nullptr || config == nullptr)
|
2019-10-08 14:52:01 +00:00
|
|
|
return JNI_FALSE;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (llarp_main_configure(ptr, config))
|
2019-10-08 14:52:01 +00:00
|
|
|
return JNI_TRUE;
|
2019-10-09 13:08:38 +00:00
|
|
|
return llarp_main_setup(ptr) == 0 ? JNI_TRUE : JNI_FALSE;
|
2019-10-08 14:52:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jint JNICALL
|
2020-04-07 18:38:56 +00:00
|
|
|
Java_network_loki_lokinet_LokinetDaemon_Mainloop(JNIEnv* env, jobject self)
|
2019-10-08 14:52:01 +00:00
|
|
|
{
|
|
|
|
static llarp_main_runtime_opts opts;
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_main* ptr = GetImpl<llarp_main>(env, self);
|
|
|
|
if (ptr == nullptr)
|
2019-10-08 14:52:01 +00:00
|
|
|
return -1;
|
|
|
|
return llarp_main_run(ptr, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jboolean JNICALL
|
2020-04-07 18:38:56 +00:00
|
|
|
Java_network_loki_lokinet_LokinetDaemon_IsRunning(JNIEnv* env, jobject self)
|
2019-10-08 14:52:01 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_main* ptr = GetImpl<llarp_main>(env, self);
|
|
|
|
return (ptr != nullptr && llarp_main_is_running(ptr)) ? JNI_TRUE : JNI_FALSE;
|
2019-10-08 14:52:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jboolean JNICALL
|
2020-04-07 18:38:56 +00:00
|
|
|
Java_network_loki_lokinet_LokinetDaemon_Stop(JNIEnv* env, jobject self)
|
2019-10-08 14:52:01 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_main* ptr = GetImpl<llarp_main>(env, self);
|
|
|
|
if (ptr == nullptr)
|
2019-10-08 14:52:01 +00:00
|
|
|
return JNI_FALSE;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not llarp_main_is_running(ptr))
|
2019-10-08 14:52:01 +00:00
|
|
|
return JNI_FALSE;
|
|
|
|
llarp_main_stop(ptr);
|
|
|
|
return llarp_main_is_running(ptr) ? JNI_FALSE : JNI_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jboolean JNICALL
|
2020-04-07 18:38:56 +00:00
|
|
|
Java_network_loki_lokinet_LokinetDaemon_InjectVPN(JNIEnv* env, jobject self, jobject vpn)
|
2019-10-08 14:52:01 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_main* ptr = GetImpl<llarp_main>(env, self);
|
|
|
|
lokinet_jni_vpnio* impl = GetImpl<lokinet_jni_vpnio>(env, vpn);
|
|
|
|
if (ptr == nullptr || impl == nullptr)
|
2019-10-08 14:52:01 +00:00
|
|
|
return JNI_FALSE;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (impl->info.netmask == 0)
|
2019-10-09 12:40:40 +00:00
|
|
|
return JNI_FALSE;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not impl->Init(ptr))
|
2019-10-09 12:40:40 +00:00
|
|
|
return JNI_FALSE;
|
2020-04-07 18:38:56 +00:00
|
|
|
return llarp_main_inject_default_vpn(ptr, &impl->io, impl->info) ? JNI_TRUE : JNI_FALSE;
|
2019-10-08 14:52:01 +00:00
|
|
|
}
|
|
|
|
}
|