2 meta.c -- handle the meta communication
3 Copyright (C) 2000-2002 Guus Sliepen <guus@sliepen.warande.net>,
4 2000-2002 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 2002/04/28 12:46:26 zarq Exp $
30 /* This line must be below the rest for FreeBSD */
31 #include <sys/types.h>
32 #include <sys/socket.h>
34 #include <openssl/evp.h>
37 #include "connection.h"
38 #include "interface.h"
43 int send_meta(connection_t *c, char *buffer, int length)
47 char outbuf[MAXBUFSIZE];
49 log(DEBUG_META, TLOG_DEBUG,
50 _("Sending %d bytes of metadata to %s (%s)"),
51 length, c->name, c->hostname);
53 if(c->status.encryptout)
55 EVP_EncryptUpdate(c->outctx, outbuf, &outlen, buffer, length);
62 if(write(c->socket, bufp, length) < 0)
64 syslog(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name, c->hostname, strerror(errno));
71 void broadcast_meta(connection_t *from, char *buffer, int length)
76 for(node = connection_tree->head; node; node = node->next)
78 c = (connection_t *)node->data;
79 if(c != from && c->status.active)
80 send_meta(c, buffer, length);
85 int receive_meta(connection_t *c)
91 char inbuf[MAXBUFSIZE];
93 if(getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
95 syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%s %s (%s)"), __FILE__, __LINE__, c->socket, strerror(errno),
96 c->name, c->hostname);
101 syslog(LOG_ERR, _("Metadata socket error for %s (%s): %s"),
102 c->name, c->hostname, strerror(x));
107 - Read as much as possible from the TCP socket in one go.
109 - Check if a full request is in the input buffer.
110 - If yes, process request and remove it from the buffer,
112 - If not, keep stuff in buffer and exit.
115 lenin = read(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen);
121 if(debug_lvl >= DEBUG_CONNECTIONS)
122 syslog(LOG_NOTICE, _("Connection closed by %s (%s)"),
123 c->name, c->hostname);
129 syslog(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
130 c->name, c->hostname, strerror(errno));
142 if(c->status.decryptin && !decrypted)
144 EVP_DecryptUpdate(c->inctx, inbuf, &lenin, c->buffer + oldlen, lenin);
145 memcpy(c->buffer + oldlen, inbuf, lenin);
149 /* Otherwise we are waiting for a request */
153 for(i = oldlen; i < c->buflen; i++)
155 if(c->buffer[i] == '\n')
157 c->buffer[i] = '\0'; /* replace end-of-line by end-of-string so we can use sscanf */
165 if(receive_request(c))
170 memmove(c->buffer, c->buffer + reqlen, c->buflen);
180 if(c->buflen >= MAXBUFSIZE)
182 syslog(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
183 c->name, c->hostname);
187 c->last_ping_time = now;