Remove use of bufferevents and evbuffers.
authorGuus Sliepen <guus@tinc-vpn.org>
Fri, 14 Jan 2011 16:34:48 +0000 (17:34 +0100)
committerGuus Sliepen <guus@tinc-vpn.org>
Fri, 14 Jan 2011 16:34:48 +0000 (17:34 +0100)
src/connection.c
src/connection.h
src/meta.c
src/net_socket.c
src/protocol_misc.c

index cf5ec1b60507d7815c1d6a730eb3a76b0e356b14..2fa789bdf6ba01cb47eb7d3eed2a80085d4b11e4 100644 (file)
@@ -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);
 
index ae1af9fde8785d448598f9abd4b15066396925a7..798078e644fdfe7211e5744af76d38851d982639 100644 (file)
@@ -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 */
index 1e4eb7fe4d1c1f9024ab57e40bf572e811f1118f..43e1e19e7f8b990b1eeb822020cd170ef11a5e8c 100644 (file)
@@ -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;
+       }
 }
index 4316e2d6c86a2b8b73e35668191128f1cc27fb26..ca681e854577919f68a98f5d80e6b83beaf995b7 100644 (file)
@@ -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);
index 43df9bbc77bff4a5d077d3d6a2820b56190acecd..95fc9b362dd7384f754a23d36cd21d16562ccbce 100644 (file)
@@ -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;