mirror of https://github.com/M66B/NetGuard.git
parent
4b847acc88
commit
cf3f629b52
|
@ -316,8 +316,6 @@ int check_tcp_session(const struct arguments *args,
|
||||||
struct ng_session *s,
|
struct ng_session *s,
|
||||||
int sessions, int maxsessions);
|
int sessions, int maxsessions);
|
||||||
|
|
||||||
int get_select_timeout(int sessions, int maxsessions);
|
|
||||||
|
|
||||||
int get_icmp_timeout(const struct icmp_session *u, int sessions, int maxsessions);
|
int get_icmp_timeout(const struct icmp_session *u, int sessions, int maxsessions);
|
||||||
|
|
||||||
int get_udp_timeout(const struct udp_session *u, int sessions, int maxsessions);
|
int get_udp_timeout(const struct udp_session *u, int sessions, int maxsessions);
|
||||||
|
|
|
@ -114,7 +114,6 @@ void *handle_events(void *a) {
|
||||||
int tsessions = 0;
|
int tsessions = 0;
|
||||||
struct ng_session *s = ng_session;
|
struct ng_session *s = ng_session;
|
||||||
while (s != NULL) {
|
while (s != NULL) {
|
||||||
int del = 0;
|
|
||||||
if (s->protocol == IPPROTO_ICMP || s->protocol == IPPROTO_ICMPV6) {
|
if (s->protocol == IPPROTO_ICMP || s->protocol == IPPROTO_ICMPV6) {
|
||||||
if (!s->icmp.stop)
|
if (!s->icmp.stop)
|
||||||
isessions++;
|
isessions++;
|
||||||
|
@ -132,16 +131,40 @@ void *handle_events(void *a) {
|
||||||
int sessions = isessions + usessions + tsessions;
|
int sessions = isessions + usessions + tsessions;
|
||||||
|
|
||||||
// Check sessions
|
// Check sessions
|
||||||
|
time_t now = time(NULL);
|
||||||
|
int timeout = SELECT_TIMEOUT;
|
||||||
struct ng_session *sl = NULL;
|
struct ng_session *sl = NULL;
|
||||||
s = ng_session;
|
s = ng_session;
|
||||||
while (s != NULL) {
|
while (s != NULL) {
|
||||||
int del = 0;
|
int del = 0;
|
||||||
if (s->protocol == IPPROTO_ICMP || s->protocol == IPPROTO_ICMPV6)
|
if (s->protocol == IPPROTO_ICMP || s->protocol == IPPROTO_ICMPV6) {
|
||||||
del = check_icmp_session(args, s, sessions, maxsessions);
|
del = check_icmp_session(args, s, sessions, maxsessions);
|
||||||
else if (s->protocol == IPPROTO_UDP)
|
if (!s->icmp.stop) {
|
||||||
|
int stimeout =
|
||||||
|
s->icmp.time + get_icmp_timeout(&s->icmp, sessions, maxsessions) - now +
|
||||||
|
1;
|
||||||
|
if (stimeout > 0 && stimeout < timeout)
|
||||||
|
timeout = stimeout;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (s->protocol == IPPROTO_UDP) {
|
||||||
del = check_udp_session(args, s, sessions, maxsessions);
|
del = check_udp_session(args, s, sessions, maxsessions);
|
||||||
else if (s->protocol == IPPROTO_TCP)
|
if (s->udp.state == UDP_ACTIVE) {
|
||||||
|
int stimeout =
|
||||||
|
s->udp.time + get_udp_timeout(&s->udp, sessions, maxsessions) - now + 1;
|
||||||
|
if (stimeout > 0 && stimeout < timeout)
|
||||||
|
timeout = stimeout;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (s->protocol == IPPROTO_TCP) {
|
||||||
del = check_tcp_session(args, s, sessions, maxsessions);
|
del = check_tcp_session(args, s, sessions, maxsessions);
|
||||||
|
if (s->tcp.state != TCP_CLOSING && s->tcp.state != TCP_CLOSE) {
|
||||||
|
int stimeout =
|
||||||
|
s->tcp.time + get_tcp_timeout(&s->tcp, sessions, maxsessions) - now + 1;
|
||||||
|
if (stimeout > 0 && stimeout < timeout)
|
||||||
|
timeout = stimeout;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (del) {
|
if (del) {
|
||||||
if (sl == NULL)
|
if (sl == NULL)
|
||||||
|
@ -160,11 +183,8 @@ void *handle_events(void *a) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log_android(ANDROID_LOG_DEBUG, "sessions ICMP %d UDP %d TCP %d max %d/%d",
|
log_android(ANDROID_LOG_DEBUG, "sessions ICMP %d UDP %d TCP %d max %d/%d timeout %d s",
|
||||||
isessions, usessions, tsessions, sessions, maxsessions);
|
isessions, usessions, tsessions, sessions, maxsessions, timeout);
|
||||||
|
|
||||||
// Next event time
|
|
||||||
int timeout = get_select_timeout(sessions, maxsessions);
|
|
||||||
|
|
||||||
// Poll
|
// Poll
|
||||||
struct epoll_event ev;
|
struct epoll_event ev;
|
||||||
|
@ -217,8 +237,8 @@ void *handle_events(void *a) {
|
||||||
gettimeofday(&start, NULL);
|
gettimeofday(&start, NULL);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Check upstream
|
|
||||||
int error = 0;
|
int error = 0;
|
||||||
|
|
||||||
if (ev.data.ptr == &ev_pipe) {
|
if (ev.data.ptr == &ev_pipe) {
|
||||||
stopping = 1;
|
stopping = 1;
|
||||||
uint8_t buffer[1];
|
uint8_t buffer[1];
|
||||||
|
@ -228,6 +248,7 @@ void *handle_events(void *a) {
|
||||||
log_android(ANDROID_LOG_WARN, "Read pipe");
|
log_android(ANDROID_LOG_WARN, "Read pipe");
|
||||||
|
|
||||||
} else if (ev.data.ptr == NULL) {
|
} else if (ev.data.ptr == NULL) {
|
||||||
|
// Check upstream
|
||||||
if (check_tun(args, &ev, epoll_fd, sessions, maxsessions) < 0)
|
if (check_tun(args, &ev, epoll_fd, sessions, maxsessions) < 0)
|
||||||
error = 1;
|
error = 1;
|
||||||
|
|
||||||
|
@ -288,44 +309,6 @@ void *handle_events(void *a) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_select_timeout(int sessions, int maxsessions) {
|
|
||||||
time_t now = time(NULL);
|
|
||||||
int timeout = SELECT_TIMEOUT;
|
|
||||||
|
|
||||||
struct ng_session *s = ng_session;
|
|
||||||
while (s != NULL) {
|
|
||||||
if (s->protocol == IPPROTO_ICMP || s->protocol == IPPROTO_ICMPV6) {
|
|
||||||
if (!s->icmp.stop) {
|
|
||||||
int stimeout =
|
|
||||||
s->icmp.time + get_icmp_timeout(&s->icmp, sessions, maxsessions) - now + 1;
|
|
||||||
if (stimeout > 0 && stimeout < timeout)
|
|
||||||
timeout = stimeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (s->protocol == IPPROTO_UDP) {
|
|
||||||
if (s->udp.state == UDP_ACTIVE) {
|
|
||||||
int stimeout =
|
|
||||||
s->udp.time + get_udp_timeout(&s->udp, sessions, maxsessions) - now + 1;
|
|
||||||
if (stimeout > 0 && stimeout < timeout)
|
|
||||||
timeout = stimeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (s->protocol == IPPROTO_TCP) {
|
|
||||||
if (s->tcp.state != TCP_CLOSING && s->tcp.state != TCP_CLOSE) {
|
|
||||||
int stimeout =
|
|
||||||
s->tcp.time + get_tcp_timeout(&s->tcp, sessions, maxsessions) - now + 1;
|
|
||||||
if (stimeout > 0 && stimeout < timeout)
|
|
||||||
timeout = stimeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
s = s->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
return timeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
void check_allowed(const struct arguments *args) {
|
void check_allowed(const struct arguments *args) {
|
||||||
char source[INET6_ADDRSTRLEN + 1];
|
char source[INET6_ADDRSTRLEN + 1];
|
||||||
char dest[INET6_ADDRSTRLEN + 1];
|
char dest[INET6_ADDRSTRLEN + 1];
|
||||||
|
|
Loading…
Reference in New Issue