From 2ccd5cc7da5f6157beca8cfe9dbf115e95a99776 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Wed, 10 Dec 2014 11:09:59 +0100 Subject: [PATCH] Add fin_wanted() function that checks whether a FIN bit should be set on a packet. --- utcp.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/utcp.c b/utcp.c index d49c4dd..f062319 100644 --- a/utcp.c +++ b/utcp.c @@ -103,6 +103,19 @@ static void set_state(struct utcp_connection *c, enum state state) { debug("%p new state: %s\n", c->utcp, strstate[state]); } +static bool fin_wanted(struct utcp_connection *c, uint32_t seq) { + if(seq != c->snd.last) + return false; + switch(c->state) { + case FIN_WAIT_1: + case CLOSING: + case LAST_ACK: + return true; + default: + return false; + } +} + static inline void list_connections(struct utcp *utcp) { debug("%p has %d connections:\n", utcp, utcp->nconnections); for(int i = 0; i < utcp->nconnections; i++) @@ -366,16 +379,9 @@ static void ack(struct utcp_connection *c, bool sendatleastone) { c->snd.nxt += seglen; left -= seglen; - if(c->state != ESTABLISHED && seglen && c->snd.nxt == c->snd.last) { - switch(c->state) { - case FIN_WAIT_1: - case CLOSING: - seglen--; - pkt->hdr.ctl |= FIN; - break; - default: - break; - } + if(seglen && fin_wanted(c, c->snd.nxt)) { + seglen--; + pkt->hdr.ctl |= FIN; } print_packet(c->utcp, "send", pkt, sizeof pkt->hdr + seglen); -- 2.39.5