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