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 <openssl/err.h>
25 #include <openssl/evp.h>
28 #include "connection.h"
36 bool send_meta(connection_t *c, const char *buffer, int length) {
41 logger(LOG_ERR, "send_meta() called with NULL pointer!");
45 ifdebug(META) logger(LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
46 c->name, c->hostname);
49 c->last_flushed_time = now;
51 /* Find room in connection's buffer */
52 if(length + c->outbuflen > c->outbufsize) {
53 c->outbufsize = length + c->outbuflen;
54 c->outbuf = xrealloc(c->outbuf, c->outbufsize);
57 if(length + c->outbuflen + c->outbufstart > c->outbufsize) {
58 memmove(c->outbuf, c->outbuf + c->outbufstart, c->outbuflen);
62 /* Add our data to buffer */
63 if(c->status.encryptout) {
64 result = EVP_EncryptUpdate(c->outctx, (unsigned char *)c->outbuf + c->outbufstart + c->outbuflen,
65 &outlen, (unsigned char *)buffer, length);
66 if(!result || outlen < length) {
67 logger(LOG_ERR, "Error while encrypting metadata to %s (%s): %s",
68 c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
70 } else if(outlen > length) {
71 logger(LOG_EMERG, "Encrypted data too long! Heap corrupted!");
74 c->outbuflen += outlen;
76 memcpy(c->outbuf + c->outbufstart + c->outbuflen, buffer, length);
77 c->outbuflen += length;
83 bool flush_meta(connection_t *c) {
86 ifdebug(META) logger(LOG_DEBUG, "Flushing %d bytes to %s (%s)",
87 c->outbuflen, c->name, c->hostname);
90 result = send(c->socket, c->outbuf + c->outbufstart, c->outbuflen, 0);
92 if(!errno || errno == EPIPE) {
93 ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)",
94 c->name, c->hostname);
95 } else if(errno == EINTR) {
98 } else if(errno == EWOULDBLOCK) {
99 ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Flushing %d bytes to %s (%s) would block",
100 c->outbuflen, c->name, c->hostname);
104 logger(LOG_ERR, "Flushing meta data to %s (%s) failed: %s", c->name,
105 c->hostname, strerror(errno));
111 c->outbufstart += result;
112 c->outbuflen -= result;
115 c->outbufstart = 0; /* avoid unnecessary memmoves */
119 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) {
132 int oldlen, i, result;
133 int lenin, lenout, reqlen;
134 bool decrypted = false;
135 char inbuf[MAXBUFSIZE];
138 - Read as much as possible from the TCP socket in one go.
140 - Check if a full request is in the input buffer.
141 - If yes, process request and remove it from the buffer,
143 - If not, keep stuff in buffer and exit.
146 lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0);
149 if(!lenin || !errno) {
150 ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)",
151 c->name, c->hostname);
152 } else if(errno == EINTR)
155 logger(LOG_ERR, "Metadata socket read error for %s (%s): %s",
156 c->name, c->hostname, strerror(errno));
167 if(c->status.decryptin && !decrypted) {
168 result = EVP_DecryptUpdate(c->inctx, (unsigned char *)inbuf, &lenout, (unsigned char *)c->buffer + oldlen, lenin);
169 if(!result || lenout != lenin) {
170 logger(LOG_ERR, "Error while decrypting metadata from %s (%s): %s",
171 c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
174 memcpy(c->buffer + oldlen, inbuf, lenin);
178 /* Are we receiving a TCPpacket? */
181 if(c->tcplen <= c->buflen) {
182 receive_tcppacket(c, c->buffer, c->tcplen);
184 c->buflen -= c->tcplen;
185 lenin -= c->tcplen - oldlen;
186 memmove(c->buffer, c->buffer + c->tcplen, c->buflen);
195 /* Otherwise we are waiting for a request */
199 for(i = oldlen; i < c->buflen; i++) {
200 if(c->buffer[i] == '\n') {
201 c->buffer[i] = '\0'; /* replace end-of-line by end-of-string so we can use sscanf */
209 if(!receive_request(c))
213 lenin -= reqlen - oldlen;
214 memmove(c->buffer, c->buffer + reqlen, c->buflen);
222 if(c->buflen >= MAXBUFSIZE) {
223 logger(LOG_ERR, "Metadata read buffer overflow for %s (%s)",
224 c->name, c->hostname);