2 meta.c -- handle the meta communication
3 Copyright (C) 2000-2009 Guus Sliepen <guus@tinc-vpn.org>,
4 2000-2005 Ivo Timmermans
5 2006 Scott Lamb <slamb@slamb.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "splay_tree.h"
26 #include "connection.h"
34 bool send_meta(connection_t *c, const char *buffer, int length) {
36 logger(LOG_ERR, "send_meta() called with NULL pointer!");
40 ifdebug(META) logger(LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
41 c->name, c->hostname);
43 /* Add our data to buffer */
44 if(c->status.encryptout) {
45 size_t outlen = length;
47 if(!cipher_encrypt(&c->outcipher, buffer, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) {
48 logger(LOG_ERR, "Error while encrypting metadata to %s (%s)",
49 c->name, c->hostname);
54 buffer_add(&c->outbuf, buffer, length);
57 event_add(&c->outevent, NULL);
62 void broadcast_meta(connection_t *from, const char *buffer, int length) {
66 for(node = connection_tree->head; node; node = node->next) {
69 if(c != from && c->status.active)
70 send_meta(c, buffer, length);
74 bool receive_meta(connection_t *c) {
76 char inbuf[MAXBUFSIZE];
77 char *bufp = inbuf, *endp;
80 - Read as much as possible from the TCP socket in one go.
82 - Check if a full request is in the input buffer.
83 - If yes, process request and remove it from the buffer,
85 - If not, keep stuff in buffer and exit.
88 buffer_compact(&c->inbuf);
90 if(sizeof inbuf <= c->inbuf.len) {
91 logger(LOG_ERR, "Input buffer full for %s (%s)\n");
95 inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 0);
98 if(!inlen || !errno) {
99 ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)",
100 c->name, c->hostname);
101 } else if(sockwouldblock(sockerrno))
104 logger(LOG_ERR, "Metadata socket read error for %s (%s): %s",
105 c->name, c->hostname, sockstrerror(sockerrno));
110 if(!c->status.decryptin) {
111 endp = memchr(bufp, '\n', inlen);
117 buffer_add(&c->inbuf, bufp, endp - bufp);
119 inlen -= endp - bufp;
122 size_t outlen = inlen;
123 ifdebug(META) logger(LOG_DEBUG, "Received encrypted %d bytes", inlen);
125 if(!cipher_decrypt(&c->incipher, bufp, inlen, buffer_prepare(&c->inbuf, inlen), &outlen, false) || inlen != outlen) {
126 logger(LOG_ERR, "Error while decrypting metadata from %s (%s)",
127 c->name, c->hostname);
134 while(c->inbuf.len) {
135 /* Are we receiving a TCPpacket? */
138 char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen);
140 receive_tcppacket(c, tcpbuffer, c->tcplen);
147 /* Otherwise we are waiting for a request */
149 char *request = buffer_readline(&c->inbuf);
151 bool result = receive_request(c, request);