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);
63 /* Add our data to buffer */
64 if(c->status.encryptout) {
68 size_t outlen = length;
70 if(!cipher_encrypt(c->outcipher, buffer, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) {
71 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting metadata to %s (%s)",
72 c->name, c->hostname);
77 buffer_add(&c->outbuf, buffer, length);
80 io_set(&c->io, IO_READ | IO_WRITE);
85 void send_meta_raw(connection_t *c, const char *buffer, int length) {
87 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
91 logger(DEBUG_META, LOG_DEBUG, "Sending %d bytes of raw metadata to %s (%s)", length,
92 c->name, c->hostname);
94 buffer_add(&c->outbuf, buffer, length);
96 io_set(&c->io, IO_READ | IO_WRITE);
99 void broadcast_meta(connection_t *from, const char *buffer, int length) {
100 for list_each(connection_t, c, connection_list)
101 if(c != from && c->edge)
102 send_meta(c, buffer, length);
105 bool receive_meta_sptps(void *handle, uint8_t type, const void *vdata, uint16_t length) {
106 const char *data = vdata;
107 connection_t *c = handle;
110 logger(DEBUG_ALWAYS, LOG_ERR, "receive_meta_sptps() called with NULL pointer!");
114 if(type == SPTPS_HANDSHAKE) {
115 if(c->allow_request == ACK)
124 /* Are we receiving a TCPpacket? */
127 if(length != c->tcplen)
129 receive_tcppacket(c, data, length);
134 /* Change newline to null byte, just like non-SPTPS requests */
136 if(data[length - 1] == '\n')
137 ((char *)data)[length - 1] = 0;
139 /* Otherwise we are waiting for a request */
141 return receive_request(c, data);
144 bool receive_meta(connection_t *c) {
146 char inbuf[MAXBUFSIZE];
147 char *bufp = inbuf, *endp;
150 - Read as much as possible from the TCP socket in one go.
152 - Check if a full request is in the input buffer.
153 - If yes, process request and remove it from the buffer,
155 - If not, keep stuff in buffer and exit.
158 buffer_compact(&c->inbuf, MAXBUFSIZE);
160 if(sizeof inbuf <= c->inbuf.len) {
161 logger(DEBUG_ALWAYS, LOG_ERR, "Input buffer full for %s (%s)", c->name, c->hostname);
165 inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 0);
168 if(!inlen || !sockerrno) {
169 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)",
170 c->name, c->hostname);
171 } else if(sockwouldblock(sockerrno))
174 logger(DEBUG_ALWAYS, LOG_ERR, "Metadata socket read error for %s (%s): %s",
175 c->name, c->hostname, sockstrerror(sockerrno));
180 /* Are we receiving a SPTPS packet? */
183 int len = MIN(inlen, c->sptpslen - c->inbuf.len);
184 buffer_add(&c->inbuf, bufp, len);
186 char *sptpspacket = buffer_read(&c->inbuf, c->sptpslen);
190 if(!receive_tcppacket_sptps(c, sptpspacket, c->sptpslen))
199 if(c->protocol_minor >= 2) {
200 int len = sptps_receive_data(&c->sptps, bufp, inlen);
208 if(!c->status.decryptin) {
209 endp = memchr(bufp, '\n', inlen);
215 buffer_add(&c->inbuf, bufp, endp - bufp);
217 inlen -= endp - bufp;
220 #ifdef DISABLE_LEGACY
223 size_t outlen = inlen;
225 if(!cipher_decrypt(c->incipher, bufp, inlen, buffer_prepare(&c->inbuf, inlen), &outlen, false) || inlen != outlen) {
226 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting metadata from %s (%s)",
227 c->name, c->hostname);
235 while(c->inbuf.len) {
236 /* Are we receiving a TCPpacket? */
239 char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen);
244 if(c->outgoing && proxytype == PROXY_SOCKS4 && c->allow_request == ID) {
245 if(tcpbuffer[0] == 0 && tcpbuffer[1] == 0x5a) {
246 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
248 logger(DEBUG_CONNECTIONS, LOG_ERR, "Proxy request rejected");
251 } else if(c->outgoing && proxytype == PROXY_SOCKS5 && c->allow_request == ID) {
252 if(tcpbuffer[0] != 5) {
253 logger(DEBUG_CONNECTIONS, LOG_ERR, "Invalid response from proxy server");
256 if(tcpbuffer[1] == (char)0xff) {
257 logger(DEBUG_CONNECTIONS, LOG_ERR, "Proxy request rejected: unsuitable authentication method");
260 if(tcpbuffer[2] != 5) {
261 logger(DEBUG_CONNECTIONS, LOG_ERR, "Invalid response from proxy server");
264 if(tcpbuffer[3] == 0) {
265 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
267 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request rejected");
271 logger(DEBUG_CONNECTIONS, LOG_ERR, "c->tcplen set but c->node is NULL!");
275 if(c->allow_request == ALL) {
276 receive_tcppacket(c, tcpbuffer, c->tcplen);
278 logger(DEBUG_CONNECTIONS, LOG_ERR, "Got unauthorized TCP packet from %s (%s)", c->name, c->hostname);
286 /* Otherwise we are waiting for a request */
288 char *request = buffer_readline(&c->inbuf);
290 bool result = receive_request(c, request);