Native pause on idle

This commit is contained in:
M66B 2016-01-16 19:31:44 +01:00
parent 83db13a773
commit 81e21c71c5
1 changed files with 18 additions and 16 deletions

View File

@ -185,7 +185,7 @@ void *handle_events(void *a) {
// Loop // Loop
while (1) { while (1) {
time_t now = time(NULL); time_t now = time(NULL);
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Select thread %u", thread_id); __android_log_print(ANDROID_LOG_DEBUG, TAG, "Loop thread %u", thread_id);
// Select // Select
FD_ZERO(&rfds); FD_ZERO(&rfds);
@ -273,7 +273,7 @@ void *handle_events(void *a) {
ts.tv_sec = SELECTWAIT; ts.tv_sec = SELECTWAIT;
ts.tv_nsec = 0; ts.tv_nsec = 0;
sigemptyset(&emptyset); sigemptyset(&emptyset);
int ready = pselect(max + 1, &rfds, &wfds, &efds, &ts, &emptyset); int ready = pselect(max + 1, &rfds, &wfds, &efds, session == NULL ? NULL : &ts, &emptyset);
if (ready < 0) { if (ready < 0) {
if (errno == EINTR) { if (errno == EINTR) {
if (signaled) { ; if (signaled) { ;
@ -284,14 +284,14 @@ void *handle_events(void *a) {
continue; continue;
} }
} else { } else {
__android_log_print(ANDROID_LOG_ERROR, TAG, "select error %d: %s", __android_log_print(ANDROID_LOG_ERROR, TAG, "pselect error %d: %s",
errno, strerror(errno)); errno, strerror(errno));
break; break;
} }
} }
if (ready == 0) if (ready == 0)
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Yield"); __android_log_print(ANDROID_LOG_DEBUG, TAG, "pselect timeout");
else { else {
// Check tun exception // Check tun exception
if (FD_ISSET(args->tun, &efds)) { if (FD_ISSET(args->tun, &efds)) {
@ -365,8 +365,8 @@ void *handle_events(void *a) {
cur = cur->next; cur = cur->next;
continue; continue;
} else { } else {
cur->local_seq++; cur->local_seq++; // local SYN
cur->remote_seq++; cur->remote_seq++; // remote SYN
cur->state = TCP_SYN_RECV; cur->state = TCP_SYN_RECV;
} }
} }
@ -403,7 +403,7 @@ void *handle_events(void *a) {
else { else {
__android_log_print(ANDROID_LOG_DEBUG, TAG, __android_log_print(ANDROID_LOG_DEBUG, TAG,
"Half close initiated"); "Half close initiated");
cur->local_seq++; cur->local_seq++; // local FIN
if (cur->state == TCP_SYN_RECV || cur->state == TCP_ESTABLISHED) if (cur->state == TCP_SYN_RECV || cur->state == TCP_ESTABLISHED)
cur->state = TCP_FIN_WAIT1; cur->state = TCP_FIN_WAIT1;
else // close wait else // close wait
@ -419,7 +419,7 @@ void *handle_events(void *a) {
"write ACK lport %u error %d: %s", "write ACK lport %u error %d: %s",
cur->lport, errno, strerror((errno))); cur->lport, errno, strerror((errno)));
else else
cur->local_seq += bytes; cur->local_seq += bytes; // received from socket
} }
} }
} }
@ -721,7 +721,7 @@ void handle_tcp(JNIEnv *env, jobject instance, const struct arguments *args,
cur->state = TCP_ESTABLISHED; cur->state = TCP_ESTABLISHED;
} }
else if (cur->state == TCP_ESTABLISHED) { else if (cur->state == TCP_ESTABLISHED) {
__android_log_print(ANDROID_LOG_DEBUG, TAG, "New ack"); __android_log_print(ANDROID_LOG_DEBUG, TAG, "New ack data %u", datalen);
if (datalen) { if (datalen) {
__android_log_print(ANDROID_LOG_DEBUG, TAG, "send socket data %u", __android_log_print(ANDROID_LOG_DEBUG, TAG, "send socket data %u",
datalen); datalen);
@ -737,7 +737,7 @@ void handle_tcp(JNIEnv *env, jobject instance, const struct arguments *args,
"write data error %d: %s", "write data error %d: %s",
errno, strerror((errno))); errno, strerror((errno)));
else else
cur->remote_seq += datalen; cur->remote_seq += datalen; // received from tun
} }
} }
} }
@ -788,14 +788,16 @@ void handle_tcp(JNIEnv *env, jobject instance, const struct arguments *args,
"write ACK error %d: %s", "write ACK error %d: %s",
errno, strerror((errno))); errno, strerror((errno)));
else { else {
cur->remote_seq += 1 + datalen; // FIN + data cur->remote_seq += 1 + datalen; // FIN + received from tun
// TODO check ACK !TCP_FIN_WAIT1
if (cur->state == TCP_ESTABLISHED) if (cur->state == TCP_ESTABLISHED)
cur->state = TCP_CLOSE_WAIT; cur->state = TCP_CLOSE_WAIT;
else if (cur->state == TCP_FIN_WAIT1) if (tcphdr->ack) else if (cur->state == TCP_FIN_WAIT1) {
cur->state = TCP_TIME_WAIT; if (tcphdr->ack)
else cur->state = TCP_TIME_WAIT;
cur->state = TCP_CLOSING; else
else if (cur->state == TCP_FIN_WAIT2) cur->state = TCP_CLOSING;
} else if (cur->state == TCP_FIN_WAIT2)
cur->state = TCP_TIME_WAIT; cur->state = TCP_TIME_WAIT;
else else
__android_log_print(ANDROID_LOG_ERROR, TAG, "Invalid FIN"); __android_log_print(ANDROID_LOG_ERROR, TAG, "Invalid FIN");