2 meta.c -- handle the meta communication
3 Copyright (C) 2000-2014 Guus Sliepen <guus@tinc-vpn.org>,
4 2000-2005 Ivo Timmermans
5 2006 Scott Lamb <slamb@slamb.org>
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.
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.
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.
25 #include "connection.h"
34 #define MIN(x, y) (((x)<(y))?(x):(y))
37 bool send_meta_sptps(void *handle, uint8_t type, const void *buffer, size_t length) {
38 connection_t *c = handle;
41 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta_sptps() called with NULL pointer!");
45 buffer_add(&c->outbuf, buffer, length);
46 io_set(&c->io, IO_READ | IO_WRITE);
51 bool send_meta(connection_t *c, const char *buffer, int length) {
53 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
57 logger(DEBUG_META, LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
58 c->name, c->hostname);
60 if(c->protocol_minor >= 2) {
61 return sptps_send_record(&c->sptps, 0, buffer, length);
64 /* Add our data to buffer */
65 if(c->status.encryptout) {
70 if(length > c->outbudget) {
71 logger(DEBUG_META, LOG_ERR, "Byte limit exceeded for encryption to %s (%s)", c->name, c->hostname);
74 c->outbudget -= length;
77 size_t outlen = length;
79 if(!cipher_encrypt(c->outcipher, buffer, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) {
80 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting metadata to %s (%s)",
81 c->name, c->hostname);
87 buffer_add(&c->outbuf, buffer, length);
90 io_set(&c->io, IO_READ | IO_WRITE);
95 void send_meta_raw(connection_t *c, const char *buffer, int length) {
97 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
101 logger(DEBUG_META, LOG_DEBUG, "Sending %d bytes of raw metadata to %s (%s)", length,
102 c->name, c->hostname);
104 buffer_add(&c->outbuf, buffer, length);
106 io_set(&c->io, IO_READ | IO_WRITE);
109 void broadcast_meta(connection_t *from, const char *buffer, int length) {
110 for list_each(connection_t, c, connection_list)
111 if(c != from && c->edge) {
112 send_meta(c, buffer, length);
116 bool receive_meta_sptps(void *handle, uint8_t type, const void *vdata, uint16_t length) {
117 const char *data = vdata;
118 connection_t *c = handle;
121 logger(DEBUG_ALWAYS, LOG_ERR, "receive_meta_sptps() called with NULL pointer!");
125 if(type == SPTPS_HANDSHAKE) {
126 if(c->allow_request == ACK) {
137 /* Are we receiving a TCPpacket? */
140 if(length != c->tcplen) {
144 receive_tcppacket(c, data, length);
149 /* Change newline to null byte, just like non-SPTPS requests */
151 if(data[length - 1] == '\n') {
152 ((char *)data)[length - 1] = 0;
155 /* Otherwise we are waiting for a request */
157 return receive_request(c, data);
160 bool receive_meta(connection_t *c) {
162 char inbuf[MAXBUFSIZE];
163 char *bufp = inbuf, *endp;
166 - Read as much as possible from the TCP socket in one go.
168 - Check if a full request is in the input buffer.
169 - If yes, process request and remove it from the buffer,
171 - If not, keep stuff in buffer and exit.
174 buffer_compact(&c->inbuf, MAXBUFSIZE);
176 if(sizeof(inbuf) <= c->inbuf.len) {
177 logger(DEBUG_ALWAYS, LOG_ERR, "Input buffer full for %s (%s)", c->name, c->hostname);
181 inlen = recv(c->socket, inbuf, sizeof(inbuf) - c->inbuf.len, 0);
184 if(!inlen || !sockerrno) {
185 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)",
186 c->name, c->hostname);
187 } else if(sockwouldblock(sockerrno)) {
190 logger(DEBUG_ALWAYS, LOG_ERR, "Metadata socket read error for %s (%s): %s",
191 c->name, c->hostname, sockstrerror(sockerrno));
197 /* Are we receiving a SPTPS packet? */
200 int len = MIN(inlen, c->sptpslen - c->inbuf.len);
201 buffer_add(&c->inbuf, bufp, len);
203 char *sptpspacket = buffer_read(&c->inbuf, c->sptpslen);
209 if(!receive_tcppacket_sptps(c, sptpspacket, c->sptpslen)) {
220 if(c->protocol_minor >= 2) {
221 int len = sptps_receive_data(&c->sptps, bufp, inlen);
232 if(!c->status.decryptin) {
233 endp = memchr(bufp, '\n', inlen);
241 buffer_add(&c->inbuf, bufp, endp - bufp);
243 inlen -= endp - bufp;
246 #ifdef DISABLE_LEGACY
250 if(inlen > c->inbudget) {
251 logger(DEBUG_META, LOG_ERR, "yte limit exceeded for decryption from %s (%s)", c->name, c->hostname);
254 c->inbudget -= inlen;
257 size_t outlen = inlen;
259 if(!cipher_decrypt(c->incipher, bufp, inlen, buffer_prepare(&c->inbuf, inlen), &outlen, false) || inlen != outlen) {
260 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting metadata from %s (%s)",
261 c->name, c->hostname);
269 while(c->inbuf.len) {
270 /* Are we receiving a TCPpacket? */
273 char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen);
280 if(c->outgoing && proxytype == PROXY_SOCKS4 && c->allow_request == ID) {
281 if(tcpbuffer[0] == 0 && tcpbuffer[1] == 0x5a) {
282 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
284 logger(DEBUG_CONNECTIONS, LOG_ERR, "Proxy request rejected");
287 } else if(c->outgoing && proxytype == PROXY_SOCKS5 && c->allow_request == ID) {
288 if(tcpbuffer[0] != 5) {
289 logger(DEBUG_CONNECTIONS, LOG_ERR, "Invalid response from proxy server");
293 if(tcpbuffer[1] == (char)0xff) {
294 logger(DEBUG_CONNECTIONS, LOG_ERR, "Proxy request rejected: unsuitable authentication method");
298 if(tcpbuffer[2] != 5) {
299 logger(DEBUG_CONNECTIONS, LOG_ERR, "Invalid response from proxy server");
303 if(tcpbuffer[3] == 0) {
304 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
306 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request rejected");
310 logger(DEBUG_CONNECTIONS, LOG_ERR, "c->tcplen set but c->node is NULL!");
314 if(c->allow_request == ALL) {
315 receive_tcppacket(c, tcpbuffer, c->tcplen);
317 logger(DEBUG_CONNECTIONS, LOG_ERR, "Got unauthorized TCP packet from %s (%s)", c->name, c->hostname);
325 /* Otherwise we are waiting for a request */
327 char *request = buffer_readline(&c->inbuf);
330 bool result = receive_request(c, request);