Nexthop calculation should always use the shortest path.
[tinc] / src / meta.c
1 /*
2     meta.c -- handle the meta communication
3     Copyright (C) 2000-2013 Guus Sliepen <guus@tinc-vpn.org>,
4                   2000-2005 Ivo Timmermans
5                   2006      Scott Lamb <slamb@slamb.org>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write to the Free Software Foundation, Inc.,
19     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "system.h"
23
24 #include <openssl/err.h>
25 #include <openssl/evp.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 "utils.h"
34 #include "xalloc.h"
35
36 bool send_meta(connection_t *c, const char *buffer, int length) {
37         int outlen;
38         int result;
39
40         if(!c) {
41                 logger(LOG_ERR, "send_meta() called with NULL pointer!");
42                 abort();
43         }
44
45         ifdebug(META) logger(LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
46                            c->name, c->hostname);
47
48         if(!c->outbuflen)
49                 c->last_flushed_time = now;
50
51         /* Find room in connection's buffer */
52         if(length + c->outbuflen > c->outbufsize) {
53                 c->outbufsize = length + c->outbuflen;
54                 c->outbuf = xrealloc(c->outbuf, c->outbufsize);
55         }
56
57         if(length + c->outbuflen + c->outbufstart > c->outbufsize) {
58                 memmove(c->outbuf, c->outbuf + c->outbufstart, c->outbuflen);
59                 c->outbufstart = 0;
60         }
61
62         /* Add our data to buffer */
63         if(c->status.encryptout) {
64                 result = EVP_EncryptUpdate(c->outctx, (unsigned char *)c->outbuf + c->outbufstart + c->outbuflen,
65                                 &outlen, (unsigned char *)buffer, length);
66                 if(!result || outlen < length) {
67                         logger(LOG_ERR, "Error while encrypting metadata to %s (%s): %s",
68                                         c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
69                         return false;
70                 } else if(outlen > length) {
71                         logger(LOG_EMERG, "Encrypted data too long! Heap corrupted!");
72                         abort();
73                 }
74                 c->outbuflen += outlen;
75         } else {
76                 memcpy(c->outbuf + c->outbufstart + c->outbuflen, buffer, length);
77                 c->outbuflen += length;
78         }
79
80         return true;
81 }
82
83 bool flush_meta(connection_t *c) {
84         int result;
85         
86         ifdebug(META) logger(LOG_DEBUG, "Flushing %d bytes to %s (%s)",
87                          c->outbuflen, c->name, c->hostname);
88
89         while(c->outbuflen) {
90                 result = send(c->socket, c->outbuf + c->outbufstart, c->outbuflen, 0);
91                 if(result <= 0) {
92                         if(!errno || errno == EPIPE) {
93                                 ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)",
94                                                    c->name, c->hostname);
95                         } else if(errno == EINTR) {
96                                 continue;
97                         } else if(sockwouldblock(sockerrno)) {
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                         } else {
102                                 logger(LOG_ERR, "Flushing meta data to %s (%s) failed: %s", c->name,
103                                            c->hostname, sockstrerror(sockerrno));
104                         }
105
106                         return false;
107                 }
108
109                 c->outbufstart += result;
110                 c->outbuflen -= result;
111         }
112
113         c->outbufstart = 0; /* avoid unnecessary memmoves */
114         return true;
115 }
116
117 void broadcast_meta(connection_t *from, const char *buffer, int length) {
118         avl_node_t *node;
119         connection_t *c;
120
121         for(node = connection_tree->head; node; node = node->next) {
122                 c = node->data;
123
124                 if(c != from && c->status.active)
125                         send_meta(c, buffer, length);
126         }
127 }
128
129 bool receive_meta(connection_t *c) {
130         int oldlen, i, result;
131         int lenin, lenout, reqlen;
132         bool decrypted = false;
133         char inbuf[MAXBUFSIZE];
134
135         /* Strategy:
136            - Read as much as possible from the TCP socket in one go.
137            - Decrypt it.
138            - Check if a full request is in the input buffer.
139            - If yes, process request and remove it from the buffer,
140            then check again.
141            - If not, keep stuff in buffer and exit.
142          */
143
144         lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0);
145
146         if(lenin <= 0) {
147                 if(!lenin || !errno) {
148                         ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)",
149                                            c->name, c->hostname);
150                 } else if(sockwouldblock(sockerrno))
151                         return true;
152                 else
153                         logger(LOG_ERR, "Metadata socket read error for %s (%s): %s",
154                                    c->name, c->hostname, sockstrerror(sockerrno));
155
156                 return false;
157         }
158
159         oldlen = c->buflen;
160         c->buflen += lenin;
161
162         while(lenin > 0) {
163                 /* Decrypt */
164
165                 if(c->status.decryptin && !decrypted) {
166                         result = EVP_DecryptUpdate(c->inctx, (unsigned char *)inbuf, &lenout, (unsigned char *)c->buffer + oldlen, lenin);
167                         if(!result || lenout != lenin) {
168                                 logger(LOG_ERR, "Error while decrypting metadata from %s (%s): %s",
169                                                 c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
170                                 return false;
171                         }
172                         memcpy(c->buffer + oldlen, inbuf, lenin);
173                         decrypted = true;
174                 }
175
176                 /* Are we receiving a TCPpacket? */
177
178                 if(c->tcplen) {
179                         if(c->tcplen <= c->buflen) {
180                                 if(!c->node) {
181                                         if(c->outgoing && proxytype == PROXY_SOCKS4 && c->allow_request == ID) {
182                                                 if(c->buffer[0] == 0 && c->buffer[1] == 0x5a) {
183                                                         ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Proxy request granted");
184                                                 } else {
185                                                         logger(LOG_ERR, "Proxy request rejected");
186                                                         return false;
187                                                 }
188                                         } else if(c->outgoing && proxytype == PROXY_SOCKS5 && c->allow_request == ID) {
189                                                 if(c->buffer[0] != 5) {
190                                                         logger(LOG_ERR, "Invalid response from proxy server");
191                                                         return false;
192                                                 }
193                                                 if(c->buffer[1] == (char)0xff) {
194                                                         logger(LOG_ERR, "Proxy request rejected: unsuitable authentication method");
195                                                         return false;
196                                                 }
197                                                 if(c->buffer[2] != 5) {
198                                                         logger(LOG_ERR, "Invalid response from proxy server");
199                                                         return false;
200                                                 }
201                                                 if(c->buffer[3] == 0) {
202                                                         ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Proxy request granted");
203                                                 } else {
204                                                         logger(LOG_ERR, "Proxy request rejected");
205                                                         return false;
206                                                 }
207                                         } else {
208                                                 logger(LOG_ERR, "c->tcplen set but c->node is NULL!");
209                                                 abort();
210                                         }
211                                 } else {
212                                         if(c->allow_request == ALL) {
213                                                 receive_tcppacket(c, c->buffer, c->tcplen);
214                                         } else {
215                                                 logger(LOG_ERR, "Got unauthorized TCP packet from %s (%s)", c->name, c->hostname);
216                                                 return false;
217                                         }
218                                 }
219
220                                 c->buflen -= c->tcplen;
221                                 lenin -= c->tcplen - oldlen;
222                                 memmove(c->buffer, c->buffer + c->tcplen, c->buflen);
223                                 oldlen = 0;
224                                 c->tcplen = 0;
225                                 continue;
226                         } else {
227                                 break;
228                         }
229                 }
230
231                 /* Otherwise we are waiting for a request */
232
233                 reqlen = 0;
234
235                 for(i = oldlen; i < c->buflen; i++) {
236                         if(c->buffer[i] == '\n') {
237                                 c->buffer[i] = '\0';    /* replace end-of-line by end-of-string so we can use sscanf */
238                                 reqlen = i + 1;
239                                 break;
240                         }
241                 }
242
243                 if(reqlen) {
244                         c->reqlen = reqlen;
245                         if(!receive_request(c))
246                                 return false;
247
248                         c->buflen -= reqlen;
249                         lenin -= reqlen - oldlen;
250                         memmove(c->buffer, c->buffer + reqlen, c->buflen);
251                         oldlen = 0;
252                         continue;
253                 } else {
254                         break;
255                 }
256         }
257
258         if(c->buflen >= MAXBUFSIZE) {
259                 logger(LOG_ERR, "Metadata read buffer overflow for %s (%s)",
260                            c->name, c->hostname);
261                 return false;
262         }
263
264         return true;
265 }