2 meta.c -- handle the meta communication
3 Copyright (C) 2000 Guus Sliepen <guus@sliepen.warande.net>,
4 2000 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.2 2000/10/11 10:35:15 guus Exp $
28 #include <sys/signal.h>
29 #include <sys/socket.h>
30 #include <openssl/evp.h>
35 int send_meta(conn_list_t *cl, char *buffer, int length)
37 char outbuf[MAXBUFSIZE];
41 if(debug_lvl >= DEBUG_META)
42 syslog(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s): %s"), length,
43 cl->name, cl->hostname, buffer);
45 if(cl->status.encryptout)
47 EVP_EncryptUpdate(cl->cipher_outctx, outbuf, &outlen, buffer, length);
53 if(write(cl->meta_socket, bufp, length) < 0)
55 syslog(LOG_ERR, _("Sending meta data to %s (%s) failed: %m"), cl->name, cl->hostname);
62 int broadcast_meta(conn_list_t *cl, const char *buffer, int length)
66 for(p = conn_list; p != NULL; p = p->next)
67 if(p != cl && p->status.meta && p->status.active)
68 send_meta(p, buffer, length);
73 int receive_meta(conn_list_t *cl)
78 char inbuf[MAXBUFSIZE];
81 if(getsockopt(cl->meta_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
83 syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m %s (%s)"), __FILE__, __LINE__, cl->meta_socket,
84 cl->name, cl->hostname);
89 syslog(LOG_ERR, _("Metadata socket error for %s (%s): %s"),
90 cl->name, cl->hostname, strerror(x));
94 if(cl->status.decryptin)
97 bufp = cl->buffer + cl->buflen;
99 lenin = read(cl->meta_socket, bufp, MAXBUFSIZE - cl->buflen);
107 if(debug_lvl>DEBUG_CONNECTIONS)
108 syslog(LOG_NOTICE, _("Connection closed by %s (%s)"),
109 cl->name, cl->hostname);
112 syslog(LOG_ERR, _("Metadata socket read error for %s (%s): %m"),
113 cl->name, cl->hostname);
117 if(cl->status.decryptin)
119 EVP_DecryptUpdate(cl->cipher_inctx, cl->buffer + cl->buflen, NULL, inbuf, lenin);
129 for(i = oldlen; i < cl->buflen; i++)
131 if(cl->buffer[i] == '\n')
133 cl->buffer[i] = 0; /* replace end-of-line by end-of-string so we can use sscanf */
141 if(debug_lvl > DEBUG_META)
142 syslog(LOG_DEBUG, _("Got request from %s (%s): %s"),
143 cl->name, cl->hostname, cl->buffer);
145 if(receive_request(cl))
148 cl->buflen -= cl->reqlen;
149 memmove(cl->buffer, cl->buffer + cl->reqlen, cl->buflen);
158 if(cl->buflen >= MAXBUFSIZE)
160 syslog(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
161 cl->name, cl->hostname);
165 cl->last_ping_time = time(NULL);