]> git.meshlink.io Git - meshlink/blobdiff - src/utcp.c
Never automatically try to bind to ports >= 32768.
[meshlink] / src / utcp.c
index 20dd0aba049757ca30833779ec772c63ba685310..ca91bee26ef6d21c0f0f57dd743c27c0f9b14f8b 100644 (file)
@@ -196,6 +196,16 @@ static bool buffer_wraps(struct buffer *buf) {
 }
 
 static bool buffer_resize(struct buffer *buf, uint32_t newsize) {
+       assert(!buf->external);
+
+       if(!newsize) {
+               free(buf->data);
+               buf->data = NULL;
+               buf->size = 0;
+               buf->offset = 0;
+               return true;
+       }
+
        char *newdata = realloc(buf->data, newsize);
 
        if(!newdata) {
@@ -395,8 +405,99 @@ 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;
+
+               if(minsize) {
+                       data = malloc(minsize);
+
+                       if(!data) {
+                               // Cannot handle this
+                               abort();
+                       }
+
+                       buffer_transfer(buf, data, minsize);
+                       buf->data = data;
+               } else {
+                       buf->data = NULL;
+                       buf->size = 0;
+               }
+
+               buf->external = false;
+       } else {
+               // Don't do anything if the buffer wraps
+               if(buffer_wraps(buf)) {
+                       return;
+               }
+
+               // Realloc internal storage
+               size_t minsize = max(DEFAULT_SNDBUFSIZE, buf->offset + buf->used);
+
+               if(minsize) {
+                       data = realloc(buf->data, minsize);
+
+                       if(data) {
+                               buf->data = data;
+                               buf->size = minsize;
+                       }
+               } else {
+                       free(buf->data);
+                       buf->data = NULL;
+                       buf->size = 0;
+               }
+       }
+}
+
 static void buffer_exit(struct buffer *buf) {
-       free(buf->data);
+       if(!buf->external) {
+               free(buf->data);
+       }
+
        memset(buf, 0, sizeof(*buf));
 }
 
@@ -1499,6 +1600,8 @@ synack:
                        // The peer has aborted our connection.
                        set_state(c, CLOSED);
                        errno = ECONNRESET;
+                       buffer_clear(&c->sndbuf);
+                       buffer_clear(&c->rcvbuf);
 
                        if(c->recv) {
                                c->recv(c, NULL, 0);
@@ -1971,8 +2074,8 @@ static bool reset_connection(struct utcp_connection *c) {
                return false;
        }
 
-       c->recv = NULL;
-       c->poll = NULL;
+       buffer_clear(&c->sndbuf);
+       buffer_clear(&c->rcvbuf);
 
        switch(c->state) {
        case CLOSED:
@@ -2005,14 +2108,24 @@ static bool reset_connection(struct utcp_connection *c) {
        hdr.ack = c->rcv.nxt;
        hdr.wnd = 0;
        hdr.ctl = RST;
+       hdr.aux = 0;
 
        print_packet(c, "send", &hdr, sizeof(hdr));
        c->utcp->send(c->utcp, &hdr, sizeof(hdr));
        return true;
 }
 
-// Closes all the opened connections
-void utcp_abort_all_connections(struct utcp *utcp) {
+static void set_reapable(struct utcp_connection *c) {
+       set_buffer_storage(&c->sndbuf, NULL, min(c->sndbuf.maxsize, DEFAULT_MAXSNDBUFSIZE));
+       set_buffer_storage(&c->rcvbuf, NULL, min(c->rcvbuf.maxsize, DEFAULT_MAXRCVBUFSIZE));
+
+       c->recv = NULL;
+       c->poll = NULL;
+       c->reapable = true;
+}
+
+// Resets all connections, but does not invalidate connection handles
+void utcp_reset_all_connections(struct utcp *utcp) {
        if(!utcp) {
                errno = EINVAL;
                return;
@@ -2025,19 +2138,16 @@ void utcp_abort_all_connections(struct utcp *utcp) {
                        continue;
                }
 
-               utcp_recv_t old_recv = c->recv;
-               utcp_poll_t old_poll = c->poll;
-
                reset_connection(c);
 
-               if(old_recv) {
+               if(c->recv) {
                        errno = 0;
-                       old_recv(c, NULL, 0);
+                       c->recv(c, NULL, 0);
                }
 
-               if(old_poll && !c->reapable) {
+               if(c->poll && !c->reapable) {
                        errno = 0;
-                       old_poll(c, 0);
+                       c->poll(c, 0);
                }
        }
 
@@ -2049,9 +2159,7 @@ int utcp_close(struct utcp_connection *c) {
                return -1;
        }
 
-       c->recv = NULL;
-       c->poll = NULL;
-       c->reapable = true;
+       set_reapable(c);
        return 0;
 }
 
@@ -2060,7 +2168,7 @@ int utcp_abort(struct utcp_connection *c) {
                return -1;
        }
 
-       c->reapable = true;
+       set_reapable(c);
        return 0;
 }
 
@@ -2096,6 +2204,8 @@ struct timespec utcp_timeout(struct utcp *utcp) {
                if(timespec_isset(&c->conn_timeout) && timespec_lt(&c->conn_timeout, &now)) {
                        errno = ETIMEDOUT;
                        c->state = CLOSED;
+                       buffer_clear(&c->sndbuf);
+                       buffer_clear(&c->rcvbuf);
 
                        if(c->recv) {
                                c->recv(c, NULL, 0);
@@ -2198,6 +2308,9 @@ void utcp_exit(struct utcp *utcp) {
                struct utcp_connection *c = utcp->connections[i];
 
                if(!c->reapable) {
+                       buffer_clear(&c->sndbuf);
+                       buffer_clear(&c->rcvbuf);
+
                        if(c->recv) {
                                c->recv(c, NULL, 0);
                        }
@@ -2315,16 +2428,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);
 }
@@ -2341,16 +2450,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) {
@@ -2428,6 +2533,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);