lokinet/jni/lokinet_daemon.cpp
Thomas Winget 4c630e0437 Large collection of changes to make android work
- Previous android java and jni code updated to work, but with much love
  still needed to make it work nicely, e.g. handling when the VPN is
  turned off.

- DNS handling refactored to allow android to intercept and handle DNS
  requests as we can't set the system DNS to use a high port
  (and apparently Chrome ignores system DNS settings anyway)

- add packet router structure to allow separate handling of specific
  intercepted traffic, e.g. UDP traffic to port 53 gets handled by our
  DNS handler rather than being naively forwarded as exit traffic.

- For now, android lokinet is exit-only and hard-coded to use exit.loki
  as its exit.  The exit will be configurable before release, but
  allowing to not use exit-only mode is more of a challenge.

- some old gitignore remnants which were matching to things we don't
  want them to (and are no longer relevant) removed

- some minor changes to CI configuration
2021-03-02 13:18:22 -05:00

104 lines
2.7 KiB
C++

#include "network_loki_lokinet_LokinetDaemon.h"
#include "lokinet_jni_common.hpp"
#include "lokinet_jni_vpnio.hpp"
#include <llarp.hpp>
#include <config/config.hpp>
extern "C"
{
JNIEXPORT jobject JNICALL
Java_network_loki_lokinet_LokinetDaemon_Obtain(JNIEnv* env, jclass)
{
auto* ptr = new llarp::Context();
if (ptr == nullptr)
return nullptr;
return env->NewDirectByteBuffer(ptr, sizeof(llarp::Context));
}
JNIEXPORT void JNICALL
Java_network_loki_lokinet_LokinetDaemon_Free(JNIEnv* env, jclass, jobject buf)
{
auto ptr = FromBuffer<llarp::Context>(env, buf);
delete ptr;
}
JNIEXPORT jboolean JNICALL
Java_network_loki_lokinet_LokinetDaemon_Configure(JNIEnv* env, jobject self, jobject conf)
{
auto ptr = GetImpl<llarp::Context>(env, self);
auto config = GetImpl<llarp::Config>(env, conf);
if (ptr == nullptr || config == nullptr)
return JNI_FALSE;
try
{
llarp::RuntimeOptions opts{};
// janky make_shared deep copy because jni + shared pointer = scary
ptr->Configure(std::make_shared<llarp::Config>(*config));
ptr->Setup(opts);
}
catch (...)
{
return JNI_FALSE;
}
return JNI_TRUE;
}
JNIEXPORT jint JNICALL
Java_network_loki_lokinet_LokinetDaemon_Mainloop(JNIEnv* env, jobject self)
{
auto ptr = GetImpl<llarp::Context>(env, self);
if (ptr == nullptr)
return -1;
llarp::RuntimeOptions opts{};
return ptr->Run(opts);
}
JNIEXPORT jboolean JNICALL
Java_network_loki_lokinet_LokinetDaemon_IsRunning(JNIEnv* env, jobject self)
{
auto ptr = GetImpl<llarp::Context>(env, self);
return (ptr != nullptr && ptr->IsUp()) ? JNI_TRUE : JNI_FALSE;
}
JNIEXPORT jboolean JNICALL
Java_network_loki_lokinet_LokinetDaemon_Stop(JNIEnv* env, jobject self)
{
auto ptr = GetImpl<llarp::Context>(env, self);
if (ptr == nullptr)
return JNI_FALSE;
if (not ptr->IsUp())
return JNI_FALSE;
ptr->CloseAsync();
ptr->Wait();
return ptr->IsUp() ? JNI_FALSE : JNI_TRUE;
}
JNIEXPORT void JNICALL
Java_network_loki_lokinet_LokinetDaemon_InjectVPNFD(JNIEnv* env, jobject self)
{
auto ptr = GetImpl<llarp::Context>(env, self);
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());
}
}