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.42 2003/08/16 12:40:00 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 EVP_EncryptUpdate(c->outctx, outbuf, &outlen, buffer, length);
56 result = send(c->socket, bufp, length, 0);
58 if(!errno || errno == EPIPE) {
59 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
60 c->name, c->hostname);
61 } else if(errno == EINTR)
64 logger(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name,
65 c->hostname, strerror(errno));
75 void broadcast_meta(connection_t *from, const char *buffer, int length)
82 for(node = connection_tree->head; node; node = node->next) {
83 c = (connection_t *) node->data;
85 if(c != from && c->status.active)
86 send_meta(c, buffer, length);
90 bool receive_meta(connection_t *c)
93 socklen_t l = sizeof(x);
96 bool decrypted = false;
97 char inbuf[MAXBUFSIZE];
102 - Read as much as possible from the TCP socket in one go.
104 - Check if a full request is in the input buffer.
105 - If yes, process request and remove it from the buffer,
107 - If not, keep stuff in buffer and exit.
110 lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0);
113 if(!lenin || !errno) {
114 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
115 c->name, c->hostname);
116 } else if(errno == EINTR)
119 logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
120 c->name, c->hostname, strerror(errno));
131 if(c->status.decryptin && !decrypted) {
132 EVP_DecryptUpdate(c->inctx, inbuf, &lenin, c->buffer + oldlen, lenin);
133 memcpy(c->buffer + oldlen, inbuf, lenin);
137 /* Are we receiving a TCPpacket? */
140 if(c->tcplen <= c->buflen) {
141 receive_tcppacket(c, c->buffer, c->tcplen);
143 c->buflen -= c->tcplen;
145 memmove(c->buffer, c->buffer + c->tcplen, c->buflen);
154 /* Otherwise we are waiting for a request */
158 for(i = oldlen; i < c->buflen; i++) {
159 if(c->buffer[i] == '\n') {
160 c->buffer[i] = '\0'; /* replace end-of-line by end-of-string so we can use sscanf */
168 if(!receive_request(c))
173 memmove(c->buffer, c->buffer + reqlen, c->buflen);
181 if(c->buflen >= MAXBUFSIZE) {
182 logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
183 c->name, c->hostname);
187 c->last_ping_time = now;