e56c86f9124d585966759fb63b9a8ae5a4b6f267
[oweals/tinc.git] / 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                 size_t outlen = length;
46
47                 if(!cipher_encrypt(&c->outcipher, buffer, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) {
48                         logger(LOG_ERR, "Error while encrypting metadata to %s (%s)",
49                                         c->name, c->hostname);
50                         return false;
51                 }
52
53         } else {
54                 buffer_add(&c->outbuf, buffer, length);
55         }
56
57         event_add(&c->outevent, NULL);
58
59         return true;
60 }
61
62 void broadcast_meta(connection_t *from, const char *buffer, int length) {
63         splay_node_t *node;
64         connection_t *c;
65
66         for(node = connection_tree->head; node; node = node->next) {
67                 c = node->data;
68
69                 if(c != from && c->status.active)
70                         send_meta(c, buffer, length);
71         }
72 }
73
74 bool receive_meta(connection_t *c) {
75         int inlen;
76         char inbuf[MAXBUFSIZE];
77         char *bufp = inbuf, *endp;
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         buffer_compact(&c->inbuf, MAXBUFSIZE);
89
90         if(sizeof inbuf <= c->inbuf.len) {
91                 logger(LOG_ERR, "Input buffer full for %s (%s)\n", c, c->hostname);
92                 return false;
93         }
94
95         inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 0);
96
97         if(inlen <= 0) {
98                 if(!inlen || !errno) {
99                         ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)",
100                                            c->name, c->hostname);
101                 } else if(sockwouldblock(sockerrno))
102                         return true;
103                 else
104                         logger(LOG_ERR, "Metadata socket read error for %s (%s): %s",
105                                    c->name, c->hostname, sockstrerror(sockerrno));
106                 return false;
107         }
108
109         do {
110                 if(!c->status.decryptin) {
111                         endp = memchr(bufp, '\n', inlen);
112                         if(endp)
113                                 endp++;
114                         else
115                                 endp = bufp + inlen;
116
117                         buffer_add(&c->inbuf, bufp, endp - bufp);
118
119                         inlen -= endp - bufp;
120                         bufp = endp;
121                 } else {
122                         size_t outlen = inlen;
123                         ifdebug(META) logger(LOG_DEBUG, "Received encrypted %d bytes", inlen);
124
125                         if(!cipher_decrypt(&c->incipher, bufp, inlen, buffer_prepare(&c->inbuf, inlen), &outlen, false) || inlen != outlen) {
126                                 logger(LOG_ERR, "Error while decrypting metadata from %s (%s)",
127                                            c->name, c->hostname);
128                                 return false;
129                         }
130
131                         inlen = 0;
132                 }
133
134                 while(c->inbuf.len) {
135                         /* Are we receiving a TCPpacket? */
136
137                         if(c->tcplen) {
138                                 char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen);
139                                 if(tcpbuffer) {
140                                         c->tcplen = 0;
141                                         receive_tcppacket(c, tcpbuffer, c->tcplen);
142                                         continue;
143                                 } else {
144                                         break;
145                                 }
146                         }
147
148                         /* Otherwise we are waiting for a request */
149
150                         char *request = buffer_readline(&c->inbuf);
151                         if(request) {
152                                 bool result = receive_request(c, request);
153                                 if(!result)
154                                         return false;
155                                 continue;
156                         } else {
157                                 break;
158                         }
159                 }
160         } while(inlen);
161
162         return true;
163 }