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