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.49 2003/11/10 22:31:53 guus Exp $
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)
41 char outbuf[MAXBUFSIZE];
46 ifdebug(META) logger(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length,
47 c->name, c->hostname);
49 if(c->status.encryptout) {
50 result = EVP_EncryptUpdate(c->outctx, outbuf, &outlen, buffer, length);
51 if(!result || outlen != length) {
52 logger(LOG_ERR, _("Error while encrypting metadata to %s (%s): %s"),
53 c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
62 result = send(c->socket, bufp, length, 0);
64 if(!errno || errno == EPIPE) {
65 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
66 c->name, c->hostname);
67 } else if(errno == EINTR)
70 logger(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name,
71 c->hostname, strerror(errno));
81 void broadcast_meta(connection_t *from, const char *buffer, int length)
88 for(node = connection_tree->head; node; node = node->next) {
91 if(c != from && c->status.active && !c->status.opaque)
92 send_meta(c, buffer, length);
96 bool receive_meta(connection_t *c)
98 int oldlen, i, result;
99 int lenin, lenout, reqlen;
100 bool decrypted = false;
101 char inbuf[MAXBUFSIZE];
106 - Read as much as possible from the TCP socket in one go.
108 - Check if a full request is in the input buffer.
109 - If yes, process request and remove it from the buffer,
111 - If not, keep stuff in buffer and exit.
114 lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0);
117 if(!lenin || !errno) {
118 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
119 c->name, c->hostname);
120 } else if(errno == EINTR)
123 logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
124 c->name, c->hostname, strerror(errno));
135 if(c->status.decryptin && !decrypted) {
136 result = EVP_DecryptUpdate(c->inctx, inbuf, &lenout, c->buffer + oldlen, lenin);
137 if(!result || lenout != lenin) {
138 logger(LOG_ERR, _("Error while decrypting metadata from %s (%s): %s"),
139 c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
142 memcpy(c->buffer + oldlen, inbuf, lenin);
146 /* Are we receiving a TCPpacket? */
149 if(c->tcplen <= c->buflen) {
150 receive_tcppacket(c, c->buffer, c->tcplen);
152 c->buflen -= c->tcplen;
153 lenin -= c->tcplen - oldlen;
154 memmove(c->buffer, c->buffer + c->tcplen, c->buflen);
163 /* Otherwise we are waiting for a request */
167 for(i = oldlen; i < c->buflen; i++) {
168 if(c->buffer[i] == '\n') {
169 c->buffer[i] = '\0'; /* replace end-of-line by end-of-string so we can use sscanf */
177 if(!receive_request(c))
181 lenin -= reqlen - oldlen;
182 memmove(c->buffer, c->buffer + reqlen, c->buflen);
190 if(c->buflen >= MAXBUFSIZE) {
191 logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
192 c->name, c->hostname);
196 c->last_ping_time = now;