Skip to content

Commit

Permalink
Revert "Fixed send hanging by simplifying"
Browse files Browse the repository at this point in the history
This reverts commit 679a22e.
  • Loading branch information
M66B committed May 5, 2019
1 parent 697b7bd commit 0673398
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/src/main/jni/netguard/netguard.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ struct allowed {
struct segment {
uint32_t seq;
uint16_t len;
uint16_t sent;
int psh;
uint8_t *data;
struct segment *next;
Expand Down
37 changes: 21 additions & 16 deletions app/src/main/jni/netguard/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ int monitor_tcp_session(const struct arguments *args, struct ng_session *s, int
// Check for outgoing data
if (s->tcp.forward != NULL) {
uint32_t buffer_size = (uint32_t) get_receive_buffer(s);
if (s->tcp.forward->seq == s->tcp.remote_seq &&
s->tcp.forward->len < buffer_size)
if (s->tcp.forward->seq + s->tcp.forward->sent == s->tcp.remote_seq &&
s->tcp.forward->len - s->tcp.forward->sent < buffer_size)
events = events | EPOLLOUT;
else
recheck = 1;
Expand Down Expand Up @@ -205,7 +205,7 @@ uint32_t get_receive_window(const struct ng_session *cur) {
uint32_t toforward = 0;
struct segment *q = cur->tcp.forward;
while (q != NULL) {
toforward += q->len;
toforward += (q->len - q->sent);
q = q->next;
}

Expand Down Expand Up @@ -459,15 +459,17 @@ void check_tcp_socket(const struct arguments *args,
// Forward data
uint32_t buffer_size = (uint32_t) get_receive_buffer(s);
while (s->tcp.forward != NULL &&
s->tcp.forward->seq == s->tcp.remote_seq &&
s->tcp.forward->len < buffer_size) {
log_android(ANDROID_LOG_DEBUG, "%s fwd %u...%u",
s->tcp.forward->seq + s->tcp.forward->sent == s->tcp.remote_seq &&
s->tcp.forward->len - s->tcp.forward->sent < buffer_size) {
log_android(ANDROID_LOG_DEBUG, "%s fwd %u...%u sent %u",
session,
s->tcp.forward->seq - s->tcp.remote_start,
s->tcp.forward->seq + s->tcp.forward->len - s->tcp.remote_start);
s->tcp.forward->seq + s->tcp.forward->len - s->tcp.remote_start,
s->tcp.forward->sent);

ssize_t sent = send(s->socket,
s->tcp.forward->data, s->tcp.forward->len,
s->tcp.forward->data + s->tcp.forward->sent,
s->tcp.forward->len - s->tcp.forward->sent,
(unsigned int) (MSG_NOSIGNAL | (s->tcp.forward->psh
? 0
: MSG_MORE)));
Expand All @@ -485,18 +487,18 @@ void check_tcp_socket(const struct arguments *args,
fwd = 1;
buffer_size -= sent;
s->tcp.sent += sent;
s->tcp.forward->seq += sent;
s->tcp.forward->len -= sent;
s->tcp.remote_seq = s->tcp.forward->seq;
s->tcp.forward->sent += sent;
s->tcp.remote_seq = s->tcp.forward->seq + s->tcp.forward->sent;

if (s->tcp.forward->len == 0) {
if (s->tcp.forward->len == s->tcp.forward->sent) {
struct segment *p = s->tcp.forward;
s->tcp.forward = s->tcp.forward->next;
free(p->data);
free(p);
} else {
log_android(ANDROID_LOG_WARN, "%s partial send %u/%u",
session, sent, s->tcp.forward->len);
log_android(ANDROID_LOG_WARN,
"%s partial send %u/%u",
session, s->tcp.forward->sent, s->tcp.forward->len);
break;
}
}
Expand All @@ -505,10 +507,11 @@ void check_tcp_socket(const struct arguments *args,
// Log data buffered
struct segment *seg = s->tcp.forward;
while (seg != NULL) {
log_android(ANDROID_LOG_WARN, "%s queued %u...%u",
log_android(ANDROID_LOG_WARN, "%s queued %u...%u sent %u",
session,
seg->seq - s->tcp.remote_start,
seg->seq + seg->len - s->tcp.remote_start);
seg->seq + seg->len - s->tcp.remote_start,
seg->sent);
seg = seg->next;
}
}
Expand Down Expand Up @@ -747,6 +750,7 @@ jboolean handle_tcp(const struct arguments *args,
s->tcp.forward = malloc(sizeof(struct segment));
s->tcp.forward->seq = s->tcp.remote_seq;
s->tcp.forward->len = datalen;
s->tcp.forward->sent = 0;
s->tcp.forward->psh = tcphdr->psh;
s->tcp.forward->data = malloc(datalen);
memcpy(s->tcp.forward->data, data, datalen);
Expand Down Expand Up @@ -981,6 +985,7 @@ void queue_tcp(const struct arguments *args,
struct segment *n = malloc(sizeof(struct segment));
n->seq = seq;
n->len = datalen;
n->sent = 0;
n->psh = tcphdr->psh;
n->data = malloc(datalen);
memcpy(n->data, data, datalen);
Expand Down

0 comments on commit 0673398

Please sign in to comment.