make android compile

pull/495/head
Jeff Becker 5 years ago
parent 2edf1da819
commit 3da6551e82
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -416,6 +416,7 @@ else()
if(ANDROID) if(ANDROID)
add_library(${ANDROID_LIB} SHARED jni/lokinet_android.cpp) add_library(${ANDROID_LIB} SHARED jni/lokinet_android.cpp)
set_property(TARGET ${ANDROID_LIB} PROPERTY CXX_STANDARD 14)
add_log_tag(${ANDROID_LIB}) add_log_tag(${ANDROID_LIB})
target_link_libraries(${ANDROID_LIB} ${STATIC_LIB} ${LIBS}) target_link_libraries(${ANDROID_LIB} ${STATIC_LIB} ${LIBS})
endif(ANDROID) endif(ANDROID)

@ -185,6 +185,7 @@ android-gradle-prepare:
echo "#auto generated don't modify kthnx" >> $(ANDROID_PROPS) echo "#auto generated don't modify kthnx" >> $(ANDROID_PROPS)
echo "lokinetCMake=$(REPO)/CMakeLists.txt" >> $(ANDROID_PROPS) echo "lokinetCMake=$(REPO)/CMakeLists.txt" >> $(ANDROID_PROPS)
echo "org.gradle.parallel=true" >> $(ANDROID_PROPS) echo "org.gradle.parallel=true" >> $(ANDROID_PROPS)
echo "org.gradle.jvmargs=-Xmx1536M" >> $(ANDROID_PROPS)
echo "#auto generated don't modify kthnx" >> $(ANDROID_LOCAL_PROPS) echo "#auto generated don't modify kthnx" >> $(ANDROID_LOCAL_PROPS)
echo "sdk.dir=$(ANDROID_SDK)" >> $(ANDROID_LOCAL_PROPS) echo "sdk.dir=$(ANDROID_SDK)" >> $(ANDROID_LOCAL_PROPS)
echo "ndk.dir=$(ANDROID_NDK)" >> $(ANDROID_LOCAL_PROPS) echo "ndk.dir=$(ANDROID_NDK)" >> $(ANDROID_LOCAL_PROPS)

