Merge branch 'master' of git://tinc-vpn.org/tinc 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_sptps(void *handle, const char *buffer, size_t length) {
35         connection_t *c = handle;
36
37         if(!c) {
38                 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta_sptps() called with NULL pointer!");
39                 abort();
40         }
41
42         logger(DEBUG_ALWAYS, LOG_DEBUG, "send_meta_sptps(%s, %p, %zu)", c->name, buffer, length);
43
44         buffer_add(&c->outbuf, buffer, length);
45         event_add(&c->outevent, NULL);
46
47         return true;
48 }
49
50 bool send_meta(connection_t *c, const char *buffer, int length) {
51         if(!c) {
52                 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
53                 abort();
54         }
55
56         logger(DEBUG_META, LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
57                            c->name, c->hostname);
58
59         if(c->protocol_minor >= 2)
60                 return sptps_send_record(&c->sptps, 0, buffer, length);
61
62         /* Add our data to buffer */
63         if(c->status.encryptout) {
64                 size_t outlen = length;
65
66                 if(!cipher_encrypt(&c->outcipher, buffer, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) {
67                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting metadata to %s (%s)",
68                                         c->name, c->hostname);
69                         return false;
70                 }
71
72         } else {
73                 buffer_add(&c->outbuf, buffer, length);
74         }
75
76         event_add(&c->outevent, NULL);
77
78         return true;
79 }
80
81 void broadcast_meta(connection_t *from, const char *buffer, int length) {
82         splay_node_t *node;
83         connection_t *c;
84
85         for(node = connection_tree->head; node; node = node->next) {
86                 c = node->data;
87
88                 if(c != from && c->status.active)
89                         send_meta(c, buffer, length);
90         }
91 }
92
93 bool receive_meta_sptps(void *handle, uint8_t type, const char *data, uint16_t length) {
94         connection_t *c = handle;
95
96         if(!c) {
97                 logger(DEBUG_ALWAYS, LOG_ERR, "receive_meta_sptps() called with NULL pointer!");
98                 abort();
99         }
100
101         logger(DEBUG_ALWAYS, LOG_DEBUG, "receive_meta_sptps(%s, %d, %p, %hu)", c->name, type, data, length);
102
103         if(type == SPTPS_HANDSHAKE) {
104                 if(c->allow_request == ACK)
105                         return send_ack(c);
106                 else
107                         return true;
108         }
109
110         if(!data)
111                 return true;
112
113         /* Are we receiving a TCPpacket? */
114
115         if(c->tcplen) {
116                 if(length != c->tcplen)
117                         return false;
118                 receive_tcppacket(c, data, length);
119                 c->tcplen = 0;
120                 return true;
121         }
122
123         /* Otherwise we are waiting for a request */
124
125         return receive_request(c, data);
126 }
127
128 bool receive_meta(connection_t *c) {
129         int inlen;
130         char inbuf[MAXBUFSIZE];
131         char *bufp = inbuf, *endp;
132
133         /* Strategy:
134            - Read as much as possible from the TCP socket in one go.
135            - Decrypt it.
136            - Check if a full request is in the input buffer.
137            - If yes, process request and remove it from the buffer,
138            then check again.
139            - If not, keep stuff in buffer and exit.
140          */
141
142         buffer_compact(&c->inbuf, MAXBUFSIZE);
143
144         if(sizeof inbuf <= c->inbuf.len) {
145                 logger(DEBUG_ALWAYS, LOG_ERR, "Input buffer full for %s (%s)", c->name, c->hostname);
146                 return false;
147         }
148
149         inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 0);
150
151         if(inlen <= 0) {
152                 if(!inlen || !errno) {
153                         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)",
154                                            c->name, c->hostname);
155                 } else if(sockwouldblock(sockerrno))
156                         return true;
157                 else
158                         logger(DEBUG_ALWAYS, LOG_ERR, "Metadata socket read error for %s (%s): %s",
159                                    c->name, c->hostname, sockstrerror(sockerrno));
160                 return false;
161         }
162
163         do {
164                 if(c->protocol_minor >= 2) {
165                         logger(DEBUG_ALWAYS, LOG_DEBUG, "Receiving %d bytes of SPTPS data", inlen);
166                         return sptps_receive_data(&c->sptps, bufp, inlen);
167                 }
168
169                 if(!c->status.decryptin) {
170                         endp = memchr(bufp, '\n', inlen);
171                         if(endp)
172                                 endp++;
173                         else
174                                 endp = bufp + inlen;
175
176                         buffer_add(&c->inbuf, bufp, endp - bufp);
177
178                         inlen -= endp - bufp;
179                         bufp = endp;
180                 } else {
181                         size_t outlen = inlen;
182                         logger(DEBUG_META, LOG_DEBUG, "Received encrypted %d bytes", inlen);
183
184                         if(!cipher_decrypt(&c->incipher, bufp, inlen, buffer_prepare(&c->inbuf, inlen), &outlen, false) || inlen != outlen) {
185                                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting metadata from %s (%s)",
186                                            c->name, c->hostname);
187                                 return false;
188                         }
189
190                         inlen = 0;
191                 }
192
193                 while(c->inbuf.len) {
194                         /* Are we receiving a TCPpacket? */
195
196                         if(c->tcplen) {
197                                 char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen);
198                                 if(tcpbuffer) {
199                                         receive_tcppacket(c, tcpbuffer, c->tcplen);
200                                         c->tcplen = 0;
201                                         continue;
202                                 } else {
203                                         break;
204                                 }
205                         }
206
207                         /* Otherwise we are waiting for a request */
208
209                         char *request = buffer_readline(&c->inbuf);
210                         if(request) {
211                                 bool result = receive_request(c, request);
212                                 if(!result)
213                                         return false;
214                                 continue;
215                         } else {
216                                 break;
217                         }
218                 }
219         } while(inlen);
220
221         return true;
222 }