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.20 2001/07/20 13:54:19 guus Exp $
29 #include <sys/signal.h>
32 /* This line must be below the rest for FreeBSD */
33 #include <sys/socket.h>
35 #include <openssl/evp.h>
38 #include "connection.h"
42 int send_meta(connection_t *cl, char *buffer, int length)
46 char outbuf[MAXBUFSIZE];
48 if(debug_lvl >= DEBUG_META)
49 syslog(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length,
50 cl->name, cl->hostname);
52 if(cl->status.encryptout)
54 EVP_EncryptUpdate(cl->cipher_outctx, outbuf, &outlen, buffer, length);
61 if(write(cl->meta_socket, bufp, length) < 0)
63 syslog(LOG_ERR, _("Sending meta data to %s (%s) failed: %m"), cl->name, cl->hostname);
70 void broadcast_meta(connection_t *cl, char *buffer, int length)
75 for(node = connection_tree->head; node; node = node->next)
77 p = (connection_t *)node->data;
78 if(p != cl && p->status.active)
79 send_meta(p, buffer, length);
84 int receive_meta(connection_t *cl)
90 char inbuf[MAXBUFSIZE];
92 if(getsockopt(cl->meta_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
94 syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m %s (%s)"), __FILE__, __LINE__, cl->meta_socket,
95 cl->name, cl->hostname);
100 syslog(LOG_ERR, _("Metadata socket error for %s (%s): %s"),
101 cl->name, cl->hostname, strerror(x));
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 = read(cl->meta_socket, cl->buffer + cl->buflen, MAXBUFSIZE - cl->buflen);
120 if(debug_lvl >= DEBUG_CONNECTIONS)
121 syslog(LOG_NOTICE, _("Connection closed by %s (%s)"),
122 cl->name, cl->hostname);
128 syslog(LOG_ERR, _("Metadata socket read error for %s (%s): %m"),
129 cl->name, cl->hostname);
141 if(cl->status.decryptin && !decrypted)
143 EVP_DecryptUpdate(cl->cipher_inctx, inbuf, &lenin, cl->buffer + oldlen, lenin);
144 memcpy(cl->buffer + oldlen, inbuf, lenin);
148 /* Are we receiving a TCPpacket? */
152 if(cl->tcplen <= cl->buflen)
154 receive_tcppacket(cl, cl->buffer, cl->tcplen);
156 cl->buflen -= cl->tcplen;
158 memmove(cl->buffer, cl->buffer + cl->tcplen, cl->buflen);
169 /* Otherwise we are waiting for a request */
173 for(i = oldlen; i < cl->buflen; i++)
175 if(cl->buffer[i] == '\n')
177 cl->buffer[i] = '\0'; /* replace end-of-line by end-of-string so we can use sscanf */
185 if(receive_request(cl))
188 cl->buflen -= reqlen;
190 memmove(cl->buffer, cl->buffer + reqlen, cl->buflen);
200 if(cl->buflen >= MAXBUFSIZE)
202 syslog(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
203 cl->name, cl->hostname);
207 cl->last_ping_time = time(NULL);