From b037fe8727280285224d2531ba1322bd461b6d86 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 14 Feb 2016 14:11:05 +0100 Subject: [PATCH] Native refactoring --- app/src/main/jni/netguard/ip.c | 70 +++++++++++++++------------- app/src/main/jni/netguard/netguard.h | 3 ++ 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/app/src/main/jni/netguard/ip.c b/app/src/main/jni/netguard/ip.c index feb58be9..486db00b 100644 --- a/app/src/main/jni/netguard/ip.c +++ b/app/src/main/jni/netguard/ip.c @@ -281,38 +281,8 @@ void handle_ip(const struct arguments *args, jint uid = -1; if (protocol == IPPROTO_ICMP || protocol == IPPROTO_ICMPV6 || (protocol == IPPROTO_UDP && !has_udp_session(args, pkt, payload)) || - (protocol == IPPROTO_TCP && syn)) { - log_android(ANDROID_LOG_INFO, "get uid %s/%u v%d p%d syn %d", - dest, dport, version, protocol, syn); - int tries = 0; - usleep(1000 * UID_DELAY); - while (uid < 0 && tries++ < UID_MAXTRY) { - // Check IPv6 table first - int dump = (tries == UID_MAXTRY); - if (version == 4) { - int8_t saddr128[16]; - memset(saddr128, 0, 10); - saddr128[10] = (uint8_t) 0xFF; - saddr128[11] = (uint8_t) 0xFF; - memcpy(saddr128 + 12, saddr, 4); - uid = get_uid(protocol, 6, saddr128, sport, dump); - } - - if (uid < 0) - uid = get_uid(protocol, version, saddr, sport, dump); - - // Retry delay - if (uid < 0 && tries < UID_MAXTRY) { - log_android(ANDROID_LOG_WARN, "get uid %s/%u v%d p%d syn %d try %d", - dest, dport, version, protocol, syn, tries); - usleep(1000 * UID_DELAYTRY); - } - } - - if (uid < 0) - log_android(ANDROID_LOG_ERROR, "uid %s/%u v%d p%d syn %d not found", - dest, dport, version, protocol, syn); - } + (protocol == IPPROTO_TCP && syn)) + uid = get_uid_retry(protocol, version, saddr, sport); log_android(ANDROID_LOG_DEBUG, "Packet v%d %s/%u > %s/%u proto %d flags %s uid %d", @@ -367,6 +337,42 @@ void handle_ip(const struct arguments *args, #endif } +jint get_uid_retry(const int protocol, const int version, + const void *saddr, const uint16_t sport) { + log_android(ANDROID_LOG_INFO, "get uid v%d p%d %s/%u", version, protocol, saddr, sport); + + jint uid = -1; + int tries = 0; + usleep(1000 * UID_DELAY); + while (uid < 0 && tries++ < UID_MAXTRY) { + // Check IPv6 table first + if (version == 4) { + int8_t saddr128[16]; + memset(saddr128, 0, 10); + saddr128[10] = (uint8_t) 0xFF; + saddr128[11] = (uint8_t) 0xFF; + memcpy(saddr128 + 12, saddr, 4); + uid = get_uid(protocol, 6, saddr128, sport, tries == UID_MAXTRY); + } + + if (uid < 0) + uid = get_uid(protocol, version, saddr, sport, tries == UID_MAXTRY); + + // Retry delay + if (uid < 0 && tries < UID_MAXTRY) { + log_android(ANDROID_LOG_WARN, "get uid v%d p%d %s/%u try %d", + version, protocol, saddr, sport, tries); + usleep(1000 * UID_DELAYTRY); + } + } + + if (uid < 0) + log_android(ANDROID_LOG_ERROR, "uid v%d p%d %s/%u not found", + version, protocol, saddr, sport); + + return uid; +} + jint get_uid(const int protocol, const int version, const void *saddr, const uint16_t sport, int dump) { char line[250]; diff --git a/app/src/main/jni/netguard/netguard.h b/app/src/main/jni/netguard/netguard.h index f188ec43..b90d6248 100644 --- a/app/src/main/jni/netguard/netguard.h +++ b/app/src/main/jni/netguard/netguard.h @@ -419,6 +419,9 @@ uint8_t char2nible(const char c); void hex2bytes(const char *hex, uint8_t *buffer); +jint get_uid_retry(const int protocol, const int version, + const void *saddr, const uint16_t sport); + jint get_uid(const int protocol, const int version, const void *saddr, const uint16_t sport, int dump);