@ -24,17 +24,17 @@ android {
defaultConfig { defaultConfig {
applicationId "network.loki.lokinet" applicationId "network.loki.lokinet"
targetSdkVersion 28 targetSdkVersion 28
minSdkVersion 14 minSdkVersion 23
versionCode 1 versionCode 1
versionName "0.4.0" versionName "0.4.0"
ndk { ndk {
abiFilters 'armeabi' abiFilters 'armeabi'
} }
externalNativeBuild { externalNativeBuild {
cmake { cmake {
targets "lokinetandroid" targets "lokinetandroid"
arguments "-DANDROID=ON", "-DANDROID_STL=c++_static" arguments "-DANDROID=ON", "-DANDROID_STL=c++_static", "-DANDROID_ARM_NEON=TRUE"
cppFlags "-fexceptions" cppFlags "-fexceptions -std=c++14 -frtti"
} }
} }
packagingOptions{ packagingOptions{

@ -29,10 +29,11 @@ public class Lokinet_JNI {
public static native void onNetworkStateChanged(boolean isConnected); public static native void onNetworkStateChanged(boolean isConnected);
/** /**
* set vpn network interface fd * set vpn network interface fd pair
* @param fd the file descriptor of the vpn interface * @param rfd the file descriptor of read end
* @param wfd the file descriptor of the write end
*/ */
public static native void setVPNFileDescriptor(int fd); public static native void setVPNFileDescriptor(int rfd, int wfd);
/** /**
* load jni libraries * load jni libraries

@ -1,15 +1,15 @@
#ifdef KAT #ifdef KAT
#include <stdio.h> #include <stdio.h>
#endif #endif
#include <sodium/crypto_hash_sha512.h>
#include <sodium/crypto_verify_32.h>
#include <sodium/crypto_kem.h>
#include "params.h" #include "params.h"
#include "small.h" #include "small.h"
#include "mod3.h" #include "mod3.h"
#include "rq.h" #include "rq.h"
#include "r3.h" #include "r3.h"
#include <sodium/crypto_hash_sha512.h>
#include <sodium/crypto_verify_32.h>
#include <sodium/crypto_kem.h>
int int
crypto_kem_dec_avx2(unsigned char *k, const unsigned char *cstr, crypto_kem_dec_avx2(unsigned char *k, const unsigned char *cstr,

@ -1,15 +1,15 @@
#ifdef KAT #ifdef KAT
#include <stdio.h> #include <stdio.h>
#endif #endif
#include <sodium/crypto_hash_sha512.h>
#include <sodium/crypto_verify_32.h>
#include <sodium/crypto_kem.h>
#include "params.h" #include "params.h"
#include "small.h" #include "small.h"
#include "mod3.h" #include "mod3.h"
#include "rq.h" #include "rq.h"
#include "r3.h" #include "r3.h"
#include <sodium/crypto_hash_sha512.h>
#include <sodium/crypto_verify_32.h>
#include <sodium/crypto_kem.h>
int int
crypto_kem_dec_ref(unsigned char *k, const unsigned char *cstr, crypto_kem_dec_ref(unsigned char *k, const unsigned char *cstr,

@ -35,9 +35,9 @@ extern "C"
void void
llarp_main_signal(struct llarp_main *ptr, int sig); llarp_main_signal(struct llarp_main *ptr, int sig);
/// give main context a vpn file descriptor (android/ios) /// give main context a vpn file descriptor pair (android/ios)
void void
llarp_main_inject_vpn_fd(struct llarp_main *m, int fd); llarp_main_inject_vpn_fd(struct llarp_main *m, int rfd, int wfd);
/// setup main context, returns 0 on success /// setup main context, returns 0 on success
int int

@ -97,10 +97,10 @@ struct AndroidMain
} }
void void
SetVPN_FD(int fd) SetVPN_FD(int rfd, int wfd)
{ {
if(m_impl) if(m_impl)
llarp_main_inject_vpn_fd(m_impl, fd); llarp_main_inject_vpn_fd(m_impl, rfd, wfd);
} }
/// stop daemon thread /// stop daemon thread
@ -120,7 +120,7 @@ struct AndroidMain
typedef std::unique_ptr< AndroidMain > Ptr; typedef std::unique_ptr< AndroidMain > Ptr;
}; };
static AndroidMain::Ptr daemon(new AndroidMain()); static AndroidMain::Ptr daemon_ptr(new AndroidMain());
extern "C" extern "C"
{ {
@ -135,7 +135,7 @@ extern "C"
Java_network_loki_lokinet_Lokinet_1JNI_startLokinet(JNIEnv* env, jclass, Java_network_loki_lokinet_Lokinet_1JNI_startLokinet(JNIEnv* env, jclass,
jstring configfile) jstring configfile)
{ {
if(daemon->Running()) if(daemon_ptr->Running())
return env->NewStringUTF("already running"); return env->NewStringUTF("already running");
std::string conf; std::string conf;
fs::path basepath; fs::path basepath;
@ -145,9 +145,9 @@ extern "C"
env->ReleaseStringUTFChars(configfile, nativeString); env->ReleaseStringUTFChars(configfile, nativeString);
basepath = fs::path(conf).parent_path(); basepath = fs::path(conf).parent_path();
} }
if(daemon->Configure(conf.c_str(), basepath.string().c_str())) if(daemon_ptr->Configure(conf.c_str(), basepath.string().c_str()))
{ {
if(daemon->Start()) if(daemon_ptr->Start())
return env->NewStringUTF("ok"); return env->NewStringUTF("ok");
else else
return env->NewStringUTF("failed to start daemon"); return env->NewStringUTF("failed to start daemon");
@ -159,24 +159,25 @@ extern "C"
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_network_loki_lokinet_Lokinet_1JNI_stopLokinet(JNIEnv*, jclass) Java_network_loki_lokinet_Lokinet_1JNI_stopLokinet(JNIEnv*, jclass)
{ {
if(daemon->Running()) if(daemon_ptr->Running())
{ {
daemon->Stop(); daemon_ptr->Stop();
} }
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_network_loki_lokinet_Lokinet_1JNI_setVPNFileDescriptor(JNIEnv*, jclass, Java_network_loki_lokinet_Lokinet_1JNI_setVPNFileDescriptor(JNIEnv*, jclass,
jint fd) jint rfd,
jint wfd)
{ {
daemon->SetVPN_FD(fd); daemon_ptr->SetVPN_FD(rfd, wfd);
} }
JNIEXPORT jstring JNICALL JNIEXPORT jstring JNICALL
Java_network_loki_lokinet_Lokinet_1JNI_getIfAddr(JNIEnv* env, jclass) Java_network_loki_lokinet_Lokinet_1JNI_getIfAddr(JNIEnv* env, jclass)
{ {
if(daemon) if(daemon_ptr)
return env->NewStringUTF(daemon->GetIfAddr()); return env->NewStringUTF(daemon_ptr->GetIfAddr());
else else
return env->NewStringUTF(""); return env->NewStringUTF("");
} }
@ -184,8 +185,8 @@ extern "C"
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_network_loki_lokinet_Lokinet_1JNI_getIfRange(JNIEnv*, jclass) Java_network_loki_lokinet_Lokinet_1JNI_getIfRange(JNIEnv*, jclass)
{ {
if(daemon) if(daemon_ptr)
return daemon->GetIfRange(); return daemon_ptr->GetIfRange();
else else
return -1; return -1;
} }
@ -196,17 +197,17 @@ extern "C"
{ {
if(isConnected) if(isConnected)
{ {
if(!daemon->Running()) if(!daemon_ptr->Running())
{ {
if(!daemon->Start()) if(!daemon_ptr->Start())
{ {
// TODO: do some kind of callback here // TODO: do some kind of callback here
} }
} }
} }
else if(daemon->Running()) else if(daemon_ptr->Running())
{ {
daemon->Stop(); daemon_ptr->Stop();
} }
} }
} }

@ -5,48 +5,52 @@
#ifndef _Included_network_loki_lokinet_Lokinet_JNI #ifndef _Included_network_loki_lokinet_Lokinet_JNI
#define _Included_network_loki_lokinet_Lokinet_JNI #define _Included_network_loki_lokinet_Lokinet_JNI
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C"
{
#endif #endif
/* /*
* Class: network_loki_lokinet_Lokinet_JNI * Class: network_loki_lokinet_Lokinet_JNI
* Method: getABICompiledWith * Method: getABICompiledWith
* Signature: ()Ljava/lang/String; * Signature: ()Ljava/lang/String;
*/ */
JNIEXPORT jstring JNICALL Java_network_loki_lokinet_Lokinet_1JNI_getABICompiledWith JNIEXPORT jstring JNICALL
(JNIEnv *, jclass); Java_network_loki_lokinet_Lokinet_1JNI_getABICompiledWith(JNIEnv *, jclass);
/* /*
* Class: network_loki_lokinet_Lokinet_JNI * Class: network_loki_lokinet_Lokinet_JNI
* Method: startLokinet * Method: startLokinet
* Signature: (Ljava/lang/String;)Ljava/lang/String; * Signature: (Ljava/lang/String;)Ljava/lang/String;
*/ */
JNIEXPORT jstring JNICALL Java_network_loki_lokinet_Lokinet_1JNI_startLokinet JNIEXPORT jstring JNICALL
(JNIEnv *, jclass, jstring); Java_network_loki_lokinet_Lokinet_1JNI_startLokinet(JNIEnv *, jclass,
jstring);
JNIEXPORT jstring JNICALL Java_network_loki_lokinet_Lokinet_1JNI_getIfAddr
(JNIEnv *, jclass); JNIEXPORT jstring JNICALL
Java_network_loki_lokinet_Lokinet_1JNI_getIfAddr(JNIEnv *, jclass);
JNIEXPORT jint JNICALL Java_network_loki_lokinet_Lokinet_1JNI_getIfRange
(JNIEnv *, jclass); JNIEXPORT jint JNICALL
Java_network_loki_lokinet_Lokinet_1JNI_getIfRange(JNIEnv *, jclass);
/*
* Class: network_loki_lokinet_Lokinet_JNI /*
* Method: stopLokinet * Class: network_loki_lokinet_Lokinet_JNI
* Signature: ()V * Method: stopLokinet
*/ * Signature: ()V
JNIEXPORT void JNICALL Java_network_loki_lokinet_Lokinet_1JNI_stopLokinet */
(JNIEnv *, jclass); JNIEXPORT void JNICALL
Java_network_loki_lokinet_Lokinet_1JNI_stopLokinet(JNIEnv *, jclass);
JNIEXPORT void JNICALL Java_network_loki_lokinet_Lokinet_1JNI_setVPNFileDescriptor
(JNIEnv *, jclass, jint); JNIEXPORT void JNICALL
Java_network_loki_lokinet_Lokinet_1JNI_setVPNFileDescriptor(JNIEnv *, jclass,
/* jint, jint);
* Class: network_loki_lokinet_Lokinet_JNI
* Method: onNetworkStateChanged /*
* Signature: (Z)V * Class: network_loki_lokinet_Lokinet_JNI
*/ * Method: onNetworkStateChanged
JNIEXPORT void JNICALL Java_network_loki_lokinet_Lokinet_1JNI_onNetworkStateChanged * Signature: (Z)V
(JNIEnv *, jclass, jboolean); */
JNIEXPORT void JNICALL
Java_network_loki_lokinet_Lokinet_1JNI_onNetworkStateChanged(JNIEnv *, jclass,
jboolean);
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -414,7 +414,7 @@ extern "C"
} }
void void
llarp_main_inject_vpn_fd(struct llarp_main *ptr, int fd) llarp_main_inject_vpn_fd(struct llarp_main *ptr, int rfd, int wfd)
{ {
llarp::handlers::TunEndpoint *tun = llarp::handlers::TunEndpoint *tun =
ptr->ctx->router->hiddenServiceContext().getFirstTun(); ptr->ctx->router->hiddenServiceContext().getFirstTun();
@ -422,7 +422,7 @@ extern "C"
return; return;
if(!tun->Promise) if(!tun->Promise)
return; return;
tun->Promise->Set(fd); tun->Promise->Set({rfd, wfd});
} }
int int

@ -40,12 +40,6 @@ llarp_make_ev_loop()
return r; return r;
} }
int
llarp_fd_promise_wait_for_value(struct llarp_fd_promise *p)
{
return p->Get();
}
void void
llarp_ev_loop_run_single_process(llarp_ev_loop_ptr ev, llarp_ev_loop_run_single_process(llarp_ev_loop_ptr ev,
struct llarp_threadpool *tp, struct llarp_threadpool *tp,

@ -660,7 +660,7 @@ namespace llarp
struct llarp_fd_promise struct llarp_fd_promise
{ {
void void
Set(int) Set(std::pair< int, int >)
{ {
} }
@ -673,18 +673,19 @@ struct llarp_fd_promise
#else #else
struct llarp_fd_promise struct llarp_fd_promise
{ {
llarp_fd_promise(std::promise< int >* p) : _impl(p) using promise_val_t = std::pair< int, int >;
llarp_fd_promise(std::promise< promise_val_t >* p) : _impl(p)
{ {
} }
std::promise< int >* _impl; std::promise< promise_val_t >* _impl;
void void
Set(int fd) Set(promise_val_t fds)
{ {
_impl->set_value(fd); _impl->set_value(fds);
} }
int promise_val_t
Get() Get()
{ {
auto future = _impl->get_future(); auto future = _impl->get_future();

@ -1,10 +1,5 @@
#include <ev/ev_epoll.hpp> #include <ev/ev_epoll.hpp>
#ifdef ANDROID
/** TODO: correct this value */
#define SOCK_NONBLOCK (0)
#endif
namespace llarp namespace llarp
{ {
int int
@ -191,6 +186,14 @@ namespace llarp
return ret; return ret;
} }
ssize_t
tun::do_write(void* buf, size_t sz)
{
if(writefd != -1) // case of android
return ::write(writefd, buf, sz);
return ev_io::do_write(buf, sz);
}
int int
tun::wait_for_fd_promise(struct device* dev) tun::wait_for_fd_promise(struct device* dev)
{ {
@ -199,7 +202,14 @@ namespace llarp
{ {
struct llarp_fd_promise* promise = t->t->get_fd_promise(t->t); struct llarp_fd_promise* promise = t->t->get_fd_promise(t->t);
if(promise) if(promise)
return llarp_fd_promise_wait_for_value(promise); {
// get promise
auto p = promise->Get();
// set write fd
t->writefd = p.second;
// return read fd
return p.first;
}
} }
return -1; return -1;
} }

@ -18,11 +18,6 @@
#include <tuntap.h> #include <tuntap.h>
#include <unistd.h> #include <unistd.h>
#ifdef ANDROID
/** TODO: correct this value */
#define SOCK_NONBLOCK (0)
#endif
namespace llarp namespace llarp
{ {
struct udp_listener : public ev_io struct udp_listener : public ev_io
@ -36,22 +31,24 @@ namespace llarp
} }
bool bool
tick(); tick() override;
int int
read(byte_t* buf, size_t sz); read(byte_t* buf, size_t sz) override;
int int
sendto(const sockaddr* to, const void* data, size_t sz); sendto(const sockaddr* to, const void* data, size_t sz) override;
}; };
struct tun : public ev_io struct tun : public ev_io
{ {
llarp_tun_io* t; llarp_tun_io* t;
int writefd;
device* tunif; device* tunif;
tun(llarp_tun_io* tio, llarp_ev_loop_ptr l) tun(llarp_tun_io* tio, llarp_ev_loop_ptr l)
: ev_io(-1, new LossyWriteQueue_t("tun_write_queue", l, l)) : ev_io(-1, new LossyWriteQueue_t("tun_write_queue", l, l))
, t(tio) , t(tio)
, writefd(-1)
, tunif(tuntap_init()) , tunif(tuntap_init())
{ {
@ -59,16 +56,19 @@ namespace llarp
}; };
int int
sendto(const sockaddr* to, const void* data, size_t sz); sendto(const sockaddr* to, const void* data, size_t sz) override;
bool bool
tick(); tick() override;
void void
flush_write(); flush_write() override;
ssize_t
do_write(void* buf, size_t sz) override;
int int
read(byte_t* buf, size_t sz); read(byte_t* buf, size_t sz) override;
static int static int
wait_for_fd_promise(struct device* dev); wait_for_fd_promise(struct device* dev);
@ -84,8 +84,9 @@ namespace llarp
}; };
}; // namespace llarp }; // namespace llarp
struct llarp_epoll_loop : public llarp_ev_loop, struct llarp_epoll_loop
public std::enable_shared_from_this< llarp_ev_loop > : public llarp_ev_loop,
public std::enable_shared_from_this< llarp_epoll_loop >
{ {
int epollfd; int epollfd;

@ -212,7 +212,7 @@ namespace llarp
#ifndef WIN32 #ifndef WIN32
/// handles fd injection force android /// handles fd injection force android
std::promise< int > m_VPNPromise; std::promise< std::pair< int, int > > m_VPNPromise;
#endif #endif
/// our dns resolver /// our dns resolver

@ -49,7 +49,7 @@ if (MSVC)
/D_ENABLE_EXTENDED_ALIGNED_STORAGE /D_ENABLE_EXTENDED_ALIGNED_STORAGE
) )
else() else()
set(ABSL_STD_CXX_FLAG "-std=c++11" CACHE STRING "c++ std flag (default: c++11)") set(ABSL_STD_CXX_FLAG "-std=c++17" CACHE STRING "c++ std flag (default: c++11)")
endif() endif()

@ -23,7 +23,7 @@
#if defined(_WIN32) && defined(_MSC_VER) #if defined(_WIN32) && defined(_MSC_VER)
#include "absl/base/internal/spinlock_win32.inc" #include "absl/base/internal/spinlock_win32.inc"
#elif defined(__linux__) #elif defined(__linux__) && !defined(ANDROID)
#include "absl/base/internal/spinlock_linux.inc" #include "absl/base/internal/spinlock_linux.inc"
#elif defined(__akaros__) #elif defined(__akaros__)
#include "absl/base/internal/spinlock_akaros.inc" #include "absl/base/internal/spinlock_akaros.inc"
@ -63,11 +63,11 @@ int SpinLockSuggestedDelayNS(int loop) {
// Weak pseudo-random number generator to get some spread between threads // Weak pseudo-random number generator to get some spread between threads
// when many are spinning. // when many are spinning.
uint64_t r = delay_rand.load(std::memory_order_relaxed); uint64_t r = delay_rand.load(std::memory_order_relaxed);
r = 0x5deece66dLL * r + 0xb; // numbers from nrand48() r = 0x5deece66dLL * r + 0xb; // numbers from nrand48()
delay_rand.store(r, std::memory_order_relaxed); delay_rand.store(r, std::memory_order_relaxed);
r <<= 16; // 48-bit random number now in top 48-bits. r <<= 16; // 48-bit random number now in top 48-bits.
if (loop < 0 || loop > 32) { // limit loop to 0..32 if (loop < 0 || loop > 32) { // limit loop to 0..32
loop = 32; loop = 32;
} }
// loop>>3 cannot exceed 4 because loop cannot exceed 32. // loop>>3 cannot exceed 4 because loop cannot exceed 32.
@ -80,5 +80,5 @@ int SpinLockSuggestedDelayNS(int loop) {
} }
} // namespace base_internal } // namespace base_internal
} // inline namespace lts_2018_12_18 } // namespace lts_2018_12_18
} // namespace absl } // namespace absl

Loading…
Cancel
Save