X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Futcp.c;h=8b3509ef0f5c376ee7b37640cb3c82dba1819df4;hb=0f5ab29d787c7fb444908797d5ea746cb4311be4;hp=9e2229ad522fb48b2d5431b6658e82671422680e;hpb=0fdc99b6a501992b8c1dea2d1a909363b5564d0d;p=meshlink diff --git a/src/utcp.c b/src/utcp.c index 9e2229ad..8b3509ef 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) { @@ -395,6 +405,94 @@ 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) { if(!buf->external) { free(buf->data); @@ -2014,6 +2112,15 @@ static bool reset_connection(struct utcp_connection *c) { return true; } +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; +} + // Closes all the opened connections void utcp_abort_all_connections(struct utcp *utcp) { if(!utcp) { @@ -2031,7 +2138,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; @@ -2052,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; } @@ -2063,7 +2168,7 @@ int utcp_abort(struct utcp_connection *c) { return -1; } - c->reapable = true; + set_reapable(c); return 0; } @@ -2318,66 +2423,6 @@ size_t utcp_get_sndbuf_free(struct utcp_connection *c) { } } -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 - buf->offset); - } 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; - } -} - void utcp_set_sndbuf(struct utcp_connection *c, void *data, size_t size) { if(!c) { return;