Remove use of bufferevent and eventbuffers, use our own buffering instead.
[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, outbuf, length, buffer_prepare(&c->outbuf, length), &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         } else {
55                 buffer_add(&c->outbuf, buffer, length);
56         }
57
58         event_add(&c->outevent, NULL);
59
60         return true;
61 }
62
63 void broadcast_meta(connection_t *from, const char *buffer, int length) {
64         splay_node_t *node;
65         connection_t *c;
66
67         for(node = connection_tree->head; node; node = node->next) {
68                 c = node->data;
69
70                 if(c != from && c->status.active)
71                         send_meta(c, buffer, length);
72         }
73 }
74
75 bool receive_meta(connection_t *c) {
76         int inlen, reqlen;
77         char inbuf[MAXBUFSIZE];
78         char *bufp = inbuf, *endp;
79
80         /* Strategy:
81            - Read as much as possible from the TCP socket in one go.
82            - Decrypt it.
83            - Check if a full request is in the input buffer.
84            - If yes, process request and remove it from the buffer,
85            then check again.
86            - If not, keep stuff in buffer and exit.
87          */
88
89         inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 0);
90
91         if(inlen <= 0) {
92                 if(!inlen || !errno) {
93                         ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)",
94                                            c->name, c->hostname);
95                 } else if(sockwouldblock(sockerrno))
96                         return true;
97                 else
98                         logger(LOG_ERR, "Metadata socket read error for %s (%s): %s",
99                                    c->name, c->hostname, sockstrerror(sockerrno));
100                 return false;
101         }
102
103         do {
104                 if(!c->status.decryptin) {
105                         endp = memchr(bufp, '\n', inlen);
106                         if(endp)
107                                 endp++;
108                         else
109                                 endp = bufp + inlen;
110
111                         buffer_add(&c->inbuf, bufp, endp - bufp);
112
113                         inlen -= endp - bufp;
114                         bufp = endp;
115                 } else {
116                         size_t outlen = inlen;
117                         ifdebug(META) logger(LOG_DEBUG, "Received encrypted %d bytes", inlen);
118
119                         if(!cipher_decrypt(&c->incipher, bufp, inlen, buffer_prepare(&c->inbuf, inlen), &outlen, false) || inlen != outlen) {
120                                 logger(LOG_ERR, "Error while decrypting metadata from %s (%s)",
121                                            c->name, c->hostname);
122                                 return false;
123                         }
124
125                         inlen = 0;
126                 }
127
128                 while(c->inbuf.len) {
129                         /* Are we receiving a TCPpacket? */
130
131                         if(c->tcplen) {
132                                 char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen);
133                                 if(tcpbuffer) {
134                                         receive_tcppacket(c, tcpbuffer, c->tcplen);
135                                         continue;
136                                 } else {
137                                         break;
138                                 }
139                         }
140
141                         /* Otherwise we are waiting for a request */
142
143                         char *request = buffer_readline(&c->inbuf);
144                         if(request) {
145                                 bool result = receive_request(c, request);
146                                 if(!result)
147                                         return false;
148                                 continue;
149                         } else {
150                                 break;
151                         }
152                 }
153         } while(inlen);
154
155         return true;
156 }