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