X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;ds=sidebyside;f=src%2Futcp.c;h=65e6be7a2de4e19ac42401ef2696a1590561435d;hb=HEAD;hp=46d7fd35f065473f0baa1a27fe307ee8795eeebc;hpb=49ddbd4e0febb4ca44ee22711eafec3c4b9c4fd1;p=meshlink diff --git a/src/utcp.c b/src/utcp.c index 46d7fd35..ca91bee2 100644 --- a/src/utcp.c +++ b/src/utcp.c @@ -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) { @@ -441,17 +451,45 @@ static void set_buffer_storage(struct buffer *buf, char *data, size_t size) { 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); + size_t minsize = buf->used <= DEFAULT_SNDBUFSIZE ? DEFAULT_SNDBUFSIZE : buf->used; - if(!data) { - // Cannot handle this - abort(); + 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; } - buffer_transfer(buf, data, minsize); - buf->data = data; 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; + } } } @@ -1562,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); @@ -2034,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: @@ -2068,6 +2108,7 @@ 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)); @@ -2075,21 +2116,16 @@ static bool reset_connection(struct utcp_connection *c) { } 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); - } + 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; } -// Closes all the opened connections -void utcp_abort_all_connections(struct utcp *utcp) { +// Resets all connections, but does not invalidate connection handles +void utcp_reset_all_connections(struct utcp *utcp) { if(!utcp) { errno = EINVAL; return; @@ -2102,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); - utcp_abort(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); } } @@ -2171,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); @@ -2273,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); }