From 9de37fa18e63d71a4676e9d1c9820b09861c949b Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Fri, 14 Jan 2011 17:34:48 +0100 Subject: [PATCH] Remove use of bufferevents and evbuffers. --- src/connection.c | 3 - src/connection.h | 1 - src/meta.c | 139 ++++++++++++++++++++++---------------------- src/net_socket.c | 29 --------- src/protocol_misc.c | 6 -- 5 files changed, 69 insertions(+), 109 deletions(-) diff --git a/src/connection.c b/src/connection.c index cf5ec1b6..2fa789bd 100644 --- a/src/connection.c +++ b/src/connection.c @@ -75,9 +75,6 @@ void free_connection(connection_t *c) { if(c->config_tree) exit_configuration(&c->config_tree); - if(c->buffer) - bufferevent_free(c->buffer); - if(c->thread) thread_destroy(&c->thread); diff --git a/src/connection.h b/src/connection.h index ae1af9fd..798078e6 100644 --- a/src/connection.h +++ b/src/connection.h @@ -80,7 +80,6 @@ typedef struct connection_t { char *hischallenge; /* The challenge we sent to him */ - struct bufferevent *buffer; /* buffer events on this metadata connection */ thread_t thread; 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 1e4eb7fe..43e1e19e 100644 --- a/src/meta.c +++ b/src/meta.c @@ -51,13 +51,9 @@ bool send_meta(connection_t *c, const char *buffer, int length) { return false; } - ifdebug(META) logger(LOG_DEBUG, "Encrypted write %p %p %p %d", c, c->buffer, outbuf, length); write(c->socket, 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); write(c->socket, buffer, length); - ifdebug(META) logger(LOG_DEBUG, "Done."); } return true; @@ -75,89 +71,92 @@ void broadcast_meta(connection_t *from, const char *buffer, int length) { } } +static bool process_meta(connection_t *c, char *reqbuf, int *len) { + while(*len) { + if(c->tcplen) { + if(c->tcplen > *len) + break; + + receive_tcppacket(c, reqbuf, c->tcplen); + + memmove(reqbuf, reqbuf, *len - c->tcplen); + *len -= c->tcplen; + } else { + char *end = memchr(reqbuf, '\n', *len); + if(!end) + break; + else + *end++ = 0; + + if(!receive_request(c, reqbuf)) + return false; + + memmove(reqbuf, end, *len - (end - reqbuf)); + *len -= end - reqbuf; + } + } + + return true; +} + bool receive_meta(connection_t *c) { int inlen; + int reqlen = 0; char inbuf[MAXBUFSIZE]; - char *bufp = inbuf, *endp; + char reqbuf[MAXBUFSIZE]; /* Strategy: - Read as much as possible from the TCP socket in one go. - - Decrypt it. - - Check if a full request is in the input buffer. - - If yes, process request and remove it from the buffer, - then check again. - - If not, keep stuff in buffer and exit. + - Decrypt it if necessary. + - Check if a full request is in the request buffer. + - If yes, process request and remove it from the buffer, then check again. + - If not, try to read more. */ - inlen = recv(c->socket, inbuf, sizeof inbuf, 0); - - if(inlen <= 0) { - if(!inlen || !errno) { - ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)", - c->name, c->hostname); - } else if(sockwouldblock(sockerrno)) - return true; - else - logger(LOG_ERR, "Metadata socket read error for %s (%s): %s", - c->name, c->hostname, sockstrerror(sockerrno)); - return false; - } + while(true) { + inlen = recv(c->socket, inbuf, sizeof inbuf - reqlen, 0); - do { - if(!c->status.decryptin) { - endp = memchr(bufp, '\n', inlen); - if(endp) - endp++; + if(inlen <= 0) { + if(!inlen || !errno) { + ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)", + c->name, c->hostname); + } else if(sockwouldblock(sockerrno)) + continue; else - endp = bufp + inlen; - - evbuffer_add(c->buffer->input, 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); + logger(LOG_ERR, "Metadata socket read error for %s (%s): %s", + c->name, c->hostname, sockstrerror(sockerrno)); + return false; + } - if(!cipher_decrypt(&c->incipher, bufp, inlen, c->buffer->input->buffer + c->buffer->input->off, &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; + while(inlen) { + if(!c->status.decryptin) { + char *end = memchr(inbuf, '\n', inlen); + if(!end) + end = inbuf + inlen; + else + end++; + memcpy(reqbuf + reqlen, inbuf, end - inbuf); + reqlen += end - inbuf; + + if(!process_meta(c, reqbuf, &reqlen)) + return false; - inlen = 0; - } + memmove(inbuf, end, inlen - (end - inbuf)); + inlen -= end - inbuf; + } else { + size_t outlen = inlen; - while(c->buffer->input->off) { - /* 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; - continue; - } else { - break; + if(!cipher_decrypt(&c->incipher, inbuf, inlen, reqbuf + reqlen, &outlen, false) || inlen != outlen) { + logger(LOG_ERR, "Error while decrypting metadata from %s (%s)", c->name, c->hostname); + return false; } - } - /* Otherwise we are waiting for a request */ + reqlen += inlen; + inlen = 0; - char *request = evbuffer_readline(c->buffer->input); - if(request) { - bool result = receive_request(c, request); - free(request); - if(!result) + if(!process_meta(c, reqbuf, &reqlen)) return false; - continue; - } else { - break; } } - } while(inlen); - - return true; + } } diff --git a/src/net_socket.c b/src/net_socket.c index 4316e2d6..ca681e85 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -413,21 +413,6 @@ 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) { - ifdebug(META) logger(LOG_DEBUG, "handle_meta_write() called"); -} - -void handle_meta_connection_error(struct bufferevent *event, short what, void *data) { - connection_t *c = data; - logger(LOG_ERR, "handle_meta_connection_error() called: %d: %s", what, strerror(errno)); - terminate_connection(c, c->status.active); -} - void setup_outgoing_connection(outgoing_t *outgoing) { connection_t *c; node_t *n; @@ -469,13 +454,6 @@ void setup_outgoing_connection(outgoing_t *outgoing) { do_outgoing_connection(c); - 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); - if(!thread_create(&c->thread, handle_meta_connection_data, c)) { logger(LOG_ERR, "create_thread() failed: %s", strerror(errno)); abort(); @@ -515,13 +493,6 @@ void handle_new_meta_connection(int sock, short events, void *data) { ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection from %s", c->hostname); - 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); connection_add(c); diff --git a/src/protocol_misc.c b/src/protocol_misc.c index 43df9bbc..95fc9b36 100644 --- a/src/protocol_misc.c +++ b/src/protocol_misc.c @@ -116,12 +116,6 @@ bool pong_h(connection_t *c, char *request) { /* Sending and receiving packets via TCP */ 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) - return true; - if(!send_request(c, "%d %hd", PACKET, packet->len)) return false; -- 2.20.1