From cdb793f687262b9f56823ca9046523a609a758af Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sat, 14 May 2011 19:20:56 +0200 Subject: [PATCH] Remove use of bufferevent and eventbuffers, use our own buffering instead. --- src/connection.c | 4 ++-- src/connection.h | 5 ++++- src/meta.c | 35 ++++++++++++++--------------------- src/net_socket.c | 34 ++++++++++++++++------------------ src/protocol_misc.c | 2 +- 5 files changed, 37 insertions(+), 43 deletions(-) diff --git a/src/connection.c b/src/connection.c index fecb48d4..5a6bc746 100644 --- a/src/connection.c +++ b/src/connection.c @@ -75,8 +75,8 @@ void free_connection(connection_t *c) { if(c->config_tree) exit_configuration(&c->config_tree); - if(c->buffer) - bufferevent_free(c->buffer); + buffer_clear(&c->inbuf); + buffer_clear(&c->outbuf); if(event_initialized(&c->inevent)) event_del(&c->inevent); diff --git a/src/connection.h b/src/connection.h index 0fc49ef3..41986723 100644 --- a/src/connection.h +++ b/src/connection.h @@ -21,6 +21,7 @@ #ifndef __TINC_CONNECTION_H__ #define __TINC_CONNECTION_H__ +#include "buffer.h" #include "cipher.h" #include "digest.h" #include "rsa.h" @@ -80,8 +81,10 @@ typedef struct connection_t { char *hischallenge; /* The challenge we sent to him */ - struct bufferevent *buffer; /* buffer events on this metadata connection */ + struct buffer_t inbuf; + struct buffer_t outbuf; struct event inevent; /* input event on this metadata connection */ + struct event outevent; /* output event on this metadata connection */ int tcplen; /* length of incoming TCPpacket */ int allow_request; /* defined if there's only one request possible */ diff --git a/src/meta.c b/src/meta.c index a3f7ef3c..726dcca9 100644 --- a/src/meta.c +++ b/src/meta.c @@ -45,21 +45,18 @@ bool send_meta(connection_t *c, const char *buffer, int length) { char outbuf[length]; size_t outlen = length; - if(!cipher_encrypt(&c->outcipher, buffer, length, outbuf, &outlen, false) || outlen != length) { + if(!cipher_encrypt(&c->outcipher, outbuf, 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; } - - ifdebug(META) logger(LOG_DEBUG, "Encrypted write %p %p %p %d", c, c->buffer, outbuf, length); - bufferevent_write(c->buffer, (void *)outbuf, length); - ifdebug(META) logger(LOG_DEBUG, "Done."); + } else { - ifdebug(META) logger(LOG_DEBUG, "Unencrypted write %p %p %p %d", c, c->buffer, buffer, length); - bufferevent_write(c->buffer, (void *)buffer, length); - ifdebug(META) logger(LOG_DEBUG, "Done."); + buffer_add(&c->outbuf, buffer, length); } + event_add(&c->outevent, NULL); + return true; } @@ -76,7 +73,7 @@ void broadcast_meta(connection_t *from, const char *buffer, int length) { } bool receive_meta(connection_t *c) { - int inlen; + int inlen, reqlen; char inbuf[MAXBUFSIZE]; char *bufp = inbuf, *endp; @@ -89,7 +86,7 @@ bool receive_meta(connection_t *c) { - If not, keep stuff in buffer and exit. */ - inlen = recv(c->socket, inbuf, sizeof inbuf, 0); + inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 0); if(inlen <= 0) { if(!inlen || !errno) { @@ -111,33 +108,30 @@ bool receive_meta(connection_t *c) { else endp = bufp + inlen; - evbuffer_add(c->buffer->input, bufp, endp - bufp); + buffer_add(&c->inbuf, bufp, endp - bufp); inlen -= endp - bufp; bufp = endp; } else { size_t outlen = inlen; ifdebug(META) logger(LOG_DEBUG, "Received encrypted %d bytes", inlen); - evbuffer_expand(c->buffer->input, c->buffer->input->off + inlen); - if(!cipher_decrypt(&c->incipher, bufp, inlen, c->buffer->input->buffer + c->buffer->input->off, &outlen, false) || inlen != outlen) { + if(!cipher_decrypt(&c->incipher, bufp, inlen, buffer_prepare(&c->inbuf, inlen), &outlen, false) || inlen != outlen) { logger(LOG_ERR, "Error while decrypting metadata from %s (%s)", c->name, c->hostname); return false; } - c->buffer->input->off += inlen; inlen = 0; } - while(c->buffer->input->off) { + while(c->inbuf.len) { /* Are we receiving a TCPpacket? */ if(c->tcplen) { - if(c->tcplen <= c->buffer->input->off) { - receive_tcppacket(c, (char *)c->buffer->input->buffer, c->tcplen); - evbuffer_drain(c->buffer->input, c->tcplen); - c->tcplen = 0; + char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen); + if(tcpbuffer) { + receive_tcppacket(c, tcpbuffer, c->tcplen); continue; } else { break; @@ -146,10 +140,9 @@ bool receive_meta(connection_t *c) { /* Otherwise we are waiting for a request */ - char *request = evbuffer_readline(c->buffer->input); + char *request = buffer_readline(&c->inbuf); if(request) { bool result = receive_request(c, request); - free(request); if(!result) return false; continue; diff --git a/src/net_socket.c b/src/net_socket.c index e20076fb..6093f0e0 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -449,13 +449,21 @@ begin: return; } -void handle_meta_read(struct bufferevent *event, void *data) { - logger(LOG_ERR, "handle_meta_read() called"); - abort(); -} - -void handle_meta_write(struct bufferevent *event, void *data) { +void handle_meta_write(int sock, short events, void *data) { ifdebug(META) logger(LOG_DEBUG, "handle_meta_write() called"); + + connection_t *c = data; + + size_t outlen = write(c->socket, c->outbuf.data + c->outbuf.offset, c->outbuf.len - c->outbuf.offset); + if(outlen <= 0) { + logger(LOG_ERR, "Onoes, outlen = %zd (%s)", outlen, strerror(errno)); + terminate_connection(c, c->status.active); + return; + } + + buffer_read(&c->outbuf, outlen); + if(!c->outbuf.len) + event_del(&c->outevent); } void handle_meta_connection_error(struct bufferevent *event, short what, void *data) { @@ -506,13 +514,8 @@ void setup_outgoing_connection(outgoing_t *outgoing) { do_outgoing_connection(c); event_set(&c->inevent, c->socket, EV_READ | EV_PERSIST, handle_meta_connection_data, c); + event_set(&c->outevent, c->socket, EV_WRITE | EV_PERSIST, handle_meta_write, c); event_add(&c->inevent, NULL); - c->buffer = bufferevent_new(c->socket, handle_meta_read, handle_meta_write, handle_meta_connection_error, c); - if(!c->buffer) { - logger(LOG_ERR, "bufferevent_new() failed: %s", strerror(errno)); - abort(); - } - bufferevent_disable(c->buffer, EV_READ); } /* @@ -549,13 +552,8 @@ void handle_new_meta_connection(int sock, short events, void *data) { ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection from %s", c->hostname); event_set(&c->inevent, c->socket, EV_READ | EV_PERSIST, handle_meta_connection_data, c); + event_set(&c->outevent, c->socket, EV_WRITE | EV_PERSIST, handle_meta_write, c); event_add(&c->inevent, NULL); - c->buffer = bufferevent_new(c->socket, NULL, handle_meta_write, handle_meta_connection_error, c); - if(!c->buffer) { - logger(LOG_ERR, "bufferevent_new() failed: %s", strerror(errno)); - abort(); - } - bufferevent_disable(c->buffer, EV_READ); configure_tcp(c); diff --git a/src/protocol_misc.c b/src/protocol_misc.c index 43df9bbc..4ca15853 100644 --- a/src/protocol_misc.c +++ b/src/protocol_misc.c @@ -119,7 +119,7 @@ bool send_tcppacket(connection_t *c, vpn_packet_t *packet) { /* If there already is a lot of data in the outbuf buffer, discard this packet. We use a very simple Random Early Drop algorithm. */ - if(2.0 * c->buffer->output->off / (float)maxoutbufsize - 1 > (float)rand()/(float)RAND_MAX) + if(2.0 * c->outbuf.len / (float)maxoutbufsize - 1 > (float)rand()/(float)RAND_MAX) return true; if(!send_request(c, "%d %hd", PACKET, packet->len)) -- 2.20.1