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 c->last_flushed_time = now;
50 /* Find room in connection's buffer */
51 if(length + c->outbuflen > c->outbufsize) {
52 c->outbufsize = length + c->outbuflen;
53 c->outbuf = xrealloc(c->outbuf, c->outbufsize);
56 if(length + c->outbuflen + c->outbufstart > c->outbufsize) {
57 memmove(c->outbuf, c->outbuf + c->outbufstart, c->outbuflen);
61 /* Add our data to buffer */
62 if(c->status.encryptout) {
63 result = EVP_EncryptUpdate(c->outctx, (unsigned char *)c->outbuf + c->outbufstart + c->outbuflen,
64 &outlen, (unsigned char *)buffer, length);
65 if(!result || outlen < length) {
66 logger(LOG_ERR, _("Error while encrypting metadata to %s (%s): %s"),
67 c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
69 } else if(outlen > length) {
70 logger(LOG_EMERG, _("Encrypted data too long! Heap corrupted!"));
73 c->outbuflen += outlen;
75 memcpy(c->outbuf + c->outbufstart + c->outbuflen, buffer, length);
76 c->outbuflen += length;
82 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)
126 for(node = connection_tree->head; node; node = node->next) {
129 if(c != from && c->status.active)
130 send_meta(c, buffer, length);
134 bool receive_meta(connection_t *c)
136 int oldlen, i, result;
137 int lenin, lenout, reqlen;
138 bool decrypted = false;
139 char inbuf[MAXBUFSIZE];
144 - Read as much as possible from the TCP socket in one go.
146 - Check if a full request is in the input buffer.
147 - If yes, process request and remove it from the buffer,
149 - If not, keep stuff in buffer and exit.
152 lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0);
155 if(!lenin || !errno) {
156 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
157 c->name, c->hostname);
158 } else if(errno == EINTR)
161 logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
162 c->name, c->hostname, strerror(errno));
173 if(c->status.decryptin && !decrypted) {
174 result = EVP_DecryptUpdate(c->inctx, (unsigned char *)inbuf, &lenout, (unsigned char *)c->buffer + oldlen, lenin);
175 if(!result || lenout != lenin) {
176 logger(LOG_ERR, _("Error while decrypting metadata from %s (%s): %s"),
177 c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
180 memcpy(c->buffer + oldlen, inbuf, lenin);
184 /* Are we receiving a TCPpacket? */
187 if(c->tcplen <= c->buflen) {
188 receive_tcppacket(c, c->buffer, c->tcplen);
190 c->buflen -= c->tcplen;
191 lenin -= c->tcplen - oldlen;
192 memmove(c->buffer, c->buffer + c->tcplen, c->buflen);
201 /* Otherwise we are waiting for a request */
205 for(i = oldlen; i < c->buflen; i++) {
206 if(c->buffer[i] == '\n') {
207 c->buffer[i] = '\0'; /* replace end-of-line by end-of-string so we can use sscanf */
215 if(!receive_request(c))
219 lenin -= reqlen - oldlen;
220 memmove(c->buffer, c->buffer + reqlen, c->buflen);
228 if(c->buflen >= MAXBUFSIZE) {
229 logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
230 c->name, c->hostname);
234 c->last_ping_time = now;