2019-10-08 14:52:01 +00:00
|
|
|
#include "network_loki_lokinet_LokinetDaemon.h"
|
|
|
|
#include "lokinet_jni_common.hpp"
|
2020-09-22 19:04:15 +00:00
|
|
|
#include <llarp.hpp>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/config/config.hpp>
|
2019-10-08 14:52:01 +00:00
|
|
|
|
|
|
|
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-09-22 19:04:15 +00:00
|
|
|
auto* ptr = new llarp::Context();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ptr == nullptr)
|
2019-10-08 14:52:01 +00:00
|
|
|
return nullptr;
|
2020-09-22 19:04:15 +00:00
|
|
|
return env->NewDirectByteBuffer(ptr, sizeof(llarp::Context));
|
2019-10-08 14:52:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-22 19:04:15 +00:00
|
|
|
auto ptr = FromBuffer<llarp::Context>(env, buf);
|
|
|
|
delete ptr;
|
2019-10-08 14:52:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-22 19:04:15 +00:00
|
|
|
auto ptr = GetImpl<llarp::Context>(env, self);
|
|
|
|
auto config = GetImpl<llarp::Config>(env, conf);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ptr == nullptr || config == nullptr)
|
2019-10-08 14:52:01 +00:00
|
|
|
return JNI_FALSE;
|
2020-09-22 19:04:15 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
llarp::RuntimeOptions opts{};
|
2021-03-02 18:18:22 +00:00
|
|
|
|
|
|
|
// janky make_shared deep copy because jni + shared pointer = scary
|
|
|
|
ptr->Configure(std::make_shared<llarp::Config>(*config));
|
2020-09-22 19:04:15 +00:00
|
|
|
ptr->Setup(opts);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return JNI_FALSE;
|
|
|
|
}
|
|
|
|
return JNI_TRUE;
|
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
|
|
|
{
|
2020-09-22 19:04:15 +00:00
|
|
|
auto ptr = GetImpl<llarp::Context>(env, self);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ptr == nullptr)
|
2019-10-08 14:52:01 +00:00
|
|
|
return -1;
|
2020-09-22 19:04:15 +00:00
|
|
|
llarp::RuntimeOptions opts{};
|
|
|
|
return ptr->Run(opts);
|
2019-10-08 14:52:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09-22 19:04:15 +00:00
|
|
|
auto ptr = GetImpl<llarp::Context>(env, self);
|
|
|
|
return (ptr != nullptr && ptr->IsUp()) ? 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-09-22 19:04:15 +00:00
|
|
|
auto ptr = GetImpl<llarp::Context>(env, self);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ptr == nullptr)
|
2019-10-08 14:52:01 +00:00
|
|
|
return JNI_FALSE;
|
2020-09-22 19:04:15 +00:00
|
|
|
if (not ptr->IsUp())
|
2019-10-08 14:52:01 +00:00
|
|
|
return JNI_FALSE;
|
2020-09-22 19:04:15 +00:00
|
|
|
ptr->CloseAsync();
|
|
|
|
ptr->Wait();
|
|
|
|
return ptr->IsUp() ? JNI_FALSE : JNI_TRUE;
|
2019-10-08 14:52:01 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 18:18:22 +00:00
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
Java_network_loki_lokinet_LokinetDaemon_InjectVPNFD(JNIEnv* env, jobject self)
|
2019-10-08 14:52:01 +00:00
|
|
|
{
|
2020-09-22 19:04:15 +00:00
|
|
|
auto ptr = GetImpl<llarp::Context>(env, self);
|
2021-03-02 18:18:22 +00:00
|
|
|
|
|
|
|
ptr->androidFD = GetObjectMemberAsInt<int>(env, self, "m_FD");
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jint JNICALL
|
|
|
|
Java_network_loki_lokinet_LokinetDaemon_GetUDPSocket(JNIEnv* env, jobject self)
|
|
|
|
{
|
|
|
|
auto ptr = GetImpl<llarp::Context>(env, self);
|
|
|
|
|
|
|
|
return ptr->GetUDPSocket();
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jstring JNICALL
|
|
|
|
Java_network_loki_lokinet_LokinetDaemon_DetectFreeRange(JNIEnv* env, jclass)
|
|
|
|
{
|
|
|
|
std::string rangestr{};
|
|
|
|
if (auto maybe = llarp::FindFreeRange())
|
|
|
|
{
|
|
|
|
rangestr = maybe->ToString();
|
|
|
|
}
|
|
|
|
return env->NewStringUTF(rangestr.c_str());
|
2019-10-08 14:52:01 +00:00
|
|
|
}
|
2021-03-02 18:18:22 +00:00
|
|
|
}
|