From b59d1a1e3ec5bf7573160a519363b713a492ffc0 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 6 Feb 2016 09:41:55 +0100 Subject: [PATCH] Native check if tun is writable --- app/src/main/jni/netguard/netguard.c | 16 ++++++++++++++-- app/src/main/jni/netguard/netguard.h | 1 - 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/src/main/jni/netguard/netguard.c b/app/src/main/jni/netguard/netguard.c index f9cc1e0a..836ae5dc 100644 --- a/app/src/main/jni/netguard/netguard.c +++ b/app/src/main/jni/netguard/netguard.c @@ -350,10 +350,21 @@ void *handle_events(void *a) { int idle = (sessions == 0 && sdk >= 16); log_android(ANDROID_LOG_DEBUG, "sessions %d idle %d sdk %d", sessions, idle, sdk); - // Select ts.tv_sec = SELECT_TIMEOUT; ts.tv_nsec = 0; sigemptyset(&emptyset); + + // Check if tun is writable + FD_ZERO(&rfds); + FD_ZERO(&wfds); + FD_ZERO(&efds); + FD_SET(args->tun, &wfds); + if (pselect(args->tun + 1, &rfds, &wfds, &efds, &ts, &emptyset) == 0) { + log_android(ANDROID_LOG_WARN, "tun not writable"); + continue; + } + + // Select int max = get_selects(args, &rfds, &wfds, &efds); int ready = pselect(max + 1, &rfds, &wfds, &efds, idle ? NULL : &ts, &emptyset); @@ -822,6 +833,7 @@ int check_tun(const struct arguments *args, fd_set *rfds, fd_set *wfds, fd_set * log_android(ANDROID_LOG_ERROR, "tun read error %d: %s", errno, strerror(errno)); if (errno == EINTR || errno == EAGAIN) + // Retry later return 0; else { report_exit(args, "tun read error %d: %s", errno, strerror(errno)); @@ -1492,7 +1504,7 @@ void handle_ip(const struct arguments *args, const uint8_t *pkt, const size_t le saddr128[10] = (uint8_t) 0xFF; saddr128[11] = (uint8_t) 0xFF; memcpy(saddr128 + 12, saddr, 4); - uid = get_uid(protocol, 6, saddr128, sport, dump); + uid = get_uid(protocol, 6, saddr128, sport, dump); } if (uid < 0) diff --git a/app/src/main/jni/netguard/netguard.h b/app/src/main/jni/netguard/netguard.h index dbe8d641..68c7bd91 100644 --- a/app/src/main/jni/netguard/netguard.h +++ b/app/src/main/jni/netguard/netguard.h @@ -124,7 +124,6 @@ struct tcp_session { uint8_t state; jint socket; - struct segment *forward; struct tcp_session *next;