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 <openssl/err.h>
26 #include <openssl/evp.h>
29 #include "connection.h"
37 bool send_meta(connection_t *c, const char *buffer, int length)
44 ifdebug(META) logger(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length,
45 c->name, c->hostname);
48 if(event_add(&c->outev, NULL) < 0) {
49 logger(LOG_EMERG, _("event_add failed: %s"), strerror(errno));
54 /* Find room in connection's buffer */
55 if(length + c->outbuflen > c->outbufsize) {
56 c->outbufsize = length + c->outbuflen;
57 c->outbuf = xrealloc(c->outbuf, c->outbufsize);
60 if(length + c->outbuflen + c->outbufstart > c->outbufsize) {
61 memmove(c->outbuf, c->outbuf + c->outbufstart, c->outbuflen);
65 /* Add our data to buffer */
66 if(c->status.encryptout) {
67 result = EVP_EncryptUpdate(c->outctx, (unsigned char *)c->outbuf + c->outbufstart + c->outbuflen,
68 &outlen, (unsigned char *)buffer, length);
69 if(!result || outlen < length) {
70 logger(LOG_ERR, _("Error while encrypting metadata to %s (%s): %s"),
71 c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
73 } else if(outlen > length) {
74 logger(LOG_EMERG, _("Encrypted data too long! Heap corrupted!"));
77 c->outbuflen += outlen;
79 memcpy(c->outbuf + c->outbufstart + c->outbuflen, buffer, length);
80 c->outbuflen += length;
86 void flush_meta(int fd, short events, void *data)
88 connection_t *c = data;
91 ifdebug(META) logger(LOG_DEBUG, _("Flushing %d bytes to %s (%s)"),
92 c->outbuflen, c->name, c->hostname);
95 result = send(c->socket, c->outbuf + c->outbufstart, c->outbuflen, 0);
97 if(!errno || errno == EPIPE) {
98 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
99 c->name, c->hostname);
100 } else if(errno == EINTR) {
103 } else if(errno == EWOULDBLOCK) {
104 ifdebug(CONNECTIONS) logger(LOG_DEBUG, _("Flushing %d bytes to %s (%s) would block"),
105 c->outbuflen, c->name, c->hostname);
109 logger(LOG_ERR, _("Flushing meta data to %s (%s) failed: %s"), c->name,
110 c->hostname, strerror(errno));
113 terminate_connection(c, c->status.active);
117 c->outbufstart += result;
118 c->outbuflen -= result;
121 event_del(&c->outev);
123 c->outbufstart = 0; /* avoid unnecessary memmoves */
126 void broadcast_meta(connection_t *from, const char *buffer, int length)
133 for(node = connection_tree->head; node; node = node->next) {
136 if(c != from && c->status.active)
137 send_meta(c, buffer, length);
141 bool receive_meta(connection_t *c)
143 int oldlen, i, result;
144 int lenin, lenout, reqlen;
145 bool decrypted = false;
146 char inbuf[MAXBUFSIZE];
151 - Read as much as possible from the TCP socket in one go.
153 - Check if a full request is in the input buffer.
154 - If yes, process request and remove it from the buffer,
156 - If not, keep stuff in buffer and exit.
159 lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0);
162 if(!lenin || !errno) {
163 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
164 c->name, c->hostname);
165 } else if(errno == EINTR)
168 logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
169 c->name, c->hostname, strerror(errno));
180 if(c->status.decryptin && !decrypted) {
181 result = EVP_DecryptUpdate(c->inctx, (unsigned char *)inbuf, &lenout, (unsigned char *)c->buffer + oldlen, lenin);
182 if(!result || lenout != lenin) {
183 logger(LOG_ERR, _("Error while decrypting metadata from %s (%s): %s"),
184 c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
187 memcpy(c->buffer + oldlen, inbuf, lenin);
191 /* Are we receiving a TCPpacket? */
194 if(c->tcplen <= c->buflen) {
195 receive_tcppacket(c, c->buffer, c->tcplen);
197 c->buflen -= c->tcplen;
198 lenin -= c->tcplen - oldlen;
199 memmove(c->buffer, c->buffer + c->tcplen, c->buflen);
208 /* Otherwise we are waiting for a request */
212 for(i = oldlen; i < c->buflen; i++) {
213 if(c->buffer[i] == '\n') {
214 c->buffer[i] = '\0'; /* replace end-of-line by end-of-string so we can use sscanf */
222 if(!receive_request(c))
226 lenin -= reqlen - oldlen;
227 memmove(c->buffer, c->buffer + reqlen, c->buflen);
235 if(c->buflen >= MAXBUFSIZE) {
236 logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
237 c->name, c->hostname);
241 c->last_ping_time = time(NULL);