From c066ae612359825df8c71b46227e353b1c78fb75 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 26 Mar 2017 09:02:29 +0200 Subject: [PATCH] Get uid on source port on first try --- app/src/main/jni/netguard/ip.c | 37 ++++++++-------------------- app/src/main/jni/netguard/netguard.h | 7 +++--- 2 files changed, 13 insertions(+), 31 deletions(-) diff --git a/app/src/main/jni/netguard/ip.c b/app/src/main/jni/netguard/ip.c index 247f3917..d046dcac 100644 --- a/app/src/main/jni/netguard/ip.c +++ b/app/src/main/jni/netguard/ip.c @@ -351,11 +351,11 @@ jint get_uid_retry(const int version, const int protocol, daddr128[10] = (uint8_t) 0xFF; daddr128[11] = (uint8_t) 0xFF; memcpy(daddr128 + 12, daddr, 4); - uid = get_uid(6, protocol, saddr, sport, daddr128, dport, tries == UID_MAXTRY); + uid = get_uid(6, protocol, saddr, sport, daddr128, dport); } if (uid < 0) - uid = get_uid(version, protocol, saddr, sport, daddr, dport, tries == UID_MAXTRY); + uid = get_uid(version, protocol, saddr, sport, daddr, dport); // Retry delay if (uid < 0 && tries < UID_MAXTRY) { @@ -374,8 +374,7 @@ jint get_uid_retry(const int version, const int protocol, jint get_uid(const int version, const int protocol, const void *saddr, const uint16_t sport, - const void *daddr, const uint16_t dport, - int lasttry) { + const void *daddr, const uint16_t dport) { char line[250]; char hex[16 * 2 + 1]; int fields; @@ -406,12 +405,6 @@ jint get_uid(const int version, const int protocol, else return uid; - if (lasttry) { - char dest[INET6_ADDRSTRLEN + 1]; - inet_ntop(version == 4 ? AF_INET : AF_INET6, daddr, dest, sizeof(dest)); - log_android(ANDROID_LOG_WARN, "Searching %u > %s/%u in %s", sport, dest, dport, fn); - } - // Open proc file FILE *fd = fopen(fn, "r"); if (fd == NULL) { @@ -439,8 +432,9 @@ jint get_uid(const int version, const int protocol, line, "%*d: %*X:%X %32s:%X %*X %*lX:%*lX %*X:%*X %*X %d %*d %*ld", &_sport, hex, &_dport, &u); + if (fields == 4 && (version == 4 ? strlen(hex) == 8 : strlen(hex) == 32)) { - if (_sport > 0 && _dport > 0 && u >= 0) { + if (_sport > 0 && u >= 0) { hex2bytes(hex, version == 4 ? _daddr4 : _daddr6); if (version == 4) ((uint32_t *) _daddr4)[0] = htonl(((uint32_t *) _daddr4)[0]); @@ -448,23 +442,12 @@ jint get_uid(const int version, const int protocol, for (int w = 0; w < 4; w++) ((uint32_t *) _daddr6)[w] = htonl(((uint32_t *) _daddr6)[w]); - if (lasttry) { - char dest[INET6_ADDRSTRLEN + 1]; - inet_ntop(version == 4 ? AF_INET : AF_INET6, - version == 4 ? _daddr4 : _daddr6, - dest, sizeof(dest)); - log_android(ANDROID_LOG_WARN, "%u > %s/%u %d %s", - _sport, dest, _dport, u, line); - } - - if (_sport == sport && - (lasttry || (_dport == dport && - memcmp(version == 4 - ? _daddr4 - : _daddr6, daddr, - version == 4 ? 4 : 16) == 0))) { + if (_sport == sport) { uid = u; - break; + if (_dport == dport && + memcmp(version == 4 ? _daddr4 : _daddr6, daddr, + version == 4 ? 4 : 16) == 0) + break; } } } diff --git a/app/src/main/jni/netguard/netguard.h b/app/src/main/jni/netguard/netguard.h index b9529926..6faed505 100644 --- a/app/src/main/jni/netguard/netguard.h +++ b/app/src/main/jni/netguard/netguard.h @@ -56,8 +56,8 @@ // https://en.wikipedia.org/wiki/Maximum_segment_lifetime #define UID_DELAY 1 // milliseconds -#define UID_DELAYTRY 10 // milliseconds -#define UID_MAXTRY 3 +#define UID_DELAYTRY 1 // milliseconds +#define UID_MAXTRY 1 #define SOCKS5_NONE 1 #define SOCKS5_HELLO 2 @@ -458,8 +458,7 @@ jint get_uid_retry(const int version, const int protocol, jint get_uid(const int version, const int protocol, const void *saddr, const uint16_t sport, - const void *daddr, const uint16_t dport, - int lasttry); + const void *daddr, const uint16_t dport); int protect_socket(const struct arguments *args, int socket);