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