Imported gnutls based branch.
[tinc] / src / meta.c
1 /*
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>
5
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.
10
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.
15
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.
19
20     $Id: meta.c,v 1.1.2.50 2003/11/17 15:30:17 guus Exp $
21 */
22
23 #include "system.h"
24
25 #include <gnutls/gnutls.h>
26
27 #include "avl_tree.h"
28 #include "connection.h"
29 #include "logger.h"
30 #include "meta.h"
31 #include "net.h"
32 #include "protocol.h"
33 #include "system.h"
34 #include "utils.h"
35
36 bool send_meta(connection_t *c, const char *buffer, int length)
37 {
38         int result;
39
40         cp();
41
42         ifdebug(META) logger(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length,
43                            c->name, c->hostname);
44
45         while(length) {
46                 result = gnutls_record_send(c->session, buffer, length);
47
48                 if(result <= 0) {
49                         if(!result) {
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)
53                                 continue;
54                         else
55                                 logger(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name,
56                                            c->hostname, gnutls_strerror(result));
57                         return false;
58                 }
59                 buffer += result;
60                 length -= result;
61         }
62         
63         return true;
64 }
65
66 void broadcast_meta(connection_t *from, const char *buffer, int length)
67 {
68         avl_node_t *node;
69         connection_t *c;
70
71         cp();
72
73         for(node = connection_tree->head; node; node = node->next) {
74                 c = node->data;
75
76                 if(c != from && c->status.active)
77                         send_meta(c, buffer, length);
78         }
79 }
80
81 bool receive_meta(connection_t *c)
82 {
83         int oldlen, i, result;
84         int reqlen;
85
86         cp();
87
88         /* Strategy:
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,
92            then check again.
93            - If not, keep stuff in buffer and exit.
94          */
95
96         if(c->allow_request == ID) {
97                 logger(LOG_DEBUG, _("Continuing handshake..."));
98                 result = gnutls_handshake(c->session);
99                 if(!result) {
100                         logger(LOG_DEBUG, _("Handshake with %s (%s) completed!"), c->name, c->hostname);
101                         c->allow_request = ACK;
102                         return send_ack(c);
103                 }
104                 if(result == GNUTLS_E_INTERRUPTED || result == GNUTLS_E_AGAIN)
105                         return true;
106                 logger(LOG_DEBUG, _("Handshake with %s (%s) failed: %s"), c->name, c->hostname, gnutls_strerror(result));
107                 return false;
108         }
109
110         result = gnutls_record_recv(c->session, c->buffer + c->buflen, MAXBUFSIZE - c->buflen);
111
112         if(result <= 0) {
113                 if(!result) {
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)
117                         return true;
118                 else
119                         logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
120                                    c->name, c->hostname, gnutls_strerror(result));
121
122                 return false;
123         }
124
125         oldlen = c->buflen;
126         c->buflen += result;
127
128         while(c->buflen > 0) {
129                 /* Are we receiving a TCPpacket? */
130
131                 if(c->tcplen) {
132                         if(c->tcplen <= c->buflen) {
133                                 receive_tcppacket(c, c->buffer, c->tcplen);
134
135                                 c->buflen -= c->tcplen;
136                                 memmove(c->buffer, c->buffer + c->tcplen, c->buflen);
137                                 oldlen = 0;
138                                 c->tcplen = 0;
139                                 continue;
140                         } else {
141                                 break;
142                         }
143                 }
144
145                 /* Otherwise we are waiting for a request */
146
147                 reqlen = 0;
148
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 */
152                                 reqlen = i + 1;
153                                 break;
154                         }
155                 }
156
157                 if(reqlen) {
158                         c->reqlen = reqlen;
159                         if(!receive_request(c))
160                                 return false;
161
162                         c->buflen -= reqlen;
163                         memmove(c->buffer, c->buffer + reqlen, c->buflen);
164                         oldlen = 0;
165                         continue;
166                 } else {
167                         break;
168                 }
169         }
170
171         if(c->buflen >= MAXBUFSIZE) {
172                 logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
173                            c->name, c->hostname);
174                 return false;
175         }
176
177         c->last_ping_time = now;
178
179         return true;
180 }