Native report select exit to Java

This commit is contained in:
M66B 2016-01-21 12:55:08 +01:00
parent 2d8ae4735d
commit c3f0aac602
4 changed files with 46 additions and 11 deletions

View File

@ -25,6 +25,7 @@
#JNI callback
-keep class eu.faircode.netguard.SinkholeService {
void selectExit(boolean);
void logPacket(long, int, java.lang.String, int, int, java.lang.String, int, boolean);
}

View File

@ -771,6 +771,15 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
}
}
// Called from native code
private void selectExit(boolean planned) {
Log.w(TAG, "Select exit planned=" + planned);
if (!planned) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putBoolean("enabled", false).apply();
}
}
// Called from native code
private void logPacket(
long time,

View File

@ -39,14 +39,13 @@
// TODO TCP options
// TODO TCP fragmentation
// TODO TCP push
// TODO TCP window
// TODO TCPv6
// TODO UDPv6
// TODO fix warnings
// TODO non blocking send/write/close, handle EAGAIN/EWOULDBLOCK
// Window size < 2^31: x <= y: (uint32_t)(y-x) < 0x80000000
// It is assumed that no packets will get lost and that packets arrive in order
// http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/index.html
// Global variables
@ -258,8 +257,7 @@ void handle_events(void *a) {
continue;
}
} else {
log_android(ANDROID_LOG_ERROR, "pselect error %d: %s",
errno, strerror(errno));
log_android(ANDROID_LOG_ERROR, "pselect error %d: %s", errno, strerror(errno));
break;
}
}
@ -320,16 +318,42 @@ void handle_events(void *a) {
}
}
free(args->uid);
free(args);
// Report exit to Java
report_exit(args);
(*env)->DeleteGlobalRef(env, args->instance);
// Detach from Java
rs = (*jvm)->DetachCurrentThread(jvm);
if (rs != JNI_OK)
log_android(ANDROID_LOG_ERROR, "DetachCurrentThread failed");
// Cleanup
free(args->uid);
free(args);
log_android(ANDROID_LOG_INFO, "Stopped events tun=%d thread %lu", args->tun, thread_id);
// TODO report exit to Java
}
void report_exit(struct arguments *args) {
JNIEnv *env = args->env;
jclass cls = (*env)->GetObjectClass(env, args->instance);
jmethodID mid = (*env)->GetMethodID(env, cls, "selectExit", "(Z)V");
if (mid == 0)
log_android(ANDROID_LOG_ERROR, "selectExit method not found");
else {
jboolean planned = signaled;
(*env)->CallVoidMethod(env, args->instance, mid, planned);
jthrowable ex = (*env)->ExceptionOccurred(env);
if (ex) {
log_android(ANDROID_LOG_ERROR, "selectExit exception");
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
(*env)->DeleteLocalRef(env, ex);
}
}
(*env)->DeleteLocalRef(env, cls);
}
void check_sessions(const struct arguments *args) {
@ -599,7 +623,6 @@ void check_tcp_sockets(const struct arguments *args, fd_set *rfds, fd_set *wfds,
if (FD_ISSET(cur->socket, rfds)) {
cur->time = time(NULL);
// TODO window size
uint8_t buffer[MAX_DATASIZE4];
ssize_t bytes = recv(cur->socket, buffer, sizeof(buffer), 0);
if (bytes < 0) {
@ -926,10 +949,8 @@ jboolean handle_tcp(const struct arguments *args, const uint8_t *buffer, uint16_
uint8_t ipoptlen = (iphdr->ihl - 5) * 4;
struct tcphdr *tcphdr = buffer + sizeof(struct iphdr) + ipoptlen;
uint8_t tcpoptlen = (tcphdr->doff - 5) * 4;
if (tcpoptlen) {
// TODO handle TCP options
if (tcpoptlen)
log_android(ANDROID_LOG_DEBUG, "optlen %d", tcpoptlen);
}
// Get data
uint16_t dataoff = sizeof(struct iphdr) + ipoptlen + sizeof(struct tcphdr) + tcpoptlen;
@ -1641,6 +1662,8 @@ int protect_socket(struct arguments *args, int socket) {
return -1;
}
(*env)->DeleteLocalRef(env, cls);
return 0;
}

View File

@ -90,6 +90,8 @@ void handle_signal(int sig, siginfo_t *info, void *context);
void handle_events(void *a);
void report_exit(struct arguments *args);
void check_sessions(const struct arguments *args);
int get_selects(const struct arguments *args, fd_set *rfds, fd_set *wfds, fd_set *efds);