2 meta.c -- handle the meta communication
3 Copyright (C) 2000-2018 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.
27 #include "connection.h"
35 static ssize_t MIN(ssize_t x, ssize_t y) {
40 bool send_meta_sptps(void *handle, uint8_t type, const void *buffer, size_t length) {
42 connection_t *c = handle;
45 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta_sptps() called with NULL pointer!");
49 buffer_add(&c->outbuf, buffer, length);
50 io_set(&c->io, IO_READ | IO_WRITE);
55 bool send_meta(connection_t *c, const void *buffer, size_t length) {
57 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
61 logger(DEBUG_META, LOG_DEBUG, "Sending %lu bytes of metadata to %s (%s)",
62 (unsigned long)length, c->name, c->hostname);
64 if(c->protocol_minor >= 2) {
65 return sptps_send_record(&c->sptps, 0, buffer, length);
68 /* Add our data to buffer */
69 if(c->status.encryptout) {
75 if(!decrease_budget(&c->legacy->out, length)) {
76 logger(DEBUG_META, LOG_ERR, "Byte limit exceeded for encryption to %s (%s)", c->name, c->hostname);
80 size_t outlen = length;
82 if(!cipher_encrypt(&c->legacy->out.cipher, buffer, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) {
83 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting metadata to %s (%s)",
84 c->name, c->hostname);
90 buffer_add(&c->outbuf, buffer, length);
93 io_set(&c->io, IO_READ | IO_WRITE);
98 void send_meta_raw(connection_t *c, const void *buffer, size_t length) {
100 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
104 logger(DEBUG_META, LOG_DEBUG, "Sending %lu bytes of raw metadata to %s (%s)",
105 (unsigned long)length, c->name, c->hostname);
107 buffer_add(&c->outbuf, buffer, length);
109 io_set(&c->io, IO_READ | IO_WRITE);
112 void broadcast_meta(connection_t *from, const char *buffer, size_t length) {
113 for list_each(connection_t, c, &connection_list)
114 if(c != from && c->edge) {
115 send_meta(c, buffer, length);
119 bool receive_meta_sptps(void *handle, uint8_t type, const void *vdata, uint16_t length) {
120 const char *data = vdata;
121 connection_t *c = handle;
124 logger(DEBUG_ALWAYS, LOG_ERR, "receive_meta_sptps() called with NULL pointer!");
128 if(type == SPTPS_HANDSHAKE) {
129 if(c->allow_request == ACK) {
140 /* Are we receiving a TCPpacket? */
143 if(length != c->tcplen) {
147 receive_tcppacket(c, data, length);
152 /* Change newline to null byte, just like non-SPTPS requests */
154 if(data[length - 1] == '\n') {
155 ((char *)data)[length - 1] = 0;
158 /* Otherwise we are waiting for a request */
160 return receive_request(c, data);
163 bool receive_meta(connection_t *c) {
165 char inbuf[MAXBUFSIZE];
166 char *bufp = inbuf, *endp;
169 - Read as much as possible from the TCP socket in one go.
171 - Check if a full request is in the input buffer.
172 - If yes, process request and remove it from the buffer,
174 - If not, keep stuff in buffer and exit.
177 buffer_compact(&c->inbuf, MAXBUFSIZE);
179 if(sizeof(inbuf) <= c->inbuf.len) {
180 logger(DEBUG_ALWAYS, LOG_ERR, "Input buffer full for %s (%s)", c->name, c->hostname);
184 inlen = recv(c->socket, inbuf, sizeof(inbuf) - c->inbuf.len, 0);
187 if(!inlen || !sockerrno) {
188 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)",
189 c->name, c->hostname);
190 } else if(sockwouldblock(sockerrno)) {
193 logger(DEBUG_ALWAYS, LOG_ERR, "Metadata socket read error for %s (%s): %s",
194 c->name, c->hostname, sockstrerror(sockerrno));
200 /* Are we receiving a SPTPS packet? */
203 ssize_t len = MIN(inlen, c->sptpslen - c->inbuf.len);
204 buffer_add(&c->inbuf, bufp, len);
206 char *sptpspacket = buffer_read(&c->inbuf, c->sptpslen);
212 if(!receive_tcppacket_sptps(c, sptpspacket, c->sptpslen)) {
223 if(c->protocol_minor >= 2) {
224 size_t len = sptps_receive_data(&c->sptps, bufp, inlen);
231 inlen -= (ssize_t)len;
235 if(!c->status.decryptin) {
236 endp = memchr(bufp, '\n', inlen);
244 buffer_add(&c->inbuf, bufp, endp - bufp);
246 inlen -= endp - bufp;
249 #ifdef DISABLE_LEGACY
254 if(!decrease_budget(&c->legacy->in, (size_t) inlen)) {
255 logger(DEBUG_META, LOG_ERR, "Byte limit exceeded for decryption from %s (%s)", c->name, c->hostname);
259 size_t outlen = inlen;
261 if(!cipher_decrypt(&c->legacy->in.cipher, bufp, inlen, buffer_prepare(&c->inbuf, inlen), &outlen, false) || (size_t)inlen != outlen) {
262 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting metadata from %s (%s)",
263 c->name, c->hostname);
271 while(c->inbuf.len) {
272 /* Are we receiving a TCPpacket? */
275 char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen);
282 if(c->outgoing && proxytype == PROXY_SOCKS4 && c->allow_request == ID) {
283 if(tcpbuffer[0] == 0 && tcpbuffer[1] == 0x5a) {
284 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
286 logger(DEBUG_CONNECTIONS, LOG_ERR, "Proxy request rejected");
289 } else if(c->outgoing && proxytype == PROXY_SOCKS5 && c->allow_request == ID) {
290 if(tcpbuffer[0] != 5) {
291 logger(DEBUG_CONNECTIONS, LOG_ERR, "Invalid response from proxy server");
295 if(tcpbuffer[1] == (char)0xff) {
296 logger(DEBUG_CONNECTIONS, LOG_ERR, "Proxy request rejected: unsuitable authentication method");
300 if(tcpbuffer[2] != 5) {
301 logger(DEBUG_CONNECTIONS, LOG_ERR, "Invalid response from proxy server");
305 if(tcpbuffer[3] == 0) {
306 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
308 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request rejected");
312 logger(DEBUG_CONNECTIONS, LOG_ERR, "c->tcplen set but c->node is NULL!");
316 if(c->allow_request == ALL) {
317 receive_tcppacket(c, tcpbuffer, c->tcplen);
319 logger(DEBUG_CONNECTIONS, LOG_ERR, "Got unauthorized TCP packet from %s (%s)", c->name, c->hostname);
327 /* Otherwise we are waiting for a request */
329 char *request = buffer_readline(&c->inbuf);
332 bool result = receive_request(c, request);