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