2 meta.c -- handle the meta communication
3 Copyright (C) 2000-2005 Guus Sliepen <guus@tinc-vpn.org>,
4 2000-2005 Ivo Timmermans <ivo@tinc-vpn.org>
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 <openssl/err.h>
26 #include <openssl/evp.h>
29 #include "connection.h"
36 bool send_meta(connection_t *c, const char *buffer, int length)
43 ifdebug(META) logger(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length,
44 c->name, c->hostname);
47 c->last_flushed_time = now;
49 /* Find room in connection's buffer */
50 if(length + c->outbuflen > c->outbufsize) {
51 c->outbufsize = length + c->outbuflen;
52 c->outbuf = xrealloc(c->outbuf, c->outbufsize);
55 if(length + c->outbuflen + c->outbufstart > c->outbufsize) {
56 memmove(c->outbuf, c->outbuf + c->outbufstart, c->outbuflen);
60 /* Add our data to buffer */
61 if(c->status.encryptout) {
62 result = EVP_EncryptUpdate(c->outctx, c->outbuf + c->outbufstart + c->outbuflen,
63 &outlen, buffer, length);
64 if(!result || outlen < length) {
65 logger(LOG_ERR, _("Error while encrypting metadata to %s (%s): %s"),
66 c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
68 } else if(outlen > length) {
69 logger(LOG_EMERG, _("Encrypted data too long! Heap corrupted!"));
72 c->outbuflen += outlen;
74 memcpy(c->outbuf + c->outbufstart + c->outbuflen, buffer, length);
75 c->outbuflen += length;
81 bool flush_meta(connection_t *c)
85 ifdebug(META) logger(LOG_DEBUG, _("Flushing %d bytes to %s (%s)"),
86 c->outbuflen, c->name, c->hostname);
89 result = send(c->socket, c->outbuf + c->outbufstart, c->outbuflen, 0);
91 if(!errno || errno == EPIPE) {
92 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
93 c->name, c->hostname);
94 } else if(errno == EINTR) {
96 } else if(errno == EWOULDBLOCK) {
97 ifdebug(CONNECTIONS) logger(LOG_DEBUG, _("Flushing %d bytes to %s (%s) would block"),
98 c->outbuflen, c->name, c->hostname);
101 logger(LOG_ERR, _("Flushing meta data to %s (%s) failed: %s"), c->name,
102 c->hostname, strerror(errno));
108 c->outbufstart += result;
109 c->outbuflen -= result;
112 c->outbufstart = 0; /* avoid unnecessary memmoves */
116 void broadcast_meta(connection_t *from, const char *buffer, int length)
123 for(node = connection_tree->head; node; node = node->next) {
126 if(c != from && c->status.active)
127 send_meta(c, buffer, length);
131 bool receive_meta(connection_t *c)
133 int oldlen, i, result;
134 int lenin, lenout, reqlen;
135 bool decrypted = false;
136 char inbuf[MAXBUFSIZE];
141 - Read as much as possible from the TCP socket in one go.
143 - Check if a full request is in the input buffer.
144 - If yes, process request and remove it from the buffer,
146 - If not, keep stuff in buffer and exit.
149 lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0);
152 if(!lenin || !errno) {
153 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
154 c->name, c->hostname);
155 } else if(errno == EINTR)
158 logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
159 c->name, c->hostname, strerror(errno));
170 if(c->status.decryptin && !decrypted) {
171 result = EVP_DecryptUpdate(c->inctx, inbuf, &lenout, c->buffer + oldlen, lenin);
172 if(!result || lenout != lenin) {
173 logger(LOG_ERR, _("Error while decrypting metadata from %s (%s): %s"),
174 c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
177 memcpy(c->buffer + oldlen, inbuf, lenin);
181 /* Are we receiving a TCPpacket? */
184 if(c->tcplen <= c->buflen) {
185 receive_tcppacket(c, c->buffer, c->tcplen);
187 c->buflen -= c->tcplen;
188 lenin -= c->tcplen - oldlen;
189 memmove(c->buffer, c->buffer + c->tcplen, c->buflen);
198 /* Otherwise we are waiting for a request */
202 for(i = oldlen; i < c->buflen; i++) {
203 if(c->buffer[i] == '\n') {
204 c->buffer[i] = '\0'; /* replace end-of-line by end-of-string so we can use sscanf */
212 if(!receive_request(c))
216 lenin -= reqlen - oldlen;
217 memmove(c->buffer, c->buffer + reqlen, c->buflen);
225 if(c->buflen >= MAXBUFSIZE) {
226 logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
227 c->name, c->hostname);
231 c->last_ping_time = now;