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