]> git.meshlink.io Git - utcp/blobdiff - utcp.c
Send ACKs without a payload upon receiving an out-of-order packet.
[utcp] / utcp.c
diff --git a/utcp.c b/utcp.c
index df2a31ec6a5f7bcad210170eace9a7dda4a78c8e..db775a9e8a5b37cc3e15c2ff06bf8cc4236c4260 100644 (file)
--- a/utcp.c
+++ b/utcp.c
@@ -130,13 +130,11 @@ static ssize_t buffer_put(struct buffer *buf, const void *data, size_t len) {
                if(newsize > buf->maxsize)
                        newsize = buf->maxsize;
                char *newdata = realloc(buf->data, newsize);
-               fprintf(stderr, "%p = realloc(%p, %zu)\n", newdata, buf->data, newsize);
                if(!newdata)
                        return -1;
                buf->data = newdata;
                buf->size = newsize;
        }
-       fprintf(stderr, "memcpy(%p, %p, %zu)\n", buf->data + buf->used, data, len);
        memcpy(buf->data + buf->used, data, len);
        buf->used += len;
        return len;
@@ -589,12 +587,13 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
 #endif
 
        if(!acceptable) {
-               debug("Packet not acceptable, %u  <= %u + %zu < %u\n", c->rcv.nxt, hdr.seq, len, c->rcv.nxt + c->rcv.wnd);
+               debug("Packet not acceptable, %u <= %u + %zu < %u\n", c->rcv.nxt, hdr.seq, len, c->rcv.nxt + c->rcv.wnd);
                // Ignore unacceptable RST packets.
                if(hdr.ctl & RST)
                        return 0;
                // Otherwise, send an ACK back in the hope things improve.
-               goto ack;
+               ack(c, true);
+               return 0;
        }
 
        c->snd.wnd = hdr.wnd; // TODO: move below
@@ -803,7 +802,6 @@ ssize_t utcp_recv(struct utcp *utcp, const void *data, size_t len) {
                        rxd = c->recv(c, data, len);
                        if(rxd != len) {
                                // TODO: once we have a receive buffer, handle the application not accepting all data.
-                               fprintf(stderr, "c->recv(%p, %p, %zu) returned %zd\n", c, data, len, rxd);
                                abort();
                        }
                        if(rxd < 0)
@@ -933,6 +931,8 @@ int utcp_shutdown(struct utcp_connection *c, int dir) {
 int utcp_close(struct utcp_connection *c) {
        if(utcp_shutdown(c, SHUT_RDWR))
                return -1;
+       c->recv = NULL;
+       c->poll = NULL;
        c->reapable = true;
        return 0;
 }
@@ -949,6 +949,8 @@ int utcp_abort(struct utcp_connection *c) {
                return -1;
        }
 
+       c->recv = NULL;
+       c->poll = NULL;
        c->reapable = true;
 
        switch(c->state) {
@@ -1060,7 +1062,7 @@ static void retransmit(struct utcp_connection *c) {
  * The return value is the time to the next timeout in milliseconds,
  * or maybe a negative value if the timeout is infinite.
  */
-int utcp_timeout(struct utcp *utcp) {
+struct timeval utcp_timeout(struct utcp *utcp) {
        struct timeval now;
        gettimeofday(&now, NULL);
        struct timeval next = {now.tv_sec + 3600, now.tv_usec};
@@ -1110,9 +1112,7 @@ int utcp_timeout(struct utcp *utcp) {
 
        struct timeval diff;
        timersub(&next, &now, &diff);
-       if(diff.tv_sec < 0)
-               return 0;
-       return diff.tv_sec * 1000 + diff.tv_usec / 1000;
+       return diff;
 }
 
 struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_send_t send, void *priv) {