Fix meta data segfault when receiving a partial command.
[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 "splay_tree.h"
26 #include "cipher.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         cp();
37
38         ifdebug(META) logger(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length, c->name, c->hostname);
39
40         /* Add our data to buffer */
41         if(c->status.encryptout) {
42                 char outbuf[length];
43                 size_t outlen = length;
44
45                 if(!cipher_encrypt(&c->outcipher, buffer, length, outbuf, &outlen, false) || outlen != length) {
46                         logger(LOG_ERR, _("Error while encrypting metadata to %s (%s)"), c->name, c->hostname);
47                         return false;
48                 }
49                 
50                 bufferevent_write(c->buffer, (void *)outbuf, length);
51         } else {
52                 bufferevent_write(c->buffer, (void *)buffer, length);
53         }
54
55         return true;
56 }
57
58 void broadcast_meta(connection_t *from, const char *buffer, int length) {
59         splay_node_t *node;
60         connection_t *c;
61
62         cp();
63
64         for(node = connection_tree->head; node; node = node->next) {
65                 c = node->data;
66
67                 if(c != from && c->status.active)
68                         send_meta(c, buffer, length);
69         }
70 }
71
72 bool receive_meta(connection_t *c) {
73         size_t inlen;
74         char inbuf[MAXBUFSIZE];
75         char *bufp = inbuf, *endp;
76
77         cp();
78
79         /* Strategy:
80            - Read as much as possible from the TCP socket in one go.
81            - Decrypt it.
82            - Check if a full request is in the input buffer.
83            - If yes, process request and remove it from the buffer,
84            then check again.
85            - If not, keep stuff in buffer and exit.
86          */
87
88         inlen = recv(c->socket, inbuf, sizeof inbuf, 0);
89
90         if(inlen <= 0) {
91                 logger(LOG_ERR, _("Receive callback called for %s (%s) but no data to receive: %s"), c->name, c->hostname, strerror(errno));
92                 return false;
93         }
94
95         do {
96                 if(!c->status.decryptin) {
97                         endp = memchr(bufp, '\n', inlen);
98                         if(endp)
99                                 endp++;
100                         else
101                                 endp = bufp + inlen;
102
103                         evbuffer_add(c->buffer->input, bufp, endp - bufp);
104
105                         inlen -= endp - bufp;
106                         bufp = endp;
107                 } else {
108                         size_t outlen = inlen;
109                         evbuffer_expand(c->buffer->input, c->buffer->input->off + inlen);
110
111                         if(!cipher_decrypt(&c->incipher, bufp, inlen, c->buffer->input->buffer + c->buffer->input->off, &outlen, false) || inlen != outlen) {
112                                 logger(LOG_ERR, _("Error while decrypting metadata from %s (%s)"), c->name, c->hostname);
113                                 return false;
114                         }
115
116                         c->buffer->input->off += inlen;
117                         inlen = 0;
118                 }
119
120                 while(c->buffer->input->off) {
121                         /* Are we receiving a TCPpacket? */
122
123                         if(c->tcplen) {
124                                 if(c->tcplen <= c->buffer->input->off) {
125                                         receive_tcppacket(c, (char *)c->buffer->input->buffer, c->tcplen);
126                                         evbuffer_drain(c->buffer->input, c->tcplen);
127                                         c->tcplen = 0;
128                                         continue;
129                                 } else {
130                                         break;
131                                 }
132                         }
133
134                         /* Otherwise we are waiting for a request */
135
136                         char *request = evbuffer_readline(c->buffer->input);
137                         if(request) {
138                                 bool result = receive_request(c, request);
139                                 free(request);
140                                 if(!result)
141                                         return false;
142                                 continue;
143                         } else {
144                                 break;
145                         }
146                 }
147         } while(inlen);
148
149         c->last_ping_time = time(NULL);
150
151         return true;
152 }