Fix compatibility with TAP-Win32 9.0.0.21 and later.
[tinc] / src / meta.c
1 /*
2     meta.c -- handle the meta communication
3     Copyright (C) 2000-2014 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 "proxy.h"
34 #include "utils.h"
35 #include "xalloc.h"
36
37 bool send_meta(connection_t *c, const char *buffer, int length) {
38         int outlen;
39         int result;
40
41         if(!c) {
42                 logger(LOG_ERR, "send_meta() called with NULL pointer!");
43                 abort();
44         }
45
46         ifdebug(META) logger(LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
47                            c->name, c->hostname);
48
49         if(!c->outbuflen)
50                 c->last_flushed_time = now;
51
52         /* Find room in connection's buffer */
53         if(length + c->outbuflen > c->outbufsize) {
54                 c->outbufsize = length + c->outbuflen;
55                 c->outbuf = xrealloc(c->outbuf, c->outbufsize);
56         }
57
58         if(length + c->outbuflen + c->outbufstart > c->outbufsize) {
59                 memmove(c->outbuf, c->outbuf + c->outbufstart, c->outbuflen);
60                 c->outbufstart = 0;
61         }
62
63         /* Add our data to buffer */
64         if(c->status.encryptout) {
65                 result = EVP_EncryptUpdate(c->outctx, (unsigned char *)c->outbuf + c->outbufstart + c->outbuflen,
66                                 &outlen, (unsigned char *)buffer, length);
67                 if(!result || outlen < length) {
68                         logger(LOG_ERR, "Error while encrypting metadata to %s (%s): %s",
69                                         c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
70                         return false;
71                 } else if(outlen > length) {
72                         logger(LOG_EMERG, "Encrypted data too long! Heap corrupted!");
73                         abort();
74                 }
75                 c->outbuflen += outlen;
76         } else {
77                 memcpy(c->outbuf + c->outbufstart + c->outbuflen, buffer, length);
78                 c->outbuflen += length;
79         }
80
81         return true;
82 }
83
84 bool flush_meta(connection_t *c) {
85         int result;
86         
87         ifdebug(META) logger(LOG_DEBUG, "Flushing %d bytes to %s (%s)",
88                          c->outbuflen, c->name, c->hostname);
89
90         while(c->outbuflen) {
91                 result = send(c->socket, c->outbuf + c->outbufstart, c->outbuflen, 0);
92                 if(result <= 0) {
93                         if(!errno || errno == EPIPE) {
94                                 ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)",
95                                                    c->name, c->hostname);
96                         } else if(errno == EINTR) {
97                                 continue;
98                         } else if(sockwouldblock(sockerrno)) {
99                                 ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Flushing %d bytes to %s (%s) would block",
100                                                 c->outbuflen, c->name, c->hostname);
101                                 return true;
102                         } else {
103                                 logger(LOG_ERR, "Flushing meta data to %s (%s) failed: %s", c->name,
104                                            c->hostname, sockstrerror(sockerrno));
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(sockwouldblock(sockerrno))
152                         return true;
153                 else
154                         logger(LOG_ERR, "Metadata socket read error for %s (%s): %s",
155                                    c->name, c->hostname, sockstrerror(sockerrno));
156
157                 return false;
158         }
159
160         oldlen = c->buflen;
161         c->buflen += lenin;
162
163         while(lenin > 0) {
164                 reqlen = 0;
165
166                 /* Is it proxy metadata? */
167
168                 if(c->allow_request == PROXY) {
169                         reqlen = receive_proxy_meta(c, oldlen, lenin);
170                         if(reqlen < 0)
171                                 return false;
172                         goto consume;
173                 }
174
175                 /* Decrypt */
176
177                 if(c->status.decryptin && !decrypted) {
178                         result = EVP_DecryptUpdate(c->inctx, (unsigned char *)inbuf, &lenout, (unsigned char *)c->buffer + oldlen, lenin);
179                         if(!result || lenout != lenin) {
180                                 logger(LOG_ERR, "Error while decrypting metadata from %s (%s): %s",
181                                                 c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
182                                 return false;
183                         }
184                         memcpy(c->buffer + oldlen, inbuf, lenin);
185                         decrypted = true;
186                 }
187
188                 /* Are we receiving a TCPpacket? */
189
190                 if(c->tcplen) {
191                         if(c->tcplen <= c->buflen) {
192                                 if(c->allow_request != ALL) {
193                                         logger(LOG_ERR, "Got unauthorized TCP packet from %s (%s)", c->name, c->hostname);
194                                         return false;
195                                 }
196
197                                 receive_tcppacket(c, c->buffer, c->tcplen);
198                                 reqlen = c->tcplen;
199                                 c->tcplen = 0;
200                         }
201                 } else {
202                         /* Otherwise we are waiting for a request */
203
204                         for(i = oldlen; i < c->buflen; i++) {
205                                 if(c->buffer[i] == '\n') {
206                                         c->buffer[i] = '\0';    /* replace end-of-line by end-of-string so we can use sscanf */
207                                         c->reqlen = reqlen = i + 1;
208                                         break;
209                                 }
210                         }
211
212                         if(reqlen && !receive_request(c))
213                                 return false;
214                 }
215
216 consume:
217                 if(reqlen) {
218                         c->buflen -= reqlen;
219                         lenin -= reqlen - oldlen;
220                         memmove(c->buffer, c->buffer + reqlen, c->buflen);
221                         oldlen = 0;
222                         continue;
223                 } else {
224                         break;
225                 }
226         }
227
228         if(c->buflen >= MAXBUFSIZE) {
229                 logger(LOG_ERR, "Metadata read buffer overflow for %s (%s)",
230                            c->name, c->hostname);
231                 return false;
232         }
233
234         return true;
235 }