2 meta.c -- handle the meta communication
3 Copyright (C) 2000-2003 Guus Sliepen <guus@sliepen.eu.org>,
4 2000-2003 Ivo Timmermans <ivo@o2w.nl>
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.
20 $Id: meta.c,v 1.1.2.45 2003/10/10 16:24:24 guus Exp $
25 #include <openssl/evp.h>
28 #include "connection.h"
36 bool send_meta(connection_t *c, const char *buffer, int length)
40 char outbuf[MAXBUFSIZE];
45 ifdebug(META) logger(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length,
46 c->name, c->hostname);
48 if(c->status.encryptout) {
49 result = EVP_EncryptUpdate(c->outctx, outbuf, &outlen, buffer, length);
50 if(!result || outlen != length) {
51 logger(LOG_ERR, _("Error while encrypting metadata to %s (%s): %s"), ERR_error_string(ERR_get_error(), NULL));
60 result = send(c->socket, bufp, length, 0);
62 if(!errno || errno == EPIPE) {
63 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
64 c->name, c->hostname);
65 } else if(errno == EINTR)
68 logger(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name,
69 c->hostname, strerror(errno));
79 void broadcast_meta(connection_t *from, const char *buffer, int length)
86 for(node = connection_tree->head; node; node = node->next) {
89 if(c != from && c->status.active)
90 send_meta(c, buffer, length);
94 bool receive_meta(connection_t *c)
96 int oldlen, i, result;
97 int lenin, lenout, reqlen;
98 bool decrypted = false;
99 char inbuf[MAXBUFSIZE];
104 - Read as much as possible from the TCP socket in one go.
106 - Check if a full request is in the input buffer.
107 - If yes, process request and remove it from the buffer,
109 - If not, keep stuff in buffer and exit.
112 lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0);
115 if(!lenin || !errno) {
116 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
117 c->name, c->hostname);
118 } else if(errno == EINTR)
121 logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
122 c->name, c->hostname, strerror(errno));
133 if(c->status.decryptin && !decrypted) {
134 result = EVP_DecryptUpdate(c->inctx, inbuf, &lenout, c->buffer + oldlen, lenin);
135 if(!result || lenout != lenin) {
136 logger(LOG_ERR, _("Error while decrypting metadata from %s (%s): %s"), ERR_error_string(ERR_get_error(), NULL));
139 memcpy(c->buffer + oldlen, inbuf, lenin);
143 /* Are we receiving a TCPpacket? */
146 if(c->tcplen <= c->buflen) {
147 receive_tcppacket(c, c->buffer, c->tcplen);
149 c->buflen -= c->tcplen;
151 memmove(c->buffer, c->buffer + c->tcplen, c->buflen);
160 /* Otherwise we are waiting for a request */
164 for(i = oldlen; i < c->buflen; i++) {
165 if(c->buffer[i] == '\n') {
166 c->buffer[i] = '\0'; /* replace end-of-line by end-of-string so we can use sscanf */
174 if(!receive_request(c))
179 memmove(c->buffer, c->buffer + reqlen, c->buflen);
187 if(c->buflen >= MAXBUFSIZE) {
188 logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
189 c->name, c->hostname);
193 c->last_ping_time = now;