mirror of https://github.com/M66B/NetGuard.git
Native refactoring
This commit is contained in:
parent
e0e7d33049
commit
d21d75edaf
|
@ -98,6 +98,7 @@
|
|||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/objectFiles" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/proguard-rules" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
|
||||
|
|
|
@ -33,6 +33,7 @@ static JavaVM *jvm;
|
|||
pthread_t thread_id;
|
||||
int signaled = 0;
|
||||
struct session *session = NULL;
|
||||
|
||||
int loglevel = ANDROID_LOG_WARN;
|
||||
char *pcap_fn = NULL;
|
||||
|
||||
|
@ -133,16 +134,7 @@ void sig_handler(int sig, siginfo_t *info, void *context) {
|
|||
signaled = 1;
|
||||
}
|
||||
|
||||
void *handle_events(void *a) {
|
||||
struct arguments *args = (struct arguments *) a;
|
||||
ng_log(ANDROID_LOG_INFO, "Start events tun=%d thread %ld", args->tun, thread_id);
|
||||
|
||||
JNIEnv *env;
|
||||
jint rs = (*jvm)->AttachCurrentThread(jvm, &env, NULL);
|
||||
if (rs != JNI_OK)
|
||||
ng_log(ANDROID_LOG_ERROR, "AttachCurrentThread failed");
|
||||
|
||||
int max;
|
||||
void handle_events(void *a) {
|
||||
fd_set rfds;
|
||||
fd_set wfds;
|
||||
fd_set efds;
|
||||
|
@ -152,6 +144,18 @@ void *handle_events(void *a) {
|
|||
sigset_t emptyset;
|
||||
struct sigaction sa;
|
||||
|
||||
struct arguments *args = (struct arguments *) a;
|
||||
ng_log(ANDROID_LOG_INFO, "Start events tun=%d thread %ld", args->tun, thread_id);
|
||||
|
||||
// Attach to Java
|
||||
JNIEnv *env;
|
||||
jint rs = (*jvm)->AttachCurrentThread(jvm, &env, NULL);
|
||||
if (rs != JNI_OK) {
|
||||
ng_log(ANDROID_LOG_ERROR, "AttachCurrentThread failed");
|
||||
return;
|
||||
}
|
||||
args->env = env;
|
||||
|
||||
// Block SIGUSR1
|
||||
sigemptyset(&blockset);
|
||||
sigaddset(&blockset, SIGUSR1);
|
||||
|
@ -167,19 +171,75 @@ void *handle_events(void *a) {
|
|||
|
||||
// Loop
|
||||
while (1) {
|
||||
time_t now = time(NULL);
|
||||
ng_log(ANDROID_LOG_DEBUG, "Loop thread %ld", thread_id);
|
||||
|
||||
// Select
|
||||
FD_ZERO(&rfds);
|
||||
FD_ZERO(&wfds);
|
||||
FD_ZERO(&efds);
|
||||
ts.tv_sec = SELECTWAIT;
|
||||
ts.tv_nsec = 0;
|
||||
// TODO let timeout depend on session timeouts
|
||||
sigemptyset(&emptyset);
|
||||
int max = get_selects(args, &rfds, &wfds, &efds);
|
||||
int ready = pselect(max + 1, &rfds, &wfds, &efds, session == NULL ? NULL : &ts, &emptyset);
|
||||
if (ready < 0) {
|
||||
if (errno == EINTR) {
|
||||
if (signaled) { ;
|
||||
ng_log(ANDROID_LOG_DEBUG, "pselect signaled");
|
||||
break;
|
||||
} else {
|
||||
ng_log(ANDROID_LOG_WARN, "pselect interrupted");
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
ng_log(ANDROID_LOG_ERROR, "pselect error %d: %s",
|
||||
errno, strerror(errno));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Count sessions
|
||||
int sessions = 0;
|
||||
struct session *s = session;
|
||||
while (s != NULL) {
|
||||
sessions++;
|
||||
s = s->next;
|
||||
}
|
||||
|
||||
if (ready == 0)
|
||||
ng_log(ANDROID_LOG_DEBUG, "pselect timeout sessions %d", sessions);
|
||||
else {
|
||||
ng_log(ANDROID_LOG_DEBUG, "pselect sessions %d ready %d", sessions, ready);
|
||||
|
||||
// Check upstream
|
||||
check_tun(args, &rfds, &wfds, &efds);
|
||||
|
||||
// Check downstream
|
||||
check_sockets(args, &rfds, &wfds, &efds);
|
||||
}
|
||||
}
|
||||
|
||||
(*env)->DeleteGlobalRef(env, args->instance);
|
||||
rs = (*jvm)->DetachCurrentThread(jvm);
|
||||
if (rs != JNI_OK)
|
||||
ng_log(ANDROID_LOG_ERROR, "DetachCurrentThread failed");
|
||||
free(args);
|
||||
|
||||
ng_log(ANDROID_LOG_INFO, "Stopped events tun=%d thread %ld", args->tun, thread_id);
|
||||
// TODO conditionally report to Java
|
||||
}
|
||||
|
||||
int get_selects(const struct arguments *args, fd_set *rfds, fd_set *wfds, fd_set *efds) {
|
||||
time_t now = time(NULL);
|
||||
|
||||
// Select
|
||||
FD_ZERO(rfds);
|
||||
FD_ZERO(wfds);
|
||||
FD_ZERO(efds);
|
||||
|
||||
// Always read tun
|
||||
FD_SET(args->tun, &rfds);
|
||||
FD_SET(args->tun, &efds);
|
||||
FD_SET(args->tun, rfds);
|
||||
FD_SET(args->tun, efds);
|
||||
|
||||
max = args->tun;
|
||||
int max = args->tun;
|
||||
|
||||
struct session *last = NULL;
|
||||
struct session *cur = session;
|
||||
|
@ -238,8 +298,8 @@ void *handle_events(void *a) {
|
|||
|
||||
} else if (cur->state != TCP_TIME_WAIT) {
|
||||
if (cur->state == TCP_LISTEN) {
|
||||
FD_SET(cur->socket, &efds);
|
||||
FD_SET(cur->socket, &wfds);
|
||||
FD_SET(cur->socket, efds);
|
||||
FD_SET(cur->socket, wfds);
|
||||
if (cur->socket > max)
|
||||
max = cur->socket;
|
||||
}
|
||||
|
@ -247,8 +307,8 @@ void *handle_events(void *a) {
|
|||
cur->state == TCP_SYN_RECV ||
|
||||
cur->state == TCP_CLOSE_WAIT) {
|
||||
// Check for errors / data
|
||||
FD_SET(cur->socket, &efds);
|
||||
FD_SET(cur->socket, &rfds);
|
||||
FD_SET(cur->socket, efds);
|
||||
FD_SET(cur->socket, rfds);
|
||||
if (cur->socket > max)
|
||||
max = cur->socket;
|
||||
}
|
||||
|
@ -257,56 +317,26 @@ void *handle_events(void *a) {
|
|||
last = cur;
|
||||
cur = cur->next;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
ts.tv_sec = SELECTWAIT;
|
||||
ts.tv_nsec = 0;
|
||||
// TODO let timeout depend on session timeouts
|
||||
sigemptyset(&emptyset);
|
||||
int ready = pselect(max + 1, &rfds, &wfds, &efds, session == NULL ? NULL : &ts, &emptyset);
|
||||
if (ready < 0) {
|
||||
if (errno == EINTR) {
|
||||
if (signaled) { ;
|
||||
ng_log(ANDROID_LOG_DEBUG, "pselect signaled");
|
||||
break;
|
||||
} else {
|
||||
ng_log(ANDROID_LOG_WARN, "pselect interrupted");
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
ng_log(ANDROID_LOG_ERROR, "pselect error %d: %s",
|
||||
errno, strerror(errno));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int sessions = 0;
|
||||
struct session *s = session;
|
||||
while (s != NULL) {
|
||||
sessions++;
|
||||
s = s->next;
|
||||
}
|
||||
|
||||
if (ready == 0)
|
||||
ng_log(ANDROID_LOG_DEBUG, "pselect timeout sessions %d", sessions);
|
||||
else {
|
||||
ng_log(ANDROID_LOG_DEBUG, "pselect sessions %d ready %d", sessions, ready);
|
||||
|
||||
int check_tun(const struct arguments *args, fd_set *rfds, fd_set *wfds, fd_set *efds) {
|
||||
// Check tun exception
|
||||
if (FD_ISSET(args->tun, &efds)) {
|
||||
if (FD_ISSET(args->tun, efds)) {
|
||||
ng_log(ANDROID_LOG_ERROR, "tun exception");
|
||||
break; // over and out
|
||||
return -1; // over and out
|
||||
}
|
||||
|
||||
// Check tun read
|
||||
if (FD_ISSET(args->tun, &rfds)) {
|
||||
if (FD_ISSET(args->tun, rfds)) {
|
||||
uint8_t buffer[MAXPKT];
|
||||
ssize_t length = read(args->tun, buffer, MAXPKT);
|
||||
if (length < 0) {
|
||||
ng_log(ANDROID_LOG_ERROR, "tun read error %d: %s", errno, strerror(errno));
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
return 0;
|
||||
else
|
||||
break; // over and out
|
||||
return -1; // over and out
|
||||
}
|
||||
else if (length > 0) {
|
||||
// Write pcap record
|
||||
|
@ -333,27 +363,14 @@ void *handle_events(void *a) {
|
|||
}
|
||||
|
||||
// Handle IP from tun
|
||||
handle_ip(env, args->instance, args, buffer, length);
|
||||
handle_ip(args, buffer, length);
|
||||
}
|
||||
else {
|
||||
ng_log(ANDROID_LOG_ERROR, "tun empty read");
|
||||
break; // over and out
|
||||
return -1; // over and out
|
||||
}
|
||||
}
|
||||
|
||||
// Check sockets
|
||||
check_sockets(args, &rfds, &wfds, &efds);
|
||||
}
|
||||
}
|
||||
|
||||
(*env)->DeleteGlobalRef(env, args->instance);
|
||||
rs = (*jvm)->DetachCurrentThread(jvm);
|
||||
if (rs != JNI_OK)
|
||||
ng_log(ANDROID_LOG_ERROR, "DetachCurrentThread failed");
|
||||
free(args);
|
||||
|
||||
ng_log(ANDROID_LOG_INFO, "Stopped events tun=%d thread %ld", args->tun, thread_id);
|
||||
// TODO conditionally report to Java
|
||||
return 0;
|
||||
}
|
||||
|
||||
void check_sockets(const struct arguments *args, fd_set *rfds, fd_set *wfds, fd_set *efds) {
|
||||
|
@ -469,8 +486,7 @@ void check_sockets(const struct arguments *args, fd_set *rfds, fd_set *wfds, fd_
|
|||
}
|
||||
}
|
||||
|
||||
void handle_ip(JNIEnv *env, jobject instance, const struct arguments *args,
|
||||
const uint8_t *buffer, const uint16_t length) {
|
||||
void handle_ip(const struct arguments *args, const uint8_t *buffer, const uint16_t length) {
|
||||
uint8_t protocol;
|
||||
void *saddr;
|
||||
void *daddr;
|
||||
|
@ -592,6 +608,8 @@ void handle_ip(JNIEnv *env, jobject instance, const struct arguments *args,
|
|||
|
||||
// Call back
|
||||
//if ((protocol == IPPROTO_TCP && syn) || protocol == IPPROTO_UDP) {
|
||||
JNIEnv *env = args->env;
|
||||
jobject instance = args->instance;
|
||||
if (protocol == IPPROTO_TCP || protocol == IPPROTO_UDP) {
|
||||
jclass cls = (*env)->GetObjectClass(env, instance);
|
||||
jmethodID mid = (*env)->GetMethodID(
|
||||
|
@ -625,8 +643,7 @@ void handle_ip(JNIEnv *env, jobject instance, const struct arguments *args,
|
|||
}
|
||||
}
|
||||
|
||||
void handle_tcp(JNIEnv *env, jobject instance, const struct arguments *args,
|
||||
const uint8_t *buffer, uint16_t length, int uid) {
|
||||
void handle_tcp(const struct arguments *args, const uint8_t *buffer, uint16_t length, int uid) {
|
||||
// Check version
|
||||
uint8_t version = (*buffer) >> 4;
|
||||
if (version != 4)
|
||||
|
@ -690,7 +707,7 @@ void handle_tcp(JNIEnv *env, jobject instance, const struct arguments *args,
|
|||
daddr.sin_addr.s_addr = iphdr->daddr;
|
||||
|
||||
// Open socket
|
||||
syn->socket = open_socket(env, instance, &daddr);
|
||||
syn->socket = open_socket(args, &daddr);
|
||||
if (syn->socket < 0) {
|
||||
syn->state = TCP_TIME_WAIT;
|
||||
// Remote will retry
|
||||
|
@ -904,7 +921,7 @@ void handle_tcp(JNIEnv *env, jobject instance, const struct arguments *args,
|
|||
}
|
||||
}
|
||||
|
||||
int open_socket(JNIEnv *env, jobject instance, const struct sockaddr_in *daddr) {
|
||||
int open_socket(const struct arguments *args, const struct sockaddr_in *daddr) {
|
||||
int sock = -1;
|
||||
|
||||
// Get TCP socket
|
||||
|
@ -915,6 +932,8 @@ int open_socket(JNIEnv *env, jobject instance, const struct sockaddr_in *daddr)
|
|||
}
|
||||
|
||||
// Protect
|
||||
JNIEnv *env = args->env;
|
||||
jobject instance = args->instance;
|
||||
jclass cls = (*env)->GetObjectClass(env, instance);
|
||||
jmethodID mid = (*env)->GetMethodID(env, cls, "protect", "(I)Z");
|
||||
if (mid == 0) {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#include <jni.h>
|
||||
|
||||
#define TAG "NetGuard.JNI"
|
||||
#define MAXPKT 32768
|
||||
// TODO TCP parameters (net.inet.tcp.keepinit, etc)
|
||||
|
@ -10,6 +12,7 @@
|
|||
#define MAXPCAP 80
|
||||
|
||||
struct arguments {
|
||||
JNIEnv *env;
|
||||
jobject instance;
|
||||
int tun;
|
||||
};
|
||||
|
@ -57,16 +60,19 @@ typedef struct pcaprec_hdr_s {
|
|||
|
||||
#define LINKTYPE_RAW 101
|
||||
|
||||
void *handle_events(void *);
|
||||
void handle_events(void *);
|
||||
|
||||
int get_selects(const struct arguments *args, fd_set *rfds, fd_set *wfds, fd_set *efds);
|
||||
|
||||
int check_tun(const struct arguments *, fd_set *, fd_set *, fd_set *);
|
||||
|
||||
void check_sockets(const struct arguments *, fd_set *, fd_set *, fd_set *);
|
||||
|
||||
void handle_ip(JNIEnv *, jobject, const struct arguments *, const uint8_t *, const uint16_t);
|
||||
void handle_ip(const struct arguments *, const uint8_t *, const uint16_t);
|
||||
|
||||
void handle_tcp(JNIEnv *, jobject, const struct arguments *args,
|
||||
const uint8_t *, const uint16_t, int uid);
|
||||
void handle_tcp(const struct arguments *, const uint8_t *, const uint16_t, int uid);
|
||||
|
||||
int open_socket(JNIEnv *, jobject, const struct sockaddr_in *);
|
||||
int open_socket(const struct arguments *, const struct sockaddr_in *);
|
||||
|
||||
int get_local_port(const int);
|
||||
|
||||
|
|
Loading…
Reference in New Issue