From: Guus Sliepen Date: Sat, 14 May 2011 20:30:23 +0000 (+0200) Subject: Several fixes for the buffer code. X-Git-Tag: release-1.1pre1~61 X-Git-Url: https://tinc-vpn.org/git/browse?a=commitdiff_plain;h=ffa3a443b9f01d3ea0fcb3c4fc6928a5c695cf4a;p=tinc Several fixes for the buffer code. --- diff --git a/src/buffer.c b/src/buffer.c index fc4fac0f..72888b50 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -22,6 +22,14 @@ #include "buffer.h" #include "xalloc.h" +void buffer_compact(buffer_t *buffer) { + if(buffer->offset / 7 > buffer->len / 8) { + memmove(buffer->data, buffer->data + buffer->offset, buffer->len - buffer->offset); + buffer->len -= buffer->offset; + buffer->offset = 0; + } +} + // Make sure we can add size bytes to the buffer, and return a pointer to the start of those bytes. char *buffer_prepare(buffer_t *buffer, int size) { @@ -41,8 +49,11 @@ char *buffer_prepare(buffer_t *buffer, int size) { } } + char *start = buffer->data + buffer->len; + buffer->len += size; - return buffer->data + buffer->offset; + + return start; } // Copy data into the buffer. @@ -61,8 +72,6 @@ static char *buffer_consume(buffer_t *buffer, int size) { if(buffer->offset >= buffer->len) { buffer->offset = 0; buffer->len = 0; - } else { - buffer->offset += size; } return start; @@ -76,7 +85,7 @@ char *buffer_readline(buffer_t *buffer) { if(!newline) return NULL; - int len = newline + 1 - buffer->data + buffer->offset; + int len = newline + 1 - (buffer->data + buffer->offset); *newline = 0; return buffer_consume(buffer, len); } diff --git a/src/buffer.h b/src/buffer.h index 4d6f6726..db22ac4f 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -8,6 +8,7 @@ typedef struct buffer_t { int offset; } buffer_t; +extern void buffer_compact(buffer_t *buffer); extern char *buffer_prepare(buffer_t *buffer, int size); extern char *buffer_add(buffer_t *buffer, const char *data, int size); extern char *buffer_readline(buffer_t *buffer); diff --git a/src/meta.c b/src/meta.c index 726dcca9..1fbd22c2 100644 --- a/src/meta.c +++ b/src/meta.c @@ -42,10 +42,9 @@ bool send_meta(connection_t *c, const char *buffer, int length) { /* Add our data to buffer */ if(c->status.encryptout) { - char outbuf[length]; size_t outlen = length; - if(!cipher_encrypt(&c->outcipher, outbuf, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) { + if(!cipher_encrypt(&c->outcipher, buffer, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) { logger(LOG_ERR, "Error while encrypting metadata to %s (%s)", c->name, c->hostname); return false; @@ -152,5 +151,7 @@ bool receive_meta(connection_t *c) { } } while(inlen); + buffer_compact(&c->inbuf); + return true; }