]> git.meshlink.io Git - utcp/blobdiff - utcp.c
Fix buffer resizing logic in buffer_put_at().
[utcp] / utcp.c
diff --git a/utcp.c b/utcp.c
index fc147c295208631454682640fd6e30384b29bf26..13f46584484bef5fd71ae74a77792671c1fb7637 100644 (file)
--- a/utcp.c
+++ b/utcp.c
@@ -49,7 +49,7 @@
        (r)->tv_sec = (a)->tv_sec - (b)->tv_sec;\
        (r)->tv_usec = (a)->tv_usec - (b)->tv_usec;\
        if((r)->tv_usec < 0)\
-               (r)->tv_sec--, (r)->tv_usec += 1000000;\
+               (r)->tv_sec--, (r)->tv_usec += USEC_PER_SEC;\
 } while (0)
 #endif
 
@@ -173,7 +173,7 @@ static ssize_t buffer_put_at(struct buffer *buf, size_t offset, const void *data
                } else {
                        do {
                                newsize *= 2;
-                       } while(newsize < buf->used + len);
+                       } while(newsize < required);
                }
                if(newsize > buf->maxsize)
                        newsize = buf->maxsize;
@@ -650,7 +650,7 @@ cleanup:
  * that are shifted to before the start of the receive buffer.
  *
  * There are three cases:
- * - the SACK entry is ahead of ^, in that case just change the offset.
+ * - the SACK entry is after ^, in that case just change the offset.
  * - the SACK entry starts before and ends after ^, so we have to
  *   change both its offset and size.
  * - the SACK entry is completely before ^, in that case delete it.
@@ -706,6 +706,8 @@ static void handle_out_of_order(struct utcp_connection *c, uint32_t offset, cons
                                        memmove(&c->sacks[i + 1], &c->sacks[i], (NSACKS - i - 1) * sizeof c->sacks[i]);
                                        c->sacks[i].offset = offset;
                                        c->sacks[i].len = rxd;
+                               } else {
+                                       debug("SACK entries full, dropping packet\n");
                                }
                                break;
                        } else { // merge
@@ -855,8 +857,10 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
        // In case this is for a CLOSED connection, ignore the packet.
        // TODO: make it so incoming packets can never match a CLOSED connection.
 
-       if(c->state == CLOSED)
+       if(c->state == CLOSED) {
+               debug("Got packet for closed connection\n");
                return 0;
+       }
 
        // It is for an existing connection.
 
@@ -1292,7 +1296,7 @@ int utcp_shutdown(struct utcp_connection *c, int dir) {
 }
 
 int utcp_close(struct utcp_connection *c) {
-       if(utcp_shutdown(c, SHUT_RDWR))
+       if(utcp_shutdown(c, SHUT_RDWR) && errno != ENOTCONN)
                return -1;
        c->recv = NULL;
        c->poll = NULL;
@@ -1432,8 +1436,8 @@ struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_
        utcp->send = send;
        utcp->priv = priv;
        utcp->mtu = DEFAULT_MTU;
-       utcp->timeout = DEFAULT_USER_TIMEOUT; // s
-       utcp->rto = START_RTO; // us
+       utcp->timeout = DEFAULT_USER_TIMEOUT; // sec
+       utcp->rto = START_RTO; // usec
 
        return utcp;
 }