X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Futcp.c;h=20dd0aba049757ca30833779ec772c63ba685310;hb=d1b43b5060795728b95b4ed3311b3398771cf34e;hp=acd8188bc3dca4e605465648ae108e70ee7b7b2a;hpb=6ba5501ace0b03e1573e4ec2a3fee1491f64bfe2;p=meshlink diff --git a/src/utcp.c b/src/utcp.c index acd8188b..20dd0aba 100644 --- a/src/utcp.c +++ b/src/utcp.c @@ -650,6 +650,7 @@ void utcp_accept(struct utcp_connection *c, utcp_recv_t recv, void *priv) { debug(c, "accepted %p %p\n", c, recv, priv); c->recv = recv; c->priv = priv; + c->do_poll = true; set_state(c, ESTABLISHED); } @@ -1292,7 +1293,7 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) { if(hdr.ctl & SYN && !(hdr.ctl & ACK) && utcp->accept) { // If we don't want to accept it, send a RST back - if((utcp->pre_accept && !utcp->pre_accept(utcp, hdr.dst))) { + if((utcp->listen && !utcp->listen(utcp, hdr.dst))) { len = 1; goto reset; } @@ -1367,7 +1368,7 @@ synack: if(c->state == CLOSED) { debug(c, "got packet for closed connection\n"); - return 0; + goto reset; } // It is for an existing connection. @@ -1455,17 +1456,6 @@ synack: } } - if(hdr.ctl & ACK && (seqdiff(hdr.ack, c->snd.last) > 0 || seqdiff(hdr.ack, c->snd.una) < 0)) { - debug(c, "packet ack seqno out of range, %u <= %u < %u\n", c->snd.una, hdr.ack, c->snd.una + c->sndbuf.used); - - // Ignore unacceptable RST packets. - if(hdr.ctl & RST) { - return 0; - } - - goto reset; - } - // 2. Handle RST packets if(hdr.ctl & RST) { @@ -1555,6 +1545,11 @@ synack: // 3. Advance snd.una + if(seqdiff(hdr.ack, c->snd.last) > 0 || seqdiff(hdr.ack, c->snd.una) < 0) { + debug(c, "packet ack seqno out of range, %u <= %u < %u\n", c->snd.una, hdr.ack, c->snd.una + c->sndbuf.used); + goto reset; + } + advanced = seqdiff(hdr.ack, c->snd.una); if(advanced) { @@ -1718,6 +1713,7 @@ skip_ack: c->snd.last++; set_state(c, FIN_WAIT_1); } else { + c->do_poll = true; set_state(c, ESTABLISHED); } @@ -1776,8 +1772,15 @@ skip_ack: return 0; case ESTABLISHED: + break; + case FIN_WAIT_1: case FIN_WAIT_2: + if(c->reapable) { + // We already closed the connection and are not interested in more data. + goto reset; + } + break; case CLOSE_WAIT: @@ -1999,7 +2002,7 @@ static bool reset_connection(struct utcp_connection *c) { hdr.src = c->src; hdr.dst = c->dst; hdr.seq = c->snd.nxt; - hdr.ack = 0; + hdr.ack = c->rcv.nxt; hdr.wnd = 0; hdr.ctl = RST; @@ -2042,10 +2045,6 @@ void utcp_abort_all_connections(struct utcp *utcp) { } int utcp_close(struct utcp_connection *c) { - if(c->rcvbuf.used) { - return reset_connection(c) ? 0 : -1; - } - if(utcp_shutdown(c, SHUT_RDWR) && errno != ENOTCONN) { return -1; } @@ -2156,7 +2155,7 @@ bool utcp_is_active(struct utcp *utcp) { return false; } -struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_send_t send, void *priv) { +struct utcp *utcp_init(utcp_accept_t accept, utcp_listen_t listen, utcp_send_t send, void *priv) { if(!send) { errno = EFAULT; return NULL; @@ -2182,7 +2181,7 @@ struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_ } utcp->accept = accept; - utcp->pre_accept = pre_accept; + utcp->listen = listen; utcp->send = send; utcp->priv = priv; utcp->timeout = DEFAULT_USER_TIMEOUT; // sec @@ -2399,10 +2398,10 @@ void utcp_set_poll_cb(struct utcp_connection *c, utcp_poll_t poll) { } } -void utcp_set_accept_cb(struct utcp *utcp, utcp_accept_t accept, utcp_pre_accept_t pre_accept) { +void utcp_set_accept_cb(struct utcp *utcp, utcp_accept_t accept, utcp_listen_t listen) { if(utcp) { utcp->accept = accept; - utcp->pre_accept = pre_accept; + utcp->listen = listen; } }