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"
36 static ssize_t MIN(ssize_t x, ssize_t y) {
41 bool send_meta_sptps(void *handle, uint8_t type, const void *buffer, size_t length) {
43 connection_t *c = handle;
46 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta_sptps() called with NULL pointer!");
50 buffer_add(&c->outbuf, buffer, length);
51 io_set(&c->io, IO_READ | IO_WRITE);
56 bool send_meta(connection_t *c, const void *buffer, size_t length) {
58 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
62 logger(DEBUG_META, LOG_DEBUG, "Sending %lu bytes of metadata to %s (%s)",
63 (unsigned long)length, c->name, c->hostname);
65 if(c->protocol_minor >= 2) {
66 return sptps_send_record(&c->sptps, 0, buffer, length);
69 /* Add our data to buffer */
70 if(c->status.encryptout) {
76 if(!decrease_budget(&c->legacy->out, length)) {
77 logger(DEBUG_META, LOG_ERR, "Byte limit exceeded for encryption to %s (%s)", c->name, c->hostname);
81 size_t outlen = length;
83 if(!cipher_encrypt(&c->legacy->out.cipher, buffer, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) {
84 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting metadata to %s (%s)",
85 c->name, c->hostname);
91 buffer_add(&c->outbuf, buffer, length);
94 io_set(&c->io, IO_READ | IO_WRITE);
99 void send_meta_raw(connection_t *c, const void *buffer, size_t length) {
101 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
105 logger(DEBUG_META, LOG_DEBUG, "Sending %lu bytes of raw metadata to %s (%s)",
106 (unsigned long)length, c->name, c->hostname);
108 buffer_add(&c->outbuf, buffer, length);
110 io_set(&c->io, IO_READ | IO_WRITE);
113 void broadcast_meta(connection_t *from, const char *buffer, size_t length) {
114 for list_each(connection_t, c, &connection_list)
115 if(c != from && c->edge) {
116 send_meta(c, buffer, length);
120 bool receive_meta_sptps(void *handle, uint8_t type, const void *vdata, uint16_t length) {
121 const char *data = vdata;
122 connection_t *c = handle;
125 logger(DEBUG_ALWAYS, LOG_ERR, "receive_meta_sptps() called with NULL pointer!");
129 if(type == SPTPS_HANDSHAKE) {
130 if(c->allow_request == ACK) {
141 /* Are we receiving a TCPpacket? */
144 if(length != c->tcplen) {
148 receive_tcppacket(c, data, length);
153 /* Change newline to null byte, just like non-SPTPS requests */
155 if(data[length - 1] == '\n') {
156 ((char *)data)[length - 1] = 0;
159 /* Otherwise we are waiting for a request */
161 return receive_request(c, data);
164 bool receive_meta(connection_t *c) {
166 char inbuf[MAXBUFSIZE];
167 char *bufp = inbuf, *endp;
170 - Read as much as possible from the TCP socket in one go.
172 - Check if a full request is in the input buffer.
173 - If yes, process request and remove it from the buffer,
175 - If not, keep stuff in buffer and exit.
178 buffer_compact(&c->inbuf, MAXBUFSIZE);
180 if(sizeof(inbuf) <= c->inbuf.len) {
181 logger(DEBUG_ALWAYS, LOG_ERR, "Input buffer full for %s (%s)", c->name, c->hostname);
185 inlen = recv(c->socket, inbuf, sizeof(inbuf) - c->inbuf.len, 0);
188 if(!inlen || !sockerrno) {
189 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)",
190 c->name, c->hostname);
191 } else if(sockwouldblock(sockerrno)) {
194 logger(DEBUG_ALWAYS, LOG_ERR, "Metadata socket read error for %s (%s): %s",
195 c->name, c->hostname, sockstrerror(sockerrno));
201 /* Are we receiving a SPTPS packet? */
204 ssize_t len = MIN(inlen, c->sptpslen - c->inbuf.len);
205 buffer_add(&c->inbuf, bufp, len);
207 char *sptpspacket = buffer_read(&c->inbuf, c->sptpslen);
213 if(!receive_tcppacket_sptps(c, sptpspacket, c->sptpslen)) {
224 if(c->protocol_minor >= 2) {
225 size_t len = sptps_receive_data(&c->sptps, bufp, inlen);
232 inlen -= (ssize_t)len;
236 if(!c->status.decryptin) {
237 endp = memchr(bufp, '\n', inlen);
245 buffer_add(&c->inbuf, bufp, endp - bufp);
247 inlen -= endp - bufp;
250 #ifdef DISABLE_LEGACY
255 if(!decrease_budget(&c->legacy->in, (size_t) inlen)) {
256 logger(DEBUG_META, LOG_ERR, "Byte limit exceeded for decryption from %s (%s)", c->name, c->hostname);
260 size_t outlen = inlen;
262 if(!cipher_decrypt(&c->legacy->in.cipher, bufp, inlen, buffer_prepare(&c->inbuf, inlen), &outlen, false) || (size_t)inlen != outlen) {
263 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting metadata from %s (%s)",
264 c->name, c->hostname);
272 while(c->inbuf.len) {
273 /* Are we receiving a TCPpacket? */
276 char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen);
283 if(c->outgoing && c->allow_request == ID && (proxytype == PROXY_SOCKS4 || proxytype == PROXY_SOCKS5)) {
284 if(!check_socks_resp(proxytype, tcpbuffer, c->tcplen)) {
288 logger(DEBUG_CONNECTIONS, LOG_ERR, "c->tcplen set but c->node is NULL!");
292 if(c->allow_request == ALL) {
293 receive_tcppacket(c, tcpbuffer, c->tcplen);
295 logger(DEBUG_CONNECTIONS, LOG_ERR, "Got unauthorized TCP packet from %s (%s)", c->name, c->hostname);
303 /* Otherwise we are waiting for a request */
305 char *request = buffer_readline(&c->inbuf);
308 bool result = receive_request(c, request);