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