2 meta.c -- handle the meta communication
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>,
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "connection.h"
31 bool send_meta_sptps(void *handle, uint8_t type, const char *buffer, size_t length) {
32 connection_t *c = handle;
35 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta_sptps() called with NULL pointer!");
39 buffer_add(&c->outbuf, buffer, length);
40 io_set(&c->io, IO_READ | IO_WRITE);
45 bool send_meta(connection_t *c, const char *buffer, int length) {
47 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
51 logger(DEBUG_META, LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
52 c->name, c->hostname);
54 if(c->protocol_minor >= 2)
55 return sptps_send_record(&c->sptps, 0, buffer, length);
57 /* Add our data to buffer */
58 if(c->status.encryptout) {
59 size_t outlen = length;
61 if(!cipher_encrypt(c->outcipher, buffer, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) {
62 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting metadata to %s (%s)",
63 c->name, c->hostname);
67 buffer_add(&c->outbuf, buffer, length);
70 io_set(&c->io, IO_READ | IO_WRITE);
75 void broadcast_meta(connection_t *from, const char *buffer, int length) {
76 for list_each(connection_t, c, connection_list)
77 if(c != from && c->status.active)
78 send_meta(c, buffer, length);
81 bool receive_meta_sptps(void *handle, uint8_t type, const char *data, uint16_t length) {
82 connection_t *c = handle;
85 logger(DEBUG_ALWAYS, LOG_ERR, "receive_meta_sptps() called with NULL pointer!");
89 if(type == SPTPS_HANDSHAKE) {
90 if(c->allow_request == ACK)
99 /* Are we receiving a TCPpacket? */
102 if(length != c->tcplen)
104 receive_tcppacket(c, data, length);
109 /* Change newline to null byte, just like non-SPTPS requests */
111 if(data[length - 1] == '\n')
112 ((char *)data)[length - 1] = 0;
114 /* Otherwise we are waiting for a request */
116 return receive_request(c, data);
119 bool receive_meta(connection_t *c) {
121 char inbuf[MAXBUFSIZE];
122 char *bufp = inbuf, *endp;
125 - Read as much as possible from the TCP socket in one go.
127 - Check if a full request is in the input buffer.
128 - If yes, process request and remove it from the buffer,
130 - If not, keep stuff in buffer and exit.
133 buffer_compact(&c->inbuf, MAXBUFSIZE);
135 if(sizeof inbuf <= c->inbuf.len) {
136 logger(DEBUG_ALWAYS, LOG_ERR, "Input buffer full for %s (%s)", c->name, c->hostname);
140 inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 0);
143 if(!inlen || !errno) {
144 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)",
145 c->name, c->hostname);
146 } else if(sockwouldblock(sockerrno))
149 logger(DEBUG_ALWAYS, LOG_ERR, "Metadata socket read error for %s (%s): %s",
150 c->name, c->hostname, sockstrerror(sockerrno));
155 if(c->protocol_minor >= 2)
156 return sptps_receive_data(&c->sptps, bufp, inlen);
158 if(!c->status.decryptin) {
159 endp = memchr(bufp, '\n', inlen);
165 buffer_add(&c->inbuf, bufp, endp - bufp);
167 inlen -= endp - bufp;
170 size_t outlen = inlen;
172 if(!cipher_decrypt(c->incipher, bufp, inlen, buffer_prepare(&c->inbuf, inlen), &outlen, false) || inlen != outlen) {
173 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting metadata from %s (%s)",
174 c->name, c->hostname);
181 while(c->inbuf.len) {
182 /* Are we receiving a TCPpacket? */
185 char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen);
190 if(c->outgoing && proxytype == PROXY_SOCKS4 && c->allow_request == ID) {
191 if(tcpbuffer[0] == 0 && tcpbuffer[1] == 0x5a) {
192 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
194 logger(DEBUG_CONNECTIONS, LOG_ERR, "Proxy request rejected");
197 } else if(c->outgoing && proxytype == PROXY_SOCKS5 && c->allow_request == ID) {
198 if(tcpbuffer[0] != 5) {
199 logger(DEBUG_CONNECTIONS, LOG_ERR, "Invalid response from proxy server");
202 if(tcpbuffer[1] == (char)0xff) {
203 logger(DEBUG_CONNECTIONS, LOG_ERR, "Proxy request rejected: unsuitable authentication method");
206 if(tcpbuffer[2] != 5) {
207 logger(DEBUG_CONNECTIONS, LOG_ERR, "Invalid response from proxy server");
210 if(tcpbuffer[3] == 0) {
211 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
213 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request rejected");
217 logger(DEBUG_CONNECTIONS, LOG_ERR, "c->tcplen set but c->node is NULL!");
221 if(c->allow_request == ALL) {
222 receive_tcppacket(c, tcpbuffer, c->tcplen);
224 logger(DEBUG_CONNECTIONS, LOG_ERR, "Got unauthorized TCP packet from %s (%s)", c->name, c->hostname);
232 /* Otherwise we are waiting for a request */
234 char *request = buffer_readline(&c->inbuf);
236 bool result = receive_request(c, request);