2 meta.c -- handle the meta communication
3 Copyright (C) 2000,2001 Guus Sliepen <guus@sliepen.warande.net>,
4 2000,2001 Ivo Timmermans <itimmermans@bigfoot.com>
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.18 2001/05/25 11:54:28 guus Exp $
29 #include <sys/signal.h>
32 /* This line must be below the rest for FreeBSD */
33 #include <sys/socket.h>
35 #ifdef HAVE_OPENSSL_EVP_H
36 # include <openssl/evp.h>
42 #include "connection.h"
46 int send_meta(connection_t *cl, char *buffer, int length)
50 char outbuf[MAXBUFSIZE];
52 if(debug_lvl >= DEBUG_META)
53 syslog(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length,
54 cl->name, cl->hostname);
56 if(cl->status.encryptout)
58 EVP_EncryptUpdate(cl->cipher_outctx, outbuf, &outlen, buffer, length);
65 if(write(cl->meta_socket, bufp, length) < 0)
67 syslog(LOG_ERR, _("Sending meta data to %s (%s) failed: %m"), cl->name, cl->hostname);
74 void broadcast_meta(connection_t *cl, char *buffer, int length)
79 for(node = connection_tree->head; node; node = node->next)
81 p = (connection_t *)node->data;
82 if(p != cl && p->status.meta && p->status.active)
83 send_meta(p, buffer, length);
88 int receive_meta(connection_t *cl)
94 char inbuf[MAXBUFSIZE];
96 if(getsockopt(cl->meta_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
98 syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m %s (%s)"), __FILE__, __LINE__, cl->meta_socket,
99 cl->name, cl->hostname);
104 syslog(LOG_ERR, _("Metadata socket error for %s (%s): %s"),
105 cl->name, cl->hostname, strerror(x));
110 - Read as much as possible from the TCP socket in one go.
112 - Check if a full request is in the input buffer.
113 - If yes, process request and remove it from the buffer,
115 - If not, keep stuff in buffer and exit.
118 lenin = read(cl->meta_socket, cl->buffer + cl->buflen, MAXBUFSIZE - cl->buflen);
124 if(debug_lvl >= DEBUG_CONNECTIONS)
125 syslog(LOG_NOTICE, _("Connection closed by %s (%s)"),
126 cl->name, cl->hostname);
132 syslog(LOG_ERR, _("Metadata socket read error for %s (%s): %m"),
133 cl->name, cl->hostname);
145 if(cl->status.decryptin && !decrypted)
147 EVP_DecryptUpdate(cl->cipher_inctx, inbuf, &lenin, cl->buffer + oldlen, lenin);
148 memcpy(cl->buffer + oldlen, inbuf, lenin);
152 /* Are we receiving a TCPpacket? */
156 if(cl->tcplen <= cl->buflen)
158 receive_tcppacket(cl, cl->buffer, cl->tcplen);
160 cl->buflen -= cl->tcplen;
162 memmove(cl->buffer, cl->buffer + cl->tcplen, cl->buflen);
173 /* Otherwise we are waiting for a request */
177 for(i = oldlen; i < cl->buflen; i++)
179 if(cl->buffer[i] == '\n')
181 cl->buffer[i] = '\0'; /* replace end-of-line by end-of-string so we can use sscanf */
189 if(receive_request(cl))
192 cl->buflen -= reqlen;
194 memmove(cl->buffer, cl->buffer + reqlen, cl->buflen);
204 if(cl->buflen >= MAXBUFSIZE)
206 syslog(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
207 cl->name, cl->hostname);
211 cl->last_ping_time = time(NULL);