K&R style braces.
[tinc] / src / meta.c
1 /*
2     meta.c -- handle the meta communication
3     Copyright (C) 2000-2009 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 along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include <openssl/err.h>
24 #include <openssl/evp.h>
25
26 #include "avl_tree.h"
27 #include "connection.h"
28 #include "logger.h"
29 #include "meta.h"
30 #include "net.h"
31 #include "protocol.h"
32 #include "utils.h"
33 #include "xalloc.h"
34
35 bool send_meta(connection_t *c, const char *buffer, int length) {
36         int outlen;
37         int result;
38
39         cp();
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 #ifdef EWOULDBLOCK
99                         } else if(errno == EWOULDBLOCK) {
100                                 ifdebug(CONNECTIONS) logger(LOG_DEBUG, _("Flushing %d bytes to %s (%s) would block"),
101                                                 c->outbuflen, c->name, c->hostname);
102                                 return true;
103 #endif
104                         } else {
105                                 logger(LOG_ERR, _("Flushing meta data to %s (%s) failed: %s"), c->name,
106                                            c->hostname, strerror(errno));
107                         }
108
109                         return false;
110                 }
111
112                 c->outbufstart += result;
113                 c->outbuflen -= result;
114         }
115
116         c->outbufstart = 0; /* avoid unnecessary memmoves */
117         return true;
118 }
119
120 void broadcast_meta(connection_t *from, const char *buffer, int length) {
121         avl_node_t *node;
122         connection_t *c;
123
124         cp();
125
126         for(node = connection_tree->head; node; node = node->next) {
127                 c = node->data;
128
129                 if(c != from && c->status.active)
130                         send_meta(c, buffer, length);
131         }
132 }
133
134 bool receive_meta(connection_t *c) {
135         int oldlen, i, result;
136         int lenin, lenout, reqlen;
137         bool decrypted = false;
138         char inbuf[MAXBUFSIZE];
139
140         cp();
141
142         /* Strategy:
143            - Read as much as possible from the TCP socket in one go.
144            - Decrypt it.
145            - Check if a full request is in the input buffer.
146            - If yes, process request and remove it from the buffer,
147            then check again.
148            - If not, keep stuff in buffer and exit.
149          */
150
151         lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0);
152
153         if(lenin <= 0) {
154                 if(!lenin || !errno) {
155                         ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
156                                            c->name, c->hostname);
157                 } else if(errno == EINTR)
158                         return true;
159                 else
160                         logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
161                                    c->name, c->hostname, strerror(errno));
162
163                 return false;
164         }
165
166         oldlen = c->buflen;
167         c->buflen += lenin;
168
169         while(lenin > 0) {
170                 /* Decrypt */
171
172                 if(c->status.decryptin && !decrypted) {
173                         result = EVP_DecryptUpdate(c->inctx, (unsigned char *)inbuf, &lenout, (unsigned char *)c->buffer + oldlen, lenin);
174                         if(!result || lenout != lenin) {
175                                 logger(LOG_ERR, _("Error while decrypting metadata from %s (%s): %s"),
176                                                 c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
177                                 return false;
178                         }
179                         memcpy(c->buffer + oldlen, inbuf, lenin);
180                         decrypted = true;
181                 }
182
183                 /* Are we receiving a TCPpacket? */
184
185                 if(c->tcplen) {
186                         if(c->tcplen <= c->buflen) {
187                                 receive_tcppacket(c, c->buffer, c->tcplen);
188
189                                 c->buflen -= c->tcplen;
190                                 lenin -= c->tcplen - oldlen;
191                                 memmove(c->buffer, c->buffer + c->tcplen, c->buflen);
192                                 oldlen = 0;
193                                 c->tcplen = 0;
194                                 continue;
195                         } else {
196                                 break;
197                         }
198                 }
199
200                 /* Otherwise we are waiting for a request */
201
202                 reqlen = 0;
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                                 reqlen = i + 1;
208                                 break;
209                         }
210                 }
211
212                 if(reqlen) {
213                         c->reqlen = reqlen;
214                         if(!receive_request(c))
215                                 return false;
216
217                         c->buflen -= reqlen;
218                         lenin -= reqlen - oldlen;
219                         memmove(c->buffer, c->buffer + reqlen, c->buflen);
220                         oldlen = 0;
221                         continue;
222                 } else {
223                         break;
224                 }
225         }
226
227         if(c->buflen >= MAXBUFSIZE) {
228                 logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
229                            c->name, c->hostname);
230                 return false;
231         }
232
233         c->last_ping_time = now;
234
235         return true;
236 }