]> git.meshlink.io Git - meshlink/blobdiff - src/utcp.c
Ensure we don't use external buffers after a channel is closed.
[meshlink] / src / utcp.c
index 1c0890c6743b4a71b4da052bdb7ee800958b71ad..46d7fd35f065473f0baa1a27fe307ee8795eeebc 100644 (file)
@@ -395,8 +395,71 @@ static bool buffer_set_size(struct buffer *buf, uint32_t minsize, uint32_t maxsi
        return buf->size >= minsize || buffer_resize(buf, minsize);
 }
 
+static void buffer_transfer(struct buffer *buf, char *newdata, size_t newsize) {
+       if(buffer_wraps(buf)) {
+               // Old situation:
+               // [345......012]
+               // New situation:
+               // [012345......]
+               uint32_t tailsize = buf->size - buf->offset;
+               memcpy(newdata, buf->data + buf->offset, tailsize);
+               memcpy(newdata + tailsize, buf->data, buf->used - tailsize);
+       } else {
+               // Old situation:
+               // [....012345..]
+               // New situation:
+               // [012345......]
+               memcpy(newdata, buf->data + buf->offset, buf->used);
+       }
+
+       buf->offset = 0;
+       buf->size = newsize;
+}
+
+static void set_buffer_storage(struct buffer *buf, char *data, size_t size) {
+       if(size > UINT32_MAX) {
+               size = UINT32_MAX;
+       }
+
+       buf->maxsize = size;
+
+       if(data) {
+               if(buf->external) {
+                       // Don't allow resizing an external buffer
+                       abort();
+               }
+
+               if(size < buf->used) {
+                       // Ignore requests for an external buffer if we are already using more than it can store
+                       return;
+               }
+
+               // Transition from internal to external buffer
+               buffer_transfer(buf, data, size);
+               free(buf->data);
+               buf->data = data;
+               buf->external = true;
+       } else if(buf->external) {
+               // Transition from external to internal buf
+               size_t minsize = buf->used < DEFAULT_SNDBUFSIZE ? DEFAULT_SNDBUFSIZE : buf->used;
+               data = malloc(minsize);
+
+               if(!data) {
+                       // Cannot handle this
+                       abort();
+               }
+
+               buffer_transfer(buf, data, minsize);
+               buf->data = data;
+               buf->external = false;
+       }
+}
+
 static void buffer_exit(struct buffer *buf) {
-       free(buf->data);
+       if(!buf->external) {
+               free(buf->data);
+       }
+
        memset(buf, 0, sizeof(*buf));
 }
 
@@ -650,6 +713,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 +1356,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 +1431,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 +1519,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 +1608,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 +1776,7 @@ skip_ack:
                                c->snd.last++;
                                set_state(c, FIN_WAIT_1);
                        } else {
+                               c->do_poll = true;
                                set_state(c, ESTABLISHED);
                        }
 
@@ -1776,8 +1835,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 +2065,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;
 
@@ -2008,6 +2074,20 @@ static bool reset_connection(struct utcp_connection *c) {
        return true;
 }
 
+static void set_reapable(struct utcp_connection *c) {
+       if(c->sndbuf.external) {
+               set_buffer_storage(&c->sndbuf, NULL, DEFAULT_MTU);
+       }
+
+       if(c->rcvbuf.external) {
+               set_buffer_storage(&c->rcvbuf, NULL, DEFAULT_MTU);
+       }
+
+       c->recv = NULL;
+       c->poll = NULL;
+       c->reapable = true;
+}
+
 // Closes all the opened connections
 void utcp_abort_all_connections(struct utcp *utcp) {
        if(!utcp) {
@@ -2025,7 +2105,7 @@ void utcp_abort_all_connections(struct utcp *utcp) {
                utcp_recv_t old_recv = c->recv;
                utcp_poll_t old_poll = c->poll;
 
-               reset_connection(c);
+               utcp_abort(c);
 
                if(old_recv) {
                        errno = 0;
@@ -2042,18 +2122,11 @@ void utcp_abort_all_connections(struct utcp *utcp) {
 }
 
 int utcp_close(struct utcp_connection *c) {
-       if(c->rcvbuf.used) {
-               fprintf(stderr, "UTCP channel closed with stuff in receive buffer\n");
-               return reset_connection(c) ? 0 : -1;
-       }
-
        if(utcp_shutdown(c, SHUT_RDWR) && errno != ENOTCONN) {
                return -1;
        }
 
-       c->recv = NULL;
-       c->poll = NULL;
-       c->reapable = true;
+       set_reapable(c);
        return 0;
 }
 
@@ -2062,7 +2135,7 @@ int utcp_abort(struct utcp_connection *c) {
                return -1;
        }
 
-       c->reapable = true;
+       set_reapable(c);
        return 0;
 }
 
@@ -2157,7 +2230,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;
@@ -2183,7 +2256,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
@@ -2317,16 +2390,12 @@ size_t utcp_get_sndbuf_free(struct utcp_connection *c) {
        }
 }
 
-void utcp_set_sndbuf(struct utcp_connection *c, size_t size) {
+void utcp_set_sndbuf(struct utcp_connection *c, void *data, size_t size) {
        if(!c) {
                return;
        }
 
-       c->sndbuf.maxsize = size;
-
-       if(c->sndbuf.maxsize != size) {
-               c->sndbuf.maxsize = -1;
-       }
+       set_buffer_storage(&c->sndbuf, data, size);
 
        c->do_poll = is_reliable(c) && buffer_free(&c->sndbuf);
 }
@@ -2343,16 +2412,12 @@ size_t utcp_get_rcvbuf_free(struct utcp_connection *c) {
        }
 }
 
-void utcp_set_rcvbuf(struct utcp_connection *c, size_t size) {
+void utcp_set_rcvbuf(struct utcp_connection *c, void *data, size_t size) {
        if(!c) {
                return;
        }
 
-       c->rcvbuf.maxsize = size;
-
-       if(c->rcvbuf.maxsize != size) {
-               c->rcvbuf.maxsize = -1;
-       }
+       set_buffer_storage(&c->rcvbuf, data, size);
 }
 
 size_t utcp_get_sendq(struct utcp_connection *c) {
@@ -2400,10 +2465,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;
        }
 }
 
@@ -2430,6 +2495,11 @@ void utcp_expect_data(struct utcp_connection *c, bool expect) {
        }
 }
 
+void utcp_set_flags(struct utcp_connection *c, uint32_t flags) {
+       c->flags &= ~UTCP_CHANGEABLE_FLAGS;
+       c->flags |= flags & UTCP_CHANGEABLE_FLAGS;
+}
+
 void utcp_offline(struct utcp *utcp, bool offline) {
        struct timespec now;
        clock_gettime(UTCP_CLOCK, &now);