Use a separate event structure to handle meta data writes.
[tinc] / src / meta.c
1 /*
2     meta.c -- handle the meta communication
3     Copyright (C) 2000-2006 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         ifdebug(META) logger(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length,
45                            c->name, c->hostname);
46
47         if(!c->outbuflen) {
48                 c->last_flushed_time = now;
49                 if(event_add(&c->outev, NULL) < 0) {
50                         logger(LOG_EMERG, _("event_add failed: %s"), strerror(errno));
51                         abort();
52                 }
53         }
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 void flush_meta(int fd, short events, void *data)
88 {
89         connection_t *c = data;
90         int result;
91         
92         ifdebug(META) logger(LOG_DEBUG, _("Flushing %d bytes to %s (%s)"),
93                          c->outbuflen, c->name, c->hostname);
94
95         while(c->outbuflen) {
96                 result = send(c->socket, c->outbuf + c->outbufstart, c->outbuflen, 0);
97                 if(result <= 0) {
98                         if(!errno || errno == EPIPE) {
99                                 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
100                                                    c->name, c->hostname);
101                         } else if(errno == EINTR) {
102                                 continue;
103 #ifdef EWOULDBLOCK
104                         } else if(errno == EWOULDBLOCK) {
105                                 ifdebug(CONNECTIONS) logger(LOG_DEBUG, _("Flushing %d bytes to %s (%s) would block"),
106                                                 c->outbuflen, c->name, c->hostname);
107                                 return;
108 #endif
109                         } else {
110                                 logger(LOG_ERR, _("Flushing meta data to %s (%s) failed: %s"), c->name,
111                                            c->hostname, strerror(errno));
112                         }
113
114                         terminate_connection(c, c->status.active);
115                         return;
116                 }
117
118                 c->outbufstart += result;
119                 c->outbuflen -= result;
120         }
121
122         event_del(&c->outev);
123
124         c->outbufstart = 0; /* avoid unnecessary memmoves */
125 }
126
127 void broadcast_meta(connection_t *from, const char *buffer, int length)
128 {
129         avl_node_t *node;
130         connection_t *c;
131
132         cp();
133
134         for(node = connection_tree->head; node; node = node->next) {
135                 c = node->data;
136
137                 if(c != from && c->status.active)
138                         send_meta(c, buffer, length);
139         }
140 }
141
142 bool receive_meta(connection_t *c)
143 {
144         int oldlen, i, result;
145         int lenin, lenout, reqlen;
146         bool decrypted = false;
147         char inbuf[MAXBUFSIZE];
148
149         cp();
150
151         /* Strategy:
152            - Read as much as possible from the TCP socket in one go.
153            - Decrypt it.
154            - Check if a full request is in the input buffer.
155            - If yes, process request and remove it from the buffer,
156            then check again.
157            - If not, keep stuff in buffer and exit.
158          */
159
160         lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0);
161
162         if(lenin <= 0) {
163                 if(!lenin || !errno) {
164                         ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
165                                            c->name, c->hostname);
166                 } else if(errno == EINTR)
167                         return true;
168                 else
169                         logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
170                                    c->name, c->hostname, strerror(errno));
171
172                 return false;
173         }
174
175         oldlen = c->buflen;
176         c->buflen += lenin;
177
178         while(lenin > 0) {
179                 /* Decrypt */
180
181                 if(c->status.decryptin && !decrypted) {
182                         result = EVP_DecryptUpdate(c->inctx, (unsigned char *)inbuf, &lenout, (unsigned char *)c->buffer + oldlen, lenin);
183                         if(!result || lenout != lenin) {
184                                 logger(LOG_ERR, _("Error while decrypting metadata from %s (%s): %s"),
185                                                 c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
186                                 return false;
187                         }
188                         memcpy(c->buffer + oldlen, inbuf, lenin);
189                         decrypted = true;
190                 }
191
192                 /* Are we receiving a TCPpacket? */
193
194                 if(c->tcplen) {
195                         if(c->tcplen <= c->buflen) {
196                                 receive_tcppacket(c, c->buffer, c->tcplen);
197
198                                 c->buflen -= c->tcplen;
199                                 lenin -= c->tcplen - oldlen;
200                                 memmove(c->buffer, c->buffer + c->tcplen, c->buflen);
201                                 oldlen = 0;
202                                 c->tcplen = 0;
203                                 continue;
204                         } else {
205                                 break;
206                         }
207                 }
208
209                 /* Otherwise we are waiting for a request */
210
211                 reqlen = 0;
212
213                 for(i = oldlen; i < c->buflen; i++) {
214                         if(c->buffer[i] == '\n') {
215                                 c->buffer[i] = '\0';    /* replace end-of-line by end-of-string so we can use sscanf */
216                                 reqlen = i + 1;
217                                 break;
218                         }
219                 }
220
221                 if(reqlen) {
222                         c->reqlen = reqlen;
223                         if(!receive_request(c))
224                                 return false;
225
226                         c->buflen -= reqlen;
227                         lenin -= reqlen - oldlen;
228                         memmove(c->buffer, c->buffer + reqlen, c->buflen);
229                         oldlen = 0;
230                         continue;
231                 } else {
232                         break;
233                 }
234         }
235
236         if(c->buflen >= MAXBUFSIZE) {
237                 logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
238                            c->name, c->hostname);
239                 return false;
240         }
241
242         c->last_ping_time = now;
243
244         return true;
245 }