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);
#ifndef __TINC_CONNECTION_H__
#define __TINC_CONNECTION_H__
+#include "buffer.h"
#include "cipher.h"
#include "digest.h"
#include "rsa.h"
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 */
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;
}
}
bool receive_meta(connection_t *c) {
- int inlen;
+ int inlen, reqlen;
char inbuf[MAXBUFSIZE];
char *bufp = inbuf, *endp;
- 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) {
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;
/* 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;
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) {
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);
}
/*
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);
/* 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))