Remove use of bufferevent and eventbuffers, use our own buffering instead.
This commit is contained in:
parent
f431fcb35f
commit
cdb793f687
5 changed files with 37 additions and 43 deletions
|
@ -75,8 +75,8 @@ void free_connection(connection_t *c) {
|
||||||
if(c->config_tree)
|
if(c->config_tree)
|
||||||
exit_configuration(&c->config_tree);
|
exit_configuration(&c->config_tree);
|
||||||
|
|
||||||
if(c->buffer)
|
buffer_clear(&c->inbuf);
|
||||||
bufferevent_free(c->buffer);
|
buffer_clear(&c->outbuf);
|
||||||
|
|
||||||
if(event_initialized(&c->inevent))
|
if(event_initialized(&c->inevent))
|
||||||
event_del(&c->inevent);
|
event_del(&c->inevent);
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
#ifndef __TINC_CONNECTION_H__
|
#ifndef __TINC_CONNECTION_H__
|
||||||
#define __TINC_CONNECTION_H__
|
#define __TINC_CONNECTION_H__
|
||||||
|
|
||||||
|
#include "buffer.h"
|
||||||
#include "cipher.h"
|
#include "cipher.h"
|
||||||
#include "digest.h"
|
#include "digest.h"
|
||||||
#include "rsa.h"
|
#include "rsa.h"
|
||||||
|
@ -80,8 +81,10 @@ typedef struct connection_t {
|
||||||
|
|
||||||
char *hischallenge; /* The challenge we sent to him */
|
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 inevent; /* input event on this metadata connection */
|
||||||
|
struct event outevent; /* output event on this metadata connection */
|
||||||
int tcplen; /* length of incoming TCPpacket */
|
int tcplen; /* length of incoming TCPpacket */
|
||||||
int allow_request; /* defined if there's only one request possible */
|
int allow_request; /* defined if there's only one request possible */
|
||||||
|
|
||||||
|
|
33
src/meta.c
33
src/meta.c
|
@ -45,21 +45,18 @@ bool send_meta(connection_t *c, const char *buffer, int length) {
|
||||||
char outbuf[length];
|
char outbuf[length];
|
||||||
size_t outlen = 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)",
|
logger(LOG_ERR, "Error while encrypting metadata to %s (%s)",
|
||||||
c->name, c->hostname);
|
c->name, c->hostname);
|
||||||
return false;
|
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 {
|
} else {
|
||||||
ifdebug(META) logger(LOG_DEBUG, "Unencrypted write %p %p %p %d", c, c->buffer, buffer, length);
|
buffer_add(&c->outbuf, buffer, length);
|
||||||
bufferevent_write(c->buffer, (void *)buffer, length);
|
|
||||||
ifdebug(META) logger(LOG_DEBUG, "Done.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
event_add(&c->outevent, NULL);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,7 +73,7 @@ void broadcast_meta(connection_t *from, const char *buffer, int length) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool receive_meta(connection_t *c) {
|
bool receive_meta(connection_t *c) {
|
||||||
int inlen;
|
int inlen, reqlen;
|
||||||
char inbuf[MAXBUFSIZE];
|
char inbuf[MAXBUFSIZE];
|
||||||
char *bufp = inbuf, *endp;
|
char *bufp = inbuf, *endp;
|
||||||
|
|
||||||
|
@ -89,7 +86,7 @@ bool receive_meta(connection_t *c) {
|
||||||
- If not, keep stuff in buffer and exit.
|
- 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 <= 0) {
|
||||||
if(!inlen || !errno) {
|
if(!inlen || !errno) {
|
||||||
|
@ -111,33 +108,30 @@ bool receive_meta(connection_t *c) {
|
||||||
else
|
else
|
||||||
endp = bufp + inlen;
|
endp = bufp + inlen;
|
||||||
|
|
||||||
evbuffer_add(c->buffer->input, bufp, endp - bufp);
|
buffer_add(&c->inbuf, bufp, endp - bufp);
|
||||||
|
|
||||||
inlen -= endp - bufp;
|
inlen -= endp - bufp;
|
||||||
bufp = endp;
|
bufp = endp;
|
||||||
} else {
|
} else {
|
||||||
size_t outlen = inlen;
|
size_t outlen = inlen;
|
||||||
ifdebug(META) logger(LOG_DEBUG, "Received encrypted %d bytes", 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)",
|
logger(LOG_ERR, "Error while decrypting metadata from %s (%s)",
|
||||||
c->name, c->hostname);
|
c->name, c->hostname);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
c->buffer->input->off += inlen;
|
|
||||||
|
|
||||||
inlen = 0;
|
inlen = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
while(c->buffer->input->off) {
|
while(c->inbuf.len) {
|
||||||
/* Are we receiving a TCPpacket? */
|
/* Are we receiving a TCPpacket? */
|
||||||
|
|
||||||
if(c->tcplen) {
|
if(c->tcplen) {
|
||||||
if(c->tcplen <= c->buffer->input->off) {
|
char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen);
|
||||||
receive_tcppacket(c, (char *)c->buffer->input->buffer, c->tcplen);
|
if(tcpbuffer) {
|
||||||
evbuffer_drain(c->buffer->input, c->tcplen);
|
receive_tcppacket(c, tcpbuffer, c->tcplen);
|
||||||
c->tcplen = 0;
|
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
|
@ -146,10 +140,9 @@ bool receive_meta(connection_t *c) {
|
||||||
|
|
||||||
/* Otherwise we are waiting for a request */
|
/* Otherwise we are waiting for a request */
|
||||||
|
|
||||||
char *request = evbuffer_readline(c->buffer->input);
|
char *request = buffer_readline(&c->inbuf);
|
||||||
if(request) {
|
if(request) {
|
||||||
bool result = receive_request(c, request);
|
bool result = receive_request(c, request);
|
||||||
free(request);
|
|
||||||
if(!result)
|
if(!result)
|
||||||
return false;
|
return false;
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -449,13 +449,21 @@ begin:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_meta_read(struct bufferevent *event, void *data) {
|
void handle_meta_write(int sock, short events, void *data) {
|
||||||
logger(LOG_ERR, "handle_meta_read() called");
|
ifdebug(META) logger(LOG_DEBUG, "handle_meta_write() called");
|
||||||
abort();
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_meta_write(struct bufferevent *event, void *data) {
|
buffer_read(&c->outbuf, outlen);
|
||||||
ifdebug(META) logger(LOG_DEBUG, "handle_meta_write() called");
|
if(!c->outbuf.len)
|
||||||
|
event_del(&c->outevent);
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_meta_connection_error(struct bufferevent *event, short what, void *data) {
|
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);
|
do_outgoing_connection(c);
|
||||||
|
|
||||||
event_set(&c->inevent, c->socket, EV_READ | EV_PERSIST, handle_meta_connection_data, 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);
|
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);
|
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->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);
|
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);
|
configure_tcp(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.
|
/* If there already is a lot of data in the outbuf buffer, discard this packet.
|
||||||
We use a very simple Random Early Drop algorithm. */
|
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;
|
return true;
|
||||||
|
|
||||||
if(!send_request(c, "%d %hd", PACKET, packet->len))
|
if(!send_request(c, "%d %hd", PACKET, packet->len))
|
||||||
|
|
Loading…
Reference in a new issue