2 meta.c -- handle the meta communication
3 Copyright (C) 2000-2006 Guus Sliepen <guus@tinc-vpn.org>,
4 2000-2005 Ivo Timmermans
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "splay_tree.h"
27 #include "connection.h"
35 bool send_meta(connection_t *c, const char *buffer, int length) {
38 ifdebug(META) logger(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length,
39 c->name, c->hostname);
41 /* Add our data to buffer */
42 if(c->status.encryptout) {
44 size_t outlen = length;
46 if(!cipher_encrypt(&c->outcipher, buffer, length, outbuf, &outlen, false) || outlen != length) {
47 logger(LOG_ERR, _("Error while encrypting metadata to %s (%s)"),
48 c->name, c->hostname);
52 ifdebug(META) logger(LOG_DEBUG, _("Encrypted write %p %p %p %d"), c, c->buffer, outbuf, length);
53 bufferevent_write(c->buffer, (void *)outbuf, length);
54 ifdebug(META) logger(LOG_DEBUG, _("Done."));
56 ifdebug(META) logger(LOG_DEBUG, _("Unencrypted write %p %p %p %d"), c, c->buffer, buffer, length);
57 bufferevent_write(c->buffer, (void *)buffer, length);
58 ifdebug(META) logger(LOG_DEBUG, _("Done."));
64 void broadcast_meta(connection_t *from, const char *buffer, int length) {
70 for(node = connection_tree->head; node; node = node->next) {
73 if(c != from && c->status.active)
74 send_meta(c, buffer, length);
78 bool receive_meta(connection_t *c) {
80 char inbuf[MAXBUFSIZE];
81 char *bufp = inbuf, *endp;
86 - Read as much as possible from the TCP socket in one go.
88 - Check if a full request is in the input buffer.
89 - If yes, process request and remove it from the buffer,
91 - If not, keep stuff in buffer and exit.
94 inlen = recv(c->socket, inbuf, sizeof inbuf, 0);
97 logger(LOG_ERR, _("Receive callback called for %s (%s) but no data to receive: %s"), c->name, c->hostname, strerror(errno));
102 if(!c->status.decryptin) {
103 endp = memchr(bufp, '\n', inlen);
109 evbuffer_add(c->buffer->input, bufp, endp - bufp);
111 inlen -= endp - bufp;
114 size_t outlen = inlen;
115 ifdebug(META) logger(LOG_DEBUG, _("Received encrypted %d bytes"), inlen);
116 evbuffer_expand(c->buffer->input, c->buffer->input->off + inlen);
118 if(!cipher_decrypt(&c->incipher, bufp, inlen, c->buffer->input->buffer + c->buffer->input->off, &outlen, false) || inlen != outlen) {
119 logger(LOG_ERR, _("Error while decrypting metadata from %s (%s)"),
120 c->name, c->hostname);
123 c->buffer->input->off += inlen;
128 while(c->buffer->input->off) {
129 /* Are we receiving a TCPpacket? */
132 if(c->tcplen <= c->buffer->input->off) {
133 receive_tcppacket(c, (char *)c->buffer->input->buffer, c->tcplen);
134 evbuffer_drain(c->buffer->input, c->tcplen);
142 /* Otherwise we are waiting for a request */
144 char *request = evbuffer_readline(c->buffer->input);
146 bool result = receive_request(c, request);
157 c->last_ping_time = time(NULL);