1
0
Fork 0
mirror of https://github.com/M66B/NetGuard.git synced 2024-12-26 09:48:46 +00:00

Removed local keep alive to save battery

This commit is contained in:
M66B 2016-03-01 11:22:41 +01:00
parent 5402eeed2a
commit 223de55f58
2 changed files with 23 additions and 55 deletions

View file

@ -136,7 +136,6 @@ struct udp_session {
struct tcp_session {
jint uid;
time_t time;
time_t keep_alive;
int version;
uint16_t recv_window; // host notation
uint16_t send_window; // host notation

View file

@ -65,12 +65,9 @@ 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)
timeout = TCP_INIT_TIMEOUT;
else if (t->state == TCP_ESTABLISHED) {
if (t->keep_alive)
timeout = TCP_IDLE_TIMEOUT;
else
timeout = TCP_IDLE_TIMEOUT / 2;
} else
else if (t->state == TCP_ESTABLISHED)
timeout = TCP_IDLE_TIMEOUT;
else
timeout = TCP_CLOSE_TIMEOUT;
int scale = 100 - sessions * 100 / maxsessions;
@ -101,42 +98,30 @@ void check_tcp_sessions(const struct arguments *args, int sessions, int maxsessi
int timeout = get_tcp_timeout(t, sessions, maxsessions);
// Keep alive
if (t->state == TCP_ESTABLISHED && t->keep_alive == 0 && t->time + timeout < now) {
log_android(ANDROID_LOG_WARN, "%s keep alive %d/%d sec ",
session, now - t->time, timeout);
t->keep_alive = time(NULL);
t->local_seq--;
write_ack(args, t);
t->local_seq++;
// Check session timeout
if (t->state != TCP_CLOSING && t->state != TCP_CLOSE && t->time + timeout < now) {
log_android(ANDROID_LOG_WARN, "%s idle %d/%d sec ", session, now - t->time,
timeout);
if (t->state == TCP_LISTEN)
t->state = TCP_CLOSING;
else
write_rst(args, t);
}
else {
// Check session timeout
if (t->state != TCP_CLOSING && t->state != TCP_CLOSE && t->time + timeout < now) {
log_android(ANDROID_LOG_WARN, "%s idle %d/%d sec ", session, now - t->time,
timeout);
if (t->state == TCP_LISTEN)
t->state = TCP_CLOSING;
// Check closing sessions
if (t->state == TCP_CLOSING) {
// eof closes socket
if (t->socket >= 0) {
if (close(t->socket))
log_android(ANDROID_LOG_ERROR, "%s close error %d: %s",
session, errno, strerror(errno));
else
write_rst(args, t);
log_android(ANDROID_LOG_WARN, "%s close", session);
t->socket = -1;
}
// Check closing sessions
if (t->state == TCP_CLOSING) {
// eof closes socket
if (t->socket >= 0) {
if (close(t->socket))
log_android(ANDROID_LOG_ERROR, "%s close error %d: %s",
session, errno, strerror(errno));
else
log_android(ANDROID_LOG_WARN, "%s close", session);
t->socket = -1;
}
t->time = time(NULL);
t->state = TCP_CLOSE;
}
t->time = time(NULL);
t->state = TCP_CLOSE;
}
if ((t->state == TCP_CLOSING || t->state == TCP_CLOSE) && (t->sent || t->received)) {
@ -467,7 +452,6 @@ jboolean handle_tcp(const struct arguments *args,
// Register session
struct tcp_session *syn = malloc(sizeof(struct tcp_session));
syn->time = time(NULL);
syn->keep_alive = 0;
syn->uid = uid;
syn->version = version;
syn->recv_window = TCP_RECV_WINDOW;
@ -571,21 +555,6 @@ jboolean handle_tcp(const struct arguments *args,
cur->time = time(NULL);
cur->send_window = ntohs(tcphdr->window);
// Enable socket keep alive
if (cur->keep_alive) {
cur->keep_alive = 0;
if (cur->state == TCP_ESTABLISHED) {
int on = 1;
if (setsockopt(cur->socket, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)))
log_android(ANDROID_LOG_ERROR,
"%s setsockopt SO_KEEPALIVE error %d: %s",
session, errno, strerror(errno));
else
log_android(ANDROID_LOG_WARN, "%s enabled keep alive", session);
}
}
// Do not change the order of the conditions
// Queue data to forward