Merge branch 'master' into 1.1
[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                   2006      Scott Lamb <slamb@slamb.org>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write to the Free Software Foundation, Inc.,
19     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "system.h"
23
24 #include "splay_tree.h"
25 #include "cipher.h"
26 #include "connection.h"
27 #include "logger.h"
28 #include "meta.h"
29 #include "net.h"
30 #include "protocol.h"
31 #include "utils.h"
32 #include "xalloc.h"
33
34 bool send_meta(connection_t *c, const char *buffer, int length) {
35         if(!c) {
36                 logger(LOG_ERR, "send_meta() called with NULL pointer!");
37                 abort();
38         }
39
40         ifdebug(META) logger(LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
41                            c->name, c->hostname);
42
43         /* Add our data to buffer */
44         if(c->status.encryptout) {
45                 char outbuf[length];
46                 size_t outlen = length;
47
48                 if(!cipher_encrypt(&c->outcipher, buffer, length, outbuf, &outlen, false) || outlen != length) {
49                         logger(LOG_ERR, "Error while encrypting metadata to %s (%s)",
50                                         c->name, c->hostname);
51                         return false;
52                 }
53                 
54                 ifdebug(META) logger(LOG_DEBUG, "Encrypted write %p %p %p %d", c, c->buffer, outbuf, length);
55                 bufferevent_write(c->buffer, (void *)outbuf, length);
56                 ifdebug(META) logger(LOG_DEBUG, "Done.");
57         } else {
58                 ifdebug(META) logger(LOG_DEBUG, "Unencrypted write %p %p %p %d", c, c->buffer, buffer, length);
59                 bufferevent_write(c->buffer, (void *)buffer, length);
60                 ifdebug(META) logger(LOG_DEBUG, "Done.");
61         }
62
63         return true;
64 }
65
66 void broadcast_meta(connection_t *from, const char *buffer, int length) {
67         splay_node_t *node;
68         connection_t *c;
69
70         for(node = connection_tree->head; node; node = node->next) {
71                 c = node->data;
72
73                 if(c != from && c->status.active)
74                         send_meta(c, buffer, length);
75         }
76 }
77
78 bool receive_meta(connection_t *c) {
79         size_t inlen;
80         char inbuf[MAXBUFSIZE];
81         char *bufp = inbuf, *endp;
82
83         /* Strategy:
84            - Read as much as possible from the TCP socket in one go.
85            - Decrypt it.
86            - Check if a full request is in the input buffer.
87            - If yes, process request and remove it from the buffer,
88            then check again.
89            - If not, keep stuff in buffer and exit.
90          */
91
92         inlen = recv(c->socket, inbuf, sizeof inbuf, 0);
93
94         if(inlen <= 0) {
95                 logger(LOG_ERR, "Receive callback called for %s (%s) but no data to receive: %s", c->name, c->hostname, strerror(errno));
96                 return false;
97         }
98
99         do {
100                 if(!c->status.decryptin) {
101                         endp = memchr(bufp, '\n', inlen);
102                         if(endp)
103                                 endp++;
104                         else
105                                 endp = bufp + inlen;
106
107                         evbuffer_add(c->buffer->input, bufp, endp - bufp);
108
109                         inlen -= endp - bufp;
110                         bufp = endp;
111                 } else {
112                         size_t outlen = inlen;
113                         ifdebug(META) logger(LOG_DEBUG, "Received encrypted %zu bytes", inlen);
114                         evbuffer_expand(c->buffer->input, c->buffer->input->off + inlen);
115
116                         if(!cipher_decrypt(&c->incipher, bufp, inlen, c->buffer->input->buffer + c->buffer->input->off, &outlen, false) || inlen != outlen) {
117                                 logger(LOG_ERR, "Error while decrypting metadata from %s (%s)",
118                                            c->name, c->hostname);
119                                 return false;
120                         }
121                         c->buffer->input->off += inlen;
122
123                         inlen = 0;
124                 }
125
126                 while(c->buffer->input->off) {
127                         /* Are we receiving a TCPpacket? */
128
129                         if(c->tcplen) {
130                                 if(c->tcplen <= c->buffer->input->off) {
131                                         receive_tcppacket(c, (char *)c->buffer->input->buffer, c->tcplen);
132                                         evbuffer_drain(c->buffer->input, c->tcplen);
133                                         c->tcplen = 0;
134                                         continue;
135                                 } else {
136                                         break;
137                                 }
138                         }
139
140                         /* Otherwise we are waiting for a request */
141
142                         char *request = evbuffer_readline(c->buffer->input);
143                         if(request) {
144                                 bool result = receive_request(c, request);
145                                 free(request);
146                                 if(!result)
147                                         return false;
148                                 continue;
149                         } else {
150                                 break;
151                         }
152                 }
153         } while(inlen);
154
155         c->last_ping_time = time(NULL);
156
157         return true;
158 }