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.50 2003/11/17 15:30:17 guus Exp $
25 #include <gnutls/gnutls.h>
28 #include "connection.h"
36 bool send_meta(connection_t *c, const char *buffer, int length)
42 ifdebug(META) logger(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length,
43 c->name, c->hostname);
46 result = gnutls_record_send(c->session, buffer, length);
50 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
51 c->name, c->hostname);
52 } else if(result == GNUTLS_E_INTERRUPTED || result == GNUTLS_E_AGAIN)
55 logger(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name,
56 c->hostname, gnutls_strerror(result));
66 void broadcast_meta(connection_t *from, const char *buffer, int length)
73 for(node = connection_tree->head; node; node = node->next) {
76 if(c != from && c->status.active)
77 send_meta(c, buffer, length);
81 bool receive_meta(connection_t *c)
83 int oldlen, i, result;
89 - Read as much as possible from the TCP socket in one go.
90 - Check if a full request is in the input buffer.
91 - If yes, process request and remove it from the buffer,
93 - If not, keep stuff in buffer and exit.
96 if(c->allow_request == ID) {
97 logger(LOG_DEBUG, _("Continuing handshake..."));
98 result = gnutls_handshake(c->session);
100 logger(LOG_DEBUG, _("Handshake with %s (%s) completed!"), c->name, c->hostname);
101 c->allow_request = ACK;
104 if(result == GNUTLS_E_INTERRUPTED || result == GNUTLS_E_AGAIN)
106 logger(LOG_DEBUG, _("Handshake with %s (%s) failed: %s"), c->name, c->hostname, gnutls_strerror(result));
110 result = gnutls_record_recv(c->session, c->buffer + c->buflen, MAXBUFSIZE - c->buflen);
114 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
115 c->name, c->hostname);
116 } else if(result == GNUTLS_E_INTERRUPTED || result == GNUTLS_E_AGAIN)
119 logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
120 c->name, c->hostname, gnutls_strerror(result));
128 while(c->buflen > 0) {
129 /* Are we receiving a TCPpacket? */
132 if(c->tcplen <= c->buflen) {
133 receive_tcppacket(c, c->buffer, c->tcplen);
135 c->buflen -= c->tcplen;
136 memmove(c->buffer, c->buffer + c->tcplen, c->buflen);
145 /* Otherwise we are waiting for a request */
149 for(i = oldlen; i < c->buflen; i++) {
150 if(c->buffer[i] == '\n') {
151 c->buffer[i] = '\0'; /* replace end-of-line by end-of-string so we can use sscanf */
159 if(!receive_request(c))
163 memmove(c->buffer, c->buffer + reqlen, c->buflen);
171 if(c->buflen >= MAXBUFSIZE) {
172 logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
173 c->name, c->hostname);
177 c->last_ping_time = now;