Remove checkpoint tracing.
[oweals/tinc.git] / src / meta.c
1 /*
2     meta.c -- handle the meta communication
3     Copyright (C) 2000-2009 Guus Sliepen <guus@tinc-vpn.org>,
4                   2000-2005 Ivo Timmermans
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 along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include <openssl/err.h>
24 #include <openssl/evp.h>
25
26 #include "avl_tree.h"
27 #include "connection.h"
28 #include "logger.h"
29 #include "meta.h"
30 #include "net.h"
31 #include "protocol.h"
32 #include "utils.h"
33 #include "xalloc.h"
34
35 bool send_meta(connection_t *c, const char *buffer, int length) {
36         int outlen;
37         int result;
38
39         if(!c) {
40                 logger(LOG_ERR, _("send_meta() called with NULL pointer!"));
41                 abort();
42         }
43
44         ifdebug(META) logger(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length,
45                            c->name, c->hostname);
46
47         if(!c->outbuflen)
48                 c->last_flushed_time = now;
49
50         /* Find room in connection's buffer */
51         if(length + c->outbuflen > c->outbufsize) {
52                 c->outbufsize = length + c->outbuflen;
53                 c->outbuf = xrealloc(c->outbuf, c->outbufsize);
54         }
55
56         if(length + c->outbuflen + c->outbufstart > c->outbufsize) {
57                 memmove(c->outbuf, c->outbuf + c->outbufstart, c->outbuflen);
58                 c->outbufstart = 0;
59         }
60
61         /* Add our data to buffer */
62         if(c->status.encryptout) {
63                 result = EVP_EncryptUpdate(c->outctx, (unsigned char *)c->outbuf + c->outbufstart + c->outbuflen,
64                                 &outlen, (unsigned char *)buffer, length);
65                 if(!result || outlen < length) {
66                         logger(LOG_ERR, _("Error while encrypting metadata to %s (%s): %s"),
67                                         c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
68                         return false;
69                 } else if(outlen > length) {
70                         logger(LOG_EMERG, _("Encrypted data too long! Heap corrupted!"));
71                         abort();
72                 }
73                 c->outbuflen += outlen;
74         } else {
75                 memcpy(c->outbuf + c->outbufstart + c->outbuflen, buffer, length);
76                 c->outbuflen += length;
77         }
78
79         return true;
80 }
81
82 bool flush_meta(connection_t *c) {
83         int result;
84         
85         ifdebug(META) logger(LOG_DEBUG, _("Flushing %d bytes to %s (%s)"),
86                          c->outbuflen, c->name, c->hostname);
87
88         while(c->outbuflen) {
89                 result = send(c->socket, c->outbuf + c->outbufstart, c->outbuflen, 0);
90                 if(result <= 0) {
91                         if(!errno || errno == EPIPE) {
92                                 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
93                                                    c->name, c->hostname);
94                         } else if(errno == EINTR) {
95                                 continue;
96 #ifdef EWOULDBLOCK
97                         } else if(errno == EWOULDBLOCK) {
98                                 ifdebug(CONNECTIONS) logger(LOG_DEBUG, _("Flushing %d bytes to %s (%s) would block"),
99                                                 c->outbuflen, c->name, c->hostname);
100                                 return true;
101 #endif
102                         } else {
103                                 logger(LOG_ERR, _("Flushing meta data to %s (%s) failed: %s"), c->name,
104                                            c->hostname, strerror(errno));
105                         }
106
107                         return false;
108                 }
109
110                 c->outbufstart += result;
111                 c->outbuflen -= result;
112         }
113
114         c->outbufstart = 0; /* avoid unnecessary memmoves */
115         return true;
116 }
117
118 void broadcast_meta(connection_t *from, const char *buffer, int length) {
119         avl_node_t *node;
120         connection_t *c;
121
122         for(node = connection_tree->head; node; node = node->next) {
123                 c = node->data;
124
125                 if(c != from && c->status.active)
126                         send_meta(c, buffer, length);
127         }
128 }
129
130 bool receive_meta(connection_t *c) {
131         int oldlen, i, result;
132         int lenin, lenout, reqlen;
133         bool decrypted = false;
134         char inbuf[MAXBUFSIZE];
135
136         /* Strategy:
137            - Read as much as possible from the TCP socket in one go.
138            - Decrypt it.
139            - Check if a full request is in the input buffer.
140            - If yes, process request and remove it from the buffer,
141            then check again.
142            - If not, keep stuff in buffer and exit.
143          */
144
145         lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0);
146
147         if(lenin <= 0) {
148                 if(!lenin || !errno) {
149                         ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
150                                            c->name, c->hostname);
151                 } else if(errno == EINTR)
152                         return true;
153                 else
154                         logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
155                                    c->name, c->hostname, strerror(errno));
156
157                 return false;
158         }
159
160         oldlen = c->buflen;
161         c->buflen += lenin;
162
163         while(lenin > 0) {
164                 /* Decrypt */
165
166                 if(c->status.decryptin && !decrypted) {
167                         result = EVP_DecryptUpdate(c->inctx, (unsigned char *)inbuf, &lenout, (unsigned char *)c->buffer + oldlen, lenin);
168                         if(!result || lenout != lenin) {
169                                 logger(LOG_ERR, _("Error while decrypting metadata from %s (%s): %s"),
170                                                 c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
171                                 return false;
172                         }
173                         memcpy(c->buffer + oldlen, inbuf, lenin);
174                         decrypted = true;
175                 }
176
177                 /* Are we receiving a TCPpacket? */
178
179                 if(c->tcplen) {
180                         if(c->tcplen <= c->buflen) {
181                                 receive_tcppacket(c, c->buffer, c->tcplen);
182
183                                 c->buflen -= c->tcplen;
184                                 lenin -= c->tcplen - oldlen;
185                                 memmove(c->buffer, c->buffer + c->tcplen, c->buflen);
186                                 oldlen = 0;
187                                 c->tcplen = 0;
188                                 continue;
189                         } else {
190                                 break;
191                         }
192                 }
193
194                 /* Otherwise we are waiting for a request */
195
196                 reqlen = 0;
197
198                 for(i = oldlen; i < c->buflen; i++) {
199                         if(c->buffer[i] == '\n') {
200                                 c->buffer[i] = '\0';    /* replace end-of-line by end-of-string so we can use sscanf */
201                                 reqlen = i + 1;
202                                 break;
203                         }
204                 }
205
206                 if(reqlen) {
207                         c->reqlen = reqlen;
208                         if(!receive_request(c))
209                                 return false;
210
211                         c->buflen -= reqlen;
212                         lenin -= reqlen - oldlen;
213                         memmove(c->buffer, c->buffer + reqlen, c->buflen);
214                         oldlen = 0;
215                         continue;
216                 } else {
217                         break;
218                 }
219         }
220
221         if(c->buflen >= MAXBUFSIZE) {
222                 logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
223                            c->name, c->hostname);
224                 return false;
225         }
226
227         c->last_ping_time = now;
228
229         return true;
230 }