revive android build system (#1339)

* it lives?

* clean up

* add readme and add x86_64 to abi filters

* disable route poking on android

* make it compile on android

* it compiles!!111

* typofix

* re-enable ccache for android
pull/1359/head
Jeff 4 years ago committed by GitHub
parent ef794bbd11
commit fe30193a97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -275,7 +275,7 @@ pkg_check_modules(SD libsystemd)
# Default WITH_SYSTEMD to true if we found it # Default WITH_SYSTEMD to true if we found it
option(WITH_SYSTEMD "enable systemd integration for sd_notify" ${SD_FOUND}) option(WITH_SYSTEMD "enable systemd integration for sd_notify" ${SD_FOUND})
if(WITH_SYSTEMD) if(WITH_SYSTEMD AND (NOT ANDROID))
if(NOT SD_FOUND) if(NOT SD_FOUND)
message(FATAL_ERROR "libsystemd not found") message(FATAL_ERROR "libsystemd not found")
endif() endif()

@ -3,11 +3,7 @@
package="network.loki.lokinet" package="network.loki.lokinet"
android:installLocation="auto" android:installLocation="auto"
android:versionCode="1" android:versionCode="1"
android:versionName="0.3.2"> android:versionName="0.8.0">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="28" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" /> <!-- normal perm, per https://developer.android.com/guide/topics/permissions/normal-permissions.html --> <uses-permission android:name="android.permission.INTERNET" /> <!-- normal perm, per https://developer.android.com/guide/topics/permissions/normal-permissions.html -->

@ -2,9 +2,10 @@ buildscript {
repositories { repositories {
mavenCentral() mavenCentral()
jcenter() jcenter()
google()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:2.3.3' classpath 'com.android.tools.build:gradle:4.0.1'
} }
} }
@ -16,31 +17,33 @@ repositories {
maven { maven {
url 'https://maven.google.com' url 'https://maven.google.com'
} }
google()
} }
android { android {
compileSdkVersion 28 compileSdkVersion 28
buildToolsVersion "28.0.1"
defaultConfig { defaultConfig {
applicationId "network.loki.lokinet" applicationId "network.loki.lokinet"
targetSdkVersion 28 targetSdkVersion 28
minSdkVersion 23 minSdkVersion 23
versionCode 1 versionCode 1
versionName "0.4.0" versionName '0.8.0'
ndk {
abiFilters 'armeabi'
}
externalNativeBuild { externalNativeBuild {
cmake { cmake {
targets "lokinetandroid" // targets "lokinet-android"
arguments "-DANDROID=ON", "-DANDROID_STL=c++_static", "-DANDROID_ARM_NEON=TRUE", "-DLIBUV_ROOT=" + libuvsrc, "-DANDROID_TOOLCHAIN=clang", "-DANDROID_ARM_MODE=arm" arguments "-DWITH_LTO=OFF", "-DCXXOPTS_BUILD_TESTS=OFF","-DWITH_TESTS=OFF", "-DCMAKE_CROSSCOMPILING=ON", "-DNATIVE_BUILD=OFF", "-DANDROID=ON", "-DANDROID_STL=c++_static", "-DBUILD_STATIC_DEPS=ON", "-DBUILD_SHARED_LIBS=OFF", "-DSTATIC_LINK=ON", "-DDOWNLOAD_UV=FORCE", "-DANDROID_ARM_MODE=arm"
cppFlags "-fexceptions -std=c++14 -frtti" cppFlags "-std=c++17"
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
// abiFilters 'armeabi-v7a'
// abiFilters 'arm64-v8a', 'x86_64', 'armeabi-v7a'
} }
} }
packagingOptions{
doNotStrip "*/armeabi/*.so"
doNotStrip "*/armeabi-v7a/*.so" }
doNotStrip "*/x86/*.so" externalNativeBuild {
cmake {
path "../CMakeLists.txt"
} }
} }
sourceSets { sourceSets {
@ -63,13 +66,16 @@ android {
minifyEnabled true minifyEnabled true
//signingConfig signingConfigs.jeff //signingConfig signingConfigs.jeff
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt' proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
debuggable false
} }
} debug {
externalNativeBuild { // jniDebuggable true
cmake {
path lokinetCMake
} }
} }
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
} }

