Optimize session counting

Refs #431
This commit is contained in:
M66B 2016-06-24 15:48:03 +02:00
parent 11c79001b5
commit 6c7c66322a
6 changed files with 38 additions and 45 deletions

View File

@ -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;

View File

@ -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");

View File

@ -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,

View File

@ -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

View File

@ -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)

View File

@ -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);