From 6c7c66322adc0b1b5b2e6be3e1efa3c515de1f24 Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 24 Jun 2016 15:48:03 +0200 Subject: [PATCH] Optimize session counting Refs #431 --- app/src/main/jni/netguard/icmp.c | 11 ----------- app/src/main/jni/netguard/netguard.c | 22 +++++++++++++++++++--- app/src/main/jni/netguard/netguard.h | 6 ------ app/src/main/jni/netguard/session.c | 22 +++++++++++++++++++--- app/src/main/jni/netguard/tcp.c | 11 ----------- app/src/main/jni/netguard/udp.c | 11 ----------- 6 files changed, 38 insertions(+), 45 deletions(-) diff --git a/app/src/main/jni/netguard/icmp.c b/app/src/main/jni/netguard/icmp.c index 670128b7..2ddd44cc 100644 --- a/app/src/main/jni/netguard/icmp.c +++ b/app/src/main/jni/netguard/icmp.c @@ -22,17 +22,6 @@ extern struct ng_session *ng_session; extern FILE *pcap_file; -int get_icmp_sessions() { - int count = 0; - struct ng_session *s = ng_session; - while (s != NULL) { - if ((s->protocol == IPPROTO_ICMP || s->protocol == IPPROTO_ICMPV6) && !s->icmp.stop) - count++; - s = s->next; - } - return count; -} - int get_icmp_timeout(const struct icmp_session *u, int sessions, int maxsessions) { int timeout = ICMP_TIMEOUT; diff --git a/app/src/main/jni/netguard/netguard.c b/app/src/main/jni/netguard/netguard.c index 0c52baab..f84802fa 100644 --- a/app/src/main/jni/netguard/netguard.c +++ b/app/src/main/jni/netguard/netguard.c @@ -32,6 +32,7 @@ jboolean signaled = 0; int loglevel = ANDROID_LOG_WARN; extern int max_tun_msg; +extern struct ng_session *ng_session; extern FILE *pcap_file; extern size_t pcap_record_size; extern long pcap_file_size; @@ -186,9 +187,24 @@ Java_eu_faircode_netguard_ServiceSinkhole_jni_1get_1stats(JNIEnv *env, jobject i jintArray jarray = (*env)->NewIntArray(env, 5); jint *jcount = (*env)->GetIntArrayElements(env, jarray, NULL); - jcount[0] = get_icmp_sessions(); - jcount[1] = get_udp_sessions(); - jcount[2] = get_tcp_sessions(); + + + struct ng_session *s = ng_session; + while (s != NULL) { + if (s->protocol == IPPROTO_ICMP || s->protocol == IPPROTO_ICMPV6) { + if (!s->icmp.stop) + jcount[0]++; + } + else if (s->protocol == IPPROTO_UDP) { + if (s->udp.state == UDP_ACTIVE) + jcount[1]++; + } + else if (s->protocol == IPPROTO_TCP) { + if (s->tcp.state != TCP_CLOSING && s->tcp.state != TCP_CLOSE) + jcount[2]++; + } + s = s->next; + } if (pthread_mutex_unlock(&lock)) log_android(ANDROID_LOG_ERROR, "pthread_mutex_unlock failed"); diff --git a/app/src/main/jni/netguard/netguard.h b/app/src/main/jni/netguard/netguard.h index c768df88..d7b4db63 100644 --- a/app/src/main/jni/netguard/netguard.h +++ b/app/src/main/jni/netguard/netguard.h @@ -354,16 +354,12 @@ void handle_ip(const struct arguments *args, const int epoll_fd, int sessions, int maxsessions); -int get_icmp_sessions(); - jboolean handle_icmp(const struct arguments *args, const uint8_t *pkt, size_t length, const uint8_t *payload, int uid, const int epoll_fd); -int get_udp_sessions(); - int has_udp_session(const struct arguments *args, const uint8_t *pkt, const uint8_t *payload); void block_udp(const struct arguments *args, @@ -390,8 +386,6 @@ int check_dhcp(const struct arguments *args, const struct udp_session *u, void clear_tcp_data(struct tcp_session *cur); -int get_tcp_sessions(); - jboolean handle_tcp(const struct arguments *args, const uint8_t *pkt, size_t length, const uint8_t *payload, diff --git a/app/src/main/jni/netguard/session.c b/app/src/main/jni/netguard/session.c index b5128202..accc9d95 100644 --- a/app/src/main/jni/netguard/session.c +++ b/app/src/main/jni/netguard/session.c @@ -127,9 +127,25 @@ void *handle_events(void *a) { log_android(ANDROID_LOG_DEBUG, "Loop thread %x", thread_id); // Count sessions - int isessions = get_icmp_sessions(); - int usessions = get_udp_sessions(); - int tsessions = get_tcp_sessions(); + int isessions = 0; + int usessions = 0; + int tsessions = 0; + struct ng_session *s = ng_session; + while (s != NULL) { + if (s->protocol == IPPROTO_ICMP || s->protocol == IPPROTO_ICMPV6) { + if (!s->icmp.stop) + isessions++; + } + else if (s->protocol == IPPROTO_UDP) { + if (s->udp.state == UDP_ACTIVE) + usessions++; + } + else if (s->protocol == IPPROTO_TCP) { + if (s->tcp.state != TCP_CLOSING && s->tcp.state != TCP_CLOSE) + tsessions++; + } + s = s->next; + } int sessions = isessions + usessions + tsessions; // Check sessions diff --git a/app/src/main/jni/netguard/tcp.c b/app/src/main/jni/netguard/tcp.c index 987f0d80..7c696528 100644 --- a/app/src/main/jni/netguard/tcp.c +++ b/app/src/main/jni/netguard/tcp.c @@ -32,17 +32,6 @@ void clear_tcp_data(struct tcp_session *cur) { } } -int get_tcp_sessions() { - int count = 0; - struct ng_session *s = ng_session; - while (s != NULL) { - if (s->protocol == IPPROTO_TCP && s->tcp.state != TCP_CLOSING && s->tcp.state != TCP_CLOSE) - count++; - s = s->next; - } - return count; -} - int get_tcp_timeout(const struct tcp_session *t, int sessions, int maxsessions) { int timeout; if (t->state == TCP_LISTEN || t->state == TCP_SYN_RECV) diff --git a/app/src/main/jni/netguard/udp.c b/app/src/main/jni/netguard/udp.c index 6bf35f26..e7bdffa7 100644 --- a/app/src/main/jni/netguard/udp.c +++ b/app/src/main/jni/netguard/udp.c @@ -22,17 +22,6 @@ extern struct ng_session *ng_session; extern FILE *pcap_file; -int get_udp_sessions() { - int count = 0; - struct ng_session *s = ng_session; - while (s != NULL) { - if (s->protocol == IPPROTO_UDP && s->udp.state == UDP_ACTIVE) - count++; - s = s->next; - } - return count; -} - int get_udp_timeout(const struct udp_session *u, int sessions, int maxsessions) { int timeout = (ntohs(u->dest) == 53 ? UDP_TIMEOUT_53 : UDP_TIMEOUT_ANY);