@ -0,0 +1,33 @@
# lokinet android
this directory contains basic stuff for lokinet on android.
## Prerequsites
To build you need the following:
* Gradle (6.x)
* Android SDK (latest version)
* Android NDK (latest version)
## Building
Next set up the path to Android SDK and NDK in `local.properties`
```
sdk.dir=/path/to/android/sdk
ndk.dir=/path/to/android/ndk
```
Then build:
$ gradle assemble
This fetches a large amount (several dozen Gigabytes) of files from some
server somewhere dumping it on your filesystem to make the thing do the
building, then proceeds to peg all your cores for several dozen minutes
while it does the required incantations to build 2 apks.
The build outputs apks to to subdirectories in `build/outputs/apk/`
one called `debug` for debug builds and one called `release` for release builds.

@ -42,7 +42,7 @@ public class LokiNetActivity extends Activity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
textView = new TextView(this); textView = new TextView(this);
setContentView(textView); setContentView(textView);
System.loadLibrary("lokinetandroid"); System.loadLibrary("lokinet-android");
} }

@ -68,6 +68,7 @@ if(CMAKE_CXX_COMPILER_LAUNCHER)
set(deps_cxx "${CMAKE_CXX_COMPILER_LAUNCHER} ${deps_cxx}") set(deps_cxx "${CMAKE_CXX_COMPILER_LAUNCHER} ${deps_cxx}")
endif() endif()
function(expand_urls output source_file) function(expand_urls output source_file)
set(expanded) set(expanded)
foreach(mirror ${ARGN}) foreach(mirror ${ARGN})
@ -100,6 +101,46 @@ if(CMAKE_CROSSCOMPILING)
set(cross_rc "WINDRES=${CMAKE_RC_COMPILER}") set(cross_rc "WINDRES=${CMAKE_RC_COMPILER}")
endif() endif()
endif() endif()
if(ANDROID)
set(android_toolchain_suffix linux-android)
set(android_compiler_suffix linux-android23)
if(CMAKE_ANDROID_ARCH_ABI MATCHES x86_64)
set(android_machine x86_64)
set(cross_host "--host=x86_64-linux-android")
set(android_compiler_prefix x86_64)
set(android_compiler_suffix linux-android23)
set(android_toolchain_prefix x86_64)
set(android_toolchain_suffix linux-android)
elseif(CMAKE_ANDROID_ARCH_ABI MATCHES x86)
set(android_machine i686)
set(cross_host "--host=i686-linux-android")
set(android_compiler_prefix i686)
set(android_compiler_suffix linux-android23)
set(android_toolchain_prefix i686)
set(android_toolchain_suffix linux-android)
elseif(CMAKE_ANDROID_ARCH_ABI MATCHES armeabi-v7a)
set(android_machine armv7)
set(cross_host "--host=armv7a-linux-androideabi")
set(android_compiler_prefix armv7a)
set(android_compiler_suffix linux-androideabi23)
set(android_toolchain_prefix arm)
set(android_toolchain_suffix linux-androideabi)
elseif(CMAKE_ANDROID_ARCH_ABI MATCHES arm64-v8a)
set(android_machine aarch64)
set(cross_host "--host=aarch64-linux-android")
set(android_compiler_prefix aarch64)
set(android_compiler_suffix linux-android23)
set(android_toolchain_prefix aarch64)
set(android_toolchain_suffix linux-android)
else()
message(FATAL_ERROR "unknown android arch: ${CMAKE_ANDROID_ARCH_ABI}")
endif()
set(deps_cc "${CMAKE_ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin/${android_compiler_prefix}-${android_compiler_suffix}-clang")
set(deps_cxx "${CMAKE_ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin/${android_compiler_prefix}-${android_compiler_suffix}-clang++")
set(deps_ld "${CMAKE_ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin/${android_compiler_prefix}-${android_toolchain_suffix}-ld")
set(deps_ranlib "${CMAKE_ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin/${android_toolchain_prefix}-${android_toolchain_suffix}-ranlib")
set(deps_ar "${CMAKE_ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin/${android_toolchain_prefix}-${android_toolchain_suffix}-ar")
endif()
# Builds a target; takes the target name (e.g. "readline") and builds it in an external project with # Builds a target; takes the target name (e.g. "readline") and builds it in an external project with
@ -149,11 +190,14 @@ if(CMAKE_CROSSCOMPILING)
set(openssl_system_env SYSTEM=MINGW64 RC=${CMAKE_RC_COMPILER} AR=${ARCH_TRIPLET}-ar RANLIB=${ARCH_TRIPLET}-ranlib) set(openssl_system_env SYSTEM=MINGW64 RC=${CMAKE_RC_COMPILER} AR=${ARCH_TRIPLET}-ar RANLIB=${ARCH_TRIPLET}-ranlib)
elseif(ARCH_TRIPLET STREQUAL i686-w64-mingw32) elseif(ARCH_TRIPLET STREQUAL i686-w64-mingw32)
set(openssl_system_env SYSTEM=MINGW32 RC=${CMAKE_RC_COMPILER} AR=${ARCH_TRIPLET}-ar RANLIB=${ARCH_TRIPLET}-ranlib) set(openssl_system_env SYSTEM=MINGW32 RC=${CMAKE_RC_COMPILER} AR=${ARCH_TRIPLET}-ar RANLIB=${ARCH_TRIPLET}-ranlib)
elseif(ANDROID)
set(openssl_system_env SYSTEM=Linux MACHINE=${android_machine} LD=${deps_ld} RANLIB=${deps_ranlib} AR=${deps_ar})
set(openssl_extra_opts no-asm)
endif() endif()
endif() endif()
build_external(openssl build_external(openssl
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env CC=${deps_cc} ${openssl_system_env} ./config CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env CC=${deps_cc} ${openssl_system_env} ./config
--prefix=${DEPS_DESTDIR} no-shared no-capieng no-dso no-dtls1 no-ec_nistp_64_gcc_128 no-gost --prefix=${DEPS_DESTDIR} ${openssl_extra_opts} no-shared no-capieng no-dso no-dtls1 no-ec_nistp_64_gcc_128 no-gost
no-heartbeats no-md2 no-rc5 no-rdrand no-rfc3779 no-sctp no-ssl-trace no-ssl2 no-ssl3 no-heartbeats no-md2 no-rc5 no-rdrand no-rfc3779 no-sctp no-ssl-trace no-ssl2 no-ssl3
no-static-engine no-tests no-weak-ssl-ciphers no-zlib no-zlib-dynamic "CFLAGS=-O2 ${flto}" no-static-engine no-tests no-weak-ssl-ciphers no-zlib no-zlib-dynamic "CFLAGS=-O2 ${flto}"
INSTALL_COMMAND make install_sw INSTALL_COMMAND make install_sw
@ -197,7 +241,8 @@ endif()
build_external(sodium) build_external(sodium CONFIGURE_COMMAND ./configure ${cross_host} ${cross_rc} --prefix=${DEPS_DESTDIR} --disable-shared
--enable-static --with-pic "CC=${deps_cc}" "CFLAGS=-O2 ${flto}")
add_static_target(sodium sodium_external libsodium.a) add_static_target(sodium sodium_external libsodium.a)
build_external(sqlite3) build_external(sqlite3)
@ -208,6 +253,7 @@ if(ZMQ_VERSION VERSION_LESS 4.3.4 AND CMAKE_CROSSCOMPILING AND ARCH_TRIPLET MATC
set(zmq_patch set(zmq_patch
PATCH_COMMAND patch -p1 -i ${PROJECT_SOURCE_DIR}/contrib/cross/patches/libzmq-mingw-closesocket.patch) PATCH_COMMAND patch -p1 -i ${PROJECT_SOURCE_DIR}/contrib/cross/patches/libzmq-mingw-closesocket.patch)
endif() endif()
build_external(zmq build_external(zmq
DEPENDS sodium_external DEPENDS sodium_external
${zmq_patch} ${zmq_patch}

@ -1,5 +1,7 @@
if(NOT UNIX) if(NOT ANDROID)
return() if(NOT UNIX)
return()
endif()
endif() endif()
include(CheckCXXSourceCompiles) include(CheckCXXSourceCompiles)
@ -35,8 +37,10 @@ elseif(DOWNLOAD_UV)
add_subdirectory(${LIBUV_ROOT}) add_subdirectory(${LIBUV_ROOT})
set(LIBUV_INCLUDE_DIRS ${LIBUV_ROOT}/include) set(LIBUV_INCLUDE_DIRS ${LIBUV_ROOT}/include)
set(LIBUV_LIBRARY uv_a) set(LIBUV_LIBRARY uv_a)
add_definitions(-D_LARGEFILE_SOURCE) if(NOT ANDROID)
add_definitions(-D_FILE_OFFSET_BITS=64) add_definitions(-D_LARGEFILE_SOURCE)
add_definitions(-D_FILE_OFFSET_BITS=64)
endif()
endif() endif()
include_directories(${LIBUV_INCLUDE_DIRS}) include_directories(${LIBUV_INCLUDE_DIRS})

@ -46,7 +46,7 @@ set(NTRU_AVX_SRC
include(CheckCXXCompilerFlag) include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-mavx2 COMPILER_SUPPORTS_AVX2) check_cxx_compiler_flag(-mavx2 COMPILER_SUPPORTS_AVX2)
check_cxx_compiler_flag(-mfma COMPILER_SUPPORTS_FMA) check_cxx_compiler_flag(-mfma COMPILER_SUPPORTS_FMA)
if(COMPILER_SUPPORTS_AVX2 AND COMPILER_SUPPORTS_FMA) if(COMPILER_SUPPORTS_AVX2 AND COMPILER_SUPPORTS_FMA AND (NOT ANDROID))
target_sources(lokinet-cryptography PRIVATE ${NTRU_AVX_SRC}) target_sources(lokinet-cryptography PRIVATE ${NTRU_AVX_SRC})
set_property(SOURCE ${NTRU_AVX_SRC} APPEND PROPERTY COMPILE_FLAGS "-mavx2 -mfma") set_property(SOURCE ${NTRU_AVX_SRC} APPEND PROPERTY COMPILE_FLAGS "-mavx2 -mfma")
message(STATUS "Building libntrup with runtime AVX2/FMA support") message(STATUS "Building libntrup with runtime AVX2/FMA support")

@ -85,14 +85,13 @@ extern "C"
void void
llarp_vpn_io_close_async(struct llarp_vpn_io* io); llarp_vpn_io_close_async(struct llarp_vpn_io* io);
/*
/// give main context a vpn io for mobile when it is reader to do io with /// give main context a vpn io for mobile when it is reader to do io with
/// associated info tries to give the vpn io to endpoint with name epName a /// associated info tries to give the vpn io to endpoint with name epName a
/// deferred call to llarp_vpn_io.injected is queued unconditionally /// deferred call to llarp_vpn_io.injected is queued unconditionally
/// thread safe /// thread safe
bool bool
llarp_main_inject_vpn_by_name( llarp_main_inject_vpn_by_name(
struct llarp_main* m, struct llarp::Context* m,
const char* epName, const char* epName,
struct llarp_vpn_io* io, struct llarp_vpn_io* io,
struct llarp_vpn_ifaddr_info info); struct llarp_vpn_ifaddr_info info);
@ -100,11 +99,10 @@ extern "C"
/// give main context a vpn io on its default endpoint /// give main context a vpn io on its default endpoint
static bool static bool
llarp_main_inject_default_vpn( llarp_main_inject_default_vpn(
struct llarp_main* m, struct llarp_vpn_io* io, struct llarp_vpn_ifaddr_info info) struct llarp::Context* m, struct llarp_vpn_io* io, struct llarp_vpn_ifaddr_info info)
{ {
return llarp_main_inject_vpn_by_name(m, llarp_main_get_default_endpoint_name(m), io, info); return llarp_main_inject_vpn_by_name(m, "default", io, info);
} }
*/
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -1,4 +1,5 @@
add_library(lokinet-android add_library(lokinet-android
SHARED
lokinet_config.cpp lokinet_config.cpp
lokinet_daemon.cpp lokinet_daemon.cpp
lokinet_vpn.cpp) lokinet_vpn.cpp)

@ -7,30 +7,29 @@ extern "C"
JNIEXPORT jobject JNICALL JNIEXPORT jobject JNICALL
Java_network_loki_lokinet_LokinetConfig_Obtain(JNIEnv* env, jclass) Java_network_loki_lokinet_LokinetConfig_Obtain(JNIEnv* env, jclass)
{ {
llarp_config* conf = llarp_default_config(); auto conf = new llarp::Config();
if (conf == nullptr) if (conf == nullptr)
return nullptr; return nullptr;
return env->NewDirectByteBuffer(conf, llarp_config_size()); return env->NewDirectByteBuffer(conf, sizeof(llarp::Config));
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_network_loki_lokinet_LokinetConfig_Free(JNIEnv* env, jclass, jobject buf) Java_network_loki_lokinet_LokinetConfig_Free(JNIEnv* env, jclass, jobject buf)
{ {
llarp_config_free(FromBuffer<llarp_config>(env, buf)); auto ptr = FromBuffer<llarp::Config>(env, buf);
delete ptr;
} }
JNIEXPORT jboolean JNICALL JNIEXPORT jboolean JNICALL
Java_network_loki_lokinet_LokinetConfig_Load(JNIEnv* env, jobject self, jstring fname) Java_network_loki_lokinet_LokinetConfig_Load(JNIEnv* env, jobject self, jstring fname)
{ {
llarp_config* conf = GetImpl<llarp_config>(env, self); auto conf = GetImpl<llarp::Config>(env, self);
if (conf == nullptr) if (conf == nullptr)
return JNI_FALSE; return JNI_FALSE;
return VisitStringAsStringView<jboolean>( return VisitStringAsStringView<jboolean>(env, fname, [conf](std::string_view val) -> jboolean {
env, fname, [conf](llarp::string_view val) -> jboolean { if (conf->Load(val, false, llarp::GetDefaultDataDir()))
const auto filename = llarp::string_view_string(val); return JNI_TRUE;
if (llarp_config_read_file(conf, filename.c_str())) return JNI_FALSE;
return JNI_TRUE; });
return JNI_FALSE;
});
} }
} }

@ -1,72 +1,81 @@
#include "network_loki_lokinet_LokinetDaemon.h" #include "network_loki_lokinet_LokinetDaemon.h"
#include "lokinet_jni_common.hpp" #include "lokinet_jni_common.hpp"
#include "lokinet_jni_vpnio.hpp" #include "lokinet_jni_vpnio.hpp"
#include <llarp.h> #include <llarp.hpp>
extern "C" extern "C"
{ {
JNIEXPORT jobject JNICALL JNIEXPORT jobject JNICALL
Java_network_loki_lokinet_LokinetDaemon_Obtain(JNIEnv* env, jclass) Java_network_loki_lokinet_LokinetDaemon_Obtain(JNIEnv* env, jclass)
{ {
llarp_main* ptr = llarp_main_default_init(); auto* ptr = new llarp::Context();
if (ptr == nullptr) if (ptr == nullptr)
return nullptr; return nullptr;
return env->NewDirectByteBuffer(ptr, llarp_main_size()); return env->NewDirectByteBuffer(ptr, sizeof(llarp::Context));
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_network_loki_lokinet_LokinetDaemon_Free(JNIEnv* env, jclass, jobject buf) Java_network_loki_lokinet_LokinetDaemon_Free(JNIEnv* env, jclass, jobject buf)
{ {
llarp_main* ptr = FromBuffer<llarp_main>(env, buf); auto ptr = FromBuffer<llarp::Context>(env, buf);
llarp_main_free(ptr); delete ptr;
} }
JNIEXPORT jboolean JNICALL JNIEXPORT jboolean JNICALL
Java_network_loki_lokinet_LokinetDaemon_Configure(JNIEnv* env, jobject self, jobject conf) Java_network_loki_lokinet_LokinetDaemon_Configure(JNIEnv* env, jobject self, jobject conf)
{ {
llarp_main* ptr = GetImpl<llarp_main>(env, self); auto ptr = GetImpl<llarp::Context>(env, self);
llarp_config* config = GetImpl<llarp_config>(env, conf); auto config = GetImpl<llarp::Config>(env, conf);
if (ptr == nullptr || config == nullptr) if (ptr == nullptr || config == nullptr)
return JNI_FALSE; return JNI_FALSE;
if (llarp_main_configure(ptr, config)) try
return JNI_TRUE; {
return llarp_main_setup(ptr) == 0 ? JNI_TRUE : JNI_FALSE; llarp::RuntimeOptions opts{};
ptr->Configure(*config);
ptr->Setup(opts);
}
catch (...)
{
return JNI_FALSE;
}
return JNI_TRUE;
} }
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_network_loki_lokinet_LokinetDaemon_Mainloop(JNIEnv* env, jobject self) Java_network_loki_lokinet_LokinetDaemon_Mainloop(JNIEnv* env, jobject self)
{ {
static llarp_main_runtime_opts opts; auto ptr = GetImpl<llarp::Context>(env, self);
llarp_main* ptr = GetImpl<llarp_main>(env, self);
if (ptr == nullptr) if (ptr == nullptr)
return -1; return -1;
return llarp_main_run(ptr, opts); llarp::RuntimeOptions opts{};
return ptr->Run(opts);
} }
JNIEXPORT jboolean JNICALL JNIEXPORT jboolean JNICALL
Java_network_loki_lokinet_LokinetDaemon_IsRunning(JNIEnv* env, jobject self) Java_network_loki_lokinet_LokinetDaemon_IsRunning(JNIEnv* env, jobject self)
{ {
llarp_main* ptr = GetImpl<llarp_main>(env, self); auto ptr = GetImpl<llarp::Context>(env, self);
return (ptr != nullptr && llarp_main_is_running(ptr)) ? JNI_TRUE : JNI_FALSE; return (ptr != nullptr && ptr->IsUp()) ? JNI_TRUE : JNI_FALSE;
} }
JNIEXPORT jboolean JNICALL JNIEXPORT jboolean JNICALL
Java_network_loki_lokinet_LokinetDaemon_Stop(JNIEnv* env, jobject self) Java_network_loki_lokinet_LokinetDaemon_Stop(JNIEnv* env, jobject self)
{ {
llarp_main* ptr = GetImpl<llarp_main>(env, self); auto ptr = GetImpl<llarp::Context>(env, self);
if (ptr == nullptr) if (ptr == nullptr)
return JNI_FALSE; return JNI_FALSE;
if (not llarp_main_is_running(ptr)) if (not ptr->IsUp())
return JNI_FALSE; return JNI_FALSE;
llarp_main_stop(ptr); ptr->CloseAsync();
return llarp_main_is_running(ptr) ? JNI_FALSE : JNI_TRUE; ptr->Wait();
return ptr->IsUp() ? JNI_FALSE : JNI_TRUE;
} }
JNIEXPORT jboolean JNICALL JNIEXPORT jboolean JNICALL
Java_network_loki_lokinet_LokinetDaemon_InjectVPN(JNIEnv* env, jobject self, jobject vpn) Java_network_loki_lokinet_LokinetDaemon_InjectVPN(JNIEnv* env, jobject self, jobject vpn)
{ {
llarp_main* ptr = GetImpl<llarp_main>(env, self); auto ptr = GetImpl<llarp::Context>(env, self);
lokinet_jni_vpnio* impl = GetImpl<lokinet_jni_vpnio>(env, vpn); auto impl = GetImpl<lokinet_jni_vpnio>(env, vpn);
if (ptr == nullptr || impl == nullptr) if (ptr == nullptr || impl == nullptr)
return JNI_FALSE; return JNI_FALSE;
if (impl->info.netmask == 0) if (impl->info.netmask == 0)

@ -2,7 +2,7 @@
#define LOKINET_JNI_COMMON_HPP #define LOKINET_JNI_COMMON_HPP
#include <jni.h> #include <jni.h>
#include <util/string_view.hpp> #include <string_view>
#include <functional> #include <functional>
/// visit string as native bytes /// visit string as native bytes
@ -21,7 +21,7 @@ VisitStringAsStringView(JNIEnv* env, jobject str, V visit)
const size_t length = env->GetArrayLength(stringJbytes); const size_t length = env->GetArrayLength(stringJbytes);
jbyte* pBytes = env->GetByteArrayElements(stringJbytes, NULL); jbyte* pBytes = env->GetByteArrayElements(stringJbytes, NULL);
T result = visit(llarp::string_view((const char*)pBytes, length)); T result = visit(std::string_view((const char*)pBytes, length));
env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT); env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT);
env->DeleteLocalRef(stringJbytes); env->DeleteLocalRef(stringJbytes);

@ -4,7 +4,7 @@
#include <llarp.h> #include <llarp.h>
#include <memory> #include <memory>
#include <future> #include <future>
#include <util/string_view.hpp> #include <string_view>
#include <algorithm> #include <algorithm>
#include <jni.h> #include <jni.h>
@ -56,7 +56,7 @@ namespace lokinet
} }
bool bool
Init(llarp_main* ptr) Init(llarp::Context* ptr)
{ {
if (Ready()) if (Ready())
return false; return false;
@ -114,14 +114,14 @@ namespace lokinet
} }
void void
SetIfName(llarp::string_view val) SetIfName(std::string_view val)
{ {
const auto sz = std::min(val.size(), sizeof(info.ifname)); const auto sz = std::min(val.size(), sizeof(info.ifname));
std::copy_n(val.data(), sz, info.ifname); std::copy_n(val.data(), sz, info.ifname);
} }
void void
SetIfAddr(llarp::string_view val) SetIfAddr(std::string_view val)
{ {
const auto sz = std::min(val.size(), sizeof(info.ifaddr)); const auto sz = std::min(val.size(), sizeof(info.ifaddr));
std::copy_n(val.data(), sz, info.ifaddr); std::copy_n(val.data(), sz, info.ifaddr);

@ -2,6 +2,8 @@
#include "lokinet_jni_vpnio.hpp" #include "lokinet_jni_vpnio.hpp"
#include "lokinet_jni_common.hpp" #include "lokinet_jni_common.hpp"
#include <net/ip.hpp> #include <net/ip.hpp>
#include <net/ip_packet.hpp>
#include <string_view>
extern "C" extern "C"
{ {
@ -69,12 +71,12 @@ extern "C"
if (vpn == nullptr) if (vpn == nullptr)
return; return;
VisitObjectMemberStringAsStringView<bool>( VisitObjectMemberStringAsStringView<bool>(
env, info, "ifaddr", [vpn](llarp::string_view val) -> bool { env, info, "ifaddr", [vpn](std::string_view val) -> bool {
vpn->SetIfAddr(val); vpn->SetIfAddr(val);
return true; return true;
}); });
VisitObjectMemberStringAsStringView<bool>( VisitObjectMemberStringAsStringView<bool>(
env, info, "ifname", [vpn](llarp::string_view val) -> bool { env, info, "ifname", [vpn](std::string_view val) -> bool {
vpn->SetIfName(val); vpn->SetIfName(val);
return true; return true;
}); });

@ -6,8 +6,10 @@
#include <unistd.h> #include <unistd.h>
#include <net/if.h> #include <net/if.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#ifndef ANDROID
#include <sys/socket.h> #include <sys/socket.h>
#include <linux/rtnetlink.h> #include <linux/rtnetlink.h>
#endif
#include <net/net.hpp> #include <net/net.hpp>
#include <exception> #include <exception>
#include <charconv> #include <charconv>
@ -76,6 +78,7 @@ namespace llarp::net
#endif #endif
#ifdef __linux__ #ifdef __linux__
#ifndef ANDROID
struct NLSocket struct NLSocket
{ {
NLSocket() : fd(socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) NLSocket() : fd(socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE))
@ -217,12 +220,13 @@ namespace llarp::net
} }
#endif #endif
#endif
void void
AddRoute(std::string ip, std::string gateway) AddRoute(std::string ip, std::string gateway)
{ {
LogInfo("Add route: ", ip, " via ", gateway); LogInfo("Add route: ", ip, " via ", gateway);
#ifdef __linux__ #ifdef __linux__
#ifndef ANDROID
NLSocket sock; NLSocket sock;
int default_gw = 0; int default_gw = 0;
int if_idx = 0; int if_idx = 0;
@ -233,6 +237,7 @@ namespace llarp::net
read_addr(gateway.c_str(), &gw_addr); read_addr(gateway.c_str(), &gw_addr);
read_addr(ip.c_str(), &to_addr); read_addr(ip.c_str(), &to_addr);
do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, default_gw, if_idx); do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, default_gw, if_idx);
#endif
#else #else
std::stringstream ss; std::stringstream ss;
#if _WIN32 #if _WIN32
@ -251,6 +256,7 @@ namespace llarp::net
{ {
LogInfo("Delete route: ", ip, " via ", gateway); LogInfo("Delete route: ", ip, " via ", gateway);
#ifdef __linux__ #ifdef __linux__
#ifndef ANDROID
NLSocket sock; NLSocket sock;
int default_gw = 0; int default_gw = 0;
int if_idx = 0; int if_idx = 0;
@ -261,6 +267,7 @@ namespace llarp::net
read_addr(gateway.c_str(), &gw_addr); read_addr(gateway.c_str(), &gw_addr);
read_addr(ip.c_str(), &to_addr); read_addr(ip.c_str(), &to_addr);
do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, default_gw, if_idx); do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, default_gw, if_idx);
#endif
#else #else
std::stringstream ss; std::stringstream ss;
#if _WIN32 #if _WIN32
@ -279,6 +286,7 @@ namespace llarp::net
{ {
LogInfo("Add default route via ", ifname); LogInfo("Add default route via ", ifname);
#ifdef __linux__ #ifdef __linux__
#ifndef ANDROID
NLSocket sock; NLSocket sock;
int default_gw = 1; int default_gw = 1;
int if_idx = if_nametoindex(ifname.c_str()); int if_idx = if_nametoindex(ifname.c_str());
@ -292,6 +300,7 @@ namespace llarp::net
int nl_flags = NLM_F_CREATE | NLM_F_EXCL; int nl_flags = NLM_F_CREATE | NLM_F_EXCL;
read_addr(maybe->toHost().c_str(), &gw_addr); read_addr(maybe->toHost().c_str(), &gw_addr);
do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, default_gw, if_idx); do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, default_gw, if_idx);
#endif
#elif _WIN32 #elif _WIN32
ifname.back()++; ifname.back()++;
Execute("route ADD 0.0.0.0 MASK 128.0.0.0 " + ifname); Execute("route ADD 0.0.0.0 MASK 128.0.0.0 " + ifname);
@ -309,6 +318,7 @@ namespace llarp::net
{ {
LogInfo("Remove default route via ", ifname); LogInfo("Remove default route via ", ifname);
#ifdef __linux__ #ifdef __linux__
#ifndef ANDROID
NLSocket sock; NLSocket sock;
int default_gw = 1; int default_gw = 1;
int if_idx = if_nametoindex(ifname.c_str()); int if_idx = if_nametoindex(ifname.c_str());
@ -322,6 +332,7 @@ namespace llarp::net
int nl_flags = 0; int nl_flags = 0;
read_addr(maybe->toHost().c_str(), &gw_addr); read_addr(maybe->toHost().c_str(), &gw_addr);
do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, default_gw, if_idx); do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, default_gw, if_idx);
#endif
#elif _WIN32 #elif _WIN32
ifname.back()++; ifname.back()++;
Execute("route DELETE 0.0.0.0 MASK 128.0.0.0 " + ifname); Execute("route DELETE 0.0.0.0 MASK 128.0.0.0 " + ifname);

@ -16,7 +16,9 @@ namespace llarp
return; return;
case eLogTrace: case eLogTrace:
ss << "[TRC] "; ss << "[TRC] ";
break case eLogDebug : ss << "[DBG] "; break;
case eLogDebug:
ss << "[DBG] ";
break; break;
case eLogInfo: case eLogInfo:
ss << "[NFO] "; ss << "[NFO] ";
@ -50,8 +52,6 @@ namespace llarp
switch (lvl) switch (lvl)
{ {
case eLogTrace: case eLogTrace:
__android_log_write(ANDROID_LOG_TRACE, str.c_str(), msg.c_str());
return;
case eLogDebug: case eLogDebug:
__android_log_write(ANDROID_LOG_DEBUG, str.c_str(), msg.c_str()); __android_log_write(ANDROID_LOG_DEBUG, str.c_str(), msg.c_str());
return; return;

@ -24,6 +24,9 @@ namespace llarp
PostLog(std::stringstream&) const override; PostLog(std::stringstream&) const override;
void Tick(llarp_time_t) override; void Tick(llarp_time_t) override;
void
ImmediateFlush() override{};
}; };
} // namespace llarp } // namespace llarp

Loading…
Cancel
Save