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